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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
| #include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#define BUFSIZE 1500
char sendbuf[BUFSIZE];
char *host;
int nsent;
pid_t pid;
int sockfd;
int verbose;
void init_v6(void);
void proc_v4(char *, ssize_t, struct msghdr *, struct timeval *);
void proc_v6(char *, ssize_t, struct msghdr *, struct timeval *);
void send_v4(void);
void send_v6(void);
void readloop(void);
void sig_alrm(int);
void tv_sub(struct timeval *, struct timeval *);
struct proto{
void (*fproc)(char *, ssize_t, struct msghdr *, struct timeval *);
void (*fsend)(void);
void (*finit)(void);
struct sockaddr *sasend;
struct sockaddr *sarecv;
socklen_t salen;
int icmpproto;
}*pr;
#ifdef IPV6
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#endif
/*定义IPv4和IPv6的proto结构*/
struct proto proto_v4 =
{proc_v4, send_v4, NULL, NULL, NULL ,0, IPPROTO_ICMP};
#ifdef IPV6
struct proto proto_v6 =
{proc_v6, send_v6, init_v6, NULL, NULL ,0, IPPROTO_ICMP6};
#endif
int datalen =56;
/*获取主机和相关信息*/
struct addrinfo *host_serv(const char *host, const char *serv, int family, int socktype)
{
int n;
struct addrinfo hints, *res;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = family;
hints.ai_socktype = socktype;
if( ( n = getaddrinfo( host, serv, &hints, &res)) != 0)
return (NULL);
return (res);
}
/*将ip地址值形式转变为表达形式*/
char *sock_ntop_host(const struct sockaddr *sa, socklen_t salen)
{
char portstr[8];
static char str[128];
switch( sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if(inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL)
return (NULL);
if(ntohs(sin->sin_port) != 0)
{
snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin->sin_port));
strcat(str, portstr);
}
return (str);
}
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if(inet_ntop(AF_INET6, &sin6->sin6_addr, str,sizeof(str)) == NULL)
return (NULL);
if(ntohs(sin6->sin6_port) != 0)
{
snprintf(portstr, sizeof(portstr), ":%d", ntohs(sin6->sin6_port));
strcat(str, portstr);
}
return (str);
}
}
}
int main(int argc, char **argv)
{
int c;
struct addrinfo *ai;
char *h;
opterr = 0;
while( ( c = getopt(argc, argv, "v")) != -1) /*处理命令行选项*/
{
switch(c)
{
case 'v':
verbose++;
break;
case '?':
printf("unrecognized option:%c", c);
}
}
if( optind != argc-1)
printf("usage:ping [ -v ] <hostname>");
host = argv[optind];
pid = getpid()&0xffff;
signal(SIGALRM, sig_alrm);
ai = host_serv(host, NULL, 0, 0); /*处理主机名参数*/
h = sock_ntop_host(ai->ai_addr, ai->ai_addrlen);
printf("PING %s (%s): %d data bytes \n", ai->ai_canonname? ai->ai_canonname:h, h, datalen);
if ( ai->ai_family == AF_INET )
{
pr = &proto_v4;
#ifdef IPV6
}
else if (ai->ai_family == AF_INET6)
{
pr = &proto_v6;
if(IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *) ai->ai_addr)->sin6_addr)))
printf("cannot ping IPV4-mapped IPV6 address");
#endif
}
else
printf("unknown address family %d", ai->ai_family);
pr->sasend = ai->ai_addr;
pr->sarecv = calloc(1, ai->ai_addrlen);
pr->salen = ai->ai_addrlen;
readloop();
exit(0);
}
void readloop(void)
{
int size;
char recvbuf[BUFSIZE];
char controlbuf[BUFSIZE];
struct msghdr msg;
struct iovec iov;
ssize_t n;
struct timeval tval;
sockfd = socket(pr->sasend->sa_family, SOCK_RAW, pr->icmpproto); /*创建原始的套接字*/
setuid(getuid()); /*不需要特殊的允许*/
if( pr->finit)
(*pr->finit)();
size = 60*1024;
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); /*设置套接字接受缓冲区的大小*/
sig_alrm(SIGALRM); /*发送第一个分组*/
iov.iov_base = recvbuf; /*为recvmsg设置msghdr结构*/
iov.iov_len = sizeof(recvbuf);
msg.msg_name = pr->sarecv;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = controlbuf;
for( ; ;) /*读取 所有ICMP消息的无限循环*/
{
msg.msg_namelen = pr->salen;
msg.msg_controllen = sizeof(controlbuf);
n = recvmsg(sockfd, &msg, 0);
if( n < 0)
{
if( errno == EINTR)
continue;
else
printf("recvmsg error");
}
}
gettimeofday(&tval, NULL);
(*pr->fproc)(recvbuf, n, &msg, &tval);
}
void tv_sub(struct timeval *out, struct timeval *in)
{
if ( ( out->tv_usec -= in->tv_usec) < 0) /*out -=in*/
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
void proc_v4(char *ptr, ssize_t len, struct msghdr *msg, struct timeval *tvrecv)
{
int hlen1, icmplen;
double rtt;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
ip = (struct ip*)ptr; /*IP节的头*/
hlen1 = ip->ip_hl << 2; /*IP节的长度*/
if( ip->ip_p != IPPROTO_ICMP)
return; /*没有ICMP*/
icmp = (struct icmp*) (ptr + hlen1); /*ICMP节的头*/
if( (icmplen = len -hlen1) < 8)
return;
if(icmp->icmp_type == ICMP_ECHOREPLY)
{
if( icmp->icmp_id != pid) /*不是对我们的ECHO_REQUEST进行回复*/
return;
if( icmplen < 16)
return; /*没有足够数据可用*/
tvsend = (struct timeval *)icmp->icmp_data;
tv_sub(tvrecv, tvsend);
rtt = tvrecv->tv_sec*1000.0 + tvrecv->tv_usec/1000.0;
printf("%d bytes from %s: seq=%u, ttl=%d, rtt=%.3f ms\n",icmplen, sock_ntop_host(pr->sarecv, pr->salen),
icmp->icmp_seq, ip->ip_ttl, rtt);
}
else if(verbose)
{
printf("%d bytes from %s: type = %d, code = %d\n", icmplen, sock_ntop_host(pr->sarecv, pr->salen),
icmp->icmp_type, icmp->icmp_code);
}
}
void init_v6()
{
#ifdef IPV6
int on = 1;
if ( verbose == 0) /*设置ICMPv6接收过滤器*/
{
struct icmp6_filter myfilt;
ICMP6_FILTER_SETBLOCKALL(&myflit);
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &myflit);
setsockopt(sockfd, IPPROTO_IPV6, ICMP6_FILTER, &myflit, sizeof(myflit));
}
#ifdef IPV6_RECVHOPLIMIT
setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof(on));
#else
setsockopt(sockfd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof(on));
#endif
#endif
}
void proc_v6(char *ptr, ssize_t len, struct msghdr *msg, struct timeval *tvrecv)
{
#ifdef IPV6
double rtt;
struct icmp6_hdr* icmp6;
struct timeval * tvsend;
struct cmsghdr *cmsg;
int hlim;
icmp6 = (struct icmp6_hdr *)ptr; /*获取ICMPV6首部的指针*/
if( len < 8)
return;
if(icmp6->icmp6_type == ICMP6_ECHO_REPLY) /*检查ICMP回射应答*/
{
if( icmp6->icmp6_id != pid) /*不是对我们的ECHO_REQUEST进行回复*/
return;
if(len < 16)
return; /*没有足够数据可用*/
tvsend = (struct timeval *)(icmp6 + 1);
tv_sub(tvrecv, tvsend);
rtt = tvrecv->tv_sec*1000.0 + tvrecv->tv_usec/1000.0;
hlim = -1;
for(cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg,cmsg))
{
if(cmsg->cmsg_level == IPPROTO_IPv6 && cmsg->cmsg_type == IPV6_HOPLIMIT)
{
hlim = *(u_int32_t*)CMSG_DATA(cmsg);
break;
}
}
printf("%d bytes from %s:seq = %u, hlim =", len,sock_ntop_host(pr->sarecv, pr->salen), icmp6->icmp6_seq);
if(hlim == -1)
printf("???");
else
printf("%d",hlim);
printf(", rtt=%.3f ms\n", rtt);
}
else if( verbose) /*若指定-v则显示所有接收ICMP消息*/
{
printf(" %d bytes from %s: type = %d, code = %d\n", len, sock_ntop_host(pr->sarecv, pr->salen),
icmp6->icmp6_type, icmp6->icmp6_code);
}
#endif
}
void sig_alrm(int signo)
{
(*pr->fsend)();
alarm(1);
return;
}
uint16_t in_cksum(uint16_t *addr, int len)
{
int nleft = len;
uint32_t sum = 0;
uint16_t *w = addr;
uint16_t answer = 0;
while ( nleft > 1)
{
sum+=*w++;
nleft -= 2;
}
if( nleft == 1)
{
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >>16) + (sum&0xffff);
sum += (sum >>16);
answer = ~sum;
return (answer);
}
void send_v4(void)
{
int len;
struct icmp *icmp;
icmp = (struct icmp *)sendbuf; /*构造ICMPV4消息*/
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_id = pid;
icmp->icmp_seq = nsent++;
memset(icmp->icmp_data, 0xa5, datalen); /*按照icmp模式来填写*/
gettimeofday( ( struct timeval *) icmp->icmp_data, NULL);
len = 8+ datalen; /*校验ICMP 节和数据*/
icmp->icmp_cksum = 0;
icmp->icmp_cksum = in_cksum(( u_short *)icmp, len);
sendto(sockfd, sendbuf, len, 0, pr->sasend, pr->salen); /*发送数据报*/
}
void send_v6(void)
{
#ifdef IPV6
int len;
struct icmp6_hdr *icmp6;
icmp6 = (struct icmp6_hdr *)sendbuf;
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_id = pid;
icmp6->icmp6_seq = nsent++;
memset((icmp6+1), 0xa5, datalen);
gettimeofday( ( struct timeval *) (icmp6+1), NULL);
len = 8+ datalen;
sendto(sockfd, sendbuf, len, 0, pr->sasend, pr->salen);
#endif
}
|