]> The Tcpdump Group git mirrors - tcpdump/blob - print-timed.c
Pull a bunch of headers into the only source file that includes them.
[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 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-timed.c,v 1.9 2003-11-16 09:36:40 guy Exp $";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "interface.h"
37 #include "extract.h"
38
39 /*
40 * Time Synchronization Protocol
41 */
42
43 struct tsp_timeval {
44 u_int32_t tv_sec;
45 u_int32_t tv_usec;
46 };
47
48 struct tsp {
49 u_int8_t tsp_type;
50 u_int8_t tsp_vers;
51 u_int16_t tsp_seq;
52 union {
53 struct tsp_timeval tspu_time;
54 int8_t tspu_hopcnt;
55 } tsp_u;
56 int8_t tsp_name[256];
57 };
58
59 #define tsp_time tsp_u.tspu_time
60 #define tsp_hopcnt tsp_u.tspu_hopcnt
61
62 /*
63 * Command types.
64 */
65 #define TSP_ANY 0 /* match any types */
66 #define TSP_ADJTIME 1 /* send adjtime */
67 #define TSP_ACK 2 /* generic acknowledgement */
68 #define TSP_MASTERREQ 3 /* ask for master's name */
69 #define TSP_MASTERACK 4 /* acknowledge master request */
70 #define TSP_SETTIME 5 /* send network time */
71 #define TSP_MASTERUP 6 /* inform slaves that master is up */
72 #define TSP_SLAVEUP 7 /* slave is up but not polled */
73 #define TSP_ELECTION 8 /* advance candidature for master */
74 #define TSP_ACCEPT 9 /* support candidature of master */
75 #define TSP_REFUSE 10 /* reject candidature of master */
76 #define TSP_CONFLICT 11 /* two or more masters present */
77 #define TSP_RESOLVE 12 /* masters' conflict resolution */
78 #define TSP_QUIT 13 /* reject candidature if master is up */
79 #define TSP_DATE 14 /* reset the time (date command) */
80 #define TSP_DATEREQ 15 /* remote request to reset the time */
81 #define TSP_DATEACK 16 /* acknowledge time setting */
82 #define TSP_TRACEON 17 /* turn tracing on */
83 #define TSP_TRACEOFF 18 /* turn tracing off */
84 #define TSP_MSITE 19 /* find out master's site */
85 #define TSP_MSITEREQ 20 /* remote master's site request */
86 #define TSP_TEST 21 /* for testing election algo */
87 #define TSP_SETDATE 22 /* New from date command */
88 #define TSP_SETDATEREQ 23 /* New remote for above */
89 #define TSP_LOOP 24 /* loop detection packet */
90
91 #define TSPTYPENUMBER 25
92
93 static const char tstr[] = "[|timed]";
94
95 static const char *tsptype[TSPTYPENUMBER] =
96 { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
97 "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
98 "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
99 "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
100
101 void
102 timed_print(register const u_char *bp)
103 {
104 #define endof(x) ((u_char *)&(x) + sizeof (x))
105 struct tsp *tsp = (struct tsp *)bp;
106 long sec, usec;
107 const u_char *end;
108
109 if (endof(tsp->tsp_type) > snapend) {
110 printf("%s", tstr);
111 return;
112 }
113 if (tsp->tsp_type < TSPTYPENUMBER)
114 printf("TSP_%s", tsptype[tsp->tsp_type]);
115 else
116 printf("(tsp_type %#x)", tsp->tsp_type);
117
118 if (endof(tsp->tsp_vers) > snapend) {
119 printf(" %s", tstr);
120 return;
121 }
122 printf(" vers %d", tsp->tsp_vers);
123
124 if (endof(tsp->tsp_seq) > snapend) {
125 printf(" %s", tstr);
126 return;
127 }
128 printf(" seq %d", tsp->tsp_seq);
129
130 if (tsp->tsp_type == TSP_LOOP) {
131 if (endof(tsp->tsp_hopcnt) > snapend) {
132 printf(" %s", tstr);
133 return;
134 }
135 printf(" hopcnt %d", tsp->tsp_hopcnt);
136 } else if (tsp->tsp_type == TSP_SETTIME ||
137 tsp->tsp_type == TSP_ADJTIME ||
138 tsp->tsp_type == TSP_SETDATE ||
139 tsp->tsp_type == TSP_SETDATEREQ) {
140 if (endof(tsp->tsp_time) > snapend) {
141 printf(" %s", tstr);
142 return;
143 }
144 sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
145 usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
146 if (usec < 0)
147 /* corrupt, skip the rest of the packet */
148 return;
149 fputs(" time ", stdout);
150 if (sec < 0 && usec != 0) {
151 sec++;
152 if (sec == 0)
153 fputc('-', stdout);
154 usec = 1000000 - usec;
155 }
156 printf("%ld.%06ld", sec, usec);
157 }
158
159 end = memchr(tsp->tsp_name, '\0', snapend - (u_char *)tsp->tsp_name);
160 if (end == NULL)
161 printf(" %s", tstr);
162 else {
163 fputs(" name ", stdout);
164 fwrite(tsp->tsp_name, end - (u_char *)tsp->tsp_name, 1, stdout);
165 }
166 }