资源描述:
《协议分析器设计源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#include#include#include#includetypedefunsignedcharUCHAR;typedefunsignedshortUSHORT;voidpcap_handle(u_char*user,conststructpcap_pkthdr*header,constu_char*pkt_data);typedefstruct{//定义以太帧的头
2、部数据类型UCHARDestMac[6];UCHARSrcMac[6];UCHAREtype[2];}ETHHEADER;typedefstruct{//IP头部的数据类型UCHARheader_len:4;UCHARversion:4;UCHARtos:8;//typeofserviceUSHORTtotal_len:16;//lengthofthepacketUSHORTident:16;//uniqueidentifierUSHORTflags:16;UCHARttl:8;UCHARproto:8;//protocol(IP,TCP,UDPetc)USHORTch
3、ecksum:16;UCHARsourceIP[4];UCHARdestIP[4];}IPHEADER;typedefstruct{//定义端口数据类型用于解析TCP和UDP中的源端口和目的端口USHORTsrcPort;USHORTdecPort;}PORT;char*Proto[]={"Reserved","ICMP","IGMP","GGP","IP","ST","TCP","UCL","EGP","IGP","BBN-RCC-MON","NVP-II","PUP","ARGUS","EMCON","XNET","CHAOS","UDP","MUX","DCN-M
4、EAS","HMP","PRM","XNS-IDP","TRUNK-1","TRUNK-2","LEAF-1","LEAF-2","RDP","IRTP","ISO-TP4","NETBLT","MFE-NSP","MERIT-INP","SEP","3PC","IDPR","XTP","DDP","IDPR-CMTP","TP++","IL","SIP","SDRP","SIP-SR","SIP-FRAG","IDRP","RSVP","GRE","MHRP","BNA","SIPP-ESP","SIPP-AH","I-NLSP","SWIPE","NHRP","un
5、assigned","unassigned","unassigned","unassigned","unassigned","unassigned","anyhostinternalprotocol","CFTP","anylocalnetwork","SAT-EXPAK","KRYPTOLAN","RVD","IPPC","anydistributedfilesystem","SAT-MON","VISA","IPCV","CPNX","CPHB","WSN","PVP","BR-SAT-MON","SUN-ND","WB-MON","WB-EXPAK","ISO-I
6、P","VMTP","SECURE-VMTP","VINES","TTP","NSFNET-IGP","DGP","TCF","IGRP","OSPFIGP","Sprite-RPC","LARP","MTP","AX.25","IPIP","MICP","SCC-SP","ETHERIP","ENCAP","anyprivateencryptionscheme","GMTP"};intnpacketnum;intmain(intargc,char*argv[]){char*device="eth0";charerrbuf[PCAP_ERRBUF_SIZE];pcap_
7、t*phandle;bpf_u_int32ipaddress,ipmask;structbpf_programfcode;intdatalink;fprintf(stderr,"inputfilteris%s",argv[1]);if((device=pcap_lookupdev(errbuf))==NULL){perror(errbuf);return0;}else{fprintf(stderr,"device:%s",device);}phandle=pcap_open_live(device,200,0,500,errbuf