7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 static void ifprint(pcap_if_t *d);
15 static char *iptos(bpf_u_int32 in);
17 int main(int argc, char **argv)
22 bpf_u_int32 net, mask;
24 char errbuf[PCAP_ERRBUF_SIZE+1];
25 if (pcap_findalldevs(&alldevs, errbuf) == -1)
27 fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
30 for(d=alldevs;d;d=d->next)
35 if ( (s = pcap_lookupdev(errbuf)) == NULL)
37 fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf);
41 printf("Preferred device name: %s\n",s);
44 if (pcap_lookupnet(s, &net, &mask, errbuf) < 0)
46 fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
50 printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
56 static void ifprint(pcap_if_t *d)
60 char ntop_buf[INET6_ADDRSTRLEN];
63 printf("%s\n",d->name);
65 printf("\tDescription: %s\n",d->description);
66 printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
68 for(a=d->addresses;a;a=a->next) {
69 switch(a->addr->sa_family)
72 printf("\tAddress Family: AF_INET\n");
74 printf("\t\tAddress: %s\n",
75 inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr));
77 printf("\t\tNetmask: %s\n",
78 inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr));
80 printf("\t\tBroadcast Address: %s\n",
81 inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr));
83 printf("\t\tDestination Address: %s\n",
84 inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr));
88 printf("\tAddress Family: AF_INET6\n");
90 printf("\t\tAddress: %s\n",
92 ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr,
93 ntop_buf, sizeof ntop_buf));
95 printf("\t\tNetmask: %s\n",
97 ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr,
98 ntop_buf, sizeof ntop_buf));
100 printf("\t\tBroadcast Address: %s\n",
102 ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr,
103 ntop_buf, sizeof ntop_buf));
105 printf("\t\tDestination Address: %s\n",
107 ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr,
108 ntop_buf, sizeof ntop_buf));
112 printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
119 /* From tcptraceroute */
120 #define IPTOSBUFFERS 12
121 static char *iptos(bpf_u_int32 in)
123 static char output[IPTOSBUFFERS][3*4+3+1];
128 which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
129 sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
130 return output[which];