Skip to content

Commit b518465

Browse files
committed
PTP: Refine timestamp printing.
In ptp_print_timestamp_identity() and ptp_print_announce_msg() use ptp_print_timestamp() to deduplicate code. In ptp_print_timestamp() change the format to SECONDS.NANOSECONDS and indicate an invalid nanoseconds value. Update the tests. This resolves GH issue #1260.
1 parent a126cbf commit b518465

File tree

6 files changed

+264
-281
lines changed

6 files changed

+264
-281
lines changed

Diff for: CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
3838
the subrule is present (inspird by the OpenBSD tcpdump).
3939
pflog: Handle OpenBSD's "rewritten" flag and addresses (adapted
4040
from the OpenBSD tcpdump).
41+
PTP: Refine timestamp printing.
4142
SLL2: Translate interface indices to names on Linux only.
4243
RT6: Add a bounds check for the PadN TLV in Segment Routing Header.
4344
TCP: Add support for the AE (AccECN) flag.

Diff for: print-ptp.c

+12-30
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "netdissect-stdinc.h"
2323
#include "netdissect.h"
2424
#include "extract.h"
25+
#include "timeval-operations.h"
2526

2627
/*
2728
* PTP header
@@ -227,6 +228,7 @@ static const struct tok ptp_control_field[] = {
227228
#define PTP_FALSE !PTP_TRUE
228229

229230
#define PTP_HDR_LEN 0x22
231+
#define PTP_TIMESTAMP_LEN 10U
230232

231233
/* mask based on the first byte */
232234
#define PTP_MAJOR_VERS_MASK 0x0F
@@ -495,37 +497,28 @@ ptp_print_timestamp(netdissect_options *ndo, const u_char *bp, u_int *len, const
495497
ND_PRINT(", %s:", stype);
496498
/* sec time stamp 6 bytes */
497499
secs = GET_BE_U_6(bp);
498-
ND_PRINT(" %"PRIu64" seconds,", secs);
500+
ND_PRINT(" %"PRIu64".", secs);
499501
*len -= 6;
500502
bp += 6;
501503

502504
/* NS time stamp 4 bytes */
503505
nsecs = GET_BE_U_4(bp);
504-
ND_PRINT(" %u nanoseconds", nsecs);
506+
ND_PRINT("%09u seconds", nsecs);
507+
if (nsecs > ND_NANO_PER_SEC - 1)
508+
ND_PRINT(" " ND_INVALID_NANO_SEC_STR);
505509
*len -= 4;
506510
bp += 4;
507511
}
508512
static void
509513
ptp_print_timestamp_identity(netdissect_options *ndo,
510514
const u_char *bp, u_int *len, const char *ttype)
511515
{
512-
uint64_t secs;
513-
uint32_t nsecs;
514516
uint16_t port_id;
515517
uint64_t port_identity;
516518

517-
ND_PRINT(", %s:", ttype);
518-
/* sec time stamp 6 bytes */
519-
secs = GET_BE_U_6(bp);
520-
ND_PRINT(" %"PRIu64" seconds,", secs);
521-
*len -= 6;
522-
bp += 6;
523-
524-
/* NS time stamp 4 bytes */
525-
nsecs = GET_BE_U_4(bp);
526-
ND_PRINT(" %u nanoseconds", nsecs);
527-
*len -= 4;
528-
bp += 4;
519+
ptp_print_timestamp(ndo, bp, len, ttype);
520+
*len -= PTP_TIMESTAMP_LEN;
521+
bp += PTP_TIMESTAMP_LEN;
529522

530523
/* port identity*/
531524
port_identity = GET_BE_U_8(bp);
@@ -545,21 +538,10 @@ ptp_print_announce_msg(netdissect_options *ndo, const u_char *bp, u_int *len)
545538
uint8_t rsvd, gm_prio_1, gm_prio_2, gm_clk_cls, gm_clk_acc, time_src;
546539
uint16_t origin_cur_utc, gm_clk_var, steps_removed;
547540
uint64_t gm_clock_id;
548-
uint64_t secs;
549-
uint32_t nsecs;
550541

551-
ND_PRINT(", %s:", p_origin_ts);
552-
/* sec time stamp 6 bytes */
553-
secs = GET_BE_U_6(bp);
554-
ND_PRINT(" %"PRIu64" seconds", secs);
555-
*len -= 6;
556-
bp += 6;
557-
558-
/* NS time stamp 4 bytes */
559-
nsecs = GET_BE_U_4(bp);
560-
ND_PRINT(" %u nanoseconds", nsecs);
561-
*len -= 4;
562-
bp += 4;
542+
ptp_print_timestamp(ndo, bp, len, p_origin_ts);
543+
*len -= PTP_TIMESTAMP_LEN;
544+
bp += PTP_TIMESTAMP_LEN;
563545

564546
/* origin cur utc */
565547
origin_cur_utc = GET_BE_U_2(bp);

Diff for: tests/ptp.out

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
1 2018-01-23 19:44:09.248292 IP 11.0.0.110.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x7cfe90fffef950b4, port id: 1, seq id: 132, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0 seconds, 0 nanoseconds
2-
2 2018-01-23 19:44:09.248437 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 132, control: 3 (Delay_Resp), log message interval: 0, receiveTimeStamp: 1516736649 seconds, 248292005 nanoseconds, port identity: 0x7cfe90fffef950b4, port id: 1
3-
3 2018-01-23 19:44:09.982883 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: announce msg, length: 64, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 534, control: 5 (Other), log message interval: 1, originTimeStamp: 0 seconds 0 nanoseconds, origin cur utc:36, rsvd: 0, gm priority_1: 128, gm clock class: 248, gm clock accuracy: 254, gm clock variance: 65535, gm priority_2: 128, gm clock id: 0x200fffe000001, steps removed: 0, time source: 0xa0
4-
4 2018-01-23 19:44:10.034745 IP 11.0.0.9.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 0, reserved1: 0, Flags [two step], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 0 (Sync), log message interval: 0, originTimeStamp: 0 seconds, 0 nanoseconds
5-
5 2018-01-23 19:44:10.034796 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: follow up msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 2 (Follow_Up), log message interval: 0, preciseOriginTimeStamp: 1516736650 seconds, 34751783 nanoseconds
1+
1 2018-01-23 19:44:09.248292 IP 11.0.0.110.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x7cfe90fffef950b4, port id: 1, seq id: 132, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0.000000000 seconds
2+
2 2018-01-23 19:44:09.248437 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 132, control: 3 (Delay_Resp), log message interval: 0, receiveTimeStamp: 1516736649.248292005 seconds, port identity: 0x7cfe90fffef950b4, port id: 1
3+
3 2018-01-23 19:44:09.982883 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: announce msg, length: 64, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 534, control: 5 (Other), log message interval: 1, originTimeStamp: 0.000000000 seconds, origin cur utc:36, rsvd: 0, gm priority_1: 128, gm clock class: 248, gm clock accuracy: 254, gm clock variance: 65535, gm priority_2: 128, gm clock id: 0x200fffe000001, steps removed: 0, time source: 0xa0
4+
4 2018-01-23 19:44:10.034745 IP 11.0.0.9.319 > 224.0.1.129.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 0, reserved1: 0, Flags [two step], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 0 (Sync), log message interval: 0, originTimeStamp: 0.000000000 seconds
5+
5 2018-01-23 19:44:10.034796 IP 11.0.0.9.320 > 224.0.1.129.320: PTPv2, majorSdoId: 0x0, msg type: follow up msg, length: 44, domain: 0, reserved1: 0, Flags [none], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0x200fffe000001, port id: 1, seq id: 1067, control: 2 (Follow_Up), log message interval: 0, preciseOriginTimeStamp: 1516736650.034751783 seconds

Diff for: tests/ptp_corrections.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
1 2022-10-11 17:52:26.679146 IP 4.5.0.2.319 > 2.2.2.2.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0xa0369ffffe856e8a, port id: 1, seq id: 1203, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0 seconds, 0 nanoseconds
2-
2 2022-10-11 17:52:26.679265 IP 2.2.2.2.320 > 4.5.0.2.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 44, reserved1: 0, Flags [unicast], NS correction: 36035, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1203, control: 3 (Delay_Resp), log message interval: 127, receiveTimeStamp: 1665510783 seconds, 679015501 nanoseconds, port identity: 0xa0369ffffe856e8a, port id: 1
3-
3 2022-10-11 17:52:26.682034 IP 2.2.2.2.319 > 4.5.0.2.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 105045, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1213, control: 0 (Sync), log message interval: 127, originTimeStamp: 1665510783 seconds, 681548698 nanoseconds
1+
1 2022-10-11 17:52:26.679146 IP 4.5.0.2.319 > 2.2.2.2.319: PTPv2, majorSdoId: 0x0, msg type: delay req msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 0, sub NS correction: 0, reserved2: 0, clock identity: 0xa0369ffffe856e8a, port id: 1, seq id: 1203, control: 1 (Delay_Req), log message interval: 127, originTimeStamp: 0.000000000 seconds
2+
2 2022-10-11 17:52:26.679265 IP 2.2.2.2.320 > 4.5.0.2.320: PTPv2, majorSdoId: 0x0, msg type: delay resp msg, length: 54, domain: 44, reserved1: 0, Flags [unicast], NS correction: 36035, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1203, control: 3 (Delay_Resp), log message interval: 127, receiveTimeStamp: 1665510783.679015501 seconds, port identity: 0xa0369ffffe856e8a, port id: 1
3+
3 2022-10-11 17:52:26.682034 IP 2.2.2.2.319 > 4.5.0.2.319: PTPv2, majorSdoId: 0x0, msg type: sync msg, length: 44, domain: 44, reserved1: 0, Flags [unicast], NS correction: 105045, sub NS correction: 0, reserved2: 0, clock identity: 0xe8c57affff01313f, port id: 3, seq id: 1213, control: 0 (Sync), log message interval: 127, originTimeStamp: 1665510783.681548698 seconds

0 commit comments

Comments
 (0)