• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
main(int argc,char ** argv)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 
ifprint(pcap_if_t * d)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   const char *sep;
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("\tFlags: ");
77   sep = "";
78   if (d->flags & PCAP_IF_UP) {
79     printf("%sUP", sep);
80     sep = ", ";
81   }
82   if (d->flags & PCAP_IF_RUNNING) {
83     printf("%sRUNNING", sep);
84     sep = ", ";
85   }
86   if (d->flags & PCAP_IF_LOOPBACK) {
87     printf("%sLOOPBACK", sep);
88     sep = ", ";
89   }
90   printf("\n");
91 
92   for(a=d->addresses;a;a=a->next) {
93     if (a->addr != NULL)
94       switch(a->addr->sa_family) {
95       case AF_INET:
96         printf("\tAddress Family: AF_INET\n");
97         if (a->addr)
98           printf("\t\tAddress: %s\n",
99             inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr));
100         if (a->netmask)
101           printf("\t\tNetmask: %s\n",
102             inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr));
103         if (a->broadaddr)
104           printf("\t\tBroadcast Address: %s\n",
105             inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr));
106         if (a->dstaddr)
107           printf("\t\tDestination Address: %s\n",
108             inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr));
109         break;
110 #ifdef INET6
111       case AF_INET6:
112         printf("\tAddress Family: AF_INET6\n");
113         if (a->addr)
114           printf("\t\tAddress: %s\n",
115             inet_ntop(AF_INET6,
116                ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr,
117                ntop_buf, sizeof ntop_buf));
118         if (a->netmask)
119           printf("\t\tNetmask: %s\n",
120             inet_ntop(AF_INET6,
121               ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr,
122                ntop_buf, sizeof ntop_buf));
123         if (a->broadaddr)
124           printf("\t\tBroadcast Address: %s\n",
125             inet_ntop(AF_INET6,
126               ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr,
127                ntop_buf, sizeof ntop_buf));
128         if (a->dstaddr)
129           printf("\t\tDestination Address: %s\n",
130             inet_ntop(AF_INET6,
131               ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr,
132                ntop_buf, sizeof ntop_buf));
133         break;
134 #endif
135       default:
136         printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
137         break;
138       }
139     else
140     {
141       fprintf(stderr, "\tWarning: a->addr is NULL, skipping this address.\n");
142       status = 0;
143     }
144   }
145   printf("\n");
146   return status;
147 }
148 
149 /* From tcptraceroute */
150 #define IPTOSBUFFERS	12
iptos(bpf_u_int32 in)151 static char *iptos(bpf_u_int32 in)
152 {
153 	static char output[IPTOSBUFFERS][3*4+3+1];
154 	static short which;
155 	u_char *p;
156 
157 	p = (u_char *)&in;
158 	which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
159 	sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
160 	return output[which];
161 }
162