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