Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / print-ntp.c
blob819d9f4fdec2aa41c3e02c4f1b8dcbb422fda405
1 /* $NetBSD$ */
3 /*
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
18 * written permission.
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>
29 #ifndef lint
30 #if 0
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)";
33 #else
34 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
35 #endif
36 #endif
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <tcpdump-stdinc.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_STRFTIME
47 #include <time.h>
48 #endif
50 #include "interface.h"
51 #include "addrtoname.h"
52 #include "extract.h"
53 #ifdef MODEMASK
54 #undef MODEMASK /* Solaris sucks */
55 #endif
56 #include "ntp.h"
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" },
71 { 0, NULL }
74 static struct tok ntp_leapind_values[] = {
75 { NO_WARNING, "" },
76 { PLUS_SEC, "+1s" },
77 { MINUS_SEC, "-1s" },
78 { ALARM, "clock unsynchronized" },
79 { 0, NULL }
83 * Print ntp requests
85 void
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;
93 TCHECK(bp->status);
95 version = (int)(bp->status & VERSIONMASK) >> 3;
96 printf("NTPv%d", version);
98 mode = bp->status & MODEMASK;
99 if (!vflag) {
100 printf (", %s, length %u",
101 tok2str(ntp_mode_values, "Unknown mode", mode),
102 length);
103 return;
106 printf (", length %u\n\t%s",
107 length,
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),
113 leapind);
115 TCHECK(bp->stratum);
116 printf(", Stratum %u", bp->stratum);
118 TCHECK(bp->ppoll);
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);
133 TCHECK(bp->refid);
134 fputs(", Reference-ID: ", stdout);
135 /* Interpretation depends on stratum */
136 switch (bp->stratum) {
138 case UNSPECIFIED:
139 printf("(unspec)");
140 break;
142 case PRIM_REF:
143 if (fn_printn((u_char *)&(bp->refid), 4, snapend))
144 goto trunc;
145 break;
147 case INFO_QUERY:
148 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
149 /* this doesn't have more content */
150 return;
152 case INFO_REPLY:
153 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
154 /* this is too complex to be worth printing */
155 return;
157 default:
158 printf("%s", ipaddr_string(&(bp->refid)));
159 break;
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 */
186 return;
188 trunc:
189 fputs(" [|ntp]", stdout);
192 static void
193 p_sfix(register const struct s_fixedpt *sfp)
195 register int i;
196 register int f;
197 register float ff;
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 */
208 static void
209 p_ntp_time(register const struct l_fixedpt *lfp)
211 register int32_t i;
212 register u_int32_t uf;
213 register u_int32_t f;
214 register float ff;
216 i = EXTRACT_32BITS(&lfp->int_part);
217 uf = EXTRACT_32BITS(&lfp->fraction);
218 ff = uf;
219 if (ff < 0.0) /* some compilers are buggy */
220 ff += FMAXINT;
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);
225 #ifdef HAVE_STRFTIME
227 * print the time in human-readable format.
229 if (i) {
230 time_t seconds = i - JAN_1970;
231 struct tm *tm;
232 char time_buf[128];
234 tm = localtime(&seconds);
235 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
236 printf (" (%s)", time_buf);
238 #endif
241 /* Prints time difference between *lfp and *olfp */
242 static void
243 p_ntp_delta(register const struct l_fixedpt *olfp,
244 register const struct l_fixedpt *lfp)
246 register int32_t i;
247 register u_int32_t u, uf;
248 register u_int32_t ou, ouf;
249 register u_int32_t f;
250 register float ff;
251 int signbit;
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) {
258 p_ntp_time(lfp);
259 return;
262 i = u - ou;
264 if (i > 0) { /* new is definitely greater than old */
265 signbit = 0;
266 f = uf - ouf;
267 if (ouf > uf) /* must borrow from high-order bits */
268 i -= 1;
269 } else if (i < 0) { /* new is definitely less than old */
270 signbit = 1;
271 f = ouf - uf;
272 if (uf > ouf) /* must carry into the high-order bits */
273 i += 1;
274 i = -i;
275 } else { /* int_part is zero */
276 if (uf > ouf) {
277 signbit = 0;
278 f = uf - ouf;
279 } else {
280 signbit = 1;
281 f = ouf - uf;
285 ff = f;
286 if (ff < 0.0) /* some compilers are buggy */
287 ff += FMAXINT;
288 ff = ff / FMAXINT; /* shift radix point by 32 bits */
289 f = ff * 1000000000.0; /* treat fraction as parts per billion */
290 if (signbit)
291 putchar('-');
292 else
293 putchar('+');
294 printf("%d.%09d", i, f);