]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
Merge pull request #492 from vel21ripn/nflog-print
[tcpdump] / print-ntp.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * By Jeffrey Mogul/DECWRL
22 * loosely based on print-bootp.c
23 */
24
25 /* \summary: Network Time Protocol (NTP) printer */
26
27 /*
28 * specification:
29 *
30 * RFC 1119 - NTPv2
31 * RFC 1305 - NTPv3
32 * RFC 5905 - NTPv4
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "netdissect-stdinc.h"
40
41 #ifdef HAVE_STRFTIME
42 #include <time.h>
43 #endif
44
45 #include "netdissect.h"
46 #include "addrtoname.h"
47 #include "extract.h"
48
49
50 /*
51 * Based on ntp.h from the U of MD implementation
52 * This file is based on Version 2 of the NTP spec (RFC1119).
53 */
54
55 /*
56 * Definitions for the masses
57 */
58 #define JAN_1970 INT64_T_CONSTANT(2208988800) /* 1970 - 1900 in seconds */
59
60 /*
61 * Structure definitions for NTP fixed point values
62 *
63 * 0 1 2 3
64 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * | Integer Part |
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * | Fraction Part |
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 *
71 * 0 1 2 3
72 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * | Integer Part | Fraction Part |
75 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 */
77 struct l_fixedpt {
78 nd_uint32_t int_part;
79 nd_uint32_t fraction;
80 };
81
82 struct s_fixedpt {
83 nd_uint16_t int_part;
84 nd_uint16_t fraction;
85 };
86
87 /* rfc2030
88 * 1 2 3
89 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * |LI | VN |Mode | Stratum | Poll | Precision |
92 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * | Root Delay |
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * | Root Dispersion |
96 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 * | Reference Identifier |
98 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * | |
100 * | Reference Timestamp (64) |
101 * | |
102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 * | |
104 * | Originate Timestamp (64) |
105 * | |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | |
108 * | Receive Timestamp (64) |
109 * | |
110 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 * | |
112 * | Transmit Timestamp (64) |
113 * | |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 * | Key Identifier (optional) (32) |
116 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 * | |
118 * | |
119 * | Message Digest (optional) (128) |
120 * | |
121 * | |
122 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 */
124
125 /* Length of the NTP data message with the mandatory fields ("the header")
126 * and without any optional fields (extension, Key Identifier,
127 * Message Digest).
128 */
129 #define NTP_TIMEMSG_MINLEN 48U
130
131 struct ntp_time_data {
132 nd_uint8_t status; /* status of local clock and leap info */
133 nd_uint8_t stratum; /* Stratum level */
134 nd_int8_t ppoll; /* poll value */
135 nd_int8_t precision;
136 struct s_fixedpt root_delay;
137 struct s_fixedpt root_dispersion;
138 nd_uint32_t refid;
139 struct l_fixedpt ref_timestamp;
140 struct l_fixedpt org_timestamp;
141 struct l_fixedpt rec_timestamp;
142 struct l_fixedpt xmt_timestamp;
143 nd_uint32_t key_id;
144 nd_uint8_t message_digest[20];
145 };
146 /*
147 * Leap Second Codes (high order two bits)
148 */
149 #define NO_WARNING 0x00 /* no warning */
150 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
151 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
152 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
153
154 /*
155 * Clock Status Bits that Encode Version
156 */
157 #define NTPVERSION_1 0x08
158 #define VERSIONMASK 0x38
159 #define VERSIONSHIFT 3
160 #define LEAPMASK 0xc0
161 #define LEAPSHIFT 6
162 #ifdef MODEMASK
163 #undef MODEMASK /* Solaris sucks */
164 #endif
165 #define MODEMASK 0x07
166 #define MODESHIFT 0
167
168 /*
169 * Code values
170 */
171 #define MODE_UNSPEC 0 /* unspecified */
172 #define MODE_SYM_ACT 1 /* symmetric active */
173 #define MODE_SYM_PAS 2 /* symmetric passive */
174 #define MODE_CLIENT 3 /* client */
175 #define MODE_SERVER 4 /* server */
176 #define MODE_BROADCAST 5 /* broadcast */
177 #define MODE_CONTROL 6 /* control message */
178 #define MODE_RES2 7 /* reserved */
179
180 /*
181 * Stratum Definitions
182 */
183 #define UNSPECIFIED 0
184 #define PRIM_REF 1 /* radio clock */
185 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
186 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
187
188 static void p_sfix(netdissect_options *ndo, const struct s_fixedpt *);
189 static void p_ntp_time(netdissect_options *, const struct l_fixedpt *);
190 static void p_ntp_delta(netdissect_options *, const struct l_fixedpt *, const struct l_fixedpt *);
191 static void p_poll(netdissect_options *, const int);
192
193 static const struct tok ntp_mode_values[] = {
194 { MODE_UNSPEC, "unspecified" },
195 { MODE_SYM_ACT, "symmetric active" },
196 { MODE_SYM_PAS, "symmetric passive" },
197 { MODE_CLIENT, "Client" },
198 { MODE_SERVER, "Server" },
199 { MODE_BROADCAST, "Broadcast" },
200 { MODE_CONTROL, "Control Message" },
201 { MODE_RES2, "Reserved" },
202 { 0, NULL }
203 };
204
205 static const struct tok ntp_leapind_values[] = {
206 { NO_WARNING, "" },
207 { PLUS_SEC, "+1s" },
208 { MINUS_SEC, "-1s" },
209 { ALARM, "clock unsynchronized" },
210 { 0, NULL }
211 };
212
213 static const struct tok ntp_stratum_values[] = {
214 { UNSPECIFIED, "unspecified" },
215 { PRIM_REF, "primary reference" },
216 { 0, NULL }
217 };
218
219 /* draft-ietf-ntp-mode-6-cmds-02
220 * 0 1 2 3
221 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
222 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 * |LI | VN |Mode |R|E|M| OpCode | Sequence Number |
224 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225 * | Status | Association ID |
226 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
227 * | Offset | Count |
228 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 * | |
230 * / Data (up to 468 bytes) /
231 * | |
232 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233 * | Padding (optional) |
234 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235 * | |
236 * / Authenticator (optional, 96 bytes) /
237 * | |
238 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239 *
240 * Figure 1: NTP Control Message Header
241 */
242
243 /* Length of the NTP control message with the mandatory fields ("the header")
244 * and without any optional fields (Data, Padding, Authenticator).
245 */
246 #define NTP_CTRLMSG_MINLEN 12U
247
248 struct ntp_control_data {
249 nd_uint8_t magic; /* LI, VN, Mode */
250 nd_uint8_t control; /* R, E, M, OpCode */
251 nd_uint16_t sequence; /* Sequence Number */
252 nd_uint16_t status; /* Status */
253 nd_uint16_t assoc; /* Association ID */
254 nd_uint16_t offset; /* Offset */
255 nd_uint16_t count; /* Count */
256 nd_uint8_t data[564]; /* Data, [Padding, [Authenticator]] */
257 };
258
259 /*
260 * Print NTP time requests and responses
261 */
262 static void
263 ntp_time_print(netdissect_options *ndo,
264 const struct ntp_time_data *bp, u_int length)
265 {
266 uint8_t stratum;
267
268 if (length < NTP_TIMEMSG_MINLEN)
269 goto invalid;
270
271 ND_TCHECK_1(bp->stratum);
272 stratum = GET_U_1(bp->stratum);
273 ND_PRINT(", Stratum %u (%s)",
274 stratum,
275 tok2str(ntp_stratum_values, (stratum >=2 && stratum<=15) ? "secondary reference" : "reserved", stratum));
276
277 ND_TCHECK_1(bp->ppoll);
278 ND_PRINT(", poll %d", GET_S_1(bp->ppoll));
279 p_poll(ndo, GET_S_1(bp->ppoll));
280
281 ND_TCHECK_1(bp->precision);
282 ND_PRINT(", precision %d", GET_S_1(bp->precision));
283
284 ND_TCHECK_SIZE(&bp->root_delay);
285 ND_PRINT("\n\tRoot Delay: ");
286 p_sfix(ndo, &bp->root_delay);
287
288 ND_TCHECK_SIZE(&bp->root_dispersion);
289 ND_PRINT(", Root dispersion: ");
290 p_sfix(ndo, &bp->root_dispersion);
291
292 ND_TCHECK_4(bp->refid);
293 ND_PRINT(", Reference-ID: ");
294 /* Interpretation depends on stratum */
295 switch (stratum) {
296
297 case UNSPECIFIED:
298 ND_PRINT("(unspec)");
299 break;
300
301 case PRIM_REF:
302 if (nd_printn(ndo, (const u_char *)&(bp->refid), 4, ndo->ndo_snapend))
303 goto trunc;
304 break;
305
306 case INFO_QUERY:
307 ND_PRINT("%s INFO_QUERY", GET_IPADDR_STRING(bp->refid));
308 /* this doesn't have more content */
309 return;
310
311 case INFO_REPLY:
312 ND_PRINT("%s INFO_REPLY", GET_IPADDR_STRING(bp->refid));
313 /* this is too complex to be worth printing */
314 return;
315
316 default:
317 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
318 MD5 sum of IPv6 address */
319 ND_PRINT("0x%08x", GET_BE_U_4(bp->refid));
320 break;
321 }
322
323 ND_TCHECK_SIZE(&bp->ref_timestamp);
324 ND_PRINT("\n\t Reference Timestamp: ");
325 p_ntp_time(ndo, &(bp->ref_timestamp));
326
327 ND_TCHECK_SIZE(&bp->org_timestamp);
328 ND_PRINT("\n\t Originator Timestamp: ");
329 p_ntp_time(ndo, &(bp->org_timestamp));
330
331 ND_TCHECK_SIZE(&bp->rec_timestamp);
332 ND_PRINT("\n\t Receive Timestamp: ");
333 p_ntp_time(ndo, &(bp->rec_timestamp));
334
335 ND_TCHECK_SIZE(&bp->xmt_timestamp);
336 ND_PRINT("\n\t Transmit Timestamp: ");
337 p_ntp_time(ndo, &(bp->xmt_timestamp));
338
339 ND_PRINT("\n\t Originator - Receive Timestamp: ");
340 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
341
342 ND_PRINT("\n\t Originator - Transmit Timestamp: ");
343 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
344
345 /* FIXME: this code is not aware of any extension fields */
346 if (length == NTP_TIMEMSG_MINLEN + 4) { /* Optional: key-id (crypto-NAK) */
347 ND_TCHECK_4(bp->key_id);
348 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
349 } else if (length == NTP_TIMEMSG_MINLEN + 4 + 16) { /* Optional: key-id + 128-bit digest */
350 ND_TCHECK_4(bp->key_id);
351 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
352 ND_TCHECK_LEN(bp->message_digest, 16);
353 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x",
354 GET_BE_U_4(bp->message_digest),
355 GET_BE_U_4(bp->message_digest + 4),
356 GET_BE_U_4(bp->message_digest + 8),
357 GET_BE_U_4(bp->message_digest + 12));
358 } else if (length == NTP_TIMEMSG_MINLEN + 4 + 20) { /* Optional: key-id + 160-bit digest */
359 ND_TCHECK_4(bp->key_id);
360 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
361 ND_TCHECK_LEN(bp->message_digest, 20);
362 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x%08x",
363 GET_BE_U_4(bp->message_digest),
364 GET_BE_U_4(bp->message_digest + 4),
365 GET_BE_U_4(bp->message_digest + 8),
366 GET_BE_U_4(bp->message_digest + 12),
367 GET_BE_U_4(bp->message_digest + 16));
368 } else if (length > NTP_TIMEMSG_MINLEN) {
369 ND_PRINT("\n\t(%u more bytes after the header)", length - NTP_TIMEMSG_MINLEN);
370 }
371 return;
372
373 invalid:
374 nd_print_invalid(ndo);
375 ND_TCHECK_LEN(bp, length);
376 return;
377
378 trunc:
379 nd_print_trunc(ndo);
380 }
381
382 /*
383 * Print NTP control message requests and responses
384 */
385 static void
386 ntp_control_print(netdissect_options *ndo,
387 const struct ntp_control_data *cd, u_int length)
388 {
389 uint8_t control, R, E, M, opcode;
390 uint16_t sequence, status, assoc, offset, count;
391
392 if (length < NTP_CTRLMSG_MINLEN)
393 goto invalid;
394
395 ND_TCHECK_1(cd->control);
396 control = GET_U_1(cd->control);
397 R = (control & 0x80) != 0;
398 E = (control & 0x40) != 0;
399 M = (control & 0x20) != 0;
400 opcode = control & 0x1f;
401 ND_PRINT(", %s, %s, %s, OpCode=%u\n",
402 R ? "Response" : "Request", E ? "Error" : "OK",
403 M ? "More" : "Last", opcode);
404
405 ND_TCHECK_2(cd->sequence);
406 sequence = GET_BE_U_2(cd->sequence);
407 ND_PRINT("\tSequence=%hu", sequence);
408
409 ND_TCHECK_2(cd->status);
410 status = GET_BE_U_2(cd->status);
411 ND_PRINT(", Status=%#hx", status);
412
413 ND_TCHECK_2(cd->assoc);
414 assoc = GET_BE_U_2(cd->assoc);
415 ND_PRINT(", Assoc.=%hu", assoc);
416
417 ND_TCHECK_2(cd->offset);
418 offset = GET_BE_U_2(cd->offset);
419 ND_PRINT(", Offset=%hu", offset);
420
421 ND_TCHECK_2(cd->count);
422 count = GET_BE_U_2(cd->count);
423 ND_PRINT(", Count=%hu", count);
424
425 if (NTP_CTRLMSG_MINLEN + count > length)
426 goto invalid;
427 if (count != 0) {
428 ND_TCHECK_LEN(cd->data, count);
429 ND_PRINT("\n\tTO-BE-DONE: data not interpreted");
430 }
431 return;
432
433 invalid:
434 nd_print_invalid(ndo);
435 ND_TCHECK_LEN(cd, length);
436 return;
437
438 trunc:
439 nd_print_trunc(ndo);
440 }
441
442 union ntpdata {
443 struct ntp_time_data td;
444 struct ntp_control_data cd;
445 };
446
447 /*
448 * Print NTP requests, handling the common VN, LI, and Mode
449 */
450 void
451 ntp_print(netdissect_options *ndo,
452 const u_char *cp, u_int length)
453 {
454 const union ntpdata *bp = (const union ntpdata *)cp;
455 u_int mode, version, leapind;
456 uint8_t status;
457
458 ndo->ndo_protocol = "ntp";
459 ND_TCHECK_1(bp->td.status);
460 status = GET_U_1(bp->td.status);
461
462 version = (status & VERSIONMASK) >> VERSIONSHIFT;
463 ND_PRINT("NTPv%u", version);
464
465 mode = (status & MODEMASK) >> MODESHIFT;
466 if (!ndo->ndo_vflag) {
467 ND_PRINT(", %s, length %u",
468 tok2str(ntp_mode_values, "Unknown mode", mode),
469 length);
470 return;
471 }
472
473 ND_PRINT(", %s, length %u\n",
474 tok2str(ntp_mode_values, "Unknown mode", mode), length);
475
476 /* leapind = (status & LEAPMASK) >> LEAPSHIFT; */
477 leapind = (status & LEAPMASK);
478 ND_PRINT("\tLeap indicator: %s (%u)",
479 tok2str(ntp_leapind_values, "Unknown", leapind),
480 leapind);
481
482 switch (mode) {
483
484 case MODE_UNSPEC:
485 case MODE_SYM_ACT:
486 case MODE_SYM_PAS:
487 case MODE_CLIENT:
488 case MODE_SERVER:
489 case MODE_BROADCAST:
490 ntp_time_print(ndo, &bp->td, length);
491 break;
492
493 case MODE_CONTROL:
494 ntp_control_print(ndo, &bp->cd, length);
495 break;
496
497 default:
498 break; /* XXX: not implemented! */
499 }
500 return;
501
502 trunc:
503 nd_print_trunc(ndo);
504 }
505
506 static void
507 p_sfix(netdissect_options *ndo,
508 const struct s_fixedpt *sfp)
509 {
510 int i;
511 int f;
512 double ff;
513
514 i = GET_BE_U_2(sfp->int_part);
515 f = GET_BE_U_2(sfp->fraction);
516 ff = f / 65536.0; /* shift radix point by 16 bits */
517 f = (int)(ff * 1000000.0); /* Treat fraction as parts per million */
518 ND_PRINT("%d.%06d", i, f);
519 }
520
521 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
522
523 static void
524 p_ntp_time(netdissect_options *ndo,
525 const struct l_fixedpt *lfp)
526 {
527 uint32_t i;
528 uint32_t uf;
529 uint32_t f;
530 double ff;
531
532 i = GET_BE_U_4(lfp->int_part);
533 uf = GET_BE_U_4(lfp->fraction);
534 ff = uf;
535 if (ff < 0.0) /* some compilers are buggy */
536 ff += FMAXINT;
537 ff = ff / FMAXINT; /* shift radix point by 32 bits */
538 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
539 ND_PRINT("%u.%09u", i, f);
540
541 #ifdef HAVE_STRFTIME
542 /*
543 * print the UTC time in human-readable format.
544 */
545 if (i) {
546 int64_t seconds_64bit = (int64_t)i - JAN_1970;
547 time_t seconds;
548 struct tm *tm;
549 char time_buf[128];
550
551 seconds = (time_t)seconds_64bit;
552 if (seconds != seconds_64bit) {
553 /*
554 * It doesn't fit into a time_t, so we can't hand it
555 * to gmtime.
556 */
557 ND_PRINT(" (unrepresentable)");
558 } else {
559 tm = gmtime(&seconds);
560 if (tm == NULL) {
561 /*
562 * gmtime() can't handle it.
563 * (Yes, that might happen with some version of
564 * Microsoft's C library.)
565 */
566 ND_PRINT(" (unrepresentable)");
567 } else {
568 /* use ISO 8601 (RFC3339) format */
569 strftime(time_buf, sizeof (time_buf), "%Y-%m-%dT%H:%M:%SZ", tm);
570 ND_PRINT(" (%s)", time_buf);
571 }
572 }
573 }
574 #endif
575 }
576
577 /* Prints time difference between *lfp and *olfp */
578 static void
579 p_ntp_delta(netdissect_options *ndo,
580 const struct l_fixedpt *olfp,
581 const struct l_fixedpt *lfp)
582 {
583 uint32_t u, uf;
584 uint32_t ou, ouf;
585 uint32_t i;
586 uint32_t f;
587 double ff;
588 int signbit;
589
590 u = GET_BE_U_4(lfp->int_part);
591 ou = GET_BE_U_4(olfp->int_part);
592 uf = GET_BE_U_4(lfp->fraction);
593 ouf = GET_BE_U_4(olfp->fraction);
594 if (ou == 0 && ouf == 0) {
595 p_ntp_time(ndo, lfp);
596 return;
597 }
598
599 if (u > ou) { /* new is definitely greater than old */
600 signbit = 0;
601 i = u - ou;
602 f = uf - ouf;
603 if (ouf > uf) /* must borrow from high-order bits */
604 i -= 1;
605 } else if (u < ou) { /* new is definitely less than old */
606 signbit = 1;
607 i = ou - u;
608 f = ouf - uf;
609 if (uf > ouf) /* must borrow from the high-order bits */
610 i -= 1;
611 } else { /* int_part is zero */
612 i = 0;
613 if (uf > ouf) {
614 signbit = 0;
615 f = uf - ouf;
616 } else {
617 signbit = 1;
618 f = ouf - uf;
619 }
620 }
621
622 ff = f;
623 if (ff < 0.0) /* some compilers are buggy */
624 ff += FMAXINT;
625 ff = ff / FMAXINT; /* shift radix point by 32 bits */
626 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
627 ND_PRINT("%s%u.%09u", signbit ? "-" : "+", i, f);
628 }
629
630 /* Prints polling interval in log2 as seconds or fraction of second */
631 static void
632 p_poll(netdissect_options *ndo,
633 const int poll_interval)
634 {
635 if (poll_interval <= -32 || poll_interval >= 32)
636 return;
637
638 if (poll_interval >= 0)
639 ND_PRINT(" (%us)", 1U << poll_interval);
640 else
641 ND_PRINT(" (1/%us)", 1U << -poll_interval);
642 }
643