#include <stdio.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <arpa/inet.h> #define MAX_INTERFACE 64 int main(int argc, char const *argv[]) { printf("hello world!\n"); showifs(); return 0; } int showifs() { int i; int rc; int sock; int ifnum; struct ifreq ifr[MAX_INTERFACE]; // struct arpreq arp; struct ifconf ifc; sock = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if ( sock == -1 ) { return -1; } ifc.ifc_len = sizeof( ifr ); ifc.ifc_buf = ( caddr_t ) ifr; rc = ioctl ( sock, SIOCGIFCONF, &ifc ); if ( rc == -1 ) { close( sock ); return -1; } ifnum = ifc.ifc_len / sizeof ( struct ifreq ); fprintf( stdout, "ifnum=[%d]\n======================================\n", ifnum ); for ( i = 0; i < ifnum; i ++ ) { fprintf( stdout, "ifname%d [%s]\n", i, ifr[i].ifr_name ); //ip rc = ioctl ( sock, SIOCGIFADDR, ifr + i ); if ( rc != -1 ) { fprintf( stdout, "IP=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_addr ) )->sin_addr ) ); } //netmask rc = ioctl ( sock, SIOCGIFNETMASK, ifr + i ); if ( rc != -1 ) { fprintf( stdout, "NETMASK=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_netmask ) )->sin_addr ) ); } //broadcast rc = ioctl ( sock, SIOCGIFBRDADDR, ifr + i ); if ( rc != -1 ) { fprintf( stdout, "BROADCAST=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_broadaddr ) )->sin_addr ) ); } //mac rc = ioctl ( sock, SIOCGIFHWADDR, ifr + i ); if ( rc != -1 ) { fprintf( stdout, "%02x:%02x:%02x:%02x:%02x:%02x\n", ( unsigned char )ifr[i].ifr_hwaddr.sa_data[0], ( unsigned char )ifr[i].ifr_hwaddr.sa_data[1], ( unsigned char )ifr[i].ifr_hwaddr.sa_data[2], ( unsigned char )ifr[i].ifr_hwaddr.sa_data[3], ( unsigned char )ifr[i].ifr_hwaddr.sa_data[4], ( unsigned char )ifr[i].ifr_hwaddr.sa_data[5] ); } } close( sock ); return 0; }