]> The Tcpdump Group git mirrors - tcpdump/blob - print-timed.c
Add the ndo_protocol field in the netdissect_options structure
[tcpdump] / print-timed.c
1 /*
2 * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
3 * 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
22 /* \summary: Berkeley UNIX Time Synchronization Protocol */
23
24 /* specification: http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include "netdissect-stdinc.h"
31
32 #include "netdissect.h"
33 #include "extract.h"
34
35 struct tsp_timeval {
36 nd_int32_t tv_sec;
37 nd_int32_t tv_usec;
38 };
39
40 struct tsp {
41 nd_uint8_t tsp_type;
42 nd_uint8_t tsp_vers;
43 nd_uint16_t tsp_seq;
44 union {
45 struct tsp_timeval tspu_time;
46 nd_int8_t tspu_hopcnt;
47 } tsp_u;
48 nd_byte tsp_name[256];
49 };
50
51 #define tsp_time tsp_u.tspu_time
52 #define tsp_hopcnt tsp_u.tspu_hopcnt
53
54 /*
55 * Command types.
56 */
57 #define TSP_ANY 0 /* match any types */
58 #define TSP_ADJTIME 1 /* send adjtime */
59 #define TSP_ACK 2 /* generic acknowledgement */
60 #define TSP_MASTERREQ 3 /* ask for master's name */
61 #define TSP_MASTERACK 4 /* acknowledge master request */
62 #define TSP_SETTIME 5 /* send network time */
63 #define TSP_MASTERUP 6 /* inform slaves that master is up */
64 #define TSP_SLAVEUP 7 /* slave is up but not polled */
65 #define TSP_ELECTION 8 /* advance candidature for master */
66 #define TSP_ACCEPT 9 /* support candidature of master */
67 #define TSP_REFUSE 10 /* reject candidature of master */
68 #define TSP_CONFLICT 11 /* two or more masters present */
69 #define TSP_RESOLVE 12 /* masters' conflict resolution */
70 #define TSP_QUIT 13 /* reject candidature if master is up */
71 #define TSP_DATE 14 /* reset the time (date command) */
72 #define TSP_DATEREQ 15 /* remote request to reset the time */
73 #define TSP_DATEACK 16 /* acknowledge time setting */
74 #define TSP_TRACEON 17 /* turn tracing on */
75 #define TSP_TRACEOFF 18 /* turn tracing off */
76 #define TSP_MSITE 19 /* find out master's site */
77 #define TSP_MSITEREQ 20 /* remote master's site request */
78 #define TSP_TEST 21 /* for testing election algo */
79 #define TSP_SETDATE 22 /* New from date command */
80 #define TSP_SETDATEREQ 23 /* New remote for above */
81 #define TSP_LOOP 24 /* loop detection packet */
82
83 #define TSPTYPENUMBER 25
84
85 static const char tstr[] = "[|timed]";
86
87 static const char *tsptype[TSPTYPENUMBER] =
88 { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
89 "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
90 "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
91 "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
92
93 void
94 timed_print(netdissect_options *ndo,
95 const u_char *bp)
96 {
97 const struct tsp *tsp = (const struct tsp *)bp;
98 uint8_t tsp_type;
99 int sec, usec;
100
101 ndo->ndo_protocol = "timed";
102 ND_TCHECK_1(tsp->tsp_type);
103 tsp_type = EXTRACT_U_1(tsp->tsp_type);
104 if (tsp_type < TSPTYPENUMBER)
105 ND_PRINT("TSP_%s", tsptype[tsp_type]);
106 else
107 ND_PRINT("(tsp_type %#x)", tsp_type);
108
109 ND_TCHECK_1(tsp->tsp_vers);
110 ND_PRINT(" vers %u", EXTRACT_U_1(tsp->tsp_vers));
111
112 ND_TCHECK_2(tsp->tsp_seq);
113 ND_PRINT(" seq %u", EXTRACT_BE_U_2(tsp->tsp_seq));
114
115 switch (tsp_type) {
116 case TSP_LOOP:
117 ND_TCHECK_1(tsp->tsp_hopcnt);
118 ND_PRINT(" hopcnt %u", EXTRACT_U_1(tsp->tsp_hopcnt));
119 break;
120 case TSP_SETTIME:
121 case TSP_ADJTIME:
122 case TSP_SETDATE:
123 case TSP_SETDATEREQ:
124 ND_TCHECK_8(&tsp->tsp_time);
125 sec = EXTRACT_BE_S_4(tsp->tsp_time.tv_sec);
126 usec = EXTRACT_BE_S_4(tsp->tsp_time.tv_usec);
127 /* XXX The comparison below is always false? */
128 if (usec < 0)
129 /* invalid, skip the rest of the packet */
130 return;
131 ND_PRINT(" time ");
132 if (sec < 0 && usec != 0) {
133 sec++;
134 if (sec == 0)
135 ND_PRINT("-");
136 usec = 1000000 - usec;
137 }
138 ND_PRINT("%d.%06d", sec, usec);
139 break;
140 }
141 ND_PRINT(" name ");
142 if (fn_print(ndo, (const u_char *)tsp->tsp_name, (const u_char *)tsp->tsp_name + sizeof(tsp->tsp_name)))
143 goto trunc;
144 return;
145
146 trunc:
147 ND_PRINT(" %s", tstr);
148 }