Skip to content

Commit 511aa37

Browse files
committed
complete SO_TIMESTAMPNS to SO_TIMESTAMP fallback
Commit e866063 added a fallback from setting the socket option SO_TIMESTAMPNS to setting the socket option SO_TIMESTAMP if the nanosecond timestamp option could not be set. But it did not add code to also look for the control message related to SO_TIMESTAMP. Thus microsecond timestamps were requested, but not read. This commit adds the missing code to read microsecond timestamp control messages. The problem was reported in GitHub issue #374 by @payload.
1 parent cb83286 commit 511aa37

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Next
2+
====
3+
4+
## Bugfixes and other changes
5+
6+
- Fix fallback to SO\_TIMESTAMP if SO\_TIMESTAMPNS is not available (#375, thanks
7+
@auerswal)
8+
19
fping 5.3 (2025-01-02)
210
======================
311

src/fping.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,14 @@ static inline int64_t timespec_ns(struct timespec *a)
12841284
return ((int64_t)a->tv_sec * 1000000000) + a->tv_nsec;
12851285
}
12861286

1287+
#if HAVE_SO_TIMESTAMPNS
1288+
/* convert a struct timeval to nanoseconds */
1289+
static inline int64_t timeval_ns(struct timeval *a)
1290+
{
1291+
return ((int64_t)a->tv_sec * 1000000000) + ((int64_t)a->tv_usec * 1000);
1292+
}
1293+
#endif /* HAVE_SO_TIMESTAMPNS */
1294+
12871295
void add_cidr(char *addr)
12881296
{
12891297
char *addr_end;
@@ -2105,13 +2113,18 @@ int receive_packet(int64_t wait_time,
21052113
/* ancilliary data */
21062114
{
21072115
struct timespec reply_timestamp_ts;
2116+
struct timeval reply_timestamp_tv;
21082117
for (cmsg = CMSG_FIRSTHDR(&recv_msghdr);
21092118
cmsg != NULL;
21102119
cmsg = CMSG_NXTHDR(&recv_msghdr, cmsg)) {
21112120
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPNS) {
21122121
memcpy(&reply_timestamp_ts, CMSG_DATA(cmsg), sizeof(reply_timestamp_ts));
21132122
*reply_timestamp = timespec_ns(&reply_timestamp_ts);
21142123
}
2124+
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMP) {
2125+
memcpy(&reply_timestamp_tv, CMSG_DATA(cmsg), sizeof(reply_timestamp_tv));
2126+
*reply_timestamp = timeval_ns(&reply_timestamp_tv);
2127+
}
21152128
}
21162129
}
21172130
#endif

0 commit comments

Comments
 (0)