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