]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights | |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that: (1) source code distributions | |
30 | * retain the above copyright notice and this paragraph in its entirety, (2) | |
31 | * distributions including binary code include the above copyright notice and | |
32 | * this paragraph in its entirety in the documentation or other materials | |
33 | * provided with the distribution, and (3) all advertising materials mentioning | |
34 | * features or use of this software display the following acknowledgement: | |
35 | * ``This product includes software developed by the University of California, | |
36 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of | |
37 | * the University nor the names of its contributors may be used to endorse | |
38 | * or promote products derived from this software without specific prior | |
39 | * written permission. | |
40 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED | |
41 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
42 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
43 | * | |
44 | * Format and print ntp packets. | |
45 | * By Jeffrey Mogul/DECWRL | |
46 | * loosely based on print-bootp.c | |
47 | */ | |
48 | ||
49 | #ifndef lint | |
50 | static const char rcsid[] = | |
51 | "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-ntp.c,v 1.1.1.1 1999/05/02 03:58:34 wsanchez Exp $ (LBL)"; | |
52 | #endif | |
53 | ||
54 | #include <sys/param.h> | |
55 | #include <sys/time.h> | |
56 | #include <sys/socket.h> | |
57 | ||
58 | #if __STDC__ | |
59 | struct mbuf; | |
60 | struct rtentry; | |
61 | #endif | |
62 | #include <net/if.h> | |
63 | ||
64 | #include <netinet/in.h> | |
65 | #include <netinet/if_ether.h> | |
66 | ||
67 | #include <ctype.h> | |
68 | #include <stdio.h> | |
69 | #include <string.h> | |
70 | ||
71 | #include "interface.h" | |
72 | #include "addrtoname.h" | |
73 | #undef MODEMASK /* Solaris sucks */ | |
74 | #include "ntp.h" | |
75 | ||
76 | static void p_sfix(const struct s_fixedpt *); | |
77 | static void p_ntp_time(const struct l_fixedpt *); | |
78 | static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *); | |
79 | ||
80 | /* | |
81 | * Print ntp requests | |
82 | */ | |
83 | void | |
84 | ntp_print(register const u_char *cp, u_int length) | |
85 | { | |
86 | register const struct ntpdata *bp; | |
87 | int mode, version, leapind; | |
88 | static char rclock[5]; | |
89 | ||
90 | bp = (struct ntpdata *)cp; | |
91 | /* Note funny sized packets */ | |
92 | if (length != sizeof(struct ntpdata)) | |
93 | (void)printf(" [len=%d]", length); | |
94 | ||
95 | TCHECK(bp->status); | |
96 | ||
97 | version = (int)(bp->status & VERSIONMASK) >> 3; | |
98 | printf(" v%d", version); | |
99 | ||
100 | leapind = bp->status & LEAPMASK; | |
101 | switch (leapind) { | |
102 | ||
103 | case NO_WARNING: | |
104 | break; | |
105 | ||
106 | case PLUS_SEC: | |
107 | fputs(" +1s", stdout); | |
108 | break; | |
109 | ||
110 | case MINUS_SEC: | |
111 | fputs(" -1s", stdout); | |
112 | break; | |
113 | } | |
114 | ||
115 | mode = bp->status & MODEMASK; | |
116 | switch (mode) { | |
117 | ||
118 | case MODE_UNSPEC: /* unspecified */ | |
119 | fputs(" unspec", stdout); | |
120 | break; | |
121 | ||
122 | case MODE_SYM_ACT: /* symmetric active */ | |
123 | fputs(" sym_act", stdout); | |
124 | break; | |
125 | ||
126 | case MODE_SYM_PAS: /* symmetric passive */ | |
127 | fputs(" sym_pas", stdout); | |
128 | break; | |
129 | ||
130 | case MODE_CLIENT: /* client */ | |
131 | fputs(" client", stdout); | |
132 | break; | |
133 | ||
134 | case MODE_SERVER: /* server */ | |
135 | fputs(" server", stdout); | |
136 | break; | |
137 | ||
138 | case MODE_BROADCAST: /* broadcast */ | |
139 | fputs(" bcast", stdout); | |
140 | break; | |
141 | ||
142 | case MODE_RES1: /* reserved */ | |
143 | fputs(" res1", stdout); | |
144 | break; | |
145 | ||
146 | case MODE_RES2: /* reserved */ | |
147 | fputs(" res2", stdout); | |
148 | break; | |
149 | ||
150 | } | |
151 | ||
152 | TCHECK(bp->stratum); | |
153 | printf(" strat %d", bp->stratum); | |
154 | ||
155 | TCHECK(bp->ppoll); | |
156 | printf(" poll %d", bp->ppoll); | |
157 | ||
158 | /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */ | |
159 | TCHECK2(bp->distance, 0); | |
160 | printf(" prec %d", bp->precision); | |
161 | ||
162 | if (!vflag) | |
163 | return; | |
164 | ||
165 | TCHECK(bp->distance); | |
166 | fputs(" dist ", stdout); | |
167 | p_sfix(&bp->distance); | |
168 | ||
169 | TCHECK(bp->dispersion); | |
170 | fputs(" disp ", stdout); | |
171 | p_sfix(&bp->dispersion); | |
172 | ||
173 | TCHECK(bp->refid); | |
174 | fputs(" ref ", stdout); | |
175 | /* Interpretation depends on stratum */ | |
176 | switch (bp->stratum) { | |
177 | ||
178 | case UNSPECIFIED: | |
179 | printf("(unspec)"); | |
180 | break; | |
181 | ||
182 | case PRIM_REF: | |
183 | strncpy(rclock, (char *)&(bp->refid), 4); | |
184 | rclock[4] = '\0'; | |
185 | fputs(rclock, stdout); | |
186 | break; | |
187 | ||
188 | case INFO_QUERY: | |
189 | printf("%s INFO_QUERY", ipaddr_string(&(bp->refid))); | |
190 | /* this doesn't have more content */ | |
191 | return; | |
192 | ||
193 | case INFO_REPLY: | |
194 | printf("%s INFO_REPLY", ipaddr_string(&(bp->refid))); | |
195 | /* this is too complex to be worth printing */ | |
196 | return; | |
197 | ||
198 | default: | |
199 | printf("%s", ipaddr_string(&(bp->refid))); | |
200 | break; | |
201 | } | |
202 | ||
203 | TCHECK(bp->reftime); | |
204 | putchar('@'); | |
205 | p_ntp_time(&(bp->reftime)); | |
206 | ||
207 | TCHECK(bp->org); | |
208 | fputs(" orig ", stdout); | |
209 | p_ntp_time(&(bp->org)); | |
210 | ||
211 | TCHECK(bp->rec); | |
212 | fputs(" rec ", stdout); | |
213 | p_ntp_delta(&(bp->org), &(bp->rec)); | |
214 | ||
215 | TCHECK(bp->xmt); | |
216 | fputs(" xmt ", stdout); | |
217 | p_ntp_delta(&(bp->org), &(bp->xmt)); | |
218 | ||
219 | return; | |
220 | ||
221 | trunc: | |
222 | fputs(" [|ntp]", stdout); | |
223 | } | |
224 | ||
225 | static void | |
226 | p_sfix(register const struct s_fixedpt *sfp) | |
227 | { | |
228 | register int i; | |
229 | register int f; | |
230 | register float ff; | |
231 | ||
232 | i = ntohs(sfp->int_part); | |
233 | f = ntohs(sfp->fraction); | |
234 | ff = f / 65536.0; /* shift radix point by 16 bits */ | |
235 | f = ff * 1000000.0; /* Treat fraction as parts per million */ | |
236 | printf("%d.%06d", i, f); | |
237 | } | |
238 | ||
239 | #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */ | |
240 | ||
241 | static void | |
242 | p_ntp_time(register const struct l_fixedpt *lfp) | |
243 | { | |
244 | register int32_t i; | |
245 | register u_int32_t uf; | |
246 | register u_int32_t f; | |
247 | register float ff; | |
248 | ||
249 | i = ntohl(lfp->int_part); | |
250 | uf = ntohl(lfp->fraction); | |
251 | ff = uf; | |
252 | if (ff < 0.0) /* some compilers are buggy */ | |
253 | ff += FMAXINT; | |
254 | ff = ff / FMAXINT; /* shift radix point by 32 bits */ | |
255 | f = ff * 1000000000.0; /* treat fraction as parts per billion */ | |
256 | printf("%u.%09d", i, f); | |
257 | } | |
258 | ||
259 | /* Prints time difference between *lfp and *olfp */ | |
260 | static void | |
261 | p_ntp_delta(register const struct l_fixedpt *olfp, | |
262 | register const struct l_fixedpt *lfp) | |
263 | { | |
264 | register int32_t i; | |
265 | register u_int32_t uf; | |
266 | register u_int32_t ouf; | |
267 | register u_int32_t f; | |
268 | register float ff; | |
269 | int signbit; | |
270 | ||
271 | i = ntohl(lfp->int_part) - ntohl(olfp->int_part); | |
272 | ||
273 | uf = ntohl(lfp->fraction); | |
274 | ouf = ntohl(olfp->fraction); | |
275 | ||
276 | if (i > 0) { /* new is definitely greater than old */ | |
277 | signbit = 0; | |
278 | f = uf - ouf; | |
279 | if (ouf > uf) /* must borrow from high-order bits */ | |
280 | i -= 1; | |
281 | } else if (i < 0) { /* new is definitely less than old */ | |
282 | signbit = 1; | |
283 | f = ouf - uf; | |
284 | if (uf > ouf) /* must carry into the high-order bits */ | |
285 | i += 1; | |
286 | i = -i; | |
287 | } else { /* int_part is zero */ | |
288 | if (uf > ouf) { | |
289 | signbit = 0; | |
290 | f = uf - ouf; | |
291 | } else { | |
292 | signbit = 1; | |
293 | f = ouf - uf; | |
294 | } | |
295 | } | |
296 | ||
297 | ff = f; | |
298 | if (ff < 0.0) /* some compilers are buggy */ | |
299 | ff += FMAXINT; | |
300 | ff = ff / FMAXINT; /* shift radix point by 32 bits */ | |
301 | f = ff * 1000000000.0; /* treat fraction as parts per billion */ | |
302 | if (signbit) | |
303 | putchar('-'); | |
304 | else | |
305 | putchar('+'); | |
306 | printf("%d.%09d", i, f); | |
307 | } |