1 /* $NetBSD: findalldevstest.c,v 1.3 2015/03/31 21:39:43 christos Exp $ */
4 __RCSID("$NetBSD: findalldevstest.c,v 1.3 2015/03/31 21:39:43 christos Exp $");
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
19 static int ifprint(pcap_if_t
*d
);
20 static char *iptos(bpf_u_int32 in
);
22 int main(int argc
, char **argv
)
27 bpf_u_int32 net
, mask
;
30 char errbuf
[PCAP_ERRBUF_SIZE
+1];
31 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
33 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
36 for(d
=alldevs
;d
;d
=d
->next
)
42 if ( (s
= pcap_lookupdev(errbuf
)) == NULL
)
44 fprintf(stderr
,"Error in pcap_lookupdev: %s\n",errbuf
);
49 printf("Preferred device name: %s\n",s
);
52 if (pcap_lookupnet(s
, &net
, &mask
, errbuf
) < 0)
54 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
59 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
65 static int ifprint(pcap_if_t
*d
)
69 char ntop_buf
[INET6_ADDRSTRLEN
];
71 int status
= 1; /* success */
73 printf("%s\n",d
->name
);
75 printf("\tDescription: %s\n",d
->description
);
76 printf("\tLoopback: %s\n",(d
->flags
& PCAP_IF_LOOPBACK
)?"yes":"no");
78 for(a
=d
->addresses
;a
;a
=a
->next
) {
80 switch(a
->addr
->sa_family
) {
82 printf("\tAddress Family: AF_INET\n");
84 printf("\t\tAddress: %s\n",
85 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
87 printf("\t\tNetmask: %s\n",
88 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
90 printf("\t\tBroadcast Address: %s\n",
91 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
93 printf("\t\tDestination Address: %s\n",
94 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
98 printf("\tAddress Family: AF_INET6\n");
100 printf("\t\tAddress: %s\n",
102 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
103 ntop_buf
, sizeof ntop_buf
));
105 printf("\t\tNetmask: %s\n",
107 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
108 ntop_buf
, sizeof ntop_buf
));
110 printf("\t\tBroadcast Address: %s\n",
112 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
113 ntop_buf
, sizeof ntop_buf
));
115 printf("\t\tDestination Address: %s\n",
117 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
118 ntop_buf
, sizeof ntop_buf
));
122 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
127 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
135 /* From tcptraceroute */
136 #define IPTOSBUFFERS 12
137 static char *iptos(bpf_u_int32 in
)
139 static char output
[IPTOSBUFFERS
][3*4+3+1];
144 which
= (which
+ 1 == IPTOSBUFFERS
? 0 : which
+ 1);
145 sprintf(output
[which
], "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
146 return output
[which
];