add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples-pcap / iflist / iflist.c
blobabd2e6676af58d679902e6e0a2ed378a35ee7228
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "pcap.h"
37 #ifndef WIN32
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #else
41 #include <winsock.h>
42 #endif
45 // Function prototypes
46 void ifprint(pcap_if_t *d);
47 char *iptos(u_long in);
48 char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen);
51 int main()
53 pcap_if_t *alldevs;
54 pcap_if_t *d;
55 char errbuf[PCAP_ERRBUF_SIZE+1];
57 /* Retrieve the device list */
58 if(pcap_findalldevs(&alldevs, errbuf) == -1)
60 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
61 exit(1);
64 /* Scan the list printing every entry */
65 for(d=alldevs;d;d=d->next)
67 ifprint(d);
70 /* Free the device list */
71 pcap_freealldevs(alldevs);
73 return 1;
78 /* Print all the available information on the given interface */
79 void ifprint(pcap_if_t *d)
81 pcap_addr_t *a;
82 char ip6str[128];
84 /* Name */
85 printf("%s\n",d->name);
87 /* Description */
88 if (d->description)
89 printf("\tDescription: %s\n",d->description);
91 /* Loopback Address*/
92 printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
94 /* IP addresses */
95 for(a=d->addresses;a;a=a->next) {
96 printf("\tAddress Family: #%d\n",a->addr->sa_family);
98 switch(a->addr->sa_family)
100 case AF_INET:
101 printf("\tAddress Family Name: AF_INET\n");
102 if (a->addr)
103 printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
104 if (a->netmask)
105 printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
106 if (a->broadaddr)
107 printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
108 if (a->dstaddr)
109 printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
110 break;
112 case AF_INET6:
113 printf("\tAddress Family Name: AF_INET6\n");
114 #ifndef __MINGW32__ /* Cygnus doesn't have IPv6 */
115 if (a->addr)
116 printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str)));
117 #endif
118 break;
120 default:
121 printf("\tAddress Family Name: Unknown\n");
122 break;
125 printf("\n");
128 /* From tcptraceroute, convert a numeric IP address to a string */
129 #define IPTOSBUFFERS 12
130 char *iptos(u_long in)
132 static char output[IPTOSBUFFERS][3*4+3+1];
133 static short which;
134 u_char *p;
136 p = (u_char *)&in;
137 which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
138 sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
139 return output[which];
142 #ifndef __MINGW32__ /* Cygnus doesn't have IPv6 */
143 char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen)
145 socklen_t sockaddrlen;
147 #ifdef WIN32
148 sockaddrlen = sizeof(struct sockaddr_in6);
149 #else
150 sockaddrlen = sizeof(struct sockaddr_storage);
151 #endif
154 if(getnameinfo(sockaddr,
155 sockaddrlen,
156 address,
157 addrlen,
158 NULL,
160 NI_NUMERICHOST) != 0) address = NULL;
162 return address;
164 #endif /* __MINGW32__ */