| 1 |
/* |
| 2 |
Copyright (C) 2001-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 <config.h> |
| 20 |
|
| 21 |
#include <sys/param.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <sys/file.h> |
| 24 |
#include <sys/time.h> |
| 25 |
|
| 26 |
#include <netinet/in.h> |
| 27 |
#include <arpa/inet.h> |
| 28 |
|
| 29 |
/*#include <netinet/ip_icmp.h> -- deliberately not including this */ |
| 30 |
#ifdef HAVE_NETINET_IP_VAR_H |
| 31 |
# include <netinet/ip_var.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <netdb.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <ctype.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <attribute.h> |
| 42 |
|
| 43 |
#include <ping.h> |
| 44 |
#include <ping_impl.h> |
| 45 |
|
| 46 |
static int recv_timestamp (int code, void *closure, |
| 47 |
struct sockaddr_in *dest, struct sockaddr_in *from, |
| 48 |
struct ip *ip, icmphdr_t * icmp, int datalen); |
| 49 |
static void print_timestamp (int dupflag, void *closure, |
| 50 |
struct sockaddr_in *dest, |
| 51 |
struct sockaddr_in *from, struct ip *ip, |
| 52 |
icmphdr_t * icmp, int datalen); |
| 53 |
static int timestamp_finish (void); |
| 54 |
|
| 55 |
int |
| 56 |
ping_timestamp (char *hostname) |
| 57 |
{ |
| 58 |
ping_set_type (ping, ICMP_TIMESTAMP); |
| 59 |
ping_set_event_handler (ping, recv_timestamp, NULL); |
| 60 |
ping_set_packetsize (ping, ICMP_TSLEN); |
| 61 |
|
| 62 |
if (ping_set_dest (ping, hostname)) |
| 63 |
error (EXIT_FAILURE, 0, "unknown host"); |
| 64 |
|
| 65 |
printf ("PING %s (%s): sending timestamp requests\n", |
| 66 |
ping->ping_hostname, |
| 67 |
inet_ntoa (ping->ping_dest.ping_sockaddr.sin_addr)); |
| 68 |
|
| 69 |
return ping_run (ping, timestamp_finish); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
int |
| 74 |
recv_timestamp (int code, void *closure, |
| 75 |
struct sockaddr_in *dest, struct sockaddr_in *from, |
| 76 |
struct ip *ip, icmphdr_t *icmp, int datalen) |
| 77 |
{ |
| 78 |
switch (code) |
| 79 |
{ |
| 80 |
case PEV_RESPONSE: |
| 81 |
case PEV_DUPLICATE: |
| 82 |
print_timestamp (code == PEV_DUPLICATE, |
| 83 |
closure, dest, from, ip, icmp, datalen); |
| 84 |
break; |
| 85 |
case PEV_NOECHO:; |
| 86 |
print_icmp_header (from, ip, icmp, datalen); |
| 87 |
} |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
void |
| 93 |
print_timestamp (int dupflag, void *closure MAYBE_UNUSED, |
| 94 |
struct sockaddr_in *dest MAYBE_UNUSED, |
| 95 |
struct sockaddr_in *from, |
| 96 |
struct ip *ip MAYBE_UNUSED, icmphdr_t *icmp, int datalen) |
| 97 |
{ |
| 98 |
char timestr[16]; |
| 99 |
|
| 100 |
printf ("%d bytes from %s: icmp_seq=%u", datalen, |
| 101 |
inet_ntoa (*(struct in_addr *) &from->sin_addr.s_addr), |
| 102 |
ntohs (icmp->icmp_seq)); |
| 103 |
if (dupflag) |
| 104 |
printf (" (DUP!)"); |
| 105 |
printf ("\n"); |
| 106 |
printf ("icmp_otime = %s\n", |
| 107 |
ping_cvt_time (timestr, sizeof (timestr), |
| 108 |
ntohl (icmp->icmp_otime))); |
| 109 |
printf ("icmp_rtime = %s\n", |
| 110 |
ping_cvt_time (timestr, sizeof (timestr), |
| 111 |
ntohl (icmp->icmp_rtime))); |
| 112 |
printf ("icmp_ttime = %s\n", |
| 113 |
ping_cvt_time (timestr, sizeof (timestr), |
| 114 |
ntohl (icmp->icmp_ttime))); |
| 115 |
|
| 116 |
if ((options & OPT_VERBOSE) |
| 117 |
&& is_normed_time (ntohl (icmp->icmp_otime)) |
| 118 |
&& is_normed_time (ntohl (icmp->icmp_otime))) |
| 119 |
printf ("difference = %d ms\n", |
| 120 |
ntohl (icmp->icmp_ttime) - ntohl (icmp->icmp_otime)); |
| 121 |
|
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
int |
| 126 |
timestamp_finish (void) |
| 127 |
{ |
| 128 |
return ping_finish (); |
| 129 |
} |