2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
38 #include <sys/socket.h>
39 #include <netinet/in.h>
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
);
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
);
64 /* Scan the list printing every entry */
65 for(d
=alldevs
;d
;d
=d
->next
)
70 /* Free the device list */
71 pcap_freealldevs(alldevs
);
78 /* Print all the available information on the given interface */
79 void ifprint(pcap_if_t
*d
)
85 printf("%s\n",d
->name
);
89 printf("\tDescription: %s\n",d
->description
);
92 printf("\tLoopback: %s\n",(d
->flags
& PCAP_IF_LOOPBACK
)?"yes":"no");
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
)
101 printf("\tAddress Family Name: AF_INET\n");
103 printf("\tAddress: %s\n",iptos(((struct sockaddr_in
*)a
->addr
)->sin_addr
.s_addr
));
105 printf("\tNetmask: %s\n",iptos(((struct sockaddr_in
*)a
->netmask
)->sin_addr
.s_addr
));
107 printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in
*)a
->broadaddr
)->sin_addr
.s_addr
));
109 printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in
*)a
->dstaddr
)->sin_addr
.s_addr
));
113 printf("\tAddress Family Name: AF_INET6\n");
114 #ifndef __MINGW32__ /* Cygnus doesn't have IPv6 */
116 printf("\tAddress: %s\n", ip6tos(a
->addr
, ip6str
, sizeof(ip6str
)));
121 printf("\tAddress Family Name: Unknown\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];
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
;
148 sockaddrlen
= sizeof(struct sockaddr_in6
);
150 sockaddrlen
= sizeof(struct sockaddr_storage
);
154 if(getnameinfo(sockaddr
,
160 NI_NUMERICHOST
) != 0) address
= NULL
;
164 #endif /* __MINGW32__ */