Initial support for writing NetXRay 2.x (Windows Sniffer) format
[obnox/wireshark/wip.git] / packet-tsp.c
1 /* packet-quake.c
2  * Routines for Time Synchronization Protocol (TSP) packet dissection
3  *
4  * Uwe Girlich <Uwe.Girlich@philosys.de>
5  *
6  * $Id: packet-tsp.c,v 1.1 2002/01/31 07:50:29 girlich Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-quake.c
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include <glib.h>
42 #include <epan/packet.h>
43
44 /* 
45  * For a full documentation of the Time Synchronization Protocol (TSP) see:
46  * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf
47  */
48
49 static int proto_tsp = -1;
50 static int hf_tsp_type = -1; 
51 static int hf_tsp_vers = -1; 
52 static int hf_tsp_seq = -1; 
53 static int hf_tsp_hopcnt = -1; 
54 static int hf_tsp_time_sec = -1; 
55 static int hf_tsp_time_usec = -1; 
56 static int hf_tsp_name = -1; 
57
58 static gint ett_tsp = -1;
59
60 static dissector_handle_t       tsp_handle;
61
62
63 /* timed port from /etc/services */
64 #define UDP_PORT_TIMED  525
65
66
67 static const value_string names_tsp_type[] = {
68 #define TSP_ANY                 0       /* match any types */
69         {       TSP_ANY, "any" },
70 #define TSP_ADJTIME             1       /* send adjtime */
71         {       TSP_ADJTIME, "adjtime" },
72 #define TSP_ACK                 2       /* generic acknowledgement */
73         {       TSP_ACK, "ack" },
74 #define TSP_MASTERREQ           3       /* ask for master's name */
75         {       TSP_MASTERREQ, "masterreq" },
76 #define TSP_MASTERACK           4       /* acknowledge master request */
77         {       TSP_MASTERACK, "masterack" },
78 #define TSP_SETTIME             5       /* send network time */
79         {       TSP_SETTIME, "settime" },
80 #define TSP_MASTERUP            6       /* inform slaves that master is up */
81         {       TSP_MASTERUP, "masterup" },
82 #define TSP_SLAVEUP             7       /* slave is up but not polled */
83         {       TSP_SLAVEUP, "slaveup" },
84 #define TSP_ELECTION            8       /* advance candidature for master */
85         {       TSP_ELECTION, "election" },
86 #define TSP_ACCEPT              9       /* support candidature of master */
87         {       TSP_ACCEPT, "accept" },
88 #define TSP_REFUSE              10      /* reject candidature of master */
89         {       TSP_REFUSE, "refuse" },
90 #define TSP_CONFLICT            11      /* two or more masters present */
91         {       TSP_CONFLICT, "conflict" },
92 #define TSP_RESOLVE             12      /* masters' conflict resolution */
93         {       TSP_RESOLVE, "resolve" },
94 #define TSP_QUIT                13      /* reject candidature if master is up */
95         {       TSP_QUIT, "quit" },
96 #define TSP_DATE                14      /* reset the time (date command) */
97         {       TSP_DATE, "date" },
98 #define TSP_DATEREQ             15      /* remote request to reset the time */
99         {       TSP_DATEREQ, "datereq" },
100 #define TSP_DATEACK             16      /* acknowledge time setting  */
101         {       TSP_DATEACK, "dateack" },
102 #define TSP_TRACEON             17      /* turn tracing on */
103         {       TSP_TRACEON, "traceon" },
104 #define TSP_TRACEOFF            18      /* turn tracing off */
105         {       TSP_TRACEOFF, "traceoff" },
106 #define TSP_MSITE               19      /* find out master's site */
107         {       TSP_MSITE, "msite" },
108 #define TSP_MSITEREQ            20      /* remote master's site request */
109         {       TSP_MSITEREQ, "msitereq" },
110 #define TSP_TEST                21      /* for testing election algo */
111         {       TSP_TEST, "test" },
112 #define TSP_SETDATE             22      /* New from date command */
113         {       TSP_SETDATE, "setdate" },
114 #define TSP_SETDATEREQ          23      /* New remote for above */
115         {       TSP_SETDATEREQ, "setdatereq" },
116 #define TSP_LOOP                24      /* loop detection packet */
117         {       TSP_LOOP, "loop" },
118         { 0, NULL }
119 };
120
121
122 static void
123 dissect_tsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
124 {
125         proto_tree      *tsp_tree = NULL;
126         proto_item      *tsp_item = NULL;
127
128         guint8          tsp_type;
129         guint8          tsp_vers;
130         guint16         tsp_seq;
131         guint32         tsp_time_sec;
132         guint32         tsp_time_usec;
133         guint8          tsp_hopcnt;
134         gint            tsp_name_length;
135         guint8          tsp_name[256];
136
137         if (check_col(pinfo->cinfo, COL_PROTOCOL))
138                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TSP");
139         if (check_col(pinfo->cinfo, COL_INFO))
140                 col_clear(pinfo->cinfo, COL_INFO);
141
142         tsp_type = tvb_get_guint8(tvb, 0);
143         tsp_vers = tvb_get_guint8(tvb, 1);
144         tsp_seq = tvb_get_ntohs(tvb, 2);
145
146         if (tree) {
147                 tsp_item = proto_tree_add_item(tree, proto_tsp,
148                                 tvb, 0, -1, FALSE);
149                 if (tsp_item)
150                         tsp_tree = proto_item_add_subtree(tsp_item, ett_tsp);
151         }
152
153         if (tsp_tree) {
154                 proto_tree_add_uint(tsp_tree, hf_tsp_type,
155                         tvb, 0, 1, tsp_type);
156                 proto_tree_add_uint(tsp_tree, hf_tsp_vers,
157                         tvb, 1, 1, tsp_vers);
158                 proto_tree_add_uint(tsp_tree, hf_tsp_seq,
159                         tvb, 2, 2, tsp_seq);
160         }
161
162         if (tsp_type == TSP_LOOP) {
163                 tsp_hopcnt = tvb_get_guint8(tvb, 4);
164                 if (tsp_tree)
165                         proto_tree_add_uint(tsp_tree, hf_tsp_hopcnt,
166                                 tvb, 4, 1, tsp_type);
167         }
168
169         if (tsp_type == TSP_SETTIME ||
170                 tsp_type == TSP_ADJTIME ||
171                 tsp_type == TSP_SETDATE ||
172                 tsp_type == TSP_SETDATEREQ) {
173
174                 tsp_time_sec = tvb_get_ntohl(tvb, 4);
175                 if (tsp_tree)
176                         proto_tree_add_uint(tsp_tree, hf_tsp_time_sec,
177                                 tvb, 4, 4, tsp_time_sec);
178                 tsp_time_usec = tvb_get_ntohl(tvb, 8);
179                 if (tsp_tree)
180                         proto_tree_add_uint(tsp_tree, hf_tsp_time_usec,
181                                 tvb, 8, 4, tsp_time_usec);
182         }
183
184         tsp_name_length = tvb_get_nstringz(tvb, 12, 256, tsp_name);
185         if (tsp_name_length>0 && tsp_tree) {
186                 proto_tree_add_string(tsp_tree, hf_tsp_name, tvb, 12,
187                         tsp_name_length, tsp_name);
188         }
189 }
190
191
192 void
193 proto_reg_handoff_tsp(void)
194 {
195         dissector_add("udp.port", UDP_PORT_TIMED, tsp_handle);
196 }
197
198
199 void
200 proto_register_tsp(void)
201 {
202   static hf_register_info hf[] = {
203     { &hf_tsp_type,
204       { "Type", "tsp.type",
205         FT_UINT8, BASE_DEC, VALS(names_tsp_type), 0x0,
206         "Packet Type", HFILL }},
207     { &hf_tsp_vers,
208       { "Version", "tsp.version",
209         FT_UINT8, BASE_DEC, NULL, 0x0,
210         "Protocol Version Number", HFILL }},
211     { &hf_tsp_seq,
212       { "Sequence", "tsp.sequence",
213         FT_UINT16, BASE_DEC, NULL, 0x0,
214         "Sequence Number", HFILL }},
215     { &hf_tsp_hopcnt,
216       { "Hop Count", "tsp.hopcnt",
217         FT_UINT8, BASE_DEC, NULL, 0x0,
218         "Hop Count", HFILL }},
219     { &hf_tsp_time_sec,
220       { "Seconds", "tsp.sec",
221         FT_UINT32, BASE_DEC, NULL, 0x0,
222         "Seconds", HFILL }},
223     { &hf_tsp_time_usec,
224       { "Microseconds", "tsp.usec",
225         FT_UINT32, BASE_DEC, NULL, 0x0,
226         "Microseconds", HFILL }},
227     { &hf_tsp_name,
228       { "Machine Name", "tsp.name",
229         FT_STRING, BASE_DEC, NULL, 0x0,
230         "Sender Machine Name", HFILL }}
231   };
232         static gint *ett[] = {
233                 &ett_tsp
234         };
235
236         proto_tsp = proto_register_protocol("Time Synchronization Protocol",
237                                         "TSP", "tsp");
238         proto_register_field_array(proto_tsp, hf, array_length(hf));
239         proto_register_subtree_array(ett, array_length(ett));
240
241         tsp_handle = create_dissector_handle(dissect_tsp, proto_tsp);
242 }
243