Netlink Implementation.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

	I am trying very hard to understand the userspace view of Netlink sockets. 
However, my programming is not suffiecient enough to fully understand the man 
pages or the source(iproute2/zebra) and work off of that. I have tried many 
times but there seems no good documentation or implementation to help me. I 
have found this implementation written by someone on LKML 2 years ago, 
however the code produces no output. If I can get the code to produce some 
and the expected output im sure I can work and understand the code from 
there. However, because I did not write this code nor do I fully understand 
netlink sockets I am at a loss of what to do. Can someone explain to me if 
this code is correct for the 2.6.x series kernel? Furthermore, the author did 
not define replybuf nor byes nor netlink_fd so I have guessed as to their 
types. Ideally in the future Id like to use netlink to gather interface 
information and modify routing tables, however without a solid base to learn 
and adapt, I am at a loss. 

	Any help is appreciated. Thanks.

    //File descriptor
    int netlink_sk;
    
    ssize_t bytes;
    
    struct nlmsghdr *replybuf;
    struct nlmsghdr buffer;
    replybuf = &buffer;
    
    //This is the netlink message itself.
    struct {
        struct nlmsghdr hdr;
        struct ifinfomsg info;
    } msg;
    
    // This contains the info neccesary to bind the netlink socket 
    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 it
    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 = 0;

    /* 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 = (ifinfomsg *)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;
            (hdr) += hdr->nlmsg_len;
        }
        
    } 
 
----------------------------------------
EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

		--Alan Cox 2000-12-08 

----------------------------------------
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux