]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-ntp.c
This commit was generated by cvs2svn to compensate for changes in r57416,
[FreeBSD/FreeBSD.git] / contrib / tcpdump / print-ntp.c
1 /*
2  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *      The Regents of the University of California.  All rights reserved.
4  *
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
16  * written permission.
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.
20  *
21  * Format and print ntp packets.
22  *      By Jeffrey Mogul/DECWRL
23  *      loosely based on print-bootp.c
24  *
25  * $FreeBSD$
26  */
27
28 #ifndef lint
29 static const char rcsid[] =
30     "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.27 1999/11/21 09:36:57 fenner Exp $ (LBL)";
31 #endif
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/socket.h>
40
41 #if __STDC__
42 struct mbuf;
43 struct rtentry;
44 #endif
45 #include <net/if.h>
46
47 #include <netinet/in.h>
48 #include <net/ethernet.h>
49
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <string.h>
53
54 #include "interface.h"
55 #include "addrtoname.h"
56 #ifdef MODEMASK
57 #undef MODEMASK                                 /* Solaris sucks */
58 #endif
59 #include "ntp.h"
60
61 static void p_sfix(const struct s_fixedpt *);
62 static void p_ntp_time(const struct l_fixedpt *);
63 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
64
65 /*
66  * Print ntp requests
67  */
68 void
69 ntp_print(register const u_char *cp, u_int length)
70 {
71         register const struct ntpdata *bp;
72         int mode, version, leapind;
73         static char rclock[5];
74
75         bp = (struct ntpdata *)cp;
76         /* Note funny sized packets */
77         if (length != sizeof(struct ntpdata))
78                 (void)printf(" [len=%d]", length);
79
80         TCHECK(bp->status);
81
82         version = (int)(bp->status & VERSIONMASK) >> 3;
83         printf(" v%d", version);
84
85         leapind = bp->status & LEAPMASK;
86         switch (leapind) {
87
88         case NO_WARNING:
89                 break;
90
91         case PLUS_SEC:
92                 fputs(" +1s", stdout);
93                 break;
94
95         case MINUS_SEC:
96                 fputs(" -1s", stdout);
97                 break;
98         }
99
100         mode = bp->status & MODEMASK;
101         switch (mode) {
102
103         case MODE_UNSPEC:       /* unspecified */
104                 fputs(" unspec", stdout);
105                 break;
106
107         case MODE_SYM_ACT:      /* symmetric active */
108                 fputs(" sym_act", stdout);
109                 break;
110
111         case MODE_SYM_PAS:      /* symmetric passive */
112                 fputs(" sym_pas", stdout);
113                 break;
114
115         case MODE_CLIENT:       /* client */
116                 fputs(" client", stdout);
117                 break;
118
119         case MODE_SERVER:       /* server */
120                 fputs(" server", stdout);
121                 break;
122
123         case MODE_BROADCAST:    /* broadcast */
124                 fputs(" bcast", stdout);
125                 break;
126
127         case MODE_RES1:         /* reserved */
128                 fputs(" res1", stdout);
129                 break;
130
131         case MODE_RES2:         /* reserved */
132                 fputs(" res2", stdout);
133                 break;
134
135         }
136
137         TCHECK(bp->stratum);
138         printf(" strat %d", bp->stratum);
139
140         TCHECK(bp->ppoll);
141         printf(" poll %d", bp->ppoll);
142
143         /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
144         TCHECK2(bp->distance, 0);
145         printf(" prec %d", bp->precision);
146
147         if (!vflag)
148                 return;
149
150         TCHECK(bp->distance);
151         fputs(" dist ", stdout);
152         p_sfix(&bp->distance);
153
154         TCHECK(bp->dispersion);
155         fputs(" disp ", stdout);
156         p_sfix(&bp->dispersion);
157
158         TCHECK(bp->refid);
159         fputs(" ref ", stdout);
160         /* Interpretation depends on stratum */
161         switch (bp->stratum) {
162
163         case UNSPECIFIED:
164                 printf("(unspec)");
165                 break;
166
167         case PRIM_REF:
168                 strncpy(rclock, (char *)&(bp->refid), 4);
169                 rclock[4] = '\0';
170                 fputs(rclock, stdout);
171                 break;
172
173         case INFO_QUERY:
174                 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
175                 /* this doesn't have more content */
176                 return;
177
178         case INFO_REPLY:
179                 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
180                 /* this is too complex to be worth printing */
181                 return;
182
183         default:
184                 printf("%s", ipaddr_string(&(bp->refid)));
185                 break;
186         }
187
188         TCHECK(bp->reftime);
189         putchar('@');
190         p_ntp_time(&(bp->reftime));
191
192         TCHECK(bp->org);
193         fputs(" orig ", stdout);
194         p_ntp_time(&(bp->org));
195
196         TCHECK(bp->rec);
197         fputs(" rec ", stdout);
198         p_ntp_delta(&(bp->org), &(bp->rec));
199
200         TCHECK(bp->xmt);
201         fputs(" xmt ", stdout);
202         p_ntp_delta(&(bp->org), &(bp->xmt));
203
204         return;
205
206 trunc:
207         fputs(" [|ntp]", stdout);
208 }
209
210 static void
211 p_sfix(register const struct s_fixedpt *sfp)
212 {
213         register int i;
214         register int f;
215         register float ff;
216
217         i = ntohs(sfp->int_part);
218         f = ntohs(sfp->fraction);
219         ff = f / 65536.0;       /* shift radix point by 16 bits */
220         f = ff * 1000000.0;     /* Treat fraction as parts per million */
221         printf("%d.%06d", i, f);
222 }
223
224 #define FMAXINT (4294967296.0)  /* floating point rep. of MAXINT */
225
226 static void
227 p_ntp_time(register const struct l_fixedpt *lfp)
228 {
229         register int32_t i;
230         register u_int32_t uf;
231         register u_int32_t f;
232         register float ff;
233
234         i = ntohl(lfp->int_part);
235         uf = ntohl(lfp->fraction);
236         ff = uf;
237         if (ff < 0.0)           /* some compilers are buggy */
238                 ff += FMAXINT;
239         ff = ff / FMAXINT;      /* shift radix point by 32 bits */
240         f = ff * 1000000000.0;  /* treat fraction as parts per billion */
241         printf("%u.%09d", i, f);
242 }
243
244 /* Prints time difference between *lfp and *olfp */
245 static void
246 p_ntp_delta(register const struct l_fixedpt *olfp,
247             register const struct l_fixedpt *lfp)
248 {
249         register int32_t i;
250         register u_int32_t uf;
251         register u_int32_t ouf;
252         register u_int32_t f;
253         register float ff;
254         int signbit;
255
256         i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
257
258         uf = ntohl(lfp->fraction);
259         ouf = ntohl(olfp->fraction);
260
261         if (i > 0) {            /* new is definitely greater than old */
262                 signbit = 0;
263                 f = uf - ouf;
264                 if (ouf > uf)   /* must borrow from high-order bits */
265                         i -= 1;
266         } else if (i < 0) {     /* new is definitely less than old */
267                 signbit = 1;
268                 f = ouf - uf;
269                 if (uf > ouf)   /* must carry into the high-order bits */
270                         i += 1;
271                 i = -i;
272         } else {                /* int_part is zero */
273                 if (uf > ouf) {
274                         signbit = 0;
275                         f = uf - ouf;
276                 } else {
277                         signbit = 1;
278                         f = ouf - uf;
279                 }
280         }
281
282         ff = f;
283         if (ff < 0.0)           /* some compilers are buggy */
284                 ff += FMAXINT;
285         ff = ff / FMAXINT;      /* shift radix point by 32 bits */
286         f = ff * 1000000000.0;  /* treat fraction as parts per billion */
287         if (signbit)
288                 putchar('-');
289         else
290                 putchar('+');
291         printf("%d.%09d", i, f);
292 }