資源簡介
數據包捕獲程序,C++編寫的WinPcap下的網絡數據包捕獲程序

代碼片段和文件信息
#include?“pcap.h“
#include?“string.h“
char?*p[200001];
int?amount=0;
#pragma?comment(lib“wpcap.lib“)
#pragma?comment(lib“Packet.lib“)
#pragma?comment(lib“WS2_32.LIB“)
void?packet_handler(u_char?*param?const?struct?pcap_pkthdr?*header?const?u_char?*pkt_data);?
char?*temp;
int?sign=0;
long?sum=0;
/*?32位ip地址?*/
typedef?struct?ip_address
{
u_char?byte1;
u_char?byte2;
u_char?byte3;
u_char?byte4;
}ip_address;
/*?IPv4?header?*/
typedef?struct?ip_header
{
u_char ver_ihl; //?Version?(4?bits)?+?Internet?header?length?(4?bits)
u_char tos; //?Type?of?service?
u_short?tlen; //?Total?length?
u_short?identification;?//?Identification
u_short?flags_fo; //?Flags?(3?bits)?+?Fragment?offset?(13?bits)
u_char ttl; //?Time?to?live
u_char proto; //?Protocol
u_short?crc; //?Header?checksum
ip_address saddr; //?Source?address
ip_address daddr; //?Destination?address
u_int op_pad; //?Option?+?Padding
}ip_header;
/*?UDP?header*/
typedef?struct?udp_header
{
u_short?sport; //?Source?port
u_short?dport; //?Destination?port
u_short?len; //?Datagram?length
u_short?crc; //?Checksum
}udp_header;
pcap_if_t?*alldevs;?
pcap_if_t?*d;?
int?inum;
char?errbuf[PCAP_ERRBUF_SIZE];
pcap_t?*adhandle;
int?main()
{
?
int?i=0;?
?printf(“start“);
/*?獲取設備列表?*/?
if?(pcap_findalldevs(&alldevs?errbuf)?==?-1)?
{?
//fprintf(stderr“Error?in?pcap_findalldevs:?%s\n“?errbuf);?
exit(1);?
}?
char?*pstr=“hello?world“;
//for(int?k=0;k<200001;k++)
//{
// p[i]=new?char[strlen(pstr)+1];
// strcpy(p[i]pstr);
//}
/*?數據列表?*/
for(d=alldevs;?d;?d=d->next)?
{?
//printf(“%d.?%s“?++i?d->name);?
++i;
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);?
inum=1;
if(inum?1?||?inum?>?i)?
{?
//printf(“\nInterface?number?out?of?range.\n“);?
/*?釋放設備列表?*/?
pcap_freealldevs(alldevs);?
return?-1;?
}?
/*?轉到選擇的設備?*/?
for(d=alldevs?i=0;?inext?i++);?
/*?打開設備?*/?
if?(?(adhandle=?pcap_open_live(d->name?//設備名?
65536?//?捕捉完整的數據包?
1?//?混雜+模式?
1000?//?讀入超時?
errbuf?//?錯誤緩沖?
)?)?==?NULL)?
{?
/*?Y-?打開失敗*/?
// fprintf(stderr“\nUnable?to?open?the?adapter.?%s?is?not?supported?by?WinPcap\n“);?
/*?釋放列表?*/?
//pcap_freealldevs(alldevs);?
return?-1;?
}?
//fp=adhandle;
//printf(“\nlistening?on?%s...\n“?d->description);?
/*?我們已經不需要設備列表了?釋放它?*/?
//pcap_freealldevs(alldevs);?
/*?開始捕捉?*/?
pcap_loop(adhandle?0?packet_handler?NULL);?
//pcap_read_ex(adhandle?0?packet_handler?NULL);
printf(“The?sniffer?is?stop“);
return?0;?
}
/*?處理數據包的回調函數*/?
void?packet_handler(u_char?*param?const?struct?pcap_pkthdr?*header?const?u_char?*pkt_data)?
{?
sign++;
struct?tm?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5710??2010-01-30?15:07??catch_packet\catch_packet?1-30.cpp
?????文件???????5760??2010-01-30?00:00??catch_packet\catch_packet.cpp
?????文件???????4202??2010-01-26?00:00??catch_packet\catch_packet.dsp
?????文件????????547??2010-01-18?00:00??catch_packet\catch_packet.dsw
?????文件????????207??2010-01-26?00:00??catch_packet\catch_packet.h
?????文件??????66560??2011-01-20?12:33??catch_packet\catch_packet.ncb
?????文件??????48640??2011-01-20?12:33??catch_packet\catch_packet.opt
?????文件????????803??2010-03-19?00:00??catch_packet\catch_packet.plg
?????文件????1999872??2010-01-21?00:00??catch_packet\Debug\catch_packet.bsc
?????文件?????213066??2010-03-19?00:00??catch_packet\Debug\catch_packet.dll
?????文件????????884??2010-01-30?00:00??catch_packet\Debug\catch_packet.exp
?????文件?????285144??2010-03-19?00:00??catch_packet\Debug\catch_packet.ilk
?????文件???????2572??2010-01-30?00:00??catch_packet\Debug\catch_packet.lib
?????文件??????19306??2010-03-19?00:00??catch_packet\Debug\catch_packet.obj
?????文件????3698372??2010-01-26?00:00??catch_packet\Debug\catch_packet.pch
?????文件?????705536??2010-01-30?00:00??catch_packet\Debug\catch_packet.pdb
?????文件?????983196??2010-01-21?00:00??catch_packet\Debug\catch_packet.sbr
?????文件?????164864??2010-03-19?00:00??catch_packet\Debug\vc60.idb
?????文件??????77824??2010-01-30?00:00??catch_packet\Debug\vc60.pdb
?????目錄??????????0??2015-05-29?18:50??catch_packet\Debug
?????目錄??????????0??2015-05-29?18:50??catch_packet
-----------?---------??----------?-----??----
??????????????8283065????????????????????21
- 上一篇:MFC教學管理系統
- 下一篇:學生學籍管理系統 數據庫課程設計
評論
共有 條評論