]>
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.28 2000-07-01 03:39:07 assar Exp $ (LBL)";
35 #include <sys/param.h>
37 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
50 #include "interface.h"
51 #include "addrtoname.h"
53 #undef MODEMASK /* Solaris sucks */
57 static void p_sfix(const struct s_fixedpt
*);
58 static void p_ntp_time(const struct l_fixedpt
*);
59 static void p_ntp_delta(const struct l_fixedpt
*, const struct l_fixedpt
*);
65 ntp_print(register const u_char
*cp
, u_int length
)
67 register const struct ntpdata
*bp
;
68 int mode
, version
, leapind
;
69 static char rclock
[5];
71 bp
= (struct ntpdata
*)cp
;
72 /* Note funny sized packets */
73 if (length
!= sizeof(struct ntpdata
))
74 (void)printf(" [len=%d]", length
);
78 version
= (int)(bp
->status
& VERSIONMASK
) >> 3;
79 printf(" v%d", version
);
81 leapind
= bp
->status
& LEAPMASK
;
88 fputs(" +1s", stdout
);
92 fputs(" -1s", stdout
);
96 mode
= bp
->status
& MODEMASK
;
99 case MODE_UNSPEC
: /* unspecified */
100 fputs(" unspec", stdout
);
103 case MODE_SYM_ACT
: /* symmetric active */
104 fputs(" sym_act", stdout
);
107 case MODE_SYM_PAS
: /* symmetric passive */
108 fputs(" sym_pas", stdout
);
111 case MODE_CLIENT
: /* client */
112 fputs(" client", stdout
);
115 case MODE_SERVER
: /* server */
116 fputs(" server", stdout
);
119 case MODE_BROADCAST
: /* broadcast */
120 fputs(" bcast", stdout
);
123 case MODE_RES1
: /* reserved */
124 fputs(" res1", stdout
);
127 case MODE_RES2
: /* reserved */
128 fputs(" res2", stdout
);
134 printf(" strat %d", bp
->stratum
);
137 printf(" poll %d", bp
->ppoll
);
139 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
140 TCHECK2(bp
->distance
, 0);
141 printf(" prec %d", bp
->precision
);
146 TCHECK(bp
->distance
);
147 fputs(" dist ", stdout
);
148 p_sfix(&bp
->distance
);
150 TCHECK(bp
->dispersion
);
151 fputs(" disp ", stdout
);
152 p_sfix(&bp
->dispersion
);
155 fputs(" ref ", stdout
);
156 /* Interpretation depends on stratum */
157 switch (bp
->stratum
) {
164 strncpy(rclock
, (char *)&(bp
->refid
), 4);
166 fputs(rclock
, stdout
);
170 printf("%s INFO_QUERY", ipaddr_string(&(bp
->refid
)));
171 /* this doesn't have more content */
175 printf("%s INFO_REPLY", ipaddr_string(&(bp
->refid
)));
176 /* this is too complex to be worth printing */
180 printf("%s", ipaddr_string(&(bp
->refid
)));
186 p_ntp_time(&(bp
->reftime
));
189 fputs(" orig ", stdout
);
190 p_ntp_time(&(bp
->org
));
193 fputs(" rec ", stdout
);
194 p_ntp_delta(&(bp
->org
), &(bp
->rec
));
197 fputs(" xmt ", stdout
);
198 p_ntp_delta(&(bp
->org
), &(bp
->xmt
));
203 fputs(" [|ntp]", stdout
);
207 p_sfix(register const struct s_fixedpt
*sfp
)
213 i
= ntohs(sfp
->int_part
);
214 f
= ntohs(sfp
->fraction
);
215 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
216 f
= ff
* 1000000.0; /* Treat fraction as parts per million */
217 printf("%d.%06d", i
, f
);
220 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
223 p_ntp_time(register const struct l_fixedpt
*lfp
)
226 register u_int32_t uf
;
227 register u_int32_t f
;
230 i
= ntohl(lfp
->int_part
);
231 uf
= ntohl(lfp
->fraction
);
233 if (ff
< 0.0) /* some compilers are buggy */
235 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
236 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
237 printf("%u.%09d", i
, f
);
240 /* Prints time difference between *lfp and *olfp */
242 p_ntp_delta(register const struct l_fixedpt
*olfp
,
243 register const struct l_fixedpt
*lfp
)
246 register u_int32_t uf
;
247 register u_int32_t ouf
;
248 register u_int32_t f
;
252 i
= ntohl(lfp
->int_part
) - ntohl(olfp
->int_part
);
254 uf
= ntohl(lfp
->fraction
);
255 ouf
= ntohl(olfp
->fraction
);
257 if (i
> 0) { /* new is definitely greater than old */
260 if (ouf
> uf
) /* must borrow from high-order bits */
262 } else if (i
< 0) { /* new is definitely less than old */
265 if (uf
> ouf
) /* must carry into the high-order bits */
268 } else { /* int_part is zero */
279 if (ff
< 0.0) /* some compilers are buggy */
281 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
282 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
287 printf("%d.%09d", i
, f
);