Get all available ip address in linux kernel
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)); }
participants (1)
-
Guibin(Bill) Tian