d7f0d67abefae6272a6d19f9972c796a1c410658
[obnox/wireshark/wip.git] / epan / dissectors / packet-armagetronad.c
1 /* packet-armagetronad.c
2  * Routines for the Armagetronad packet dissection
3  * Copyright 2005, Guillaume Chazarain <guichaz@yahoo.fr>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31
32 #include <epan/packet.h>
33 #include <epan/emem.h>
34
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;
43
44 /* Initialize the subtree pointers */
45 static gint ett_armagetronad = -1;
46 static gint ett_message = -1;
47
48 #define UDP_PORT_ARMAGETRONAD 4534
49 #define UDP_PORT_MASTER 4533
50
51 /*
52  * The ACK packet is so common that we treat it
53  * differently: it has no MessageID
54  */
55 #define ACK 1
56
57 /*
58  * armagetronad-0.2.8.2.1
59  * The numbers and names were retrieved at runtime using the
60  * 'nDescriptor* descriptors[MAXDESCRIPTORS]' array
61  */
62 static const value_string descriptors[] = {
63         {1, "ack"},
64         {2, "req_info"},
65         {3, "login_deny"},
66         {4, "login_ignore"},
67         {5, "login_accept"},
68         {6, "login1"},
69         {7, "logout"},
70         {8, "sn_ConsoleOut"},
71         {9, "client_cen"},
72         {10, "version"},
73         {11, "login2"},
74         {20, "req_id"},
75         {21, "id_req_handler"},
76         {22, "net_destroy"},
77         {23, "net_control"},
78         {24, "net_sync"},
79         {25, "ready to get objects"},
80         {26, "net_clear"},
81         {27, "sync_ack"},
82         {28, "sync_msg"},
83         {40, "password_request"},
84         {41, "password_answer"},
85         {50, "small_server"},
86         {51, "big_server"},
87         {52, "small_request"},
88         {53, "big_request"},
89         {54, "big_server_master"},
90         {55, "big_request_master"},
91         {60, "transfer config"},
92         {200, "Chat"},
93         {201, "ePlayerNetID"},
94         {202, "player_removed_from_game"},
95         {203, "Chat Client"},
96         {210, "eTimer"},
97         {220, "eTeam"},
98         {230, "vote cast"},
99         {231, "Kick vote"},
100         {232, "Server controlled vote"},
101         {233, "Server controlled vote expired"},
102         {300, "gNetPlayerWall"},
103         {310, "game"},
104         {311, "client_gamestate"},
105         {320, "cycle"},
106         {321, "destination"},
107         {330, "gAIPlayer"},
108         {331, "gAITeam"},
109         {340, "zone"},
110         {0, NULL}
111 };
112
113 static gboolean
114 is_armagetronad_packet(tvbuff_t * tvb)
115 {
116         gint offset = 0;
117
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;
121
122 #if 0
123                 /*
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
127                  * a heuristic
128                  */
129                 if (!match_strval(tvb_get_ntohs(tvb, offset), descriptors))
130                         /* DescriptorID not found in the table */
131                         return FALSE;
132 #endif
133
134                 if (!tvb_bytes_exist(tvb, offset + 6, data_len))
135                         /* Advertised length too long */
136                         return FALSE;
137
138                 offset += 6 + data_len;
139         }
140
141         /* The packed should end with a 2 bytes ID */
142         return tvb_length_remaining(tvb, offset) == 2;
143 }
144
145 static void
146 add_message_data(tvbuff_t * tvb, gint offset, gint data_len, proto_tree * tree)
147 {
148         gchar *data = NULL;
149         gchar tmp;
150         int i;
151
152         if (!tree)
153                 return;
154
155         data = tvb_memcpy(tvb, ep_alloc(data_len + 1), offset, data_len);
156         data[data_len] = '\0';
157
158         for (i = 0; i < data_len; i += 2) {
159                 /*
160                  * There must be a better way to tell
161                  * Wireshark not to stop on null bytes
162                  * as the length is known
163                  */
164                 if (!data[i])
165                         data[i] = ' ';
166
167                 if (!data[i+1])
168                         data[i+1] = ' ';
169
170                 /* Armagetronad swaps unconditionally */
171                 tmp = data[i];
172                 data[i] = data[i+1];
173                 data[i+1] = tmp;
174         }
175
176         proto_tree_add_string(tree, hf_armagetronad_data, tvb, offset,
177                               data_len, (gchar *) data);
178 }
179
180 static gint
181 add_message(tvbuff_t * tvb, gint offset, proto_tree * tree, GString * info)
182 {
183         guint16 descriptor_id, message_id;
184         gint data_len;
185         proto_item *msg;
186         proto_tree *msg_tree;
187         const gchar *descriptor;
188
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;
192
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,
199                                                  "ACK %d messages",
200                                                  data_len / 2);
201         else
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);
207
208         msg_tree = proto_item_add_subtree(msg, ett_message);
209
210         /* DescriptorID field */
211         proto_tree_add_item(msg_tree, hf_armagetronad_descriptor_id, tvb,
212                             offset, 2, FALSE);
213         if (info)
214                 g_string_append_printf(info, "%s, ", descriptor);
215
216         /* MessageID field */
217         proto_tree_add_item(msg_tree, hf_armagetronad_message_id, tvb,
218                             offset + 2, 2, FALSE);
219
220         /* DataLen field */
221         proto_tree_add_item(msg_tree, hf_armagetronad_data_len, tvb,
222                             offset + 4, 2, FALSE);
223
224         /* Data field */
225         add_message_data(tvb, offset + 6, data_len, msg_tree);
226
227         return data_len + 6;
228 }
229
230 /* Code to actually dissect the packets */
231 static gint
232 dissect_armagetronad(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
233 {
234         proto_item *ti;
235         proto_tree *armagetronad_tree;
236         guint16 sender;
237         gint offset = 0;
238         GString *info;
239
240         if (!is_armagetronad_packet(tvb))
241                 return 0;
242
243         info = check_col(pinfo->cinfo, COL_INFO) ? g_string_new("") : NULL;
244
245         if (check_col(pinfo->cinfo, COL_PROTOCOL))
246                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Armagetronad");
247
248         if (info)
249                 col_clear(pinfo->cinfo, COL_INFO);
250
251         ti = proto_tree_add_item(tree, proto_armagetronad, tvb, 0, -1, FALSE);
252         armagetronad_tree = proto_item_add_subtree(ti, ett_armagetronad);
253
254         /* For each message in the frame */
255         while (tvb_length_remaining(tvb, offset) > 2)
256                 offset += add_message(tvb, offset, armagetronad_tree, info);
257
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                             FALSE);
262
263         if (info) {
264                 gsize new_len = info->len - 2;  /* Remove the trailing ", " */
265                 if (new_len > 0)
266                         g_string_truncate(info, new_len);
267                 else
268                         g_string_assign(info, "No message");
269
270                 col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] from 0x%04x",
271                              info->str, sender);
272                 g_string_free(info, TRUE);
273         }
274
275         return offset + 2;
276 }
277
278 void proto_register_armagetronad(void)
279 {
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}
285                  },
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}
290                  },
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}
295                  },
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}
300                  },
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}
305                  },
306                 {&hf_armagetronad_msg_subtree,
307                  {"Message", "armagetronad.message",
308                   FT_NONE, BASE_NONE, NULL, 0x0,
309                   "A message", HFILL}
310                  }
311         };
312
313         static gint *ett[] = {
314                 &ett_armagetronad,
315                 &ett_message
316         };
317
318         proto_armagetronad =
319             proto_register_protocol("The Armagetron Advanced OpenGL Tron clone",
320                                     "Armagetronad", "armagetronad");
321
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,
325                                proto_armagetronad);
326 }
327
328 void proto_reg_handoff_armagetronad(void)
329 {
330         dissector_handle_t armagetronad_handle;
331
332         armagetronad_handle = find_dissector("armagetronad");
333
334         dissector_add("udp.port", UDP_PORT_ARMAGETRONAD, armagetronad_handle);
335         dissector_add("udp.port", UDP_PORT_MASTER, armagetronad_handle);
336 }