MSWSP: add ids for another unknown Property Set
[wireshark-wip.git] / epan / dissectors / packet-quake3.c
blob598e24d2fcbfdbe5a83752ad3ddfab7b0695d9de
1 /* packet-quake3.c
2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * Copied from packet-quake2.c
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 All informations used in this decoding were gathered from
32 * some own captures of a private server,
33 * the "Server commands howto" document written by id Software
34 (http://www.quake3arena.com/tech/ServerCommandsHowto.html)
35 * source code of the game server browser tool qstat
36 (http://www.qstat.org).
40 #include "config.h"
42 #include <string.h>
43 #include <glib.h>
44 #include <epan/packet.h>
45 #include <epan/prefs.h>
46 #include <epan/addr_resolv.h>
48 static int proto_quake3 = -1;
50 static int hf_quake3_direction = -1;
51 static int hf_quake3_connectionless = -1;
52 static int hf_quake3_game = -1;
53 static int hf_quake3_connectionless_marker = -1;
54 static int hf_quake3_connectionless_text = -1;
55 static int hf_quake3_connectionless_command = -1;
56 static int hf_quake3_server_addr = -1;
57 static int hf_quake3_server_port = -1;
58 static int hf_quake3_game_seq1 = -1;
59 static int hf_quake3_game_rel1 = -1;
60 static int hf_quake3_game_seq2 = -1;
61 static int hf_quake3_game_rel2 = -1;
62 static int hf_quake3_game_qport = -1;
64 static gint ett_quake3 = -1;
65 static gint ett_quake3_connectionless = -1;
66 static gint ett_quake3_connectionless_text = -1;
67 static gint ett_quake3_server = -1;
68 static gint ett_quake3_game = -1;
69 static gint ett_quake3_game_seq1 = -1;
70 static gint ett_quake3_game_seq2 = -1;
71 static gint ett_quake3_game_clc = -1;
72 static gint ett_quake3_game_svc = -1;
74 static dissector_handle_t data_handle;
76 #define QUAKE3_SERVER_PORT 27960
77 #define QUAKE3_MASTER_PORT 27950
78 static guint gbl_quake3_server_port=QUAKE3_SERVER_PORT;
79 static guint gbl_quake3_master_port=QUAKE3_MASTER_PORT;
82 static const value_string names_direction[] = {
83 #define DIR_UNKNOWN 0
84 { DIR_UNKNOWN, "Unknown" },
85 #define DIR_C2S 1
86 { DIR_C2S, "Client to Server" },
87 #define DIR_S2C 2
88 { DIR_S2C, "Server to Client" },
89 #define DIR_C2M 3
90 { DIR_C2M, "Client to Master" },
91 #define DIR_M2C 4
92 { DIR_M2C, "Master to Client" },
93 { 0, NULL }
97 #define string_starts_with(s1, s2) (strncmp((s1), (s2), strlen(s2)) == 0)
100 static const value_string names_command[] = {
101 #define COMMAND_UNKNOWN 0
102 { COMMAND_UNKNOWN, "Unknown" },
103 #define COMMAND_statusResponse 1
104 { COMMAND_statusResponse, "Reply Status" },
105 #define COMMAND_getstatus 2
106 { COMMAND_getstatus, "Request Status" },
107 #define COMMAND_infoResponse 3
108 { COMMAND_infoResponse, "Reply Info" },
109 #define COMMAND_getinfo 4
110 { COMMAND_getinfo, "Request Info" },
111 #define COMMAND_challengeResponse 5
112 { COMMAND_challengeResponse, "Reply Challenge" },
113 #define COMMAND_getchallenge 6
114 { COMMAND_getchallenge, "Request Challenge" },
115 #define COMMAND_connectResponse 7
116 { COMMAND_connectResponse, "Reply Connect" },
117 #define COMMAND_connect 8
118 { COMMAND_connect, "Request Connect" },
119 #define COMMAND_rconResponse 9
120 { COMMAND_rconResponse, "Reply Remote Command" },
121 #define COMMAND_rcon 10
122 { COMMAND_rcon, "Request Remote Command" },
123 #define COMMAND_getmotdResponse 11
124 { COMMAND_getmotdResponse, "Reply Motto of the Day" },
125 #define COMMAND_getmotd 12
126 { COMMAND_getmotd, "Request Motto of the Day" },
127 #define COMMAND_getserversResponse 13
128 { COMMAND_getserversResponse, "Reply Servers" },
129 #define COMMAND_getservers 14
130 { COMMAND_getservers, "Request Servers" },
131 #define COMMAND_getKeyAuthorize 15
132 { COMMAND_getKeyAuthorize, "Request Key Authorization" },
133 #define COMMAND_getIpAuthorize 16
134 { COMMAND_getIpAuthorize, "Request IP Authorization" },
135 #define COMMAND_ipAuthorize 17
136 { COMMAND_ipAuthorize, "Reply IP Authorization" },
137 { 0, NULL }
141 static void
142 dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_,
143 proto_tree *tree, int* direction)
145 proto_tree *cl_tree = NULL;
146 proto_item *text_item = NULL;
147 proto_tree *text_tree = NULL;
148 guint8 text[2048];
149 int len;
150 int offset;
151 guint32 marker;
152 int command;
153 int command_len;
154 gboolean command_finished = FALSE;
156 marker = tvb_get_ntohl(tvb, 0);
157 if (tree) {
158 proto_item *cl_item = NULL;
159 cl_item = proto_tree_add_text(tree, tvb,
160 0, -1, "Connectionless");
161 cl_tree = proto_item_add_subtree(cl_item, ett_quake3_connectionless);
163 proto_tree_add_uint(cl_tree, hf_quake3_connectionless_marker,
164 tvb, 0, 4, marker);
166 /* all the rest of the packet is just text */
167 offset = 4;
169 len = tvb_get_nstringz0(tvb, offset, sizeof(text), text);
170 if (cl_tree) {
171 text_item = proto_tree_add_string(cl_tree,
172 hf_quake3_connectionless_text,
173 tvb, offset, len + 1, text);
174 text_tree = proto_item_add_subtree(text_item, ett_quake3_connectionless_text);
177 command = COMMAND_UNKNOWN;
178 command_len = 0;
180 if (strncmp(text, "statusResponse", 14) == 0) {
181 command = COMMAND_statusResponse;
182 *direction = DIR_S2C;
183 command_len = 14;
185 else if (strncmp(text, "getstatus", 9) == 0) {
186 command = COMMAND_getstatus;
187 *direction = DIR_C2S;
188 command_len = 9;
190 else if (strncmp(text, "infoResponse", 12) == 0) {
191 command = COMMAND_infoResponse;
192 *direction = DIR_S2C;
193 command_len = 12;
195 else if (strncmp(text, "getinfo", 7) == 0) {
196 command = COMMAND_getinfo;
197 *direction = DIR_C2S;
198 command_len = 7;
200 else if (strncmp(text, "challengeResponse", 17) == 0) {
201 command = COMMAND_challengeResponse;
202 *direction = DIR_S2C;
203 command_len = 17;
205 else if (strncmp(text, "getchallenge", 12) == 0) {
206 command = COMMAND_getchallenge;
207 *direction = DIR_C2S;
208 command_len = 12;
210 else if (strncmp(text, "connectResponse", 15) == 0) {
211 command = COMMAND_connectResponse;
212 *direction = DIR_S2C;
213 command_len = 15;
215 else if (strncmp(text, "connect", 7) == 0) {
216 command = COMMAND_connect;
217 *direction = DIR_C2S;
218 command_len = 7;
220 else if (strncmp(text, "rconResponse", 12) == 0) {
221 command = COMMAND_rconResponse;
222 *direction = DIR_S2C;
223 command_len = 12;
225 else if (strncmp(text, "rcon", 4) == 0) {
226 command = COMMAND_rcon;
227 *direction = DIR_C2S;
228 command_len = 4;
230 else if (strncmp(text, "getmotdResponse", 15) == 0) {
231 command = COMMAND_getmotdResponse;
232 *direction = DIR_M2C;
233 command_len = 15;
235 else if (strncmp(text, "getmotd", 7) == 0) {
236 command = COMMAND_getmotd;
237 *direction = DIR_C2M;
238 command_len = 7;
240 else if (strncmp(text, "getserversResponse", 18) == 0) {
241 int base;
242 command = COMMAND_getserversResponse;
243 *direction = DIR_M2C;
244 command_len = 18;
245 /* The data can contain 0's, and the text string is binary
246 anyway, so better replace the text string after the first
247 \ with "<DATA>". */
248 if (text_item) {
249 /* first correct the length until the end of the packet */
250 proto_item_set_len(text_item, tvb_length_remaining(tvb, offset));
251 /* then replace the text */
252 proto_item_set_text(text_item, "Text: getserversResponse<DATA>");
254 if (text_tree)
255 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
256 tvb, offset, command_len,
257 val_to_str_const(command, names_command, "Unknown"));
258 command_finished = TRUE;
260 /* now we decode all the rest */
261 base = offset + 18;
262 /* '/', ip-address in network order, port in network order */
263 while (tvb_reported_length_remaining(tvb, base) >= 7) {
264 guint32 ip_addr;
265 guint16 udp_port;
267 ip_addr = tvb_get_ipv4(tvb, base + 1);
268 udp_port = tvb_get_ntohs(tvb, base + 5);
270 /* It may be a good idea to create a conversation for
271 every server in this list, as we'll see at
272 least a getinfo request to each of them and they
273 may run on totally unusual ports. */
275 if (text_tree) {
276 proto_item *server_item;
277 proto_tree *server_tree;
278 server_item = proto_tree_add_text(text_tree,
279 tvb, base, 7,
280 "Server: %s:%u",
281 get_hostname(ip_addr),
282 udp_port);
283 server_tree = proto_item_add_subtree(
284 server_item,
285 ett_quake3_server);
286 proto_tree_add_ipv4(server_tree, hf_quake3_server_addr,
287 tvb, base + 1, 4, ip_addr);
288 proto_tree_add_uint(server_tree, hf_quake3_server_port,
289 tvb, base + 5, 2, udp_port);
292 base += 7;
295 else if (strncmp(text, "getservers", 10) == 0) {
296 command = COMMAND_getservers;
297 *direction = DIR_C2M;
298 command_len = 10;
300 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
301 command = COMMAND_getKeyAuthorize;
302 *direction = DIR_C2M;
303 command_len = 15;
305 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
306 command = COMMAND_getIpAuthorize;
307 *direction = DIR_C2M;
308 command_len = 14;
310 else if (strncmp(text, "ipAuthorize", 11) == 0) {
311 command = COMMAND_ipAuthorize;
312 *direction = DIR_M2C;
313 command_len = 11;
315 else {
316 *direction = DIR_UNKNOWN;
319 if (text_tree && command_finished == FALSE) {
320 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
321 tvb, offset, command_len,
322 val_to_str_const(command, names_command, "Unknown"));
325 /*offset += len + 1;*/
330 static void
331 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
332 proto_tree *tree)
334 /* this shouldn't be too difficult */
335 call_dissector(data_handle,tvb, pinfo, tree);
339 static void
340 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
341 proto_tree *tree)
343 /* It is totally forbidden to decode this any further,
344 I wont do it. */
345 call_dissector(data_handle,tvb, pinfo, tree);
349 static const value_string names_reliable[] = {
350 { 0, "Non Reliable" },
351 { 1, "Reliable" },
352 { 0, NULL }
356 static void
357 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
358 proto_tree *tree, int *direction)
360 proto_tree *game_tree = NULL;
361 guint32 seq1;
362 guint32 seq2;
363 int rel1;
364 int rel2;
365 int offset;
366 guint rest_length;
368 *direction = (pinfo->destport == gbl_quake3_server_port) ?
369 DIR_C2S : DIR_S2C;
371 if (tree) {
372 proto_item *game_item;
373 game_item = proto_tree_add_text(tree, tvb, 0, -1, "Game");
374 game_tree = proto_item_add_subtree(game_item, ett_quake3_game);
377 offset = 0;
379 seq1 = tvb_get_letohs(tvb, offset);
380 rel1 = seq1 & 0x8000 ? 1 : 0;
381 seq1 &= ~0x8000;
382 if (game_tree) {
383 proto_item *seq1_item = proto_tree_add_text(game_tree,
384 tvb, offset, 2, "Current Sequence: %u (%s)",
385 seq1, val_to_str(rel1,names_reliable,"%u"));
386 proto_tree *seq1_tree = proto_item_add_subtree(
387 seq1_item, ett_quake3_game_seq1);
388 proto_tree_add_uint(seq1_tree, hf_quake3_game_seq1,
389 tvb, offset, 2, seq1);
390 proto_tree_add_boolean(seq1_tree, hf_quake3_game_rel1,
391 tvb, offset+1, 1, rel1);
393 offset += 2;
395 seq2 = tvb_get_letohs(tvb, offset);
396 rel2 = seq2 & 0x8000 ? 1 : 0;
397 seq2 &= ~0x8000;
398 if (game_tree) {
399 proto_item *seq2_item = proto_tree_add_text(game_tree,
400 tvb, offset, 2, "Acknowledge Sequence: %u (%s)",
401 seq2, val_to_str(rel2,names_reliable,"%u"));
402 proto_tree *seq2_tree = proto_item_add_subtree(
403 seq2_item, ett_quake3_game_seq2);
404 proto_tree_add_uint(seq2_tree, hf_quake3_game_seq2,
405 tvb, offset, 2, seq2);
406 proto_tree_add_boolean(seq2_tree, hf_quake3_game_rel2,
407 tvb, offset+1, 1, rel2);
409 offset += 2;
411 if (*direction == DIR_C2S) {
412 /* client to server */
413 guint16 qport = tvb_get_letohs(tvb, offset);
414 if (game_tree) {
415 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
416 tvb, offset, 2, qport);
418 offset +=2;
421 /* all the rest is pure game data */
422 rest_length = tvb_reported_length(tvb) - offset;
423 if (rest_length) {
424 tvbuff_t *next_tvb =
425 tvb_new_subset(tvb, offset, rest_length , rest_length);
427 if (*direction == DIR_C2S) {
428 proto_tree *c_tree = NULL;
429 if (tree) {
430 proto_item *c_item;
431 c_item = proto_tree_add_text(game_tree, next_tvb,
432 0, -1, "Client Commands");
433 c_tree = proto_item_add_subtree(
434 c_item, ett_quake3_game_clc);
436 dissect_quake3_client_commands(next_tvb, pinfo, c_tree);
438 else {
439 proto_tree *c_tree = NULL;
440 if (tree) {
441 proto_item *c_item;
442 c_item = proto_tree_add_text(game_tree, next_tvb,
443 0, -1, "Server Commands");
444 c_tree = proto_item_add_subtree(
445 c_item, ett_quake3_game_svc);
447 dissect_quake3_server_commands(next_tvb, pinfo, c_tree);
453 static void
454 dissect_quake3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
456 proto_tree *quake3_tree = NULL;
457 proto_item *dir_item = NULL;
458 int direction;
460 direction = DIR_UNKNOWN;
462 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
464 if (tree) {
465 proto_item *quake3_item;
466 quake3_item = proto_tree_add_item(tree, proto_quake3,
467 tvb, 0, -1, ENC_NA);
468 quake3_tree = proto_item_add_subtree(quake3_item, ett_quake3);
470 dir_item = proto_tree_add_none_format(
471 quake3_tree,
472 hf_quake3_direction, tvb, 0, 0,
473 "Direction: %s",
474 val_to_str(direction,
475 names_direction, "%u"));
478 if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
479 col_set_str(pinfo->cinfo, COL_INFO, "Connectionless ");
480 proto_tree_add_uint_format(quake3_tree,
481 hf_quake3_connectionless,
482 tvb, 0, 0, 1,
483 "Type: Connectionless");
484 dissect_quake3_ConnectionlessPacket(
485 tvb, pinfo, quake3_tree, &direction);
487 else {
488 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
489 proto_tree_add_uint_format(quake3_tree,
490 hf_quake3_game,
491 tvb, 0, 0, 1,
492 "Type: Game");
493 dissect_quake3_GamePacket(
494 tvb, pinfo, quake3_tree, &direction);
496 if (direction != DIR_UNKNOWN && dir_item)
497 proto_item_set_text(dir_item,
498 "Direction: %s",
499 val_to_str(direction,
500 names_direction, "%u"));
502 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
503 names_direction, "%u"));
507 void proto_reg_handoff_quake3(void);
509 void
510 proto_register_quake3(void)
512 static hf_register_info hf[] = {
513 { &hf_quake3_direction,
514 { "Direction", "quake3.direction",
515 FT_NONE, BASE_NONE, NULL, 0x0,
516 "Packet Direction", HFILL }},
517 { &hf_quake3_connectionless,
518 { "Connectionless", "quake3.connectionless",
519 FT_UINT32, BASE_DEC, NULL, 0x0,
520 NULL, HFILL }},
521 { &hf_quake3_game,
522 { "Game", "quake3.game",
523 FT_UINT32, BASE_DEC, NULL, 0x0,
524 NULL, HFILL }},
525 { &hf_quake3_connectionless_marker,
526 { "Marker", "quake3.connectionless.marker",
527 FT_UINT32, BASE_HEX, NULL, 0x0,
528 NULL, HFILL }},
529 { &hf_quake3_connectionless_text,
530 { "Text", "quake3.connectionless.text",
531 FT_STRING, BASE_NONE, NULL, 0x0,
532 NULL, HFILL }},
533 { &hf_quake3_connectionless_command,
534 { "Command", "quake3.connectionless.command",
535 FT_STRING, BASE_NONE, NULL, 0x0,
536 NULL, HFILL }},
537 { &hf_quake3_server_addr,
538 { "Server Address", "quake3.server.addr",
539 FT_IPv4, BASE_NONE, NULL, 0x0,
540 "Server IP Address", HFILL }},
541 { &hf_quake3_server_port,
542 { "Server Port", "quake3.server.port",
543 FT_UINT16, BASE_DEC, NULL, 0x0,
544 "Server UDP Port", HFILL }},
545 { &hf_quake3_game_seq1,
546 { "Sequence Number", "quake3.game.seq1",
547 FT_UINT32, BASE_DEC, NULL, 0x0,
548 "Sequence number of the current packet", HFILL }},
549 { &hf_quake3_game_rel1,
550 { "Reliable", "quake3.game.rel1",
551 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
552 "Packet is reliable and may be retransmitted", HFILL }},
553 { &hf_quake3_game_seq2,
554 { "Sequence Number", "quake3.game.seq2",
555 FT_UINT32, BASE_DEC, NULL, 0x0,
556 "Sequence number of the last received packet", HFILL }},
557 { &hf_quake3_game_rel2,
558 { "Reliable", "quake3.game.rel2",
559 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
560 "Packet was reliable and may be retransmitted", HFILL }},
561 { &hf_quake3_game_qport,
562 { "QPort", "quake3.game.qport",
563 FT_UINT32, BASE_DEC, NULL, 0x0,
564 "Quake III Arena Client Port", HFILL }}
566 static gint *ett[] = {
567 &ett_quake3,
568 &ett_quake3_connectionless,
569 &ett_quake3_connectionless_text,
570 &ett_quake3_server,
571 &ett_quake3_game,
572 &ett_quake3_game_seq1,
573 &ett_quake3_game_seq2,
574 &ett_quake3_game_clc,
575 &ett_quake3_game_svc
577 module_t *quake3_module;
579 proto_quake3 = proto_register_protocol("Quake III Arena Network Protocol",
580 "QUAKE3", "quake3");
581 proto_register_field_array(proto_quake3, hf, array_length(hf));
582 proto_register_subtree_array(ett, array_length(ett));
584 /* Register a configuration option for port */
585 quake3_module = prefs_register_protocol(proto_quake3,
586 proto_reg_handoff_quake3);
587 prefs_register_uint_preference(quake3_module, "udp.arena_port",
588 "Quake III Arena Server UDP Base Port",
589 "Set the UDP base port for the Quake III Arena Server",
590 10, &gbl_quake3_server_port);
591 prefs_register_uint_preference(quake3_module, "udp.master_port",
592 "Quake III Arena Master Server UDP Base Port",
593 "Set the UDP base port for the Quake III Arena Master Server",
594 10, &gbl_quake3_master_port);
598 void
599 proto_reg_handoff_quake3(void)
601 static gboolean initialized=FALSE;
602 static dissector_handle_t quake3_handle;
603 static guint server_port;
604 static guint master_port;
605 int i;
607 if (!initialized) {
608 quake3_handle = create_dissector_handle(dissect_quake3,
609 proto_quake3);
610 data_handle = find_dissector("data");
611 initialized=TRUE;
612 } else {
613 for (i=0;i<4;i++)
614 dissector_delete_uint("udp.port", server_port+i, quake3_handle);
615 for (i=0;i<4;i++)
616 dissector_delete_uint("udp.port", master_port+i, quake3_handle);
619 /* set port for future deletes */
620 server_port = gbl_quake3_server_port;
621 master_port = gbl_quake3_master_port;
623 /* add dissectors */
624 for (i=0;i<4;i++)
625 dissector_add_uint("udp.port", gbl_quake3_server_port + i,
626 quake3_handle);
627 for (i=0;i<4;i++)
628 dissector_add_uint("udp.port", gbl_quake3_master_port + i,
629 quake3_handle);