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

Query on Netlink sockets on debian etch

Hello Debian forum,

This is Sathya and am presently using debian etch platform as my development.

I am using program which creates a netlink sockets and registers for a IPv4 address notifications on a interface.

#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>

int main()
    struct sockaddr_nl addr;
    int nls,len,rtl;
    char buffer[4096];
    struct nlmsghdr *nlh;
    struct ifaddrmsg *ifa;
    struct rtattr *rth;

    if ((nls = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)   perror
("socket failure\n");

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

    if (bind(nls, (struct sockaddr *)&addr, sizeof(addr)) == -1)    perror
("bind failure\n");

    nlh = (struct nlmsghdr *)buffer;
    while ((len = recv (nls,nlh,4096,0)) > 0)
        for (;(NLMSG_OK (nlh, len)) && (nlh->nlmsg_type != NLMSG_DONE); nlh =
NLMSG_NEXT(nlh, len))
            if (nlh->nlmsg_type != RTM_NEWADDR) continue; /* some other kind of
announcement */

            ifa = (struct ifaddrmsg *) NLMSG_DATA (nlh);

            rth = IFA_RTA (ifa);
            rtl = IFA_PAYLOAD (nlh);
            for (;rtl && RTA_OK (rth, rtl); rth = RTA_NEXT (rth,rtl))
                char name[IFNAMSIZ];
                uint32_t ipaddr;

                if (rth->rta_type != IFA_LOCAL) continue;

                ipaddr = * ((uint32_t *)RTA_DATA(rth));
                ipaddr = htonl(ipaddr);

                fprintf (stdout,"%s is now %X rta_type:%d\n",if_indextoname(ifa->ifa_index,name),ipaddr, rth->rta_type);

But, when I reconfigure my network interface with the below ifconfig command, I am shocked that the above netlink program is giving me two notifications with RTM_NEWADDR (new address being added to an interface)

I am not sure why the second ifconfig command gives me the two RTM_NEWADDR netlink notifications?

ifconfig eth0 inet 0
ifconfig eth0 inet netmask

To analyse further on this, I then changed the second ifconfig to as in below(removed the netmask part),

ifconfig eth0 inet

Now I am getting only one RTM_NEWADDR netlink notification from the kernel.(although the netmask and broadcast address as been assigned to the default address by the kernel).

Could you please help me out by answering why with my prior netlink command the debian kernel was triggering  two RTM_NEWADDR notifications.

Could you please let me know if this is a problem with debian's netlink socket implementation.

Please let me know if you have any concerns.

Thanks and regards,

(P.S: As I am a newbie to this, I was not sure to which forum would be appropriate for this. So, posted to all these relavent forum Sorry for bothering.)

Reply to: