| 1 |
/* |
| 2 |
Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <netinet/in_systm.h> |
| 20 |
#include <netinet/in.h> |
| 21 |
#include <netinet/ip.h> |
| 22 |
#include <netinet/icmp6.h> |
| 23 |
#include <icmp.h> |
| 24 |
#include <error.h> |
| 25 |
#include <progname.h> |
| 26 |
|
| 27 |
#include <stdbool.h> |
| 28 |
|
| 29 |
#define MAXWAIT 10 /* Max seconds to wait for response. */ |
| 30 |
#define MAXPATTERN 16 /* Maximal length of pattern. */ |
| 31 |
|
| 32 |
#define OPT_FLOOD 0x001 |
| 33 |
#define OPT_INTERVAL 0x002 |
| 34 |
#define OPT_NUMERIC 0x004 |
| 35 |
#define OPT_QUIET 0x008 |
| 36 |
#define OPT_RROUTE 0x010 |
| 37 |
#define OPT_VERBOSE 0x020 |
| 38 |
#define OPT_IPTIMESTAMP 0x040 |
| 39 |
#define OPT_FLOWINFO 0x080 |
| 40 |
#define OPT_TCLASS 0x100 |
| 41 |
|
| 42 |
#define SOPT_TSONLY 0x001 |
| 43 |
#define SOPT_TSADDR 0x002 |
| 44 |
#define SOPT_TSPRESPEC 0x004 |
| 45 |
|
| 46 |
struct ping_stat |
| 47 |
{ |
| 48 |
double tmin; /* minimum round trip time */ |
| 49 |
double tmax; /* maximum round trip time */ |
| 50 |
double tsum; /* sum of all times, for doing average */ |
| 51 |
double tsumsq; /* sum of all times squared, for std. dev. */ |
| 52 |
}; |
| 53 |
|
| 54 |
#define PEV_RESPONSE 0 |
| 55 |
#define PEV_DUPLICATE 1 |
| 56 |
#define PEV_NOECHO 2 |
| 57 |
|
| 58 |
#define PING_CKTABSIZE 128 |
| 59 |
|
| 60 |
/* The rationale for not exiting after a sending N packets is that we |
| 61 |
want to follow the traditional behaviour of ping. */ |
| 62 |
#define DEFAULT_PING_COUNT 0 |
| 63 |
|
| 64 |
#define PING_HEADER_LEN (USE_IPV6 ? sizeof (struct icmp6_hdr) : ICMP_MINLEN) |
| 65 |
#define PING_TIMING(s) ((s) >= sizeof (struct timeval)) |
| 66 |
#define PING_DATALEN (64 - PING_HEADER_LEN) /* default data length */ |
| 67 |
|
| 68 |
#define PING_DEFAULT_INTERVAL 1000 /* Milliseconds */ |
| 69 |
#define PING_PRECISION 1000 /* Millisecond precision */ |
| 70 |
|
| 71 |
#define PING_SET_INTERVAL(t,i) do {\ |
| 72 |
(t).tv_sec = (i)/PING_PRECISION;\ |
| 73 |
(t).tv_nsec = ((i)%PING_PRECISION)*(1e9/PING_PRECISION) ;\ |
| 74 |
} while (0) |
| 75 |
|
| 76 |
|
| 77 |
#define PING_MIN_USER_INTERVAL (200000/PING_PRECISION) |
| 78 |
|
| 79 |
/* FIXME: Adjust IPv6 case for options and their consumption. */ |
| 80 |
#define _PING_BUFLEN(p, u) ((u)? ((p)->ping_datalen + sizeof (struct icmp6_hdr)) : \ |
| 81 |
(MAXIPLEN + (p)->ping_datalen + ICMP_TSLEN)) |
| 82 |
|
| 83 |
typedef int (*ping_efp6) (int code, void *closure, struct sockaddr_in6 * dest, |
| 84 |
struct sockaddr_in6 * from, struct icmp6_hdr * icmp, |
| 85 |
int datalen); |
| 86 |
|
| 87 |
typedef int (*ping_efp) (int code, |
| 88 |
void *closure, |
| 89 |
struct sockaddr_in * dest, |
| 90 |
struct sockaddr_in * from, |
| 91 |
struct ip * ip, icmphdr_t * icmp, int datalen); |
| 92 |
|
| 93 |
union event |
| 94 |
{ |
| 95 |
ping_efp6 handler6; |
| 96 |
ping_efp handler; |
| 97 |
}; |
| 98 |
|
| 99 |
union ping_address |
| 100 |
{ |
| 101 |
struct sockaddr_in ping_sockaddr; |
| 102 |
struct sockaddr_in6 ping_sockaddr6; |
| 103 |
}; |
| 104 |
|
| 105 |
typedef struct ping_data PING; |
| 106 |
|
| 107 |
struct ping_data |
| 108 |
{ |
| 109 |
int ping_fd; /* Raw socket descriptor */ |
| 110 |
int ping_type; /* Type of packets to send */ |
| 111 |
size_t ping_count; /* Number of packets to send */ |
| 112 |
struct timespec ping_start_time; /* Start time */ |
| 113 |
size_t ping_interval; /* Number of seconds to wait between sending pkts */ |
| 114 |
union ping_address ping_dest; /* whom to ping */ |
| 115 |
char *ping_hostname; /* Printable hostname */ |
| 116 |
size_t ping_datalen; /* Length of data */ |
| 117 |
int ping_ident; /* Our identifier */ |
| 118 |
union event ping_event; /* User-defined handler */ |
| 119 |
void *ping_closure; /* User-defined data */ |
| 120 |
|
| 121 |
/* Runtime info */ |
| 122 |
int ping_cktab_size; |
| 123 |
char *ping_cktab; |
| 124 |
|
| 125 |
unsigned char *ping_buffer; /* I/O buffer */ |
| 126 |
union ping_address ping_from; |
| 127 |
size_t ping_num_xmit; /* Number of packets transmitted */ |
| 128 |
size_t ping_num_recv; /* Number of packets received */ |
| 129 |
size_t ping_num_rept; /* Number of duplicates received */ |
| 130 |
}; |
| 131 |
|
| 132 |
#define _C_BIT(p,bit) (p)->ping_cktab[(bit)>>3] /* byte in ck array */ |
| 133 |
#define _C_MASK(bit) (1 << ((bit) & 0x07)) |
| 134 |
#define _C_IND(p,bit) ((bit) % (8 * (p)->ping_cktab_size)) |
| 135 |
|
| 136 |
#define _PING_SET(p,bit) \ |
| 137 |
do \ |
| 138 |
{ \ |
| 139 |
int n = _C_IND (p,bit); \ |
| 140 |
_C_BIT (p,n) |= _C_MASK (n); \ |
| 141 |
} \ |
| 142 |
while (0) |
| 143 |
|
| 144 |
#define _PING_CLR(p,bit) \ |
| 145 |
do \ |
| 146 |
{ \ |
| 147 |
int n = _C_IND (p,bit); \ |
| 148 |
_C_BIT (p,n) &= ~_C_MASK (n); \ |
| 149 |
} \ |
| 150 |
while (0) |
| 151 |
|
| 152 |
#define _PING_TST(p,bit) \ |
| 153 |
(_C_BIT (p, _C_IND (p,bit)) & _C_MASK (_C_IND (p,bit))) |
| 154 |
|
| 155 |
|
| 156 |
double nabs (double a); |
| 157 |
double nsqrt (double a, double prec); |
| 158 |
|
| 159 |
size_t ping_cvt_number (const char *optarg, size_t maxval, int allow_zero); |
| 160 |
int is_normed_time (n_time t); |
| 161 |
const char *ping_cvt_time (char *buf, size_t buflen, n_time t); |
| 162 |
|
| 163 |
void init_data_buffer (unsigned char *pat, size_t len); |
| 164 |
|
| 165 |
void decode_pattern (const char *text, int *pattern_len, |
| 166 |
unsigned char *pattern_data); |
| 167 |
int _ping_setbuf (PING * p, bool use_ipv6); |
| 168 |
int ping_set_data (PING * p, void *data, size_t off, size_t len, |
| 169 |
bool use_ipv6); |
| 170 |
void ping_set_count (PING * ping, size_t count); |
| 171 |
void ping_set_sockopt (PING * ping, int opt, void *val, int valsize); |
| 172 |
void ping_set_interval (PING * ping, size_t interval); |
| 173 |
void ping_unset_data (PING * p); |
| 174 |
bool ping_timeout_p (struct timespec *start_time, int timeout); |
| 175 |
|
| 176 |
char *ipaddr2str (struct sockaddr *from, socklen_t fromlen); |
| 177 |
char *sinaddr2str (struct in_addr ina); |