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