aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-armagetronad.c
blob: 248a2e1c326b908df17c15ad033859c474b53131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* packet-armagetronad.c
 * Routines for the Armagetronad packet dissection
 * Copyright 2005, Guillaume Chazarain <guichaz@yahoo.fr>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
void proto_register_armagetronad(void);
void proto_reg_handoff_armagetronad(void);

/* Initialize the protocol and registered fields */
static int proto_armagetronad = -1;
static int hf_armagetronad_descriptor_id = -1;
static int hf_armagetronad_message_id = -1;
static int hf_armagetronad_data_len = -1;
static int hf_armagetronad_data = -1;
static int hf_armagetronad_sender_id = -1;
static int hf_armagetronad_msg_subtree = -1;

/* Initialize the subtree pointers */
static gint ett_armagetronad = -1;
static gint ett_message = -1;

static dissector_handle_t armagetronad_handle = NULL;

#define ARMAGETRONAD_UDP_PORT_RANGE "4533-4534" /* 4533 is not IANA registered, 4534 is */

/*
 * The ACK packet is so common that we treat it
 * differently: it has no MessageID
 */
#define ACK 1

/*
 * armagetronad-0.2.8.2.1
 * The numbers and names were retrieved at runtime using the
 * 'nDescriptor* descriptors[MAXDESCRIPTORS]' array
 */
static const value_string descriptors[] = {
	{  1, "ack"},
	{  2, "req_info"},
	{  3, "login_deny"},
	{  4, "login_ignore"},
	{  5, "login_accept"},
	{  6, "login1"},
	{  7, "logout"},
	{  8, "sn_ConsoleOut"},
	{  9, "client_cen"},
	{ 10, "version"},
	{ 11, "login2"},
	{ 20, "req_id"},
	{ 21, "id_req_handler"},
	{ 22, "net_destroy"},
	{ 23, "net_control"},
	{ 24, "net_sync"},
	{ 25, "ready to get objects"},
	{ 26, "net_clear"},
	{ 27, "sync_ack"},
	{ 28, "sync_msg"},
	{ 40, "password_request"},
	{ 41, "password_answer"},
	{ 50, "small_server"},
	{ 51, "big_server"},
	{ 52, "small_request"},
	{ 53, "big_request"},
	{ 54, "big_server_master"},
	{ 55, "big_request_master"},
	{ 60, "transfer config"},
	{200, "Chat"},
	{201, "ePlayerNetID"},
	{202, "player_removed_from_game"},
	{203, "Chat Client"},
	{210, "eTimer"},
	{220, "eTeam"},
	{230, "vote cast"},
	{231, "Kick vote"},
	{232, "Server controlled vote"},
	{233, "Server controlled vote expired"},
	{300, "gNetPlayerWall"},
	{310, "game"},
	{311, "client_gamestate"},
	{320, "cycle"},
	{321, "destination"},
	{330, "gAIPlayer"},
	{331, "gAITeam"},
	{340, "zone"},
	{0, NULL}
};

static gboolean
is_armagetronad_packet(tvbuff_t * tvb)
{
	gint offset = 0;

	/* For each message in the frame */
	while (tvb_captured_length_remaining(tvb, offset) > 2) {
		gint data_len = tvb_get_ntohs(tvb, offset + 4) * 2;

#if 0
		/*
		 * If the descriptor_id is not in the table it's possibly
		 * because the protocol evoluated, losing synchronization
		 * with the table, that's why we don't consider that as
		 * a heuristic
		 */
		if (!try_val_to_str(tvb_get_ntohs(tvb, offset), descriptors))
			/* DescriptorID not found in the table */
			return FALSE;
#endif

		if (!tvb_bytes_exist(tvb, offset + 6, data_len))
			/* Advertised length too long */
			return FALSE;

		offset += 6 + data_len;
	}

	/* The packed should end with a 2 bytes ID */
	return tvb_captured_length_remaining(tvb, offset) == 2;
}

static void
add_message_data(tvbuff_t * tvb, gint offset, gint data_len, proto_tree * tree)
{
	if (!tree)
		return;

	/*
	 * XXX - if these are text strings, as the original dissector
	 * treated them as, why the byte swapping?  That would make
	 * sense only if they're UCS-2 strings, but the rest of the
	 * code wasn't treating them as such.
	 *
	 * Just treat it as a byte array.  If that's wrong, submit
	 * a change, with a sample packet, that treats it as whatever
	 * it is, an with a comment that *states* what it is.
	 *
	 * XXX - this claimed that "Armagetronad swaps unconditionally",
	 * but I'm not seeing any obvious unconditional byte-swapping
	 * in the code, and if somebody's never seen a big-endian
	 * machine in their life, they may well confuse "convert to
	 * network byte order" with "unconditionally swap".  So
	 * if you want to dump this out as a sequence of shorts,
	 * fetch the shorts one at a time using ENC_BIG_ENDIAN.
	 */
	proto_tree_add_item(tree, hf_armagetronad_data, tvb, offset,
			      data_len, ENC_NA);
}

static gint
add_message(tvbuff_t * tvb, gint offset, proto_tree * tree, wmem_strbuf_t * info)
{
	guint16      descriptor_id, message_id;
	gint         data_len;
	proto_item  *msg;
	proto_tree  *msg_tree;
	const gchar *descriptor;

	descriptor_id = tvb_get_ntohs(tvb, offset);
	message_id = tvb_get_ntohs(tvb, offset + 2);
	data_len = tvb_get_ntohs(tvb, offset + 4) * 2;

	/* Message subtree */
	descriptor = val_to_str(descriptor_id, descriptors, "Unknown (%u)");
	if (descriptor_id == ACK)
		msg = proto_tree_add_none_format(tree,
						 hf_armagetronad_msg_subtree,
						 tvb, offset, data_len + 6,
						 "ACK %d messages",
						 data_len / 2);
	else
		msg = proto_tree_add_none_format(tree,
						 hf_armagetronad_msg_subtree,
						 tvb, offset, data_len + 6,
						 "Message 0x%04x [%s]",
						 message_id, descriptor);

	msg_tree = proto_item_add_subtree(msg, ett_message);

	/* DescriptorID field */
	proto_tree_add_item(msg_tree, hf_armagetronad_descriptor_id, tvb,
			    offset, 2, ENC_BIG_ENDIAN);
	if (info)
		wmem_strbuf_append_printf(info, "%s, ", descriptor);

	/* MessageID field */
	proto_tree_add_item(msg_tree, hf_armagetronad_message_id, tvb,
			    offset + 2, 2, ENC_BIG_ENDIAN);

	/* DataLen field */
	proto_tree_add_item(msg_tree, hf_armagetronad_data_len, tvb,
			    offset + 4, 2, ENC_BIG_ENDIAN);

	/* Data field */
	add_message_data(tvb, offset + 6, data_len, msg_tree);

	return data_len + 6;
}

/* Code to actually dissect the packets */
static gint
dissect_armagetronad(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void * data _U_)
{
	proto_item    *ti;
	proto_tree    *armagetronad_tree;
	guint16        sender;
	gint           offset = 0;
	wmem_strbuf_t *info;
	gsize          new_len;

	if (!is_armagetronad_packet(tvb))
		return 0;

	info = wmem_strbuf_new(wmem_packet_scope(), "");

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "Armagetronad");

	col_clear(pinfo->cinfo, COL_INFO);

	ti = proto_tree_add_item(tree, proto_armagetronad, tvb, 0, -1, ENC_NA);
	armagetronad_tree = proto_item_add_subtree(ti, ett_armagetronad);

	/* For each message in the frame */
	while (tvb_reported_length_remaining(tvb, offset) > 2)
		offset += add_message(tvb, offset, armagetronad_tree, info);

	/* After the messages, comes the SenderID */
	sender = tvb_get_ntohs(tvb, offset);
	proto_tree_add_item(ti, hf_armagetronad_sender_id, tvb, offset, 2,
			    ENC_BIG_ENDIAN);

	new_len = wmem_strbuf_get_len(info) - 2;	/* Remove the trailing ", " */
	if (new_len > 0)
		wmem_strbuf_truncate(info, new_len);
	else
		info = wmem_strbuf_new(wmem_packet_scope(), "No message");

	col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] from 0x%04x",
		     wmem_strbuf_get_str(info), sender);

	return offset + 2;
}

void proto_register_armagetronad(void)
{
	static hf_register_info hf[] = {
		{&hf_armagetronad_descriptor_id,
		 {"Descriptor", "armagetronad.descriptor_id",
		  FT_UINT16, BASE_DEC, VALS(descriptors), 0x0,
		  "The ID of the descriptor (the command)", HFILL}
		 },
		{&hf_armagetronad_message_id,
		 {"MessageID", "armagetronad.message_id",
		  FT_UINT16, BASE_HEX, NULL, 0x0,
		  "The ID of the message (to ack it)", HFILL}
		 },
		{&hf_armagetronad_data_len,
		 {"DataLen", "armagetronad.data_len",
		  FT_UINT16, BASE_DEC, NULL, 0x0,
		  "The length of the data (in shorts)", HFILL}
		 },
		{&hf_armagetronad_data,
		 {"Data", "armagetronad.data",
		  FT_BYTES, BASE_NONE, NULL, 0x0,
		  "The actual data (array of shorts in network order)", HFILL}
		 },
		{&hf_armagetronad_sender_id,
		 {"SenderID", "armagetronad.sender_id",
		  FT_UINT16, BASE_HEX, NULL, 0x0,
		  "The ID of the sender (0x0000 for the server)", HFILL}
		 },
		{&hf_armagetronad_msg_subtree,
		 {"Message", "armagetronad.message",
		  FT_NONE, BASE_NONE, NULL, 0x0,
		  "A message", HFILL}
		 }
	};

	static gint *ett[] = {
		&ett_armagetronad,
		&ett_message
	};

	proto_armagetronad = proto_register_protocol("The Armagetron Advanced OpenGL Tron clone", "Armagetronad", "armagetronad");

	proto_register_field_array(proto_armagetronad, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	armagetronad_handle = register_dissector("armagetronad", dissect_armagetronad, proto_armagetronad);
}

void proto_reg_handoff_armagetronad(void)
{
	dissector_add_uint_range_with_preference("udp.port", ARMAGETRONAD_UDP_PORT_RANGE, armagetronad_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */