Skip to content

Commit 12d2585

Browse files
committed
Fix getifaddrs()
Names were not properly terminated Fixes #72
1 parent b73c576 commit 12d2585

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

socket/ifaddrs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int __getifaddrs(struct ifaddrs **ifap)
112112
/* Now copy the information we already have from SIOCGIFCONF. */
113113
storage->ia.ifa_name = names;
114114
strcpy(names, ifr->ifr_name);
115-
names += strlen(names);
115+
names += strlen(names) + 1;
116116
storage->addr = ifr->ifr_addr;
117117
storage->ia.ifa_addr = &storage->addr;
118118

socket/test-ifaddrs.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <ifaddrs.h>
2+
#include <net/if.h>
3+
#include <stdio.h>
4+
#include <sys/socket.h>
5+
#include <sys/ioctl.h>
6+
#include <unistd.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <errno.h>
10+
#include <netinet/in.h>
11+
#include <arpa/inet.h>
12+
13+
int main(void)
14+
{
15+
struct ifaddrs *ifap;
16+
struct ifaddrs *ifa;
17+
struct sockaddr_in *sa;
18+
char *addr;
19+
20+
if (getifaddrs(&ifap) == -1)
21+
{
22+
perror("getifaddrs");
23+
return 1;
24+
}
25+
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
26+
{
27+
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
28+
{
29+
sa = (struct sockaddr_in *) ifa->ifa_addr;
30+
addr = inet_ntoa(sa->sin_addr);
31+
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
32+
}
33+
34+
}
35+
36+
freeifaddrs(ifap);
37+
return 0;
38+
}

0 commit comments

Comments
 (0)