]> git.ipfire.org Git - thirdparty/libnl.git/commitdiff
Cache message type association interface cleanups
authorThomas Graf <tgraf@suug.ch>
Thu, 11 Oct 2007 21:09:49 +0000 (23:09 +0200)
committerThomas Graf <tgraf@suug.ch>
Thu, 11 Oct 2007 21:09:49 +0000 (23:09 +0200)
include/netlink/cache.h
lib/cache_mngt.c
lib/msg.c
src/nl-list-caches.c

index 5f31d8e6dba96f3319ebee5a9291b970ae871a7c..cb7741bff0121f6b95601ae47c0e73c912434226 100644 (file)
 #include <netlink/msg.h>
 #include <netlink/utils.h>
 #include <netlink/object.h>
+#include <netlink/cache-api.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 struct nl_cache;
-struct nl_cache_ops;
 
 typedef void (*change_func_t)(struct nl_cache *, struct nl_object *, int);
 
@@ -91,8 +91,9 @@ extern void                   nl_cache_foreach_filter(struct nl_cache *,
 
 /* Cache type management */
 extern struct nl_cache_ops *   nl_cache_ops_lookup(const char *);
-extern struct nl_cache_ops *   nl_cache_ops_lookup_for_obj(struct nl_object_ops *);
-extern void                    nl_cache_mngt_foreach(void (*cb)(struct nl_cache_ops *, void *), void *);
+extern struct nl_cache_ops *   nl_cache_ops_associate(int, int);
+extern struct nl_msgtype *     nl_msgtype_lookup(struct nl_cache_ops *, int);
+extern void                    nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *);
 extern int                     nl_cache_mngt_register(struct nl_cache_ops *);
 extern int                     nl_cache_mngt_unregister(struct nl_cache_ops *);
 
index 7cf1a0306491e0620806057b6a6dfeb3b09393dc..de2bf243409d404a8430d213a3b1128c70a311cd 100644 (file)
 
 static struct nl_cache_ops *cache_ops;
 
+/**
+ * @name Cache Operations Sets
+ * @{
+ */
+
+/**
+ * Lookup the set cache operations of a certain cache type
+ * @arg name           name of the cache type
+ *
+ * @return The cache operations or NULL if no operations
+ *         have been registered under the specified name.
+ */
+struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
+{
+       struct nl_cache_ops *ops;
+
+       for (ops = cache_ops; ops; ops = ops->co_next)
+               if (!strcmp(ops->co_name, name))
+                       return ops;
+
+       return NULL;
+}
+
 /**
  * Associate a message type to a set of cache operations
  * @arg protocol               netlink protocol
- * @arg message_type           netlink message type
+ * @arg msgtype                        netlink message type
  *
  * Associates the specified netlink message type with
  * a registered set of cache operations.
@@ -32,14 +55,14 @@ static struct nl_cache_ops *cache_ops;
  * @return The cache operations or NULL if no association
  *         could be made.
  */
-struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
+struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
 {
        int i;
        struct nl_cache_ops *ops;
 
        for (ops = cache_ops; ops; ops = ops->co_next)
                for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
-                       if (ops->co_msgtypes[i].mt_id == message_type &&
+                       if (ops->co_msgtypes[i].mt_id == msgtype &&
                            ops->co_protocol == protocol)
                                return ops;
 
@@ -47,68 +70,27 @@ struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
 }
 
 /**
- * Convert message type to character string.
- * @arg ops            Cache operations.
- * @arg protocol       Netlink Protocol.
- * @arg msgtype                Message type.
- * @arg buf            Destination buffer.
- * @arg len            Size of destination buffer.
+ * Lookup message type cache association
+ * @arg ops                    cache operations
+ * @arg msgtype                        netlink message type
  *
- * Converts a message type to a character string and stores it in the
- * provided buffer.
+ * Searches for a matching message type association ing the specified
+ * cache operations.
  *
- * @return The destination buffer or the message type encoded in
- *         hexidecimal form if no match was found.
+ * @return A message type association or NULL.
  */
-char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int protocol,
-                             int msgtype, char *buf, size_t len)
+struct nl_msgtype *nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
 {
        int i;
 
-       for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
-               if (ops->co_msgtypes[i].mt_id == msgtype &&
-                   ops->co_protocol == protocol) {
-                       snprintf(buf, len, "%s::%s",
-                                ops->co_name,
-                                ops->co_msgtypes[i].mt_name);
-                       return buf;
-               }
-       }
-
-       snprintf(buf, len, "%d:%s->0x%x()", protocol, ops->co_name, msgtype);
-       return buf;
-}
-
-/**
- * @name Cache Type Management
- * @{
- */
-
-/**
- * Lookup the set cache operations of a certain cache type
- * @arg name           name of the cache type
- *
- * @return The cache operations or NULL if no operations
- *         have been registered under the specified name.
- */
-struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
-{
-       struct nl_cache_ops *ops;
-
-       for (ops = cache_ops; ops; ops = ops->co_next)
-               if (!strcmp(ops->co_name, name))
-                       return ops;
+       for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
+               if (ops->co_msgtypes[i].mt_id == msgtype)
+                       return &ops->co_msgtypes[i];
 
        return NULL;
 }
 
-/**
- * Lookupt the set of cache operations responsible for a type of object
- * @arg obj_ops                Object operations
- *
- * @return The cache operations or NULL if not found.
- */
-struct nl_cache_ops *nl_cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
+static struct nl_cache_ops *cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
 {
        struct nl_cache_ops *ops;
 
@@ -125,7 +107,7 @@ struct nl_cache_ops *nl_cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
  * @arg cb             Callback function to be called
  * @arg arg            User specific argument.
  */
-void nl_cache_mngt_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
+void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
 {
        struct nl_cache_ops *ops;
 
@@ -208,7 +190,7 @@ void nl_cache_mngt_provide(struct nl_cache *cache)
 {
        struct nl_cache_ops *ops;
 
-       ops = nl_cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
+       ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
        if (!ops)
                BUG();
        else
@@ -227,7 +209,7 @@ void nl_cache_mngt_unprovide(struct nl_cache *cache)
 {
        struct nl_cache_ops *ops;
 
-       ops = nl_cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
+       ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
        if (!ops)
                BUG();
        else if (ops->co_major_cache == cache)
index caae744bdaeffba2f7b1941ba168873ffd5488f2..d19ac08572c05fdda39301021185a2a74e3b17a2 100644 (file)
--- a/lib/msg.c
+++ b/lib/msg.c
@@ -752,8 +752,8 @@ int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
                .arg = arg,
        };
 
-       ops = nl_cache_mngt_associate(nlmsg_get_proto(msg),
-                                     nlmsg_hdr(msg)->nlmsg_type);
+       ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
+                                    nlmsg_hdr(msg)->nlmsg_type);
        if (ops == NULL)
                return nl_error(ENOENT, "Unknown message type %d",
                                nlmsg_hdr(msg)->nlmsg_type);
@@ -815,16 +815,22 @@ static void print_hdr(FILE *ofd, struct nl_msg *msg)
 {
        struct nlmsghdr *nlh = nlmsg_hdr(msg);
        struct nl_cache_ops *ops;
+       struct nl_msgtype *mt;
        char buf[128];
 
        fprintf(ofd, "    .nlmsg_len = %d\n", nlh->nlmsg_len);
 
-       ops = nl_cache_mngt_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
+       ops = nl_cache_ops_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
+       if (ops) {
+               mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
+               if (!mt)
+                       BUG();
 
-       fprintf(ofd, "    .nlmsg_type = %d <%s>\n", nlh->nlmsg_type,
-               ops ? nl_cache_mngt_type2name(ops, msg->nm_protocol,
-                                             nlh->nlmsg_type, buf, sizeof(buf))
-               : nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf)));
+               snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
+       } else
+               nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
+
+       fprintf(ofd, "    .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
        fprintf(ofd, "    .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
                nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
        fprintf(ofd, "    .nlmsg_seq = %d\n", nlh->nlmsg_seq);
@@ -901,8 +907,8 @@ void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
                int payloadlen = nlmsg_len(hdr);
                int attrlen = 0;
 
-               ops = nl_cache_mngt_associate(nlmsg_get_proto(msg),
-                                             hdr->nlmsg_type);
+               ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
+                                            hdr->nlmsg_type);
                if (ops) {
                        attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
                        payloadlen -= attrlen;
index db71d6df75a8b346f59c0f6ac58a5cdc0f4052e9..42a2546ff7272ad8a8e7db575ac06a1cd4e8fc62 100644 (file)
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
        if (argc > 1 && !strcasecmp(argv[1], "-h"))
                print_usage();
 
-       nl_cache_mngt_foreach(print, NULL);
+       nl_cache_ops_foreach(print, NULL);
 
        return 0;
 }