1 #ifdef HAVE_CONFIG_H 2 #include "config.h" 3 #endif 4 5 #include <stdlib.h> 6 #include <sys/types.h> 7 #include <sys/socket.h> 8 #include <netinet/in.h> 9 #include <arpa/inet.h> 10 #include <netdb.h> 11 12 #include <pcap.h> 13 14 static int ifprint(pcap_if_t *d); 15 static char *iptos(bpf_u_int32 in); 16 17 int main(int argc, char **argv) 18 { 19 pcap_if_t *alldevs; 20 pcap_if_t *d; 21 char *s; 22 bpf_u_int32 net, mask; 23 int exit_status = 0; 24 25 char errbuf[PCAP_ERRBUF_SIZE+1]; 26 if (pcap_findalldevs(&alldevs, errbuf) == -1) 27 { 28 fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); 29 exit(1); 30 } 31 for(d=alldevs;d;d=d->next) 32 { 33 if (!ifprint(d)) 34 exit_status = 2; 35 } 36 37 if ( (s = pcap_lookupdev(errbuf)) == NULL) 38 { 39 fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf); 40 exit_status = 2; 41 } 42 else 43 { 44 printf("Preferred device name: %s\n",s); 45 } 46 47 if (pcap_lookupnet(s, &net, &mask, errbuf) < 0) 48 { 49 fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf); 50 exit_status = 2; 51 } 52 else 53 { 54 printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask)); 55 } 56 57 exit(exit_status); 58 } 59 60 static int ifprint(pcap_if_t *d) 61 { 62 pcap_addr_t *a; 63 #ifdef INET6 64 char ntop_buf[INET6_ADDRSTRLEN]; 65 #endif 66 int status = 1; /* success */ 67 68 printf("%s\n",d->name); 69 if (d->description) 70 printf("\tDescription: %s\n",d->description); 71 printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no"); 72 73 for(a=d->addresses;a;a=a->next) { 74 if (a->addr != NULL) 75 switch(a->addr->sa_family) { 76 case AF_INET: 77 printf("\tAddress Family: AF_INET\n"); 78 if (a->addr) 79 printf("\t\tAddress: %s\n", 80 inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr)); 81 if (a->netmask) 82 printf("\t\tNetmask: %s\n", 83 inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr)); 84 if (a->broadaddr) 85 printf("\t\tBroadcast Address: %s\n", 86 inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr)); 87 if (a->dstaddr) 88 printf("\t\tDestination Address: %s\n", 89 inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr)); 90 break; 91 #ifdef INET6 92 case AF_INET6: 93 printf("\tAddress Family: AF_INET6\n"); 94 if (a->addr) 95 printf("\t\tAddress: %s\n", 96 inet_ntop(AF_INET6, 97 ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr, 98 ntop_buf, sizeof ntop_buf)); 99 if (a->netmask) 100 printf("\t\tNetmask: %s\n", 101 inet_ntop(AF_INET6, 102 ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr, 103 ntop_buf, sizeof ntop_buf)); 104 if (a->broadaddr) 105 printf("\t\tBroadcast Address: %s\n", 106 inet_ntop(AF_INET6, 107 ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr, 108 ntop_buf, sizeof ntop_buf)); 109 if (a->dstaddr) 110 printf("\t\tDestination Address: %s\n", 111 inet_ntop(AF_INET6, 112 ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr, 113 ntop_buf, sizeof ntop_buf)); 114 break; 115 #endif 116 default: 117 printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family); 118 break; 119 } 120 else 121 { 122 fprintf(stderr, "\tWarning: a->addr is NULL, skipping this address.\n"); 123 status = 0; 124 } 125 } 126 printf("\n"); 127 return status; 128 } 129 130 /* From tcptraceroute */ 131 #define IPTOSBUFFERS 12 132 static char *iptos(bpf_u_int32 in) 133 { 134 static char output[IPTOSBUFFERS][3*4+3+1]; 135 static short which; 136 u_char *p; 137 138 p = (u_char *)∈ 139 which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1); 140 sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); 141 return output[which]; 142 } 143