]>
The Tcpdump Group git mirrors - libpcap/blob - tests/findalldevstest.c
7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 static int ifprint(pcap_if_t
*d
);
15 static char *iptos(bpf_u_int32 in
);
17 int main(int argc
, char **argv
)
22 bpf_u_int32 net
, mask
;
25 char errbuf
[PCAP_ERRBUF_SIZE
+1];
26 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
28 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
31 for(d
=alldevs
;d
;d
=d
->next
)
37 if ( (s
= pcap_lookupdev(errbuf
)) == NULL
)
39 fprintf(stderr
,"Error in pcap_lookupdev: %s\n",errbuf
);
44 printf("Preferred device name: %s\n",s
);
47 if (pcap_lookupnet(s
, &net
, &mask
, errbuf
) < 0)
49 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
54 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
60 static int ifprint(pcap_if_t
*d
)
64 char ntop_buf
[INET6_ADDRSTRLEN
];
66 int status
= 1; /* success */
68 printf("%s\n",d
->name
);
70 printf("\tDescription: %s\n",d
->description
);
71 printf("\tLoopback: %s\n",(d
->flags
& PCAP_IF_LOOPBACK
)?"yes":"no");
73 for(a
=d
->addresses
;a
;a
=a
->next
) {
75 switch(a
->addr
->sa_family
) {
77 printf("\tAddress Family: AF_INET\n");
79 printf("\t\tAddress: %s\n",
80 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
82 printf("\t\tNetmask: %s\n",
83 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
85 printf("\t\tBroadcast Address: %s\n",
86 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
88 printf("\t\tDestination Address: %s\n",
89 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
93 printf("\tAddress Family: AF_INET6\n");
95 printf("\t\tAddress: %s\n",
97 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
98 ntop_buf
, sizeof ntop_buf
));
100 printf("\t\tNetmask: %s\n",
102 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
103 ntop_buf
, sizeof ntop_buf
));
105 printf("\t\tBroadcast Address: %s\n",
107 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
108 ntop_buf
, sizeof ntop_buf
));
110 printf("\t\tDestination Address: %s\n",
112 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
113 ntop_buf
, sizeof ntop_buf
));
117 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
122 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
130 /* From tcptraceroute */
131 #define IPTOSBUFFERS 12
132 static char *iptos(bpf_u_int32 in
)
134 static char output
[IPTOSBUFFERS
][3*4+3+1];
139 which
= (which
+ 1 == IPTOSBUFFERS
? 0 : which
+ 1);
140 sprintf(output
[which
], "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
141 return output
[which
];