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