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