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