2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Format and print ntp packets.
22 * By Jeffrey Mogul/DECWRL
23 * loosely based on print-bootp.c
27 static const char rcsid[] _U_ =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.41.2.1 2005/05/06 07:57:18 guy Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
43 #include "interface.h"
44 #include "addrtoname.h"
47 #undef MODEMASK /* Solaris sucks */
51 static void p_sfix(const struct s_fixedpt *);
52 static void p_ntp_time(const struct l_fixedpt *);
53 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
55 static struct tok ntp_mode_values[] = {
56 { MODE_UNSPEC, "unspecified" },
57 { MODE_SYM_ACT, "symmetric active" },
58 { MODE_SYM_PAS, "symmetric passive" },
59 { MODE_CLIENT, "Client" },
60 { MODE_SERVER, "Server" },
61 { MODE_BROADCAST, "Broadcast" },
62 { MODE_RES1, "Reserved" },
63 { MODE_RES2, "Reserved" },
67 static struct tok ntp_leapind_values[] = {
71 { ALARM, "clock unsynchronized" },
79 ntp_print(register const u_char *cp, u_int length)
81 register const struct ntpdata *bp;
82 int mode, version, leapind;
84 bp = (struct ntpdata *)cp;
88 version = (int)(bp->status & VERSIONMASK) >> 3;
89 printf("NTPv%d", version);
91 mode = bp->status & MODEMASK;
93 printf (", %s, length %u",
94 tok2str(ntp_mode_values, "Unknown mode", mode),
99 printf (", length %u\n\t%s",
101 tok2str(ntp_mode_values, "Unknown mode", mode));
103 leapind = bp->status & LEAPMASK;
104 printf (", Leap indicator: %s (%u)",
105 tok2str(ntp_leapind_values, "Unknown", leapind),
109 printf(", Stratum %u", bp->stratum);
112 printf(", poll %us", bp->ppoll);
114 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
115 TCHECK2(bp->root_delay, 0);
116 printf(", precision %d", bp->precision);
118 TCHECK(bp->root_delay);
119 fputs("\n\tRoot Delay: ", stdout);
120 p_sfix(&bp->root_delay);
122 TCHECK(bp->root_dispersion);
123 fputs(", Root dispersion: ", stdout);
124 p_sfix(&bp->root_dispersion);
127 fputs(", Reference-ID: ", stdout);
128 /* Interpretation depends on stratum */
129 switch (bp->stratum) {
136 if (fn_printn((u_char *)&(bp->refid), 4, snapend))
141 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
142 /* this doesn't have more content */
146 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
147 /* this is too complex to be worth printing */
151 printf("%s", ipaddr_string(&(bp->refid)));
155 TCHECK(bp->ref_timestamp);
156 fputs("\n\t Reference Timestamp: ", stdout);
157 p_ntp_time(&(bp->ref_timestamp));
159 TCHECK(bp->org_timestamp);
160 fputs("\n\t Originator Timestamp: ", stdout);
161 p_ntp_time(&(bp->org_timestamp));
163 TCHECK(bp->rec_timestamp);
164 fputs("\n\t Receive Timestamp: ", stdout);
165 p_ntp_time(&(bp->rec_timestamp));
167 TCHECK(bp->xmt_timestamp);
168 fputs("\n\t Transmit Timestamp: ", stdout);
169 p_ntp_time(&(bp->xmt_timestamp));
171 fputs("\n\t Originator - Receive Timestamp: ", stdout);
172 p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp));
174 fputs("\n\t Originator - Transmit Timestamp: ", stdout);
175 p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp));
177 /* FIXME key-id, authentication */
182 fputs(" [|ntp]", stdout);
186 p_sfix(register const struct s_fixedpt *sfp)
192 i = EXTRACT_16BITS(&sfp->int_part);
193 f = EXTRACT_16BITS(&sfp->fraction);
194 ff = f / 65536.0; /* shift radix point by 16 bits */
195 f = ff * 1000000.0; /* Treat fraction as parts per million */
196 printf("%d.%06d", i, f);
199 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
202 p_ntp_time(register const struct l_fixedpt *lfp)
205 register u_int32_t uf;
206 register u_int32_t f;
209 i = EXTRACT_32BITS(&lfp->int_part);
210 uf = EXTRACT_32BITS(&lfp->fraction);
212 if (ff < 0.0) /* some compilers are buggy */
214 ff = ff / FMAXINT; /* shift radix point by 32 bits */
215 f = ff * 1000000000.0; /* treat fraction as parts per billion */
216 printf("%u.%09d", i, f);
220 * print the time in human-readable format.
223 time_t seconds = i - JAN_1970;
227 tm = localtime(&seconds);
228 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
229 printf (" (%s)", time_buf);
234 /* Prints time difference between *lfp and *olfp */
236 p_ntp_delta(register const struct l_fixedpt *olfp,
237 register const struct l_fixedpt *lfp)
240 register u_int32_t u, uf;
241 register u_int32_t ou, ouf;
242 register u_int32_t f;
246 u = EXTRACT_32BITS(&lfp->int_part);
247 ou = EXTRACT_32BITS(&olfp->int_part);
248 uf = EXTRACT_32BITS(&lfp->fraction);
249 ouf = EXTRACT_32BITS(&olfp->fraction);
250 if (ou == 0 && ouf == 0) {
257 if (i > 0) { /* new is definitely greater than old */
260 if (ouf > uf) /* must borrow from high-order bits */
262 } else if (i < 0) { /* new is definitely less than old */
265 if (uf > ouf) /* must carry into the high-order bits */
268 } else { /* int_part is zero */
279 if (ff < 0.0) /* some compilers are buggy */
281 ff = ff / FMAXINT; /* shift radix point by 32 bits */
282 f = ff * 1000000000.0; /* treat fraction as parts per billion */
287 printf("%d.%09d", i, f);