Last active
May 19, 2021 20:46
-
-
Save azat/cc667d145bc74215c40cdbb69f38189b to your computer and use it in GitHub Desktop.
This file contains 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
/// Based on https://bugzilla.redhat.com/show_bug.cgi?id=178986#c1 | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <ifaddrs.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <net/if.h> | |
void print_addr(const char *prefix, struct sockaddr *sa) | |
{ | |
char addr[128]; | |
if (!sa) | |
return; | |
printf("(%s)\t(%s) ", prefix, inet_ntop( | |
sa->sa_family, | |
sa->sa_family == AF_INET | |
? (void*)&(((struct sockaddr_in*)sa)->sin_addr) | |
: (void*)&(((struct sockaddr_in6*)sa)->sin6_addr), | |
&(addr[0]), | |
128 | |
)); | |
} | |
int main(int argc, char **argv, char **envp) | |
{ | |
struct ifaddrs *ifa = NULL, *ifap = NULL; | |
if ((getifaddrs( &ifap ) != 0) || (!ifap)) { | |
fprintf(stderr,"getifaddrs failed: %s\n", strerror(errno)); | |
return 1; | |
} | |
for (ifa = ifap; ifa != 0L; ifa = ifa->ifa_next) { | |
printf("%s\t(flags=%s)\t", | |
ifa->ifa_name, | |
ifa->ifa_flags & IFF_POINTOPOINT ? "IFF_POINTOPOINT" : "IFF_BROADCAST" | |
); | |
print_addr("ifa_addr", ifa->ifa_addr); | |
print_addr("ifu_dstaddr", ifa->ifa_ifu.ifu_dstaddr); | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment