]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
From Darren Reed: just show protocol 0x00fb as "compressed PPP data".
[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 * Format and print ntp packets.
22 * By Jeffrey Mogul/DECWRL
23 * loosely based on print-bootp.c
24 */
25
26 #ifndef lint
27 static const char rcsid[] _U_ =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.41 2004-01-28 14:54:50 hannes Exp $ (LBL)";
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <tcpdump-stdinc.h>
36
37 #include <stdio.h>
38 #include <string.h>
39 #ifdef HAVE_STRFTIME
40 #include <time.h>
41 #endif
42
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h"
46 #ifdef MODEMASK
47 #undef MODEMASK /* Solaris sucks */
48 #endif
49 #include "ntp.h"
50
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 *);
54
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" },
64 { 0, NULL }
65 };
66
67 static struct tok ntp_leapind_values[] = {
68 { NO_WARNING, "" },
69 { PLUS_SEC, "+1s" },
70 { MINUS_SEC, "-1s" },
71 { ALARM, "clock unsynchronized" },
72 { 0, NULL }
73 };
74
75 /*
76 * Print ntp requests
77 */
78 void
79 ntp_print(register const u_char *cp, u_int length)
80 {
81 register const struct ntpdata *bp;
82 int mode, version, leapind;
83
84 bp = (struct ntpdata *)cp;
85
86 TCHECK(bp->status);
87
88 version = (int)(bp->status & VERSIONMASK) >> 3;
89 printf("NTPv%d", version);
90
91 mode = bp->status & MODEMASK;
92 if (!vflag) {
93 printf (", %s, length %u",
94 tok2str(ntp_mode_values, "Unknown mode", mode),
95 length);
96 return;
97 }
98
99 printf (", length %u\n\t%s",
100 length,
101 tok2str(ntp_mode_values, "Unknown mode", mode));
102
103 leapind = bp->status & LEAPMASK;
104 printf (", Leap indicator: %s (%u)",
105 tok2str(ntp_leapind_values, "Unknown", leapind),
106 leapind);
107
108 TCHECK(bp->stratum);
109 printf(", Stratum %u", bp->stratum);
110
111 TCHECK(bp->ppoll);
112 printf(", poll %us", bp->ppoll);
113
114 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
115 TCHECK2(bp->root_delay, 0);
116 printf(", precision %d", bp->precision);
117
118 TCHECK(bp->root_delay);
119 fputs("\n\tRoot Delay: ", stdout);
120 p_sfix(&bp->root_delay);
121
122 TCHECK(bp->root_dispersion);
123 fputs(", Root dispersion: ", stdout);
124 p_sfix(&bp->root_dispersion);
125
126 TCHECK(bp->refid);
127 fputs(", Reference-ID: ", stdout);
128 /* Interpretation depends on stratum */
129 switch (bp->stratum) {
130
131 case UNSPECIFIED:
132 printf("(unspec)");
133 break;
134
135 case PRIM_REF:
136 fn_printn((u_char *)&(bp->refid), 4, NULL);
137 break;
138
139 case INFO_QUERY:
140 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
141 /* this doesn't have more content */
142 return;
143
144 case INFO_REPLY:
145 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
146 /* this is too complex to be worth printing */
147 return;
148
149 default:
150 printf("%s", ipaddr_string(&(bp->refid)));
151 break;
152 }
153
154 TCHECK(bp->ref_timestamp);
155 fputs("\n\t Reference Timestamp: ", stdout);
156 p_ntp_time(&(bp->ref_timestamp));
157
158 TCHECK(bp->org_timestamp);
159 fputs("\n\t Originator Timestamp: ", stdout);
160 p_ntp_time(&(bp->org_timestamp));
161
162 TCHECK(bp->rec_timestamp);
163 fputs("\n\t Receive Timestamp: ", stdout);
164 p_ntp_time(&(bp->rec_timestamp));
165
166 TCHECK(bp->xmt_timestamp);
167 fputs("\n\t Transmit Timestamp: ", stdout);
168 p_ntp_time(&(bp->xmt_timestamp));
169
170 fputs("\n\t Originator - Receive Timestamp: ", stdout);
171 p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp));
172
173 fputs("\n\t Originator - Transmit Timestamp: ", stdout);
174 p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp));
175
176 /* FIXME key-id, authentication */
177
178 return;
179
180 trunc:
181 fputs(" [|ntp]", stdout);
182 }
183
184 static void
185 p_sfix(register const struct s_fixedpt *sfp)
186 {
187 register int i;
188 register int f;
189 register float ff;
190
191 i = EXTRACT_16BITS(&sfp->int_part);
192 f = EXTRACT_16BITS(&sfp->fraction);
193 ff = f / 65536.0; /* shift radix point by 16 bits */
194 f = ff * 1000000.0; /* Treat fraction as parts per million */
195 printf("%d.%06d", i, f);
196 }
197
198 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
199
200 static void
201 p_ntp_time(register const struct l_fixedpt *lfp)
202 {
203 register int32_t i;
204 register u_int32_t uf;
205 register u_int32_t f;
206 register float ff;
207
208 i = EXTRACT_32BITS(&lfp->int_part);
209 uf = EXTRACT_32BITS(&lfp->fraction);
210 ff = uf;
211 if (ff < 0.0) /* some compilers are buggy */
212 ff += FMAXINT;
213 ff = ff / FMAXINT; /* shift radix point by 32 bits */
214 f = ff * 1000000000.0; /* treat fraction as parts per billion */
215 printf("%u.%09d", i, f);
216
217 #ifdef HAVE_STRFTIME
218 /*
219 * print the time in human-readable format.
220 */
221 if (i) {
222 time_t seconds = i - JAN_1970;
223 struct tm *tm;
224 char time_buf[128];
225
226 tm = localtime(&seconds);
227 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
228 printf (" (%s)", time_buf);
229 }
230 #endif
231 }
232
233 /* Prints time difference between *lfp and *olfp */
234 static void
235 p_ntp_delta(register const struct l_fixedpt *olfp,
236 register const struct l_fixedpt *lfp)
237 {
238 register int32_t i;
239 register u_int32_t u, uf;
240 register u_int32_t ou, ouf;
241 register u_int32_t f;
242 register float ff;
243 int signbit;
244
245 u = EXTRACT_32BITS(&lfp->int_part);
246 ou = EXTRACT_32BITS(&olfp->int_part);
247 uf = EXTRACT_32BITS(&lfp->fraction);
248 ouf = EXTRACT_32BITS(&olfp->fraction);
249 if (ou == 0 && ouf == 0) {
250 p_ntp_time(lfp);
251 return;
252 }
253
254 i = u - ou;
255
256 if (i > 0) { /* new is definitely greater than old */
257 signbit = 0;
258 f = uf - ouf;
259 if (ouf > uf) /* must borrow from high-order bits */
260 i -= 1;
261 } else if (i < 0) { /* new is definitely less than old */
262 signbit = 1;
263 f = ouf - uf;
264 if (uf > ouf) /* must carry into the high-order bits */
265 i += 1;
266 i = -i;
267 } else { /* int_part is zero */
268 if (uf > ouf) {
269 signbit = 0;
270 f = uf - ouf;
271 } else {
272 signbit = 1;
273 f = ouf - uf;
274 }
275 }
276
277 ff = f;
278 if (ff < 0.0) /* some compilers are buggy */
279 ff += FMAXINT;
280 ff = ff / FMAXINT; /* shift radix point by 32 bits */
281 f = ff * 1000000000.0; /* treat fraction as parts per billion */
282 if (signbit)
283 putchar('-');
284 else
285 putchar('+');
286 printf("%d.%09d", i, f);
287 }
288