aboutsummaryrefslogtreecommitdiff
path: root/networking/ping.c
diff options
context:
space:
mode:
authorJoachim Nilsson <joachim.nilsson@vmlinux.org>2010-11-28 23:01:18 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-11-28 23:01:18 +0100
commit714e2b7e6ab9cef6cb853c52b6c128d390964a25 (patch)
tree9286fc497e4e710c2670e815e3e96a0fb45f189c /networking/ping.c
parent713d241852ec726ad07920476fa18d0f9d455246 (diff)
downloadbusybox-w32-714e2b7e6ab9cef6cb853c52b6c128d390964a25.tar.gz
busybox-w32-714e2b7e6ab9cef6cb853c52b6c128d390964a25.tar.bz2
busybox-w32-714e2b7e6ab9cef6cb853c52b6c128d390964a25.zip
ping: add -t TTL option
function old new delta common_ping_main 1755 1796 +41 packed_usage 27954 27971 +17 bbconfig_config_bz2 4965 4966 +1 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 59/0) Total: 59 bytes Signed-off-by: Joachim Nilsson <joachim.nilsson@vmlinux.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/ping.c')
-rw-r--r--networking/ping.c111
1 files changed, 102 insertions, 9 deletions
diff --git a/networking/ping.c b/networking/ping.c
index 3aba4906e..8571d179b 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -29,6 +29,92 @@
29#include <netinet/ip_icmp.h> 29#include <netinet/ip_icmp.h>
30#include "libbb.h" 30#include "libbb.h"
31 31
32//config:config PING
33//config: bool "ping"
34//config: default y
35//config: depends on PLATFORM_LINUX
36//config: help
37//config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
38//config: elicit an ICMP ECHO_RESPONSE from a host or gateway.
39//config:
40//config:config PING6
41//config: bool "ping6"
42//config: default y
43//config: depends on FEATURE_IPV6 && PING
44//config: help
45//config: This will give you a ping that can talk IPv6.
46//config:
47//config:config FEATURE_FANCY_PING
48//config: bool "Enable fancy ping output"
49//config: default y
50//config: depends on PING
51//config: help
52//config: Make the output from the ping applet include statistics, and at the
53//config: same time provide full support for ICMP packets.
54
55//applet:IF_PING(APPLET(ping, _BB_DIR_BIN, _BB_SUID_MAYBE))
56//applet:IF_PING6(APPLET(ping6, _BB_DIR_BIN, _BB_SUID_MAYBE))
57
58//kbuild:lib-$(CONFIG_PING) += ping.o
59//kbuild:lib-$(CONFIG_PING6) += ping.o
60
61//usage:#if !ENABLE_FEATURE_FANCY_PING
62//usage:# define ping_trivial_usage
63//usage: "HOST"
64//usage:# define ping_full_usage "\n\n"
65//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
66//usage:# define ping6_trivial_usage
67//usage: "HOST"
68//usage:# define ping6_full_usage "\n\n"
69//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
70//usage:#else
71//usage:# define ping_trivial_usage
72//usage: "[OPTIONS] HOST"
73//usage:# define ping_full_usage "\n\n"
74//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
75//usage: "\nOptions:"
76//usage: "\n -4,-6 Force IP or IPv6 name resolution"
77//usage: "\n -c CNT Send only CNT pings"
78//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
79//usage: "\n -t TTL Set TTL"
80//usage: "\n -I IFACE/IP Use interface or IP address as source"
81//usage: "\n -W SEC Seconds to wait for the first response (default:10)"
82//usage: "\n (after all -c CNT packets are sent)"
83//usage: "\n -w SEC Seconds until ping exits (default:infinite)"
84//usage: "\n (can exit earlier with -c CNT)"
85//usage: "\n -q Quiet, only displays output at start"
86//usage: "\n and when finished"
87//usage:
88//usage:# define ping6_trivial_usage
89//usage: "[OPTIONS] HOST"
90//usage:# define ping6_full_usage "\n\n"
91//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
92//usage: "\nOptions:"
93//usage: "\n -c CNT Send only CNT pings"
94//usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)"
95//usage: "\n -I IFACE/IP Use interface or IP address as source"
96//usage: "\n -q Quiet, only displays output at start"
97//usage: "\n and when finished"
98//usage:
99//usage:#endif
100//usage:
101//usage:#define ping_example_usage
102//usage: "$ ping localhost\n"
103//usage: "PING slag (127.0.0.1): 56 data bytes\n"
104//usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
105//usage: "\n"
106//usage: "--- debian ping statistics ---\n"
107//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
108//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
109//usage:#define ping6_example_usage
110//usage: "$ ping6 ip6-localhost\n"
111//usage: "PING ip6-localhost (::1): 56 data bytes\n"
112//usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
113//usage: "\n"
114//usage: "--- ip6-localhost ping statistics ---\n"
115//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
116//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
117
32#if ENABLE_PING6 118#if ENABLE_PING6
33# include <netinet/icmp6.h> 119# include <netinet/icmp6.h>
34/* I see RENUMBERED constants in bits/in.h - !!? 120/* I see RENUMBERED constants in bits/in.h - !!?
@@ -223,17 +309,18 @@ static int common_ping_main(sa_family_t af, char **argv)
223 309
224/* Full(er) version */ 310/* Full(er) version */
225 311
226#define OPT_STRING ("qvc:s:w:W:I:4" IF_PING6("6")) 312#define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
227enum { 313enum {
228 OPT_QUIET = 1 << 0, 314 OPT_QUIET = 1 << 0,
229 OPT_VERBOSE = 1 << 1, 315 OPT_VERBOSE = 1 << 1,
230 OPT_c = 1 << 2, 316 OPT_c = 1 << 2,
231 OPT_s = 1 << 3, 317 OPT_s = 1 << 3,
232 OPT_w = 1 << 4, 318 OPT_t = 1 << 4,
233 OPT_W = 1 << 5, 319 OPT_w = 1 << 5,
234 OPT_I = 1 << 6, 320 OPT_W = 1 << 6,
235 OPT_IPV4 = 1 << 7, 321 OPT_I = 1 << 7,
236 OPT_IPV6 = (1 << 8) * ENABLE_PING6, 322 OPT_IPV4 = 1 << 8,
323 OPT_IPV6 = (1 << 9) * ENABLE_PING6,
237}; 324};
238 325
239 326
@@ -244,6 +331,7 @@ struct globals {
244 len_and_sockaddr *source_lsa; 331 len_and_sockaddr *source_lsa;
245 unsigned datalen; 332 unsigned datalen;
246 unsigned pingcount; /* must be int-sized */ 333 unsigned pingcount; /* must be int-sized */
334 unsigned opt_ttl;
247 unsigned long ntransmitted, nreceived, nrepeats; 335 unsigned long ntransmitted, nreceived, nrepeats;
248 uint16_t myid; 336 uint16_t myid;
249 unsigned tmin, tmax; /* in us */ 337 unsigned tmin, tmax; /* in us */
@@ -275,6 +363,7 @@ struct globals {
275#define nreceived (G.nreceived ) 363#define nreceived (G.nreceived )
276#define nrepeats (G.nrepeats ) 364#define nrepeats (G.nrepeats )
277#define pingcount (G.pingcount ) 365#define pingcount (G.pingcount )
366#define opt_ttl (G.opt_ttl )
278#define myid (G.myid ) 367#define myid (G.myid )
279#define tmin (G.tmin ) 368#define tmin (G.tmin )
280#define tmax (G.tmax ) 369#define tmax (G.tmax )
@@ -586,6 +675,10 @@ static void ping4(len_and_sockaddr *lsa)
586 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ 675 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
587 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); 676 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
588 677
678 if (opt_ttl != 0)
679 //setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl));
680 setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl));
681
589 signal(SIGINT, print_stats_and_exit); 682 signal(SIGINT, print_stats_and_exit);
590 683
591 /* start the ping's going ... */ 684 /* start the ping's going ... */
@@ -735,9 +828,9 @@ static int common_ping_main(int opt, char **argv)
735 828
736 INIT_G(); 829 INIT_G();
737 830
738 /* exactly one argument needed; -v and -q don't mix; -c NUM, -w NUM, -W NUM */ 831 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
739 opt_complementary = "=1:q--v:v--q:c+:w+:W+"; 832 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
740 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &deadline, &timeout, &str_I); 833 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
741 if (opt & OPT_s) 834 if (opt & OPT_s)
742 datalen = xatou16(str_s); // -s 835 datalen = xatou16(str_s); // -s
743 if (opt & OPT_I) { // -I 836 if (opt & OPT_I) { // -I