2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
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).
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
[] = {
84 { DIR_UNKNOWN
, "Unknown" },
86 { DIR_C2S
, "Client to Server" },
88 { DIR_S2C
, "Server to Client" },
90 { DIR_C2M
, "Client to Master" },
92 { DIR_M2C
, "Master to Client" },
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" },
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
;
154 gboolean command_finished
= FALSE
;
156 marker
= tvb_get_ntohl(tvb
, 0);
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
,
166 /* all the rest of the packet is just text */
169 len
= tvb_get_nstringz0(tvb
, offset
, sizeof(text
), text
);
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
;
180 if (strncmp(text
, "statusResponse", 14) == 0) {
181 command
= COMMAND_statusResponse
;
182 *direction
= DIR_S2C
;
185 else if (strncmp(text
, "getstatus", 9) == 0) {
186 command
= COMMAND_getstatus
;
187 *direction
= DIR_C2S
;
190 else if (strncmp(text
, "infoResponse", 12) == 0) {
191 command
= COMMAND_infoResponse
;
192 *direction
= DIR_S2C
;
195 else if (strncmp(text
, "getinfo", 7) == 0) {
196 command
= COMMAND_getinfo
;
197 *direction
= DIR_C2S
;
200 else if (strncmp(text
, "challengeResponse", 17) == 0) {
201 command
= COMMAND_challengeResponse
;
202 *direction
= DIR_S2C
;
205 else if (strncmp(text
, "getchallenge", 12) == 0) {
206 command
= COMMAND_getchallenge
;
207 *direction
= DIR_C2S
;
210 else if (strncmp(text
, "connectResponse", 15) == 0) {
211 command
= COMMAND_connectResponse
;
212 *direction
= DIR_S2C
;
215 else if (strncmp(text
, "connect", 7) == 0) {
216 command
= COMMAND_connect
;
217 *direction
= DIR_C2S
;
220 else if (strncmp(text
, "rconResponse", 12) == 0) {
221 command
= COMMAND_rconResponse
;
222 *direction
= DIR_S2C
;
225 else if (strncmp(text
, "rcon", 4) == 0) {
226 command
= COMMAND_rcon
;
227 *direction
= DIR_C2S
;
230 else if (strncmp(text
, "getmotdResponse", 15) == 0) {
231 command
= COMMAND_getmotdResponse
;
232 *direction
= DIR_M2C
;
235 else if (strncmp(text
, "getmotd", 7) == 0) {
236 command
= COMMAND_getmotd
;
237 *direction
= DIR_C2M
;
240 else if (strncmp(text
, "getserversResponse", 18) == 0) {
242 command
= COMMAND_getserversResponse
;
243 *direction
= DIR_M2C
;
245 /* The data can contain 0's, and the text string is binary
246 anyway, so better replace the text string after the first
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>");
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 */
262 /* '/', ip-address in network order, port in network order */
263 while (tvb_reported_length_remaining(tvb
, base
) >= 7) {
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. */
276 proto_item
*server_item
;
277 proto_tree
*server_tree
;
278 server_item
= proto_tree_add_text(text_tree
,
281 get_hostname(ip_addr
),
283 server_tree
= proto_item_add_subtree(
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
);
295 else if (strncmp(text
, "getservers", 10) == 0) {
296 command
= COMMAND_getservers
;
297 *direction
= DIR_C2M
;
300 else if (strncmp(text
, "getKeyAuthorize", 15) == 0) {
301 command
= COMMAND_getKeyAuthorize
;
302 *direction
= DIR_C2M
;
305 else if (strncmp(text
, "getIpAuthorize", 14) == 0) {
306 command
= COMMAND_getIpAuthorize
;
307 *direction
= DIR_C2M
;
310 else if (strncmp(text
, "ipAuthorize", 11) == 0) {
311 command
= COMMAND_ipAuthorize
;
312 *direction
= DIR_M2C
;
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;*/
331 dissect_quake3_client_commands(tvbuff_t
*tvb
, packet_info
*pinfo
,
334 /* this shouldn't be too difficult */
335 call_dissector(data_handle
,tvb
, pinfo
, tree
);
340 dissect_quake3_server_commands(tvbuff_t
*tvb
, packet_info
*pinfo
,
343 /* It is totally forbidden to decode this any further,
345 call_dissector(data_handle
,tvb
, pinfo
, tree
);
349 static const value_string names_reliable
[] = {
350 { 0, "Non Reliable" },
357 dissect_quake3_GamePacket(tvbuff_t
*tvb
, packet_info
*pinfo
,
358 proto_tree
*tree
, int *direction
)
360 proto_tree
*game_tree
= NULL
;
368 *direction
= (pinfo
->destport
== gbl_quake3_server_port
) ?
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
);
379 seq1
= tvb_get_letohs(tvb
, offset
);
380 rel1
= seq1
& 0x8000 ? 1 : 0;
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
);
395 seq2
= tvb_get_letohs(tvb
, offset
);
396 rel2
= seq2
& 0x8000 ? 1 : 0;
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
);
411 if (*direction
== DIR_C2S
) {
412 /* client to server */
413 guint16 qport
= tvb_get_letohs(tvb
, offset
);
415 proto_tree_add_uint(game_tree
, hf_quake3_game_qport
,
416 tvb
, offset
, 2, qport
);
421 /* all the rest is pure game data */
422 rest_length
= tvb_reported_length(tvb
) - offset
;
425 tvb_new_subset(tvb
, offset
, rest_length
, rest_length
);
427 if (*direction
== DIR_C2S
) {
428 proto_tree
*c_tree
= NULL
;
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
);
439 proto_tree
*c_tree
= NULL
;
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
);
454 dissect_quake3(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
456 proto_tree
*quake3_tree
= NULL
;
457 proto_item
*dir_item
= NULL
;
460 direction
= DIR_UNKNOWN
;
462 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "QUAKE3");
465 proto_item
*quake3_item
;
466 quake3_item
= proto_tree_add_item(tree
, proto_quake3
,
468 quake3_tree
= proto_item_add_subtree(quake3_item
, ett_quake3
);
470 dir_item
= proto_tree_add_none_format(
472 hf_quake3_direction
, tvb
, 0, 0,
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
,
483 "Type: Connectionless");
484 dissect_quake3_ConnectionlessPacket(
485 tvb
, pinfo
, quake3_tree
, &direction
);
488 col_set_str(pinfo
->cinfo
, COL_INFO
, "Game ");
489 proto_tree_add_uint_format(quake3_tree
,
493 dissect_quake3_GamePacket(
494 tvb
, pinfo
, quake3_tree
, &direction
);
496 if (direction
!= DIR_UNKNOWN
&& dir_item
)
497 proto_item_set_text(dir_item
,
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);
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,
522 { "Game", "quake3.game",
523 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
525 { &hf_quake3_connectionless_marker
,
526 { "Marker", "quake3.connectionless.marker",
527 FT_UINT32
, BASE_HEX
, NULL
, 0x0,
529 { &hf_quake3_connectionless_text
,
530 { "Text", "quake3.connectionless.text",
531 FT_STRING
, BASE_NONE
, NULL
, 0x0,
533 { &hf_quake3_connectionless_command
,
534 { "Command", "quake3.connectionless.command",
535 FT_STRING
, BASE_NONE
, NULL
, 0x0,
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
[] = {
568 &ett_quake3_connectionless
,
569 &ett_quake3_connectionless_text
,
572 &ett_quake3_game_seq1
,
573 &ett_quake3_game_seq2
,
574 &ett_quake3_game_clc
,
577 module_t
*quake3_module
;
579 proto_quake3
= proto_register_protocol("Quake III Arena Network Protocol",
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
);
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
;
608 quake3_handle
= create_dissector_handle(dissect_quake3
,
610 data_handle
= find_dissector("data");
614 dissector_delete_uint("udp.port", server_port
+i
, quake3_handle
);
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
;
625 dissector_add_uint("udp.port", gbl_quake3_server_port
+ i
,
628 dissector_add_uint("udp.port", gbl_quake3_master_port
+ i
,