winPcap獲取網絡卡網路地址和子網掩碼
阿新 • • 發佈:2019-02-18
下面是獲取網路地址(不是IP地址)和子網掩碼的示例,沒時間接著往下做例子了,因為接下來需要在LINUX下面使用libPcap,當然我會貼出程式碼,會linux程式設計的大牛一般都會,所以準確的說是貼給自己的,喜歡玩資料包的朋友自己看官方例子就行
#include<pcap.h> /** 資料包主執行函式 */ #pragma comment(lib,"wpcap.lib") #pragma comment(lib,"Packet.lib") #pragma comment(lib,"ws2_32.lib") void getAddr(); int main(int argc,char *argv[]) { getAddr(); return 0; } //獲取網絡卡網路地址和子網掩碼 void getAddr() { pcap_if_t *alldevs; pcap_if_t *d; struct in_addr net_ip_address;//網絡卡IP資訊,在pcap.h裡面有定義 u_int32_t net_ip; char *net_ip_string; struct in_addr net_mask_address; u_int32_t net_mask; char *net_mask_string; int i=0; char errbuf[PCAP_ERRBUF_SIZE]; if(pcap_findalldevs(&alldevs,errbuf)==-1)//無法找到網絡卡列表 { fprintf(stderr,"error in pcap_findalldevs: %s\n",errbuf); exit(1); } /* 掃描列表 */ for(d=alldevs;d;d=d->next) { printf("%s\n",d->name); printf("Description: %s\n",d->description); pcap_lookupnet(d->name,&net_ip,&net_mask,errbuf); net_ip_address.s_addr = net_ip; net_ip_string = inet_ntoa(net_ip_address);//format printf("網路地址: %s \n",net_ip_string); net_mask_address.s_addr = net_mask; net_mask_string = inet_ntoa(net_mask_address);//format printf("子網掩碼: %s \n",net_mask_string); printf("\n"); } /* 釋放連結串列 */ pcap_freealldevs(alldevs); printf("\n"); }
VC6.0下測試通過