From d36d396fd0ae94aa2269546a02b5973b4ec27775 Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Thu, 11 Oct 2007 23:09:49 +0200 Subject: [PATCH] Cache message type association interface cleanups --- include/netlink/cache.h | 7 +-- lib/cache_mngt.c | 114 +++++++++++++++++----------------------- lib/msg.c | 24 +++++---- src/nl-list-caches.c | 2 +- 4 files changed, 68 insertions(+), 79 deletions(-) diff --git a/include/netlink/cache.h b/include/netlink/cache.h index 5f31d8e..cb7741b 100644 --- a/include/netlink/cache.h +++ b/include/netlink/cache.h @@ -16,13 +16,13 @@ #include #include #include +#include #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 *); diff --git a/lib/cache_mngt.c b/lib/cache_mngt.c index 7cf1a03..de2bf24 100644 --- a/lib/cache_mngt.c +++ b/lib/cache_mngt.c @@ -22,65 +22,7 @@ static struct nl_cache_ops *cache_ops; /** - * Associate a message type to a set of cache operations - * @arg protocol netlink protocol - * @arg message_type netlink message type - * - * Associates the specified netlink message type with - * a registered set of cache operations. - * - * @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) -{ - 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 && - ops->co_protocol == protocol) - return ops; - - return NULL; -} - -/** - * 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. - * - * Converts a message type to a character string and stores it in the - * provided buffer. - * - * @return The destination buffer or the message type encoded in - * hexidecimal form if no match was found. - */ -char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int protocol, - int msgtype, char *buf, size_t len) -{ - 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 + * @name Cache Operations Sets * @{ */ @@ -103,12 +45,52 @@ struct nl_cache_ops *nl_cache_ops_lookup(const char *name) } /** - * Lookupt the set of cache operations responsible for a type of object - * @arg obj_ops Object operations + * Associate a message type to a set of cache operations + * @arg protocol netlink protocol + * @arg msgtype netlink message type * - * @return The cache operations or NULL if not found. + * Associates the specified netlink message type with + * a registered set of cache operations. + * + * @return The cache operations or NULL if no association + * could be made. */ -struct nl_cache_ops *nl_cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops) +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 == msgtype && + ops->co_protocol == protocol) + return ops; + + return NULL; +} + +/** + * Lookup message type cache association + * @arg ops cache operations + * @arg msgtype netlink message type + * + * Searches for a matching message type association ing the specified + * cache operations. + * + * @return A message type association or NULL. + */ +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) + return &ops->co_msgtypes[i]; + + return NULL; +} + +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) diff --git a/lib/msg.c b/lib/msg.c index caae744..d19ac08 100644 --- 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; diff --git a/src/nl-list-caches.c b/src/nl-list-caches.c index db71d6d..42a2546 100644 --- a/src/nl-list-caches.c +++ b/src/nl-list-caches.c @@ -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; }