MSWSP: add ids for another unknown Property Set
[wireshark-wip.git] / epan / dissectors / packet-armagetronad.c
blobc445beaa62875ecc0270727db13e6ce64b9737c4
1 /* packet-armagetronad.c
2 * Routines for the Armagetronad packet dissection
3 * Copyright 2005, Guillaume Chazarain <guichaz@yahoo.fr>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <glib.h>
30 #include <epan/packet.h>
31 #include <epan/wmem/wmem.h>
33 void proto_register_armagetronad(void);
34 void proto_reg_handoff_armagetronad(void);
36 /* Initialize the protocol and registered fields */
37 static int proto_armagetronad = -1;
38 static int hf_armagetronad_descriptor_id = -1;
39 static int hf_armagetronad_message_id = -1;
40 static int hf_armagetronad_data_len = -1;
41 static int hf_armagetronad_data = -1;
42 static int hf_armagetronad_sender_id = -1;
43 static int hf_armagetronad_msg_subtree = -1;
45 /* Initialize the subtree pointers */
46 static gint ett_armagetronad = -1;
47 static gint ett_message = -1;
49 #define UDP_PORT_ARMAGETRONAD 4534
50 #define UDP_PORT_MASTER 4533
53 * The ACK packet is so common that we treat it
54 * differently: it has no MessageID
56 #define ACK 1
59 * armagetronad-0.2.8.2.1
60 * The numbers and names were retrieved at runtime using the
61 * 'nDescriptor* descriptors[MAXDESCRIPTORS]' array
63 static const value_string descriptors[] = {
64 {1, "ack"},
65 {2, "req_info"},
66 {3, "login_deny"},
67 {4, "login_ignore"},
68 {5, "login_accept"},
69 {6, "login1"},
70 {7, "logout"},
71 {8, "sn_ConsoleOut"},
72 {9, "client_cen"},
73 {10, "version"},
74 {11, "login2"},
75 {20, "req_id"},
76 {21, "id_req_handler"},
77 {22, "net_destroy"},
78 {23, "net_control"},
79 {24, "net_sync"},
80 {25, "ready to get objects"},
81 {26, "net_clear"},
82 {27, "sync_ack"},
83 {28, "sync_msg"},
84 {40, "password_request"},
85 {41, "password_answer"},
86 {50, "small_server"},
87 {51, "big_server"},
88 {52, "small_request"},
89 {53, "big_request"},
90 {54, "big_server_master"},
91 {55, "big_request_master"},
92 {60, "transfer config"},
93 {200, "Chat"},
94 {201, "ePlayerNetID"},
95 {202, "player_removed_from_game"},
96 {203, "Chat Client"},
97 {210, "eTimer"},
98 {220, "eTeam"},
99 {230, "vote cast"},
100 {231, "Kick vote"},
101 {232, "Server controlled vote"},
102 {233, "Server controlled vote expired"},
103 {300, "gNetPlayerWall"},
104 {310, "game"},
105 {311, "client_gamestate"},
106 {320, "cycle"},
107 {321, "destination"},
108 {330, "gAIPlayer"},
109 {331, "gAITeam"},
110 {340, "zone"},
111 {0, NULL}
114 static gboolean
115 is_armagetronad_packet(tvbuff_t * tvb)
117 gint offset = 0;
119 /* For each message in the frame */
120 while (tvb_length_remaining(tvb, offset) > 2) {
121 gint data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
123 #if 0
125 * If the descriptor_id is not in the table it's possibly
126 * because the protocol evoluated, losing synchronization
127 * with the table, that's why we don't consider that as
128 * a heuristic
130 if (!try_val_to_str(tvb_get_ntohs(tvb, offset), descriptors))
131 /* DescriptorID not found in the table */
132 return FALSE;
133 #endif
135 if (!tvb_bytes_exist(tvb, offset + 6, data_len))
136 /* Advertised length too long */
137 return FALSE;
139 offset += 6 + data_len;
142 /* The packed should end with a 2 bytes ID */
143 return tvb_length_remaining(tvb, offset) == 2;
146 static void
147 add_message_data(tvbuff_t * tvb, gint offset, gint data_len, proto_tree * tree)
149 gchar *data = NULL;
150 gchar tmp;
151 int i;
153 if (!tree)
154 return;
156 data = (gchar *)tvb_memcpy(tvb, wmem_alloc(wmem_packet_scope(), data_len + 1), offset, data_len);
157 data[data_len] = '\0';
159 for (i = 0; i < data_len; i += 2) {
161 * There must be a better way to tell
162 * Wireshark not to stop on null bytes
163 * as the length is known
165 if (!data[i])
166 data[i] = ' ';
168 if (!data[i+1])
169 data[i+1] = ' ';
171 /* Armagetronad swaps unconditionally */
172 tmp = data[i];
173 data[i] = data[i+1];
174 data[i+1] = tmp;
177 proto_tree_add_string(tree, hf_armagetronad_data, tvb, offset,
178 data_len, (gchar *) data);
181 static gint
182 add_message(tvbuff_t * tvb, gint offset, proto_tree * tree, wmem_strbuf_t * info)
184 guint16 descriptor_id, message_id;
185 gint data_len;
186 proto_item *msg;
187 proto_tree *msg_tree;
188 const gchar *descriptor;
190 descriptor_id = tvb_get_ntohs(tvb, offset);
191 message_id = tvb_get_ntohs(tvb, offset + 2);
192 data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
194 /* Message subtree */
195 descriptor = val_to_str(descriptor_id, descriptors, "Unknown (%u)");
196 if (descriptor_id == ACK)
197 msg = proto_tree_add_none_format(tree,
198 hf_armagetronad_msg_subtree,
199 tvb, offset, data_len + 6,
200 "ACK %d messages",
201 data_len / 2);
202 else
203 msg = proto_tree_add_none_format(tree,
204 hf_armagetronad_msg_subtree,
205 tvb, offset, data_len + 6,
206 "Message 0x%04x [%s]",
207 message_id, descriptor);
209 msg_tree = proto_item_add_subtree(msg, ett_message);
211 /* DescriptorID field */
212 proto_tree_add_item(msg_tree, hf_armagetronad_descriptor_id, tvb,
213 offset, 2, ENC_BIG_ENDIAN);
214 if (info)
215 wmem_strbuf_append_printf(info, "%s, ", descriptor);
217 /* MessageID field */
218 proto_tree_add_item(msg_tree, hf_armagetronad_message_id, tvb,
219 offset + 2, 2, ENC_BIG_ENDIAN);
221 /* DataLen field */
222 proto_tree_add_item(msg_tree, hf_armagetronad_data_len, tvb,
223 offset + 4, 2, ENC_BIG_ENDIAN);
225 /* Data field */
226 add_message_data(tvb, offset + 6, data_len, msg_tree);
228 return data_len + 6;
231 /* Code to actually dissect the packets */
232 static gint
233 dissect_armagetronad(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void * data _U_)
235 proto_item *ti;
236 proto_tree *armagetronad_tree;
237 guint16 sender;
238 gint offset = 0;
239 wmem_strbuf_t *info;
240 gsize new_len;
242 if (!is_armagetronad_packet(tvb))
243 return 0;
245 info = wmem_strbuf_new(wmem_packet_scope(), "");
247 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Armagetronad");
249 col_clear(pinfo->cinfo, COL_INFO);
251 ti = proto_tree_add_item(tree, proto_armagetronad, tvb, 0, -1, ENC_NA);
252 armagetronad_tree = proto_item_add_subtree(ti, ett_armagetronad);
254 /* For each message in the frame */
255 while (tvb_length_remaining(tvb, offset) > 2)
256 offset += add_message(tvb, offset, armagetronad_tree, info);
258 /* After the messages, comes the SenderID */
259 sender = tvb_get_ntohs(tvb, offset);
260 proto_tree_add_item(ti, hf_armagetronad_sender_id, tvb, offset, 2,
261 ENC_BIG_ENDIAN);
263 new_len = wmem_strbuf_get_len(info) - 2; /* Remove the trailing ", " */
264 if (new_len > 0)
265 wmem_strbuf_truncate(info, new_len);
266 else
267 info = wmem_strbuf_new(wmem_packet_scope(), "No message");
269 col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] from 0x%04x",
270 wmem_strbuf_get_str(info), sender);
272 return offset + 2;
275 void proto_register_armagetronad(void)
277 static hf_register_info hf[] = {
278 {&hf_armagetronad_descriptor_id,
279 {"Descriptor", "armagetronad.descriptor_id",
280 FT_UINT16, BASE_DEC, VALS(descriptors), 0x0,
281 "The ID of the descriptor (the command)", HFILL}
283 {&hf_armagetronad_message_id,
284 {"MessageID", "armagetronad.message_id",
285 FT_UINT16, BASE_HEX, NULL, 0x0,
286 "The ID of the message (to ack it)", HFILL}
288 {&hf_armagetronad_data_len,
289 {"DataLen", "armagetronad.data_len",
290 FT_UINT16, BASE_DEC, NULL, 0x0,
291 "The length of the data (in shorts)", HFILL}
293 {&hf_armagetronad_data,
294 {"Data", "armagetronad.data",
295 FT_STRING, BASE_NONE, NULL, 0x0,
296 "The actual data (array of shorts in network order)", HFILL}
298 {&hf_armagetronad_sender_id,
299 {"SenderID", "armagetronad.sender_id",
300 FT_UINT16, BASE_HEX, NULL, 0x0,
301 "The ID of the sender (0x0000 for the server)", HFILL}
303 {&hf_armagetronad_msg_subtree,
304 {"Message", "armagetronad.message",
305 FT_NONE, BASE_NONE, NULL, 0x0,
306 "A message", HFILL}
310 static gint *ett[] = {
311 &ett_armagetronad,
312 &ett_message
315 proto_armagetronad =
316 proto_register_protocol("The Armagetron Advanced OpenGL Tron clone",
317 "Armagetronad", "armagetronad");
319 proto_register_field_array(proto_armagetronad, hf, array_length(hf));
320 proto_register_subtree_array(ett, array_length(ett));
321 new_register_dissector("armagetronad", dissect_armagetronad,
322 proto_armagetronad);
325 void proto_reg_handoff_armagetronad(void)
327 dissector_handle_t armagetronad_handle;
329 armagetronad_handle = find_dissector("armagetronad");
331 dissector_add_uint("udp.port", UDP_PORT_ARMAGETRONAD, armagetronad_handle);
332 dissector_add_uint("udp.port", UDP_PORT_MASTER, armagetronad_handle);