2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * $Id: packet-quake3.c,v 1.10 2002/01/24 09:20:50 guy 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,
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 ip_addr = tvb_get_ntohl(tvb, base + 1);
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(htonl(ip_addr)),
301 server_tree = proto_item_add_subtree(
306 /* I'm not sure about all this ordering stuff
307 but so it works on my (le) machine */
308 proto_tree_add_ipv4(server_tree, hf_quake3_server_addr,
309 tvb, base + 1, 4, htonl(ip_addr));
310 proto_tree_add_uint(server_tree, hf_quake3_server_port,
311 tvb, base + 5, 2, udp_port);
317 else if (strncmp(text, "getservers", 10) == 0) {
318 command = COMMAND_getservers;
319 *direction = DIR_C2M;
322 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
323 command = COMMAND_getKeyAuthorize;
324 *direction = DIR_C2M;
327 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
328 command = COMMAND_getIpAuthorize;
329 *direction = DIR_C2M;
332 else if (strncmp(text, "ipAuthorize", 11) == 0) {
333 command = COMMAND_ipAuthorize;
334 *direction = DIR_M2C;
338 *direction = DIR_UNKNOWN;
341 if (text_tree && command_finished == FALSE) {
342 proto_tree_add_string(text_tree, hf_quake3_connectionless_command,
343 tvb, offset, command_len,
344 val_to_str(command, names_command, "Unknown"));
353 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
356 /* this shouldn't be too difficult */
357 call_dissector(data_handle,tvb, pinfo, tree);
362 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
365 /* It is totally forbidden to decode this any further,
367 call_dissector(data_handle,tvb, pinfo, tree);
371 static const value_string names_reliable[] = {
372 { 0, "Non Reliable" },
379 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
380 proto_tree *tree, int *direction)
382 proto_tree *game_tree = NULL;
383 proto_item *game_item = NULL;
391 *direction = (pinfo->destport == gbl_quake3_server_port) ?
395 game_item = proto_tree_add_text(tree, tvb,
398 game_tree = proto_item_add_subtree(
399 game_item, ett_quake3_game);
404 seq1 = tvb_get_letohs(tvb, offset);
405 rel1 = seq1 & 0x8000 ? 1 : 0;
408 proto_item *seq1_item = proto_tree_add_text(game_tree,
409 tvb, offset, 2, "Current Sequence: %u (%s)",
410 seq1, val_to_str(rel1,names_reliable,"%u"));
412 proto_tree *seq1_tree = proto_item_add_subtree(
413 seq1_item, ett_quake3_game_seq1);
414 proto_tree_add_uint(seq1_tree, hf_quake3_game_seq1,
415 tvb, offset, 2, seq1);
416 proto_tree_add_boolean(seq1_tree, hf_quake3_game_rel1,
417 tvb, offset+1, 1, rel1);
422 seq2 = tvb_get_letohs(tvb, offset);
423 rel2 = seq2 & 0x8000 ? 1 : 0;
426 proto_item *seq2_item = proto_tree_add_text(game_tree,
427 tvb, offset, 2, "Acknowledge Sequence: %u (%s)",
428 seq2, val_to_str(rel2,names_reliable,"%u"));;
430 proto_tree *seq2_tree = proto_item_add_subtree(
431 seq2_item, ett_quake3_game_seq2);
432 proto_tree_add_uint(seq2_tree, hf_quake3_game_seq2,
433 tvb, offset, 2, seq2);
434 proto_tree_add_boolean(seq2_tree, hf_quake3_game_rel2,
435 tvb, offset+1, 1, rel2);
440 if (*direction == DIR_C2S) {
441 /* client to server */
442 guint16 qport = tvb_get_letohs(tvb, offset);
444 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
445 tvb, offset, 2, qport);
450 /* all the rest is pure game data */
451 rest_length = tvb_reported_length(tvb) - offset;
454 tvb_new_subset(tvb, offset, rest_length , rest_length);
456 if (*direction == DIR_C2S) {
457 proto_item *c_item = NULL;
458 proto_tree *c_tree = NULL;
460 c_item = proto_tree_add_text(game_tree, next_tvb,
461 0, -1, "Client Commands");
463 c_tree = proto_item_add_subtree(
464 c_item, ett_quake3_game_clc);
467 dissect_quake3_client_commands(next_tvb, pinfo, c_tree);
470 proto_item *c_item = NULL;
471 proto_tree *c_tree = NULL;
473 c_item = proto_tree_add_text(game_tree, next_tvb,
474 0, -1, "Server Commands");
476 c_tree = proto_item_add_subtree(
477 c_item, ett_quake3_game_svc);
480 dissect_quake3_server_commands(next_tvb, pinfo, c_tree);
487 dissect_quake3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
489 proto_tree *quake3_tree = NULL;
490 proto_item *quake3_item = NULL;
491 proto_item *dir_item = NULL;
494 direction = DIR_UNKNOWN;
496 if (check_col(pinfo->cinfo, COL_PROTOCOL))
497 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
500 quake3_item = proto_tree_add_item(tree, proto_quake3,
503 quake3_tree = proto_item_add_subtree(
504 quake3_item, ett_quake3);
507 dir_item = proto_tree_add_none_format(
509 hf_quake3_direction, tvb, 0, 0,
511 val_to_str(direction,
512 names_direction, "%u"));
516 if (tvb_get_ntohl(tvb, 0) == 0xffffffff) {
517 if (check_col(pinfo->cinfo, COL_INFO)) {
518 col_set_str(pinfo->cinfo, COL_INFO, "Connectionless ");
521 proto_tree_add_uint_format(quake3_tree,
522 hf_quake3_connectionless,
524 "Type: Connectionless");
525 dissect_quake3_ConnectionlessPacket(
526 tvb, pinfo, quake3_tree, &direction);
529 if (check_col(pinfo->cinfo, COL_INFO)) {
530 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
533 proto_tree_add_uint_format(quake3_tree,
537 dissect_quake3_GamePacket(
538 tvb, pinfo, quake3_tree, &direction);
540 if (direction != DIR_UNKNOWN && dir_item)
541 proto_item_set_text(dir_item,
543 val_to_str(direction,
544 names_direction, "%u"));
546 if (check_col(pinfo->cinfo, COL_INFO))
547 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(direction,
548 names_direction, "%u"));
553 proto_reg_handoff_quake3(void)
555 static int initialized=FALSE;
556 static dissector_handle_t quake3_handle;
557 static int server_port;
558 static int master_port;
562 quake3_handle = create_dissector_handle(dissect_quake3,
567 dissector_delete("udp.port", server_port+i, quake3_handle);
569 dissector_delete("udp.port", master_port+i, quake3_handle);
572 /* set port for future deletes */
573 server_port = gbl_quake3_server_port;
574 master_port = gbl_quake3_master_port;
578 dissector_add("udp.port", gbl_quake3_server_port + i,
581 dissector_add("udp.port", gbl_quake3_master_port + i,
583 data_handle = find_dissector("data");
588 proto_register_quake3(void)
590 static hf_register_info hf[] = {
591 { &hf_quake3_direction,
592 { "Direction", "quake3.direction",
593 FT_NONE, BASE_DEC, NULL, 0x0,
594 "Packet Direction", HFILL }},
595 { &hf_quake3_connectionless,
596 { "Connectionless", "quake3.connectionless",
597 FT_UINT32, BASE_DEC, NULL, 0x0,
598 "Connectionless", HFILL }},
600 { "Game", "quake3.game",
601 FT_UINT32, BASE_DEC, NULL, 0x0,
603 { &hf_quake3_connectionless_marker,
604 { "Marker", "quake3.connectionless.marker",
605 FT_UINT32, BASE_HEX, NULL, 0x0,
607 { &hf_quake3_connectionless_text,
608 { "Text", "quake3.connectionless.text",
609 FT_STRING, BASE_DEC, NULL, 0x0,
611 { &hf_quake3_connectionless_command,
612 { "Command", "quake3.connectionless.command",
613 FT_STRING, BASE_DEC, NULL, 0x0,
615 { &hf_quake3_server_addr,
616 { "Server Address", "quake3.server.addr",
617 FT_IPv4, BASE_DEC, NULL, 0x0,
618 "Server IP Address", HFILL }},
619 { &hf_quake3_server_port,
620 { "Server Port", "quake3.server.port",
621 FT_UINT16, BASE_DEC, NULL, 0x0,
622 "Server UDP Port", HFILL }},
623 { &hf_quake3_game_seq1,
624 { "Sequence Number", "quake3.game.seq1",
625 FT_UINT32, BASE_DEC, NULL, 0x0,
626 "Sequence number of the current packet", HFILL }},
627 { &hf_quake3_game_rel1,
628 { "Reliable", "quake3.game.rel1",
629 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
630 "Packet is reliable and may be retransmitted", HFILL }},
631 { &hf_quake3_game_seq2,
632 { "Sequence Number", "quake3.game.seq2",
633 FT_UINT32, BASE_DEC, NULL, 0x0,
634 "Sequence number of the last received packet", HFILL }},
635 { &hf_quake3_game_rel2,
636 { "Reliable", "quake3.game.rel2",
637 FT_BOOLEAN, BASE_DEC, NULL, 0x0,
638 "Packet was reliable and may be retransmitted", HFILL }},
639 { &hf_quake3_game_qport,
640 { "QPort", "quake3.game.qport",
641 FT_UINT32, BASE_DEC, NULL, 0x0,
642 "Quake III Arena Client Port", HFILL }}
644 static gint *ett[] = {
646 &ett_quake3_connectionless,
647 &ett_quake3_connectionless_text,
650 &ett_quake3_game_seq1,
651 &ett_quake3_game_seq2,
652 &ett_quake3_game_clc,
655 module_t *quake3_module;
657 proto_quake3 = proto_register_protocol("Quake III Arena Network Protocol",
659 proto_register_field_array(proto_quake3, hf, array_length(hf));
660 proto_register_subtree_array(ett, array_length(ett));
662 /* Register a configuration option for port */
663 quake3_module = prefs_register_protocol(proto_quake3,
664 proto_reg_handoff_quake3);
665 prefs_register_uint_preference(quake3_module, "udp.arena_port",
666 "Quake III Arena Server UDP Base Port",
667 "Set the UDP base port for the Quake III Arena Server",
668 10, &gbl_quake3_server_port);
669 prefs_register_uint_preference(quake3_module, "udp.master_port",
670 "Quake III Arena Master Server UDP Base Port",
671 "Set the UDP base port for the Quake III Arena Master Server",
672 10, &gbl_quake3_master_port);