diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-02-09 18:14:42 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-02-09 18:14:42 +0000 |
commit | c8e9993514f3be99be4eea75878ccc1b6ac85cea (patch) | |
tree | b5d953c8569b18d0e23ca0d742c739a6a8aab4b7 /networking/ping.c | |
parent | 9ca26d38c513c918cf88db8fef057b7ae5c133f0 (diff) | |
download | busybox-w32-c8e9993514f3be99be4eea75878ccc1b6ac85cea.tar.gz busybox-w32-c8e9993514f3be99be4eea75878ccc1b6ac85cea.tar.bz2 busybox-w32-c8e9993514f3be99be4eea75878ccc1b6ac85cea.zip |
ping: reuse another bit of common code. -60 bytes
Diffstat (limited to 'networking/ping.c')
-rw-r--r-- | networking/ping.c | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/networking/ping.c b/networking/ping.c index 9bf38130e..4c1ec836e 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -302,13 +302,21 @@ static void pingstats(int junk ATTRIBUTE_UNUSED) | |||
302 | exit(status); | 302 | exit(status); |
303 | } | 303 | } |
304 | 304 | ||
305 | static void sendping_tail(void (*sp)(int), int sz, int sizeof_packet) | 305 | static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt) |
306 | { | 306 | { |
307 | int sz; | ||
308 | |||
309 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); | ||
310 | ntransmitted++; | ||
311 | |||
312 | /* sizeof(pingaddr) can be larger than real sa size, but I think | ||
313 | * it doesn't matter */ | ||
314 | sz = sendto(pingsock, pkt, size_pkt, 0, &pingaddr.sa, sizeof(pingaddr)); | ||
307 | if (sz < 0) | 315 | if (sz < 0) |
308 | bb_perror_msg_and_die("sendto"); | 316 | bb_perror_msg_and_die("sendto"); |
309 | if (sz != sizeof_packet) | 317 | if (sz != size_pkt) |
310 | bb_error_msg_and_die("ping wrote %d chars; %d expected", sz, | 318 | bb_error_msg_and_die("ping wrote %d chars; %d expected", sz, |
311 | sizeof_packet); | 319 | size_pkt); |
312 | 320 | ||
313 | signal(SIGALRM, sp); | 321 | signal(SIGALRM, sp); |
314 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ | 322 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
@@ -322,51 +330,31 @@ static void sendping_tail(void (*sp)(int), int sz, int sizeof_packet) | |||
322 | 330 | ||
323 | static void sendping4(int junk ATTRIBUTE_UNUSED) | 331 | static void sendping4(int junk ATTRIBUTE_UNUSED) |
324 | { | 332 | { |
325 | struct icmp *pkt; | 333 | struct icmp *pkt = alloca(datalen + ICMP_MINLEN); |
326 | int i; | ||
327 | char packet[datalen + ICMP_MINLEN]; | ||
328 | |||
329 | pkt = (struct icmp *) packet; | ||
330 | 334 | ||
331 | pkt->icmp_type = ICMP_ECHO; | 335 | pkt->icmp_type = ICMP_ECHO; |
332 | pkt->icmp_code = 0; | 336 | pkt->icmp_code = 0; |
333 | pkt->icmp_cksum = 0; | 337 | pkt->icmp_cksum = 0; |
334 | pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ | 338 | pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ |
335 | pkt->icmp_id = myid; | 339 | pkt->icmp_id = myid; |
336 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); | ||
337 | ntransmitted++; | ||
338 | |||
339 | gettimeofday((struct timeval *) &pkt->icmp_dun, NULL); | 340 | gettimeofday((struct timeval *) &pkt->icmp_dun, NULL); |
340 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); | 341 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN); |
341 | |||
342 | i = sendto(pingsock, packet, sizeof(packet), 0, | ||
343 | &pingaddr.sa, sizeof(pingaddr.sin)); | ||
344 | 342 | ||
345 | sendping_tail(sendping4, i, sizeof(packet)); | 343 | sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN); |
346 | } | 344 | } |
347 | #if ENABLE_PING6 | 345 | #if ENABLE_PING6 |
348 | static void sendping6(int junk ATTRIBUTE_UNUSED) | 346 | static void sendping6(int junk ATTRIBUTE_UNUSED) |
349 | { | 347 | { |
350 | struct icmp6_hdr *pkt; | 348 | struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr)); |
351 | int i; | ||
352 | char packet[datalen + sizeof (struct icmp6_hdr)]; | ||
353 | |||
354 | pkt = (struct icmp6_hdr *) packet; | ||
355 | 349 | ||
356 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; | 350 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
357 | pkt->icmp6_code = 0; | 351 | pkt->icmp6_code = 0; |
358 | pkt->icmp6_cksum = 0; | 352 | pkt->icmp6_cksum = 0; |
359 | pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ | 353 | pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ |
360 | pkt->icmp6_id = myid; | 354 | pkt->icmp6_id = myid; |
361 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); | ||
362 | ntransmitted++; | ||
363 | |||
364 | gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL); | 355 | gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL); |
365 | 356 | ||
366 | i = sendto(pingsock, packet, sizeof(packet), 0, | 357 | sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr)); |
367 | &pingaddr.sa, sizeof(pingaddr.sin6)); | ||
368 | |||
369 | sendping_tail(sendping6, i, sizeof(packet)); | ||
370 | } | 358 | } |
371 | #endif | 359 | #endif |
372 | 360 | ||