-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.c
374 lines (319 loc) · 8.7 KB
/
ping.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<netinet/ip_icmp.h>
#include<unistd.h>
#include<signal.h>
#include<arpa/inet.h>
#include<errno.h>
#include<sys/time.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<pthread.h>
#include<stdint.h>
typedef struct
{
struct timeval tv_begin;
struct timeval tv_end;
short seq;
int flag;
}pingm_packet;
#define PACK_SIZE 128
static pingm_packet* icmp_findpacket(int seq);
static unsigned short icmp_cksum(unsigned char* data, int len);
static struct timeval icmp_tvsub(struct timeval end, struct timeval begin);
static void icmp_staistics(void);
static void icmp_pack(struct icmp* icmph, int seq, struct timeval* tv, int length);
static int icmp_unpack(char* buf, int len);
static void* icmp_recv(void* argv);
static void* icmp_send(void* argv);
static void icmp_sigint(int signo);
static void icmp_usage(void);
#define K 1024
#define BUFF_SIZE 72
static unsigned char send_buff[BUFF_SIZE];
static unsigned char recv_buff[2 * K];
static struct sockaddr_in dest;
static int rawsock = 0;
static pid_t pid = 0;
static int alive = 0;
static short packet_send = 0;
static short packet_recv = 0;
static struct timeval tv_interval, tv_end, tv_begin;
static char dest_str[80];
static pingm_packet pingpacket[PACK_SIZE];
static void icmp_usage(void)
{
printf("ping aaa.bbb.ccc.ddd\n");
}
static pingm_packet* icmp_findpacket(int seq)
{
int i = 0;
pingm_packet* found = NULL;
if(seq == -1)
{
for( i = 0; i < PACK_SIZE; i++)
{
if(pingpacket[i].flag == 0)
{
found = &pingpacket[i];
break;
}
}
}
else if(seq >= 0)
{
for(i = 0; i < PACK_SIZE; i++)
{
if(pingpacket[i].seq == seq)
{
found = &pingpacket[i];
break;
}
}
}
return found;
}
static unsigned short icmp_cksum(unsigned char* data, int len)
{
int sum = 0;
int odd = len & 0x01; /**奇数判断**/
unsigned short* value = (unsigned short*) data;
while(len & 0xfffe) /**将数据2字节为单位累加*/
{
sum += *(unsigned short*) data;
data += 2 ;
len -= 2;
}
/********判断是否为奇数个数据,如果为奇数个字节,会剩下最后一个字节***************************/
if(odd)
{
unsigned short tmp = ((*data) << 8) & 0xff00;
sum += tmp;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
static struct timeval icmp_tvsub(struct timeval end, struct timeval begin)
{
struct timeval tv;
tv.tv_sec = end.tv_sec - begin.tv_sec;
tv.tv_usec = end.tv_usec - begin.tv_usec;
if(tv.tv_usec < 0)
{
tv.tv_sec --;
tv.tv_usec += 1000000;
}
return tv;
}
static void icmp_staistics(void)
{
long time = (tv_interval.tv_sec * 1000) + (tv_interval.tv_usec / 1000);
printf("--- %s ping statistics ---\n", dest_str);
printf("%d package transmitted, %d received, %d%c packet loss, time %d ms \n",
packet_send, packet_recv, (packet_send - packet_recv) * 100 / (packet_send == 0 ? 1 : packet_send) , '%', (int)time);
}
static void icmp_pack(struct icmp* icmph, int seq, struct timeval* tv, int length)
{
unsigned char i = 0;
icmph->icmp_type = ICMP_ECHO;
icmph->icmp_code = 0;
icmph->icmp_cksum = 0;
icmph->icmp_seq = seq;
icmph->icmp_id = pid & 0xffff;
for(i = 0; i < length; i++)
{
icmph->icmp_data[i] = i;
}
icmph->icmp_cksum = icmp_cksum((unsigned char*)icmph, length);
}
static int icmp_unpack(char* buf, int len)
{
int i, iphdrrlen;
struct ip* ip_add = NULL;
struct icmp* icmpt = NULL;
int rtt;
ip_add = (struct ip*) buf;
iphdrrlen = ip_add->ip_hl * 4;
icmpt = (struct icmp*)(buf + iphdrrlen);
len -= iphdrrlen;
if(len < 8)
{
printf("ICMP packets \'s length is less than 8\n");
return -1;
}
if((icmpt->icmp_type == ICMP_ECHOREPLY) && (icmpt->icmp_id == pid) )
{
struct timeval tv_internel, tv_recv, tv_send;
pingm_packet* packet = icmp_findpacket(icmpt->icmp_seq);
if(packet == NULL)
{
return -1;
}
packet->flag = 0;
tv_send = packet->tv_begin;
gettimeofday(&tv_recv, NULL);
tv_internel = icmp_tvsub(tv_recv, tv_send);
rtt = (tv_internel.tv_sec * 1000) + (tv_internel.tv_usec / 1000);
/*******打印结果,包含了icmp长度和源ip地址,包的序列号, TTL,时间差****************/
printf("%d byte from %s : icmp_seq = %u ttl = %d rrt = %d ms\n ",
len, inet_ntoa(ip_add->ip_src), icmpt->icmp_seq, ip_add->ip_ttl, (rtt - 1000) ); //减去睡眠的1s
packet_recv ++;
}
else
{
return -1;
}
}
static void* icmp_recv(void* argv)
{
struct timeval tv;
tv.tv_usec = 200;
tv.tv_sec = 0;
fd_set readfd;
while(alive)
{
int ret = 0;
FD_ZERO(&readfd);
FD_SET(rawsock, &readfd);
ret = select(rawsock + 1, &readfd, NULL, NULL, &tv);
switch(ret)
{
case -1:
break; //error
case 0:
break;//timeout
default:
{
int fromlen = 0;
struct sockaddr from;
int size = recv(rawsock, recv_buff, sizeof(recv_buff), 0);
if(errno == EINTR)
{
perror("recvfrom error");
continue;
}
ret = icmp_unpack(recv_buff, size);
if(ret == -1)
{
continue;
}
}
break;
}
}
}
static void* icmp_send(void* argv)
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
gettimeofday(&tv_begin, NULL);
while(alive)
{
int size = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
icmp_pack((struct icmp*) send_buff, packet_send, &tv, 64);
size = sendto(rawsock, send_buff, 64, 0, (struct sockaddr*) &dest, sizeof(dest));
if(size < 0)
{
perror("sendto error");
continue;
}
else
{
pingm_packet* packet = icmp_findpacket(-1);
packet_send ++;
if(packet )
{
packet->seq = packet_send;
packet->flag = 1;
gettimeofday(&packet->tv_begin, NULL);
}
}
sleep(1);
}
}
static void icmp_sigint(int signo)
{
alive = 0;
gettimeofday(&tv_end, NULL);
tv_interval = icmp_tvsub(tv_end, tv_begin);
return;
}
int main(int argc, char* argv[])
{
struct hostent* host = NULL;
struct protoent* protocol = NULL;
char protoname[] = "icmp";
unsigned long inaddr = 1;
int size = 128 * K;
if(argc < 2)
{
icmp_usage();
return -1;
}
protocol = getprotobyname(protoname);
if(protocol == NULL)
{
perror("getprotobyname()");
return -1;
}
memcpy(dest_str, argv[1], strlen(argv[1]) + 1);
memset(pingpacket, 0, sizeof(pingm_packet) * 128);
rawsock = socket(AF_INET, SOCK_RAW, protocol->p_proto);
if(rawsock < 0)
{
perror("socket..");
return -1;
}
pid = getuid();
setsockopt(rawsock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
inaddr = inet_addr(argv[1]);
if(inaddr == INADDR_NONE)
{
host = gethostbyname(argv[1]);
if(host == NULL)
{
perror("gethostbyname");
return -1;
}
memcpy((char*) &dest.sin_addr,host->h_addr_list, host->h_length);
}
else
{
memcpy((char*)&dest.sin_addr, &inaddr, sizeof(inaddr));
}
inaddr = dest.sin_addr.s_addr;
printf("ping %s (%d.%d.%d.%d) 56(84) bytes of data.\n",
dest_str,
(int)(inaddr & 0x000000ff) >> 0,
(int)(inaddr & 0x0000ff00) >> 8,
(int)(inaddr & 0x00ff0000) >> 16,
(int)(inaddr & 0xff000000) >> 24);
signal(SIGINT, icmp_sigint);
alive = 1;
pthread_t send_id, recv_id;
int err = 0;
err = pthread_create(&send_id, NULL, icmp_send, NULL);
if(err < 0)
{
return -1;
}
err = pthread_create(&recv_id, NULL, icmp_recv, NULL);
if(err < 0)
{
return -1;
}
pthread_join(send_id, NULL);
pthread_join(recv_id, NULL);
close(rawsock);
icmp_staistics();
return 0;
}