+static void noresp(int ign)
+{
+ printf("No response from %s\n", hostname);
+ exit(EXIT_FAILURE);
+}
+
+static void ping(const char *host)
+{
+ struct hostent *h;
+ struct sockaddr_in pingaddr;
+ struct icmp *pkt;
+ int pingsock, c;
+ char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
+
+ pingsock = create_icmp_socket();
+
+ memset(&pingaddr, 0, sizeof(struct sockaddr_in));
+
+ pingaddr.sin_family = AF_INET;
+ h = xgethostbyname(host);
+ memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
+ hostname = h->h_name;
+
+ pkt = (struct icmp *) packet;
+ memset(pkt, 0, sizeof(packet));
+ pkt->icmp_type = ICMP_ECHO;
+ pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
+
+ c = sendto(pingsock, packet, sizeof(packet), 0,
+ (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
+
+ if (c < 0 || c != sizeof(packet))
+ bb_perror_msg_and_die("sendto");
+
+ signal(SIGALRM, noresp);
+ alarm(5); /* give the host 5000ms to respond */
+ /* listen for replies */
+ while (1) {
+ struct sockaddr_in from;
+ size_t fromlen = sizeof(from);
+
+ if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
+ (struct sockaddr *) &from, &fromlen)) < 0) {
+ if (errno == EINTR)
+ continue;
+ bb_perror_msg("recvfrom");
+ continue;
+ }
+ if (c >= 76) { /* ip + icmp */
+ struct iphdr *iphdr = (struct iphdr *) packet;
+
+ pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
+ if (pkt->icmp_type == ICMP_ECHOREPLY)
+ break;
+ }
+ }
+ printf("%s is alive!\n", hostname);
+ return;
+}
+
+extern int ping_main(int argc, char **argv)
+{
+ argc--;
+ argv++;
+ if (argc < 1)
+ bb_show_usage();
+ ping(*argv);
+ return EXIT_SUCCESS;
+}
+
+#else /* ! CONFIG_FEATURE_FANCY_PING */
+/* full(er) version */