2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * $Id: packet-quake3.c,v 1.12 2002/04/02 06:28:16 girlich 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).
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
50 #include <epan/packet.h>
52 #include <epan/resolv.h>
54 static int proto_quake3 = -1;
56 static int hf_quake3_direction = -1;
57 static int hf_quake3_connectionless = -1;
58 static int hf_quake3_game = -1;
59 static int hf_quake3_connectionless_marker = -1;
60 static int hf_quake3_connectionless_text = -1;
61 static int hf_quake3_connectionless_command = -1;
62 static int hf_quake3_server_addr = -1;
63 static int hf_quake3_server_port = -1;
64 static int hf_quake3_game_seq1 = -1;
65 static int hf_quake3_game_rel1 = -1;
66 static int hf_quake3_game_seq2 = -1;
67 static int hf_quake3_game_rel2 = -1;
68 static int hf_quake3_game_qport = -1;
70 static gint ett_quake3 = -1;
71 static gint ett_quake3_connectionless = -1;
72 static gint ett_quake3_connectionless_text = -1;
73 static gint ett_quake3_server = -1;
74 static gint ett_quake3_game = -1;
75 static gint ett_quake3_game_seq1 = -1;
76 static gint ett_quake3_game_seq2 = -1;
77 static gint ett_quake3_game_clc = -1;
78 static gint ett_quake3_game_svc = -1;
80 static dissector_handle_t data_handle;
82 #define QUAKE3_SERVER_PORT 27960
83 #define QUAKE3_MASTER_PORT 27950
84 static unsigned int gbl_quake3_server_port=QUAKE3_SERVER_PORT;
85 static unsigned int gbl_quake3_master_port=QUAKE3_MASTER_PORT;
88 static const value_string names_direction[] = {
90 { DIR_UNKNOWN, "Unknown" },
92 { DIR_C2S, "Client to Server" },
94 { DIR_S2C, "Server to Client" },
96 { DIR_C2M, "Client to Master" },
98 { DIR_M2C, "Master to Client" },
103 #define string_starts_with(s1, s2) (strncmp((s1), (s2), strlen(s2)) == 0)
106 static const value_string names_command[] = {
107 #define COMMAND_UNKNOWN 0
108 { COMMAND_UNKNOWN, "Unknown" },
109 #define COMMAND_statusResponse 1
110 { COMMAND_statusResponse, "Reply Status" },
111 #define COMMAND_getstatus 2
112 { COMMAND_getstatus, "Request Status" },
113 #define COMMAND_infoResponse 3
114 { COMMAND_infoResponse, "Reply Info" },
115 #define COMMAND_getinfo 4
116 { COMMAND_getinfo, "Request Info" },
117 #define COMMAND_challengeResponse 5
118 { COMMAND_challengeResponse, "Reply Challenge" },
119 #define COMMAND_getchallenge 6
120 { COMMAND_getchallenge, "Request Challenge" },
121 #define COMMAND_connectResponse 7
122 { COMMAND_connectResponse, "Reply Connect" },
123 #define COMMAND_connect 8
124 { COMMAND_connect, "Request Connect" },
125 #define COMMAND_rconResponse 9
126 { COMMAND_rconResponse, "Reply Remote Command" },
127 #define COMMAND_rcon 10
128 { COMMAND_rcon, "Request Remote Command" },
129 #define COMMAND_getmotdResponse 11
130 { COMMAND_getmotdResponse, "Reply Motto of the Day" },
131 #define COMMAND_getmotd 12
132 { COMMAND_getmotd, "Request Motto of the Day" },
133 #define COMMAND_getserversResponse 13
134 { COMMAND_getserversResponse, "Reply Servers" },
135 #define COMMAND_getservers 14
136 { COMMAND_getservers, "Request Servers" },
137 #define COMMAND_getKeyAuthorize 15
138 { COMMAND_getKeyAuthorize, "Request Key Authorization" },
139 #define COMMAND_getIpAuthorize 16
140 { COMMAND_getIpAuthorize, "Request IP Authorization" },
141 #define COMMAND_ipAuthorize 17
142 { COMMAND_ipAuthorize, "Reply IP Authorization" },
148 dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_,
149 proto_tree *tree, int* direction)
151 proto_tree *cl_tree = NULL;
152 proto_item *cl_item = NULL;
153 proto_item *text_item = NULL;
154 proto_tree *text_tree = NULL;
162 int command_finished = FALSE;
164 marker = tvb_get_ntohl(tvb, 0);
166 cl_item = proto_tree_add_text(tree, tvb,
167 0, -1, "Connectionless");
169 cl_tree = proto_item_add_subtree(
170 cl_item, ett_quake3_connectionless);
174 proto_tree_add_uint(cl_tree, hf_quake3_connectionless_marker,
178 /* all the rest of the packet is just text */
181 maxbufsize = MIN((gint)sizeof(text), tvb_length_remaining(tvb, offset));
182 len = tvb_get_nstringz0(tvb, offset, maxbufsize, text);
184 text_item = proto_tree_add_string(cl_tree,
185 hf_quake3_connectionless_text,
186 tvb, offset, len + 1, text);
188 text_tree = proto_item_add_subtree(
190 ett_quake3_connectionless_text);
194 command = COMMAND_UNKNOWN;
197 if (strncmp(text, "statusResponse", 14) == 0) {
198 command = COMMAND_statusResponse;
199 *direction = DIR_S2C;
202 else if (strncmp(text, "getstatus", 9) == 0) {
203 command = COMMAND_getstatus;
204 *direction = DIR_C2S;
207 else if (strncmp(text, "infoResponse", 12) == 0) {
208 command = COMMAND_infoResponse;
209 *direction = DIR_S2C;
212 else if (strncmp(text, "getinfo", 7) == 0) {
213 command = COMMAND_getinfo;
214 *direction = DIR_C2S;
217 else if (strncmp(text, "challengeResponse", 17) == 0) {
218 command = COMMAND_challengeResponse;
219 *direction = DIR_S2C;
222 else if (strncmp(text, "getchallenge", 12) == 0) {
223 command = COMMAND_getchallenge;
224 *direction = DIR_C2S;
227 else if (strncmp(text, "connectResponse", 15) == 0) {
228 command = COMMAND_connectResponse;
229 *direction = DIR_S2C;
232 else if (strncmp(text, "connect", 7) == 0) {
233 command = COMMAND_connect;
234 *direction = DIR_C2S;
237 else if (strncmp(text, "rconResponse", 12) == 0) {
238 command = COMMAND_rconResponse;
239 *direction = DIR_S2C;
242 else if (strncmp(text, "rcon", 4) == 0) {
243 command = COMMAND_rcon;
244 *direction = DIR_C2S;
247 else if (strncmp(text, "getmotdResponse", 15) == 0) {
248 command = COMMAND_getmotdResponse;
249 *direction = DIR_M2C;
252 else if (strncmp(text, "getmotd", 7) == 0) {
253 command = COMMAND_getmotd;
254 *direction = DIR_C2M;
257 else if (strncmp(text, "getserversResponse", 18) == 0) {
259 command = COMMAND_getserversResponse;
260 *direction = DIR_M2C;
262 /* The data can contain 0's, and the text string is binary
263 anyway, so better replace the text string after the first
266 /* first correct the length until the end of the packet */
267 proto_item_set_len(text_item, tvb_length_remaining(tvb, offset));
268 /* then replace the text */
269 proto_item_set_text(text_item, "Text: getserversResponse<DATA>");
272 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
273 tvb, offset, command_len,
274 val_to_str(command, names_command, "Unknown"));
275 command_finished = TRUE;
277 /* now we decode all the rest */
279 /* '/', ip-address in network order, port in network order */
280 while (tvb_reported_length_remaining(tvb, base) >= 7) {
283 proto_item *server_item = NULL;
284 proto_tree *server_tree = NULL;
286 tvb_memcpy(tvb, (guint8 *)&ip_addr, base + 1, 4);
287 udp_port = tvb_get_ntohs(tvb, base + 5);
289 /* It may be a good idea to create a conversation for
290 every server in this list, as we'll see at
291 least a getinfo request to each of them and they
292 may run on totally unusual ports. */
295 server_item = proto_tree_add_text(text_tree,
298 get_hostname(ip_addr),
301 server_tree = proto_item_add_subtree(
306 proto_tree_add_ipv4(server_tree, hf_quake3_server_addr,
307 tvb, base + 1, 4, ip_addr);
308 proto_tree_add_uint(server_tree, hf_quake3_server_port,
309 tvb, base + 5, 2, udp_port);
315 else if (strncmp(text, "getservers", 10) == 0) {
316 command = COMMAND_getservers;
317 *direction = DIR_C2M;
320 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
321 command = COMMAND_getKeyAuthorize;
322 *direction = DIR_C2M;
325 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
326 command = COMMAND_getIpAuthorize;
327 *direction = DIR_C2M;
330 else if (strncmp(text, "ipAuthorize", 11) == 0) {
331 command = COMMAND_ipAuthorize;
332 *direction = DIR_M2C;
336 *direction = DIR_UNKNOWN;
339 if (text_tree && command_finished == FALSE) {
340 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
341 tvb, offset, command_len,
342 val_to_str(command, names_command, "Unknown"));
351 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
354 /* this shouldn't be too difficult */
355 call_dissector(data_handle,tvb, pinfo, tree);
360 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
363 /* It is totally forbidden to decode this any further,
365 call_dissector(data_handle,tvb, pinfo, tree);
369 static const value_string names_reliable[] = {
370 { 0, "Non Reliable" },
377 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
378 proto_tree *tree, int *direction)
380 proto_tree *game_tree = NULL;
381 proto_item *game_item = NULL;
389 *direction = (pinfo->destport == gbl_quake3_server_port) ?
393 game_item = proto_tree_add_text(tree, tvb,
396 game_tree = proto_item_add_subtree(
397 game_item, ett_quake3_game);
402 seq1 = tvb_get_letohs(tvb, offset);
403 rel1 = seq1 & 0x8000 ? 1 : 0;
406 proto_item *seq1_item = proto_tree_add_text(game_tree,
407 tvb, offset, 2, "Current Sequence: %u (%s)",
408 seq1, val_to_str(rel1,names_reliable,"%u"));
410 proto_tree *seq1_tree = proto_item_add_subtree(
411 seq1_item, ett_quake3_game_seq1);
412 proto_tree_add_uint(seq1_tree, hf_quake3_game_seq1,
413 tvb, offset, 2, seq1);
414 proto_tree_add_boolean(seq1_tree, hf_quake3_game_rel1,
415 tvb, offset+1, 1, rel1);
420 seq2 = tvb_get_letohs(tvb, offset);
421 rel2 = seq2 & 0x8000 ? 1 : 0;
424 proto_item *seq2_item = proto_tree_add_text(game_tree,
425 tvb, offset, 2, "Acknowledge Sequence: %u (%s)",
426 seq2, val_to_str(rel2,names_reliable,"%u"));;
428 proto_tree *seq2_tree = proto_item_add_subtree(
429 seq2_item, ett_quake3_game_seq2);
430 proto_tree_add_uint(seq2_tree, hf_quake3_game_seq2,
431 tvb, offset, 2, seq2);
432 proto_tree_add_boolean(seq2_tree, hf_quake3_game_rel2,
433 tvb, offset+1, 1, rel2);
438 if (*direction == DIR_C2S) {
439 /* client to server */
440 guint16 qport = tvb_get_letohs(tvb, offset);
442 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
443 tvb, offset, 2, qport);
448 /* all the rest is pure game data */
449 rest_length = tvb_reported_length(tvb) - offset;
452 tvb_new_subset(tvb, offset, rest_length , rest_length);
454 if (*direction == DIR_C2S) {
455 proto_item *c_item = NULL;
456 proto_tree *c_tree = NULL;
458 c_item = proto_tree_add_text(game_tree, next_tvb,
459 0, -1, "Client Commands");
461 c_tree = proto_item_add_subtree(
462 c_item, ett_quake3_game_clc);
465 dissect_quake3_client_commands(next_tvb, pinfo, c_tree);
468 proto_item *c_item = NULL;
469 proto_tree *c_tree = NULL;
471 c_item = proto_tree_add_text(game_tree, next_tvb,
472 0, -1, "Server Commands");
474 c_tree = proto_item_add_subtree(
475 c_item, ett_quake3_game_svc);
478 dissect_quake3_server_commands(next_tvb, pinfo, c_tree);
485 dissect_quake3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
487 proto_tree *quake3_tree = NULL;
488 proto_item *quake3_item = NULL;
489 proto_item *dir_item = NULL;
492 direction = DIR_UNKNOWN;
494 if (check_col(pinfo->cinfo, COL_PROTOCOL))
495 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
498 quake3_item = proto_tree_add_item(tree, proto_quake3,
501 quake3_tree = proto_item_add_subtree(
502 quake3_item, ett_quake3);
505 dir_item = proto_tree_add_none_format(
507 hf_quake3_direction, tvb, 0, 0,
509 val_to_str(direction,
510 names_direction, "%u"));
514 if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
515 if (check_col(pinfo->cinfo, COL_INFO)) {
516 col_set_str(pinfo->cinfo, COL_INFO, "Connectionless ");
519 proto_tree_add_uint_format(quake3_tree,
520 hf_quake3_connectionless,
522 "Type: Connectionless");
523 dissect_quake3_ConnectionlessPacket(
524 tvb, pinfo, quake3_tree, &direction);
527 if (check_col(pinfo->cinfo, COL_INFO)) {
528 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
531 proto_tree_add_uint_format(quake3_tree,
535 dissect_quake3_GamePacket(
536 tvb, pinfo, quake3_tree, &direction);
538 if (direction != DIR_UNKNOWN && dir_item)
539 proto_item_set_text(dir_item,
541 val_to_str(direction,
542 names_direction, "%u"));
544 if (check_col(pinfo->cinfo, COL_INFO))
545 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
546 names_direction, "%u"));
551 proto_reg_handoff_quake3(void)
553 static int initialized=FALSE;
554 static dissector_handle_t quake3_handle;
555 static int server_port;
556 static int master_port;
560 quake3_handle = create_dissector_handle(dissect_quake3,
565 dissector_delete("udp.port", server_port+i, quake3_handle);
567 dissector_delete("udp.port", master_port+i, quake3_handle);
570 /* set port for future deletes */
571 server_port = gbl_quake3_server_port;
572 master_port = gbl_quake3_master_port;
576 dissector_add("udp.port", gbl_quake3_server_port + i,
579 dissector_add("udp.port", gbl_quake3_master_port + i,
581 data_handle = find_dissector("data");
586 proto_register_quake3(void)
588 static hf_register_info hf[] = {
589 { &hf_quake3_direction,
590 { "Direction", "quake3.direction",
591 FT_NONE, BASE_DEC, NULL, 0x0,
592 "Packet Direction", HFILL }},
593 { &hf_quake3_connectionless,
594 { "Connectionless", "quake3.connectionless",
595 FT_UINT32, BASE_DEC, NULL, 0x0,
596 "Connectionless", HFILL }},
598 { "Game", "quake3.game",
599 FT_UINT32, BASE_DEC, NULL, 0x0,
601 { &hf_quake3_connectionless_marker,
602 { "Marker", "quake3.connectionless.marker",
603 FT_UINT32, BASE_HEX, NULL, 0x0,
605 { &hf_quake3_connectionless_text,
606 { "Text", "quake3.connectionless.text",
607 FT_STRING, BASE_DEC, NULL, 0x0,
609 { &hf_quake3_connectionless_command,
610 { "Command", "quake3.connectionless.command",
611 FT_STRING, BASE_DEC, NULL, 0x0,
613 { &hf_quake3_server_addr,
614 { "Server Address", "quake3.server.addr",
615 FT_IPv4, BASE_DEC, NULL, 0x0,
616 "Server IP Address", HFILL }},
617 { &hf_quake3_server_port,
618 { "Server Port", "quake3.server.port",
619 FT_UINT16, BASE_DEC, NULL, 0x0,
620 "Server UDP Port", HFILL }},
621 { &hf_quake3_game_seq1,
622 { "Sequence Number", "quake3.game.seq1",
623 FT_UINT32, BASE_DEC, NULL, 0x0,
624 "Sequence number of the current packet", HFILL }},
625 { &hf_quake3_game_rel1,
626 { "Reliable", "quake3.game.rel1",
627 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
628 "Packet is reliable and may be retransmitted", HFILL }},
629 { &hf_quake3_game_seq2,
630 { "Sequence Number", "quake3.game.seq2",
631 FT_UINT32, BASE_DEC, NULL, 0x0,
632 "Sequence number of the last received packet", HFILL }},
633 { &hf_quake3_game_rel2,
634 { "Reliable", "quake3.game.rel2",
635 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
636 "Packet was reliable and may be retransmitted", HFILL }},
637 { &hf_quake3_game_qport,
638 { "QPort", "quake3.game.qport",
639 FT_UINT32, BASE_DEC, NULL, 0x0,
640 "Quake III Arena Client Port", HFILL }}
642 static gint *ett[] = {
644 &ett_quake3_connectionless,
645 &ett_quake3_connectionless_text,
648 &ett_quake3_game_seq1,
649 &ett_quake3_game_seq2,
650 &ett_quake3_game_clc,
653 module_t *quake3_module;
655 proto_quake3 = proto_register_protocol("Quake III Arena Network Protocol",
657 proto_register_field_array(proto_quake3, hf, array_length(hf));
658 proto_register_subtree_array(ett, array_length(ett));
660 /* Register a configuration option for port */
661 quake3_module = prefs_register_protocol(proto_quake3,
662 proto_reg_handoff_quake3);
663 prefs_register_uint_preference(quake3_module, "udp.arena_port",
664 "Quake III Arena Server UDP Base Port",
665 "Set the UDP base port for the Quake III Arena Server",
666 10, &gbl_quake3_server_port);
667 prefs_register_uint_preference(quake3_module, "udp.master_port",
668 "Quake III Arena Master Server UDP Base Port",
669 "Set the UDP base port for the Quake III Arena Master Server",
670 10, &gbl_quake3_master_port);