資源描述:
《上機1基于WinPcap的網絡嗅探器設計與實現(xiàn).doc》由會員上傳分享,免費在線閱讀,更多相關內容在教育資源-天天文庫。
1、上機1:基于WinPcap的網絡嗅探器設計1、目的與要求掌握基于WinPcap的網絡編程模式。理解并能應用WinPcap設計并實現(xiàn)網絡數(shù)據包的捕獲與解析。2、設備與上機環(huán)境l連網PC機(至少一臺)。l計算機硬件要求:IntelPentium5處理器、256MB以上內存,Ethernet網卡,網線若干。l計算機軟件要求:MSWindows9x/2000/XP操作系統(tǒng),TCP/IP協(xié)議,WinPcap430,Visualc++6.0/.net系統(tǒng)。3、上機內容與步驟:在程序設計之前,請參照提供的軟件安裝WinPcap。之后,按照如下步驟操作:步驟1:在VC++6.0下創(chuàng)建一個DOS命
2、令行程序,工程名:自己的學號-PacketDump步驟2:打開main()函數(shù),在主程序中增加如下頭文件和常量定義:#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