HTML5技术

Linux编程之PING的实现 - 冠军的试炼(3)

字号+ 作者:H5之家 来源:H5之家 2017-01-27 08:06 我要评论( )

1 #include stdio.h 2 #include netinet/ in .h 3 #include netinet/ip.h 4 #include netinet/ip_icmp.h 5 #include unistd.h 6 #include signal.h 7 #include arpa/inet.h 8 #include errno.h 9 #include sys/time

1 #include <stdio.h> 2 #include <netinet/in.h> 3 #include <netinet/ip.h> 4 #include <netinet/ip_icmp.h> 5 #include <unistd.h> 6 #include <signal.h> 7 #include <arpa/inet.h> 8 #include <errno.h> 9 #include <sys/time.h> 10 #include <string.h> 11 #include <netdb.h> 12 #include <pthread.h> PACKET_SEND_MAX_NUM 64 16 17 typedef struct ping_packet_status 18 { 19 struct timeval begin_time; 20 struct timeval end_time; seq; }ping_packet_status; ping_packet_status ping_packet[PACKET_SEND_MAX_NUM]; alive; 30 int rawsock; 31 int send_count; 32 int recv_count; 33 pid_t pid; 34 struct sockaddr_in dest; 35 struct timeval start_time; 36 struct timeval end_time; 37 struct timeval time_interval; unsigned short cal_chksum(unsigned short *addr,int len) 41 { int nleft=len; 42 int sum=0; 43 unsigned short *w=addr; 44 unsigned short answer=0; (nleft>1) 48 { 49 sum+=*w++; 50 nleft-=2; 51 } ( 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 } timeval cal_time_offset(struct timeval begin, struct timeval end) 65 { 66 struct timeval ans; 67 ans.tv_sec = end.tv_sec - begin.tv_sec; 68 ans.tv_usec = end.tv_usec - begin.tv_usec; { 71 ans.tv_sec--; 72 ans.tv_usec+=1000000; 73 } 74 return ans; 75 } icmp_pack(struct icmp* icmphdr, int seq, int length) 78 { 79 int i = 0; 80 81 icmphdr->icmp_type = ICMP_ECHO; 82 icmphdr->icmp_code = 0; 83 icmphdr->icmp_cksum = 0; 84 icmphdr->icmp_seq = seq; 85 icmphdr->icmp_id = pid & 0xffff; 86 for(i=0;i<length;i++) 87 { 88 icmphdr->icmp_data[i] = i; 89 } 90 91 icmphdr->icmp_cksum = cal_chksum((unsigned short*)icmphdr, length); 92 } icmp_unpack(char* buf, int len) 95 { 96 int iphdr_len; 97 struct timeval begin_time, recv_time, offset_time; ip* ip_hdr = (struct ip *)buf; 101 iphdr_len = ip_hdr->ip_hl*4; 102 struct icmp* icmp = (struct icmp*)(buf+iphdr_len); (len < { ); 107 return -1; 108 } ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == (pid & 0xffff))) 112 { 113 if((icmp->icmp_seq < 0) || (icmp->icmp_seq > PACKET_SEND_MAX_NUM)) 114 { ); 116 return -1; 117 } 118 119 ping_packet[icmp->icmp_seq].flag = 0; 120 begin_time = ping_packet[icmp->icmp_seq].begin_time; 121 gettimeofday(&recv_time, NULL); 122 123 offset_time = cal_time_offset(begin_time, recv_time); printf(, 127 len, inet_ntoa(ip_hdr->ip_src), icmp->icmp_seq, ip_hdr->ip_ttl, rtt); 128 129 } { ); 133 return -1; 134 } ; 136 } ping_send() 139 { 140 char send_buf[128]; 141 memset(send_buf, 0, sizeof(send_buf)); (alive) 144 { 145 int size = 0; 146 gettimeofday(&(ping_packet[send_count].begin_time), NULL); icmp_pack((size = sendto(rawsock, send_buf, 64, 0, (struct sockaddr*)&dest, sizeof(dest)); (size < 0) 153 { ); 155 continue; 156 } 157 158 sleep(1); 159 } 160 } ping_recv() 163 { 164 struct timeval tv; tv.tv_sec = 0; 167 fd_set read_fd; 168 char recv_buf[512]; 169 memset(recv_buf, 0 ,sizeof(recv_buf)); 170 while(alive) 171 { 172 int ret = 0; 173 FD_ZERO(&read_fd); 174 FD_SET(rawsock, &read_fd); 175 ret = select(rawsock+1, &read_fd, NULL, NULL, &tv); 176 switch(ret) 177 { 178 case -1: ); 180 break; : 182 break; 183 default: 184 { 185 int size = recv(rawsock, recv_buf, sizeof(recv_buf), 0); 186 if(size < 0) 187 { ); 189 continue; 190 } (ret == - { 195 continue; 196 } } 199 break; 200 } 201 202 } 203 } icmp_sigint(int signo) 206 { 207 alive = 0; 208 gettimeofday(&end_time, NULL); 209 time_interval = cal_time_offset(start_time, end_time); 210 } ping_stats_show() 213 { 214 long time = time_interval.tv_sec*1000+time_interval.tv_usec/1000; printf(, , time); 218 } main(int argc, char* argv[]) 222 { protoent* protocol = NULL; 225 char dest_addr_str[80]; 226 memset(dest_addr_str, 0, 80); 227 unsigned int inaddr = 1; 228 struct hostent* host = NULL; 229 230 pthread_t send_id,recv_id; (argc < 2) 233 { ); 235 return -1; 236 } ); (protocol == NULL) 240 { ); 242 return -1; 243 } 244 245 memcpy(dest_addr_str, argv[1], strlen(argv[1])+1); 246 247 rawsock = socket(AF_INET,SOCK_RAW,protocol->p_proto); 248 if(rawsock < 0) 249 { ); 251 return -1; 252 } 253 254 pid = getpid(); bzero(&dest,sizeof(dest)); 259 260 dest.sin_family = AF_INET; 261 262 inaddr = inet_addr(argv[1]); { host = gethostbyname(argv[1]); 267 if(host == NULL) 268 { ); 270 return -1; 271 } 272 273 memcpy((char*)&dest.sin_addr, host->h_addr, host->h_length); 274 } { } 279 inaddr = dest.sin_addr.s_addr; ,dest_addr_str, 281 (inaddr&0x000000ff), (inaddr&0x0000ff00)>>8, 282 (inaddr&0x00ff0000)>>16, (inaddr&0xff000000)>>24); signal(SIGINT, icmp_sigint); (pthread_create(&send_id, NULL, (void*)ping_send, NULL)) 289 { ); 291 return -1; 292 } (pthread_create(&recv_id, NULL, (void*)ping_recv, NULL)) 295 { ); 297 return -1; 298 } pthread_join(recv_id, NULL); ping_stats_show(); 304 305 close(rawsock); ; 307 308 }

编译以及实验现象如下:
我的实验环境是两台服务器,发起ping的主机是172.0.5.183,被ping的主机是172.0.5.182,以下是我的两次实验现象(ping IP和ping 域名)。

 

特别注意: 

只有root用户才能利用socket()函数生成原始套接字,要让Linux的一般用户能执行以上程序,需进行如下的特别操作:用root登陆,编译以上程序gcc -lpthread -o ping ping.c

 

实验现象可以看出,PING是成功的,表明两主机间的网络是通的,发出的所有ping包都收到了回复。

 

下面是Linux系统自带的PING程序,我们可以对比一下我们设计的PING程序跟系统自带的PING程序有何不同。

 

 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Linux编程之给你的程序开后门 - 冠军的试炼

    Linux编程之给你的程序开后门 - 冠军的试炼

    2017-01-16 15:00

  • Web Worker javascript多线程编程(一) - PeakLeo

    Web Worker javascript多线程编程(一) - PeakLeo

    2016-12-27 10:00

  • 怎样实现前端裁剪上传图片功能 - 会编程的银猪

    怎样实现前端裁剪上传图片功能 - 会编程的银猪

    2016-10-19 13:00

  • 【菜鸟玩Linux开发】通过MySQL自动同步刷新Redis - zhxilin

    【菜鸟玩Linux开发】通过MySQL自动同步刷新Redis - zhxilin

    2016-10-03 16:00

网友点评