From 52cb0728241d3024374b45efdd26990e070e7b22 Mon Sep 17 00:00:00 2001 From: David Gwynne Date: Wed, 11 Mar 2015 03:38:57 +0000 Subject: port src/sbin/ping/ping.c r1.115. > use clock_gettime(CLOCK_MONOTONIC) to get timestamps to measure the > interval between sending a ping and getting a reply for it. > > this makes it resistant against local wall clock changes, which can > skew the intervals reported or make them go negative. requested by deraadt@ florian@ --- sbin/ping6/ping6.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'sbin/ping6/ping6.c') diff --git a/sbin/ping6/ping6.c b/sbin/ping6/ping6.c index 812a10a489e..4b9fe1f07b3 100644 --- a/sbin/ping6/ping6.c +++ b/sbin/ping6/ping6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping6.c,v 1.101 2015/01/16 06:40:00 deraadt Exp $ */ +/* $OpenBSD: ping6.c,v 1.102 2015/03/11 03:38:56 dlg Exp $ */ /* $KAME: ping6.c,v 1.163 2002/10/25 02:19:06 itojun Exp $ */ /* @@ -1072,10 +1072,13 @@ pinger(void) icp->icmp6_id = htons(ident); icp->icmp6_seq = ntohs(seq); if (timing) { + struct timespec ts; struct timeval tv; struct tv32 tv32; - (void)gettimeofday(&tv, NULL); + if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) + err(1, "clock_gettime(CLOCK_MONOTONIC)"); + TIMESPEC_TO_TIMEVAL(&tv, &ts); tv32.tv32_sec = htonl(tv.tv_sec); /* XXX 2038 */ tv32.tv32_usec = htonl(tv.tv_usec); bcopy(&tv32, &outpack[ICMP6ECHOLEN], sizeof(tv32)); @@ -1206,6 +1209,7 @@ pr_pack(u_char *buf, int cc, struct msghdr *mhdr) int fromlen; u_char *cp = NULL, *dp, *end = buf + cc; struct in6_pktinfo *pktinfo = NULL; + struct timespec ts; struct timeval tv, tp; struct tv32 tv32; double triptime = 0; @@ -1215,7 +1219,9 @@ pr_pack(u_char *buf, int cc, struct msghdr *mhdr) u_int16_t seq; char dnsname[MAXDNAME + 1]; - (void)gettimeofday(&tv, NULL); + if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) + err(1, "clock_gettime(CLOCK_MONOTONIC)"); + TIMESPEC_TO_TIMEVAL(&tv, &ts); if (!mhdr || !mhdr->msg_name || mhdr->msg_namelen != sizeof(struct sockaddr_in6) || -- cgit v1.2.3