oping: Drop root privileges as soon as possible.
[liboping.git] / src / oping.c
index 5f0940d..3201e85 100644 (file)
@@ -4,8 +4,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation; only version 2 of the License is
+ * applicable.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -70,9 +70,10 @@ typedef struct ping_context
        double latency_total_square;
 } ping_context_t;
 
-static double opt_interval   = 1.0;
-static int    opt_addrfamily = PING_DEF_AF;
-static int    opt_count      = -1;
+static double  opt_interval   = 1.0;
+static int     opt_addrfamily = PING_DEF_AF;
+static char   *opt_srcaddr    = NULL;
+static int     opt_count      = -1;
 
 void sigint_handler (int signal)
 {
@@ -115,7 +116,7 @@ int read_options (int argc, char **argv)
 
        while (1)
        {
-               optchar = getopt (argc, argv, "46c:i:");
+               optchar = getopt (argc, argv, "46c:hi:I:");
 
                if (optchar == -1)
                        break;
@@ -144,7 +145,15 @@ int read_options (int argc, char **argv)
                                                opt_interval = new_interval;
                                }
                                break;
+                       case 'I':
+                               {
+                                       if (opt_srcaddr != NULL)
+                                               free (opt_srcaddr);
+                                       opt_srcaddr = strdup (optarg);
+                               }
+                               break;
 
+                       case 'h':
                        default:
                                usage_exit (argv[0]);
                }
@@ -155,9 +164,10 @@ int read_options (int argc, char **argv)
 
 void print_host (pingobj_iter_t *iter)
 {
-       double   latency;
-       uint16_t sequence;
-       size_t   buffer_len;
+       double          latency;
+       unsigned int    sequence;
+       size_t          buffer_len;
+       size_t          data_len;
        ping_context_t *context;
        
        buffer_len = sizeof (latency);
@@ -168,6 +178,10 @@ void print_host (pingobj_iter_t *iter)
        ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
                        &sequence, &buffer_len);
 
+       data_len = 0;
+       ping_iterator_get_info (iter, PING_INFO_DATA,
+                       NULL, &data_len);
+
        context = (ping_context_t *) ping_iterator_get_context (iter);
 
        context->req_sent++;
@@ -182,7 +196,8 @@ void print_host (pingobj_iter_t *iter)
                if ((context->latency_min < 0.0) || (context->latency_min > latency))
                        context->latency_min = latency;
 
-               printf ("echo reply from %s (%s): icmp_seq=%u time=%.2f ms\n",
+               printf ("%u bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
+                               (unsigned int) data_len,
                                context->host, context->addr,
                                (unsigned int) sequence, latency);
        }
@@ -256,17 +271,17 @@ int main (int argc, char **argv)
        int optind;
        int i;
 
+       optind = read_options (argc, argv);
+
+       if (optind >= argc)
+               usage_exit (argv[0]);
+
        if (geteuid () != 0)
        {
                fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
                return (1);
        }
 
-       optind = read_options (argc, argv);
-
-       if (optind >= argc)
-               usage_exit (argv[0]);
-
        if ((ping = ping_construct ()) == NULL)
        {
                fprintf (stderr, "ping_construct failed\n");
@@ -287,15 +302,30 @@ int main (int argc, char **argv)
        if (opt_addrfamily != PING_DEF_AF)
                ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
 
+       if (opt_srcaddr != NULL)
+       {
+               if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
+               {
+                       fprintf (stderr, "Setting source address failed: %s\n",
+                                       ping_get_error (ping));
+               }
+       }
+
        for (i = optind; i < argc; i++)
        {
-               if (ping_host_add (ping, argv[i]) > 0)
+               if (ping_host_add (ping, argv[i]) < 0)
                {
-                       fprintf (stderr, "ping_host_add (%s) failed\n", argv[i]);
+                       const char *errmsg = ping_get_error (ping);
+
+                       fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
                        continue;
                }
        }
 
+       /* Drop root privileges if we're setuid-root. */
+       setuid (getuid ());
+
+       i = 0;
        for (iter = ping_iterator_get (ping);
                        iter != NULL;
                        iter = ping_iterator_next (iter))
@@ -311,9 +341,20 @@ int main (int argc, char **argv)
                buffer_size = sizeof (context->addr);
                ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
 
+               buffer_size = 0;
+               ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
+
+               printf ("PING %s (%s) %u bytes of data.\n",
+                               context->host, context->addr, (unsigned int) buffer_size);
+
                ping_iterator_set_context (iter, (void *) context);
+
+               i++;
        }
 
+       if (i == 0)
+               return (1);
+
        memset (&sigint_action, '\0', sizeof (sigint_action));
        sigint_action.sa_handler = sigint_handler;
        if (sigaction (SIGINT, &sigint_action, NULL) < 0)
@@ -334,7 +375,8 @@ int main (int argc, char **argv)
 
                if (ping_send (ping) < 0)
                {
-                       fprintf (stderr, "ping_send failed\n");
+                       fprintf (stderr, "ping_send failed: %s\n",
+                                       ping_get_error (ping));
                        return (1);
                }
 
@@ -400,7 +442,8 @@ int main (int argc, char **argv)
                        num_total = (double) context->req_rcvd;
 
                        average = context->latency_total / num_total;
-                       deviation = sqrt ((context->latency_total_square / num_total) - (average * average));
+                       deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
+                                       / (num_total * (num_total - 1.0)));
 
                        printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
                                        context->latency_min,