From c8174b14e1b5430df4631ecaab6b0f77f282d888 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 15 Mar 2009 11:47:43 +0100 Subject: [PATCH] src/oping.c: Allow setting of the TTL using `-t'. --- src/mans/oping.pod | 13 +++++-------- src/oping.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/mans/oping.pod b/src/mans/oping.pod index c4e3e63..fba2f56 100644 --- a/src/mans/oping.pod +++ b/src/mans/oping.pod @@ -36,6 +36,11 @@ Send (and receive) I ICMP packets, then stop and exit. Send one ICMP packet (per host) each I seconds. This can be a floating-point number to specify sub-second precision. +=item B<-t> I + +Set the IP Time to Live to I. This must be a number between (and +including) 1EandE255. If omitted, the value B<64> is used. + =item B<-I> I
Set the source address to use. You B pass the interface name, as you @@ -43,14 +48,6 @@ can with GNU's L. =back -=head1 BUGS - -=over 4 - -=item The TTL cannot be set - -=back - =head1 SEE ALSO L, L, L diff --git a/src/oping.c b/src/oping.c index bc33c27..0f39dc5 100644 --- a/src/oping.c +++ b/src/oping.c @@ -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; -- 2.11.0