2 * Copyright (c) 1988-1990 The Regents of the University of California.
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
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.
21 * Format and print ntp packets.
22 * By Jeffrey Mogul/DECWRL
23 * loosely based on print-bootp.c
28 "@(#) $Header: print-ntp.c,v 1.7 92/01/04 01:45:16 leres Exp $ (LBL)";
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
42 #include "interface.h"
43 #include "addrtoname.h"
51 register struct ntpdata *bp;
55 int mode, version, leapind;
56 static char rclock[5];
58 #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
60 /* Note funny sized packets */
61 if (length != sizeof(struct ntpdata))
62 (void)printf(" [len=%d]", length);
64 /* 'ep' points to the end of avaible data. */
65 ep = (u_char *)snapend;
67 TCHECK(bp->status, sizeof(bp->status));
69 version = (bp->status & VERSIONMASK) >> 3;
70 printf(" v%d", version);
72 leapind = bp->status & LEAPMASK;
79 fputs(" +1s", stdout);
83 fputs(" -1s", stdout);
87 mode = bp->status & MODEMASK;
90 case MODE_UNSPEC: /* unspecified */
91 fputs(" unspec", stdout);
94 case MODE_SYM_ACT: /* symmetric active */
95 fputs(" sym_act", stdout);
98 case MODE_SYM_PAS: /* symmetric passive */
99 fputs(" sym_pas", stdout);
102 case MODE_CLIENT: /* client */
103 fputs(" client", stdout);
106 case MODE_SERVER: /* server */
107 fputs(" server", stdout);
110 case MODE_BROADCAST: /* broadcast */
111 fputs(" bcast", stdout);
114 case MODE_RES1: /* reserved */
115 fputs(" res1", stdout);
118 case MODE_RES2: /* reserved */
119 fputs(" res2", stdout);
124 TCHECK(bp->stratum, sizeof(bp->stratum));
125 printf(" strat %d", bp->stratum);
127 TCHECK(bp->ppoll, sizeof(bp->ppoll));
128 printf(" poll %d", bp->ppoll);
130 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
131 TCHECK(bp->distance, 0);
132 printf(" prec %d", bp->precision);
137 TCHECK(bp->distance, sizeof(bp->distance));
138 fputs(" dist ", stdout);
139 p_sfix(&bp->distance);
141 TCHECK(bp->dispersion, sizeof(bp->dispersion));
142 fputs(" disp ", stdout);
143 p_sfix(&bp->dispersion);
145 TCHECK(bp->refid, sizeof(bp->refid));
146 fputs(" ref ", stdout);
147 /* Interpretation depends on stratum */
148 switch (bp->stratum) {
152 strncpy(rclock, (char *)&(bp->refid), 4);
154 fputs(rclock, stdout);
158 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
159 /* this doesn't have more content */
163 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
164 /* this is too complex to be worth printing */
168 printf("%s", ipaddr_string(&(bp->refid)));
172 TCHECK(bp->reftime, sizeof(bp->reftime));
174 p_ntp_time(&(bp->reftime));
176 TCHECK(bp->org, sizeof(bp->org));
177 fputs(" orig ", stdout);
178 p_ntp_time(&(bp->org));
180 TCHECK(bp->rec, sizeof(bp->rec));
181 fputs(" rec ", stdout);
182 p_ntp_delta(&(bp->org), &(bp->rec));
184 TCHECK(bp->xmt, sizeof(bp->xmt));
185 fputs(" xmt ", stdout);
186 p_ntp_delta(&(bp->org), &(bp->xmt));
191 fputs(" [|ntp]", stdout);
196 register struct s_fixedpt *sfp;
202 i = ntohs(sfp->int_part);
203 f = ntohs(sfp->fraction);
204 ff = f / 65536.0; /* shift radix point by 16 bits */
205 f = ff * 1000000.0; /* Treat fraction as parts per million */
206 printf("%d.%06d", i, f);
209 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
212 register struct l_fixedpt *lfp;
215 register unsigned long uf;
216 register unsigned long f;
219 i = ntohl(lfp->int_part);
220 uf = ntohl(lfp->fraction);
222 if (ff < 0.0) /* some compilers are buggy */
224 ff = ff / FMAXINT; /* shift radix point by 32 bits */
225 f = ff * 1000000000.0; /* treat fraction as parts per billion */
226 printf("%lu.%09d", i, f);
229 /* Prints time difference between *lfp and *olfp */
230 p_ntp_delta(olfp, lfp)
231 register struct l_fixedpt *olfp;
232 register struct l_fixedpt *lfp;
235 register unsigned long uf;
236 register unsigned long ouf;
237 register unsigned long f;
241 i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
243 uf = ntohl(lfp->fraction);
244 ouf = ntohl(olfp->fraction);
246 if (i > 0) { /* new is definitely greater than old */
249 if (ouf > uf) /* must borrow from high-order bits */
251 } else if (i < 0) { /* new is definitely less than old */
254 if (uf > ouf) /* must carry into the high-order bits */
257 } else { /* int_part is zero */
268 if (ff < 0.0) /* some compilers are buggy */
270 ff = ff / FMAXINT; /* shift radix point by 32 bits */
271 f = ff * 1000000000.0; /* treat fraction as parts per billion */
276 printf("%d.%09d", i, f);