work in progress...
[oweals/busybox.git] / ping.c
diff --git a/ping.c b/ping.c
index 9f8a103bd8e7ad4ccb592f6cac98e1e6ef7f68d9..92b62def3f91d7fdc2a25e07486447f962a7ebb8 100644 (file)
--- a/ping.c
+++ b/ping.c
@@ -1,5 +1,5 @@
 /*
- * $Id: ping.c,v 1.1 1999/12/07 23:14:59 andersen Exp $
+ * $Id: ping.c,v 1.6 1999/12/11 08:41:28 andersen Exp $
  * Mini ping implementation for busybox
  *
  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
@@ -53,6 +53,7 @@
 #define        MAXPACKET       65468
 #define        MAX_DUP_CHK     (8 * 128)
 #define MAXWAIT         10
+#define PINGINTERVAL    1 /* second */
 
 #define O_QUIET         (1 << 0)
 
 #define        CLR(bit)        (A(bit) &= (~B(bit)))
 #define        TST(bit)        (A(bit) & B(bit))
 
-static const char* ping_usage = "ping [OPTION]... host\n\n"
-"Options:\n"
-"\t-q\t\tquiet\n"
-"\t-c\t\tping count\n";
-
-static char *hostname = NULL;
-static struct sockaddr_in pingaddr;
-static int pingsock = -1;
-static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
-static int myid = 0, options = 0;
-static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
-static char rcvd_tbl[MAX_DUP_CHK / 8];
-
-static void pingstats(int);
-static void sendping(int);
-static void unpack(char *, int, struct sockaddr_in *);
-static void ping(char *);
-static int in_cksum(unsigned short *, int);
-
-/**************************************************************************/
-
+/* common routines */
 static int in_cksum(unsigned short *buf, int sz)
 {
     int nleft = sz;
@@ -106,6 +87,114 @@ static int in_cksum(unsigned short *buf, int sz)
     return(ans);
 }   
 
+/* simple version */
+#ifdef BB_SIMPLE_PING
+static const char* ping_usage = "ping host\n\n";
+
+static char* hostname = NULL;
+
+static void noresp(int ign)
+{
+    printf("No response from %s\n", hostname);
+    exit(0);
+}
+
+static int ping(const char *host)
+{
+    struct hostent *h;
+    struct sockaddr_in pingaddr;
+    struct icmp *pkt;
+    int pingsock, c;
+    char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
+    
+    if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */
+        perror("ping");
+       exit(1);
+    }
+
+    /* drop root privs if running setuid */
+    setuid(getuid());
+    
+    memset(&pingaddr, 0, sizeof(struct sockaddr_in));
+    pingaddr.sin_family = AF_INET;
+    if (!(h = gethostbyname(host))) {
+        fprintf(stderr, "ping: unknown host %s\n", host);
+        exit(1);
+    }  
+    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)) {
+        if (c < 0) perror("ping");
+        fprintf(stderr, "ping: write incomplete\n");
+       exit(1);
+    }
+
+    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;
+           perror("ping");
+           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(TRUE);
+}
+
+extern int ping_main(int argc, char **argv)
+{
+    argc--;
+    argv++;
+    if (argc < 1) usage(ping_usage);
+    ping(*argv);
+    exit(TRUE);
+}
+
+#else 
+/* full(er) version */
+static const char* ping_usage = "ping [OPTION]... host\n\n"
+"Send ICMP ECHO_REQUEST packets to network hosts.\n\n"
+"Options:\n"
+"\t-q\t\tQuiet mode, only displays output at start and when finished.\n"
+"\t-c COUNT\tSend only COUNT pings.\n";
+
+static char *hostname = NULL;
+static struct sockaddr_in pingaddr;
+static int pingsock = -1;
+
+static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
+static int myid = 0, options = 0;
+static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
+static char rcvd_tbl[MAX_DUP_CHK / 8];
+
+static void sendping(int);
+static void pingstats(int);
+static void unpack(char *, int, struct sockaddr_in *);
+
+static void ping(char *);
+
+/**************************************************************************/
+
 static void pingstats(int ign) {
     signal(SIGINT, SIG_IGN);
     
@@ -154,8 +243,8 @@ static void sendping(int ign)
     }
 
     signal(SIGALRM, sendping);
-    if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next */
-        alarm(1);
+    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);
@@ -242,30 +331,25 @@ static void ping(char *host)
        exit(1);
     }
 
-#ifdef SUID_BUSYBOX
+    /* drop root privs if running setuid */
     setuid(getuid());
-#endif
     
     memset(&pingaddr, 0, sizeof(struct sockaddr_in));
     pingaddr.sin_family = AF_INET;
-    if (inet_aton(host, &pingaddr.sin_addr)) {
-        hostname = host;
-    } else {    
-        if (!(h = gethostbyname(host))) {
-            fprintf(stderr, "ping: unknown host %s\n", host);
-           exit(1);
-        }
+    if (!(h = gethostbyname(host))) {
+        fprintf(stderr, "ping: unknown host %s\n", host);
+        exit(1);
+    }
 
-       if (h->h_addrtype != AF_INET) {
-           fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
-           exit(1);
-       }
-       
-       pingaddr.sin_family = AF_INET; /* h->h_addrtype */
-        memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); 
-        strncpy(buf, h->h_name, sizeof(buf)-1);
-       hostname = buf;
+    if (h->h_addrtype != AF_INET) {
+        fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
+        exit(1);
     }
+       
+    pingaddr.sin_family = AF_INET; /* h->h_addrtype */
+    memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); 
+    strncpy(buf, h->h_name, sizeof(buf)-1);
+    hostname = buf;
 
     /* enable broadcast pings */
     sockopt = 1;
@@ -304,36 +388,33 @@ static void ping(char *host)
 
 extern int ping_main(int argc, char **argv)
 {
+    char *thisarg;
+    
     argc--;
     argv++;
     options = 0;
     /* Parse any options */
-    if (argc < 1) usage(ping_usage);
-
-    while (**argv == '-') {
-       while (*++(*argv))
-           switch (**argv) {
-           case 'c':
-               argc--; argv++;
-               if (argc < 1) usage(ping_usage);
-               pingcount = atoi(*argv);
-               break;
-           case 'q':
-               options |= O_QUIET;
-               break;
-           default:
-               usage(ping_usage);
-           }
-       argc--;
-       argv++;
+    while (argc > 1) {
+        if (**argv != '-') usage(ping_usage);
+        thisarg = *argv; thisarg++;
+        switch (*thisarg) {
+           case 'q': options |= O_QUIET; break;
+           case 'c':
+               argc--; argv++;
+               pingcount = atoi(*argv);                
+               break;
+           default:
+               usage(ping_usage);
+        }
+        argc--; argv++;
     }
-
-    if (argc < 1) usage(ping_usage);
+    if (argc < 1) usage(ping_usage);       
 
     myid = getpid() & 0xFFFF;
-    ping(*(argv++));
-    exit( TRUE);
+    ping(*argv);
+    exit(TRUE);
 }
+#endif
 
 /*
  * Copyright (c) 1989 The Regents of the University of California.
@@ -370,5 +451,3 @@ extern int ping_main(int argc, char **argv)
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-
-