Using nl_cache_subset()

Pedro Francisco pedrogfrancisco at gmail.com
Tue Mar 30 06:45:58 EDT 2010


I'm trying to extract from an addr_cache only the IP belonging to a certain 
interface.
I'm trying not to use a callback for no particular reason and I assumed 
nl_cache_subset() would do the trick.

It apparently doesn't. The output is pasted below and the code is at the 
bottom or at http://paste.info.tm/241 :

-----------------------------------8<-----------------------------------8<-----------------------------------
nl_cache_nitems(subset_cache) = 1
--------------------------------------
original addr_cache dump-to-screen:
127.0.0.1/8 inet dev lo scope host <permanent>
192.168.1.1/24 inet dev eth0 scope universe <permanent>
SOME_OTHER_IP peer 10.64.64.64 inet dev ppp0 scope universe <permanent>
::1 inet6 dev lo scope host <permanent>
--------------------------------------


--------------------------------------
subset_cache dump-to-screen:
none inet dev eth0 scope universe <permanent>
-----------------------------------8<-----------------------------------8<-----------------------------------

Note that after applying the nl_cache_subset() the IP of eth0 is "none".

What am I missing here?
Thanks in Advance,
--
Pedro


Code at http://paste.info.tm/241 (1 month expiry) or below if the URL is no 
longer available:

-----------------------------------8<-----------------------------------8<-----------------------------------
#include <stdio.h>
#include <assert.h>

#include <netlink/route/addr.h>
#include <netlink/route/link.h>


int main(int argc, char *argv[])
{
    struct nl_handle *nlh = NULL;
    struct nl_cache *link_cache = NULL;
    struct nl_cache *addr_cache = NULL;
    struct nl_cache *subset_cache = NULL;
    struct rtnl_addr *addr = NULL;
    struct nl_dump_params params;

    int err = 1;

    nlh = nl_handle_alloc();
    if (!nlh)
	return -1;

    addr = rtnl_addr_alloc();
    if (!addr)
	return -1;

    if (nl_connect(nlh, NETLINK_ROUTE) < 0)
	goto errout_rtnl_addr;


    link_cache = rtnl_link_alloc_cache(nlh);
    if (!link_cache)
	goto errout_handler;

    nl_cache_mngt_provide(link_cache);
    
    addr_cache = rtnl_addr_alloc_cache(nlh);
    if (!addr_cache)
	goto errout_link_cache;

    rtnl_addr_set_ifindex(addr, if_nametoindex("eth0"));

    subset_cache = nl_cache_subset(addr_cache, (struct nl_object *) addr);
    if (!subset_cache)
	goto errout_addr_cache;

    printf("nl_cache_nitems(subset_cache) = %d\n", 
nl_cache_nitems(subset_cache));
    assert(nl_cache_is_empty(subset_cache)!=1);

    params.dp_type = NL_DUMP_BRIEF;
    params.dp_prefix = 30;
    params.dp_print_index = 1;
    params.dp_dump_msgtype = 1;
    params.dp_cb = NULL;
    params.dp_nl_cb = NULL;
    params.dp_data = NULL;
    params.dp_fd = stdout;
    params.dp_buf = NULL;
    params.dp_buflen = 0;

    printf("--------------------------------------\n");
    printf("original addr_cache dump-to-screen:\n");
    nl_cache_dump (addr_cache, &params);
    printf("--------------------------------------\n\n\n");


    printf("--------------------------------------\n");
    printf("subset_cache dump-to-screen:\n");
    nl_cache_dump (subset_cache, &params);
    printf("--------------------------------------\n\n\n");

    err = 0;


    errout_subset_cache:
	nl_cache_free(subset_cache);
    errout_addr_cache:
	nl_cache_free(addr_cache);
    errout_link_cache:
	nl_cache_free(link_cache);
    errout_handler:
	nl_close(nlh);
    errout_rtnl_addr:
	rtnl_addr_put(addr);
    errout:
	return err;
}
-----------------------------------8<-----------------------------------8<-----------------------------------



More information about the libnl mailing list