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