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