<div dir="ltr">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.<div><br></div><div><div> s = socket(AF_INET, SOCK_STREAM, 0);</div><div>
if(s < 0)</div><div> {</div><div> printf("socket screwup\n");</div><div> exit(1);</div><div> }</div><div> memset(&ifr, 0, sizeof(ifr));</div><div> ifc.ifc_len = sizeof(buff);</div>
<div> ifc.ifc_buf = buff;</div><div> if(ioctl(s, SIOCGIFCONF, &ifc) < 0)</div><div> {</div><div> printf("SIOCGIFCONF screwed up\n");</div><div> exit(1);</div><div> }</div><div> pifr = (struct ifreq *)(ifc.ifc_req);</div>
<div> for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; pifr++)</div><div> {</div><div> strcpy(ifr.ifr_name, pifr->ifr_name); //eth0 eth1 ...</div><div> if(ioctl(s, SIOCGIFADDR, &ifr) < 0)</div>
<div> {</div><div> printf("ip screwed up\n");</div><div> exit(1);</div><div> }</div><div> memcpy(ip, ifr.ifr_addr.sa_data+2, 4);</div><div> printf("%s\n",inet_ntoa(*(struct in_addr *)ip));</div>
<div> }</div></div></div>