Last active
December 4, 2022 02:59
-
-
Save kayoch1n/6b68080442ae3d909a5ecd78587cfd76 to your computer and use it in GitHub Desktop.
getifaddrs on *nix
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
// gcc getifaddrs_linux.c -o getifaddrs_linux | |
// ./getifaddrs_linux | |
// https://stackoverflow.com/a/12878352 | |
#include <stdlib.h> //exit() | |
#include <stdio.h> //printf(),perror() | |
#include <string.h> //memset | |
#include <ifaddrs.h> //struct ifaddrs,getifaddrs() | |
#include <arpa/inet.h> //INET6_ADDRSTRLEN,inet_ntop() | |
#include <netinet/in.h> //sockaddr_in,sockaddr_in6 | |
#include <linux/if_packet.h> | |
// #include <net/ethernet.h> | |
void die(char * s) | |
{ | |
perror(s); | |
exit(1); | |
} | |
void print_all_ip() | |
{ | |
struct ifaddrs *ifaddr, *ifa; | |
char ip_str[INET6_ADDRSTRLEN]; | |
if (getifaddrs(&ifaddr) < 0) { | |
die("getifaddrs"); | |
} | |
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | |
if (ifa->ifa_addr == NULL) { continue; } | |
switch (ifa->ifa_addr->sa_family) { | |
case AF_INET: { | |
struct sockaddr_in *a = (struct sockaddr_in *)ifa->ifa_addr; | |
inet_ntop(AF_INET, &(a->sin_addr), ip_str, INET_ADDRSTRLEN); | |
uint32_t n = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr; | |
int i = 0; | |
while (n > 0) { | |
if (n & 1) i++; | |
n = n >> 1; | |
} | |
printf("IPv4 %s: %s/%d\n", ifa->ifa_name, ip_str, i); | |
break; | |
} | |
case AF_INET6: { | |
struct sockaddr_in6 *a = (struct sockaddr_in6 *)ifa->ifa_addr; | |
inet_ntop(AF_INET6, &(a->sin6_addr), ip_str, INET6_ADDRSTRLEN); | |
unsigned char *c = ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr.s6_addr; | |
int i = 0, j = 0; | |
unsigned char n = 0; | |
while (i < 16) { | |
n = c[i]; | |
while (n > 0) { | |
if (n & 1) j++; | |
n = n/2; | |
} | |
i++; | |
} | |
printf("IPv6 %s: %s/%d\n", ifa->ifa_name, ip_str, j); | |
break; | |
} | |
case AF_PACKET: { | |
struct sockaddr_ll *a=(struct sockaddr_ll *)ifa->ifa_addr; | |
unsigned char* ptr = (unsigned char*)a->sll_addr; | |
printf("MAC %s: %02x:%02x:%02x:%02x:%02x:%02x\n", | |
ifa->ifa_name, | |
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); | |
} | |
default: | |
break; | |
} | |
} | |
freeifaddrs(ifaddr); | |
return; | |
} | |
int main() | |
{ | |
print_all_ip(); | |
exit(0); | |
} |
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
// gcc getifaddrs_macos.c -o getifaddrs_macos | |
// ./getifaddrs_macos | |
// https://stackoverflow.com/a/12878352 | |
#include <stdlib.h> //exit() | |
#include <stdio.h> //printf(),perror() | |
#include <string.h> //memset | |
#include <ifaddrs.h> //struct ifaddrs,getifaddrs() | |
#include <arpa/inet.h> //INET6_ADDRSTRLEN,inet_ntop() | |
#include <netinet/in.h> //sockaddr_in,sockaddr_in6 | |
// #include <linux/if_packet.h> | |
#include <net/if_dl.h> | |
// #include <net/ethernet.h> | |
void die(char * s) | |
{ | |
perror(s); | |
exit(1); | |
} | |
void print_all_ip() | |
{ | |
struct ifaddrs *ifaddr, *ifa; | |
char ip_str[INET6_ADDRSTRLEN]; | |
if (getifaddrs(&ifaddr) < 0) { | |
die("getifaddrs"); | |
} | |
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | |
if (ifa->ifa_addr == NULL) { continue; } | |
switch (ifa->ifa_addr->sa_family) { | |
case AF_INET: { | |
struct sockaddr_in *a = (struct sockaddr_in *)ifa->ifa_addr; | |
inet_ntop(AF_INET, &(a->sin_addr), ip_str, INET_ADDRSTRLEN); | |
uint32_t n = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr; | |
int i = 0; | |
while (n > 0) { | |
if (n & 1) i++; | |
n = n >> 1; | |
} | |
printf("IPv4 %s: %s/%d\n", ifa->ifa_name, ip_str, i); | |
break; | |
} | |
case AF_INET6: { | |
struct sockaddr_in6 *a = (struct sockaddr_in6 *)ifa->ifa_addr; | |
inet_ntop(AF_INET6, &(a->sin6_addr), ip_str, INET6_ADDRSTRLEN); | |
unsigned char *c = ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr.s6_addr; | |
int i = 0, j = 0; | |
unsigned char n = 0; | |
while (i < 16) { | |
n = c[i]; | |
while (n > 0) { | |
if (n & 1) j++; | |
n = n/2; | |
} | |
i++; | |
} | |
printf("IPv6 %s: %s/%d\n", ifa->ifa_name, ip_str, j); | |
break; | |
} | |
case AF_LINK: { // AF_PACKET for linux | |
// struct sockaddr_ll *a=(struct sockaddr_ll *)ifa->ifa_addr; | |
// unsigned char* ptr = (unsigned char*)a->sll_addr; | |
struct sockaddr_dl* a = (struct sockaddr_dl*)ifa->ifa_addr; | |
unsigned char* ptr=(unsigned char*)LLADDR(a); | |
printf("MAC %s: %02x:%02x:%02x:%02x:%02x:%02x\n", | |
ifa->ifa_name, | |
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); | |
} | |
default: | |
break; | |
} | |
} | |
freeifaddrs(ifaddr); | |
return; | |
} | |
int main() | |
{ | |
print_all_ip(); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment