summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko2007-02-09 18:14:42 +0000
committerDenis Vlasenko2007-02-09 18:14:42 +0000
commitc8e9993514f3be99be4eea75878ccc1b6ac85cea (patch)
treeb5d953c8569b18d0e23ca0d742c739a6a8aab4b7
parent9ca26d38c513c918cf88db8fef057b7ae5c133f0 (diff)
downloadbusybox-c8e9993514f3be99be4eea75878ccc1b6ac85cea.zip
busybox-c8e9993514f3be99be4eea75878ccc1b6ac85cea.tar.gz
ping: reuse another bit of common code. -60 bytes
-rw-r--r--networking/ping.c44
1 files changed, 16 insertions, 28 deletions
diff --git a/networking/ping.c b/networking/ping.c
index 9bf3813..4c1ec83 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -302,13 +302,21 @@ static void pingstats(int junk ATTRIBUTE_UNUSED)
exit(status);
}
-static void sendping_tail(void (*sp)(int), int sz, int sizeof_packet)
+static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
{
+ int sz;
+
+ CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
+ ntransmitted++;
+
+ /* sizeof(pingaddr) can be larger than real sa size, but I think
+ * it doesn't matter */
+ sz = sendto(pingsock, pkt, size_pkt, 0, &pingaddr.sa, sizeof(pingaddr));
if (sz < 0)
bb_perror_msg_and_die("sendto");
- if (sz != sizeof_packet)
+ if (sz != size_pkt)
bb_error_msg_and_die("ping wrote %d chars; %d expected", sz,
- sizeof_packet);
+ size_pkt);
signal(SIGALRM, sp);
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)
static void sendping4(int junk ATTRIBUTE_UNUSED)
{
- struct icmp *pkt;
- int i;
- char packet[datalen + ICMP_MINLEN];
-
- pkt = (struct icmp *) packet;
+ struct icmp *pkt = alloca(datalen + ICMP_MINLEN);
pkt->icmp_type = ICMP_ECHO;
pkt->icmp_code = 0;
pkt->icmp_cksum = 0;
pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
pkt->icmp_id = myid;
- CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
- ntransmitted++;
-
gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
- pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
-
- i = sendto(pingsock, packet, sizeof(packet), 0,
- &pingaddr.sa, sizeof(pingaddr.sin));
+ pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
- sendping_tail(sendping4, i, sizeof(packet));
+ sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
}
#if ENABLE_PING6
static void sendping6(int junk ATTRIBUTE_UNUSED)
{
- struct icmp6_hdr *pkt;
- int i;
- char packet[datalen + sizeof (struct icmp6_hdr)];
-
- pkt = (struct icmp6_hdr *) packet;
+ struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr));
pkt->icmp6_type = ICMP6_ECHO_REQUEST;
pkt->icmp6_code = 0;
pkt->icmp6_cksum = 0;
pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
pkt->icmp6_id = myid;
- CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
- ntransmitted++;
-
gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
- i = sendto(pingsock, packet, sizeof(packet), 0,
- &pingaddr.sa, sizeof(pingaddr.sin6));
-
- sendping_tail(sendping6, i, sizeof(packet));
+ sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
}
#endif