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