2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * $Id: packet-quake3.c,v 1.15 2003/05/19 03:23:11 gerald 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 len = tvb_get_nstringz0(tvb, offset, sizeof(text), text);
179 text_item = proto_tree_add_string(cl_tree,
180 hf_quake3_connectionless_text,
181 tvb, offset, len + 1, text);
183 text_tree = proto_item_add_subtree(
185 ett_quake3_connectionless_text);
189 command = COMMAND_UNKNOWN;
192 if (strncmp(text, "statusResponse", 14) == 0) {
193 command = COMMAND_statusResponse;
194 *direction = DIR_S2C;
197 else if (strncmp(text, "getstatus", 9) == 0) {
198 command = COMMAND_getstatus;
199 *direction = DIR_C2S;
202 else if (strncmp(text, "infoResponse", 12) == 0) {
203 command = COMMAND_infoResponse;
204 *direction = DIR_S2C;
207 else if (strncmp(text, "getinfo", 7) == 0) {
208 command = COMMAND_getinfo;
209 *direction = DIR_C2S;
212 else if (strncmp(text, "challengeResponse", 17) == 0) {
213 command = COMMAND_challengeResponse;
214 *direction = DIR_S2C;
217 else if (strncmp(text, "getchallenge", 12) == 0) {
218 command = COMMAND_getchallenge;
219 *direction = DIR_C2S;
222 else if (strncmp(text, "connectResponse", 15) == 0) {
223 command = COMMAND_connectResponse;
224 *direction = DIR_S2C;
227 else if (strncmp(text, "connect", 7) == 0) {
228 command = COMMAND_connect;
229 *direction = DIR_C2S;
232 else if (strncmp(text, "rconResponse", 12) == 0) {
233 command = COMMAND_rconResponse;
234 *direction = DIR_S2C;
237 else if (strncmp(text, "rcon", 4) == 0) {
238 command = COMMAND_rcon;
239 *direction = DIR_C2S;
242 else if (strncmp(text, "getmotdResponse", 15) == 0) {
243 command = COMMAND_getmotdResponse;
244 *direction = DIR_M2C;
247 else if (strncmp(text, "getmotd", 7) == 0) {
248 command = COMMAND_getmotd;
249 *direction = DIR_C2M;
252 else if (strncmp(text, "getserversResponse", 18) == 0) {
254 command = COMMAND_getserversResponse;
255 *direction = DIR_M2C;
257 /* The data can contain 0's, and the text string is binary
258 anyway, so better replace the text string after the first
261 /* first correct the length until the end of the packet */
262 proto_item_set_len(text_item, tvb_length_remaining(tvb, offset));
263 /* then replace the text */
264 proto_item_set_text(text_item, "Text: getserversResponse<DATA>");
267 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
268 tvb, offset, command_len,
269 val_to_str(command, names_command, "Unknown"));
270 command_finished = TRUE;
272 /* now we decode all the rest */
274 /* '/', ip-address in network order, port in network order */
275 while (tvb_reported_length_remaining(tvb, base) >= 7) {
278 proto_item *server_item = NULL;
279 proto_tree *server_tree = NULL;
281 tvb_memcpy(tvb, (guint8 *)&ip_addr, base + 1, 4);
282 udp_port = tvb_get_ntohs(tvb, base + 5);
284 /* It may be a good idea to create a conversation for
285 every server in this list, as we'll see at
286 least a getinfo request to each of them and they
287 may run on totally unusual ports. */
290 server_item = proto_tree_add_text(text_tree,
293 get_hostname(ip_addr),
296 server_tree = proto_item_add_subtree(
301 proto_tree_add_ipv4(server_tree, hf_quake3_server_addr,
302 tvb, base + 1, 4, ip_addr);
303 proto_tree_add_uint(server_tree, hf_quake3_server_port,
304 tvb, base + 5, 2, udp_port);
310 else if (strncmp(text, "getservers", 10) == 0) {
311 command = COMMAND_getservers;
312 *direction = DIR_C2M;
315 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
316 command = COMMAND_getKeyAuthorize;
317 *direction = DIR_C2M;
320 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
321 command = COMMAND_getIpAuthorize;
322 *direction = DIR_C2M;
325 else if (strncmp(text, "ipAuthorize", 11) == 0) {
326 command = COMMAND_ipAuthorize;
327 *direction = DIR_M2C;
331 *direction = DIR_UNKNOWN;
334 if (text_tree && command_finished == FALSE) {
335 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
336 tvb, offset, command_len,
337 val_to_str(command, names_command, "Unknown"));
346 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
349 /* this shouldn't be too difficult */
350 call_dissector(data_handle,tvb, pinfo, tree);
355 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
358 /* It is totally forbidden to decode this any further,
360 call_dissector(data_handle,tvb, pinfo, tree);
364 static const value_string names_reliable[] = {
365 { 0, "Non Reliable" },
372 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
373 proto_tree *tree, int *direction)
375 proto_tree *game_tree = NULL;
376 proto_item *game_item = NULL;
384 *direction = (pinfo->destport == gbl_quake3_server_port) ?
388 game_item = proto_tree_add_text(tree, tvb,
391 game_tree = proto_item_add_subtree(
392 game_item, ett_quake3_game);
397 seq1 = tvb_get_letohs(tvb, offset);
398 rel1 = seq1 & 0x8000 ? 1 : 0;
401 proto_item *seq1_item = proto_tree_add_text(game_tree,
402 tvb, offset, 2, "Current Sequence: %u (%s)",
403 seq1, val_to_str(rel1,names_reliable,"%u"));
405 proto_tree *seq1_tree = proto_item_add_subtree(
406 seq1_item, ett_quake3_game_seq1);
407 proto_tree_add_uint(seq1_tree, hf_quake3_game_seq1,
408 tvb, offset, 2, seq1);
409 proto_tree_add_boolean(seq1_tree, hf_quake3_game_rel1,
410 tvb, offset+1, 1, rel1);
415 seq2 = tvb_get_letohs(tvb, offset);
416 rel2 = seq2 & 0x8000 ? 1 : 0;
419 proto_item *seq2_item = proto_tree_add_text(game_tree,
420 tvb, offset, 2, "Acknowledge Sequence: %u (%s)",
421 seq2, val_to_str(rel2,names_reliable,"%u"));;
423 proto_tree *seq2_tree = proto_item_add_subtree(
424 seq2_item, ett_quake3_game_seq2);
425 proto_tree_add_uint(seq2_tree, hf_quake3_game_seq2,
426 tvb, offset, 2, seq2);
427 proto_tree_add_boolean(seq2_tree, hf_quake3_game_rel2,
428 tvb, offset+1, 1, rel2);
433 if (*direction == DIR_C2S) {
434 /* client to server */
435 guint16 qport = tvb_get_letohs(tvb, offset);
437 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
438 tvb, offset, 2, qport);
443 /* all the rest is pure game data */
444 rest_length = tvb_reported_length(tvb) - offset;
447 tvb_new_subset(tvb, offset, rest_length , rest_length);
449 if (*direction == DIR_C2S) {
450 proto_item *c_item = NULL;
451 proto_tree *c_tree = NULL;
453 c_item = proto_tree_add_text(game_tree, next_tvb,
454 0, -1, "Client Commands");
456 c_tree = proto_item_add_subtree(
457 c_item, ett_quake3_game_clc);
460 dissect_quake3_client_commands(next_tvb, pinfo, c_tree);
463 proto_item *c_item = NULL;
464 proto_tree *c_tree = NULL;
466 c_item = proto_tree_add_text(game_tree, next_tvb,
467 0, -1, "Server Commands");
469 c_tree = proto_item_add_subtree(
470 c_item, ett_quake3_game_svc);
473 dissect_quake3_server_commands(next_tvb, pinfo, c_tree);
480 dissect_quake3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
482 proto_tree *quake3_tree = NULL;
483 proto_item *quake3_item = NULL;
484 proto_item *dir_item = NULL;
487 direction = DIR_UNKNOWN;
489 if (check_col(pinfo->cinfo, COL_PROTOCOL))
490 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
493 quake3_item = proto_tree_add_item(tree, proto_quake3,
496 quake3_tree = proto_item_add_subtree(
497 quake3_item, ett_quake3);
500 dir_item = proto_tree_add_none_format(
502 hf_quake3_direction, tvb, 0, 0,
504 val_to_str(direction,
505 names_direction, "%u"));
509 if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
510 if (check_col(pinfo->cinfo, COL_INFO)) {
511 col_set_str(pinfo->cinfo, COL_INFO, "Connectionless ");
514 proto_tree_add_uint_format(quake3_tree,
515 hf_quake3_connectionless,
517 "Type: Connectionless");
518 dissect_quake3_ConnectionlessPacket(
519 tvb, pinfo, quake3_tree, &direction);
522 if (check_col(pinfo->cinfo, COL_INFO)) {
523 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
526 proto_tree_add_uint_format(quake3_tree,
530 dissect_quake3_GamePacket(
531 tvb, pinfo, quake3_tree, &direction);
533 if (direction != DIR_UNKNOWN && dir_item)
534 proto_item_set_text(dir_item,
536 val_to_str(direction,
537 names_direction, "%u"));
539 if (check_col(pinfo->cinfo, COL_INFO))
540 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
541 names_direction, "%u"));
546 proto_reg_handoff_quake3(void)
548 static int initialized=FALSE;
549 static dissector_handle_t quake3_handle;
550 static int server_port;
551 static int master_port;
555 quake3_handle = create_dissector_handle(dissect_quake3,
560 dissector_delete("udp.port", server_port+i, quake3_handle);
562 dissector_delete("udp.port", master_port+i, quake3_handle);
565 /* set port for future deletes */
566 server_port = gbl_quake3_server_port;
567 master_port = gbl_quake3_master_port;
571 dissector_add("udp.port", gbl_quake3_server_port + i,
574 dissector_add("udp.port", gbl_quake3_master_port + i,
576 data_handle = find_dissector("data");
581 proto_register_quake3(void)
583 static hf_register_info hf[] = {
584 { &hf_quake3_direction,
585 { "Direction", "quake3.direction",
586 FT_NONE, BASE_DEC, NULL, 0x0,
587 "Packet Direction", HFILL }},
588 { &hf_quake3_connectionless,
589 { "Connectionless", "quake3.connectionless",
590 FT_UINT32, BASE_DEC, NULL, 0x0,
591 "Connectionless", HFILL }},
593 { "Game", "quake3.game",
594 FT_UINT32, BASE_DEC, NULL, 0x0,
596 { &hf_quake3_connectionless_marker,
597 { "Marker", "quake3.connectionless.marker",
598 FT_UINT32, BASE_HEX, NULL, 0x0,
600 { &hf_quake3_connectionless_text,
601 { "Text", "quake3.connectionless.text",
602 FT_STRING, BASE_DEC, NULL, 0x0,
604 { &hf_quake3_connectionless_command,
605 { "Command", "quake3.connectionless.command",
606 FT_STRING, BASE_DEC, NULL, 0x0,
608 { &hf_quake3_server_addr,
609 { "Server Address", "quake3.server.addr",
610 FT_IPv4, BASE_DEC, NULL, 0x0,
611 "Server IP Address", HFILL }},
612 { &hf_quake3_server_port,
613 { "Server Port", "quake3.server.port",
614 FT_UINT16, BASE_DEC, NULL, 0x0,
615 "Server UDP Port", HFILL }},
616 { &hf_quake3_game_seq1,
617 { "Sequence Number", "quake3.game.seq1",
618 FT_UINT32, BASE_DEC, NULL, 0x0,
619 "Sequence number of the current packet", HFILL }},
620 { &hf_quake3_game_rel1,
621 { "Reliable", "quake3.game.rel1",
622 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
623 "Packet is reliable and may be retransmitted", HFILL }},
624 { &hf_quake3_game_seq2,
625 { "Sequence Number", "quake3.game.seq2",
626 FT_UINT32, BASE_DEC, NULL, 0x0,
627 "Sequence number of the last received packet", HFILL }},
628 { &hf_quake3_game_rel2,
629 { "Reliable", "quake3.game.rel2",
630 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
631 "Packet was reliable and may be retransmitted", HFILL }},
632 { &hf_quake3_game_qport,
633 { "QPort", "quake3.game.qport",
634 FT_UINT32, BASE_DEC, NULL, 0x0,
635 "Quake III Arena Client Port", HFILL }}
637 static gint *ett[] = {
639 &ett_quake3_connectionless,
640 &ett_quake3_connectionless_text,
643 &ett_quake3_game_seq1,
644 &ett_quake3_game_seq2,
645 &ett_quake3_game_clc,
648 module_t *quake3_module;
650 proto_quake3 = proto_register_protocol("Quake III Arena Network Protocol",
652 proto_register_field_array(proto_quake3, hf, array_length(hf));
653 proto_register_subtree_array(ett, array_length(ett));
655 /* Register a configuration option for port */
656 quake3_module = prefs_register_protocol(proto_quake3,
657 proto_reg_handoff_quake3);
658 prefs_register_uint_preference(quake3_module, "udp.arena_port",
659 "Quake III Arena Server UDP Base Port",
660 "Set the UDP base port for the Quake III Arena Server",
661 10, &gbl_quake3_server_port);
662 prefs_register_uint_preference(quake3_module, "udp.master_port",
663 "Quake III Arena Master Server UDP Base Port",
664 "Set the UDP base port for the Quake III Arena Master Server",
665 10, &gbl_quake3_master_port);