]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
don't pass on src & dst MAC adresses to the isoclns decoder as MAC adresses
[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[] =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.36 2002-12-28 17:15:06 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
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h"
43 #ifdef MODEMASK
44 #undef MODEMASK /* Solaris sucks */
45 #endif
46 #include "ntp.h"
47
48 static void p_sfix(const struct s_fixedpt *);
49 static void p_ntp_time(const struct l_fixedpt *);
50 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
51
52 /*
53 * Print ntp requests
54 */
55 void
56 ntp_print(register const u_char *cp, u_int length)
57 {
58 register const struct ntpdata *bp;
59 int mode, version, leapind;
60
61 bp = (struct ntpdata *)cp;
62 /* Note funny sized packets */
63 if (length != sizeof(struct ntpdata))
64 (void)printf(" [len=%d]", length);
65
66 TCHECK(bp->status);
67
68 version = (int)(bp->status & VERSIONMASK) >> 3;
69 printf("NTPv%d", version);
70
71 leapind = bp->status & LEAPMASK;
72 switch (leapind) {
73
74 case NO_WARNING:
75 break;
76
77 case PLUS_SEC:
78 fputs(" +1s", stdout);
79 break;
80
81 case MINUS_SEC:
82 fputs(" -1s", stdout);
83 break;
84 }
85
86 mode = bp->status & MODEMASK;
87 switch (mode) {
88
89 case MODE_UNSPEC: /* unspecified */
90 fputs(" unspec", stdout);
91 break;
92
93 case MODE_SYM_ACT: /* symmetric active */
94 fputs(" sym_act", stdout);
95 break;
96
97 case MODE_SYM_PAS: /* symmetric passive */
98 fputs(" sym_pas", stdout);
99 break;
100
101 case MODE_CLIENT: /* client */
102 fputs(" client", stdout);
103 break;
104
105 case MODE_SERVER: /* server */
106 fputs(" server", stdout);
107 break;
108
109 case MODE_BROADCAST: /* broadcast */
110 fputs(" bcast", stdout);
111 break;
112
113 case MODE_RES1: /* reserved */
114 fputs(" res1", stdout);
115 break;
116
117 case MODE_RES2: /* reserved */
118 fputs(" res2", stdout);
119 break;
120
121 }
122
123 TCHECK(bp->stratum);
124 printf(", strat %d", bp->stratum);
125
126 TCHECK(bp->ppoll);
127 printf(", poll %d", bp->ppoll);
128
129 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
130 TCHECK2(bp->distance, 0);
131 printf(", prec %d", bp->precision);
132
133 if (!vflag)
134 return;
135
136 TCHECK(bp->distance);
137 fputs(" dist ", stdout);
138 p_sfix(&bp->distance);
139
140 TCHECK(bp->dispersion);
141 fputs(", disp ", stdout);
142 p_sfix(&bp->dispersion);
143
144 TCHECK(bp->refid);
145 fputs(", ref ", stdout);
146 /* Interpretation depends on stratum */
147 switch (bp->stratum) {
148
149 case UNSPECIFIED:
150 printf("(unspec)");
151 break;
152
153 case PRIM_REF:
154 fn_printn((u_char *)&(bp->refid), 4, NULL);
155 break;
156
157 case INFO_QUERY:
158 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
159 /* this doesn't have more content */
160 return;
161
162 case INFO_REPLY:
163 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
164 /* this is too complex to be worth printing */
165 return;
166
167 default:
168 printf("%s", ipaddr_string(&(bp->refid)));
169 break;
170 }
171
172 TCHECK(bp->reftime);
173 putchar('@');
174 p_ntp_time(&(bp->reftime));
175
176 TCHECK(bp->org);
177 fputs(" orig ", stdout);
178 p_ntp_time(&(bp->org));
179
180 TCHECK(bp->rec);
181 fputs(" rec ", stdout);
182 p_ntp_delta(&(bp->org), &(bp->rec));
183
184 TCHECK(bp->xmt);
185 fputs(" xmt ", stdout);
186 p_ntp_delta(&(bp->org), &(bp->xmt));
187
188 return;
189
190 trunc:
191 fputs(" [|ntp]", stdout);
192 }
193
194 static void
195 p_sfix(register const struct s_fixedpt *sfp)
196 {
197 register int i;
198 register int f;
199 register float ff;
200
201 i = EXTRACT_16BITS(&sfp->int_part);
202 f = EXTRACT_16BITS(&sfp->fraction);
203 ff = f / 65536.0; /* shift radix point by 16 bits */
204 f = ff * 1000000.0; /* Treat fraction as parts per million */
205 printf("%d.%06d", i, f);
206 }
207
208 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
209
210 static void
211 p_ntp_time(register const struct l_fixedpt *lfp)
212 {
213 register int32_t i;
214 register u_int32_t uf;
215 register u_int32_t f;
216 register float ff;
217
218 i = EXTRACT_32BITS(&lfp->int_part);
219 uf = EXTRACT_32BITS(&lfp->fraction);
220 ff = uf;
221 if (ff < 0.0) /* some compilers are buggy */
222 ff += FMAXINT;
223 ff = ff / FMAXINT; /* shift radix point by 32 bits */
224 f = ff * 1000000000.0; /* treat fraction as parts per billion */
225 printf("%u.%09d", i, f);
226 }
227
228 /* Prints time difference between *lfp and *olfp */
229 static void
230 p_ntp_delta(register const struct l_fixedpt *olfp,
231 register const struct l_fixedpt *lfp)
232 {
233 register int32_t i;
234 register u_int32_t uf;
235 register u_int32_t ouf;
236 register u_int32_t f;
237 register float ff;
238 int signbit;
239
240 i = EXTRACT_32BITS(&lfp->int_part) - EXTRACT_32BITS(&olfp->int_part);
241
242 uf = EXTRACT_32BITS(&lfp->fraction);
243 ouf = EXTRACT_32BITS(&olfp->fraction);
244
245 if (i > 0) { /* new is definitely greater than old */
246 signbit = 0;
247 f = uf - ouf;
248 if (ouf > uf) /* must borrow from high-order bits */
249 i -= 1;
250 } else if (i < 0) { /* new is definitely less than old */
251 signbit = 1;
252 f = ouf - uf;
253 if (uf > ouf) /* must carry into the high-order bits */
254 i += 1;
255 i = -i;
256 } else { /* int_part is zero */
257 if (uf > ouf) {
258 signbit = 0;
259 f = uf - ouf;
260 } else {
261 signbit = 1;
262 f = ouf - uf;
263 }
264 }
265
266 ff = f;
267 if (ff < 0.0) /* some compilers are buggy */
268 ff += FMAXINT;
269 ff = ff / FMAXINT; /* shift radix point by 32 bits */
270 f = ff * 1000000000.0; /* treat fraction as parts per billion */
271 if (signbit)
272 putchar('-');
273 else
274 putchar('+');
275 printf("%d.%09d", i, f);
276 }
277