source: rtems-libbsd/freebsd/contrib/tcpdump/print-ntp.c @ 084d4db

4.11
Last change on this file since 084d4db was 8440506, checked in by Chris Johns <chrisj@…>, on 06/15/15 at 07:42:23

Add tcpdump and libpcap.

  • Update the file builder generator to handle generator specific cflags and includes. The tcpdump and libpcap have localised headers and need specific headers paths to see them. There are also module specific flags and these need to be passed to the lex and yacc generators.
  • Add the tcpdump support.
  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * Format and print ntp packets.
24 *      By Jeffrey Mogul/DECWRL
25 *      loosely based on print-bootp.c
26 *
27 * $FreeBSD$
28 */
29
30#ifndef lint
31static const char rcsid[] _U_ =
32    "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.43 2007-11-30 13:45:10 hannes Exp $ (LBL)";
33#endif
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#include <tcpdump-stdinc.h>
40
41#include <stdio.h>
42#include <string.h>
43#ifdef HAVE_STRFTIME
44#include <time.h>
45#endif
46
47#include "interface.h"
48#include "addrtoname.h"
49#include "extract.h"
50#ifdef MODEMASK
51#undef MODEMASK                                 /* Solaris sucks */
52#endif
53#include "ntp.h"
54
55static void p_sfix(const struct s_fixedpt *);
56static void p_ntp_time(const struct l_fixedpt *);
57static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
58
59static struct tok ntp_mode_values[] = {
60    { MODE_UNSPEC,    "unspecified" },
61    { MODE_SYM_ACT,   "symmetric active" },
62    { MODE_SYM_PAS,   "symmetric passive" },
63    { MODE_CLIENT,    "Client" },
64    { MODE_SERVER,    "Server" },
65    { MODE_BROADCAST, "Broadcast" },
66    { MODE_RES1,      "Reserved" },
67    { MODE_RES2,      "Reserved" },
68    { 0, NULL }
69};
70
71static struct tok ntp_leapind_values[] = {
72    { NO_WARNING,     "" },
73    { PLUS_SEC,       "+1s" },
74    { MINUS_SEC,      "-1s" },
75    { ALARM,          "clock unsynchronized" },
76    { 0, NULL }
77};
78
79static struct tok ntp_stratum_values[] = {
80        { UNSPECIFIED,  "unspecified" },
81        { PRIM_REF,     "primary reference" },
82        { 0, NULL }
83};
84
85/*
86 * Print ntp requests
87 */
88void
89ntp_print(register const u_char *cp, u_int length)
90{
91        register const struct ntpdata *bp;
92        int mode, version, leapind;
93
94        bp = (struct ntpdata *)cp;
95
96        TCHECK(bp->status);
97
98        version = (int)(bp->status & VERSIONMASK) >> 3;
99        printf("NTPv%d", version);
100
101        mode = bp->status & MODEMASK;
102        if (!vflag) {
103            printf (", %s, length %u",
104                    tok2str(ntp_mode_values, "Unknown mode", mode),
105                    length);
106            return;
107        }
108       
109        printf (", length %u\n\t%s",
110                length,
111                tok2str(ntp_mode_values, "Unknown mode", mode));       
112
113        leapind = bp->status & LEAPMASK;
114        printf (", Leap indicator: %s (%u)",
115                tok2str(ntp_leapind_values, "Unknown", leapind),
116                leapind);
117
118        TCHECK(bp->stratum);
119        printf(", Stratum %u (%s)",     
120                bp->stratum,
121                tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum));
122
123        TCHECK(bp->ppoll);
124        printf(", poll %u (%us)", bp->ppoll, 1 << bp->ppoll);
125
126        /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
127        TCHECK2(bp->root_delay, 0);
128        printf(", precision %d", bp->precision);
129
130        TCHECK(bp->root_delay);
131        fputs("\n\tRoot Delay: ", stdout);
132        p_sfix(&bp->root_delay);
133
134        TCHECK(bp->root_dispersion);
135        fputs(", Root dispersion: ", stdout);
136        p_sfix(&bp->root_dispersion);
137
138        TCHECK(bp->refid);
139        fputs(", Reference-ID: ", stdout);
140        /* Interpretation depends on stratum */
141        switch (bp->stratum) {
142
143        case UNSPECIFIED:
144                printf("(unspec)");
145                break;
146
147        case PRIM_REF:
148                if (fn_printn((u_char *)&(bp->refid), 4, snapend))
149                        goto trunc;
150                break;
151
152        case INFO_QUERY:
153                printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
154                /* this doesn't have more content */
155                return;
156
157        case INFO_REPLY:
158                printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
159                /* this is too complex to be worth printing */
160                return;
161
162        default:
163                printf("%s", ipaddr_string(&(bp->refid)));
164                break;
165        }
166
167        TCHECK(bp->ref_timestamp);
168        fputs("\n\t  Reference Timestamp:  ", stdout);
169        p_ntp_time(&(bp->ref_timestamp));
170
171        TCHECK(bp->org_timestamp);
172        fputs("\n\t  Originator Timestamp: ", stdout);
173        p_ntp_time(&(bp->org_timestamp));
174
175        TCHECK(bp->rec_timestamp);
176        fputs("\n\t  Receive Timestamp:    ", stdout);
177        p_ntp_time(&(bp->rec_timestamp));
178
179        TCHECK(bp->xmt_timestamp);
180        fputs("\n\t  Transmit Timestamp:   ", stdout);
181        p_ntp_time(&(bp->xmt_timestamp));
182
183        fputs("\n\t    Originator - Receive Timestamp:  ", stdout);
184        p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp));
185
186        fputs("\n\t    Originator - Transmit Timestamp: ", stdout);
187        p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp));
188
189        if ( (sizeof(struct ntpdata) - length) == 16) {         /* Optional: key-id */
190                TCHECK(bp->key_id);
191                printf("\n\tKey id: %u", bp->key_id);
192        } else if ( (sizeof(struct ntpdata) - length) == 0) {   /* Optional: key-id + authentication */
193                TCHECK(bp->key_id);
194                printf("\n\tKey id: %u", bp->key_id);
195                TCHECK2(bp->message_digest, sizeof (bp->message_digest));
196                printf("\n\tAuthentication: %08x%08x%08x%08x",
197                               EXTRACT_32BITS(bp->message_digest),
198                               EXTRACT_32BITS(bp->message_digest + 4),
199                               EXTRACT_32BITS(bp->message_digest + 8),
200                               EXTRACT_32BITS(bp->message_digest + 12));
201        }
202        return;
203
204trunc:
205        fputs(" [|ntp]", stdout);
206}
207
208static void
209p_sfix(register const struct s_fixedpt *sfp)
210{
211        register int i;
212        register int f;
213        register float ff;
214
215        i = EXTRACT_16BITS(&sfp->int_part);
216        f = EXTRACT_16BITS(&sfp->fraction);
217        ff = f / 65536.0;       /* shift radix point by 16 bits */
218        f = ff * 1000000.0;     /* Treat fraction as parts per million */
219        printf("%d.%06d", i, f);
220}
221
222#define FMAXINT (4294967296.0)  /* floating point rep. of MAXINT */
223
224static void
225p_ntp_time(register const struct l_fixedpt *lfp)
226{
227        register int32_t i;
228        register u_int32_t uf;
229        register u_int32_t f;
230        register float ff;
231
232        i = EXTRACT_32BITS(&lfp->int_part);
233        uf = EXTRACT_32BITS(&lfp->fraction);
234        ff = uf;
235        if (ff < 0.0)           /* some compilers are buggy */
236                ff += FMAXINT;
237        ff = ff / FMAXINT;      /* shift radix point by 32 bits */
238        f = ff * 1000000000.0;  /* treat fraction as parts per billion */
239        printf("%u.%09d", i, f);
240
241#ifdef HAVE_STRFTIME
242        /*
243         * print the time in human-readable format.
244         */
245        if (i) {
246            time_t seconds = i - JAN_1970;
247            struct tm *tm;
248            char time_buf[128];
249
250            tm = localtime(&seconds);
251            strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
252            printf (" (%s)", time_buf);
253        }
254#endif
255}
256
257/* Prints time difference between *lfp and *olfp */
258static void
259p_ntp_delta(register const struct l_fixedpt *olfp,
260            register const struct l_fixedpt *lfp)
261{
262        register int32_t i;
263        register u_int32_t u, uf;
264        register u_int32_t ou, ouf;
265        register u_int32_t f;
266        register float ff;
267        int signbit;
268
269        u = EXTRACT_32BITS(&lfp->int_part);
270        ou = EXTRACT_32BITS(&olfp->int_part);
271        uf = EXTRACT_32BITS(&lfp->fraction);
272        ouf = EXTRACT_32BITS(&olfp->fraction);
273        if (ou == 0 && ouf == 0) {
274                p_ntp_time(lfp);
275                return;
276        }
277
278        i = u - ou;
279
280        if (i > 0) {            /* new is definitely greater than old */
281                signbit = 0;
282                f = uf - ouf;
283                if (ouf > uf)   /* must borrow from high-order bits */
284                        i -= 1;
285        } else if (i < 0) {     /* new is definitely less than old */
286                signbit = 1;
287                f = ouf - uf;
288                if (uf > ouf)   /* must carry into the high-order bits */
289                        i += 1;
290                i = -i;
291        } else {                /* int_part is zero */
292                if (uf > ouf) {
293                        signbit = 0;
294                        f = uf - ouf;
295                } else {
296                        signbit = 1;
297                        f = ouf - uf;
298                }
299        }
300
301        ff = f;
302        if (ff < 0.0)           /* some compilers are buggy */
303                ff += FMAXINT;
304        ff = ff / FMAXINT;      /* shift radix point by 32 bits */
305        f = ff * 1000000000.0;  /* treat fraction as parts per billion */
306        if (signbit)
307                putchar('-');
308        else
309                putchar('+');
310        printf("%d.%09d", i, f);
311}
312
Note: See TracBrowser for help on using the repository browser.