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