Get default route

alexfilth186 at ukr.net alexfilth186 at ukr.net
Wed Jun 16 09:52:06 EDT 2010


Hi all.
I am writing app with libnl (Ubuntu 10.04, libnl 1.1.5).

I need to get default route from route table. 
There is my code:

    struct nl_addr *src_addr = NULL;
    struct nl_cache *route_cache;
    struct rtnl_route *route;


    struct nl_handle *nlh = get_default_handle ();
    if(!nlh) {
        return false;
    }

    u32_t ip4_src = 0;

    route_cache = rtnl_route_alloc_cache(nlh);

    route = rtnl_route_alloc();
    src_addr = nl_addr_build(AF_INET, &ip4_src, sizeof (ip4_src));

    rtnl_route_set_scope(route, RT_SCOPE_UNIVERSE);
    rtnl_route_set_family(route, AF_INET);
    rtnl_route_set_table (route, RT_TABLE_MAIN); 

    //rtnl_route_set_dst(route, src_addr); // <--- this line

    nl_cache_foreach_filter(route_cache, (struct nl_object *)route, hword, NULL);

If line rtnl_route_set_dst(route, src_addr); commented, callback function hword called one time and obtained data was default gateway.
When I uncommented this line - no matches found.  
There is part of route table:
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0         192.168.16.1    0.0.0.0         UG    0      0        0 eth0

Is there another way to get default route?



More information about the libnl mailing list