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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
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
[] = {
76 {21, "id_req_handler"},
80 {25, "ready to get objects"},
84 {40, "password_request"},
85 {41, "password_answer"},
88 {52, "small_request"},
90 {54, "big_server_master"},
91 {55, "big_request_master"},
92 {60, "transfer config"},
94 {201, "ePlayerNetID"},
95 {202, "player_removed_from_game"},
101 {232, "Server controlled vote"},
102 {233, "Server controlled vote expired"},
103 {300, "gNetPlayerWall"},
105 {311, "client_gamestate"},
107 {321, "destination"},
115 is_armagetronad_packet(tvbuff_t
* tvb
)
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;
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
130 if (!try_val_to_str(tvb_get_ntohs(tvb
, offset
), descriptors
))
131 /* DescriptorID not found in the table */
135 if (!tvb_bytes_exist(tvb
, offset
+ 6, data_len
))
136 /* Advertised length too long */
139 offset
+= 6 + data_len
;
142 /* The packed should end with a 2 bytes ID */
143 return tvb_length_remaining(tvb
, offset
) == 2;
147 add_message_data(tvbuff_t
* tvb
, gint offset
, gint data_len
, proto_tree
* tree
)
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
171 /* Armagetronad swaps unconditionally */
177 proto_tree_add_string(tree
, hf_armagetronad_data
, tvb
, offset
,
178 data_len
, (gchar
*) data
);
182 add_message(tvbuff_t
* tvb
, gint offset
, proto_tree
* tree
, wmem_strbuf_t
* info
)
184 guint16 descriptor_id
, message_id
;
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,
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
);
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
);
222 proto_tree_add_item(msg_tree
, hf_armagetronad_data_len
, tvb
,
223 offset
+ 4, 2, ENC_BIG_ENDIAN
);
226 add_message_data(tvb
, offset
+ 6, data_len
, msg_tree
);
231 /* Code to actually dissect the packets */
233 dissect_armagetronad(tvbuff_t
* tvb
, packet_info
* pinfo
, proto_tree
* tree
, void * data _U_
)
236 proto_tree
*armagetronad_tree
;
242 if (!is_armagetronad_packet(tvb
))
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,
263 new_len
= wmem_strbuf_get_len(info
) - 2; /* Remove the trailing ", " */
265 wmem_strbuf_truncate(info
, new_len
);
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
);
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,
310 static gint
*ett
[] = {
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
,
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
);