Update FAQ
[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.4 2003/06/11 20:41:45 guy 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 #include <glib.h>
34 #include <epan/packet.h>
35
36 /*
37  * For a full documentation of the Time Synchronization Protocol (TSP) see:
38  * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf
39  */
40
41 static int proto_tsp = -1;
42 static int hf_tsp_type = -1;
43 static int hf_tsp_vers = -1;
44 static int hf_tsp_seq = -1;
45 static int hf_tsp_hopcnt = -1;
46 static int hf_tsp_time_sec = -1;
47 static int hf_tsp_time_usec = -1;
48 static int hf_tsp_name = -1;
49
50 static gint ett_tsp = -1;
51
52 static dissector_handle_t       tsp_handle;
53
54
55 /* timed port from /etc/services */
56 #define UDP_PORT_TIMED  525
57
58
59 static const value_string names_tsp_type[] = {
60 #define TSP_ANY                 0       /* match any types */
61         {       TSP_ANY, "any" },
62 #define TSP_ADJTIME             1       /* send adjtime */
63         {       TSP_ADJTIME, "adjtime" },
64 #define TSP_ACK                 2       /* generic acknowledgement */
65         {       TSP_ACK, "ack" },
66 #define TSP_MASTERREQ           3       /* ask for master's name */
67         {       TSP_MASTERREQ, "masterreq" },
68 #define TSP_MASTERACK           4       /* acknowledge master request */
69         {       TSP_MASTERACK, "masterack" },
70 #define TSP_SETTIME             5       /* send network time */
71         {       TSP_SETTIME, "settime" },
72 #define TSP_MASTERUP            6       /* inform slaves that master is up */
73         {       TSP_MASTERUP, "masterup" },
74 #define TSP_SLAVEUP             7       /* slave is up but not polled */
75         {       TSP_SLAVEUP, "slaveup" },
76 #define TSP_ELECTION            8       /* advance candidature for master */
77         {       TSP_ELECTION, "election" },
78 #define TSP_ACCEPT              9       /* support candidature of master */
79         {       TSP_ACCEPT, "accept" },
80 #define TSP_REFUSE              10      /* reject candidature of master */
81         {       TSP_REFUSE, "refuse" },
82 #define TSP_CONFLICT            11      /* two or more masters present */
83         {       TSP_CONFLICT, "conflict" },
84 #define TSP_RESOLVE             12      /* masters' conflict resolution */
85         {       TSP_RESOLVE, "resolve" },
86 #define TSP_QUIT                13      /* reject candidature if master is up */
87         {       TSP_QUIT, "quit" },
88 #define TSP_DATE                14      /* reset the time (date command) */
89         {       TSP_DATE, "date" },
90 #define TSP_DATEREQ             15      /* remote request to reset the time */
91         {       TSP_DATEREQ, "datereq" },
92 #define TSP_DATEACK             16      /* acknowledge time setting  */
93         {       TSP_DATEACK, "dateack" },
94 #define TSP_TRACEON             17      /* turn tracing on */
95         {       TSP_TRACEON, "traceon" },
96 #define TSP_TRACEOFF            18      /* turn tracing off */
97         {       TSP_TRACEOFF, "traceoff" },
98 #define TSP_MSITE               19      /* find out master's site */
99         {       TSP_MSITE, "msite" },
100 #define TSP_MSITEREQ            20      /* remote master's site request */
101         {       TSP_MSITEREQ, "msitereq" },
102 #define TSP_TEST                21      /* for testing election algo */
103         {       TSP_TEST, "test" },
104 #define TSP_SETDATE             22      /* New from date command */
105         {       TSP_SETDATE, "setdate" },
106 #define TSP_SETDATEREQ          23      /* New remote for above */
107         {       TSP_SETDATEREQ, "setdatereq" },
108 #define TSP_LOOP                24      /* loop detection packet */
109         {       TSP_LOOP, "loop" },
110         { 0, NULL }
111 };
112
113
114 static void
115 dissect_tsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
116 {
117         proto_tree      *tsp_tree = NULL;
118         proto_item      *tsp_item = NULL;
119
120         guint8          tsp_type;
121
122         if (check_col(pinfo->cinfo, COL_PROTOCOL))
123                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TSP");
124         if (check_col(pinfo->cinfo, COL_INFO))
125                 col_clear(pinfo->cinfo, COL_INFO);
126
127         tsp_type = tvb_get_guint8(tvb, 0);
128         if (check_col(pinfo->cinfo, COL_INFO))
129                 col_add_str(pinfo->cinfo, COL_INFO,
130                     val_to_str(tsp_type, names_tsp_type, "Unknown message type (%u)"));
131
132         if (tree) {
133                 tsp_item = proto_tree_add_item(tree, proto_tsp,
134                                 tvb, 0, -1, FALSE);
135                 if (tsp_item)
136                         tsp_tree = proto_item_add_subtree(tsp_item, ett_tsp);
137         }
138
139         if (tsp_tree) {
140                 proto_tree_add_uint(tsp_tree, hf_tsp_type,
141                         tvb, 0, 1, tsp_type);
142                 proto_tree_add_item(tsp_tree, hf_tsp_vers,
143                         tvb, 1, 1, FALSE);
144                 proto_tree_add_item(tsp_tree, hf_tsp_seq,
145                         tvb, 2, 2, FALSE);
146         }
147
148         switch (tsp_type) {
149
150         case TSP_LOOP:
151                 if (tsp_tree)
152                         proto_tree_add_item(tsp_tree, hf_tsp_hopcnt,
153                                 tvb, 4, 1, FALSE);
154                 break;
155
156         case TSP_SETTIME:
157         case TSP_ADJTIME:
158         case TSP_SETDATE:
159         case TSP_SETDATEREQ:
160                 if (tsp_tree) {
161                         proto_tree_add_item(tsp_tree, hf_tsp_time_sec,
162                                 tvb, 4, 4, FALSE);
163                         proto_tree_add_item(tsp_tree, hf_tsp_time_usec,
164                                 tvb, 8, 4, FALSE);
165                 }
166                 break;
167         }
168
169         if (tsp_tree) {
170                 proto_tree_add_item(tsp_tree, hf_tsp_name, tvb, 12,
171                         -1, FALSE);
172         }
173 }
174
175
176 void
177 proto_reg_handoff_tsp(void)
178 {
179         dissector_add("udp.port", UDP_PORT_TIMED, tsp_handle);
180 }
181
182
183 void
184 proto_register_tsp(void)
185 {
186   static hf_register_info hf[] = {
187     { &hf_tsp_type,
188       { "Type", "tsp.type",
189         FT_UINT8, BASE_DEC, VALS(names_tsp_type), 0x0,
190         "Packet Type", HFILL }},
191     { &hf_tsp_vers,
192       { "Version", "tsp.version",
193         FT_UINT8, BASE_DEC, NULL, 0x0,
194         "Protocol Version Number", HFILL }},
195     { &hf_tsp_seq,
196       { "Sequence", "tsp.sequence",
197         FT_UINT16, BASE_DEC, NULL, 0x0,
198         "Sequence Number", HFILL }},
199     { &hf_tsp_hopcnt,
200       { "Hop Count", "tsp.hopcnt",
201         FT_UINT8, BASE_DEC, NULL, 0x0,
202         "Hop Count", HFILL }},
203     { &hf_tsp_time_sec,
204       { "Seconds", "tsp.sec",
205         FT_UINT32, BASE_DEC, NULL, 0x0,
206         "Seconds", HFILL }},
207     { &hf_tsp_time_usec,
208       { "Microseconds", "tsp.usec",
209         FT_UINT32, BASE_DEC, NULL, 0x0,
210         "Microseconds", HFILL }},
211     { &hf_tsp_name,
212       { "Machine Name", "tsp.name",
213         FT_STRINGZ, BASE_DEC, NULL, 0x0,
214         "Sender Machine Name", HFILL }}
215   };
216         static gint *ett[] = {
217                 &ett_tsp
218         };
219
220         proto_tsp = proto_register_protocol("Time Synchronization Protocol",
221                                         "TSP", "tsp");
222         proto_register_field_array(proto_tsp, hf, array_length(hf));
223         proto_register_subtree_array(ett, array_length(ett));
224
225         tsp_handle = create_dissector_handle(dissect_tsp, proto_tsp);
226 }
227