>From a2eec38bd2a90771745a4ba9cd8baad1ce2e6c36 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Mon, 9 Sep 2013 17:16:22 +0200 Subject: [PATCH] Added rtnl_addr_get_index() to get cached objects based on ifindex This is a simpler variant of rtnl_addr_get_index(), where it only uses the ifindex reference to lookup an address cache object. Signed-off-by: David Sommerseth --- include/netlink/route/addr.h | 2 ++ lib/route/addr.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/netlink/route/addr.h b/include/netlink/route/addr.h index 56c12e7..16af6db 100644 --- a/include/netlink/route/addr.h +++ b/include/netlink/route/addr.h @@ -32,6 +32,8 @@ extern void rtnl_addr_put(struct rtnl_addr *); extern int rtnl_addr_alloc_cache(struct nl_sock *, struct nl_cache **); extern struct rtnl_addr * rtnl_addr_get(struct nl_cache *, int, struct nl_addr *); +extern struct rtnl_addr * + rtnl_addr_get_index(struct nl_cache *, int); extern int rtnl_addr_build_add_request(struct rtnl_addr *, int, struct nl_msg **); diff --git a/lib/route/addr.c b/lib/route/addr.c index 71fca94..e8332cd 100644 --- a/lib/route/addr.c +++ b/lib/route/addr.c @@ -542,6 +542,36 @@ struct rtnl_addr *rtnl_addr_get(struct nl_cache *cache, int ifindex, return NULL; } +/** + * Get address in cache based on ifindex + * @arg cache Address cache + * @arg ifindex Interface index of address + * + * Searches address cache previously allocated with rtnl_addr_alloc_cache() + * for the given ifindex + * + * The reference counter is incremented before returning the address, therefore + * the reference must be given back with rtnl_addr_put() after usage. + * + * @return Address object or NULL if no match was found. + */ +struct rtnl_addr *rtnl_addr_get_index(struct nl_cache *cache, int ifindex) +{ + struct rtnl_addr *a; + + if (cache->c_ops != &rtnl_addr_ops) + return NULL; + + nl_list_for_each_entry(a, &cache->c_items, ce_list) { + if (ifindex && a->a_ifindex == ifindex) { + nl_object_get((struct nl_object *) a); + return a; + } + } + + return NULL; +} + /** @} */ static int build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags, -- 1.8.3.1