由 pcap_findalldevs_ex() 返回的每一个 pcap_if 结构体,都包含一个 pcap_addr 结构体,这个结构体由如下元素组成:
- 一个地址列表
- 一个掩码列表 (each of which corresponds to an entry in the addresses list).
- 一个广播地址列表 (each of which corresponds to an entry in the addresses list).
- 一个目的地址列表 (each of which corresponds to an entry in the addresses list).
另外,函数 pcap_findalldevs_ex() 还能 返回远程适配器信息 和 一个位于所给的本地文件夹的pcap文件列表。
下面的范例使用了ifprint()函数来打印出 pcap_if 结构体中所有的内容。程序对每一个由 pcap_findalldevs_ex() 函数返回的pcap_if,都调用ifprint()函数来实现打印。

1 #include "pcap.h" 2 #pragma comment(lib, "wpcap.lib") 3 #pragma comment(lib, "Packet.lib") 4 #pragma comment(lib, "wsock32.lib") 5 6 7 8 #ifndef WIN32 9 #include <sys/socket.h> 10 #include <netinet/in.h> 11 #else 12 #include <winsock.h> 13 #endif 14 15 16 // 函数原型 17 void ifprint(pcap_if_t *d); 18 char *iptos(u_long in); 19 char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen); 20 21 22 int main() 23 { 24 pcap_if_t *alldevs; 25 pcap_if_t *d; 26 char errbuf[PCAP_ERRBUF_SIZE+1]; 27 char source[PCAP_ERRBUF_SIZE+1]; 28 29 printf("Enter the device you want to list: " 30 "rpcap:// ==> lists interfaces in the local machine " 31 "rpcap://hostname:port ==> lists interfaces in a remote machine " 32 " (rpcapd daemon must be up and running " 33 " and it must accept 'null' authentication) " 34 "file://foldername ==> lists all pcap files in the give folder " 35 "Enter your choice: "); 36 37 fgets(source, PCAP_ERRBUF_SIZE, stdin); 38 source[PCAP_ERRBUF_SIZE] = '