1 #include <stdio.h> 2 #include <signal.h> 3 #include <arpa/inet.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <unistd.h> 7 #include <netinet/in.h> 8 #include <netinet/ip.h> 9 #include <netinet/ip_icmp.h> 10 #include <netdb.h> 11 #include <setjmp.h> 12 #include <errno.h> 13 #define PACKET_SIZE 4096 14 #define MAX_WAIT_TIME 5 15 #define MAX_NO_PACKETS 3 16 char sendpacket[PACKET_SIZE]; 17 char recvpacket[PACKET_SIZE]; 18 int sockfd,datalen=56; 19 int nsend=0,nreceived=0; 20 struct sockaddr_in dest_addr; 21 pid_t pid; 22 struct sockaddr_in from; 23 struct timeval tvrecv; 24 void statistics(int signo); 25 unsigned short cal_chksum(unsigned short *addr,int len); 26 int pack(int pack_no); 27 void send_packet(void); 28 void recv_packet(void); 29 int unpack(char *buf,int len); 30 void tv_sub(struct timeval *out,struct timeval *in); 31 void statistics(int signo) 32 { 33 printf("\n--------------------PING statistics-------------------\n"); 34 printf("%d packets transmitted, %d received , %%%d lost\n",nsend,nreceived,(nsend-nreceived)/nsend*100); 35 close(sockfd); 36 exit(1); 37 } 38 /*校驗和算法*/ 39 unsigned short cal_chksum(unsigned short *addr,int len) 40 { 41 int nleft=len; 42 int sum=0; 43 unsigned short *w=addr; 44 unsigned short answer=0; 45 46 /*把ICMP報頭二進制數據以2字節爲單位累加起來*/ 47 while(nleft>1) 48 { 49 sum+=*w++; 50 nleft-=2; 51 } 52 /*若ICMP報頭爲奇數個字節,會剩下最後一字節。把最後一個字節視爲一個2字節數據的高字節,這個2字節數據的低字節爲0,繼續累加*/ 53 if( nleft==1) 54 { 55 *(unsigned char *)(&answer)=*(unsigned char *)w; 56 sum+=answer; 57 } 58 sum=(sum>>16)+(sum&0xffff); 59 sum+=(sum>>16); 60 answer=~sum; 61 return answer; 62 } 63 /*設置ICMP報頭*/ 64 int pack(int pack_no) 65 { 66 int i,packsize; 67 struct icmp *icmp; 68 struct timeval *tval; 69 icmp=(struct icmp*)sendpacket; 70 icmp->icmp_type=ICMP_ECHO; 71 icmp->icmp_code=0; 72 icmp->icmp_cksum=0; 73 icmp->icmp_seq=pack_no; 74 icmp->icmp_id=pid; 75 packsize=8+datalen; 76 tval= (struct timeval *)icmp->icmp_data; 77 gettimeofday(tval,NULL); /*記錄發送時間*/ 78 icmp->icmp_cksum=cal_chksum( (unsigned short*)icmp,packsize); /*校驗算法*/ 79 return packsize; 80 } 81 /*發送三個ICMP報文*/ 82 void send_packet() 83 { 84 int packetsize; 85 while( nsend<MAX_NO_PACKETS) 86 { 87 nsend++; 88 packetsize=pack(nsend); /*設置ICMP報頭*/ 89 if( sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(dest_addr) )<0 ) 90 { 91 perror("sendto error"); 92 continue; 93 } 94 sleep(1); /*每隔一秒發送一個ICMP報文*/ 95 } 96 } 97 /*接收全部ICMP報文*/ 98 void recv_packet() 99 { 100 int n,fromlen; 101 extern int errno; 102 signal(SIGALRM,statistics); 103 fromlen=sizeof(from); 104 while( nreceived<nsend) 105 { 106 alarm(MAX_WAIT_TIME); 107 if( (n=recvfrom(sockfd,recvpacket,sizeof(recvpacket),0, (struct sockaddr *)&from,&fromlen)) <0) 108 { 109 if(errno==EINTR) 110 continue; 111 perror("recvfrom error"); 112 continue; 113 } 114 gettimeofday(&tvrecv,NULL); /*記錄接收時間*/ 115 if(unpack(recvpacket,n)==-1) 116 continue; 117 nreceived++; 118 } 119 } 120 /*剝去ICMP報頭*/ 121 int unpack(char *buf,int len) 122 { 123 int i,iphdrlen; 124 struct ip *ip; 125 struct icmp *icmp; 126 struct timeval *tvsend; 127 double rtt; 128 ip=(struct ip *)buf; 129 iphdrlen=ip->ip_hl<<2; /*求ip報頭長度,即ip報頭的長度標誌乘4*/ 130 icmp=(struct icmp *)(buf+iphdrlen); /*越過ip報頭,指向ICMP報頭*/ 131 len-=iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/ 132 if( len<8) /*小於ICMP報頭長度則不合理*/ 133 { 134 printf("ICMP packets\'s length is less than 8\n"); 135 return -1; 136 } 137 /*確保所接收的是我所發的的ICMP的迴應*/ 138 if( (icmp->icmp_type==ICMP_ECHOREPLY) && (icmp->icmp_id==pid) ) 139 { 140 tvsend=(struct timeval *)icmp->icmp_data; 141 tv_sub(&tvrecv,tvsend); /*接收和發送的時間差*/ 142 rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000; /*以毫秒爲單位計算rtt*/ 143 /*顯示相關信息*/ 144 printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f ms\n",len,inet_ntoa(from.sin_addr), icmp->icmp_seq, ip->ip_ttl,rtt);} 145 else 146 return -1; 147 } 148 main(int argc,char *argv[]) 149 { 150 struct hostent *host; 151 struct protoent *protocol; 152 unsigned long inaddr=0l; 153 int waittime=MAX_WAIT_TIME; 154 int size=50*1024; 155 if(argc<2) 156 { 157 printf("usage:%s hostname/IP address\n",argv[0]); 158 exit(1); 159 } 160 if( (protocol=getprotobyname("icmp") )==NULL) 161 { 162 perror("getprotobyname"); 163 exit(1); 164 } 165 /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/ 166 if( (sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto) )<0) 167 { 168 perror("socket error"); 169 exit(1); 170 } 171 /* 回收root權限,設置當前用戶權限*/ 172 setuid(getuid()); 173 /*擴大套接字接收緩衝區到50K這樣作主要爲了減少接收緩衝區溢出的的可能性,若無心中ping一個廣播地址或多播地址,將會引來大量應答*/ 174 setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size) ); 175 bzero(&dest_addr,sizeof(dest_addr)); 176 dest_addr.sin_family=AF_INET; 177 /*判斷是主機名仍是ip地址*/ 178 if( inaddr=inet_addr(argv[1])==INADDR_NONE) 179 { 180 if((host=gethostbyname(argv[1]) )==NULL) /*是主機名*/ 181 { 182 perror("gethostbyname error"); 183 exit(1); 184 } 185 memcpy( (char *)&dest_addr.sin_addr,host->h_addr,host->h_length); 186 } 187 else /*是ip地址*/ 188 memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length); 189 /*獲取main的進程id,用於設置ICMP的標誌符*/ 190 pid=getpid(); 191 printf("PING %s(%s): %d bytes data in ICMP packets.\n",argv[1],inet_ntoa(dest_addr.sin_addr),datalen); 192 send_packet(); /*發送全部ICMP報文*/ 193 recv_packet(); /*接收全部ICMP報文*/ 194 statistics(SIGALRM); /*進行統計*/ 195 return 0; 196 } 197 /*兩個timeval結構相減*/ 198 void tv_sub(struct timeval *out,struct timeval *in) 199 { 200 if( (out->tv_usec-=in->tv_usec)<0) 201 { 202 --out->tv_sec; 203 out->tv_usec+=1000000; 204 } 205 out->tv_sec-=in->tv_sec; 206 } 207 /*------------- The End -----------*/
仍然須要注意權限的問題,按照以下方式編譯:算法
sudo gcc myping.c -o mypingless
sudo chmod u+s mypingsocket
./myping www.163.comui
運行顯示結果:spa
PING www.cn.ibm.com(121.195.178.238): 56 bytes data in ICMP packets.code
64 byte from 121.195.178.238: icmp_seq=1 ttl=242 rtt=3029.000 msblog
64 byte from 121.195.178.238: icmp_seq=2 ttl=242 rtt=2020.000 ms進程
64 byte from 121.195.178.238: icmp_seq=3 ttl=242 rtt=1010.000 msip
--------------------PING statistics-------------------get
3 packets transmitted, 3 received , %0 lost