1 /* packet-armagetronad.c
2 * Routines for the Armagetronad packet dissection
3 * Copyright 2005, Guillaume Chazarain <guichaz@yahoo.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <epan/packet.h>
33 #include <epan/emem.h>
35 /* Initialize the protocol and registered fields */
36 static int proto_armagetronad = -1;
37 static int hf_armagetronad_descriptor_id = -1;
38 static int hf_armagetronad_message_id = -1;
39 static int hf_armagetronad_data_len = -1;
40 static int hf_armagetronad_data = -1;
41 static int hf_armagetronad_sender_id = -1;
42 static int hf_armagetronad_msg_subtree = -1;
44 /* Initialize the subtree pointers */
45 static gint ett_armagetronad = -1;
46 static gint ett_message = -1;
48 #define UDP_PORT_ARMAGETRONAD 4534
49 #define UDP_PORT_MASTER 4533
52 * The ACK packet is so common that we treat it
53 * differently: it has no MessageID
58 * armagetronad-0.2.8.2.1
59 * The numbers and names were retrieved at runtime using the
60 * 'nDescriptor* descriptors[MAXDESCRIPTORS]' array
62 static const value_string descriptors[] = {
75 {21, "id_req_handler"},
79 {25, "ready to get objects"},
83 {40, "password_request"},
84 {41, "password_answer"},
87 {52, "small_request"},
89 {54, "big_server_master"},
90 {55, "big_request_master"},
91 {60, "transfer config"},
93 {201, "ePlayerNetID"},
94 {202, "player_removed_from_game"},
100 {232, "Server controlled vote"},
101 {233, "Server controlled vote expired"},
102 {300, "gNetPlayerWall"},
104 {311, "client_gamestate"},
106 {321, "destination"},
114 is_armagetronad_packet(tvbuff_t * tvb)
118 /* For each message in the frame */
119 while (tvb_length_remaining(tvb, offset) > 2) {
120 gint data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
124 * If the descriptor_id is not in the table it's possibly
125 * because the protocol evoluated, losing synchronization
126 * with the table, that's why we don't consider that as
129 if (!match_strval(tvb_get_ntohs(tvb, offset), descriptors))
130 /* DescriptorID not found in the table */
134 if (!tvb_bytes_exist(tvb, offset + 6, data_len))
135 /* Advertised length too long */
138 offset += 6 + data_len;
141 /* The packed should end with a 2 bytes ID */
142 return tvb_length_remaining(tvb, offset) == 2;
146 add_message_data(tvbuff_t * tvb, gint offset, gint data_len, proto_tree * tree)
155 data = tvb_memcpy(tvb, ep_alloc(data_len + 1), offset, data_len);
156 data[data_len] = '\0';
158 for (i = 0; i < data_len; i += 2) {
160 * There must be a better way to tell
161 * Wireshark not to stop on null bytes
162 * as the length is known
170 /* Armagetronad swaps unconditionally */
176 proto_tree_add_string(tree, hf_armagetronad_data, tvb, offset,
177 data_len, (gchar *) data);
181 add_message(tvbuff_t * tvb, gint offset, proto_tree * tree, GString * info)
183 guint16 descriptor_id, message_id;
186 proto_tree *msg_tree;
187 const gchar *descriptor;
189 descriptor_id = tvb_get_ntohs(tvb, offset);
190 message_id = tvb_get_ntohs(tvb, offset + 2);
191 data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
193 /* Message subtree */
194 descriptor = val_to_str(descriptor_id, descriptors, "Unknown (%u)");
195 if (descriptor_id == ACK)
196 msg = proto_tree_add_none_format(tree,
197 hf_armagetronad_msg_subtree,
198 tvb, offset, data_len + 6,
202 msg = proto_tree_add_none_format(tree,
203 hf_armagetronad_msg_subtree,
204 tvb, offset, data_len + 6,
205 "Message 0x%04x [%s]",
206 message_id, descriptor);
208 msg_tree = proto_item_add_subtree(msg, ett_message);
210 /* DescriptorID field */
211 proto_tree_add_item(msg_tree, hf_armagetronad_descriptor_id, tvb,
214 g_string_append_printf(info, "%s, ", descriptor);
216 /* MessageID field */
217 proto_tree_add_item(msg_tree, hf_armagetronad_message_id, tvb,
218 offset + 2, 2, FALSE);
221 proto_tree_add_item(msg_tree, hf_armagetronad_data_len, tvb,
222 offset + 4, 2, FALSE);
225 add_message_data(tvb, offset + 6, data_len, msg_tree);
230 /* Code to actually dissect the packets */
232 dissect_armagetronad(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
235 proto_tree *armagetronad_tree;
240 if (!is_armagetronad_packet(tvb))
243 info = check_col(pinfo->cinfo, COL_INFO) ? g_string_new("") : NULL;
245 if (check_col(pinfo->cinfo, COL_PROTOCOL))
246 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, FALSE);
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,
264 gsize new_len = info->len - 2; /* Remove the trailing ", " */
266 g_string_truncate(info, new_len);
268 g_string_assign(info, "No message");
270 col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] from 0x%04x",
272 g_string_free(info, TRUE);
278 void proto_register_armagetronad(void)
280 static hf_register_info hf[] = {
281 {&hf_armagetronad_descriptor_id,
282 {"Descriptor", "armagetronad.descriptor_id",
283 FT_UINT16, BASE_DEC, VALS(descriptors), 0x0,
284 "The ID of the descriptor (the command)", HFILL}
286 {&hf_armagetronad_message_id,
287 {"MessageID", "armagetronad.message_id",
288 FT_UINT16, BASE_HEX, NULL, 0x0,
289 "The ID of the message (to ack it)", HFILL}
291 {&hf_armagetronad_data_len,
292 {"DataLen", "armagetronad.data_len",
293 FT_UINT16, BASE_DEC, NULL, 0x0,
294 "The length of the data (in shorts)", HFILL}
296 {&hf_armagetronad_data,
297 {"Data", "armagetronad.data",
298 FT_STRING, BASE_NONE, NULL, 0x0,
299 "The actual data (array of shorts in network order)", HFILL}
301 {&hf_armagetronad_sender_id,
302 {"SenderID", "armagetronad.sender_id",
303 FT_UINT16, BASE_HEX, NULL, 0x0,
304 "The ID of the sender (0x0000 for the server)", HFILL}
306 {&hf_armagetronad_msg_subtree,
307 {"Message", "armagetronad.message",
308 FT_NONE, BASE_NONE, NULL, 0x0,
313 static gint *ett[] = {
319 proto_register_protocol("The Armagetron Advanced OpenGL Tron clone",
320 "Armagetronad", "armagetronad");
322 proto_register_field_array(proto_armagetronad, hf, array_length(hf));
323 proto_register_subtree_array(ett, array_length(ett));
324 new_register_dissector("armagetronad", dissect_armagetronad,
328 void proto_reg_handoff_armagetronad(void)
330 dissector_handle_t armagetronad_handle;
332 armagetronad_handle = find_dissector("armagetronad");
334 dissector_add("udp.port", UDP_PORT_ARMAGETRONAD, armagetronad_handle);
335 dissector_add("udp.port", UDP_PORT_MASTER, armagetronad_handle);