]>
The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
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: /tcpdump/master/tcpdump/print-ntp.c,v 1.29 2000-09-23 08:03:38 guy Exp $ (LBL)";
35 #include <sys/param.h>
37 #include <sys/socket.h>
43 #include <netinet/in.h>
49 #include "interface.h"
50 #include "addrtoname.h"
52 #undef MODEMASK /* Solaris sucks */
56 static void p_sfix(const struct s_fixedpt
*);
57 static void p_ntp_time(const struct l_fixedpt
*);
58 static void p_ntp_delta(const struct l_fixedpt
*, const struct l_fixedpt
*);
64 ntp_print(register const u_char
*cp
, u_int length
)
66 register const struct ntpdata
*bp
;
67 int mode
, version
, leapind
;
68 static char rclock
[5];
70 bp
= (struct ntpdata
*)cp
;
71 /* Note funny sized packets */
72 if (length
!= sizeof(struct ntpdata
))
73 (void)printf(" [len=%d]", length
);
77 version
= (int)(bp
->status
& VERSIONMASK
) >> 3;
78 printf(" v%d", version
);
80 leapind
= bp
->status
& LEAPMASK
;
87 fputs(" +1s", stdout
);
91 fputs(" -1s", stdout
);
95 mode
= bp
->status
& MODEMASK
;
98 case MODE_UNSPEC
: /* unspecified */
99 fputs(" unspec", stdout
);
102 case MODE_SYM_ACT
: /* symmetric active */
103 fputs(" sym_act", stdout
);
106 case MODE_SYM_PAS
: /* symmetric passive */
107 fputs(" sym_pas", stdout
);
110 case MODE_CLIENT
: /* client */
111 fputs(" client", stdout
);
114 case MODE_SERVER
: /* server */
115 fputs(" server", stdout
);
118 case MODE_BROADCAST
: /* broadcast */
119 fputs(" bcast", stdout
);
122 case MODE_RES1
: /* reserved */
123 fputs(" res1", stdout
);
126 case MODE_RES2
: /* reserved */
127 fputs(" res2", stdout
);
133 printf(" strat %d", bp
->stratum
);
136 printf(" poll %d", bp
->ppoll
);
138 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
139 TCHECK2(bp
->distance
, 0);
140 printf(" prec %d", bp
->precision
);
145 TCHECK(bp
->distance
);
146 fputs(" dist ", stdout
);
147 p_sfix(&bp
->distance
);
149 TCHECK(bp
->dispersion
);
150 fputs(" disp ", stdout
);
151 p_sfix(&bp
->dispersion
);
154 fputs(" ref ", stdout
);
155 /* Interpretation depends on stratum */
156 switch (bp
->stratum
) {
163 strncpy(rclock
, (char *)&(bp
->refid
), 4);
165 fputs(rclock
, stdout
);
169 printf("%s INFO_QUERY", ipaddr_string(&(bp
->refid
)));
170 /* this doesn't have more content */
174 printf("%s INFO_REPLY", ipaddr_string(&(bp
->refid
)));
175 /* this is too complex to be worth printing */
179 printf("%s", ipaddr_string(&(bp
->refid
)));
185 p_ntp_time(&(bp
->reftime
));
188 fputs(" orig ", stdout
);
189 p_ntp_time(&(bp
->org
));
192 fputs(" rec ", stdout
);
193 p_ntp_delta(&(bp
->org
), &(bp
->rec
));
196 fputs(" xmt ", stdout
);
197 p_ntp_delta(&(bp
->org
), &(bp
->xmt
));
202 fputs(" [|ntp]", stdout
);
206 p_sfix(register const struct s_fixedpt
*sfp
)
212 i
= ntohs(sfp
->int_part
);
213 f
= ntohs(sfp
->fraction
);
214 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
215 f
= ff
* 1000000.0; /* Treat fraction as parts per million */
216 printf("%d.%06d", i
, f
);
219 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
222 p_ntp_time(register const struct l_fixedpt
*lfp
)
225 register u_int32_t uf
;
226 register u_int32_t f
;
229 i
= ntohl(lfp
->int_part
);
230 uf
= ntohl(lfp
->fraction
);
232 if (ff
< 0.0) /* some compilers are buggy */
234 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
235 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
236 printf("%u.%09d", i
, f
);
239 /* Prints time difference between *lfp and *olfp */
241 p_ntp_delta(register const struct l_fixedpt
*olfp
,
242 register const struct l_fixedpt
*lfp
)
245 register u_int32_t uf
;
246 register u_int32_t ouf
;
247 register u_int32_t f
;
251 i
= ntohl(lfp
->int_part
) - ntohl(olfp
->int_part
);
253 uf
= ntohl(lfp
->fraction
);
254 ouf
= ntohl(olfp
->fraction
);
256 if (i
> 0) { /* new is definitely greater than old */
259 if (ouf
> uf
) /* must borrow from high-order bits */
261 } else if (i
< 0) { /* new is definitely less than old */
264 if (uf
> ouf
) /* must carry into the high-order bits */
267 } else { /* int_part is zero */
278 if (ff
< 0.0) /* some compilers are buggy */
280 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
281 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
286 printf("%d.%09d", i
, f
);