aboutsummaryrefslogtreecommitdiff
path: root/tests/findalldevstest.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/findalldevstest.c')
-rw-r--r--tests/findalldevstest.c56
1 files changed, 43 insertions, 13 deletions
diff --git a/tests/findalldevstest.c b/tests/findalldevstest.c
index ec7c95015c9e..5925bf6a8176 100644
--- a/tests/findalldevstest.c
+++ b/tests/findalldevstest.c
@@ -4,14 +4,18 @@
#include <stdlib.h>
#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
+#ifdef _WIN32
+ #include <winsock2.h>
+#else
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ #include <netdb.h>
+#endif
#include <pcap.h>
-static void ifprint(pcap_if_t *d);
+static int ifprint(pcap_if_t *d);
static char *iptos(bpf_u_int32 in);
int main(int argc, char **argv)
@@ -20,7 +24,8 @@ int main(int argc, char **argv)
pcap_if_t *d;
char *s;
bpf_u_int32 net, mask;
-
+ int exit_status = 0;
+
char errbuf[PCAP_ERRBUF_SIZE+1];
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
@@ -29,12 +34,14 @@ int main(int argc, char **argv)
}
for(d=alldevs;d;d=d->next)
{
- ifprint(d);
+ if (!ifprint(d))
+ exit_status = 2;
}
if ( (s = pcap_lookupdev(errbuf)) == NULL)
{
fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf);
+ exit_status = 2;
}
else
{
@@ -44,30 +51,47 @@ int main(int argc, char **argv)
if (pcap_lookupnet(s, &net, &mask, errbuf) < 0)
{
fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
+ exit_status = 2;
}
else
{
printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
}
-
- exit(0);
+
+ exit(exit_status);
}
-static void ifprint(pcap_if_t *d)
+static int ifprint(pcap_if_t *d)
{
pcap_addr_t *a;
#ifdef INET6
char ntop_buf[INET6_ADDRSTRLEN];
#endif
+ const char *sep;
+ int status = 1; /* success */
printf("%s\n",d->name);
if (d->description)
printf("\tDescription: %s\n",d->description);
- printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
+ printf("\tFlags: ");
+ sep = "";
+ if (d->flags & PCAP_IF_UP) {
+ printf("%sUP", sep);
+ sep = ", ";
+ }
+ if (d->flags & PCAP_IF_RUNNING) {
+ printf("%sRUNNING", sep);
+ sep = ", ";
+ }
+ if (d->flags & PCAP_IF_LOOPBACK) {
+ printf("%sLOOPBACK", sep);
+ sep = ", ";
+ }
+ printf("\n");
for(a=d->addresses;a;a=a->next) {
- switch(a->addr->sa_family)
- {
+ if (a->addr != NULL)
+ switch(a->addr->sa_family) {
case AF_INET:
printf("\tAddress Family: AF_INET\n");
if (a->addr)
@@ -111,9 +135,15 @@ static void ifprint(pcap_if_t *d)
default:
printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
break;
+ }
+ else
+ {
+ fprintf(stderr, "\tWarning: a->addr is NULL, skipping this address.\n");
+ status = 0;
}
}
printf("\n");
+ return status;
}
/* From tcptraceroute */