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