-#include <arpa/inet.h>
-#include <netdb.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include "busybox.h"
-
-
-static const int DEFDATALEN = 56;
-static const int MAXIPLEN = 60;
-static const int MAXICMPLEN = 76;
-static const int MAXPACKET = 65468;
-#define MAX_DUP_CHK (8 * 128)
-static const int MAXWAIT = 10;
-static const int PINGINTERVAL = 1; /* second */
-
-#define O_QUIET (1 << 0)
-
-#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
-#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
-#define SET(bit) (A(bit) |= B(bit))
-#define CLR(bit) (A(bit) &= (~B(bit)))
-#define TST(bit) (A(bit) & B(bit))
-
-static void ping(const char *host);
-
-/* common routines */
-static int in_cksum(unsigned short *buf, int sz)
-{
- int nleft = sz;
- int sum = 0;
- unsigned short *w = buf;
- unsigned short ans = 0;
-
- while (nleft > 1) {
- sum += *w++;
- nleft -= 2;
- }
+#include "libbb.h"
+
+#ifdef __BIONIC__
+/* should be in netinet/ip_icmp.h */
+# define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
+# define ICMP_SOURCE_QUENCH 4 /* Source Quench */
+# define ICMP_REDIRECT 5 /* Redirect (change route) */
+# define ICMP_ECHO 8 /* Echo Request */
+# define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
+# define ICMP_PARAMETERPROB 12 /* Parameter Problem */
+# define ICMP_TIMESTAMP 13 /* Timestamp Request */
+# define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
+# define ICMP_INFO_REQUEST 15 /* Information Request */
+# define ICMP_INFO_REPLY 16 /* Information Reply */
+# define ICMP_ADDRESS 17 /* Address Mask Request */
+# define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
+#endif
+
+//config:config PING
+//config: bool "ping"
+//config: default y
+//config: select PLATFORM_LINUX
+//config: help
+//config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
+//config: elicit an ICMP ECHO_RESPONSE from a host or gateway.
+//config:
+//config:config PING6
+//config: bool "ping6"
+//config: default y
+//config: depends on FEATURE_IPV6 && PING
+//config: help
+//config: This will give you a ping that can talk IPv6.
+//config:
+//config:config FEATURE_FANCY_PING
+//config: bool "Enable fancy ping output"
+//config: default y
+//config: depends on PING
+//config: help
+//config: Make the output from the ping applet include statistics, and at the
+//config: same time provide full support for ICMP packets.
+
+/* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */
+//applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE))
+//applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE))
+
+//kbuild:lib-$(CONFIG_PING) += ping.o
+//kbuild:lib-$(CONFIG_PING6) += ping.o
+
+//usage:#if !ENABLE_FEATURE_FANCY_PING
+//usage:# define ping_trivial_usage
+//usage: "HOST"
+//usage:# define ping_full_usage "\n\n"
+//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
+//usage:# define ping6_trivial_usage
+//usage: "HOST"
+//usage:# define ping6_full_usage "\n\n"
+//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
+//usage:#else
+//usage:# define ping_trivial_usage
+//usage: "[OPTIONS] HOST"
+//usage:# define ping_full_usage "\n\n"
+//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
+//usage: IF_PING6(
+//usage: "\n -4,-6 Force IP or IPv6 name resolution"
+//usage: )
+//usage: "\n -c CNT Send only CNT pings"
+//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
+//usage: "\n -t TTL Set TTL"
+//usage: "\n -I IFACE/IP Use interface or IP address as source"
+//usage: "\n -W SEC Seconds to wait for the first response (default:10)"
+//usage: "\n (after all -c CNT packets are sent)"
+//usage: "\n -w SEC Seconds until ping exits (default:infinite)"
+//usage: "\n (can exit earlier with -c CNT)"
+//usage: "\n -q Quiet, only display output at start"
+//usage: "\n and when finished"
+//usage: "\n -p Pattern to use for payload"
+//usage:
+//usage:# define ping6_trivial_usage
+//usage: "[OPTIONS] HOST"
+//usage:# define ping6_full_usage "\n\n"
+//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
+//usage: "\n -c CNT Send only CNT pings"
+//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
+//usage: "\n -I IFACE/IP Use interface or IP address as source"
+//usage: "\n -q Quiet, only display output at start"
+//usage: "\n and when finished"
+//usage: "\n -p Pattern to use for payload"
+//usage:
+//usage:#endif
+//usage:
+//usage:#define ping_example_usage
+//usage: "$ ping localhost\n"
+//usage: "PING slag (127.0.0.1): 56 data bytes\n"
+//usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
+//usage: "\n"
+//usage: "--- debian ping statistics ---\n"
+//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
+//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
+//usage:#define ping6_example_usage
+//usage: "$ ping6 ip6-localhost\n"
+//usage: "PING ip6-localhost (::1): 56 data bytes\n"
+//usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
+//usage: "\n"
+//usage: "--- ip6-localhost ping statistics ---\n"
+//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
+//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
+
+#if ENABLE_PING6
+# include <netinet/icmp6.h>
+/* I see RENUMBERED constants in bits/in.h - !!?
+ * What a fuck is going on with libc? Is it a glibc joke? */
+# ifdef IPV6_2292HOPLIMIT
+# undef IPV6_HOPLIMIT
+# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
+# endif
+#endif