生活随笔
收集整理的這篇文章主要介紹了
winpcap编程 解析数据包
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WinPcap和Libpcap的最強大的特性之一,是擁有過濾數據包的引擎。 它提供了有效的方法去獲取網絡中的某些數據包,這也是WinPcap捕獲機制中的一個組成部分。 用來過濾數據包的函數是?pcap_compile()?和pcap_setfilter()?。
pcap_compile()?它將一個高層的布爾過濾表達式編譯成一個能夠被過濾引擎所解釋的低層的字節碼。有關布爾過濾表達式的語法可以參見?Filtering expression syntax?這一節的內容。
pcap_setfilter()?將一個過濾器與內核捕獲會話向關聯。當?pcap_setfilter()?被調用時,這個過濾器將被應用到來自網絡的所有數據包,并且,所有的符合要求的數據包 (即那些經過過濾器以后,布爾表達式為真的包) ,將會立即復制給應用程序。
現在,我們可以捕捉并過濾網絡流量了,那就讓我們學以致用,來做一個簡單使用的程序吧。
在本講中,我們將會利用上一講的一些代碼,來建立一個更實用的程序。 本程序的主要目標是展示如何解析所捕獲的數據包的協議首部。這個程序可以稱為UDPdump,打印一些網絡上傳輸的UDP數據的信息。
我們選擇分析和現實UDP協議而不是TCP等其它協議,是因為它比其它的協議更簡單,作為一個入門程序范例,是很不錯的選擇。讓我們看看代碼:
?
[cpp]?view plaincopy
#include?"pcap.h"??????typedef?struct?ip_address{??????u_char?byte1;??????u_char?byte2;??????u_char?byte3;??????u_char?byte4;??}ip_address;??????typedef?struct?ip_header{??????u_char??ver_ihl;??????????????u_char??tos;??????????????????u_short?tlen;?????????????????u_short?identification;???????u_short?flags_fo;?????????????u_char??ttl;??????????????????u_char??proto;????????????????u_short?crc;??????????????????ip_address??saddr;????????????ip_address??daddr;????????????u_int???op_pad;???????????}ip_header;??????typedef?struct?udp_header{??????u_short?sport;????????????????u_short?dport;????????????????u_short?len;??????????????????u_short?crc;??????????????}udp_header;??????void?packet_handler(u_char?*param,?const?struct?pcap_pkthdr?*header,?const?u_char?*pkt_data);??????int?main()??{??pcap_if_t?*alldevs;??pcap_if_t?*d;??int?inum;??int?i=0;??pcap_t?*adhandle;??char?errbuf[PCAP_ERRBUF_SIZE];??u_int?netmask;??char?packet_filter[]?=?"ip?and?udp";??struct?bpf_program?fcode;??????????????if?(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,?NULL,?&alldevs,?errbuf)?==?-1)??????{??????????fprintf(stderr,"Error?in?pcap_findalldevs:?%s/n",?errbuf);??????????exit(1);??????}??????????????????for(d=alldevs;?d;?d=d->next)??????{??????????printf("%d.?%s",?++i,?d->name);??????????if?(d->description)??????????????printf("?(%s)/n",?d->description);??????????else??????????????printf("?(No?description?available)/n");??????}????????if(i==0)??????{??????????printf("/nNo?interfaces?found!?Make?sure?WinPcap?is?installed./n");??????????return?-1;??????}????????????printf("Enter?the?interface?number?(1-%d):",i);??????scanf("%d",?&inum);????????????if(inum?<?1?||?inum?>?i)??????{??????????printf("/nInterface?number?out?of?range./n");????????????????????pcap_freealldevs(alldevs);??????????return?-1;??????}??????????????for(d=alldevs,?i=0;?i<?inum-1?;d=d->next,?i++);??????????????????if?(?(adhandle=?pcap_open(d->name,?????????????????????????????????65536,??????????????????????????????????????????????????????????????????????????????PCAP_OPENFLAG_PROMISCUOUS,????????????????????????????????????????1000,?????????????????????????????????????NULL,?????????????????????????????????????errbuf????????????????????????????????????)?)?==?NULL)??????{??????????fprintf(stderr,"/nUnable?to?open?the?adapter.?%s?is?not?supported?by?WinPcap/n");????????????????????pcap_freealldevs(alldevs);??????????return?-1;??????}??????????????????if(pcap_datalink(adhandle)?!=?DLT_EN10MB)??????{??????????fprintf(stderr,"/nThis?program?works?only?on?Ethernet?networks./n");????????????????????pcap_freealldevs(alldevs);??????????return?-1;??????}????????????if(d->addresses?!=?NULL)????????????????????netmask=((struct?sockaddr_in?*)(d->addresses->netmask))->sin_addr.S_un.S_addr;??????else????????????????????netmask=0xffffff;?????????????????if?(pcap_compile(adhandle,?&fcode,?packet_filter,?1,?netmask)?<0?)??????{??????????fprintf(stderr,"/nUnable?to?compile?the?packet?filter.?Check?the?syntax./n");????????????????????pcap_freealldevs(alldevs);??????????return?-1;??????}??????????????????if?(pcap_setfilter(adhandle,?&fcode)<0)??????{??????????fprintf(stderr,"/nError?setting?the?filter./n");????????????????????pcap_freealldevs(alldevs);??????????return?-1;??????}????????????printf("/nlistening?on?%s.../n",?d->description);??????????????????pcap_freealldevs(alldevs);??????????????????pcap_loop(adhandle,?0,?packet_handler,?NULL);????????????return?0;??}??????void?packet_handler(u_char?*param,?const?struct?pcap_pkthdr?*header,?const?u_char?*pkt_data)??{??????struct?tm?*ltime;??????char?timestr[16];??????ip_header?*ih;??????udp_header?*uh;??????u_int?ip_len;??????u_short?sport,dport;??????time_t?local_tv_sec;??????????????local_tv_sec?=?header->ts.tv_sec;??????ltime=localtime(&local_tv_sec);??????strftime(?timestr,?sizeof?timestr,?"%H:%M:%S",?ltime);??????????????printf("%s.%.6d?len:%d?",?timestr,?header->ts.tv_usec,?header->len);??????????????ih?=?(ip_header?*)?(pkt_data?+??????????14);???????????????ip_len?=?(ih->ver_ihl?&?0xf)?*?4;??????uh?=?(udp_header?*)?((u_char*)ih?+?ip_len);??????????????sport?=?ntohs(?uh->sport?);??????dport?=?ntohs(?uh->dport?);??????????????printf("%d.%d.%d.%d.%d?->?%d.%d.%d.%d.%d/n",??????????ih->saddr.byte1,??????????ih->saddr.byte2,??????????ih->saddr.byte3,??????????ih->saddr.byte4,??????????sport,??????????ih->daddr.byte1,??????????ih->daddr.byte2,??????????ih->daddr.byte3,??????????ih->daddr.byte4,??????????dport);??}?? ?
?
轉載于:https://www.cnblogs.com/jiangyea/p/3530149.html
總結
以上是生活随笔為你收集整理的winpcap编程 解析数据包的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。