On Fri, Apr 16, 2004 at 12:17:54AM +0200, Jacky Buyck wrote: > This last one, if I've correctly understand handle all the addresses > that can be use on an interface.
Yes, but note that they aren't all necessarily IPv4 addresses - you have to check the "sa_family" member of the address before printing it as an IPv4 address. I've attached a sample program (based on the test program written by the person who first implemented "pcap_findalldevs()") that lists all interfaces - try that program.
#include "pcap.h" #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include "config.h" void ifprint(pcap_if_t *d); char *iptos(u_long in); int main() { pcap_if_t *alldevs; pcap_if_t *d; char *s; bpf_u_int32 net, mask; char errbuf[PCAP_ERRBUF_SIZE+1]; if (pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf); exit(1); } for(d=alldevs;d;d=d->next) { ifprint(d); } if ( (s = pcap_lookupdev(errbuf)) == NULL) { fprintf(stderr,"Error in pcap_lookupdev: %s\n",errbuf); } else { printf("Preferred device name: %s\n",s); } if (pcap_lookupnet(s, &net, &mask, errbuf) < 0) { fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf); } else { printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask)); } exit(0); } void ifprint(pcap_if_t *d) { pcap_addr_t *a; #ifdef INET6 char ntop_buf[INET6_ADDRSTRLEN]; #endif 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"); for(a=d->addresses;a;a=a->next) { switch(a->addr->sa_family) { case AF_INET: printf("\tAddress Family: AF_INET\n"); if (a->addr) printf("\t\tAddress: %s\n", inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr)); if (a->netmask) printf("\t\tNetmask: %s\n", inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr)); if (a->broadaddr) printf("\t\tBroadcast Address: %s\n", inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr)); if (a->dstaddr) printf("\t\tDestination Address: %s\n", inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr)); break; #ifdef INET6 case AF_INET6: printf("\tAddress Family: AF_INET6\n"); if (a->addr) printf("\t\tAddress: %s\n", inet_ntop(AF_INET6, ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr, ntop_buf, sizeof ntop_buf)); if (a->netmask) printf("\t\tNetmask: %s\n", inet_ntop(AF_INET6, ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr, ntop_buf, sizeof ntop_buf)); if (a->broadaddr) printf("\t\tBroadcast Address: %s\n", inet_ntop(AF_INET6, ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr, ntop_buf, sizeof ntop_buf)); if (a->dstaddr) printf("\t\tDestination Address: %s\n", inet_ntop(AF_INET6, ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr, ntop_buf, sizeof ntop_buf)); break; #endif default: printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family); break; } } printf("\n"); } /* From tcptraceroute */ #define IPTOSBUFFERS 12 char *iptos(u_long in) { static char output[IPTOSBUFFERS][3*4+3+1]; static short which; u_char *p; p = (u_char *)∈ which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1); sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); return output[which]; }
- This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.