ICMP协议应用——Ping解析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

沈阳工程学院学生实验报告实验室名称:实验课程名称:计算机网络实验项目名称:ICMP协议应用——Ping解析班级:指导教师:实验日期:2010年12月06日试验台编号:批阅教师(签字):姓名:学号:成绩:一.实验目的1.掌握ICMP原理,体会网络层编程的不同;2.理解ICMP报文的作用,了解基本的网络编程框架。二.实验内容1.解析Ping程序的基本实现过程。2.体会ICMP协议在Ping程序中的应用。3.体会基本的C/S编程框架。三.实验前的准备掌握ICMP原理及相关概念。掌握C语言编程知识。掌握基本的网络编程内容四.实验要求及实验软硬件环境【基本要求】对Ping程序进行调试并运行实现。解析ping程序的主要部分,从而体会ICMP协议的应用。完成此项实验,完成实验报告。【实验组织方式】小组实验【实验条件】局域网环境下微机二台,编程软件。五.实验步骤1.熟悉IP以及ICMP协议的工作机制;2.熟悉创建原始套接字、IP报头和ICMP报头;3.体会ICMP协议的作用与特点;4.调试ICMP协议的Ping实现程序;5.参加答辩,并撰写实验报告六.主要程序部分(C++语言实现)#defineWIN32_LEAN_AND_MEAN#includewinsock2.h#includestdio.h#includestdlib.h#includeiostream.h#defineICMP_ECHO8#defineICMP_ECHOREPLY0#defineICMP_MIN12packet(justheader)typedefstructiphdr{unsignedcharh_len:4;unsignedcharversion:4;unsignedchartos;unsignedshorttotal_len;unsignedshortident;unsignedshortfrag_and_flags;unsignedcharttl;unsignedcharproto;unsignedshortchecksum;unsignedintsourceIP;unsignedintdestIP;}IpHeader;typedefstruct_ihdr{BYTEi_type;BYTEi_code;USHORTi_cksum;USHORTi_id;USHORTi_seq;ULONGtimestamp;}IcmpHeader;#defineSTATUS_FAILED0xFFFF#defineMAX_PACKET1024#definexmalloc(s)(char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))#definexfree(p)HeapFree(GetProcessHeap(),0,(p))USHORTchecksum(USHORT*,int);voidfill_icmp_head(char*);voiddecode_resp(char*,int,structsockaddr_in*);voidUsage(char*progname){fprintf(stderr,Usage:\n);fprintf(stderr,%shost\n,progname);ExitProcess(STATUS_FAILED);}intmain(intargc,char**argv){WSADATAwsaData;SOCKETsockRaw;structsockaddr_indest,from;structhostent*hp;intbread,datasize;intfromlen=sizeof(from);char*dest_ip;char*icmp_data;char*recvbuf;charhost[256];unsignedintaddr=0;USHORTseq_no=0;if(WSAStartup(0x0101,&wsaData)!=0){fprintf(stderr,WSAStartupfailed:%d\n,GetLastError());ExitProcess(STATUS_FAILED);}if(argc1)strcpy(host,argv[1]);else{printf(Pleaseinputhostname(Pressenterkeyforlocalhost)\nInput:);gets(host);if(strlen(host)1)strcpy(host,localhost);}if((sockRaw=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==INVALID_SOCKET){fprintf(stderr,WSAStartupfailed:%d\n,GetLastError());ExitProcess(STATUS_FAILED);}memset(&dest,0,sizeof(dest));hp=gethostbyname(host/*argv[1]*/);if(hp!=NULL){memcpy(&(dest.sin_addr),hp-h_addr,hp-h_length);dest.sin_family=AF_INET;dest_ip=inet_ntoa(dest.sin_addr);}else{fprintf(stderr,Unabletoresolve%s\n,argv[1]);ExitProcess(STATUS_FAILED);}datasize=sizeof(IcmpHeader);icmp_data=xmalloc(MAX_PACKET);recvbuf=xmalloc(MAX_PACKET);if(!icmp_data){fprintf(stderr,HeapAllocfailed%d\n,GetLastError());ExitProcess(STATUS_FAILED);}memset(icmp_data,0,MAX_PACKET);fill_icmp_head(icmp_data);while(1){intbwrote;((IcmpHeader*)icmp_data)-i_cksum=0;((IcmpHeader*)icmp_data)-timestamp=GetTickCount();((IcmpHeader*)icmp_data)-i_seq=seq_no++;((IcmpHeader*)icmp_data)-i_cksum=checksum((USHORT*)icmp_data,sizeof(IcmpHeader));bwrote=sendto(sockRaw,icmp_data,datasize,0,(structsockaddr*)&dest,sizeof(dest));if(bwrote==SOCKET_ERROR){fprintf(stderr,sendtofailed:%d\n,WSAGetLastError());ExitProcess(STATUS_FAILED);}if(bwrotedatasize)fprintf(stdout,Wrote%dbytes\n,bwrote);bread=recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(structsockaddr*)&from,&fromlen);if(bread==SOCKET_ERROR){if(WSAGetLastError()==WSAETIMEDOUT){printf(timedout\n);continue;}fprintf(stderr,recvfromfailed:%d\n,WSAGetLastError());perror(revffromfailed.);ExitProcess(STATUS_FAILED);}decode_resp(recvbuf,bread,&from);Sleep(2000);}closesocket(sockRaw);xfree(icmp_data);xfree(recvbuf);WSACleanup();return0;}voidfill_icmp_head(char*icmp_data){IcmpHeader*icmp_hdr;icmp_hdr=(IcmpHeader*)icmp_data;icmp_hdr-i_type=ICMP_ECHO;icmp_hdr-i_code=0;icmp_hdr-i_cksum=0;icmp_hdr-i_id=(USHORT)GetCurrentProcessId();icmp_hdr-i_seq=0;}voiddecode_resp(char*buf,intbytes,structsockaddr_in*from){IpHeader*iphdr;IcmpHeader*icmphdr;unsignedshortiphdrlen;iphdr=(IpHeader*)buf;iphdrlen=iphdr-h_len*4;if(bytesiphdrlen+ICMP_MIN)printf(Toofewbytesfrom%s\n,inet_ntoa(from-sin_addr));icmphdr=(IcmpHeader*)(buf+iphdrlen);if(icmphdr-i_type!=ICMP_ECHOREPLY){fprintf(stderr,non-echotype%drecvd\n,icmphdr-i_type);return;}if(icmphdr-i_id!=(USHORT)GetCurrentProcessId()){fprintf(stderr,someoneelse'spacket!\n);return;}printf(\n%dbytesfrom%s:,bytes,inet_ntoa(from-sin_addr));printf(icmp_seq=%d.,icmphdr-i_seq);printf(time:%dms,GetTickCount()-icmphdr-timestamp);}USHORTchecksum(USHORT*buffer,intsize){unsignedlongcksum=0;while(size1){cksum+=*buffer++;size-=sizeof(USHORT);}if(size)cksum+=*(UCHAR*)buffer;cksum=(cksum16)+(cksum&0xffff);cksum+=(cksum16);return(USHORT)(~cksum);}七.结果分析运行结果如图1所示:图1运行结果运行结果可看出此程序不断地在向172.20.106.45发送数据包,并且得到了应答。八.个人总结此次网络实验涉及到我们非常不熟练的基于网络协议的编程。Ping命令虽然是我们最先接触到的网络命令,但ping命令是来自于哪个网络层次的哪个协议并且是具体是如何实现的,我们就有些疑惑了,通过本次实验我们了解到ICMP协议是一个非常重要的协议,它对于网络安全、网络的正常运作具有极其重要的意义。它被用于在IP主机、路由器之间传递控制消息,如网络通不通、主机是否可达、路由是否可用等。实验中将原始数据在网络层添加IP报头和数据链路层添加ICMP的报头,再在目的端解封装这两个报头。我们小组已经对ICMP的原理及作用有了进一步的了解,将在以后的学习中发挥更大的作用。教师评语:教师签字:年月日

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功