2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
27 static const char rcsid[] =
28 "@(#) $Header: print-ntp.c,v 1.26 97/06/13 12:56:37 leres Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <net/ethernet.h>
48 #include "interface.h"
49 #include "addrtoname.h"
51 #undef MODEMASK /* Solaris sucks */
55 static void p_sfix(const struct s_fixedpt *);
56 static void p_ntp_time(const struct l_fixedpt *);
57 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
63 ntp_print(register const u_char *cp, u_int length)
65 register const struct ntpdata *bp;
66 int mode, version, leapind;
67 static char rclock[5];
69 bp = (struct ntpdata *)cp;
70 /* Note funny sized packets */
71 if (length != sizeof(struct ntpdata))
72 (void)printf(" [len=%d]", length);
76 version = (int)(bp->status & VERSIONMASK) >> 3;
77 printf(" v%d", version);
79 leapind = bp->status & LEAPMASK;
86 fputs(" +1s", stdout);
90 fputs(" -1s", stdout);
94 mode = bp->status & MODEMASK;
97 case MODE_UNSPEC: /* unspecified */
98 fputs(" unspec", stdout);
101 case MODE_SYM_ACT: /* symmetric active */
102 fputs(" sym_act", stdout);
105 case MODE_SYM_PAS: /* symmetric passive */
106 fputs(" sym_pas", stdout);
109 case MODE_CLIENT: /* client */
110 fputs(" client", stdout);
113 case MODE_SERVER: /* server */
114 fputs(" server", stdout);
117 case MODE_BROADCAST: /* broadcast */
118 fputs(" bcast", stdout);
121 case MODE_RES1: /* reserved */
122 fputs(" res1", stdout);
125 case MODE_RES2: /* reserved */
126 fputs(" res2", stdout);
132 printf(" strat %d", bp->stratum);
135 printf(" poll %d", bp->ppoll);
137 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
138 TCHECK2(bp->distance, 0);
139 printf(" prec %d", bp->precision);
144 TCHECK(bp->distance);
145 fputs(" dist ", stdout);
146 p_sfix(&bp->distance);
148 TCHECK(bp->dispersion);
149 fputs(" disp ", stdout);
150 p_sfix(&bp->dispersion);
153 fputs(" ref ", stdout);
154 /* Interpretation depends on stratum */
155 switch (bp->stratum) {
162 strncpy(rclock, (char *)&(bp->refid), 4);
164 fputs(rclock, stdout);
168 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
169 /* this doesn't have more content */
173 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
174 /* this is too complex to be worth printing */
178 printf("%s", ipaddr_string(&(bp->refid)));
184 p_ntp_time(&(bp->reftime));
187 fputs(" orig ", stdout);
188 p_ntp_time(&(bp->org));
191 fputs(" rec ", stdout);
192 p_ntp_delta(&(bp->org), &(bp->rec));
195 fputs(" xmt ", stdout);
196 p_ntp_delta(&(bp->org), &(bp->xmt));
201 fputs(" [|ntp]", stdout);
205 p_sfix(register const struct s_fixedpt *sfp)
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);
218 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
221 p_ntp_time(register const struct l_fixedpt *lfp)
224 register u_int32_t uf;
225 register u_int32_t f;
228 i = ntohl(lfp->int_part);
229 uf = ntohl(lfp->fraction);
231 if (ff < 0.0) /* some compilers are buggy */
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);
238 /* Prints time difference between *lfp and *olfp */
240 p_ntp_delta(register const struct l_fixedpt *olfp,
241 register const struct l_fixedpt *lfp)
244 register u_int32_t uf;
245 register u_int32_t ouf;
246 register u_int32_t f;
250 i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
252 uf = ntohl(lfp->fraction);
253 ouf = ntohl(olfp->fraction);
255 if (i > 0) { /* new is definitely greater than old */
258 if (ouf > uf) /* must borrow from high-order bits */
260 } else if (i < 0) { /* new is definitely less than old */
263 if (uf > ouf) /* must carry into the high-order bits */
266 } else { /* int_part is zero */
277 if (ff < 0.0) /* some compilers are buggy */
279 ff = ff / FMAXINT; /* shift radix point by 32 bits */
280 f = ff * 1000000000.0; /* treat fraction as parts per billion */
285 printf("%d.%09d", i, f);