欢迎来到天天文库
浏览记录
ID:37712713
大小:43.50 KB
页数:7页
时间:2019-05-29
《Ping程序源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Ping程序源代码/*******************************************************************************ping.c-SimplepingutilityusingSOCK_RAW**ThisisapartoftheMicrosoftSourceCodeSamples.*Copyright1996-1997MicrosoftCorporation.*Allrightsreserved.*Thissourcecodeisonlyintendedasasupplementto*M
2、icrosoftDevelopmentToolsand/orWinHelpdocumentation.*Seethesesourcesfordetailedinformationregardingthe*Microsoftsamplesprograms.******************************************************************************/#pragmapack(4)#defineWIN32_LEAN_AND_MEAN#include#include3、dio.h>#include#defineICMP_ECHO8#defineICMP_ECHOREPLY0#defineICMP_MIN8//minimum8byteicmppacket(justheader)/*TheIPheader*/typedefstructiphdr{unsignedinth_len:4;//lengthoftheheaderunsignedintversion:4;//VersionofIPunsignedchartos;//Typeofserviceunsignedshorttotal_len;//t4、otallengthofthepacketunsignedshortident;//uniqueidentifierunsignedshortfrag_and_flags;//flagsunsignedcharttl;unsignedcharproto;//protocol(TCP,UDPetc)unsignedshortchecksum;//IPchecksumunsignedintsourceIP;unsignedintdestIP;}IpHeader;////ICMPheader//typedefstruct_ihdr{BYTEi_type;B5、YTEi_code;/*typesubcode*/USHORTi_cksum;USHORTi_id;USHORTi_seq;/*Thisisnotthestdheader,butwereservespacefortime*/ULONGtimestamp;}IcmpHeader;#defineSTATUS_FAILED0xFFFF#defineDEF_PACKET_SIZE32#defineMAX_PACKET1024#definexmalloc(s)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))#de6、finexfree(p)HeapFree(GetProcessHeap(),0,(p))voidfill_icmp_data(char*,int);USHORTchecksum(USHORT*,int);voiddecode_resp(char*,int,structsockaddr_in*);voidUsage(char*progname){fprintf(stderr,"Usage:");fprintf(stderr,"%s[data_size]",progname);fprintf(stderr,"datasizecanbe7、upto1Kb");ExitProcess(STATUS_FAILED);}intmain(intargc,char**argv){WSADATAwsaData;SOCKETsockRaw;structsockaddr_indest,from;structhostent*hp;intbread,datasize;intfromlen=sizeof(from);inttimeout=1000;char*dest_ip;char*icmp_data;char*recvbuf;unsignedintaddr=0;USHORTseq_no=0;if(WS8、AStartup(MAKEWORD(2,1),&wsaData)!=0){fprintf(stderr,"W
3、dio.h>#include#defineICMP_ECHO8#defineICMP_ECHOREPLY0#defineICMP_MIN8//minimum8byteicmppacket(justheader)/*TheIPheader*/typedefstructiphdr{unsignedinth_len:4;//lengthoftheheaderunsignedintversion:4;//VersionofIPunsignedchartos;//Typeofserviceunsignedshorttotal_len;//t
4、otallengthofthepacketunsignedshortident;//uniqueidentifierunsignedshortfrag_and_flags;//flagsunsignedcharttl;unsignedcharproto;//protocol(TCP,UDPetc)unsignedshortchecksum;//IPchecksumunsignedintsourceIP;unsignedintdestIP;}IpHeader;////ICMPheader//typedefstruct_ihdr{BYTEi_type;B
5、YTEi_code;/*typesubcode*/USHORTi_cksum;USHORTi_id;USHORTi_seq;/*Thisisnotthestdheader,butwereservespacefortime*/ULONGtimestamp;}IcmpHeader;#defineSTATUS_FAILED0xFFFF#defineDEF_PACKET_SIZE32#defineMAX_PACKET1024#definexmalloc(s)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))#de
6、finexfree(p)HeapFree(GetProcessHeap(),0,(p))voidfill_icmp_data(char*,int);USHORTchecksum(USHORT*,int);voiddecode_resp(char*,int,structsockaddr_in*);voidUsage(char*progname){fprintf(stderr,"Usage:");fprintf(stderr,"%s[data_size]",progname);fprintf(stderr,"datasizecanbe
7、upto1Kb");ExitProcess(STATUS_FAILED);}intmain(intargc,char**argv){WSADATAwsaData;SOCKETsockRaw;structsockaddr_indest,from;structhostent*hp;intbread,datasize;intfromlen=sizeof(from);inttimeout=1000;char*dest_ip;char*icmp_data;char*recvbuf;unsignedintaddr=0;USHORTseq_no=0;if(WS
8、AStartup(MAKEWORD(2,1),&wsaData)!=0){fprintf(stderr,"W
此文档下载收益归作者所有