Get all available ip address in linux kernel

Guibin(Bill) Tian gbtian at gmail.com
Thu Jan 9 16:56:18 EST 2014


Does anybody know how to get all the ip addresses from linux kernel? I can
google some code that runs in user mode. But not in kernel.

    s = socket(AF_INET, SOCK_STREAM, 0);
    if(s < 0)
    {
       printf("socket screwup\n");
       exit(1);
    }
    memset(&ifr, 0, sizeof(ifr));
    ifc.ifc_len = sizeof(buff);
    ifc.ifc_buf = buff;
    if(ioctl(s, SIOCGIFCONF, &ifc) < 0)
    {
        printf("SIOCGIFCONF screwed up\n");
        exit(1);
    }
    pifr = (struct ifreq *)(ifc.ifc_req);
    for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; pifr++)
    {
        strcpy(ifr.ifr_name, pifr->ifr_name);    //eth0 eth1 ...
        if(ioctl(s, SIOCGIFADDR, &ifr) < 0)
        {
            printf("ip screwed up\n");
            exit(1);
        }
        memcpy(ip, ifr.ifr_addr.sa_data+2, 4);
        printf("%s\n",inet_ntoa(*(struct in_addr *)ip));
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140109/395a304e/attachment.html 


More information about the Kernelnewbies mailing list