Re: netlink documentation (was: of ethernet names)

From: Paul Menage (pmenage@ensim.com)
Date: Mon Jun 10 2002 - 19:06:52 EST


>But I can't even follow enough of iproute (or zebra, which also uses netlink,
>AFAICT) to figure out how to do basic stuff like a list of configured
>networking devices, or set the default route.

E.g. to get the list of devices (untested, lacking error checking, etc),
use something like:

    struct {
        struct nlmsghdr hdr;
        struct ifinfomsg info;
    } msg;
    struct sockaddr_nl addr;

    /* Create and bind the netlink socket */
    netlink_sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);

    memset(&addr, 0, sizeof(addr));
    
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = RTMGRP_IPV4_IFADDR;

    bind(netlink_sk, (struct sockaddr *)&addr, sizeof(addr)));

    /* Build the netlink request */
    memset(&msg, 0, sizeof(msg));
    msg.hdr.nlmsg_len = sizeof(msg);
    msg.hdr.nlmsg_type = RTM_GETLINK;
    msg.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
    msg.hdr.nlmsg_pid = getpid();
    msg.hdr.nlmsg_seq = 0;

    msg.info.ifi_family = AF_UNSPEC;
    msg.info.ifi_type = 0;
    msg.info.ifi_index = 0;
    msg.info.ifi_change = -1;

    /* Send the message */
    send(netlink_sk, &msg, msg.hdr.nlmsg_len, 0);

    /* Loop, as we might get replies spread over several packets */
    while((bytes = recv(netlink_sk, replybuf, sizeof(replybuf), 0))) {
        struct nlmsghdr *hdr = (struct nlmsghdr *)replybuf;

        if(hdr->nlmsg_type == NLMSG_DONE) {
            break;
        }
        
        /* Loop over the messages in this packet */
        while(bytes) {
            int len = hdr->nlmsg_len;

            struct ifinfomsg *info = NLMSG_DATA(hdr);
            struct rtattr *rta = IFLA_RTA(info);
            
            
            len -= NLMSG_LENGTH(sizeof(*info));
            
            /* Loop over the attributes in this message */
            while(RTA_OK(rta, len)) {
                switch(rta->rta_type) {
                case IFLA_IFNAME:
                    printf("Found device %s - %u\n",
                           RTA_DATA(rta), info->ifi_index);

                    break;
                }
                
                rta = RTA_NEXT(rta, len);
            }
            
            bytes -= hdr->nlmsg_len;
            ((void *)hdr) += hdr->nlmsg_len;
        }
        
    }

Paul

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Jun 15 2002 - 22:00:19 EST