资源描述:
《上机1基于WinPcap的网络嗅探器设计与实现.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、上机1:基于WinPcap的网络嗅探器设计1、目的与要求掌握基于WinPcap的网络编程模式。理解并能应用WinPcap设计并实现网络数据包的捕获与解析。2、设备与上机环境l连网PC机(至少一台)。l计算机硬件要求:IntelPentium5处理器、256MB以上内存,Ethernet网卡,网线若干。l计算机软件要求:MSWindows9x/2000/XP操作系统,TCP/IP协议,WinPcap430,Visualc++6.0/.net系统。3、上机内容与步骤:在程序设计之前,请参照提供的软件安装WinPcap。之后,按照如下步骤操作:步骤1:在VC++6.0下创建一个DOS命
2、令行程序,工程名:自己的学号-PacketDump步骤2:打开main()函数,在主程序中增加如下头文件和常量定义:#include#defineLINE_LEN16pcap_if_t*alldevs,*d;pcap_t*fp;u_intinum,i=0;charerrbuf[PCAP_ERRBUF_SIZE];intres;structpcap_pkthdr*header;constu_char*pkt_data;printf("pktdump_ex:printsthepacketsofthenetworkusingWinPcap.");printf("Us
3、age:pktdump_ex[-ssource]""Examples:""pktdump_ex-sfile.acp""pktdump_ex-s\Device\NPF_{C-F3C3-4373-94AC-9A34B7DAD998}");if(argc<3){printf("Noadapterselected:printingthedevicelist:");/*Theuserdidn'tprovideapacketsource:Retrievethelocaldevicelist*/if(pcap_findalldevs(&alldevs,err
4、buf)==-1){fprintf(stderr,"Errorinpcap_findalldevs_ex:%s",errbuf);exit(1);}/*Printthelist*/for(d=alldevs;d;d=d->next){printf("%d.%s",++i,d->name);if(d->description)printf("(%s)",d->description);elseprintf("(Nodescriptionavailable)");}if(i==0){printf("Nointerfacesfound!MakesureWinPcap
5、isinstalled.");return-1;}printf("Entertheinterfacenumber(1-%d):",i);scanf("%d",&inum);if(inum<1
6、
7、inum>i){printf("Interfacenumberoutofrange.");/*Freethedevicelist*/pcap_freealldevs(alldevs);return-1;}/*Jumptotheselectedadapter*/for(d=alldevs,i=0;inext,i++);/*Opentheadapter*/
8、if((fp=pcap_open_live(d->name,//nameofthedevice65536,//portionofthepackettocapture.//65536grantsthatthewholepacketwillbecapturedonalltheMACs.1,//promiscuousmode(nonzeromeanspromiscuous)1000,//readtimeouterrbuf//errorbuffer))==NULL){fprintf(stderr,"Erroropeningadapter");return-1;}}else{/*D
9、onotcheckfortheswitchtype('-s')*/if((fp=pcap_open_live(argv[2],//nameofthedevice65536,//portionofthepackettocapture.//65536grantsthatthewholepacketwillbecapturedonalltheMACs.1,//promiscuousmode(nonzeromeanspromiscuous)1000,//readtimeouterrbuf