libnl 1.1

lib/route/cls_api.c

00001 /*
00002  * lib/route/cls_api.c       Classifier Module API
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup cls
00014  * @defgroup cls_api Classifier Modules
00015  * @{
00016  */
00017 
00018 #include <netlink-local.h>
00019 #include <netlink-tc.h>
00020 #include <netlink/netlink.h>
00021 #include <netlink/utils.h>
00022 #include <netlink/route/tc.h>
00023 #include <netlink/route/classifier.h>
00024 #include <netlink/route/classifier-modules.h>
00025 #include <netlink/route/link.h>
00026 
00027 static struct rtnl_cls_ops *cls_ops_list;
00028 
00029 /**
00030  * @name Classifier Module API
00031  * @{
00032  */
00033 
00034 /**
00035  * Register a classifier module
00036  * @arg cops            classifier module operations
00037  */
00038 int rtnl_cls_register(struct rtnl_cls_ops *cops)
00039 {
00040         struct rtnl_cls_ops *o, **op;
00041 
00042         if (!cops->co_kind)
00043                 BUG();
00044 
00045         for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
00046                 if (!strcasecmp(cops->co_kind, o->co_kind))
00047                         return nl_errno(EEXIST);
00048 
00049         cops->co_next = NULL;
00050         *op = cops;
00051 
00052         return 0;
00053 }
00054 
00055 /**
00056  * Unregister a classifier module
00057  * @arg cops            classifier module operations
00058  */
00059 int rtnl_cls_unregister(struct rtnl_cls_ops *cops)
00060 {
00061         struct rtnl_cls_ops *o, **op;
00062 
00063         for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
00064                 if (!strcasecmp(cops->co_kind, o->co_kind))
00065                         break;
00066 
00067         if (!o)
00068                 return nl_errno(ENOENT);
00069 
00070         *op = cops->co_next;
00071 
00072         return 0;
00073 }
00074 
00075 struct rtnl_cls_ops *__rtnl_cls_lookup_ops(const char *kind)
00076 {
00077         struct rtnl_cls_ops *cops;
00078 
00079         for (cops = cls_ops_list; cops; cops = cops->co_next)
00080                 if (!strcmp(kind, cops->co_kind))
00081                         return cops;
00082 
00083         return NULL;
00084 }
00085 
00086 /**
00087  * Lookup classifier operations for a classifier object
00088  * @arg cls             Classifier object.
00089  *
00090  * @return Classifier operations or NULL if not found.
00091  */
00092 struct rtnl_cls_ops *rtnl_cls_lookup_ops(struct rtnl_cls *cls)
00093 {
00094         if (!cls->c_ops)
00095                 cls->c_ops = __rtnl_cls_lookup_ops(cls->c_kind);
00096 
00097         return cls->c_ops;
00098 }
00099 
00100 
00101 /** @} */
00102 
00103 /** @} */