1 #define MAX_INTERFACE 64 2 3 int showifs() 4 { 5 int i; 6 int rc; 7 int sock; 8 int ifnum; 9 struct ifreq ifr[MAX_INTERFACE]; 10 // struct arpreq arp; 11 struct ifconf ifc; 12 13 sock = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); 14 if ( sock == -1 ) 15 { 16 return -1; 17 } 18 19 ifc.ifc_len = sizeof( ifr ); 20 ifc.ifc_buf = ( caddr_t ) ifr; 21 rc = ioctl ( sock, SIOCGIFCONF, &ifc ); 22 if ( rc == -1 ) 23 { 24 close( sock ); 25 return -1; 26 } 27 28 ifnum = ifc.ifc_len / sizeof ( struct ifreq ); 29 fprintf( stdout, "ifnum=[%d]\n======================================\n", ifnum ); 30 for ( i = 0; i < ifnum; i ++ ) 31 { 32 fprintf( stdout, "ifname [%s]\n", ifr[i].ifr_name ); 33 34 //status 35 rc = ioctl( sock, SIOCGIFFLAGS, ifr + i ); 36 if ( rc != -1 ) 37 { 38 if ( ifr[i].ifr_flags & IFF_PROMISC ) 39 { 40 fprintf( stdout, "PROMISC=TRUE\n" ); 41 } 42 else 43 { 44 fprintf( stdout, "PROMISC=FALSE\n" ); 45 } 46 47 48 if ( ifr[i].ifr_flags & IFF_UP ) 49 { 50 fprintf( stdout, "UP=TRUE\n" ); 51 } 52 else 53 { 54 fprintf( stdout, "UP=FALSE\n" ); 55 } 56 } 57 58 //ip 59 rc = ioctl ( sock, SIOCGIFADDR, ifr + i ); 60 if ( rc != -1 ) 61 { 62 fprintf( stdout, "IP=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_addr ) )->sin_addr ) ); 63 } 64 65 //netmask 66 rc = ioctl ( sock, SIOCGIFNETMASK, ifr + i ); 67 if ( rc != -1 ) 68 { 69 fprintf( stdout, "NETMASK=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_netmask ) )->sin_addr ) ); 70 } 71 72 //broadcast 73 rc = ioctl ( sock, SIOCGIFBRDADDR, ifr + i ); 74 if ( rc != -1 ) 75 { 76 fprintf( stdout, "BROADCAST=%s\n", inet_ntoa( ( ( struct sockaddr_in* )( &ifr[i].ifr_broadaddr ) )->sin_addr ) ); 77 } 78 79 //mac 80 rc = ioctl ( sock, SIOCGIFHWADDR, ifr + i ); 81 if ( rc != -1 ) 82 { 83 fprintf( stdout, "%02x:%02x:%02x:%02x:%02x:%02x\n", 84 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[0], 85 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[1], 86 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[2], 87 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[3], 88 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[4], 89 ( unsigned char )ifr[i].ifr_hwaddr.sa_data[5] ); 90 } 91 } 92 93 close( sock ); 94 return 0; 95 }
时间: 2024-12-19 13:11:53