src/oping.c: Allow shorter interval settings.
[liboping.git] / src / oping.c
index eb97e5d..cfbe18a 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,17 +70,20 @@ 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)
+static void sigint_handler (int signal)
 {
+       /* Make compiler happy */
+       signal = 0;
        /* Exit the loop */
        opt_count = 0;
 }
 
-ping_context_t *context_create (void)
+static ping_context_t *context_create (void)
 {
        ping_context_t *ret;
 
@@ -97,25 +100,25 @@ ping_context_t *context_create (void)
        return (ret);
 }
 
-void context_destroy (ping_context_t *context)
+static void context_destroy (ping_context_t *context)
 {
        free (context);
 }
 
-void usage_exit (const char *name)
+static void usage_exit (const char *name)
 {
        fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
                        name);
        exit (1);
 }
 
-int read_options (int argc, char **argv)
+static int read_options (int argc, char **argv)
 {
        int optchar;
 
        while (1)
        {
-               optchar = getopt (argc, argv, "46c:hi:");
+               optchar = getopt (argc, argv, "46c:hi:I:");
 
                if (optchar == -1)
                        break;
@@ -140,10 +143,20 @@ int read_options (int argc, char **argv)
                                {
                                        double new_interval;
                                        new_interval = atof (optarg);
-                                       if (new_interval >= 0.2)
+                                       if (new_interval < 0.001)
+                                               fprintf (stderr, "Ignoring invalid interval %g.\n",
+                                                               new_interval);
+                                       else
                                                opt_interval = new_interval;
                                }
                                break;
+                       case 'I':
+                               {
+                                       if (opt_srcaddr != NULL)
+                                               free (opt_srcaddr);
+                                       opt_srcaddr = strdup (optarg);
+                               }
+                               break;
 
                        case 'h':
                        default:
@@ -154,7 +167,7 @@ int read_options (int argc, char **argv)
        return (optind);
 }
 
-void print_host (pingobj_iter_t *iter)
+static void print_host (pingobj_iter_t *iter)
 {
        double          latency;
        unsigned int    sequence;
@@ -188,20 +201,20 @@ void print_host (pingobj_iter_t *iter)
                if ((context->latency_min < 0.0) || (context->latency_min > latency))
                        context->latency_min = latency;
 
-               printf ("%u bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
-                               (unsigned int) data_len,
+               printf ("%zu bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
+                               data_len,
                                context->host, context->addr,
-                               (unsigned int) sequence, latency);
+                               sequence, latency);
        }
        else
        {
                printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
                                context->host, context->addr,
-                               (unsigned int) sequence);
+                               sequence);
        }
 }
 
-void time_normalize (struct timespec *ts)
+static void time_normalize (struct timespec *ts)
 {
        while (ts->tv_nsec < 0)
        {
@@ -222,7 +235,7 @@ void time_normalize (struct timespec *ts)
        }
 }
 
-void time_calc (struct timespec *ts_dest,
+static void time_calc (struct timespec *ts_dest,
                const struct timespec *ts_int,
                const struct timeval  *tv_begin,
                const struct timeval  *tv_end)
@@ -294,15 +307,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))
@@ -318,9 +346,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) %zu bytes of data.\n",
+                               context->host, context->addr, 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)
@@ -341,7 +380,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);
                }
 
@@ -418,7 +458,7 @@ int main (int argc, char **argv)
                }
 
                ping_iterator_set_context (iter, NULL);
-               free (context);
+               context_destroy (context);
        }
 
        ping_destroy (ping);