2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * $Id: packet-quake3.c,v 1.14 2002/08/28 21:00:27 jmayer Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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).
46 #include <epan/packet.h>
48 #include <epan/resolv.h>
50 static int proto_quake3 = -1;
52 static int hf_quake3_direction = -1;
53 static int hf_quake3_connectionless = -1;
54 static int hf_quake3_game = -1;
55 static int hf_quake3_connectionless_marker = -1;
56 static int hf_quake3_connectionless_text = -1;
57 static int hf_quake3_connectionless_command = -1;
58 static int hf_quake3_server_addr = -1;
59 static int hf_quake3_server_port = -1;
60 static int hf_quake3_game_seq1 = -1;
61 static int hf_quake3_game_rel1 = -1;
62 static int hf_quake3_game_seq2 = -1;
63 static int hf_quake3_game_rel2 = -1;
64 static int hf_quake3_game_qport = -1;
66 static gint ett_quake3 = -1;
67 static gint ett_quake3_connectionless = -1;
68 static gint ett_quake3_connectionless_text = -1;
69 static gint ett_quake3_server = -1;
70 static gint ett_quake3_game = -1;
71 static gint ett_quake3_game_seq1 = -1;
72 static gint ett_quake3_game_seq2 = -1;
73 static gint ett_quake3_game_clc = -1;
74 static gint ett_quake3_game_svc = -1;
76 static dissector_handle_t data_handle;
78 #define QUAKE3_SERVER_PORT 27960
79 #define QUAKE3_MASTER_PORT 27950
80 static unsigned int gbl_quake3_server_port=QUAKE3_SERVER_PORT;
81 static unsigned int gbl_quake3_master_port=QUAKE3_MASTER_PORT;
84 static const value_string names_direction[] = {
86 { DIR_UNKNOWN, "Unknown" },
88 { DIR_C2S, "Client to Server" },
90 { DIR_S2C, "Server to Client" },
92 { DIR_C2M, "Client to Master" },
94 { DIR_M2C, "Master to Client" },
99 #define string_starts_with(s1, s2) (strncmp((s1), (s2), strlen(s2)) == 0)
102 static const value_string names_command[] = {
103 #define COMMAND_UNKNOWN 0
104 { COMMAND_UNKNOWN, "Unknown" },
105 #define COMMAND_statusResponse 1
106 { COMMAND_statusResponse, "Reply Status" },
107 #define COMMAND_getstatus 2
108 { COMMAND_getstatus, "Request Status" },
109 #define COMMAND_infoResponse 3
110 { COMMAND_infoResponse, "Reply Info" },
111 #define COMMAND_getinfo 4
112 { COMMAND_getinfo, "Request Info" },
113 #define COMMAND_challengeResponse 5
114 { COMMAND_challengeResponse, "Reply Challenge" },
115 #define COMMAND_getchallenge 6
116 { COMMAND_getchallenge, "Request Challenge" },
117 #define COMMAND_connectResponse 7
118 { COMMAND_connectResponse, "Reply Connect" },
119 #define COMMAND_connect 8
120 { COMMAND_connect, "Request Connect" },
121 #define COMMAND_rconResponse 9
122 { COMMAND_rconResponse, "Reply Remote Command" },
123 #define COMMAND_rcon 10
124 { COMMAND_rcon, "Request Remote Command" },
125 #define COMMAND_getmotdResponse 11
126 { COMMAND_getmotdResponse, "Reply Motto of the Day" },
127 #define COMMAND_getmotd 12
128 { COMMAND_getmotd, "Request Motto of the Day" },
129 #define COMMAND_getserversResponse 13
130 { COMMAND_getserversResponse, "Reply Servers" },
131 #define COMMAND_getservers 14
132 { COMMAND_getservers, "Request Servers" },
133 #define COMMAND_getKeyAuthorize 15
134 { COMMAND_getKeyAuthorize, "Request Key Authorization" },
135 #define COMMAND_getIpAuthorize 16
136 { COMMAND_getIpAuthorize, "Request IP Authorization" },
137 #define COMMAND_ipAuthorize 17
138 { COMMAND_ipAuthorize, "Reply IP Authorization" },
144 dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_,
145 proto_tree *tree, int* direction)
147 proto_tree *cl_tree = NULL;
148 proto_item *cl_item = NULL;
149 proto_item *text_item = NULL;
150 proto_tree *text_tree = NULL;
158 int command_finished = FALSE;
160 marker = tvb_get_ntohl(tvb, 0);
162 cl_item = proto_tree_add_text(tree, tvb,
163 0, -1, "Connectionless");
165 cl_tree = proto_item_add_subtree(
166 cl_item, ett_quake3_connectionless);
170 proto_tree_add_uint(cl_tree, hf_quake3_connectionless_marker,
174 /* all the rest of the packet is just text */
177 maxbufsize = MIN((gint)sizeof(text), tvb_length_remaining(tvb, offset));
178 len = tvb_get_nstringz0(tvb, offset, maxbufsize, text);
180 text_item = proto_tree_add_string(cl_tree,
181 hf_quake3_connectionless_text,
182 tvb, offset, len + 1, text);
184 text_tree = proto_item_add_subtree(
186 ett_quake3_connectionless_text);
190 command = COMMAND_UNKNOWN;
193 if (strncmp(text, "statusResponse", 14) == 0) {
194 command = COMMAND_statusResponse;
195 *direction = DIR_S2C;
198 else if (strncmp(text, "getstatus", 9) == 0) {
199 command = COMMAND_getstatus;
200 *direction = DIR_C2S;
203 else if (strncmp(text, "infoResponse", 12) == 0) {
204 command = COMMAND_infoResponse;
205 *direction = DIR_S2C;
208 else if (strncmp(text, "getinfo", 7) == 0) {
209 command = COMMAND_getinfo;
210 *direction = DIR_C2S;
213 else if (strncmp(text, "challengeResponse", 17) == 0) {
214 command = COMMAND_challengeResponse;
215 *direction = DIR_S2C;
218 else if (strncmp(text, "getchallenge", 12) == 0) {
219 command = COMMAND_getchallenge;
220 *direction = DIR_C2S;
223 else if (strncmp(text, "connectResponse", 15) == 0) {
224 command = COMMAND_connectResponse;
225 *direction = DIR_S2C;
228 else if (strncmp(text, "connect", 7) == 0) {
229 command = COMMAND_connect;
230 *direction = DIR_C2S;
233 else if (strncmp(text, "rconResponse", 12) == 0) {
234 command = COMMAND_rconResponse;
235 *direction = DIR_S2C;
238 else if (strncmp(text, "rcon", 4) == 0) {
239 command = COMMAND_rcon;
240 *direction = DIR_C2S;
243 else if (strncmp(text, "getmotdResponse", 15) == 0) {
244 command = COMMAND_getmotdResponse;
245 *direction = DIR_M2C;
248 else if (strncmp(text, "getmotd", 7) == 0) {
249 command = COMMAND_getmotd;
250 *direction = DIR_C2M;
253 else if (strncmp(text, "getserversResponse", 18) == 0) {
255 command = COMMAND_getserversResponse;
256 *direction = DIR_M2C;
258 /* The data can contain 0's, and the text string is binary
259 anyway, so better replace the text string after the first
262 /* first correct the length until the end of the packet */
263 proto_item_set_len(text_item, tvb_length_remaining(tvb, offset));
264 /* then replace the text */
265 proto_item_set_text(text_item, "Text: getserversResponse<DATA>");
268 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
269 tvb, offset, command_len,
270 val_to_str(command, names_command, "Unknown"));
271 command_finished = TRUE;
273 /* now we decode all the rest */
275 /* '/', ip-address in network order, port in network order */
276 while (tvb_reported_length_remaining(tvb, base) >= 7) {
279 proto_item *server_item = NULL;
280 proto_tree *server_tree = NULL;
282 tvb_memcpy(tvb, (guint8 *)&ip_addr, base + 1, 4);
283 udp_port = tvb_get_ntohs(tvb, base + 5);
285 /* It may be a good idea to create a conversation for
286 every server in this list, as we'll see at
287 least a getinfo request to each of them and they
288 may run on totally unusual ports. */
291 server_item = proto_tree_add_text(text_tree,
294 get_hostname(ip_addr),
297 server_tree = proto_item_add_subtree(
302 proto_tree_add_ipv4(server_tree, hf_quake3_server_addr,
303 tvb, base + 1, 4, ip_addr);
304 proto_tree_add_uint(server_tree, hf_quake3_server_port,
305 tvb, base + 5, 2, udp_port);
311 else if (strncmp(text, "getservers", 10) == 0) {
312 command = COMMAND_getservers;
313 *direction = DIR_C2M;
316 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
317 command = COMMAND_getKeyAuthorize;
318 *direction = DIR_C2M;
321 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
322 command = COMMAND_getIpAuthorize;
323 *direction = DIR_C2M;
326 else if (strncmp(text, "ipAuthorize", 11) == 0) {
327 command = COMMAND_ipAuthorize;
328 *direction = DIR_M2C;
332 *direction = DIR_UNKNOWN;
335 if (text_tree && command_finished == FALSE) {
336 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
337 tvb, offset, command_len,
338 val_to_str(command, names_command, "Unknown"));
347 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
350 /* this shouldn't be too difficult */
351 call_dissector(data_handle,tvb, pinfo, tree);
356 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
359 /* It is totally forbidden to decode this any further,
361 call_dissector(data_handle,tvb, pinfo, tree);
365 static const value_string names_reliable[] = {
366 { 0, "Non Reliable" },
373 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
374 proto_tree *tree, int *direction)
376 proto_tree *game_tree = NULL;
377 proto_item *game_item = NULL;
385 *direction = (pinfo->destport == gbl_quake3_server_port) ?
389 game_item = proto_tree_add_text(tree, tvb,
392 game_tree = proto_item_add_subtree(
393 game_item, ett_quake3_game);
398 seq1 = tvb_get_letohs(tvb, offset);
399 rel1 = seq1 & 0x8000 ? 1 : 0;
402 proto_item *seq1_item = proto_tree_add_text(game_tree,
403 tvb, offset, 2, "Current Sequence: %u (%s)",
404 seq1, val_to_str(rel1,names_reliable,"%u"));
406 proto_tree *seq1_tree = proto_item_add_subtree(
407 seq1_item, ett_quake3_game_seq1);
408 proto_tree_add_uint(seq1_tree, hf_quake3_game_seq1,
409 tvb, offset, 2, seq1);
410 proto_tree_add_boolean(seq1_tree, hf_quake3_game_rel1,
411 tvb, offset+1, 1, rel1);
416 seq2 = tvb_get_letohs(tvb, offset);
417 rel2 = seq2 & 0x8000 ? 1 : 0;
420 proto_item *seq2_item = proto_tree_add_text(game_tree,
421 tvb, offset, 2, "Acknowledge Sequence: %u (%s)",
422 seq2, val_to_str(rel2,names_reliable,"%u"));;
424 proto_tree *seq2_tree = proto_item_add_subtree(
425 seq2_item, ett_quake3_game_seq2);
426 proto_tree_add_uint(seq2_tree, hf_quake3_game_seq2,
427 tvb, offset, 2, seq2);
428 proto_tree_add_boolean(seq2_tree, hf_quake3_game_rel2,
429 tvb, offset+1, 1, rel2);
434 if (*direction == DIR_C2S) {
435 /* client to server */
436 guint16 qport = tvb_get_letohs(tvb, offset);
438 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
439 tvb, offset, 2, qport);
444 /* all the rest is pure game data */
445 rest_length = tvb_reported_length(tvb) - offset;
448 tvb_new_subset(tvb, offset, rest_length , rest_length);
450 if (*direction == DIR_C2S) {
451 proto_item *c_item = NULL;
452 proto_tree *c_tree = NULL;
454 c_item = proto_tree_add_text(game_tree, next_tvb,
455 0, -1, "Client Commands");
457 c_tree = proto_item_add_subtree(
458 c_item, ett_quake3_game_clc);
461 dissect_quake3_client_commands(next_tvb, pinfo, c_tree);
464 proto_item *c_item = NULL;
465 proto_tree *c_tree = NULL;
467 c_item = proto_tree_add_text(game_tree, next_tvb,
468 0, -1, "Server Commands");
470 c_tree = proto_item_add_subtree(
471 c_item, ett_quake3_game_svc);
474 dissect_quake3_server_commands(next_tvb, pinfo, c_tree);
481 dissect_quake3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
483 proto_tree *quake3_tree = NULL;
484 proto_item *quake3_item = NULL;
485 proto_item *dir_item = NULL;
488 direction = DIR_UNKNOWN;
490 if (check_col(pinfo->cinfo, COL_PROTOCOL))
491 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
494 quake3_item = proto_tree_add_item(tree, proto_quake3,
497 quake3_tree = proto_item_add_subtree(
498 quake3_item, ett_quake3);
501 dir_item = proto_tree_add_none_format(
503 hf_quake3_direction, tvb, 0, 0,
505 val_to_str(direction,
506 names_direction, "%u"));
510 if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
511 if (check_col(pinfo->cinfo, COL_INFO)) {
512 col_set_str(pinfo->cinfo, COL_INFO, "Connectionless ");
515 proto_tree_add_uint_format(quake3_tree,
516 hf_quake3_connectionless,
518 "Type: Connectionless");
519 dissect_quake3_ConnectionlessPacket(
520 tvb, pinfo, quake3_tree, &direction);
523 if (check_col(pinfo->cinfo, COL_INFO)) {
524 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
527 proto_tree_add_uint_format(quake3_tree,
531 dissect_quake3_GamePacket(
532 tvb, pinfo, quake3_tree, &direction);
534 if (direction != DIR_UNKNOWN && dir_item)
535 proto_item_set_text(dir_item,
537 val_to_str(direction,
538 names_direction, "%u"));
540 if (check_col(pinfo->cinfo, COL_INFO))
541 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
542 names_direction, "%u"));
547 proto_reg_handoff_quake3(void)
549 static int initialized=FALSE;
550 static dissector_handle_t quake3_handle;
551 static int server_port;
552 static int master_port;
556 quake3_handle = create_dissector_handle(dissect_quake3,
561 dissector_delete("udp.port", server_port+i, quake3_handle);
563 dissector_delete("udp.port", master_port+i, quake3_handle);
566 /* set port for future deletes */
567 server_port = gbl_quake3_server_port;
568 master_port = gbl_quake3_master_port;
572 dissector_add("udp.port", gbl_quake3_server_port + i,
575 dissector_add("udp.port", gbl_quake3_master_port + i,
577 data_handle = find_dissector("data");
582 proto_register_quake3(void)
584 static hf_register_info hf[] = {
585 { &hf_quake3_direction,
586 { "Direction", "quake3.direction",
587 FT_NONE, BASE_DEC, NULL, 0x0,
588 "Packet Direction", HFILL }},
589 { &hf_quake3_connectionless,
590 { "Connectionless", "quake3.connectionless",
591 FT_UINT32, BASE_DEC, NULL, 0x0,
592 "Connectionless", HFILL }},
594 { "Game", "quake3.game",
595 FT_UINT32, BASE_DEC, NULL, 0x0,
597 { &hf_quake3_connectionless_marker,
598 { "Marker", "quake3.connectionless.marker",
599 FT_UINT32, BASE_HEX, NULL, 0x0,
601 { &hf_quake3_connectionless_text,
602 { "Text", "quake3.connectionless.text",
603 FT_STRING, BASE_DEC, NULL, 0x0,
605 { &hf_quake3_connectionless_command,
606 { "Command", "quake3.connectionless.command",
607 FT_STRING, BASE_DEC, NULL, 0x0,
609 { &hf_quake3_server_addr,
610 { "Server Address", "quake3.server.addr",
611 FT_IPv4, BASE_DEC, NULL, 0x0,
612 "Server IP Address", HFILL }},
613 { &hf_quake3_server_port,
614 { "Server Port", "quake3.server.port",
615 FT_UINT16, BASE_DEC, NULL, 0x0,
616 "Server UDP Port", HFILL }},
617 { &hf_quake3_game_seq1,
618 { "Sequence Number", "quake3.game.seq1",
619 FT_UINT32, BASE_DEC, NULL, 0x0,
620 "Sequence number of the current packet", HFILL }},
621 { &hf_quake3_game_rel1,
622 { "Reliable", "quake3.game.rel1",
623 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
624 "Packet is reliable and may be retransmitted", HFILL }},
625 { &hf_quake3_game_seq2,
626 { "Sequence Number", "quake3.game.seq2",
627 FT_UINT32, BASE_DEC, NULL, 0x0,
628 "Sequence number of the last received packet", HFILL }},
629 { &hf_quake3_game_rel2,
630 { "Reliable", "quake3.game.rel2",
631 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
632 "Packet was reliable and may be retransmitted", HFILL }},
633 { &hf_quake3_game_qport,
634 { "QPort", "quake3.game.qport",
635 FT_UINT32, BASE_DEC, NULL, 0x0,
636 "Quake III Arena Client Port", HFILL }}
638 static gint *ett[] = {
640 &ett_quake3_connectionless,
641 &ett_quake3_connectionless_text,
644 &ett_quake3_game_seq1,
645 &ett_quake3_game_seq2,
646 &ett_quake3_game_clc,
649 module_t *quake3_module;
651 proto_quake3 = proto_register_protocol("Quake III Arena Network Protocol",
653 proto_register_field_array(proto_quake3, hf, array_length(hf));
654 proto_register_subtree_array(ett, array_length(ett));
656 /* Register a configuration option for port */
657 quake3_module = prefs_register_protocol(proto_quake3,
658 proto_reg_handoff_quake3);
659 prefs_register_uint_preference(quake3_module, "udp.arena_port",
660 "Quake III Arena Server UDP Base Port",
661 "Set the UDP base port for the Quake III Arena Server",
662 10, &gbl_quake3_server_port);
663 prefs_register_uint_preference(quake3_module, "udp.master_port",
664 "Quake III Arena Master Server UDP Base Port",
665 "Set the UDP base port for the Quake III Arena Master Server",
666 10, &gbl_quake3_master_port);