mirror of
https://github.com/fluencelabs/musl
synced 2025-06-26 13:12:03 +00:00
getifaddrs: use if_nameindex to enumerate interfaces
This commit is contained in:
@ -106,31 +106,18 @@ static void dealwithipv6(stor **list, stor** head)
|
|||||||
|
|
||||||
int getifaddrs(struct ifaddrs **ifap)
|
int getifaddrs(struct ifaddrs **ifap)
|
||||||
{
|
{
|
||||||
FILE* f = fopen("/proc/net/dev", "r");
|
|
||||||
if(!f) return -1;
|
|
||||||
|
|
||||||
/* the alternative to parsing /proc.. seems to be iterating
|
|
||||||
through the interfaces using an index number in ifreq.ifr_ifindex
|
|
||||||
until we get some error code back. the kernel will fill ifr_name field
|
|
||||||
for valid ifindices (SIOCGIFINDEX) */
|
|
||||||
stor *list = 0, *head = 0;
|
stor *list = 0, *head = 0;
|
||||||
|
struct if_nameindex* ii = if_nameindex();
|
||||||
char* line; char linebuf[512];
|
if(!ii) return -1;
|
||||||
while((line = fgets(linebuf, sizeof linebuf, f))) {
|
size_t i;
|
||||||
while(isspace(*line) && *line) line++;
|
for(i = 0; ii[i].if_index || ii[i].if_name; i++) {
|
||||||
char* start = line;
|
stor* curr = list_add(&list, &head, ii[i].if_name);
|
||||||
while(*line && isalnum(*line)) line++;
|
if(!curr) {
|
||||||
if(line > start && *line == ':') {
|
if_freenameindex(ii);
|
||||||
// found interface
|
goto err2;
|
||||||
*line = 0;
|
|
||||||
stor* curr = list_add(&list, &head, start);
|
|
||||||
if(!curr) {
|
|
||||||
fclose(f);
|
|
||||||
goto err2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(f);
|
if_freenameindex(ii);
|
||||||
|
|
||||||
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
if(sock == -1) goto err2;
|
if(sock == -1) goto err2;
|
||||||
@ -139,7 +126,6 @@ int getifaddrs(struct ifaddrs **ifap)
|
|||||||
if(-1 == ioctl(sock, SIOCGIFCONF, &conf)) goto err;
|
if(-1 == ioctl(sock, SIOCGIFCONF, &conf)) goto err;
|
||||||
size_t reqitems = conf.ifc_len / sizeof(struct ifreq);
|
size_t reqitems = conf.ifc_len / sizeof(struct ifreq);
|
||||||
for(head = list; head; head = (stor*)head->next) {
|
for(head = list; head; head = (stor*)head->next) {
|
||||||
size_t i;
|
|
||||||
for(i = 0; i < reqitems; i++) {
|
for(i = 0; i < reqitems; i++) {
|
||||||
// get SIOCGIFADDR of active interfaces.
|
// get SIOCGIFADDR of active interfaces.
|
||||||
if(!strcmp(reqs[i].ifr_name, head->name)) {
|
if(!strcmp(reqs[i].ifr_name, head->name)) {
|
||||||
|
Reference in New Issue
Block a user