#include <net/if.h>
#include <netinet/ip_icmp.h>
#include "busybox.h"
+
#if ENABLE_PING6
#include <netinet/icmp6.h>
/* I see RENUMBERED constants in bits/in.h - !!?
exit(EXIT_FAILURE);
}
-static void ping(len_and_sockaddr *lsa)
+static void ping4(len_and_sockaddr *lsa)
{
struct sockaddr_in pingaddr;
struct icmp *pkt;
ping6(lsa);
else
#endif
- ping(lsa);
+ ping4(lsa);
printf("%s is alive!\n", hostname);
return EXIT_SUCCESS;
}
exit(status);
}
-static void sendping(int junk ATTRIBUTE_UNUSED)
+static void sendping_tail(void (*sp)(int), int sz, int sizeof_packet)
+{
+ if (sz < 0)
+ bb_perror_msg_and_die("sendto");
+ if (sz != sizeof_packet)
+ bb_error_msg_and_die("ping wrote %d chars; %d expected", sz,
+ sizeof_packet);
+
+ signal(SIGALRM, sp);
+ if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
+ alarm(PINGINTERVAL);
+ } else { /* done, wait for the last ping to come back */
+ /* todo, don't necessarily need to wait so long... */
+ signal(SIGALRM, pingstats);
+ alarm(MAXWAIT);
+ }
+}
+
+static void sendping4(int junk ATTRIBUTE_UNUSED)
{
struct icmp *pkt;
int i;
i = sendto(pingsock, packet, sizeof(packet), 0,
&pingaddr.sa, sizeof(pingaddr.sin));
- if (i < 0)
- bb_perror_msg_and_die("sendto");
- if ((size_t)i != sizeof(packet))
- bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
- (int)sizeof(packet));
-
- signal(SIGALRM, sendping);
- if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
- alarm(PINGINTERVAL);
- } else { /* done, wait for the last ping to come back */
- /* todo, don't necessarily need to wait so long... */
- signal(SIGALRM, pingstats);
- alarm(MAXWAIT);
- }
+ sendping_tail(sendping4, i, sizeof(packet));
}
#if ENABLE_PING6
static void sendping6(int junk ATTRIBUTE_UNUSED)
i = sendto(pingsock, packet, sizeof(packet), 0,
&pingaddr.sa, sizeof(pingaddr.sin6));
- if (i < 0)
- bb_perror_msg_and_die("sendto");
- if ((size_t)i != sizeof(packet))
- bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
- (int)sizeof(packet));
-
- signal(SIGALRM, sendping6);
- if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
- alarm(PINGINTERVAL);
- } else { /* done, wait for the last ping to come back */
- /* todo, don't necessarily need to wait so long... */
- signal(SIGALRM, pingstats);
- alarm(MAXWAIT);
- }
+ sendping_tail(sendping6, i, sizeof(packet));
}
#endif
}
#endif
-static void unpack(char *buf, int sz, struct sockaddr_in *from)
+static void unpack4(char *buf, int sz, struct sockaddr_in *from)
{
struct icmp *icmppkt;
struct iphdr *iphdr;
}
#endif
-static void ping(len_and_sockaddr *lsa)
+static void ping4(len_and_sockaddr *lsa)
{
char packet[datalen + MAXIPLEN + MAXICMPLEN];
int sockopt;
signal(SIGINT, pingstats);
/* start the ping's going ... */
- sendping(0);
+ sendping4(0);
/* listen for replies */
while (1) {
bb_perror_msg("recvfrom");
continue;
}
- unpack(packet, c, &from);
+ unpack4(packet, c, &from);
if (pingcount > 0 && nreceived >= pingcount)
break;
}
datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
- /* exactly one argument needed, -v and -q don't mix. So do 4, 6 */
+ /* exactly one argument needed, -v and -q don't mix */
opt_complementary = "=1:q--v:v--q";
getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
if (option_mask32 & OPT_c) pingcount = xatoul(opt_c); // -c
ping6(lsa);
else
#endif
- ping(lsa);
+ ping4(lsa);
pingstats(0);
return EXIT_SUCCESS;
}