]>
The Tcpdump Group git mirrors - libpcap/blob - tests/findalldevstest.c
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
18 static int ifprint(pcap_if_t
*d
);
19 static char *iptos(bpf_u_int32 in
);
21 int main(int argc
, char **argv
)
26 bpf_u_int32 net
, mask
;
29 char errbuf
[PCAP_ERRBUF_SIZE
+1];
30 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
32 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
35 for(d
=alldevs
;d
;d
=d
->next
)
41 if ( (s
= pcap_lookupdev(errbuf
)) == NULL
)
43 fprintf(stderr
,"Error in pcap_lookupdev: %s\n",errbuf
);
48 printf("Preferred device name: %s\n",s
);
51 if (pcap_lookupnet(s
, &net
, &mask
, errbuf
) < 0)
53 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
58 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
64 static int ifprint(pcap_if_t
*d
)
68 char ntop_buf
[INET6_ADDRSTRLEN
];
70 int status
= 1; /* success */
72 printf("%s\n",d
->name
);
74 printf("\tDescription: %s\n",d
->description
);
75 printf("\tLoopback: %s\n",(d
->flags
& PCAP_IF_LOOPBACK
)?"yes":"no");
77 for(a
=d
->addresses
;a
;a
=a
->next
) {
79 switch(a
->addr
->sa_family
) {
81 printf("\tAddress Family: AF_INET\n");
83 printf("\t\tAddress: %s\n",
84 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
86 printf("\t\tNetmask: %s\n",
87 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
89 printf("\t\tBroadcast Address: %s\n",
90 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
92 printf("\t\tDestination Address: %s\n",
93 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
97 printf("\tAddress Family: AF_INET6\n");
99 printf("\t\tAddress: %s\n",
101 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
102 ntop_buf
, sizeof ntop_buf
));
104 printf("\t\tNetmask: %s\n",
106 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
107 ntop_buf
, sizeof ntop_buf
));
109 printf("\t\tBroadcast Address: %s\n",
111 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
112 ntop_buf
, sizeof ntop_buf
));
114 printf("\t\tDestination Address: %s\n",
116 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
117 ntop_buf
, sizeof ntop_buf
));
121 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
126 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
134 /* From tcptraceroute */
135 #define IPTOSBUFFERS 12
136 static char *iptos(bpf_u_int32 in
)
138 static char output
[IPTOSBUFFERS
][3*4+3+1];
143 which
= (which
+ 1 == IPTOSBUFFERS
? 0 : which
+ 1);
144 sprintf(output
[which
], "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
145 return output
[which
];