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