static struct rtnl_nexthop *
nm_netlink_get_nh (struct rtnl_route * route)
{
	int hops;

	hops = rtnl_route_get_nnexthops (route);
	g_return_val_if_fail(hops > 0, NULL);
	return rtnl_route_nexthop_n (route, 0);
}
Beispiel #2
0
static void get_route(struct nl_object *obj, void *arg)
{
	struct rtnl_route *route = (struct rtnl_route *) obj;
	struct rtnl_nexthop * nexthop = rtnl_route_nexthop_n(route, 0);
	struct nl_addr *naddr = rtnl_route_nh_get_gateway(nexthop);

	if (NULL != naddr)
		printf("gwip is 0x%x\n", *(uint32_t *) (nl_addr_get_binary_addr(naddr)));
	
}
Beispiel #3
0
static void match_first_nh_gw(struct nl_object *obj, void *arg)
{
    static int found = 0;
    struct rtnl_route *route = (struct rtnl_route *)obj;
    struct nl_addr **gw = (struct nl_addr **)arg;

    if (found)
        return;

    struct rtnl_nexthop *nh = rtnl_route_nexthop_n(route, 0);
    if (nh == NULL)
        return;
    *gw = rtnl_route_nh_get_gateway(nh);
    nl_addr_get(*gw); /* Found, keep reference */
    found = 1;
}
Beispiel #4
0
CAMLprim value
ocaml_get_routing_table(value unit) {
    CAMLparam1(unit);
    CAMLlocal3( ret, tmp, entry );

    struct nl_sock *fd;
    struct nl_cache *res, *links;
    struct rtnl_route *it;
    uint32 i_ip, netmask = 0, mask_len, gw;
    int i;
    struct nl_addr *ip;
    char device_name[IFNAMSIZ];
    struct rtnl_nexthop *to;
    fd = nl_socket_alloc();
    if (!fd) {
        fprintf(stderr, "error nl_socket_alloc\n");
        exit(1);
    }

    if(nl_connect(fd, NETLINK_ROUTE) < 0) {
        fprintf(stderr, "error nl_connect\n");
        exit(1);
    }

    ret = Val_emptylist;

    if(rtnl_route_alloc_cache(fd, AF_UNSPEC, 0, &res) < 0) {
        fprintf(stderr, "error rtnl_route_alloc_cache");
        exit(1);
    }

    if(rtnl_link_alloc_cache (fd, AF_UNSPEC, &links) < 0) {
        fprintf(stderr, "error rtnl_link_alloc_cache");
        exit(1);
    }

    it = (struct rtnl_route *)nl_cache_get_first(res);
    for(; it != NULL; it = (struct rtnl_route *)
                           nl_cache_get_next((struct nl_object *)it) ) {
        if(rtnl_route_get_family (it) == AF_INET) {
            ip = rtnl_route_get_dst(it);
            i_ip = ntohl(*(int *)nl_addr_get_binary_addr(ip));
            mask_len = nl_addr_get_prefixlen(ip);
            for(i = 0; i < 32; i++)
                netmask = (netmask << 1) + (i< mask_len?1:0);
            to = rtnl_route_nexthop_n(it, 0);
            rtnl_link_i2name(links, rtnl_route_nh_get_ifindex(to),
                             device_name, IFNAMSIZ);
            if ( rtnl_route_nh_get_gateway (to) != NULL)
                gw = ntohl(*(int *)nl_addr_get_binary_addr(
                               rtnl_route_nh_get_gateway (to)));
            else
                gw = 0;
            /*printf("src ip:%x mask:%x gw:%x dev:%s\n", i_ip, netmask, */
            /*gw, device_name);*/

            entry = caml_alloc(7,0);
            Store_field(entry, 0, Val_int(i_ip & 0xFFFF));
            Store_field(entry, 1, Val_int(i_ip >> 16));
            Store_field(entry, 2, Val_int(netmask & 0xFFFF));
            Store_field(entry, 3, Val_int(netmask >> 16));
            Store_field(entry, 4, Val_int(gw & 0xFFFF));
            Store_field(entry, 5, Val_int(gw >> 16));
            Store_field(entry, 6, caml_copy_string(device_name));

            // store in list
            tmp =  caml_alloc(2, 0);
            Store_field( tmp, 0, entry);  // head
            Store_field( tmp, 1, ret);  // tail
            ret = tmp;
        }
    }