Code

src/oping.c: Allow setting of the TTL using `-t'.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 15 Mar 2009 10:47:43 +0000 (11:47 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 15 Mar 2009 10:47:43 +0000 (11:47 +0100)
src/mans/oping.pod
src/oping.c

index c4e3e6386164c5e3c24c8e96761ccf7b8e961315..fba2f568bd8396f36bc2780b7d107413296d224e 100644 (file)
@@ -36,6 +36,11 @@ Send (and receive) I<count> ICMP packets, then stop and exit.
 Send one ICMP packet (per host) each I<interval> seconds. This can be a
 floating-point number to specify sub-second precision.
 
+=item B<-t> I<ttl>
+
+Set the IP Time to Live to I<ttl>. This must be a number between (and
+including) 1E<nbsp>andE<nbsp>255. If omitted, the value B<64> is used.
+
 =item B<-I> I<address>
 
 Set the source address to use. You B<cannot> pass the interface name, as you
@@ -43,14 +48,6 @@ can with GNU's L<ping(8)>.
 
 =back
 
-=head1 BUGS
-
-=over 4
-
-=item The TTL cannot be set
-
-=back
-
 =head1 SEE ALSO
 
 L<ping(8)>, L<http://www.fping.com/>, L<liboping(3)>
index bc33c27157d221e8d339d34245459658347e06cd..0f39dc5b4d884005dd12bb4658f2bbc0d9ef2c81 100644 (file)
@@ -74,6 +74,7 @@ static double  opt_interval   = 1.0;
 static int     opt_addrfamily = PING_DEF_AF;
 static char   *opt_srcaddr    = NULL;
 static int     opt_count      = -1;
+static int     opt_send_ttl   = 64;
 
 static void sigint_handler (int signal)
 {
@@ -107,8 +108,16 @@ static void context_destroy (ping_context_t *context)
 
 static void usage_exit (const char *name)
 {
-       fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
-                       name);
+       int name_length;
+
+       name_length = (int) strlen (name);
+
+       fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval]\n"
+                       "%*s[-t ttl] [-I srcaddr]\n"
+                       "%*shost [host [host ...]]\n",
+                       name,
+                       8 + name_length, "",
+                       8 + name_length, "");
        exit (1);
 }
 
@@ -118,7 +127,7 @@ static int read_options (int argc, char **argv)
 
        while (1)
        {
-               optchar = getopt (argc, argv, "46c:hi:I:");
+               optchar = getopt (argc, argv, "46c:hi:I:t:");
 
                if (optchar == -1)
                        break;
@@ -158,6 +167,18 @@ static int read_options (int argc, char **argv)
                                }
                                break;
 
+                       case 't':
+                       {
+                               int new_send_ttl;
+                               new_send_ttl = atoi (optarg);
+                               if ((new_send_ttl > 0) && (new_send_ttl < 256))
+                                       opt_send_ttl = new_send_ttl;
+                               else
+                                       fprintf (stderr, "Invalid TTL argument: %s\n",
+                                                       optarg);
+                               break;
+                       }
+
                        case 'h':
                        default:
                                usage_exit (argv[0]);
@@ -301,6 +322,12 @@ int main (int argc, char **argv)
                return (1);
        }
 
+       if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
+       {
+               fprintf (stderr, "Setting TTL to %i failed: %s\n",
+                               opt_send_ttl, ping_get_error (ping));
+       }
+
        {
                double temp_sec;
                double temp_nsec;