2 * Routines for Quake III Arena packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * Copied from packet-quake2.c
12 * SPDX-License-Identifier: GPL-2.0-or-later
17 All informations used in this decoding were gathered from
18 * some own captures of a private server,
19 * the "Server commands howto" document written by id Software
20 (http://www.quake3arena.com/tech/ServerCommandsHowto.html)
21 * source code of the game server browser tool qstat
22 (http://www.qstat.org).
28 #include <epan/packet.h>
29 #include <epan/prefs.h>
30 #include <epan/addr_resolv.h>
32 void proto_register_quake3(void);
33 static dissector_handle_t quake3_handle
;
34 static int proto_quake3
;
36 static int hf_quake3_direction
;
37 static int hf_quake3_connectionless
;
38 static int hf_quake3_game
;
39 static int hf_quake3_connectionless_marker
;
40 static int hf_quake3_connectionless_text
;
41 static int hf_quake3_connectionless_command
;
42 static int hf_quake3_server_addr
;
43 static int hf_quake3_server_port
;
44 static int hf_quake3_game_seq1
;
45 static int hf_quake3_game_rel1
;
46 static int hf_quake3_game_seq2
;
47 static int hf_quake3_game_rel2
;
48 static int hf_quake3_game_qport
;
50 static int ett_quake3
;
51 static int ett_quake3_connectionless
;
52 static int ett_quake3_connectionless_text
;
53 static int ett_quake3_server
;
54 static int ett_quake3_game
;
55 static int ett_quake3_game_seq1
;
56 static int ett_quake3_game_seq2
;
57 static int ett_quake3_game_clc
;
58 static int ett_quake3_game_svc
;
60 #define QUAKE3_SERVER_PORT 27960
61 #define QUAKE3_MASTER_PORT 27950
62 static unsigned gbl_quake3_server_port
=QUAKE3_SERVER_PORT
;
63 static unsigned gbl_quake3_master_port
=QUAKE3_MASTER_PORT
;
66 static const value_string names_direction
[] = {
68 { DIR_UNKNOWN
, "Unknown" },
70 { DIR_C2S
, "Client to Server" },
72 { DIR_S2C
, "Server to Client" },
74 { DIR_C2M
, "Client to Master" },
76 { DIR_M2C
, "Master to Client" },
81 #define string_starts_with(s1, s2) (strncmp((s1), (s2), strlen(s2)) == 0)
84 static const value_string names_command
[] = {
85 #define COMMAND_UNKNOWN 0
86 { COMMAND_UNKNOWN
, "Unknown" },
87 #define COMMAND_statusResponse 1
88 { COMMAND_statusResponse
, "Reply Status" },
89 #define COMMAND_getstatus 2
90 { COMMAND_getstatus
, "Request Status" },
91 #define COMMAND_infoResponse 3
92 { COMMAND_infoResponse
, "Reply Info" },
93 #define COMMAND_getinfo 4
94 { COMMAND_getinfo
, "Request Info" },
95 #define COMMAND_challengeResponse 5
96 { COMMAND_challengeResponse
, "Reply Challenge" },
97 #define COMMAND_getchallenge 6
98 { COMMAND_getchallenge
, "Request Challenge" },
99 #define COMMAND_connectResponse 7
100 { COMMAND_connectResponse
, "Reply Connect" },
101 #define COMMAND_connect 8
102 { COMMAND_connect
, "Request Connect" },
103 #define COMMAND_rconResponse 9
104 { COMMAND_rconResponse
, "Reply Remote Command" },
105 #define COMMAND_rcon 10
106 { COMMAND_rcon
, "Request Remote Command" },
107 #define COMMAND_getmotdResponse 11
108 { COMMAND_getmotdResponse
, "Reply Motto of the Day" },
109 #define COMMAND_getmotd 12
110 { COMMAND_getmotd
, "Request Motto of the Day" },
111 #define COMMAND_getserversResponse 13
112 { COMMAND_getserversResponse
, "Reply Servers" },
113 #define COMMAND_getservers 14
114 { COMMAND_getservers
, "Request Servers" },
115 #define COMMAND_getKeyAuthorize 15
116 { COMMAND_getKeyAuthorize
, "Request Key Authorization" },
117 #define COMMAND_getIpAuthorize 16
118 { COMMAND_getIpAuthorize
, "Request IP Authorization" },
119 #define COMMAND_ipAuthorize 17
120 { COMMAND_ipAuthorize
, "Reply IP Authorization" },
126 dissect_quake3_ConnectionlessPacket(tvbuff_t
*tvb
, packet_info
*pinfo _U_
,
127 proto_tree
*tree
, int* direction
)
130 proto_item
*text_item
= NULL
;
131 proto_tree
*text_tree
= NULL
;
138 bool command_finished
= false;
140 cl_tree
= proto_tree_add_subtree(tree
, tvb
,
141 0, -1, ett_quake3_connectionless
, NULL
, "Connectionless");
143 marker
= tvb_get_ntohl(tvb
, 0);
144 proto_tree_add_uint(cl_tree
, hf_quake3_connectionless_marker
,
147 /* all the rest of the packet is just text */
151 * XXX - is there ever more than one null-terminated string in
154 * XXX - is the string guaranteed to be null-terminated (so
155 * that if there's no NUL at the end, it's an error)?
157 * XXX - are non-ASCII characters supported and, if so, what
158 * encoding is used for them?
160 text
= tvb_get_stringz_enc(pinfo
->pool
, tvb
, offset
, &len
, ENC_ASCII
|ENC_NA
);
162 text_item
= proto_tree_add_string(cl_tree
,
163 hf_quake3_connectionless_text
,
164 tvb
, offset
, len
, text
);
165 text_tree
= proto_item_add_subtree(text_item
, ett_quake3_connectionless_text
);
168 command
= COMMAND_UNKNOWN
;
171 if (strncmp(text
, "statusResponse", 14) == 0) {
172 command
= COMMAND_statusResponse
;
173 *direction
= DIR_S2C
;
176 else if (strncmp(text
, "getstatus", 9) == 0) {
177 command
= COMMAND_getstatus
;
178 *direction
= DIR_C2S
;
181 else if (strncmp(text
, "infoResponse", 12) == 0) {
182 command
= COMMAND_infoResponse
;
183 *direction
= DIR_S2C
;
186 else if (strncmp(text
, "getinfo", 7) == 0) {
187 command
= COMMAND_getinfo
;
188 *direction
= DIR_C2S
;
191 else if (strncmp(text
, "challengeResponse", 17) == 0) {
192 command
= COMMAND_challengeResponse
;
193 *direction
= DIR_S2C
;
196 else if (strncmp(text
, "getchallenge", 12) == 0) {
197 command
= COMMAND_getchallenge
;
198 *direction
= DIR_C2S
;
201 else if (strncmp(text
, "connectResponse", 15) == 0) {
202 command
= COMMAND_connectResponse
;
203 *direction
= DIR_S2C
;
206 else if (strncmp(text
, "connect", 7) == 0) {
207 command
= COMMAND_connect
;
208 *direction
= DIR_C2S
;
211 else if (strncmp(text
, "rconResponse", 12) == 0) {
212 command
= COMMAND_rconResponse
;
213 *direction
= DIR_S2C
;
216 else if (strncmp(text
, "rcon", 4) == 0) {
217 command
= COMMAND_rcon
;
218 *direction
= DIR_C2S
;
221 else if (strncmp(text
, "getmotdResponse", 15) == 0) {
222 command
= COMMAND_getmotdResponse
;
223 *direction
= DIR_M2C
;
226 else if (strncmp(text
, "getmotd", 7) == 0) {
227 command
= COMMAND_getmotd
;
228 *direction
= DIR_C2M
;
231 else if (strncmp(text
, "getserversResponse", 18) == 0) {
233 command
= COMMAND_getserversResponse
;
234 *direction
= DIR_M2C
;
236 /* The data can contain 0's, and the text string is binary
237 anyway, so better replace the text string after the first
240 /* first correct the length until the end of the packet */
241 proto_item_set_len(text_item
, tvb_captured_length_remaining(tvb
, offset
));
242 /* then replace the text */
243 proto_item_set_text(text_item
, "Text: getserversResponse<DATA>");
246 proto_tree_add_string(text_tree
, hf_quake3_connectionless_command
,
247 tvb
, offset
, command_len
,
248 val_to_str_const(command
, names_command
, "Unknown"));
249 command_finished
= true;
251 /* now we decode all the rest */
253 /* '/', ip-address in network order, port in network order */
254 while (tvb_reported_length_remaining(tvb
, base
) >= 7) {
258 ip_addr
= tvb_get_ipv4(tvb
, base
+ 1);
259 udp_port
= tvb_get_ntohs(tvb
, base
+ 5);
261 /* It may be a good idea to create a conversation for
262 every server in this list, as we'll see at
263 least a getinfo request to each of them and they
264 may run on totally unusual ports. */
267 proto_tree
*server_tree
;
268 server_tree
= proto_tree_add_subtree_format(text_tree
,
270 ett_quake3_server
, NULL
, "Server: %s:%u",
271 get_hostname(ip_addr
),
273 proto_tree_add_ipv4(server_tree
, hf_quake3_server_addr
,
274 tvb
, base
+ 1, 4, ip_addr
);
275 proto_tree_add_uint(server_tree
, hf_quake3_server_port
,
276 tvb
, base
+ 5, 2, udp_port
);
282 else if (strncmp(text
, "getservers", 10) == 0) {
283 command
= COMMAND_getservers
;
284 *direction
= DIR_C2M
;
287 else if (strncmp(text
, "getKeyAuthorize", 15) == 0) {
288 command
= COMMAND_getKeyAuthorize
;
289 *direction
= DIR_C2M
;
292 else if (strncmp(text
, "getIpAuthorize", 14) == 0) {
293 command
= COMMAND_getIpAuthorize
;
294 *direction
= DIR_C2M
;
297 else if (strncmp(text
, "ipAuthorize", 11) == 0) {
298 command
= COMMAND_ipAuthorize
;
299 *direction
= DIR_M2C
;
303 *direction
= DIR_UNKNOWN
;
306 if (text_tree
&& command_finished
== false) {
307 proto_tree_add_string(text_tree
, hf_quake3_connectionless_command
,
308 tvb
, offset
, command_len
,
309 val_to_str_const(command
, names_command
, "Unknown"));
318 dissect_quake3_client_commands(tvbuff_t
*tvb
, packet_info
*pinfo
,
321 /* this shouldn't be too difficult */
322 call_data_dissector(tvb
, pinfo
, tree
);
327 dissect_quake3_server_commands(tvbuff_t
*tvb
, packet_info
*pinfo
,
330 /* It is totally forbidden to decode this any further,
332 call_data_dissector(tvb
, pinfo
, tree
);
336 static const value_string names_reliable
[] = {
337 { 0, "Non Reliable" },
344 dissect_quake3_GamePacket(tvbuff_t
*tvb
, packet_info
*pinfo
,
345 proto_tree
*tree
, int *direction
)
347 proto_tree
*game_tree
;
353 unsigned rest_length
;
355 *direction
= (pinfo
->destport
== gbl_quake3_server_port
) ?
358 game_tree
= proto_tree_add_subtree(tree
, tvb
, 0, -1, ett_quake3_game
, NULL
, "Game");
362 seq1
= tvb_get_letohs(tvb
, offset
);
363 rel1
= seq1
& 0x8000 ? 1 : 0;
366 proto_tree
*seq1_tree
= proto_tree_add_subtree_format(game_tree
,
367 tvb
, offset
, 2, ett_quake3_game_seq1
, NULL
, "Current Sequence: %u (%s)",
368 seq1
, val_to_str(rel1
,names_reliable
,"%u"));
369 proto_tree_add_uint(seq1_tree
, hf_quake3_game_seq1
,
370 tvb
, offset
, 2, seq1
);
371 proto_tree_add_boolean(seq1_tree
, hf_quake3_game_rel1
,
372 tvb
, offset
+1, 1, rel1
);
376 seq2
= tvb_get_letohs(tvb
, offset
);
377 rel2
= seq2
& 0x8000 ? 1 : 0;
380 proto_tree
*seq2_tree
= proto_tree_add_subtree_format(game_tree
,
381 tvb
, offset
, 2, ett_quake3_game_seq2
, NULL
, "Acknowledge Sequence: %u (%s)",
382 seq2
, val_to_str(rel2
,names_reliable
,"%u"));
383 proto_tree_add_uint(seq2_tree
, hf_quake3_game_seq2
,
384 tvb
, offset
, 2, seq2
);
385 proto_tree_add_boolean(seq2_tree
, hf_quake3_game_rel2
,
386 tvb
, offset
+1, 1, rel2
);
390 if (*direction
== DIR_C2S
) {
391 /* client to server */
392 uint16_t qport
= tvb_get_letohs(tvb
, offset
);
394 proto_tree_add_uint(game_tree
, hf_quake3_game_qport
,
395 tvb
, offset
, 2, qport
);
400 /* all the rest is pure game data */
401 rest_length
= tvb_reported_length(tvb
) - offset
;
403 tvbuff_t
*next_tvb
= tvb_new_subset_remaining(tvb
, offset
);
406 if (*direction
== DIR_C2S
) {
407 c_tree
= proto_tree_add_subtree(game_tree
, next_tvb
,
408 0, -1, ett_quake3_game_clc
, NULL
, "Client Commands");
410 dissect_quake3_client_commands(next_tvb
, pinfo
, c_tree
);
413 c_tree
= proto_tree_add_subtree(game_tree
, next_tvb
,
414 0, -1, ett_quake3_game_svc
, NULL
, "Server Commands");
416 dissect_quake3_server_commands(next_tvb
, pinfo
, c_tree
);
423 dissect_quake3(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
425 proto_tree
*quake3_tree
= NULL
;
426 proto_item
*dir_item
= NULL
;
429 direction
= DIR_UNKNOWN
;
431 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "QUAKE3");
434 proto_item
*quake3_item
;
435 quake3_item
= proto_tree_add_item(tree
, proto_quake3
,
437 quake3_tree
= proto_item_add_subtree(quake3_item
, ett_quake3
);
439 dir_item
= proto_tree_add_none_format(
441 hf_quake3_direction
, tvb
, 0, 0,
443 val_to_str(direction
,
444 names_direction
, "%u"));
447 if (tvb_get_ntohl(tvb
, 0) == 0xffffffff) {
448 col_set_str(pinfo
->cinfo
, COL_INFO
, "Connectionless ");
449 proto_tree_add_uint_format(quake3_tree
,
450 hf_quake3_connectionless
,
452 "Type: Connectionless");
453 dissect_quake3_ConnectionlessPacket(
454 tvb
, pinfo
, quake3_tree
, &direction
);
457 col_set_str(pinfo
->cinfo
, COL_INFO
, "Game ");
458 proto_tree_add_uint_format(quake3_tree
,
462 dissect_quake3_GamePacket(
463 tvb
, pinfo
, quake3_tree
, &direction
);
465 if (direction
!= DIR_UNKNOWN
&& dir_item
)
466 proto_item_set_text(dir_item
,
468 val_to_str(direction
,
469 names_direction
, "%u"));
471 col_append_str(pinfo
->cinfo
, COL_INFO
, val_to_str(direction
,
472 names_direction
, "%u"));
473 return tvb_captured_length(tvb
);
477 void proto_reg_handoff_quake3(void);
480 proto_register_quake3(void)
482 static hf_register_info hf
[] = {
483 { &hf_quake3_direction
,
484 { "Direction", "quake3.direction",
485 FT_NONE
, BASE_NONE
, NULL
, 0x0,
486 "Packet Direction", HFILL
}},
487 { &hf_quake3_connectionless
,
488 { "Connectionless", "quake3.connectionless",
489 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
492 { "Game", "quake3.game",
493 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
495 { &hf_quake3_connectionless_marker
,
496 { "Marker", "quake3.connectionless.marker",
497 FT_UINT32
, BASE_HEX
, NULL
, 0x0,
499 { &hf_quake3_connectionless_text
,
500 { "Text", "quake3.connectionless.text",
501 FT_STRING
, BASE_NONE
, NULL
, 0x0,
503 { &hf_quake3_connectionless_command
,
504 { "Command", "quake3.connectionless.command",
505 FT_STRING
, BASE_NONE
, NULL
, 0x0,
507 { &hf_quake3_server_addr
,
508 { "Server Address", "quake3.server.addr",
509 FT_IPv4
, BASE_NONE
, NULL
, 0x0,
510 "Server IP Address", HFILL
}},
511 { &hf_quake3_server_port
,
512 { "Server Port", "quake3.server.port",
513 FT_UINT16
, BASE_DEC
, NULL
, 0x0,
514 "Server UDP Port", HFILL
}},
515 { &hf_quake3_game_seq1
,
516 { "Sequence Number", "quake3.game.seq1",
517 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
518 "Sequence number of the current packet", HFILL
}},
519 { &hf_quake3_game_rel1
,
520 { "Reliable", "quake3.game.rel1",
521 FT_BOOLEAN
, BASE_NONE
, NULL
, 0x0,
522 "Packet is reliable and may be retransmitted", HFILL
}},
523 { &hf_quake3_game_seq2
,
524 { "Sequence Number", "quake3.game.seq2",
525 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
526 "Sequence number of the last received packet", HFILL
}},
527 { &hf_quake3_game_rel2
,
528 { "Reliable", "quake3.game.rel2",
529 FT_BOOLEAN
, BASE_NONE
, NULL
, 0x0,
530 "Packet was reliable and may be retransmitted", HFILL
}},
531 { &hf_quake3_game_qport
,
532 { "QPort", "quake3.game.qport",
533 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
534 "Quake III Arena Client Port", HFILL
}}
536 static int *ett
[] = {
538 &ett_quake3_connectionless
,
539 &ett_quake3_connectionless_text
,
542 &ett_quake3_game_seq1
,
543 &ett_quake3_game_seq2
,
544 &ett_quake3_game_clc
,
547 module_t
*quake3_module
;
549 proto_quake3
= proto_register_protocol("Quake III Arena Network Protocol", "QUAKE3", "quake3");
550 proto_register_field_array(proto_quake3
, hf
, array_length(hf
));
551 proto_register_subtree_array(ett
, array_length(ett
));
553 /* Register the dissector handle */
554 quake3_handle
= register_dissector("quake3", dissect_quake3
, proto_quake3
);
556 /* Register a configuration option for port */
557 quake3_module
= prefs_register_protocol(proto_quake3
, proto_reg_handoff_quake3
);
558 prefs_register_uint_preference(quake3_module
, "udp.arena_port",
559 "Quake III Arena Server UDP Base Port",
560 "Set the UDP base port for the Quake III Arena Server",
561 10, &gbl_quake3_server_port
);
562 prefs_register_uint_preference(quake3_module
, "udp.master_port",
563 "Quake III Arena Master Server UDP Base Port",
564 "Set the UDP base port for the Quake III Arena Master Server",
565 10, &gbl_quake3_master_port
);
570 proto_reg_handoff_quake3(void)
572 static bool initialized
=false;
573 static unsigned server_port
;
574 static unsigned master_port
;
581 dissector_delete_uint("udp.port", server_port
+i
, quake3_handle
);
583 dissector_delete_uint("udp.port", master_port
+i
, quake3_handle
);
586 /* set port for future deletes */
587 server_port
= gbl_quake3_server_port
;
588 master_port
= gbl_quake3_master_port
;
590 /* add dissectors. Port preference names to specific to use "auto" */
592 dissector_add_uint("udp.port", gbl_quake3_server_port
+ i
,
595 dissector_add_uint("udp.port", gbl_quake3_master_port
+ i
,
600 * Editor modelines - https://www.wireshark.org/tools/modelines.html
605 * indent-tabs-mode: t
608 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
609 * :indentSize=8:tabSize=8:noTabs=false: