Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-armagetronad.c
blobf9afd4dd0882dc2ad32762e82ff57301bc84e7c6
1 /* packet-armagetronad.c
2 * Routines for the Armagetronad packet dissection
3 * Copyright 2005, Guillaume Chazarain <guichaz@yahoo.fr>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include <epan/packet.h>
15 void proto_register_armagetronad(void);
16 void proto_reg_handoff_armagetronad(void);
18 /* Initialize the protocol and registered fields */
19 static int proto_armagetronad;
20 static int hf_armagetronad_descriptor_id;
21 static int hf_armagetronad_message_id;
22 static int hf_armagetronad_data_len;
23 static int hf_armagetronad_data;
24 static int hf_armagetronad_sender_id;
25 static int hf_armagetronad_msg_subtree;
27 /* Initialize the subtree pointers */
28 static int ett_armagetronad;
29 static int ett_message;
31 static dissector_handle_t armagetronad_handle;
33 #define ARMAGETRONAD_UDP_PORT_RANGE "4533-4534" /* 4533 is not IANA registered, 4534 is */
36 * The ACK packet is so common that we treat it
37 * differently: it has no MessageID
39 #define ACK 1
42 * armagetronad-0.2.8.2.1
43 * The numbers and names were retrieved at runtime using the
44 * 'nDescriptor* descriptors[MAXDESCRIPTORS]' array
46 static const value_string descriptors[] = {
47 { 1, "ack"},
48 { 2, "req_info"},
49 { 3, "login_deny"},
50 { 4, "login_ignore"},
51 { 5, "login_accept"},
52 { 6, "login1"},
53 { 7, "logout"},
54 { 8, "sn_ConsoleOut"},
55 { 9, "client_cen"},
56 { 10, "version"},
57 { 11, "login2"},
58 { 20, "req_id"},
59 { 21, "id_req_handler"},
60 { 22, "net_destroy"},
61 { 23, "net_control"},
62 { 24, "net_sync"},
63 { 25, "ready to get objects"},
64 { 26, "net_clear"},
65 { 27, "sync_ack"},
66 { 28, "sync_msg"},
67 { 40, "password_request"},
68 { 41, "password_answer"},
69 { 50, "small_server"},
70 { 51, "big_server"},
71 { 52, "small_request"},
72 { 53, "big_request"},
73 { 54, "big_server_master"},
74 { 55, "big_request_master"},
75 { 60, "transfer config"},
76 {200, "Chat"},
77 {201, "ePlayerNetID"},
78 {202, "player_removed_from_game"},
79 {203, "Chat Client"},
80 {210, "eTimer"},
81 {220, "eTeam"},
82 {230, "vote cast"},
83 {231, "Kick vote"},
84 {232, "Server controlled vote"},
85 {233, "Server controlled vote expired"},
86 {300, "gNetPlayerWall"},
87 {310, "game"},
88 {311, "client_gamestate"},
89 {320, "cycle"},
90 {321, "destination"},
91 {330, "gAIPlayer"},
92 {331, "gAITeam"},
93 {340, "zone"},
94 {0, NULL}
97 static bool
98 is_armagetronad_packet(tvbuff_t * tvb)
100 int offset = 0;
102 /* For each message in the frame */
103 while (tvb_captured_length_remaining(tvb, offset) > 2) {
104 int data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
106 #if 0
108 * If the descriptor_id is not in the table it's possibly
109 * because the protocol evoluated, losing synchronization
110 * with the table, that's why we don't consider that as
111 * a heuristic
113 if (!try_val_to_str(tvb_get_ntohs(tvb, offset), descriptors))
114 /* DescriptorID not found in the table */
115 return false;
116 #endif
118 if (!tvb_bytes_exist(tvb, offset + 6, data_len))
119 /* Advertised length too long */
120 return false;
122 offset += 6 + data_len;
125 /* The packed should end with a 2 bytes ID */
126 return tvb_captured_length_remaining(tvb, offset) == 2;
129 static void
130 add_message_data(tvbuff_t * tvb, int offset, int data_len, proto_tree * tree)
132 if (!tree)
133 return;
136 * XXX - if these are text strings, as the original dissector
137 * treated them as, why the byte swapping? That would make
138 * sense only if they're UCS-2 strings, but the rest of the
139 * code wasn't treating them as such.
141 * Just treat it as a byte array. If that's wrong, submit
142 * a change, with a sample packet, that treats it as whatever
143 * it is, an with a comment that *states* what it is.
145 * XXX - this claimed that "Armagetronad swaps unconditionally",
146 * but I'm not seeing any obvious unconditional byte-swapping
147 * in the code, and if somebody's never seen a big-endian
148 * machine in their life, they may well confuse "convert to
149 * network byte order" with "unconditionally swap". So
150 * if you want to dump this out as a sequence of shorts,
151 * fetch the shorts one at a time using ENC_BIG_ENDIAN.
153 proto_tree_add_item(tree, hf_armagetronad_data, tvb, offset,
154 data_len, ENC_NA);
157 static int
158 add_message(tvbuff_t * tvb, int offset, proto_tree * tree, wmem_strbuf_t * info)
160 uint16_t descriptor_id, message_id;
161 int data_len;
162 proto_item *msg;
163 proto_tree *msg_tree;
164 const char *descriptor;
166 descriptor_id = tvb_get_ntohs(tvb, offset);
167 message_id = tvb_get_ntohs(tvb, offset + 2);
168 data_len = tvb_get_ntohs(tvb, offset + 4) * 2;
170 /* Message subtree */
171 descriptor = val_to_str(descriptor_id, descriptors, "Unknown (%u)");
172 if (descriptor_id == ACK)
173 msg = proto_tree_add_none_format(tree,
174 hf_armagetronad_msg_subtree,
175 tvb, offset, data_len + 6,
176 "ACK %d messages",
177 data_len / 2);
178 else
179 msg = proto_tree_add_none_format(tree,
180 hf_armagetronad_msg_subtree,
181 tvb, offset, data_len + 6,
182 "Message 0x%04x [%s]",
183 message_id, descriptor);
185 msg_tree = proto_item_add_subtree(msg, ett_message);
187 /* DescriptorID field */
188 proto_tree_add_item(msg_tree, hf_armagetronad_descriptor_id, tvb,
189 offset, 2, ENC_BIG_ENDIAN);
190 if (info)
191 wmem_strbuf_append_printf(info, "%s, ", descriptor);
193 /* MessageID field */
194 proto_tree_add_item(msg_tree, hf_armagetronad_message_id, tvb,
195 offset + 2, 2, ENC_BIG_ENDIAN);
197 /* DataLen field */
198 proto_tree_add_item(msg_tree, hf_armagetronad_data_len, tvb,
199 offset + 4, 2, ENC_BIG_ENDIAN);
201 /* Data field */
202 add_message_data(tvb, offset + 6, data_len, msg_tree);
204 return data_len + 6;
207 /* Code to actually dissect the packets */
208 static int
209 dissect_armagetronad(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void * data _U_)
211 proto_item *ti;
212 proto_tree *armagetronad_tree;
213 uint16_t sender;
214 int offset = 0;
215 wmem_strbuf_t *info;
216 size_t new_len;
218 if (!is_armagetronad_packet(tvb))
219 return 0;
221 info = wmem_strbuf_new(pinfo->pool, "");
223 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Armagetronad");
225 col_clear(pinfo->cinfo, COL_INFO);
227 ti = proto_tree_add_item(tree, proto_armagetronad, tvb, 0, -1, ENC_NA);
228 armagetronad_tree = proto_item_add_subtree(ti, ett_armagetronad);
230 /* For each message in the frame */
231 while (tvb_reported_length_remaining(tvb, offset) > 2)
232 offset += add_message(tvb, offset, armagetronad_tree, info);
234 /* After the messages, comes the SenderID */
235 sender = tvb_get_ntohs(tvb, offset);
236 proto_tree_add_item(ti, hf_armagetronad_sender_id, tvb, offset, 2,
237 ENC_BIG_ENDIAN);
239 new_len = wmem_strbuf_get_len(info) - 2; /* Remove the trailing ", " */
240 if (new_len > 0)
241 wmem_strbuf_truncate(info, new_len);
242 else
243 info = wmem_strbuf_new(pinfo->pool, "No message");
245 col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] from 0x%04x",
246 wmem_strbuf_get_str(info), sender);
248 return offset + 2;
251 void proto_register_armagetronad(void)
253 static hf_register_info hf[] = {
254 {&hf_armagetronad_descriptor_id,
255 {"Descriptor", "armagetronad.descriptor_id",
256 FT_UINT16, BASE_DEC, VALS(descriptors), 0x0,
257 "The ID of the descriptor (the command)", HFILL}
259 {&hf_armagetronad_message_id,
260 {"MessageID", "armagetronad.message_id",
261 FT_UINT16, BASE_HEX, NULL, 0x0,
262 "The ID of the message (to ack it)", HFILL}
264 {&hf_armagetronad_data_len,
265 {"DataLen", "armagetronad.data_len",
266 FT_UINT16, BASE_DEC, NULL, 0x0,
267 "The length of the data (in shorts)", HFILL}
269 {&hf_armagetronad_data,
270 {"Data", "armagetronad.data",
271 FT_BYTES, BASE_NONE, NULL, 0x0,
272 "The actual data (array of shorts in network order)", HFILL}
274 {&hf_armagetronad_sender_id,
275 {"SenderID", "armagetronad.sender_id",
276 FT_UINT16, BASE_HEX, NULL, 0x0,
277 "The ID of the sender (0x0000 for the server)", HFILL}
279 {&hf_armagetronad_msg_subtree,
280 {"Message", "armagetronad.message",
281 FT_NONE, BASE_NONE, NULL, 0x0,
282 "A message", HFILL}
286 static int *ett[] = {
287 &ett_armagetronad,
288 &ett_message
291 proto_armagetronad = proto_register_protocol("The Armagetron Advanced OpenGL Tron clone", "Armagetronad", "armagetronad");
293 proto_register_field_array(proto_armagetronad, hf, array_length(hf));
294 proto_register_subtree_array(ett, array_length(ett));
295 armagetronad_handle = register_dissector("armagetronad", dissect_armagetronad, proto_armagetronad);
298 void proto_reg_handoff_armagetronad(void)
300 dissector_add_uint_range_with_preference("udp.port", ARMAGETRONAD_UDP_PORT_RANGE, armagetronad_handle);
304 * Editor modelines - https://www.wireshark.org/tools/modelines.html
306 * Local variables:
307 * c-basic-offset: 8
308 * tab-width: 8
309 * indent-tabs-mode: t
310 * End:
312 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
313 * :indentSize=8:tabSize=8:noTabs=false: