4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Format and print ntp packets.
24 * By Jeffrey Mogul/DECWRL
25 * loosely based on print-bootp.c
28 #include <sys/cdefs.h>
31 static const char rcsid
[] _U_
=
32 "@(#) Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.41.2.1 2005/05/06 07:57:18 guy Exp (LBL)";
34 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
42 #include <tcpdump-stdinc.h>
50 #include "interface.h"
51 #include "addrtoname.h"
54 #undef MODEMASK /* Solaris sucks */
58 static void p_sfix(const struct s_fixedpt
*);
59 static void p_ntp_time(const struct l_fixedpt
*);
60 static void p_ntp_delta(const struct l_fixedpt
*, const struct l_fixedpt
*);
62 static struct tok ntp_mode_values
[] = {
63 { MODE_UNSPEC
, "unspecified" },
64 { MODE_SYM_ACT
, "symmetric active" },
65 { MODE_SYM_PAS
, "symmetric passive" },
66 { MODE_CLIENT
, "Client" },
67 { MODE_SERVER
, "Server" },
68 { MODE_BROADCAST
, "Broadcast" },
69 { MODE_RES1
, "Reserved" },
70 { MODE_RES2
, "Reserved" },
74 static struct tok ntp_leapind_values
[] = {
78 { ALARM
, "clock unsynchronized" },
86 ntp_print(register const u_char
*cp
, u_int length
)
88 register const struct ntpdata
*bp
;
89 int mode
, version
, leapind
;
91 bp
= (struct ntpdata
*)cp
;
95 version
= (int)(bp
->status
& VERSIONMASK
) >> 3;
96 printf("NTPv%d", version
);
98 mode
= bp
->status
& MODEMASK
;
100 printf (", %s, length %u",
101 tok2str(ntp_mode_values
, "Unknown mode", mode
),
106 printf (", length %u\n\t%s",
108 tok2str(ntp_mode_values
, "Unknown mode", mode
));
110 leapind
= bp
->status
& LEAPMASK
;
111 printf (", Leap indicator: %s (%u)",
112 tok2str(ntp_leapind_values
, "Unknown", leapind
),
116 printf(", Stratum %u", bp
->stratum
);
119 printf(", poll %us", bp
->ppoll
);
121 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
122 TCHECK2(bp
->root_delay
, 0);
123 printf(", precision %d", bp
->precision
);
125 TCHECK(bp
->root_delay
);
126 fputs("\n\tRoot Delay: ", stdout
);
127 p_sfix(&bp
->root_delay
);
129 TCHECK(bp
->root_dispersion
);
130 fputs(", Root dispersion: ", stdout
);
131 p_sfix(&bp
->root_dispersion
);
134 fputs(", Reference-ID: ", stdout
);
135 /* Interpretation depends on stratum */
136 switch (bp
->stratum
) {
143 if (fn_printn((u_char
*)&(bp
->refid
), 4, snapend
))
148 printf("%s INFO_QUERY", ipaddr_string(&(bp
->refid
)));
149 /* this doesn't have more content */
153 printf("%s INFO_REPLY", ipaddr_string(&(bp
->refid
)));
154 /* this is too complex to be worth printing */
158 printf("%s", ipaddr_string(&(bp
->refid
)));
162 TCHECK(bp
->ref_timestamp
);
163 fputs("\n\t Reference Timestamp: ", stdout
);
164 p_ntp_time(&(bp
->ref_timestamp
));
166 TCHECK(bp
->org_timestamp
);
167 fputs("\n\t Originator Timestamp: ", stdout
);
168 p_ntp_time(&(bp
->org_timestamp
));
170 TCHECK(bp
->rec_timestamp
);
171 fputs("\n\t Receive Timestamp: ", stdout
);
172 p_ntp_time(&(bp
->rec_timestamp
));
174 TCHECK(bp
->xmt_timestamp
);
175 fputs("\n\t Transmit Timestamp: ", stdout
);
176 p_ntp_time(&(bp
->xmt_timestamp
));
178 fputs("\n\t Originator - Receive Timestamp: ", stdout
);
179 p_ntp_delta(&(bp
->org_timestamp
), &(bp
->rec_timestamp
));
181 fputs("\n\t Originator - Transmit Timestamp: ", stdout
);
182 p_ntp_delta(&(bp
->org_timestamp
), &(bp
->xmt_timestamp
));
184 /* FIXME key-id, authentication */
189 fputs(" [|ntp]", stdout
);
193 p_sfix(register const struct s_fixedpt
*sfp
)
199 i
= EXTRACT_16BITS(&sfp
->int_part
);
200 f
= EXTRACT_16BITS(&sfp
->fraction
);
201 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
202 f
= ff
* 1000000.0; /* Treat fraction as parts per million */
203 printf("%d.%06d", i
, f
);
206 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
209 p_ntp_time(register const struct l_fixedpt
*lfp
)
212 register u_int32_t uf
;
213 register u_int32_t f
;
216 i
= EXTRACT_32BITS(&lfp
->int_part
);
217 uf
= EXTRACT_32BITS(&lfp
->fraction
);
219 if (ff
< 0.0) /* some compilers are buggy */
221 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
222 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
223 printf("%u.%09d", i
, f
);
227 * print the time in human-readable format.
230 time_t seconds
= i
- JAN_1970
;
234 tm
= localtime(&seconds
);
235 strftime(time_buf
, sizeof (time_buf
), "%Y/%m/%d %H:%M:%S", tm
);
236 printf (" (%s)", time_buf
);
241 /* Prints time difference between *lfp and *olfp */
243 p_ntp_delta(register const struct l_fixedpt
*olfp
,
244 register const struct l_fixedpt
*lfp
)
247 register u_int32_t u
, uf
;
248 register u_int32_t ou
, ouf
;
249 register u_int32_t f
;
253 u
= EXTRACT_32BITS(&lfp
->int_part
);
254 ou
= EXTRACT_32BITS(&olfp
->int_part
);
255 uf
= EXTRACT_32BITS(&lfp
->fraction
);
256 ouf
= EXTRACT_32BITS(&olfp
->fraction
);
257 if (ou
== 0 && ouf
== 0) {
264 if (i
> 0) { /* new is definitely greater than old */
267 if (ouf
> uf
) /* must borrow from high-order bits */
269 } else if (i
< 0) { /* new is definitely less than old */
272 if (uf
> ouf
) /* must carry into the high-order bits */
275 } else { /* int_part is zero */
286 if (ff
< 0.0) /* some compilers are buggy */
288 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
289 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
294 printf("%d.%09d", i
, f
);