]>
git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-ntp.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Format and print ntp packets.
45 * By Jeffrey Mogul/DECWRL
46 * loosely based on print-bootp.c
50 static const char rcsid
[] =
51 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-ntp.c,v 1.1.1.1 1999/05/02 03:58:34 wsanchez Exp $ (LBL)";
54 #include <sys/param.h>
56 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
71 #include "interface.h"
72 #include "addrtoname.h"
73 #undef MODEMASK /* Solaris sucks */
76 static void p_sfix(const struct s_fixedpt
*);
77 static void p_ntp_time(const struct l_fixedpt
*);
78 static void p_ntp_delta(const struct l_fixedpt
*, const struct l_fixedpt
*);
84 ntp_print(register const u_char
*cp
, u_int length
)
86 register const struct ntpdata
*bp
;
87 int mode
, version
, leapind
;
88 static char rclock
[5];
90 bp
= (struct ntpdata
*)cp
;
91 /* Note funny sized packets */
92 if (length
!= sizeof(struct ntpdata
))
93 (void)printf(" [len=%d]", length
);
97 version
= (int)(bp
->status
& VERSIONMASK
) >> 3;
98 printf(" v%d", version
);
100 leapind
= bp
->status
& LEAPMASK
;
107 fputs(" +1s", stdout
);
111 fputs(" -1s", stdout
);
115 mode
= bp
->status
& MODEMASK
;
118 case MODE_UNSPEC
: /* unspecified */
119 fputs(" unspec", stdout
);
122 case MODE_SYM_ACT
: /* symmetric active */
123 fputs(" sym_act", stdout
);
126 case MODE_SYM_PAS
: /* symmetric passive */
127 fputs(" sym_pas", stdout
);
130 case MODE_CLIENT
: /* client */
131 fputs(" client", stdout
);
134 case MODE_SERVER
: /* server */
135 fputs(" server", stdout
);
138 case MODE_BROADCAST
: /* broadcast */
139 fputs(" bcast", stdout
);
142 case MODE_RES1
: /* reserved */
143 fputs(" res1", stdout
);
146 case MODE_RES2
: /* reserved */
147 fputs(" res2", stdout
);
153 printf(" strat %d", bp
->stratum
);
156 printf(" poll %d", bp
->ppoll
);
158 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
159 TCHECK2(bp
->distance
, 0);
160 printf(" prec %d", bp
->precision
);
165 TCHECK(bp
->distance
);
166 fputs(" dist ", stdout
);
167 p_sfix(&bp
->distance
);
169 TCHECK(bp
->dispersion
);
170 fputs(" disp ", stdout
);
171 p_sfix(&bp
->dispersion
);
174 fputs(" ref ", stdout
);
175 /* Interpretation depends on stratum */
176 switch (bp
->stratum
) {
183 strncpy(rclock
, (char *)&(bp
->refid
), 4);
185 fputs(rclock
, stdout
);
189 printf("%s INFO_QUERY", ipaddr_string(&(bp
->refid
)));
190 /* this doesn't have more content */
194 printf("%s INFO_REPLY", ipaddr_string(&(bp
->refid
)));
195 /* this is too complex to be worth printing */
199 printf("%s", ipaddr_string(&(bp
->refid
)));
205 p_ntp_time(&(bp
->reftime
));
208 fputs(" orig ", stdout
);
209 p_ntp_time(&(bp
->org
));
212 fputs(" rec ", stdout
);
213 p_ntp_delta(&(bp
->org
), &(bp
->rec
));
216 fputs(" xmt ", stdout
);
217 p_ntp_delta(&(bp
->org
), &(bp
->xmt
));
222 fputs(" [|ntp]", stdout
);
226 p_sfix(register const struct s_fixedpt
*sfp
)
232 i
= ntohs(sfp
->int_part
);
233 f
= ntohs(sfp
->fraction
);
234 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
235 f
= ff
* 1000000.0; /* Treat fraction as parts per million */
236 printf("%d.%06d", i
, f
);
239 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
242 p_ntp_time(register const struct l_fixedpt
*lfp
)
245 register u_int32_t uf
;
246 register u_int32_t f
;
249 i
= ntohl(lfp
->int_part
);
250 uf
= ntohl(lfp
->fraction
);
252 if (ff
< 0.0) /* some compilers are buggy */
254 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
255 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
256 printf("%u.%09d", i
, f
);
259 /* Prints time difference between *lfp and *olfp */
261 p_ntp_delta(register const struct l_fixedpt
*olfp
,
262 register const struct l_fixedpt
*lfp
)
265 register u_int32_t uf
;
266 register u_int32_t ouf
;
267 register u_int32_t f
;
271 i
= ntohl(lfp
->int_part
) - ntohl(olfp
->int_part
);
273 uf
= ntohl(lfp
->fraction
);
274 ouf
= ntohl(olfp
->fraction
);
276 if (i
> 0) { /* new is definitely greater than old */
279 if (ouf
> uf
) /* must borrow from high-order bits */
281 } else if (i
< 0) { /* new is definitely less than old */
284 if (uf
> ouf
) /* must carry into the high-order bits */
287 } else { /* int_part is zero */
298 if (ff
< 0.0) /* some compilers are buggy */
300 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
301 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
306 printf("%d.%09d", i
, f
);