xref: /openbsd-src/usr.sbin/tcpdump/print-ntp.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: print-ntp.c,v 1.11 2002/02/19 19:39:40 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * Format and print ntp packets.
24  *	By Jeffrey Mogul/DECWRL
25  *	loosely based on print-bootp.c
26  */
27 
28 #ifndef lint
29 static const char rcsid[] =
30     "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-ntp.c,v 1.11 2002/02/19 19:39:40 millert Exp $ (LBL)";
31 #endif
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 
37 struct mbuf;
38 struct rtentry;
39 #include <net/if.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/if_ether.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
51 #undef MODEMASK					/* Solaris sucks */
52 #endif
53 #include "ntp.h"
54 
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 *);
58 
59 /*
60  * Print ntp requests
61  */
62 void
63 ntp_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 		strlcpy(rclock, (char *)&(bp->refid), sizeof(rclock));
163 		fputs(rclock, stdout);
164 		break;
165 
166 	case INFO_QUERY:
167 		printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
168 		/* this doesn't have more content */
169 		return;
170 
171 	case INFO_REPLY:
172 		printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
173 		/* this is too complex to be worth printing */
174 		return;
175 
176 	default:
177 		printf("%s", ipaddr_string(&(bp->refid)));
178 		break;
179 	}
180 
181 	TCHECK(bp->reftime);
182 	putchar('@');
183 	p_ntp_time(&(bp->reftime));
184 
185 	TCHECK(bp->org);
186 	fputs(" orig ", stdout);
187 	p_ntp_time(&(bp->org));
188 
189 	TCHECK(bp->rec);
190 	fputs(" rec ", stdout);
191 	p_ntp_delta(&(bp->org), &(bp->rec));
192 
193 	TCHECK(bp->xmt);
194 	fputs(" xmt ", stdout);
195 	p_ntp_delta(&(bp->org), &(bp->xmt));
196 
197 	return;
198 
199 trunc:
200 	fputs(" [|ntp]", stdout);
201 }
202 
203 static void
204 p_sfix(register const struct s_fixedpt *sfp)
205 {
206 	register int i;
207 	register int f;
208 	register float ff;
209 
210 	i = ntohs(sfp->int_part);
211 	f = ntohs(sfp->fraction);
212 	ff = f / 65536.0;	/* shift radix point by 16 bits */
213 	f = ff * 1000000.0;	/* Treat fraction as parts per million */
214 	printf("%d.%06d", i, f);
215 }
216 
217 #define	FMAXINT	(4294967296.0)	/* floating point rep. of MAXINT */
218 
219 static void
220 p_ntp_time(register const struct l_fixedpt *lfp)
221 {
222 	register int32_t i;
223 	register u_int32_t uf;
224 	register u_int32_t f;
225 	register float ff;
226 
227 	i = ntohl(lfp->int_part);
228 	uf = ntohl(lfp->fraction);
229 	ff = uf;
230 	if (ff < 0.0)		/* some compilers are buggy */
231 		ff += FMAXINT;
232 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
233 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
234 	printf("%u.%09d", i, f);
235 }
236 
237 /* Prints time difference between *lfp and *olfp */
238 static void
239 p_ntp_delta(register const struct l_fixedpt *olfp,
240 	    register const struct l_fixedpt *lfp)
241 {
242 	register int32_t i;
243 	register u_int32_t uf;
244 	register u_int32_t ouf;
245 	register u_int32_t f;
246 	register float ff;
247 	int signbit;
248 
249 	i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
250 
251 	uf = ntohl(lfp->fraction);
252 	ouf = ntohl(olfp->fraction);
253 
254 	if (i > 0) {		/* new is definitely greater than old */
255 		signbit = 0;
256 		f = uf - ouf;
257 		if (ouf > uf)	/* must borrow from high-order bits */
258 			i -= 1;
259 	} else if (i < 0) {	/* new is definitely less than old */
260 		signbit = 1;
261 		f = ouf - uf;
262 		if (uf > ouf)	/* must carry into the high-order bits */
263 			i += 1;
264 		i = -i;
265 	} else {		/* int_part is zero */
266 		if (uf > ouf) {
267 			signbit = 0;
268 			f = uf - ouf;
269 		} else {
270 			signbit = 1;
271 			f = ouf - uf;
272 		}
273 	}
274 
275 	ff = f;
276 	if (ff < 0.0)		/* some compilers are buggy */
277 		ff += FMAXINT;
278 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
279 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
280 	if (signbit)
281 		putchar('-');
282 	else
283 		putchar('+');
284 	printf("%d.%09d", i, f);
285 }
286