From 39648eeaf08ca5e8b12626362fc3dcbe4ae59193 Mon Sep 17 00:00:00 2001 From: Przemyslaw Czarnota Date: Mon, 23 Jul 2018 15:42:15 +0200 Subject: [06/17] link: add rtnl_link_foreach Adds rtnl_link_foreach, which allows to iterate over all network interfaces --- include/netlink/route/link.h | 3 +++ lib/route/link.c | 25 +++++++++++++++++++++++++ libnl-route-3.sym | 1 + 3 files changed, 29 insertions(+) diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h index 5f941d5..21cf2af 100644 --- a/include/netlink/route/link.h +++ b/include/netlink/route/link.h @@ -116,6 +116,9 @@ extern int rtnl_link_alloc_cache_flags(struct nl_sock *, int, extern struct rtnl_link *rtnl_link_get(struct nl_cache *, int); extern struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *, const char *); +extern void rtnl_link_foreach(struct nl_cache *, + void (*callback)(struct rtnl_link *, void *), + void *); extern int rtnl_link_build_add_request(struct rtnl_link *, int, struct nl_msg **); diff --git a/lib/route/link.c b/lib/route/link.c index 4840c23..2379521 100644 --- a/lib/route/link.c +++ b/lib/route/link.c @@ -1331,6 +1331,31 @@ struct rtnl_link *rtnl_link_get_by_name(struct nl_cache *cache, return NULL; } +/** + * Iterate over all entries in link cache + * @arg cache Link cache + * @arg callback Callback called for each link + * @arg ctx Callback context + * + * Iterates over all links in link cache. Calls callback for each link + * + * @route_doc{link_list, Iterate over all Links} + * @see rtnl_link_get_by_name() + */ +void rtnl_link_foreach(struct nl_cache *cache, + void (*callback)(struct rtnl_link *link, void *ctx), + void *ctx) +{ + struct rtnl_link *link; + + if (cache->c_ops != &rtnl_link_ops) + return; + + nl_list_for_each_entry(link, &cache->c_items, ce_list) { + callback(link, ctx); + } +} + /** * Construct RTM_GETLINK netlink message * @arg ifindex Interface index diff --git a/libnl-route-3.sym b/libnl-route-3.sym index 0d54d66..361a03c 100644 --- a/libnl-route-3.sym +++ b/libnl-route-3.sym @@ -280,6 +280,7 @@ global: rtnl_link_enslave_ifindex; rtnl_link_fill_info; rtnl_link_flags2str; + rtnl_link_foreach; rtnl_link_gem_alloc; rtnl_link_gem_get_id; rtnl_link_gem_get_index; -- 2.17.1