获取设备列表
int pcap_findalldevs_ex ( char * source, struct pcap_rmtauth * auth, pcap_if_t ** alldevs, char * errbuf )
·功能:获得已连接的网络适配器列表;
·该函数返回一个 pcap_if 结构的链表, 每个这样的结构都包含了一个适配器的详细信息;
·pcap_if结构链表中:数据域 name 和 description 表示一个适配器名称和一个可以让人们理解的描述;
`errbuf:一旦发生错误,这个参数将会被libpcap写入字符串类型的错误信息;
struct pcap_rmtauth{ int type //Type of the authentication required. char * username //Zero-terminated string containing the username that has to be used on the remote machine for authentication. char * password //Zero-terminated string containing the password that has to be used on the remote machine for authentication. };
struct pcap_if { pcap_if * next //if not NULL, a pointer to the next element in the list; NULL for the last element of the list char * name //a pointer to a string giving a name for the device to pass to pcap_open_live() char * description //if not NULL, a pointer to a string giving a human-readable description of the device pcap_addr * addresses //a pointer to the first element of a list of addresses for the interface u_int flags //PCAP_IF_ interface flags. Currently the only possible flag is PCAP_IF_LOOPBACK, that is set if the interface is a loopback interface. };
struct pcap_addr { pcap_addr * next //if not NULL, a pointer to the next element in the list; NULL for the last element of the list sockaddr * addr //a pointer to a struct sockaddr containing an address sockaddr * netmask //if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address pointed to by addr. sockaddr * broadaddr //if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre sponding to the address pointed to by addr; may be null if the interface doesn‘t support broadcasts sockaddr * dstaddr //if not NULL, a pointer to a struct sockaddr that contains the destination address corre sponding to the address pointed to by addr; may be null if the interface isn‘t a point- to-point interface };
void pcap_freealldevs ( pcap_if_t * alldevsp )
·功能:Free an interface list returned by pcap_findalldevs().(pcap_freealldevs() is used to free a list allocated by pcap_findalldevs().)
1 #include "pcap.h" 2 3 main() 4 { 5 pcap_if_t *alldevs; 6 pcap_if_t *d; 7 int i=0; 8 char errbuf[PCAP_ERRBUF_SIZE]; 9 10 /* 获取本地机器设备列表 */ 11 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1) 12 { 13 fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf); 14 exit(1); 15 } 16 17 /* 打印列表 */ 18 for(d= alldevs; d != NULL; d= d->next) 19 { 20 printf("%d. %s", ++i, d->name); 21 if (d->description) 22 printf(" (%s)\n", d->description); 23 else 24 printf(" (No description available)\n"); 25 } 26 27 if (i == 0) 28 { 29 printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); 30 return; 31 } 32 33 /* 不再需要设备列表了,释放它 */ 34 pcap_freealldevs(alldevs); 35 }
编译:在Windows平台上,您需要创建一个工程,并按照 使用WinPcap编程 里的步骤做。 然而,我们建议您使用WinPcap developer‘s pack ( 详情请访问WinPcap网站, http://www.winpcap.org ), 因为它提供了很多已经配置好的范例,包括本教程中的所有示例代码,以及在编译运行时需要的 包含文件(include) 和 库(libraries)
结果:
前部分为name,后面部分为description;