《计算机网络课程设计(第五章)发送ARP数据包(完整程序代码)(共6页).docx》由会员分享,可在线阅读,更多相关《计算机网络课程设计(第五章)发送ARP数据包(完整程序代码)(共6页).docx(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上计算机网络课程设计机械工业出版社第5章:发送ARP数据包课程设计目的:ARP协议用于完成IP地址与MAC 地址之间的转换。熟悉ARP协议对于IP数据包的传送过程具有重要意义。本课程设计的主要目的是通过封装与发送ARP帧,了解ARP协议的工作原理与ARP帧的结构。完整程序代码:#include#include#pragma comment(lib,wpcap.lib)#pragma comment(lib,ws2_32.lib)typedef struct PhyFrame unsigned char DstMAC6;unsigned char SrcMAC6;unsi
2、gned short FrameType;PHYFRAME;typedef struct ArpFrameunsigned short HardwareType;unsigned short ProtocolType;unsigned char MACLen;unsigned char IPLen;unsigned short Flag;unsigned char SrcMAC6;unsigned char SrcIP4;unsigned char DstMAC6;unsigned char DstIP4;unsigned char Padding18;ARPFRAME;typedef str
3、uct ArpPacket PHYFRAME phyFrame; ARPFRAME arpFrame;*ARPPACKET;ArpPacket arpPacket;int CheckMAC(char *argv,unsigned char *mac_addr)char mac18;int i=0,j=0,k=0;strcpy(mac,argv);int m=strlen(argv);for(i=0;im;i+) if(maci-&maci9&maciZ&maciz) coutmac address error2) coutmac address errot5) coutmac address
4、errorendl; return 0; for(i=0;i=0&*(mac+j)=a&*(mac+j)=A&*(mac+j)=0&*(mac+j)=a&*(mac+j)=A&*(mac+j)=Z)mac_addri+=(unsigned char)(*(mac+j)-A+10);j+;return 1;int CheckIP(char *argv,unsigned char *ip_addr) char ip16;int i=0,j=0,k=0;strcpy(ip,argv);int m=strlen(argv);for(i=0;im;i+) if(ipi.&ipi9) coutIP add
5、ress error!3) coutIP address error!3) coutIP address error!endl; return 0; for(i=0;i4;i+) while(*(ip+j)=.) j+; ip_addri=(unsigned char)atoi(ip+j);return 1;void main(int argc,char *argv) if(argc!=5) coutplease input command: SendArp source_ip source_mac dest_ip dest_macendl; return;int i=0;memset(&ar
6、pPacket,0,sizeof(arpPacket);if(!CheckMAC(argv4,arpPacket.phyFrame.DstMAC)return;if(!CheckMAC(argv2,arpPacket.phyFrame.SrcMAC)return;arpPacket.phyFrame.FrameType=htons(unsigned short)0x0608);arpPacket.arpFrame.HardwareType=(unsigned short)0x0100;arpPacket.arpFrame.ProtocolType=(unsigned short)0x0008;
7、arpPacket.arpFrame.MACLen=(unsigned char)6;arpPacket.arpFrame.IPLen=(unsigned char)4;if(!CheckMAC(argv2,arpPacket.arpFrame.SrcMAC)return;if(!CheckIP(argv1,arpPacket.arpFrame.SrcIP)return;if(!CheckMAC(argv4,arpPacket.arpFrame.DstMAC)return;if(!CheckIP(argv3,arpPacket.arpFrame.DstIP)return;for(i=0;i18
8、;i+)arpPacket.arpFrame.Paddingi=0;pcap_if_t *alldevs;pcap_if_t *d,*head=NULL;pcap_t *fp;char errbufPCAP_ERRBUF_SIZE;if(pcap_findalldevs(&alldevs,errbuf)=-1) coutUnable to create adapter list!next) cout+i: name; if(d-description) cout descriptionendl;if(i=0) coutNo adapter found!endl;return;coutEnter
9、 the interface number (1-ik;if(ki) coutOut of range!endl;return;for(d=alldevs,i=1;inext,i+); head=d; if(fp=pcap_open_live(head-name,1000,1,1000,errbuf)=NULL) coutUnable to open the adapter!endl; pcap_freealldevs(alldevs); return;if(pcap_sendpacket(fp,(unsigned char*)&arpPacket,sizeof(unsigned char*)&arpPacket)=-1) coutARP packet send error!endl; return;coutSource MAC: argv1endl;coutSource IP: argv2endl;coutDest MAC: argv3endl;coutDest MAC: argv4endl;coutARP packet send success!endl;结果截图专心-专注-专业