libnl/lib/route/cls_api.c
Thomas Graf 8a3efffa5b Thread-safe error handling
In order for the interface to become more thread safe, the error
handling was revised to no longer depend on a static errno and
error string buffer.

This patch converts all error paths to return a libnl specific
error code which can be translated to a error message using
nl_geterror(int error). The functions nl_error() and
nl_get_errno() are therefore obsolete.

This change required various sets of function prototypes to be
changed in order to return an error code, the most prominent
are:

    struct nl_cache *foo_alloc_cache(...);
changed to:
    int foo_alloc_cache(..., struct nl_cache **);

    struct nl_msg *foo_build_request(...);
changed to:
    int foo_build_request(..., struct nl_msg **);

    struct foo *foo_parse(...);
changed to:
    int foo_parse(..., struct foo **);

This pretty much only leaves trivial allocation functions to
still return a pointer object which can still return NULL to
signal out of memory.

This change is a serious API and ABI breaker, sorry!
2008-05-14 17:49:44 +02:00

103 lines
2 KiB
C

/*
* lib/route/cls_api.c Classifier Module API
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup cls
* @defgroup cls_api Classifier Modules
* @{
*/
#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/tc.h>
#include <netlink/route/classifier.h>
#include <netlink/route/classifier-modules.h>
#include <netlink/route/link.h>
static struct rtnl_cls_ops *cls_ops_list;
/**
* @name Classifier Module API
* @{
*/
/**
* Register a classifier module
* @arg cops classifier module operations
*/
int rtnl_cls_register(struct rtnl_cls_ops *cops)
{
struct rtnl_cls_ops *o, **op;
if (!cops->co_kind)
BUG();
for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
if (!strcasecmp(cops->co_kind, o->co_kind))
return -NLE_EXIST;
cops->co_next = NULL;
*op = cops;
return 0;
}
/**
* Unregister a classifier module
* @arg cops classifier module operations
*/
int rtnl_cls_unregister(struct rtnl_cls_ops *cops)
{
struct rtnl_cls_ops *o, **op;
for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
if (!strcasecmp(cops->co_kind, o->co_kind))
break;
if (!o)
return -NLE_OBJ_NOTFOUND;
*op = cops->co_next;
return 0;
}
struct rtnl_cls_ops *__rtnl_cls_lookup_ops(const char *kind)
{
struct rtnl_cls_ops *cops;
for (cops = cls_ops_list; cops; cops = cops->co_next)
if (!strcmp(kind, cops->co_kind))
return cops;
return NULL;
}
/**
* Lookup classifier operations for a classifier object
* @arg cls Classifier object.
*
* @return Classifier operations or NULL if not found.
*/
struct rtnl_cls_ops *rtnl_cls_lookup_ops(struct rtnl_cls *cls)
{
if (!cls->c_ops)
cls->c_ops = __rtnl_cls_lookup_ops(cls->c_kind);
return cls->c_ops;
}
/** @} */
/** @} */