Skip to content

Commit

Permalink
Fix getifaddrs()
Browse files Browse the repository at this point in the history
Names were not properly terminated

Fixes #72
  • Loading branch information
th-otto committed Jun 28, 2024
1 parent b73c576 commit 12d2585
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion socket/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int __getifaddrs(struct ifaddrs **ifap)
/* Now copy the information we already have from SIOCGIFCONF. */
storage->ia.ifa_name = names;
strcpy(names, ifr->ifr_name);
names += strlen(names);
names += strlen(names) + 1;
storage->addr = ifr->ifr_addr;
storage->ia.ifa_addr = &storage->addr;

Expand Down
38 changes: 38 additions & 0 deletions socket/test-ifaddrs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <ifaddrs.h>
#include <net/if.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void)
{
struct ifaddrs *ifap;
struct ifaddrs *ifa;
struct sockaddr_in *sa;
char *addr;

if (getifaddrs(&ifap) == -1)
{
perror("getifaddrs");
return 1;
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
{
sa = (struct sockaddr_in *) ifa->ifa_addr;
addr = inet_ntoa(sa->sin_addr);
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
}

}

freeifaddrs(ifap);
return 0;
}

0 comments on commit 12d2585

Please sign in to comment.