2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-08 04:44:59 +05:30
|
|
|
/*
|
|
|
|
* Mini ping implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
|
|
|
|
*
|
2005-12-15 13:34:17 +05:30
|
|
|
* Adapted from the ping in netkit-base 0.10:
|
1999-12-08 04:44:59 +05:30
|
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
2007-01-22 19:34:27 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Mike Muuss.
|
1999-12-08 04:44:59 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-12-08 04:44:59 +05:30
|
|
|
*/
|
2007-01-25 05:23:22 +05:30
|
|
|
/* from ping6.c:
|
|
|
|
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
|
|
|
|
*
|
|
|
|
* This version of ping is adapted from the ping in netkit-base 0.10,
|
|
|
|
* which is:
|
|
|
|
*
|
|
|
|
* Original copyright notice is retained at the end of this file.
|
|
|
|
*
|
|
|
|
* This version is an adaptation of ping.c from busybox.
|
|
|
|
* The code was modified by Bart Visscher <magick@linux-fan.com>
|
|
|
|
*/
|
2010-11-29 03:31:18 +05:30
|
|
|
//config:config PING
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "ping (10 kb)"
|
2010-11-29 03:31:18 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to
|
|
|
|
//config: elicit an ICMP ECHO_RESPONSE from a host or gateway.
|
2010-11-29 03:31:18 +05:30
|
|
|
//config:
|
|
|
|
//config:config PING6
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "ping6 (11 kb)"
|
2010-11-29 03:31:18 +05:30
|
|
|
//config: default y
|
2016-11-23 13:55:57 +05:30
|
|
|
//config: depends on FEATURE_IPV6
|
2010-11-29 03:31:18 +05:30
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Alias to "ping -6".
|
2010-11-29 03:31:18 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_FANCY_PING
|
|
|
|
//config: bool "Enable fancy ping output"
|
|
|
|
//config: default y
|
2016-11-23 13:55:57 +05:30
|
|
|
//config: depends on PING || PING6
|
2010-11-29 03:31:18 +05:30
|
|
|
//config: help
|
2017-07-21 15:28:43 +05:30
|
|
|
//config: With this option off, ping will say "HOST is alive!"
|
|
|
|
//config: or terminate with SIGALRM in 5 seconds otherwise.
|
|
|
|
//config: No command-line options will be recognized.
|
2010-11-29 03:31:18 +05:30
|
|
|
|
2011-01-18 18:28:01 +05:30
|
|
|
/* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */
|
|
|
|
//applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE))
|
|
|
|
//applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE))
|
2010-11-29 03:31:18 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_PING) += ping.o
|
|
|
|
//kbuild:lib-$(CONFIG_PING6) += ping.o
|
|
|
|
|
|
|
|
//usage:#if !ENABLE_FEATURE_FANCY_PING
|
|
|
|
//usage:# define ping_trivial_usage
|
|
|
|
//usage: "HOST"
|
|
|
|
//usage:# define ping_full_usage "\n\n"
|
|
|
|
//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
|
|
|
|
//usage:# define ping6_trivial_usage
|
|
|
|
//usage: "HOST"
|
|
|
|
//usage:# define ping6_full_usage "\n\n"
|
|
|
|
//usage: "Send ICMP ECHO_REQUEST packets to network hosts"
|
|
|
|
//usage:#else
|
|
|
|
//usage:# define ping_trivial_usage
|
|
|
|
//usage: "[OPTIONS] HOST"
|
|
|
|
//usage:# define ping_full_usage "\n\n"
|
|
|
|
//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
|
2013-03-15 04:41:35 +05:30
|
|
|
//usage: IF_PING6(
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n -4,-6 Force IP or IPv6 name resolution"
|
2013-03-15 04:41:35 +05:30
|
|
|
//usage: )
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n -c CNT Send only CNT pings"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)"
|
2018-08-03 22:20:50 +05:30
|
|
|
//usage: "\n -i SECS Interval"
|
2018-02-14 03:51:33 +05:30
|
|
|
//usage: "\n -A Ping as soon as reply is recevied"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n -t TTL Set TTL"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n -I IFACE/IP Source interface or IP address"
|
|
|
|
//usage: "\n -W SEC Seconds to wait for the first response (default 10)"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n (after all -c CNT packets are sent)"
|
|
|
|
//usage: "\n -w SEC Seconds until ping exits (default:infinite)"
|
|
|
|
//usage: "\n (can exit earlier with -c CNT)"
|
2014-08-27 19:31:25 +05:30
|
|
|
//usage: "\n -q Quiet, only display output at start"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n and when finished"
|
2017-07-21 19:37:13 +05:30
|
|
|
//usage: "\n -p HEXBYTE Pattern to use for payload"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage:
|
|
|
|
//usage:# define ping6_trivial_usage
|
|
|
|
//usage: "[OPTIONS] HOST"
|
|
|
|
//usage:# define ping6_full_usage "\n\n"
|
|
|
|
//usage: "Send ICMP ECHO_REQUEST packets to network hosts\n"
|
|
|
|
//usage: "\n -c CNT Send only CNT pings"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)"
|
2018-08-03 22:20:50 +05:30
|
|
|
//usage: "\n -i SECS Interval"
|
2018-02-14 03:51:33 +05:30
|
|
|
//usage: "\n -A Ping as soon as reply is recevied"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n -I IFACE/IP Source interface or IP address"
|
2014-08-27 19:31:25 +05:30
|
|
|
//usage: "\n -q Quiet, only display output at start"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage: "\n and when finished"
|
2017-07-21 19:37:13 +05:30
|
|
|
//usage: "\n -p HEXBYTE Pattern to use for payload"
|
2010-11-29 03:31:18 +05:30
|
|
|
//usage:
|
|
|
|
//usage:#endif
|
|
|
|
//usage:
|
|
|
|
//usage:#define ping_example_usage
|
|
|
|
//usage: "$ ping localhost\n"
|
|
|
|
//usage: "PING slag (127.0.0.1): 56 data bytes\n"
|
|
|
|
//usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "--- debian ping statistics ---\n"
|
|
|
|
//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
|
|
|
|
//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
|
|
|
|
//usage:#define ping6_example_usage
|
|
|
|
//usage: "$ ping6 ip6-localhost\n"
|
|
|
|
//usage: "PING ip6-localhost (::1): 56 data bytes\n"
|
|
|
|
//usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "--- ip6-localhost ping statistics ---\n"
|
|
|
|
//usage: "1 packets transmitted, 1 packets received, 0% packet loss\n"
|
|
|
|
//usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n"
|
|
|
|
|
2017-09-18 19:58:43 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "common_bufsiz.h"
|
|
|
|
|
|
|
|
#ifdef __BIONIC__
|
|
|
|
/* should be in netinet/ip_icmp.h */
|
|
|
|
# define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
|
|
|
|
# define ICMP_SOURCE_QUENCH 4 /* Source Quench */
|
|
|
|
# define ICMP_REDIRECT 5 /* Redirect (change route) */
|
|
|
|
# define ICMP_ECHO 8 /* Echo Request */
|
|
|
|
# define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
|
|
|
|
# define ICMP_PARAMETERPROB 12 /* Parameter Problem */
|
|
|
|
# define ICMP_TIMESTAMP 13 /* Timestamp Request */
|
|
|
|
# define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
|
|
|
|
# define ICMP_INFO_REQUEST 15 /* Information Request */
|
|
|
|
# define ICMP_INFO_REPLY 16 /* Information Reply */
|
|
|
|
# define ICMP_ADDRESS 17 /* Address Mask Request */
|
|
|
|
# define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
|
|
|
|
#endif
|
|
|
|
|
2017-10-07 23:23:21 +05:30
|
|
|
/* Some operating systems, like GNU/Hurd, don't define SOL_RAW, but do have
|
|
|
|
* IPPROTO_RAW. Since the IPPROTO definitions are also valid to use for
|
|
|
|
* setsockopt (and take the same value as their corresponding SOL definitions,
|
|
|
|
* if they exist), we can just fall back on IPPROTO_RAW. */
|
|
|
|
#ifndef SOL_RAW
|
|
|
|
# define SOL_RAW IPPROTO_RAW
|
|
|
|
#endif
|
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
2009-11-20 22:42:12 +05:30
|
|
|
# include <netinet/icmp6.h>
|
2007-01-25 05:23:22 +05:30
|
|
|
/* I see RENUMBERED constants in bits/in.h - !!?
|
|
|
|
* What a fuck is going on with libc? Is it a glibc joke? */
|
2009-11-20 22:42:12 +05:30
|
|
|
# ifdef IPV6_2292HOPLIMIT
|
|
|
|
# undef IPV6_HOPLIMIT
|
|
|
|
# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
|
|
|
|
# endif
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
1999-12-08 04:44:59 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
enum {
|
|
|
|
DEFDATALEN = 56,
|
|
|
|
MAXIPLEN = 60,
|
|
|
|
MAXICMPLEN = 76,
|
|
|
|
MAX_DUP_CHK = (8 * 128),
|
|
|
|
MAXWAIT = 10,
|
2007-01-25 05:23:22 +05:30
|
|
|
PINGINTERVAL = 1, /* 1 second */
|
2013-06-28 04:00:46 +05:30
|
|
|
pingsock = 0,
|
2006-03-11 00:52:06 +05:30
|
|
|
};
|
1999-12-08 04:44:59 +05:30
|
|
|
|
2013-06-28 04:00:46 +05:30
|
|
|
static void
|
|
|
|
#if ENABLE_PING6
|
|
|
|
create_icmp_socket(len_and_sockaddr *lsa)
|
|
|
|
#else
|
|
|
|
create_icmp_socket(void)
|
|
|
|
#define create_icmp_socket(lsa) create_icmp_socket()
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
#if ENABLE_PING6
|
|
|
|
if (lsa->u.sa.sa_family == AF_INET6)
|
|
|
|
sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
|
|
|
|
if (sock < 0) {
|
2014-01-09 16:23:26 +05:30
|
|
|
if (errno == EPERM)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root);
|
|
|
|
bb_simple_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
|
2013-06-28 04:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
xmove_fd(sock, pingsock);
|
|
|
|
}
|
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
#if !ENABLE_FEATURE_FANCY_PING
|
2006-11-20 06:18:22 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
/* Simple version */
|
2006-11-20 06:18:22 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
struct globals {
|
|
|
|
char *hostname;
|
|
|
|
char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
|
2016-06-23 21:56:32 +05:30
|
|
|
uint16_t myid;
|
2010-03-03 05:40:29 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-21 21:48:48 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2006-09-02 21:43:36 +05:30
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void noresp(int ign UNUSED_PARAM)
|
2002-03-20 17:29:28 +05:30
|
|
|
{
|
2010-03-03 05:40:29 +05:30
|
|
|
printf("No response from %s\n", G.hostname);
|
2003-01-12 11:38:33 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2002-03-20 17:29:28 +05:30
|
|
|
}
|
1999-12-11 14:11:28 +05:30
|
|
|
|
2007-01-29 23:33:54 +05:30
|
|
|
static void ping4(len_and_sockaddr *lsa)
|
1999-12-11 14:11:28 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
struct icmp *pkt;
|
2013-06-28 04:00:46 +05:30
|
|
|
int c;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
pkt = (struct icmp *) G.packet;
|
2013-06-28 04:00:46 +05:30
|
|
|
/*memset(pkt, 0, sizeof(G.packet)); already is */
|
2000-02-09 01:28:47 +05:30
|
|
|
pkt->icmp_type = ICMP_ECHO;
|
2016-06-23 21:56:32 +05:30
|
|
|
pkt->icmp_id = G.myid;
|
2020-10-01 06:37:22 +05:30
|
|
|
pkt->icmp_cksum = inet_cksum(pkt, sizeof(G.packet));
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
/* listen for replies */
|
|
|
|
while (1) {
|
2013-06-28 04:00:46 +05:30
|
|
|
#if 0
|
2000-02-09 01:28:47 +05:30
|
|
|
struct sockaddr_in from;
|
2005-07-27 04:30:59 +05:30
|
|
|
socklen_t fromlen = sizeof(from);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
|
2006-12-31 17:44:16 +05:30
|
|
|
(struct sockaddr *) &from, &fromlen);
|
2013-06-28 04:00:46 +05:30
|
|
|
#else
|
|
|
|
c = recv(pingsock, G.packet, sizeof(G.packet), 0);
|
|
|
|
#endif
|
2006-12-31 17:44:16 +05:30
|
|
|
if (c < 0) {
|
2007-01-09 05:25:33 +05:30
|
|
|
if (errno != EINTR)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("recvfrom");
|
2000-02-09 01:28:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c >= 76) { /* ip + icmp */
|
2010-03-03 05:40:29 +05:30
|
|
|
struct iphdr *iphdr = (struct iphdr *) G.packet;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
|
2016-06-23 21:56:32 +05:30
|
|
|
if (pkt->icmp_id != G.myid)
|
|
|
|
continue; /* not our ping */
|
2000-02-09 01:28:47 +05:30
|
|
|
if (pkt->icmp_type == ICMP_ECHOREPLY)
|
|
|
|
break;
|
|
|
|
}
|
1999-12-11 14:11:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
|
|
|
static void ping6(len_and_sockaddr *lsa)
|
|
|
|
{
|
|
|
|
struct icmp6_hdr *pkt;
|
2013-06-28 04:00:46 +05:30
|
|
|
int c;
|
2007-01-25 05:23:22 +05:30
|
|
|
int sockopt;
|
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
pkt = (struct icmp6_hdr *) G.packet;
|
2013-06-28 04:00:46 +05:30
|
|
|
/*memset(pkt, 0, sizeof(G.packet)); already is */
|
2007-01-25 05:23:22 +05:30
|
|
|
pkt->icmp6_type = ICMP6_ECHO_REQUEST;
|
2016-06-23 21:56:32 +05:30
|
|
|
pkt->icmp6_id = G.myid;
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
/* listen for replies */
|
|
|
|
while (1) {
|
2013-06-28 04:00:46 +05:30
|
|
|
#if 0
|
2007-01-25 05:23:22 +05:30
|
|
|
struct sockaddr_in6 from;
|
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
|
2007-01-25 05:23:22 +05:30
|
|
|
(struct sockaddr *) &from, &fromlen);
|
2013-06-28 04:00:46 +05:30
|
|
|
#else
|
|
|
|
c = recv(pingsock, G.packet, sizeof(G.packet), 0);
|
|
|
|
#endif
|
2007-01-25 05:23:22 +05:30
|
|
|
if (c < 0) {
|
|
|
|
if (errno != EINTR)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("recvfrom");
|
2007-01-25 05:23:22 +05:30
|
|
|
continue;
|
|
|
|
}
|
2013-06-28 04:00:46 +05:30
|
|
|
if (c >= ICMP_MINLEN) { /* icmp6_hdr */
|
2016-06-23 21:56:32 +05:30
|
|
|
if (pkt->icmp6_id != G.myid)
|
|
|
|
continue; /* not our ping */
|
2007-01-25 05:23:22 +05:30
|
|
|
if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-20 22:42:12 +05:30
|
|
|
#if !ENABLE_PING6
|
|
|
|
# define common_ping_main(af, argv) common_ping_main(argv)
|
|
|
|
#endif
|
|
|
|
static int common_ping_main(sa_family_t af, char **argv)
|
1999-12-11 14:11:28 +05:30
|
|
|
{
|
2007-01-25 05:23:22 +05:30
|
|
|
len_and_sockaddr *lsa;
|
2007-07-02 21:17:52 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2009-11-20 22:42:12 +05:30
|
|
|
#if ENABLE_PING6
|
2007-07-02 21:17:52 +05:30
|
|
|
while ((++argv)[0] && argv[0][0] == '-') {
|
2007-01-25 05:23:22 +05:30
|
|
|
if (argv[0][1] == '4') {
|
|
|
|
af = AF_INET;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argv[0][1] == '6') {
|
|
|
|
af = AF_INET6;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
#else
|
2000-02-09 01:28:47 +05:30
|
|
|
argv++;
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
G.hostname = *argv;
|
|
|
|
if (!G.hostname)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
#if ENABLE_PING6
|
2010-03-03 05:40:29 +05:30
|
|
|
lsa = xhost_and_af2sockaddr(G.hostname, 0, af);
|
2007-01-25 05:23:22 +05:30
|
|
|
#else
|
2010-03-03 05:40:29 +05:30
|
|
|
lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET);
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
|
|
|
/* Set timer _after_ DNS resolution */
|
|
|
|
signal(SIGALRM, noresp);
|
|
|
|
alarm(5); /* give the host 5000ms to respond */
|
|
|
|
|
2013-06-28 04:00:46 +05:30
|
|
|
create_icmp_socket(lsa);
|
2016-06-23 21:56:32 +05:30
|
|
|
G.myid = (uint16_t) getpid();
|
2020-12-13 20:23:06 +05:30
|
|
|
/* we can use native-endian ident, but other Unix ping/traceroute
|
|
|
|
* utils use *big-endian pid*, and e.g. traceroute on our machine may be
|
|
|
|
* *not* from busybox, idents may collide. Follow the convention:
|
|
|
|
*/
|
|
|
|
G.myid = htons(G.myid);
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
2008-01-29 16:03:34 +05:30
|
|
|
if (lsa->u.sa.sa_family == AF_INET6)
|
2007-01-25 05:23:22 +05:30
|
|
|
ping6(lsa);
|
|
|
|
else
|
|
|
|
#endif
|
2007-01-29 23:33:54 +05:30
|
|
|
ping4(lsa);
|
2017-07-21 15:28:43 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(pingsock);
|
2010-03-03 05:40:29 +05:30
|
|
|
printf("%s is alive!\n", G.hostname);
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
1999-12-11 14:11:28 +05:30
|
|
|
}
|
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
#else /* FEATURE_FANCY_PING */
|
|
|
|
|
2006-11-20 06:18:22 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
/* Full(er) version */
|
2006-11-20 06:18:22 +05:30
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
/* -c NUM, -t NUM, -w NUM, -W NUM */
|
2018-08-03 22:20:50 +05:30
|
|
|
#define OPT_STRING "qvAc:+s:t:+w:+W:+I:np:i:4"IF_PING6("6")
|
2006-11-20 06:18:22 +05:30
|
|
|
enum {
|
|
|
|
OPT_QUIET = 1 << 0,
|
2007-01-25 05:23:22 +05:30
|
|
|
OPT_VERBOSE = 1 << 1,
|
2018-02-14 03:51:33 +05:30
|
|
|
OPT_A = 1 << 2,
|
|
|
|
OPT_c = 1 << 3,
|
|
|
|
OPT_s = 1 << 4,
|
|
|
|
OPT_t = 1 << 5,
|
|
|
|
OPT_w = 1 << 6,
|
|
|
|
OPT_W = 1 << 7,
|
|
|
|
OPT_I = 1 << 8,
|
|
|
|
/*OPT_n = 1 << 9, - ignored */
|
|
|
|
OPT_p = 1 << 10,
|
2018-08-03 22:20:50 +05:30
|
|
|
OPT_i = 1 << 11,
|
|
|
|
OPT_IPV4 = 1 << 12,
|
|
|
|
OPT_IPV6 = (1 << 13) * ENABLE_PING6,
|
2006-11-20 06:18:22 +05:30
|
|
|
};
|
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
|
2007-06-04 16:03:48 +05:30
|
|
|
struct globals {
|
2007-10-20 03:09:25 +05:30
|
|
|
int if_index;
|
2008-10-27 17:24:45 +05:30
|
|
|
char *str_I;
|
2007-06-04 16:03:48 +05:30
|
|
|
len_and_sockaddr *source_lsa;
|
|
|
|
unsigned datalen;
|
2008-10-27 17:24:45 +05:30
|
|
|
unsigned pingcount; /* must be int-sized */
|
2010-11-29 03:31:18 +05:30
|
|
|
unsigned opt_ttl;
|
2008-10-27 17:24:45 +05:30
|
|
|
unsigned long ntransmitted, nreceived, nrepeats;
|
2007-06-04 16:03:48 +05:30
|
|
|
uint16_t myid;
|
2014-08-27 19:31:25 +05:30
|
|
|
uint8_t pattern;
|
2007-06-18 14:25:57 +05:30
|
|
|
unsigned tmin, tmax; /* in us */
|
|
|
|
unsigned long long tsum; /* in us, sum of all times */
|
2018-02-14 04:23:24 +05:30
|
|
|
unsigned cur_us; /* low word only, we don't need more */
|
|
|
|
unsigned deadline_us;
|
2018-08-03 22:20:50 +05:30
|
|
|
unsigned interval_us;
|
2008-04-07 06:16:29 +05:30
|
|
|
unsigned timeout;
|
2010-03-03 05:40:29 +05:30
|
|
|
unsigned sizeof_rcv_packet;
|
|
|
|
char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */
|
|
|
|
void *snd_packet; /* [datalen + ipv4/ipv6_const] */
|
2007-06-04 16:03:48 +05:30
|
|
|
const char *hostname;
|
|
|
|
const char *dotted;
|
|
|
|
union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in sin;
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
2007-06-04 16:03:48 +05:30
|
|
|
struct sockaddr_in6 sin6;
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
2007-06-04 16:03:48 +05:30
|
|
|
} pingaddr;
|
2013-06-28 05:03:47 +05:30
|
|
|
unsigned char rcvd_tbl[MAX_DUP_CHK / 8];
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2007-10-20 03:09:25 +05:30
|
|
|
#define if_index (G.if_index )
|
2007-06-04 16:03:48 +05:30
|
|
|
#define source_lsa (G.source_lsa )
|
2008-10-27 17:24:45 +05:30
|
|
|
#define str_I (G.str_I )
|
2007-06-04 16:03:48 +05:30
|
|
|
#define datalen (G.datalen )
|
|
|
|
#define pingcount (G.pingcount )
|
2010-11-29 03:31:18 +05:30
|
|
|
#define opt_ttl (G.opt_ttl )
|
2007-06-04 16:03:48 +05:30
|
|
|
#define myid (G.myid )
|
|
|
|
#define tmin (G.tmin )
|
|
|
|
#define tmax (G.tmax )
|
|
|
|
#define tsum (G.tsum )
|
2008-04-07 06:16:29 +05:30
|
|
|
#define timeout (G.timeout )
|
2007-06-04 16:03:48 +05:30
|
|
|
#define hostname (G.hostname )
|
|
|
|
#define dotted (G.dotted )
|
|
|
|
#define pingaddr (G.pingaddr )
|
|
|
|
#define rcvd_tbl (G.rcvd_tbl )
|
|
|
|
#define INIT_G() do { \
|
2016-04-21 21:48:48 +05:30
|
|
|
setup_common_bufsiz(); \
|
2015-10-13 18:20:20 +05:30
|
|
|
BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
|
2008-04-07 06:16:29 +05:30
|
|
|
datalen = DEFDATALEN; \
|
|
|
|
timeout = MAXWAIT; \
|
2007-06-04 16:03:48 +05:30
|
|
|
tmin = UINT_MAX; \
|
|
|
|
} while (0)
|
1999-12-11 14:11:28 +05:30
|
|
|
|
|
|
|
|
2013-06-28 05:03:47 +05:30
|
|
|
#define BYTE(bit) rcvd_tbl[(bit)>>3]
|
|
|
|
#define MASK(bit) (1 << ((bit) & 7))
|
|
|
|
#define SET(bit) (BYTE(bit) |= MASK(bit))
|
|
|
|
#define CLR(bit) (BYTE(bit) &= (~MASK(bit)))
|
|
|
|
#define TST(bit) (BYTE(bit) & MASK(bit))
|
1999-12-11 14:11:28 +05:30
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void print_stats_and_exit(int junk) NORETURN;
|
|
|
|
static void print_stats_and_exit(int junk UNUSED_PARAM)
|
2000-02-09 01:28:47 +05:30
|
|
|
{
|
2013-03-15 04:41:35 +05:30
|
|
|
unsigned long ul;
|
|
|
|
unsigned long nrecv;
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
|
2013-03-15 04:41:35 +05:30
|
|
|
nrecv = G.nreceived;
|
|
|
|
printf("\n--- %s ping statistics ---\n"
|
|
|
|
"%lu packets transmitted, "
|
|
|
|
"%lu packets received, ",
|
|
|
|
hostname, G.ntransmitted, nrecv
|
|
|
|
);
|
|
|
|
if (G.nrepeats)
|
|
|
|
printf("%lu duplicates, ", G.nrepeats);
|
|
|
|
ul = G.ntransmitted;
|
|
|
|
if (ul != 0)
|
|
|
|
ul = (ul - nrecv) * 100 / ul;
|
|
|
|
printf("%lu%% packet loss\n", ul);
|
2007-06-18 14:25:57 +05:30
|
|
|
if (tmin != UINT_MAX) {
|
2013-03-15 04:41:35 +05:30
|
|
|
unsigned tavg = tsum / (nrecv + G.nrepeats);
|
2007-06-18 14:25:57 +05:30
|
|
|
printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
|
|
|
|
tmin / 1000, tmin % 1000,
|
|
|
|
tavg / 1000, tavg % 1000,
|
|
|
|
tmax / 1000, tmax % 1000);
|
|
|
|
}
|
2008-04-07 06:16:29 +05:30
|
|
|
/* if condition is true, exit with 1 -- 'failure' */
|
2018-02-14 04:23:24 +05:30
|
|
|
exit(nrecv == 0 || (G.deadline_us && nrecv < pingcount));
|
1999-12-08 04:44:59 +05:30
|
|
|
}
|
|
|
|
|
2011-02-06 22:21:45 +05:30
|
|
|
static void sendping_tail(void (*sp)(int), int size_pkt)
|
2007-01-29 23:33:54 +05:30
|
|
|
{
|
2007-02-09 23:44:42 +05:30
|
|
|
int sz;
|
|
|
|
|
2013-03-15 04:41:35 +05:30
|
|
|
CLR((uint16_t)G.ntransmitted % MAX_DUP_CHK);
|
|
|
|
G.ntransmitted++;
|
2007-02-09 23:44:42 +05:30
|
|
|
|
2011-02-06 22:21:45 +05:30
|
|
|
size_pkt += datalen;
|
|
|
|
|
2018-02-14 04:23:24 +05:30
|
|
|
if (G.deadline_us) {
|
|
|
|
unsigned n = G.cur_us - G.deadline_us;
|
2018-02-14 03:51:33 +05:30
|
|
|
if ((int)n >= 0)
|
|
|
|
print_stats_and_exit(0);
|
|
|
|
}
|
|
|
|
|
2007-02-09 23:44:42 +05:30
|
|
|
/* sizeof(pingaddr) can be larger than real sa size, but I think
|
|
|
|
* it doesn't matter */
|
2011-02-06 22:21:45 +05:30
|
|
|
sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr));
|
2007-02-09 23:44:42 +05:30
|
|
|
if (sz != size_pkt)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die(bb_msg_write_error);
|
2007-01-29 23:33:54 +05:30
|
|
|
|
2018-02-14 03:51:33 +05:30
|
|
|
if (pingcount == 0 || G.ntransmitted < pingcount) {
|
2018-08-25 23:06:06 +05:30
|
|
|
/* Didn't send all pings yet - schedule next in -i SEC interval */
|
|
|
|
struct itimerval i;
|
2008-04-07 06:16:29 +05:30
|
|
|
signal(SIGALRM, sp);
|
2018-08-25 23:06:06 +05:30
|
|
|
/*ualarm(G.interval_us, 0); - does not work for >=1sec on some libc */
|
|
|
|
i.it_interval.tv_sec = 0;
|
|
|
|
i.it_interval.tv_usec = 0;
|
|
|
|
i.it_value.tv_sec = G.interval_us / 1000000;
|
|
|
|
i.it_value.tv_usec = G.interval_us % 1000000;
|
|
|
|
setitimer(ITIMER_REAL, &i, NULL);
|
2018-02-14 03:51:33 +05:30
|
|
|
} else { /* -c NN, and all NN are sent */
|
2008-04-07 06:16:29 +05:30
|
|
|
/* Wait for the last ping to come back.
|
|
|
|
* -W timeout: wait for a response in seconds.
|
2017-04-17 19:43:32 +05:30
|
|
|
* Affects only timeout in absence of any responses,
|
2008-04-07 06:16:29 +05:30
|
|
|
* otherwise ping waits for two RTTs. */
|
|
|
|
unsigned expire = timeout;
|
|
|
|
|
2013-03-15 04:41:35 +05:30
|
|
|
if (G.nreceived) {
|
2008-04-07 06:16:29 +05:30
|
|
|
/* approx. 2*tmax, in seconds (2 RTT) */
|
|
|
|
expire = tmax / (512*1024);
|
|
|
|
if (expire == 0)
|
|
|
|
expire = 1;
|
|
|
|
}
|
|
|
|
signal(SIGALRM, print_stats_and_exit);
|
|
|
|
alarm(expire);
|
2007-01-29 23:33:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sendping4(int junk UNUSED_PARAM)
|
1999-12-08 04:44:59 +05:30
|
|
|
{
|
2010-03-03 05:40:29 +05:30
|
|
|
struct icmp *pkt = G.snd_packet;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2014-08-27 19:31:25 +05:30
|
|
|
memset(pkt, G.pattern, datalen + ICMP_MINLEN + 4);
|
2000-02-09 01:28:47 +05:30
|
|
|
pkt->icmp_type = ICMP_ECHO;
|
2008-04-29 18:01:53 +05:30
|
|
|
/*pkt->icmp_code = 0;*/
|
2010-03-03 05:40:29 +05:30
|
|
|
pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
|
2013-03-15 04:41:35 +05:30
|
|
|
pkt->icmp_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
|
2000-02-09 01:28:47 +05:30
|
|
|
pkt->icmp_id = myid;
|
2007-06-16 19:07:59 +05:30
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
/* If datalen < 4, we store timestamp _past_ the packet,
|
|
|
|
* but it's ok - we allocated 4 extra bytes in xzalloc() just in case.
|
|
|
|
*/
|
2007-06-18 14:25:57 +05:30
|
|
|
/*if (datalen >= 4)*/
|
2010-03-03 05:40:29 +05:30
|
|
|
/* No hton: we'll read it back on the same machine */
|
2018-02-14 04:23:24 +05:30
|
|
|
*(uint32_t*)&pkt->icmp_dun = G.cur_us = monotonic_us();
|
2007-06-16 19:07:59 +05:30
|
|
|
|
2020-10-01 06:37:22 +05:30
|
|
|
pkt->icmp_cksum = inet_cksum(pkt, datalen + ICMP_MINLEN);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2011-02-06 22:21:45 +05:30
|
|
|
sendping_tail(sendping4, ICMP_MINLEN);
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
#if ENABLE_PING6
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sendping6(int junk UNUSED_PARAM)
|
2007-01-25 05:23:22 +05:30
|
|
|
{
|
2011-02-05 08:28:43 +05:30
|
|
|
struct icmp6_hdr *pkt = G.snd_packet;
|
2007-01-25 05:23:22 +05:30
|
|
|
|
2014-08-27 19:31:25 +05:30
|
|
|
memset(pkt, G.pattern, datalen + sizeof(struct icmp6_hdr) + 4);
|
2007-01-25 05:23:22 +05:30
|
|
|
pkt->icmp6_type = ICMP6_ECHO_REQUEST;
|
2008-04-29 18:01:53 +05:30
|
|
|
/*pkt->icmp6_code = 0;*/
|
|
|
|
/*pkt->icmp6_cksum = 0;*/
|
2013-03-15 04:41:35 +05:30
|
|
|
pkt->icmp6_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */
|
2007-01-25 05:23:22 +05:30
|
|
|
pkt->icmp6_id = myid;
|
2007-06-16 19:07:59 +05:30
|
|
|
|
2007-06-18 14:25:57 +05:30
|
|
|
/*if (datalen >= 4)*/
|
2018-02-14 04:23:24 +05:30
|
|
|
*(bb__aliased_uint32_t*)(&pkt->icmp6_data8[4]) = G.cur_us = monotonic_us();
|
2007-01-25 05:23:22 +05:30
|
|
|
|
2011-09-07 21:22:37 +05:30
|
|
|
//TODO? pkt->icmp_cksum = inet_cksum(...);
|
2010-03-03 05:40:29 +05:30
|
|
|
|
2011-02-06 22:21:45 +05:30
|
|
|
sendping_tail(sendping6, sizeof(struct icmp6_hdr));
|
1999-12-08 04:44:59 +05:30
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2007-01-30 05:13:18 +05:30
|
|
|
static const char *icmp_type_name(int id)
|
2000-04-26 04:54:55 +05:30
|
|
|
{
|
|
|
|
switch (id) {
|
2007-01-22 19:34:27 +05:30
|
|
|
case ICMP_ECHOREPLY: return "Echo Reply";
|
|
|
|
case ICMP_DEST_UNREACH: return "Destination Unreachable";
|
|
|
|
case ICMP_SOURCE_QUENCH: return "Source Quench";
|
|
|
|
case ICMP_REDIRECT: return "Redirect (change route)";
|
|
|
|
case ICMP_ECHO: return "Echo Request";
|
|
|
|
case ICMP_TIME_EXCEEDED: return "Time Exceeded";
|
|
|
|
case ICMP_PARAMETERPROB: return "Parameter Problem";
|
|
|
|
case ICMP_TIMESTAMP: return "Timestamp Request";
|
|
|
|
case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
|
|
|
|
case ICMP_INFO_REQUEST: return "Information Request";
|
|
|
|
case ICMP_INFO_REPLY: return "Information Reply";
|
|
|
|
case ICMP_ADDRESS: return "Address Mask Request";
|
|
|
|
case ICMP_ADDRESSREPLY: return "Address Mask Reply";
|
|
|
|
default: return "unknown ICMP type";
|
2000-04-26 04:54:55 +05:30
|
|
|
}
|
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
|
|
|
/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
|
|
|
|
* the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
|
|
|
|
#ifndef MLD_LISTENER_QUERY
|
|
|
|
# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
|
|
|
|
#endif
|
|
|
|
#ifndef MLD_LISTENER_REPORT
|
|
|
|
# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
|
|
|
|
#endif
|
|
|
|
#ifndef MLD_LISTENER_REDUCTION
|
|
|
|
# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
|
|
|
|
#endif
|
2007-01-30 05:13:18 +05:30
|
|
|
static const char *icmp6_type_name(int id)
|
2007-01-25 05:23:22 +05:30
|
|
|
{
|
|
|
|
switch (id) {
|
|
|
|
case ICMP6_DST_UNREACH: return "Destination Unreachable";
|
|
|
|
case ICMP6_PACKET_TOO_BIG: return "Packet too big";
|
|
|
|
case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
|
|
|
|
case ICMP6_PARAM_PROB: return "Parameter Problem";
|
|
|
|
case ICMP6_ECHO_REPLY: return "Echo Reply";
|
|
|
|
case ICMP6_ECHO_REQUEST: return "Echo Request";
|
|
|
|
case MLD_LISTENER_QUERY: return "Listener Query";
|
|
|
|
case MLD_LISTENER_REPORT: return "Listener Report";
|
|
|
|
case MLD_LISTENER_REDUCTION: return "Listener Reduction";
|
|
|
|
default: return "unknown ICMP type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-04-26 04:54:55 +05:30
|
|
|
|
2007-06-18 14:25:57 +05:30
|
|
|
static void unpack_tail(int sz, uint32_t *tp,
|
2007-03-03 06:06:35 +05:30
|
|
|
const char *from_str,
|
|
|
|
uint16_t recv_seq, int ttl)
|
|
|
|
{
|
2013-06-28 05:03:47 +05:30
|
|
|
unsigned char *b, m;
|
2007-03-03 06:06:35 +05:30
|
|
|
const char *dupmsg = " (DUP!)";
|
|
|
|
unsigned triptime = triptime; /* for gcc */
|
|
|
|
|
|
|
|
if (tp) {
|
2007-06-18 14:25:57 +05:30
|
|
|
/* (int32_t) cast is for hypothetical 64-bit unsigned */
|
|
|
|
/* (doesn't hurt 32-bit real-world anyway) */
|
|
|
|
triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
|
2007-03-03 06:06:35 +05:30
|
|
|
tsum += triptime;
|
|
|
|
if (triptime < tmin)
|
|
|
|
tmin = triptime;
|
|
|
|
if (triptime > tmax)
|
|
|
|
tmax = triptime;
|
|
|
|
}
|
|
|
|
|
2013-06-28 05:03:47 +05:30
|
|
|
b = &BYTE(recv_seq % MAX_DUP_CHK);
|
|
|
|
m = MASK(recv_seq % MAX_DUP_CHK);
|
|
|
|
/*if TST(recv_seq % MAX_DUP_CHK):*/
|
|
|
|
if (*b & m) {
|
2013-03-15 04:41:35 +05:30
|
|
|
++G.nrepeats;
|
2007-03-03 06:06:35 +05:30
|
|
|
} else {
|
2013-06-28 05:03:47 +05:30
|
|
|
/*SET(recv_seq % MAX_DUP_CHK):*/
|
|
|
|
*b |= m;
|
|
|
|
++G.nreceived;
|
2007-03-03 06:06:35 +05:30
|
|
|
dupmsg += 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option_mask32 & OPT_QUIET)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%d bytes from %s: seq=%u ttl=%d", sz,
|
|
|
|
from_str, recv_seq, ttl);
|
|
|
|
if (tp)
|
2007-06-18 14:25:57 +05:30
|
|
|
printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
|
2007-03-03 06:06:35 +05:30
|
|
|
puts(dupmsg);
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2007-03-03 06:06:35 +05:30
|
|
|
}
|
2018-02-14 03:51:33 +05:30
|
|
|
static int unpack4(char *buf, int sz, struct sockaddr_in *from)
|
1999-12-08 04:44:59 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
struct icmp *icmppkt;
|
|
|
|
struct iphdr *iphdr;
|
2007-03-03 06:06:35 +05:30
|
|
|
int hlen;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
/* discard if too short */
|
2000-06-08 02:08:15 +05:30
|
|
|
if (sz < (datalen + ICMP_MINLEN))
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2006-09-02 21:47:30 +05:30
|
|
|
/* check IP header */
|
|
|
|
iphdr = (struct iphdr *) buf;
|
|
|
|
hlen = iphdr->ihl << 2;
|
2000-02-09 01:28:47 +05:30
|
|
|
sz -= hlen;
|
|
|
|
icmppkt = (struct icmp *) (buf + hlen);
|
2000-04-26 04:54:55 +05:30
|
|
|
if (icmppkt->icmp_id != myid)
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0; /* not our ping */
|
2000-04-26 04:54:55 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
|
2007-01-22 19:34:27 +05:30
|
|
|
uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
|
2007-06-18 14:25:57 +05:30
|
|
|
uint32_t *tp = NULL;
|
2007-03-03 06:06:35 +05:30
|
|
|
|
2007-06-18 14:25:57 +05:30
|
|
|
if (sz >= ICMP_MINLEN + sizeof(uint32_t))
|
|
|
|
tp = (uint32_t *) icmppkt->icmp_data;
|
2007-03-03 06:06:35 +05:30
|
|
|
unpack_tail(sz, tp,
|
|
|
|
inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
|
|
|
|
recv_seq, iphdr->ttl);
|
2018-02-14 03:51:33 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (icmppkt->icmp_type != ICMP_ECHO) {
|
2007-03-03 06:06:35 +05:30
|
|
|
bb_error_msg("warning: got ICMP %d (%s)",
|
|
|
|
icmppkt->icmp_type,
|
|
|
|
icmp_type_name(icmppkt->icmp_type));
|
2007-01-22 19:34:27 +05:30
|
|
|
}
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0;
|
1999-12-08 04:44:59 +05:30
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
2018-02-14 03:51:33 +05:30
|
|
|
static int unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
|
2007-01-25 05:23:22 +05:30
|
|
|
{
|
|
|
|
struct icmp6_hdr *icmppkt;
|
|
|
|
char buf[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
/* discard if too short */
|
|
|
|
if (sz < (datalen + sizeof(struct icmp6_hdr)))
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0;
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
icmppkt = (struct icmp6_hdr *) packet;
|
|
|
|
if (icmppkt->icmp6_id != myid)
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0; /* not our ping */
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
|
|
|
|
uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
|
2007-06-18 14:25:57 +05:30
|
|
|
uint32_t *tp = NULL;
|
2007-03-03 06:06:35 +05:30
|
|
|
|
2007-06-18 14:25:57 +05:30
|
|
|
if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
|
|
|
|
tp = (uint32_t *) &icmppkt->icmp6_data8[4];
|
2007-03-03 06:06:35 +05:30
|
|
|
unpack_tail(sz, tp,
|
2011-09-11 23:57:28 +05:30
|
|
|
inet_ntop(AF_INET6, &from->sin6_addr,
|
2007-03-03 06:06:35 +05:30
|
|
|
buf, sizeof(buf)),
|
|
|
|
recv_seq, hoplimit);
|
2018-02-14 03:51:33 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
|
2007-03-03 06:06:35 +05:30
|
|
|
bb_error_msg("warning: got ICMP %d (%s)",
|
|
|
|
icmppkt->icmp6_type,
|
|
|
|
icmp6_type_name(icmppkt->icmp6_type));
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
2018-02-14 03:51:33 +05:30
|
|
|
return 0;
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
#endif
|
1999-12-08 04:44:59 +05:30
|
|
|
|
2007-01-29 23:33:54 +05:30
|
|
|
static void ping4(len_and_sockaddr *lsa)
|
1999-12-08 04:44:59 +05:30
|
|
|
{
|
2000-02-09 01:28:47 +05:30
|
|
|
int sockopt;
|
|
|
|
|
2008-01-29 16:03:34 +05:30
|
|
|
pingaddr.sin = lsa->u.sin;
|
2007-06-20 04:24:21 +05:30
|
|
|
if (source_lsa) {
|
|
|
|
if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
|
2008-01-29 16:03:34 +05:30
|
|
|
&source_lsa->u.sa, source_lsa->len))
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("can't set multicast source interface");
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
|
2007-06-20 04:24:21 +05:30
|
|
|
}
|
2006-09-02 21:47:30 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
/* enable broadcast pings */
|
2006-11-23 04:52:06 +05:30
|
|
|
setsockopt_broadcast(pingsock);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2008-08-24 05:32:18 +05:30
|
|
|
/* set recv buf (needed if we can get lots of responses: flood ping,
|
|
|
|
* broadcast ping etc) */
|
|
|
|
sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2010-11-29 16:37:12 +05:30
|
|
|
if (opt_ttl != 0) {
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_int(pingsock, IPPROTO_IP, IP_TTL, opt_ttl);
|
2017-04-17 19:43:32 +05:30
|
|
|
/* above doesn't affect packets sent to bcast IP, so... */
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_int(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, opt_ttl);
|
2010-11-29 16:37:12 +05:30
|
|
|
}
|
2010-11-29 03:31:18 +05:30
|
|
|
|
2008-04-07 06:16:29 +05:30
|
|
|
signal(SIGINT, print_stats_and_exit);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
/* start the ping's going ... */
|
2018-02-14 03:51:33 +05:30
|
|
|
send_ping:
|
2007-01-29 23:33:54 +05:30
|
|
|
sendping4(0);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
/* listen for replies */
|
|
|
|
while (1) {
|
|
|
|
struct sockaddr_in from;
|
2000-04-21 06:56:49 +05:30
|
|
|
socklen_t fromlen = (socklen_t) sizeof(from);
|
2000-02-09 01:28:47 +05:30
|
|
|
int c;
|
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0,
|
2007-01-09 05:25:33 +05:30
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
if (c < 0) {
|
|
|
|
if (errno != EINTR)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("recvfrom");
|
2000-02-09 01:28:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
2018-02-14 03:51:33 +05:30
|
|
|
c = unpack4(G.rcv_packet, c, &from);
|
2013-03-15 04:41:35 +05:30
|
|
|
if (pingcount && G.nreceived >= pingcount)
|
2000-02-09 01:28:47 +05:30
|
|
|
break;
|
2018-02-14 03:51:33 +05:30
|
|
|
if (c && (option_mask32 & OPT_A)) {
|
|
|
|
goto send_ping;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
1999-12-08 04:44:59 +05:30
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
|
|
|
static void ping6(len_and_sockaddr *lsa)
|
|
|
|
{
|
|
|
|
int sockopt;
|
|
|
|
struct msghdr msg;
|
|
|
|
struct sockaddr_in6 from;
|
|
|
|
struct iovec iov;
|
|
|
|
char control_buf[CMSG_SPACE(36)];
|
|
|
|
|
2008-01-29 16:03:34 +05:30
|
|
|
pingaddr.sin6 = lsa->u.sin6;
|
2007-02-09 23:02:16 +05:30
|
|
|
if (source_lsa)
|
2008-01-29 16:03:34 +05:30
|
|
|
xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
#ifdef ICMP6_FILTER
|
|
|
|
{
|
|
|
|
struct icmp6_filter filt;
|
|
|
|
if (!(option_mask32 & OPT_VERBOSE)) {
|
|
|
|
ICMP6_FILTER_SETBLOCKALL(&filt);
|
|
|
|
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
|
|
|
|
} else {
|
|
|
|
ICMP6_FILTER_SETPASSALL(&filt);
|
|
|
|
}
|
|
|
|
if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
|
2013-01-14 06:04:48 +05:30
|
|
|
sizeof(filt)) < 0)
|
2015-08-24 22:38:14 +05:30
|
|
|
bb_error_msg_and_die("setsockopt(%s)", "ICMP6_FILTER");
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
#endif /*ICMP6_FILTER*/
|
|
|
|
|
|
|
|
/* enable broadcast pings */
|
|
|
|
setsockopt_broadcast(pingsock);
|
|
|
|
|
2008-08-24 05:32:18 +05:30
|
|
|
/* set recv buf (needed if we can get lots of responses: flood ping,
|
|
|
|
* broadcast ping etc) */
|
|
|
|
sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
|
2015-10-13 18:20:20 +05:30
|
|
|
BUILD_BUG_ON(offsetof(struct icmp6_hdr, icmp6_cksum) != 2);
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
/* request ttl info to be returned in ancillary data */
|
2015-08-24 23:18:03 +05:30
|
|
|
setsockopt_1(pingsock, SOL_IPV6, IPV6_HOPLIMIT);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
if (if_index)
|
2007-01-25 05:30:02 +05:30
|
|
|
pingaddr.sin6.sin6_scope_id = if_index;
|
2007-01-25 05:23:22 +05:30
|
|
|
|
2008-04-07 06:16:29 +05:30
|
|
|
signal(SIGINT, print_stats_and_exit);
|
2007-01-25 05:23:22 +05:30
|
|
|
|
|
|
|
msg.msg_name = &from;
|
|
|
|
msg.msg_namelen = sizeof(from);
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = control_buf;
|
2010-03-03 05:40:29 +05:30
|
|
|
iov.iov_base = G.rcv_packet;
|
|
|
|
iov.iov_len = G.sizeof_rcv_packet;
|
2018-02-14 03:51:33 +05:30
|
|
|
|
|
|
|
/* start the ping's going ... */
|
|
|
|
send_ping:
|
|
|
|
sendping6(0);
|
|
|
|
|
|
|
|
/* listen for replies */
|
2007-01-25 05:23:22 +05:30
|
|
|
while (1) {
|
|
|
|
int c;
|
|
|
|
struct cmsghdr *mp;
|
|
|
|
int hoplimit = -1;
|
|
|
|
|
2018-02-14 03:51:33 +05:30
|
|
|
msg.msg_controllen = sizeof(control_buf);
|
2007-01-25 05:23:22 +05:30
|
|
|
c = recvmsg(pingsock, &msg, 0);
|
|
|
|
if (c < 0) {
|
|
|
|
if (errno != EINTR)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("recvfrom");
|
2007-01-25 05:23:22 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
|
|
|
|
if (mp->cmsg_level == SOL_IPV6
|
|
|
|
&& mp->cmsg_type == IPV6_HOPLIMIT
|
|
|
|
/* don't check len - we trust the kernel: */
|
|
|
|
/* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
|
|
|
|
) {
|
2009-11-26 19:56:31 +05:30
|
|
|
/*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */
|
|
|
|
move_from_unaligned_int(hoplimit, CMSG_DATA(mp));
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
}
|
2018-02-14 03:51:33 +05:30
|
|
|
c = unpack6(G.rcv_packet, c, &from, hoplimit);
|
2013-03-15 04:41:35 +05:30
|
|
|
if (pingcount && G.nreceived >= pingcount)
|
2007-01-25 05:23:22 +05:30
|
|
|
break;
|
2018-02-14 03:51:33 +05:30
|
|
|
if (c && (option_mask32 & OPT_A)) {
|
|
|
|
goto send_ping;
|
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-08 04:44:59 +05:30
|
|
|
|
2007-02-09 23:02:16 +05:30
|
|
|
static void ping(len_and_sockaddr *lsa)
|
2006-09-02 21:47:30 +05:30
|
|
|
{
|
2007-02-09 23:02:16 +05:30
|
|
|
printf("PING %s (%s)", hostname, dotted);
|
|
|
|
if (source_lsa) {
|
|
|
|
printf(" from %s",
|
2008-01-29 16:03:34 +05:30
|
|
|
xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa));
|
2006-09-02 21:47:30 +05:30
|
|
|
}
|
2007-02-09 23:02:16 +05:30
|
|
|
printf(": %d data bytes\n", datalen);
|
|
|
|
|
2013-06-28 04:00:46 +05:30
|
|
|
create_icmp_socket(lsa);
|
|
|
|
/* untested whether "-I addr" really works for IPv6: */
|
|
|
|
if (str_I)
|
|
|
|
setsockopt_bindtodevice(pingsock, str_I);
|
|
|
|
|
2010-03-03 05:40:29 +05:30
|
|
|
G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
|
|
|
|
G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
|
2007-02-09 23:02:16 +05:30
|
|
|
#if ENABLE_PING6
|
2010-03-03 05:40:29 +05:30
|
|
|
if (lsa->u.sa.sa_family == AF_INET6) {
|
|
|
|
/* +4 reserves a place for timestamp, which may end up sitting
|
|
|
|
* _after_ packet. Saves one if() - see sendping4/6() */
|
|
|
|
G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4);
|
2007-02-09 23:02:16 +05:30
|
|
|
ping6(lsa);
|
2010-03-03 05:40:29 +05:30
|
|
|
} else
|
2007-02-09 23:02:16 +05:30
|
|
|
#endif
|
2010-03-03 05:40:29 +05:30
|
|
|
{
|
|
|
|
G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4);
|
2007-02-09 23:02:16 +05:30
|
|
|
ping4(lsa);
|
2010-03-03 05:40:29 +05:30
|
|
|
}
|
2006-09-02 21:47:30 +05:30
|
|
|
}
|
|
|
|
|
2009-11-20 22:42:12 +05:30
|
|
|
static int common_ping_main(int opt, char **argv)
|
1999-12-08 04:44:59 +05:30
|
|
|
{
|
2007-01-25 05:30:02 +05:30
|
|
|
len_and_sockaddr *lsa;
|
2014-08-27 19:31:25 +05:30
|
|
|
char *str_s, *str_p;
|
2018-08-03 22:20:50 +05:30
|
|
|
char *str_i = (char*)"1";
|
|
|
|
duration_t interval;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2007-06-04 16:03:48 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
opt |= getopt32(argv, "^"
|
|
|
|
OPT_STRING
|
|
|
|
/* exactly one arg; -v and -q don't mix */
|
|
|
|
"\0" "=1:q--v:v--q",
|
2018-08-03 22:20:50 +05:30
|
|
|
&pingcount, &str_s, &opt_ttl, &G.deadline_us, &timeout, &str_I, &str_p, &str_i
|
2017-08-09 01:25:02 +05:30
|
|
|
);
|
2008-10-27 17:24:45 +05:30
|
|
|
if (opt & OPT_s)
|
|
|
|
datalen = xatou16(str_s); // -s
|
|
|
|
if (opt & OPT_I) { // -I
|
|
|
|
if_index = if_nametoindex(str_I);
|
2007-02-09 23:02:16 +05:30
|
|
|
if (!if_index) {
|
|
|
|
/* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
|
2008-10-27 17:24:45 +05:30
|
|
|
source_lsa = xdotted2sockaddr(str_I, 0);
|
|
|
|
str_I = NULL; /* don't try to bind to device later */
|
2007-02-09 23:02:16 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2014-08-27 19:31:25 +05:30
|
|
|
if (opt & OPT_p)
|
|
|
|
G.pattern = xstrtou_range(str_p, 16, 0, 255);
|
2018-02-14 04:23:24 +05:30
|
|
|
if (G.deadline_us) {
|
|
|
|
unsigned d = G.deadline_us < INT_MAX/1000000 ? G.deadline_us : INT_MAX/1000000;
|
|
|
|
G.deadline_us = 1 | ((d * 1000000) + monotonic_us());
|
2018-02-14 03:51:33 +05:30
|
|
|
}
|
2018-08-03 22:20:50 +05:30
|
|
|
interval = parse_duration_str(str_i);
|
|
|
|
if (interval > INT_MAX/1000000)
|
|
|
|
interval = INT_MAX/1000000;
|
|
|
|
G.interval_us = interval * 1000000;
|
2014-08-27 19:31:25 +05:30
|
|
|
|
2007-05-27 06:23:41 +05:30
|
|
|
myid = (uint16_t) getpid();
|
2020-12-13 20:23:06 +05:30
|
|
|
/* we can use native-endian ident, but other Unix ping/traceroute
|
|
|
|
* utils use *big-endian pid*, and e.g. traceroute on our machine may be
|
|
|
|
* *not* from busybox, idents may collide. Follow the convention:
|
|
|
|
*/
|
|
|
|
myid = htons(myid);
|
2007-01-25 05:23:22 +05:30
|
|
|
hostname = argv[optind];
|
|
|
|
#if ENABLE_PING6
|
2008-10-27 17:24:45 +05:30
|
|
|
{
|
|
|
|
sa_family_t af = AF_UNSPEC;
|
|
|
|
if (opt & OPT_IPV4)
|
|
|
|
af = AF_INET;
|
|
|
|
if (opt & OPT_IPV6)
|
|
|
|
af = AF_INET6;
|
|
|
|
lsa = xhost_and_af2sockaddr(hostname, 0, af);
|
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#else
|
2007-02-04 08:09:08 +05:30
|
|
|
lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif
|
2007-02-09 23:02:16 +05:30
|
|
|
|
2008-01-29 16:03:34 +05:30
|
|
|
if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family)
|
2007-02-09 23:02:16 +05:30
|
|
|
/* leaking it here... */
|
|
|
|
source_lsa = NULL;
|
|
|
|
|
2008-01-29 16:03:34 +05:30
|
|
|
dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
|
2007-02-09 23:02:16 +05:30
|
|
|
ping(lsa);
|
2018-02-14 03:51:33 +05:30
|
|
|
print_stats_and_exit(0);
|
2008-04-07 06:16:29 +05:30
|
|
|
/*return EXIT_SUCCESS;*/
|
1999-12-08 04:44:59 +05:30
|
|
|
}
|
2007-01-25 05:23:22 +05:30
|
|
|
#endif /* FEATURE_FANCY_PING */
|
|
|
|
|
|
|
|
|
2016-11-23 13:55:57 +05:30
|
|
|
#if ENABLE_PING
|
2009-11-20 22:42:12 +05:30
|
|
|
int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int ping_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
2016-11-23 13:55:57 +05:30
|
|
|
# if !ENABLE_FEATURE_FANCY_PING
|
2009-11-20 22:42:12 +05:30
|
|
|
return common_ping_main(AF_UNSPEC, argv);
|
2016-11-23 13:55:57 +05:30
|
|
|
# else
|
2009-11-20 22:42:12 +05:30
|
|
|
return common_ping_main(0, argv);
|
2016-11-23 13:55:57 +05:30
|
|
|
# endif
|
2009-11-20 22:42:12 +05:30
|
|
|
}
|
2016-11-23 13:55:57 +05:30
|
|
|
#endif
|
2009-11-20 22:42:12 +05:30
|
|
|
|
2007-01-25 05:23:22 +05:30
|
|
|
#if ENABLE_PING6
|
2007-10-11 15:35:36 +05:30
|
|
|
int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-10-27 17:24:45 +05:30
|
|
|
int ping6_main(int argc UNUSED_PARAM, char **argv)
|
2007-01-25 05:23:22 +05:30
|
|
|
{
|
2009-11-20 22:42:12 +05:30
|
|
|
# if !ENABLE_FEATURE_FANCY_PING
|
|
|
|
return common_ping_main(AF_INET6, argv);
|
|
|
|
# else
|
|
|
|
return common_ping_main(OPT_IPV6, argv);
|
|
|
|
# endif
|
2007-01-25 05:23:22 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* from ping6.c:
|
|
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Mike Muuss.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
|
|
|
|
* ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
|
|
|
|
*
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
2017-08-02 17:56:33 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
|
2007-01-25 05:23:22 +05:30
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|