Created
January 28, 2013 05:39
-
-
Save wolfg1969/4653302 to your computer and use it in GitHub Desktop.
getifaddrs, freeifaddrs - get interface addresses - http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <ifaddrs.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
struct ifaddrs *ifaddr, *ifa; | |
int family, s; | |
char host[NI_MAXHOST]; | |
if (getifaddrs(&ifaddr) == -1) { | |
perror("getifaddrs"); | |
exit(EXIT_FAILURE); | |
} | |
/* Walk through linked list, maintaining head pointer so we can free list later */ | |
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | |
if (ifa->ifa_addr == NULL) | |
continue; | |
family = ifa->ifa_addr->sa_family; | |
/* Display interface name and family (including symbolic form of the latter for the common families) */ | |
printf("%s address family: %d%s\n", | |
ifa->ifa_name, family, | |
(family == AF_PACKET) ? " (AF_PACKET)" : | |
(family == AF_INET) ? " (AF_INET)" : | |
(family == AF_INET6) ? " (AF_INET6)" : ""); | |
/* For an AF_INET* interface address, display the address */ | |
if (family == AF_INET || family == AF_INET6) { | |
s = getnameinfo(ifa->ifa_addr, | |
(family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), | |
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); | |
if (s != 0) { | |
printf("getnameinfo() failed: %s\n", gai_strerror(s)); | |
exit(EXIT_FAILURE); | |
} | |
printf("\taddress: <%s>\n", host); | |
} | |
} | |
freeifaddrs(ifaddr); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment