Home | History | Annotate | Download | only in tests
      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 void 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 
     24   char errbuf[PCAP_ERRBUF_SIZE+1];
     25   if (pcap_findalldevs(&alldevs, errbuf) == -1)
     26   {
     27     fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
     28     exit(1);
     29   }
     30   for(d=alldevs;d;d=d->next)
     31   {
     32     ifprint(d);
     33   }
     34 
     35   if ( (s = pcap_lookupdev(errbuf)) == NULL)
     36   {
     37     fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf);
     38   }
     39   else
     40   {
     41     printf("Preferred device name: %s\n",s);
     42   }
     43 
     44   if (pcap_lookupnet(s, &net, &mask, errbuf) < 0)
     45   {
     46     fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
     47   }
     48   else
     49   {
     50     printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
     51   }
     52 
     53   exit(0);
     54 }
     55 
     56 static void ifprint(pcap_if_t *d)
     57 {
     58   pcap_addr_t *a;
     59 #ifdef INET6
     60   char ntop_buf[INET6_ADDRSTRLEN];
     61 #endif
     62 
     63   printf("%s\n",d->name);
     64   if (d->description)
     65     printf("\tDescription: %s\n",d->description);
     66   printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
     67 
     68   for(a=d->addresses;a;a=a->next) {
     69     switch(a->addr->sa_family)
     70     {
     71       case AF_INET:
     72         printf("\tAddress Family: AF_INET\n");
     73         if (a->addr)
     74           printf("\t\tAddress: %s\n",
     75             inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr));
     76         if (a->netmask)
     77           printf("\t\tNetmask: %s\n",
     78             inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr));
     79         if (a->broadaddr)
     80           printf("\t\tBroadcast Address: %s\n",
     81             inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr));
     82         if (a->dstaddr)
     83           printf("\t\tDestination Address: %s\n",
     84             inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr));
     85         break;
     86 #ifdef INET6
     87       case AF_INET6:
     88         printf("\tAddress Family: AF_INET6\n");
     89         if (a->addr)
     90           printf("\t\tAddress: %s\n",
     91             inet_ntop(AF_INET6,
     92                ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr,
     93                ntop_buf, sizeof ntop_buf));
     94         if (a->netmask)
     95           printf("\t\tNetmask: %s\n",
     96             inet_ntop(AF_INET6,
     97               ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr,
     98                ntop_buf, sizeof ntop_buf));
     99         if (a->broadaddr)
    100           printf("\t\tBroadcast Address: %s\n",
    101             inet_ntop(AF_INET6,
    102               ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr,
    103                ntop_buf, sizeof ntop_buf));
    104         if (a->dstaddr)
    105           printf("\t\tDestination Address: %s\n",
    106             inet_ntop(AF_INET6,
    107               ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr,
    108                ntop_buf, sizeof ntop_buf));
    109         break;
    110 #endif
    111       default:
    112         printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
    113         break;
    114     }
    115   }
    116   printf("\n");
    117 }
    118 
    119 /* From tcptraceroute */
    120 #define IPTOSBUFFERS	12
    121 static char *iptos(bpf_u_int32 in)
    122 {
    123 	static char output[IPTOSBUFFERS][3*4+3+1];
    124 	static short which;
    125 	u_char *p;
    126 
    127 	p = (u_char *)&in;
    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];
    131 }
    132