| 1 |
/* |
| 2 |
* Copyright (c) 1989 The Regents of the University of California. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This code is derived from software contributed to Berkeley by |
| 6 |
* Mike Muuss. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* 3. Neither the name of the University nor the names of its contributors |
| 17 |
* may be used to endorse or promote products derived from this software |
| 18 |
* without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 |
* SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#define _GNU_SOURCE |
| 34 |
|
| 35 |
#include "iputils_common.h" |
| 36 |
#include "ping.h" |
| 37 |
|
| 38 |
#ifndef HZ |
| 39 |
#define HZ sysconf(_SC_CLK_TCK) |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef HAVE_LIBCAP |
| 43 |
static uid_t euid; |
| 44 |
#endif |
| 45 |
|
| 46 |
void usage(void) |
| 47 |
{ |
| 48 |
fprintf(stderr, _( |
| 49 |
"\nUsage\n" |
| 50 |
" ping [options] <destination>\n" |
| 51 |
"\nOptions:\n" |
| 52 |
" <destination> DNS name or IP address\n" |
| 53 |
" -3 RTT precision (do not round up the result time)\n" |
| 54 |
" -a use audible ping\n" |
| 55 |
" -A use adaptive ping\n" |
| 56 |
" -B sticky source address\n" |
| 57 |
" -c <count> stop after <count> replies\n" |
| 58 |
" -C call connect() syscall on socket creation\n" |
| 59 |
" -D print timestamps\n" |
| 60 |
" -d use SO_DEBUG socket option\n" |
| 61 |
" -e <identifier> define identifier for ping session, default is random for\n" |
| 62 |
" SOCK_RAW and kernel defined for SOCK_DGRAM\n" |
| 63 |
" Imply using SOCK_RAW (for IPv4 only for identifier 0)\n" |
| 64 |
" -f flood ping\n" |
| 65 |
" -h print help and exit\n" |
| 66 |
" -H force reverse DNS name resolution (useful for numeric\n" |
| 67 |
" destinations or for -f), override -n\n" |
| 68 |
" -I <interface> either interface name or address\n" |
| 69 |
" -i <interval> seconds between sending each packet\n" |
| 70 |
" -L suppress loopback of multicast packets\n" |
| 71 |
" -l <preload> send <preload> number of packages while waiting replies\n" |
| 72 |
" -m <mark> tag the packets going out\n" |
| 73 |
" -M <pmtud opt> define path MTU discovery, can be one of <do|dont|want|probe>\n" |
| 74 |
" -n no reverse DNS name resolution, override -H\n" |
| 75 |
" -O report outstanding replies\n" |
| 76 |
" -p <pattern> contents of padding byte\n" |
| 77 |
" -q quiet output\n" |
| 78 |
" -Q <tclass> use quality of service <tclass> bits\n" |
| 79 |
" -s <size> use <size> as number of data bytes to be sent\n" |
| 80 |
" -S <size> use <size> as SO_SNDBUF socket option value\n" |
| 81 |
" -t <ttl> define time to live\n" |
| 82 |
" -U print user-to-user latency\n" |
| 83 |
" -v verbose output\n" |
| 84 |
" -V print version and exit\n" |
| 85 |
" -w <deadline> reply wait <deadline> in seconds\n" |
| 86 |
" -W <timeout> time to wait for response\n" |
| 87 |
"\nIPv4 options:\n" |
| 88 |
" -4 use IPv4\n" |
| 89 |
" -b allow pinging broadcast\n" |
| 90 |
" -R record route\n" |
| 91 |
" -T <timestamp> define timestamp, can be one of <tsonly|tsandaddr|tsprespec>\n" |
| 92 |
"\nIPv6 options:\n" |
| 93 |
" -6 use IPv6\n" |
| 94 |
" -F <flowlabel> define flow label, default is random\n" |
| 95 |
" -N <nodeinfo opt> use IPv6 node info query, try <help> as argument\n" |
| 96 |
"\nFor more details see ping(8).\n" |
| 97 |
)); |
| 98 |
exit(2); |
| 99 |
} |
| 100 |
|
| 101 |
void limit_capabilities(struct ping_rts *rts) |
| 102 |
{ |
| 103 |
#ifdef HAVE_LIBCAP |
| 104 |
cap_t cap_cur_p; |
| 105 |
cap_t cap_p; |
| 106 |
cap_flag_value_t cap_ok; |
| 107 |
|
| 108 |
cap_cur_p = cap_get_proc(); |
| 109 |
if (!cap_cur_p) |
| 110 |
error(-1, errno, "cap_get_proc"); |
| 111 |
cap_p = cap_init(); |
| 112 |
if (!cap_p) |
| 113 |
error(-1, errno, "cap_init"); |
| 114 |
cap_ok = CAP_CLEAR; |
| 115 |
cap_get_flag(cap_cur_p, CAP_NET_ADMIN, CAP_PERMITTED, &cap_ok); |
| 116 |
if (cap_ok != CAP_CLEAR) |
| 117 |
cap_set_flag(cap_p, CAP_PERMITTED, 1, &rts->cap_admin, CAP_SET); |
| 118 |
cap_ok = CAP_CLEAR; |
| 119 |
cap_get_flag(cap_cur_p, CAP_NET_RAW, CAP_PERMITTED, &cap_ok); |
| 120 |
if (cap_ok != CAP_CLEAR) |
| 121 |
cap_set_flag(cap_p, CAP_PERMITTED, 1, &rts->cap_raw, CAP_SET); |
| 122 |
if (cap_set_proc(cap_p) < 0) |
| 123 |
error(-1, errno, "cap_set_proc"); |
| 124 |
if (prctl(PR_SET_KEEPCAPS, 1) < 0) |
| 125 |
error(-1, errno, "prctl"); |
| 126 |
if (setuid(getuid()) < 0) |
| 127 |
error(-1, errno, "setuid"); |
| 128 |
if (prctl(PR_SET_KEEPCAPS, 0) < 0) |
| 129 |
error(-1, errno, "prctl"); |
| 130 |
cap_free(cap_p); |
| 131 |
cap_free(cap_cur_p); |
| 132 |
#else |
| 133 |
euid = geteuid(); |
| 134 |
#endif |
| 135 |
rts->uid = getuid(); |
| 136 |
#ifndef HAVE_LIBCAP |
| 137 |
if (seteuid(rts->uid)) |
| 138 |
error(-1, errno, "setuid"); |
| 139 |
#endif |
| 140 |
} |
| 141 |
|
| 142 |
#ifdef HAVE_LIBCAP |
| 143 |
int modify_capability(cap_value_t cap, cap_flag_value_t on) |
| 144 |
{ |
| 145 |
cap_t cap_p = cap_get_proc(); |
| 146 |
cap_flag_value_t cap_ok; |
| 147 |
int rc = -1; |
| 148 |
|
| 149 |
if (!cap_p) { |
| 150 |
error(0, errno, "cap_get_proc"); |
| 151 |
goto out; |
| 152 |
} |
| 153 |
|
| 154 |
cap_ok = CAP_CLEAR; |
| 155 |
cap_get_flag(cap_p, cap, CAP_PERMITTED, &cap_ok); |
| 156 |
if (cap_ok == CAP_CLEAR) { |
| 157 |
rc = on ? -1 : 0; |
| 158 |
goto out; |
| 159 |
} |
| 160 |
|
| 161 |
cap_set_flag(cap_p, CAP_EFFECTIVE, 1, &cap, on); |
| 162 |
|
| 163 |
if (cap_set_proc(cap_p) < 0) { |
| 164 |
error(0, errno, "cap_set_proc"); |
| 165 |
goto out; |
| 166 |
} |
| 167 |
|
| 168 |
cap_free(cap_p); |
| 169 |
cap_p = NULL; |
| 170 |
|
| 171 |
rc = 0; |
| 172 |
out: |
| 173 |
if (cap_p) |
| 174 |
cap_free(cap_p); |
| 175 |
return rc; |
| 176 |
} |
| 177 |
#else |
| 178 |
int modify_capability(int on) |
| 179 |
{ |
| 180 |
if (seteuid(on ? euid : getuid())) { |
| 181 |
error(0, errno, "seteuid"); |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
return 0; |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
void drop_capabilities(void) |
| 190 |
{ |
| 191 |
#ifdef HAVE_LIBCAP |
| 192 |
cap_t cap = cap_init(); |
| 193 |
if (cap_set_proc(cap) < 0) |
| 194 |
error(-1, errno, "cap_set_proc"); |
| 195 |
cap_free(cap); |
| 196 |
#else |
| 197 |
if (setuid(getuid())) |
| 198 |
error(-1, errno, "setuid"); |
| 199 |
#endif |
| 200 |
} |
| 201 |
|
| 202 |
/* Fills all the outpack, excluding ICMP header, but _including_ |
| 203 |
* timestamp area with supplied pattern. |
| 204 |
*/ |
| 205 |
void fill(struct ping_rts *rts, char *patp, unsigned char *packet, unsigned packet_size) |
| 206 |
{ |
| 207 |
int ii, jj; |
| 208 |
unsigned int pat[16]; |
| 209 |
char *cp; |
| 210 |
unsigned char *bp = packet + 8; |
| 211 |
|
| 212 |
#ifdef USE_IDN |
| 213 |
setlocale(LC_ALL, "C"); |
| 214 |
#endif |
| 215 |
|
| 216 |
for (cp = patp; *cp; cp++) { |
| 217 |
if (!isxdigit(*cp)) |
| 218 |
error(2, 0, _("patterns must be specified as hex digits: %s"), cp); |
| 219 |
} |
| 220 |
ii = sscanf(patp, |
| 221 |
"%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", |
| 222 |
&pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], |
| 223 |
&pat[6], &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], |
| 224 |
&pat[12], &pat[13], &pat[14], &pat[15]); |
| 225 |
|
| 226 |
if (ii > 0) { |
| 227 |
unsigned kk; |
| 228 |
for (kk = 0; kk <= packet_size - (8 + ii); kk += ii) |
| 229 |
for (jj = 0; jj < ii; ++jj) |
| 230 |
bp[jj + kk] = pat[jj]; |
| 231 |
} |
| 232 |
if (!rts->opt_quiet) { |
| 233 |
printf(_("PATTERN: 0x")); |
| 234 |
for (jj = 0; jj < ii; ++jj) |
| 235 |
printf("%02x", bp[jj] & 0xFF); |
| 236 |
printf("\n"); |
| 237 |
} |
| 238 |
|
| 239 |
#ifdef USE_IDN |
| 240 |
setlocale(LC_ALL, ""); |
| 241 |
#endif |
| 242 |
} |
| 243 |
|
| 244 |
static void sigexit(int signo __attribute__((__unused__))) |
| 245 |
{ |
| 246 |
global_rts->exiting = 1; |
| 247 |
if (global_rts->in_pr_addr) |
| 248 |
longjmp(global_rts->pr_addr_jmp, 0); |
| 249 |
} |
| 250 |
|
| 251 |
static void sigstatus(int signo __attribute__((__unused__))) |
| 252 |
{ |
| 253 |
global_rts->status_snapshot = 1; |
| 254 |
} |
| 255 |
|
| 256 |
int __schedule_exit(int next) |
| 257 |
{ |
| 258 |
static unsigned long waittime; |
| 259 |
struct itimerval it; |
| 260 |
|
| 261 |
if (waittime) |
| 262 |
return next; |
| 263 |
|
| 264 |
if (global_rts->nreceived) { |
| 265 |
waittime = 2 * global_rts->tmax; |
| 266 |
if (waittime < 1000 * (unsigned long)global_rts->interval) |
| 267 |
waittime = 1000 * global_rts->interval; |
| 268 |
} else { |
| 269 |
waittime = global_rts->lingertime; |
| 270 |
} |
| 271 |
|
| 272 |
if (next < 0 || (unsigned long)next < waittime / 1000) |
| 273 |
next = waittime / 1000; |
| 274 |
|
| 275 |
it.it_interval.tv_sec = 0; |
| 276 |
it.it_interval.tv_usec = 0; |
| 277 |
it.it_value.tv_sec = waittime / 1000000; |
| 278 |
it.it_value.tv_usec = waittime % 1000000; |
| 279 |
setitimer(ITIMER_REAL, &it, NULL); |
| 280 |
return next; |
| 281 |
} |
| 282 |
|
| 283 |
static inline void update_interval(struct ping_rts *rts) |
| 284 |
{ |
| 285 |
int est = rts->rtt ? (int)(rts->rtt / 8) : rts->interval * 1000; |
| 286 |
|
| 287 |
rts->interval = (est + rts->rtt_addend + 500) / 1000; |
| 288 |
if (rts->uid && rts->interval < MIN_USER_INTERVAL_MS) |
| 289 |
rts->interval = MIN_USER_INTERVAL_MS; |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* Print timestamp |
| 294 |
*/ |
| 295 |
void print_timestamp(struct ping_rts *rts) |
| 296 |
{ |
| 297 |
if (rts->opt_ptimeofday) { |
| 298 |
struct timeval tv; |
| 299 |
gettimeofday(&tv, NULL); |
| 300 |
printf("[%lu.%06lu] ", |
| 301 |
(unsigned long)tv.tv_sec, (unsigned long)tv.tv_usec); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/* |
| 306 |
* pinger -- |
| 307 |
* Compose and transmit an ICMP ECHO REQUEST packet. The IP packet |
| 308 |
* will be added on by the kernel. The ID field is our UNIX process ID, |
| 309 |
* and the sequence number is an ascending integer. The first several bytes |
| 310 |
* of the data portion are used to hold a UNIX "timeval" struct in VAX |
| 311 |
* byte-order, to compute the round-trip time. |
| 312 |
*/ |
| 313 |
int pinger(struct ping_rts *rts, ping_func_set_st *fset, socket_st *sock) |
| 314 |
{ |
| 315 |
static int oom_count; |
| 316 |
static int tokens; |
| 317 |
int i; |
| 318 |
|
| 319 |
/* Have we already sent enough? If we have, return an arbitrary positive value. */ |
| 320 |
if (rts->exiting || (rts->npackets && rts->ntransmitted >= rts->npackets && !rts->deadline)) |
| 321 |
return 1000; |
| 322 |
|
| 323 |
/* Check that packets < rate*time + preload */ |
| 324 |
if (rts->cur_time.tv_sec == 0 && rts->cur_time.tv_nsec == 0) { |
| 325 |
clock_gettime(CLOCK_MONOTONIC_RAW, &rts->cur_time); |
| 326 |
tokens = rts->interval * (rts->preload - 1); |
| 327 |
} else { |
| 328 |
long ntokens, tmp; |
| 329 |
struct timespec tv; |
| 330 |
|
| 331 |
clock_gettime(CLOCK_MONOTONIC_RAW, &tv); |
| 332 |
ntokens = (tv.tv_sec - rts->cur_time.tv_sec) * 1000 + |
| 333 |
(tv.tv_nsec - rts->cur_time.tv_nsec) / 1000000; |
| 334 |
if (!rts->interval) { |
| 335 |
/* Case of unlimited flood is special; |
| 336 |
* if we see no reply, they are limited to 100pps */ |
| 337 |
if (ntokens < MIN_INTERVAL_MS && in_flight(rts) >= rts->preload) |
| 338 |
return MIN_INTERVAL_MS - ntokens; |
| 339 |
} |
| 340 |
ntokens += tokens; |
| 341 |
tmp = (long)rts->interval * (long)rts->preload; |
| 342 |
if (tmp < ntokens) |
| 343 |
ntokens = tmp; |
| 344 |
if (ntokens < rts->interval) |
| 345 |
return rts->interval - ntokens; |
| 346 |
|
| 347 |
rts->cur_time = tv; |
| 348 |
tokens = ntokens - rts->interval; |
| 349 |
} |
| 350 |
|
| 351 |
if (rts->opt_outstanding) { |
| 352 |
if (rts->ntransmitted > 0 && !rcvd_test(rts, rts->ntransmitted)) { |
| 353 |
print_timestamp(rts); |
| 354 |
printf(_("no answer yet for icmp_seq=%lu\n"), (rts->ntransmitted % MAX_DUP_CHK)); |
| 355 |
fflush(stdout); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
resend: |
| 360 |
i = fset->send_probe(rts, sock, rts->outpack, sizeof(rts->outpack)); |
| 361 |
|
| 362 |
if (i == 0) { |
| 363 |
oom_count = 0; |
| 364 |
advance_ntransmitted(rts); |
| 365 |
if (!rts->opt_quiet && rts->opt_flood) { |
| 366 |
/* Very silly, but without this output with |
| 367 |
* high preload or pipe size is very confusing. */ |
| 368 |
if ((rts->preload < rts->screen_width && rts->pipesize < rts->screen_width) || |
| 369 |
in_flight(rts) < rts->screen_width) |
| 370 |
write_stdout(".", 1); |
| 371 |
} |
| 372 |
return rts->interval - tokens; |
| 373 |
} |
| 374 |
|
| 375 |
/* And handle various errors... */ |
| 376 |
if (i > 0) { |
| 377 |
/* Apparently, it is some fatal bug. */ |
| 378 |
abort(); |
| 379 |
} else if (errno == ENOBUFS || errno == ENOMEM) { |
| 380 |
int nores_interval; |
| 381 |
|
| 382 |
/* Device queue overflow or OOM. Packet is not sent. */ |
| 383 |
tokens = 0; |
| 384 |
/* Slowdown. This works only in adaptive mode (option -A) */ |
| 385 |
rts->rtt_addend += (rts->rtt < 8 * 50000 ? rts->rtt / 8 : 50000); |
| 386 |
if (rts->opt_adaptive) |
| 387 |
update_interval(rts); |
| 388 |
nores_interval = SCHINT(rts->interval / 2); |
| 389 |
if (nores_interval > 500) |
| 390 |
nores_interval = 500; |
| 391 |
oom_count++; |
| 392 |
if ((uint32_t)(oom_count * nores_interval) < rts->lingertime/1000) |
| 393 |
return nores_interval; |
| 394 |
i = 0; |
| 395 |
/* Fall to hard error. It is to avoid complete deadlock |
| 396 |
* on stuck output device even when dealine was not requested. |
| 397 |
* Expected timings are screwed up in any case, but we will |
| 398 |
* exit some day. :-) */ |
| 399 |
} else if (errno == EAGAIN) { |
| 400 |
/* Socket buffer is full. */ |
| 401 |
tokens += rts->interval; |
| 402 |
return MIN_INTERVAL_MS; |
| 403 |
} else if (errno == EMSGSIZE) { |
| 404 |
/* For example, sendto with len > 65527 on SOCK_DGRAM fails with this errno. */ |
| 405 |
rts->nerrors++; |
| 406 |
i = 0; |
| 407 |
} else { |
| 408 |
if ((i = fset->receive_error_msg(rts, sock)) > 0) { |
| 409 |
/* An ICMP error arrived. In this case, we've received |
| 410 |
* an error from sendto(), but we've also received an |
| 411 |
* ICMP message, which means the packet did in fact |
| 412 |
* send in some capacity. So, in this odd case, report |
| 413 |
* the more specific errno as the error, and treat this |
| 414 |
* as a hard local error. */ |
| 415 |
i = 0; |
| 416 |
goto hard_local_error; |
| 417 |
} |
| 418 |
/* Compatibility with old linuces. */ |
| 419 |
if (i == 0 && rts->confirm_flag && errno == EINVAL) { |
| 420 |
rts->confirm_flag = 0; |
| 421 |
errno = 0; |
| 422 |
} |
| 423 |
if (!errno) |
| 424 |
goto resend; |
| 425 |
} |
| 426 |
|
| 427 |
hard_local_error: |
| 428 |
/* Hard local error. Pretend we sent packet. */ |
| 429 |
advance_ntransmitted(rts); |
| 430 |
|
| 431 |
if (i == 0 && !rts->opt_quiet) { |
| 432 |
if (rts->opt_flood) |
| 433 |
write_stdout("E", 1); |
| 434 |
else |
| 435 |
error(0, errno, "sendmsg"); |
| 436 |
} |
| 437 |
tokens = 0; |
| 438 |
return SCHINT(rts->interval); |
| 439 |
} |
| 440 |
|
| 441 |
/* Set socket buffers, "alloc" is an estimate of memory taken by single packet. */ |
| 442 |
|
| 443 |
void sock_setbufs(struct ping_rts *rts, socket_st *sock, int alloc) |
| 444 |
{ |
| 445 |
int rcvbuf, hold; |
| 446 |
socklen_t tmplen = sizeof(hold); |
| 447 |
|
| 448 |
if (!rts->sndbuf) |
| 449 |
rts->sndbuf = alloc; |
| 450 |
setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, (char *)&rts->sndbuf, sizeof(rts->sndbuf)); |
| 451 |
|
| 452 |
if (alloc > INT_MAX / rts->preload) { |
| 453 |
error(0, 0, _("WARNING: buffer size overflow, reduce packet size or preload")); |
| 454 |
hold = INT_MAX; |
| 455 |
} else { |
| 456 |
hold = alloc * rts->preload; |
| 457 |
} |
| 458 |
|
| 459 |
rcvbuf = hold; |
| 460 |
|
| 461 |
if (hold < 65536) |
| 462 |
hold = 65536; |
| 463 |
|
| 464 |
setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, (char *)&hold, sizeof(hold)); |
| 465 |
if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, (char *)&hold, &tmplen) == 0) { |
| 466 |
if (hold < rcvbuf) |
| 467 |
error(0, 0, _("WARNING: probably, rcvbuf is not enough to hold preload")); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
void sock_setmark(struct ping_rts *rts, int fd) |
| 472 |
{ |
| 473 |
#ifdef SO_MARK |
| 474 |
int ret; |
| 475 |
int errno_save; |
| 476 |
|
| 477 |
if (!rts->opt_mark) |
| 478 |
return; |
| 479 |
|
| 480 |
enable_capability_admin(); |
| 481 |
ret = setsockopt(fd, SOL_SOCKET, SO_MARK, &(rts->mark), sizeof(rts->mark)); |
| 482 |
errno_save = errno; |
| 483 |
disable_capability_admin(); |
| 484 |
|
| 485 |
if (ret == -1) { |
| 486 |
error(0, errno_save, _("WARNING: failed to set mark: %u"), rts->mark); |
| 487 |
|
| 488 |
if (errno_save == EPERM) |
| 489 |
error(0, 0, _("=> missing cap_net_admin+p or cap_net_raw+p (since Linux 5.17) capability?")); |
| 490 |
|
| 491 |
rts->opt_mark = 0; |
| 492 |
} |
| 493 |
#else |
| 494 |
error(0, errno_save, _("WARNING: SO_MARK not supported")); |
| 495 |
#endif |
| 496 |
} |
| 497 |
|
| 498 |
/* Protocol independent setup and parameter checks. */ |
| 499 |
|
| 500 |
void setup(struct ping_rts *rts, socket_st *sock) |
| 501 |
{ |
| 502 |
int hold; |
| 503 |
struct timeval tv; |
| 504 |
sigset_t sset; |
| 505 |
|
| 506 |
if (rts->opt_flood && !rts->opt_interval) |
| 507 |
rts->interval = 0; |
| 508 |
|
| 509 |
if (rts->uid && rts->interval < MIN_USER_INTERVAL_MS) |
| 510 |
error(2, 0, _("cannot flood, minimal interval for user must be >= %d ms, use -i %s (or higher)"), |
| 511 |
MIN_USER_INTERVAL_MS, str_interval(MIN_USER_INTERVAL_MS)); |
| 512 |
|
| 513 |
if (rts->interval >= INT_MAX / rts->preload) |
| 514 |
error(2, 0, _("illegal preload and/or interval: %d"), rts->interval); |
| 515 |
|
| 516 |
hold = 1; |
| 517 |
if (rts->opt_so_debug) |
| 518 |
setsockopt(sock->fd, SOL_SOCKET, SO_DEBUG, (char *)&hold, sizeof(hold)); |
| 519 |
if (rts->opt_so_dontroute) |
| 520 |
setsockopt(sock->fd, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, sizeof(hold)); |
| 521 |
|
| 522 |
#ifdef SO_TIMESTAMP |
| 523 |
if (!rts->opt_latency) { |
| 524 |
int on = 1; |
| 525 |
if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on))) |
| 526 |
error(0, 0, _("Warning: no SO_TIMESTAMP support, falling back to SIOCGSTAMP")); |
| 527 |
} |
| 528 |
#endif |
| 529 |
|
| 530 |
sock_setmark(rts, sock->fd); |
| 531 |
|
| 532 |
/* Set some SNDTIMEO to prevent blocking forever |
| 533 |
* on sends, when device is too slow or stalls. Just put limit |
| 534 |
* of one second, or "interval", if it is less. |
| 535 |
*/ |
| 536 |
tv.tv_sec = 1; |
| 537 |
tv.tv_usec = 0; |
| 538 |
if (rts->interval < 1000) { |
| 539 |
tv.tv_sec = 0; |
| 540 |
tv.tv_usec = 1000 * SCHINT(rts->interval); |
| 541 |
} |
| 542 |
setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); |
| 543 |
|
| 544 |
/* Set RCVTIMEO to "interval". Note, it is just an optimization |
| 545 |
* allowing to avoid redundant poll(). */ |
| 546 |
tv.tv_sec = SCHINT(rts->interval) / 1000; |
| 547 |
tv.tv_usec = 1000 * (SCHINT(rts->interval) % 1000); |
| 548 |
if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv))) |
| 549 |
rts->opt_flood_poll = 1; |
| 550 |
|
| 551 |
if (!rts->opt_pingfilled) { |
| 552 |
int i; |
| 553 |
unsigned char *p = rts->outpack + 8; |
| 554 |
|
| 555 |
/* Do not forget about case of small datalen, fill timestamp area too! */ |
| 556 |
for (i = 0; i < rts->datalen; ++i) |
| 557 |
*p++ = i; |
| 558 |
} |
| 559 |
|
| 560 |
if (sock->socktype == SOCK_RAW && rts->ident == -1) |
| 561 |
rts->ident = htons(getpid() & 0xFFFF); |
| 562 |
|
| 563 |
set_signal(SIGINT, sigexit); |
| 564 |
set_signal(SIGALRM, sigexit); |
| 565 |
set_signal(SIGQUIT, sigstatus); |
| 566 |
|
| 567 |
sigemptyset(&sset); |
| 568 |
sigprocmask(SIG_SETMASK, &sset, NULL); |
| 569 |
|
| 570 |
clock_gettime(CLOCK_MONOTONIC_RAW, &rts->start_time); |
| 571 |
|
| 572 |
if (rts->deadline) { |
| 573 |
struct itimerval it; |
| 574 |
|
| 575 |
it.it_interval.tv_sec = 0; |
| 576 |
it.it_interval.tv_usec = 0; |
| 577 |
it.it_value.tv_sec = rts->deadline; |
| 578 |
it.it_value.tv_usec = 0; |
| 579 |
setitimer(ITIMER_REAL, &it, NULL); |
| 580 |
} |
| 581 |
|
| 582 |
if (isatty(STDOUT_FILENO)) { |
| 583 |
struct winsize w; |
| 584 |
|
| 585 |
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { |
| 586 |
if (w.ws_col > 0) |
| 587 |
rts->screen_width = w.ws_col; |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
int main_loop(struct ping_rts *rts, ping_func_set_st *fset, socket_st *sock, |
| 593 |
uint8_t *packet, int packlen) |
| 594 |
{ |
| 595 |
char addrbuf[128]; |
| 596 |
char ans_data[4096]; |
| 597 |
struct iovec iov; |
| 598 |
struct msghdr msg; |
| 599 |
int cc; |
| 600 |
int next; |
| 601 |
int polling; |
| 602 |
int recv_error; |
| 603 |
|
| 604 |
iov.iov_base = (char *)packet; |
| 605 |
|
| 606 |
for (;;) { |
| 607 |
/* Check exit conditions. */ |
| 608 |
if (rts->exiting) |
| 609 |
break; |
| 610 |
if (rts->npackets && rts->nreceived + rts->nerrors >= rts->npackets) |
| 611 |
break; |
| 612 |
if (rts->deadline && rts->nerrors) |
| 613 |
break; |
| 614 |
/* Check for and do special actions. */ |
| 615 |
if (rts->status_snapshot) |
| 616 |
status(rts); |
| 617 |
|
| 618 |
/* Send probes scheduled to this time. */ |
| 619 |
do { |
| 620 |
next = pinger(rts, fset, sock); |
| 621 |
next = schedule_exit(rts, next); |
| 622 |
} while (next <= 0); |
| 623 |
|
| 624 |
/* "next" is time to send next probe, if positive. |
| 625 |
* If next<=0 send now or as soon as possible. */ |
| 626 |
|
| 627 |
/* Technical part. Looks wicked. Could be dropped, |
| 628 |
* if everyone used the newest kernel. :-) |
| 629 |
* Its purpose is: |
| 630 |
* 1. Provide intervals less than resolution of scheduler. |
| 631 |
* Solution: spinning. |
| 632 |
* 2. Avoid use of poll(), when recvmsg() can provide |
| 633 |
* timed waiting (SO_RCVTIMEO). */ |
| 634 |
polling = 0; |
| 635 |
recv_error = 0; |
| 636 |
if (rts->opt_adaptive || rts->opt_flood_poll || next < SCHINT(rts->interval)) { |
| 637 |
int recv_expected = in_flight(rts); |
| 638 |
|
| 639 |
/* If we are here, recvmsg() is unable to wait for |
| 640 |
* required timeout. */ |
| 641 |
if (1000 % HZ == 0 ? next <= 1000 / HZ : (next < INT_MAX / HZ && next * HZ <= 1000)) { |
| 642 |
/* Very short timeout... So, if we wait for |
| 643 |
* something, we sleep for MIN_INTERVAL_MS. |
| 644 |
* Otherwise, spin! */ |
| 645 |
if (recv_expected) { |
| 646 |
next = MIN_INTERVAL_MS; |
| 647 |
} else { |
| 648 |
next = 0; |
| 649 |
/* When spinning, no reasons to poll. |
| 650 |
* Use nonblocking recvmsg() instead. */ |
| 651 |
polling = MSG_DONTWAIT; |
| 652 |
/* But yield yet. */ |
| 653 |
sched_yield(); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
if (!polling && |
| 658 |
(rts->opt_adaptive || rts->opt_flood_poll || rts->interval)) { |
| 659 |
struct pollfd pset; |
| 660 |
pset.fd = sock->fd; |
| 661 |
pset.events = POLLIN; |
| 662 |
pset.revents = 0; |
| 663 |
if (poll(&pset, 1, next) < 1 || |
| 664 |
!(pset.revents & (POLLIN | POLLERR))) |
| 665 |
continue; |
| 666 |
polling = MSG_DONTWAIT; |
| 667 |
recv_error = pset.revents & POLLERR; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
for (;;) { |
| 672 |
struct timeval *recv_timep = NULL; |
| 673 |
struct timeval recv_time; |
| 674 |
int not_ours = 0; /* Raw socket can receive messages |
| 675 |
* destined to other running pings. */ |
| 676 |
|
| 677 |
iov.iov_len = packlen; |
| 678 |
memset(&msg, 0, sizeof(msg)); |
| 679 |
msg.msg_name = addrbuf; |
| 680 |
msg.msg_namelen = sizeof(addrbuf); |
| 681 |
msg.msg_iov = &iov; |
| 682 |
msg.msg_iovlen = 1; |
| 683 |
msg.msg_control = ans_data; |
| 684 |
msg.msg_controllen = sizeof(ans_data); |
| 685 |
|
| 686 |
cc = recvmsg(sock->fd, &msg, polling); |
| 687 |
polling = MSG_DONTWAIT; |
| 688 |
|
| 689 |
if (cc < 0) { |
| 690 |
/* If there was a POLLERR and there is no packet |
| 691 |
* on the socket, try to read the error queue. |
| 692 |
* Otherwise, give up. |
| 693 |
*/ |
| 694 |
if ((errno == EAGAIN && !recv_error) || |
| 695 |
errno == EINTR) |
| 696 |
break; |
| 697 |
recv_error = 0; |
| 698 |
if (!fset->receive_error_msg(rts, sock)) { |
| 699 |
if (errno) { |
| 700 |
error(0, errno, "recvmsg"); |
| 701 |
break; |
| 702 |
} |
| 703 |
not_ours = 1; |
| 704 |
} |
| 705 |
} else { |
| 706 |
|
| 707 |
#ifdef SO_TIMESTAMP |
| 708 |
struct cmsghdr *c; |
| 709 |
|
| 710 |
for (c = CMSG_FIRSTHDR(&msg); c; c = CMSG_NXTHDR(&msg, c)) { |
| 711 |
if (c->cmsg_level != SOL_SOCKET || |
| 712 |
c->cmsg_type != SO_TIMESTAMP) |
| 713 |
continue; |
| 714 |
if (c->cmsg_len < CMSG_LEN(sizeof(struct timeval))) |
| 715 |
continue; |
| 716 |
recv_timep = (struct timeval *)CMSG_DATA(c); |
| 717 |
} |
| 718 |
#endif |
| 719 |
|
| 720 |
if (rts->opt_latency || recv_timep == NULL) { |
| 721 |
if (rts->opt_latency || |
| 722 |
ioctl(sock->fd, SIOCGSTAMP, &recv_time)) |
| 723 |
gettimeofday(&recv_time, NULL); |
| 724 |
recv_timep = &recv_time; |
| 725 |
} |
| 726 |
|
| 727 |
not_ours = fset->parse_reply(rts, sock, &msg, cc, addrbuf, recv_timep); |
| 728 |
} |
| 729 |
|
| 730 |
/* See? ... someone runs another ping on this host. */ |
| 731 |
if (not_ours && sock->socktype == SOCK_RAW) |
| 732 |
fset->install_filter(rts, sock); |
| 733 |
|
| 734 |
/* If nothing is in flight, "break" returns us to pinger. */ |
| 735 |
if (in_flight(rts) == 0) |
| 736 |
break; |
| 737 |
|
| 738 |
/* Otherwise, try to recvmsg() again. recvmsg() |
| 739 |
* is nonblocking after the first iteration, so that |
| 740 |
* if nothing is queued, it will receive EAGAIN |
| 741 |
* and return to pinger. */ |
| 742 |
} |
| 743 |
} |
| 744 |
return finish(rts); |
| 745 |
} |
| 746 |
|
| 747 |
int gather_statistics(struct ping_rts *rts, uint8_t *icmph, int icmplen, |
| 748 |
int cc, uint16_t seq, int hops, |
| 749 |
int csfailed, struct timeval *tv, char *from, |
| 750 |
void (*pr_reply)(uint8_t *icmph, int cc), int multicast, |
| 751 |
int wrong_source) |
| 752 |
{ |
| 753 |
int dupflag = 0; |
| 754 |
long triptime = 0; |
| 755 |
uint8_t *ptr = icmph + icmplen; |
| 756 |
|
| 757 |
++rts->nreceived; |
| 758 |
if (!csfailed) |
| 759 |
acknowledge(rts, seq); |
| 760 |
|
| 761 |
if (rts->timing && cc >= (int)(8 + sizeof(struct timeval))) { |
| 762 |
struct timeval tmp_tv; |
| 763 |
memcpy(&tmp_tv, ptr, sizeof(tmp_tv)); |
| 764 |
|
| 765 |
restamp: |
| 766 |
tvsub(tv, &tmp_tv); |
| 767 |
|
| 768 |
if (tv->tv_usec >= 1000000) { |
| 769 |
error(0, 0, _("Warning: invalid tv_usec %ld us"), tv->tv_usec); |
| 770 |
tv->tv_usec = 999999; |
| 771 |
} |
| 772 |
|
| 773 |
if (tv->tv_usec < 0) { |
| 774 |
error(0, 0, _("Warning: invalid tv_usec %ld us"), tv->tv_usec); |
| 775 |
tv->tv_usec = 0; |
| 776 |
} |
| 777 |
|
| 778 |
if (tv->tv_sec > TV_SEC_MAX_VAL) { |
| 779 |
error(0, 0, _("Warning: invalid tv_sec %ld s"), tv->tv_sec); |
| 780 |
triptime = 0; |
| 781 |
} else if (tv->tv_sec < 0) { |
| 782 |
error(0, 0, _("Warning: time of day goes back (%ld s), taking countermeasures"), tv->tv_sec); |
| 783 |
triptime = 0; |
| 784 |
if (!rts->opt_latency) { |
| 785 |
gettimeofday(tv, NULL); |
| 786 |
rts->opt_latency = 1; |
| 787 |
goto restamp; |
| 788 |
} |
| 789 |
} else { |
| 790 |
triptime = tv->tv_sec * 1000000 + tv->tv_usec; |
| 791 |
} |
| 792 |
|
| 793 |
if (!csfailed) { |
| 794 |
rts->tsum += triptime; |
| 795 |
rts->tsum2 += (double)((long long)triptime * (long long)triptime); |
| 796 |
if (triptime < rts->tmin) |
| 797 |
rts->tmin = triptime; |
| 798 |
if (triptime > rts->tmax) |
| 799 |
rts->tmax = triptime; |
| 800 |
if (!rts->rtt) |
| 801 |
rts->rtt = ((uint64_t)triptime) * 8; |
| 802 |
else |
| 803 |
rts->rtt += triptime - rts->rtt / 8; |
| 804 |
if (rts->opt_adaptive) |
| 805 |
update_interval(rts); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
if (csfailed) { |
| 810 |
++rts->nchecksum; |
| 811 |
--rts->nreceived; |
| 812 |
} else if (rcvd_test(rts, seq)) { |
| 813 |
++rts->nrepeats; |
| 814 |
--rts->nreceived; |
| 815 |
dupflag = 1; |
| 816 |
} else { |
| 817 |
rcvd_set(rts, seq); |
| 818 |
dupflag = 0; |
| 819 |
} |
| 820 |
rts->confirm = rts->confirm_flag; |
| 821 |
|
| 822 |
if (rts->opt_quiet) |
| 823 |
return 1; |
| 824 |
|
| 825 |
if (rts->opt_flood) { |
| 826 |
if (!csfailed) |
| 827 |
write_stdout("\b \b", 3); |
| 828 |
else |
| 829 |
write_stdout("\bC", 2); |
| 830 |
} else { |
| 831 |
int i; |
| 832 |
uint8_t *cp, *dp; |
| 833 |
|
| 834 |
print_timestamp(rts); |
| 835 |
printf(_("%d bytes from %s:"), cc, from); |
| 836 |
|
| 837 |
if (pr_reply) |
| 838 |
pr_reply(icmph, cc); |
| 839 |
|
| 840 |
if (rts->opt_verbose && rts->ident != -1) |
| 841 |
printf(_(" ident=%d"), ntohs(rts->ident)); |
| 842 |
|
| 843 |
if (hops >= 0) |
| 844 |
printf(_(" ttl=%d"), hops); |
| 845 |
|
| 846 |
if (cc < rts->datalen + 8) { |
| 847 |
printf(_(" (truncated)\n")); |
| 848 |
return 1; |
| 849 |
} |
| 850 |
if (rts->timing) { |
| 851 |
char *fmt; |
| 852 |
char fmt2[30]; |
| 853 |
long int num, dec = 0; |
| 854 |
|
| 855 |
if (rts->opt_rtt_precision) { |
| 856 |
fmt = "%ld.%03ld"; |
| 857 |
num = triptime / 1000; |
| 858 |
dec = triptime % 1000; |
| 859 |
} else if (triptime >= 100000 - 50) { |
| 860 |
fmt = "%ld"; |
| 861 |
num = (triptime + 500) / 1000; |
| 862 |
} else if (triptime >= 10000 - 5) { |
| 863 |
fmt = "%ld.%01ld"; |
| 864 |
num = (triptime + 50) / 1000; |
| 865 |
dec = ((triptime + 50) % 1000) / 100; |
| 866 |
} else if (triptime >= 1000) { |
| 867 |
fmt = "%ld.%02ld"; |
| 868 |
num = (triptime + 5) / 1000; |
| 869 |
dec = ((triptime + 5) % 1000) / 10; |
| 870 |
} else { |
| 871 |
fmt = "%ld.%03ld"; |
| 872 |
num = triptime / 1000; |
| 873 |
dec = triptime % 1000; |
| 874 |
} |
| 875 |
snprintf(fmt2, sizeof(fmt2), _(" time=%s ms"), fmt); |
| 876 |
printf(fmt2, num, dec); |
| 877 |
} |
| 878 |
|
| 879 |
if (dupflag && (!multicast || rts->opt_verbose)) |
| 880 |
printf(_(" (DUP!)")); |
| 881 |
if (csfailed) |
| 882 |
printf(_(" (BAD CHECKSUM!)")); |
| 883 |
if (wrong_source) |
| 884 |
printf(_(" (DIFFERENT ADDRESS!)")); |
| 885 |
|
| 886 |
/* check the data */ |
| 887 |
cp = ((unsigned char *)ptr) + sizeof(struct timeval); |
| 888 |
dp = &rts->outpack[8 + sizeof(struct timeval)]; |
| 889 |
for (i = sizeof(struct timeval); i < rts->datalen; ++i, ++cp, ++dp) { |
| 890 |
if (*cp != *dp) { |
| 891 |
printf(_("\nwrong data byte #%d should be 0x%x but was 0x%x"), |
| 892 |
i, *dp, *cp); |
| 893 |
cp = (unsigned char *)ptr + sizeof(struct timeval); |
| 894 |
for (i = sizeof(struct timeval); i < rts->datalen; ++i, ++cp) { |
| 895 |
if ((i % 32) == sizeof(struct timeval)) |
| 896 |
printf("\n#%d\t", i); |
| 897 |
printf("%x ", *cp); |
| 898 |
} |
| 899 |
break; |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
static long llsqrt(long long a) |
| 907 |
{ |
| 908 |
long long prev = LLONG_MAX; |
| 909 |
long long x = a; |
| 910 |
|
| 911 |
if (x > 0) { |
| 912 |
while (x < prev) { |
| 913 |
prev = x; |
| 914 |
x = (x + (a / x)) / 2; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
return (long)x; |
| 919 |
} |
| 920 |
|
| 921 |
/* |
| 922 |
* finish -- |
| 923 |
* Print out statistics, and give up. |
| 924 |
*/ |
| 925 |
int finish(struct ping_rts *rts) |
| 926 |
{ |
| 927 |
struct timespec tv = rts->cur_time; |
| 928 |
char *comma = ""; |
| 929 |
|
| 930 |
tssub(&tv, &rts->start_time); |
| 931 |
|
| 932 |
putchar('\n'); |
| 933 |
fflush(stdout); |
| 934 |
printf(_("--- %s ping statistics ---\n"), rts->hostname); |
| 935 |
printf(_("%ld packets transmitted, "), rts->ntransmitted); |
| 936 |
printf(_("%ld received"), rts->nreceived); |
| 937 |
if (rts->nrepeats) |
| 938 |
printf(_(", +%ld duplicates"), rts->nrepeats); |
| 939 |
if (rts->nchecksum) |
| 940 |
printf(_(", +%ld corrupted"), rts->nchecksum); |
| 941 |
if (rts->nerrors) |
| 942 |
printf(_(", +%ld errors"), rts->nerrors); |
| 943 |
|
| 944 |
if (rts->ntransmitted) { |
| 945 |
#ifdef USE_IDN |
| 946 |
setlocale(LC_ALL, "C"); |
| 947 |
#endif |
| 948 |
printf(_(", %g%% packet loss"), |
| 949 |
(float)((((long long)(rts->ntransmitted - rts->nreceived)) * 100.0) / rts->ntransmitted)); |
| 950 |
printf(_(", time %llums"), (unsigned long long)(1000 * tv.tv_sec + (tv.tv_nsec + 500000) / 1000000)); |
| 951 |
} |
| 952 |
|
| 953 |
putchar('\n'); |
| 954 |
|
| 955 |
if (rts->nreceived && rts->timing) { |
| 956 |
double tmdev; |
| 957 |
long total = rts->nreceived + rts->nrepeats; |
| 958 |
long tmavg = rts->tsum / total; |
| 959 |
long long tmvar; |
| 960 |
|
| 961 |
if (rts->tsum < INT_MAX) |
| 962 |
/* This slightly clumsy computation order is important to avoid |
| 963 |
* integer rounding errors for small ping times. */ |
| 964 |
tmvar = (rts->tsum2 - ((rts->tsum * rts->tsum) / total)) / total; |
| 965 |
else |
| 966 |
tmvar = (rts->tsum2 / total) - (tmavg * tmavg); |
| 967 |
|
| 968 |
tmdev = llsqrt(tmvar); |
| 969 |
|
| 970 |
printf(_("rtt min/avg/max/mdev = %ld.%03ld/%lu.%03ld/%ld.%03ld/%ld.%03ld ms"), |
| 971 |
(long)rts->tmin / 1000, (long)rts->tmin % 1000, |
| 972 |
(unsigned long)(tmavg / 1000), (long)(tmavg % 1000), |
| 973 |
(long)rts->tmax / 1000, (long)rts->tmax % 1000, |
| 974 |
(long)tmdev / 1000, (long)tmdev % 1000); |
| 975 |
comma = ", "; |
| 976 |
} |
| 977 |
if (rts->pipesize > 1) { |
| 978 |
printf(_("%spipe %d"), comma, rts->pipesize); |
| 979 |
comma = ", "; |
| 980 |
} |
| 981 |
|
| 982 |
if (rts->nreceived && (!rts->interval || rts->opt_flood || rts->opt_adaptive) && rts->ntransmitted > 1) { |
| 983 |
int ipg = (1000000 * (long long)tv.tv_sec + tv.tv_nsec / 1000) / (rts->ntransmitted - 1); |
| 984 |
|
| 985 |
printf(_("%sipg/ewma %d.%03d/%d.%03d ms"), |
| 986 |
comma, ipg / 1000, ipg % 1000, (int)(rts->rtt / 8000), (int)((rts->rtt / 8) % 1000)); |
| 987 |
} |
| 988 |
putchar('\n'); |
| 989 |
return (!rts->nreceived || (rts->deadline && rts->nreceived < rts->npackets)); |
| 990 |
} |
| 991 |
|
| 992 |
void status(struct ping_rts *rts) |
| 993 |
{ |
| 994 |
int loss = 0; |
| 995 |
long tavg = 0; |
| 996 |
|
| 997 |
rts->status_snapshot = 0; |
| 998 |
|
| 999 |
if (rts->ntransmitted) |
| 1000 |
loss = (((long long)(rts->ntransmitted - rts->nreceived)) * 100) / rts->ntransmitted; |
| 1001 |
|
| 1002 |
fprintf(stderr, "\r"); |
| 1003 |
fprintf(stderr, _("%ld/%ld packets, %d%% loss"), rts->nreceived, rts->ntransmitted, loss); |
| 1004 |
|
| 1005 |
if (rts->nreceived && rts->timing) { |
| 1006 |
tavg = rts->tsum / (rts->nreceived + rts->nrepeats); |
| 1007 |
|
| 1008 |
fprintf(stderr, _(", min/avg/ewma/max = %ld.%03ld/%lu.%03ld/%d.%03d/%ld.%03ld ms"), |
| 1009 |
(long)rts->tmin / 1000, (long)rts->tmin % 1000, |
| 1010 |
tavg / 1000, tavg % 1000, |
| 1011 |
(int)(rts->rtt / 8000), (int)((rts->rtt / 8) % 1000), (long)rts->tmax / 1000, (long)rts->tmax % 1000); |
| 1012 |
} |
| 1013 |
fprintf(stderr, "\n"); |
| 1014 |
} |
| 1015 |
|
| 1016 |
inline int is_ours(struct ping_rts *rts, socket_st * sock, uint16_t id) |
| 1017 |
{ |
| 1018 |
return sock->socktype == SOCK_DGRAM || id == rts->ident; |
| 1019 |
} |
| 1020 |
|
| 1021 |
char *str_interval(int interval) |
| 1022 |
{ |
| 1023 |
static char buf[14]; |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* Avoid messing with locales and floating point due the different decimal |
| 1027 |
* point depending on locales. |
| 1028 |
*/ |
| 1029 |
if (interval % 1000) |
| 1030 |
snprintf(buf, sizeof(buf), "%1i.%03i", interval/1000, interval%1000); |
| 1031 |
else |
| 1032 |
snprintf(buf, sizeof(buf), "%i", interval/1000); |
| 1033 |
|
| 1034 |
return buf; |
| 1035 |
} |