Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-quake3.c
blobb084413307bd22ae9197c25b2e24f682d3d65f16
1 /* packet-quake3.c
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).
26 #include "config.h"
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[] = {
67 #define DIR_UNKNOWN 0
68 { DIR_UNKNOWN, "Unknown" },
69 #define DIR_C2S 1
70 { DIR_C2S, "Client to Server" },
71 #define DIR_S2C 2
72 { DIR_S2C, "Server to Client" },
73 #define DIR_C2M 3
74 { DIR_C2M, "Client to Master" },
75 #define DIR_M2C 4
76 { DIR_M2C, "Master to Client" },
77 { 0, NULL }
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" },
121 { 0, NULL }
125 static void
126 dissect_quake3_ConnectionlessPacket(tvbuff_t *tvb, packet_info *pinfo _U_,
127 proto_tree *tree, int* direction)
129 proto_tree *cl_tree;
130 proto_item *text_item = NULL;
131 proto_tree *text_tree = NULL;
132 uint8_t *text;
133 int len;
134 int offset;
135 uint32_t marker;
136 int command;
137 int command_len;
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,
145 tvb, 0, 4, marker);
147 /* all the rest of the packet is just text */
148 offset = 4;
151 * XXX - is there ever more than one null-terminated string in
152 * the packet?
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);
161 if (cl_tree) {
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;
169 command_len = 0;
171 if (strncmp(text, "statusResponse", 14) == 0) {
172 command = COMMAND_statusResponse;
173 *direction = DIR_S2C;
174 command_len = 14;
176 else if (strncmp(text, "getstatus", 9) == 0) {
177 command = COMMAND_getstatus;
178 *direction = DIR_C2S;
179 command_len = 9;
181 else if (strncmp(text, "infoResponse", 12) == 0) {
182 command = COMMAND_infoResponse;
183 *direction = DIR_S2C;
184 command_len = 12;
186 else if (strncmp(text, "getinfo", 7) == 0) {
187 command = COMMAND_getinfo;
188 *direction = DIR_C2S;
189 command_len = 7;
191 else if (strncmp(text, "challengeResponse", 17) == 0) {
192 command = COMMAND_challengeResponse;
193 *direction = DIR_S2C;
194 command_len = 17;
196 else if (strncmp(text, "getchallenge", 12) == 0) {
197 command = COMMAND_getchallenge;
198 *direction = DIR_C2S;
199 command_len = 12;
201 else if (strncmp(text, "connectResponse", 15) == 0) {
202 command = COMMAND_connectResponse;
203 *direction = DIR_S2C;
204 command_len = 15;
206 else if (strncmp(text, "connect", 7) == 0) {
207 command = COMMAND_connect;
208 *direction = DIR_C2S;
209 command_len = 7;
211 else if (strncmp(text, "rconResponse", 12) == 0) {
212 command = COMMAND_rconResponse;
213 *direction = DIR_S2C;
214 command_len = 12;
216 else if (strncmp(text, "rcon", 4) == 0) {
217 command = COMMAND_rcon;
218 *direction = DIR_C2S;
219 command_len = 4;
221 else if (strncmp(text, "getmotdResponse", 15) == 0) {
222 command = COMMAND_getmotdResponse;
223 *direction = DIR_M2C;
224 command_len = 15;
226 else if (strncmp(text, "getmotd", 7) == 0) {
227 command = COMMAND_getmotd;
228 *direction = DIR_C2M;
229 command_len = 7;
231 else if (strncmp(text, "getserversResponse", 18) == 0) {
232 int base;
233 command = COMMAND_getserversResponse;
234 *direction = DIR_M2C;
235 command_len = 18;
236 /* The data can contain 0's, and the text string is binary
237 anyway, so better replace the text string after the first
238 \ with "<DATA>". */
239 if (text_item) {
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>");
245 if (text_tree)
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 */
252 base = offset + 18;
253 /* '/', ip-address in network order, port in network order */
254 while (tvb_reported_length_remaining(tvb, base) >= 7) {
255 uint32_t ip_addr;
256 uint16_t udp_port;
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. */
266 if (text_tree) {
267 proto_tree *server_tree;
268 server_tree = proto_tree_add_subtree_format(text_tree,
269 tvb, base, 7,
270 ett_quake3_server, NULL, "Server: %s:%u",
271 get_hostname(ip_addr),
272 udp_port);
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);
279 base += 7;
282 else if (strncmp(text, "getservers", 10) == 0) {
283 command = COMMAND_getservers;
284 *direction = DIR_C2M;
285 command_len = 10;
287 else if (strncmp(text, "getKeyAuthorize", 15) == 0) {
288 command = COMMAND_getKeyAuthorize;
289 *direction = DIR_C2M;
290 command_len = 15;
292 else if (strncmp(text, "getIpAuthorize", 14) == 0) {
293 command = COMMAND_getIpAuthorize;
294 *direction = DIR_C2M;
295 command_len = 14;
297 else if (strncmp(text, "ipAuthorize", 11) == 0) {
298 command = COMMAND_ipAuthorize;
299 *direction = DIR_M2C;
300 command_len = 11;
302 else {
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"));
312 /*offset += len;*/
317 static void
318 dissect_quake3_client_commands(tvbuff_t *tvb, packet_info *pinfo,
319 proto_tree *tree)
321 /* this shouldn't be too difficult */
322 call_data_dissector(tvb, pinfo, tree);
326 static void
327 dissect_quake3_server_commands(tvbuff_t *tvb, packet_info *pinfo,
328 proto_tree *tree)
330 /* It is totally forbidden to decode this any further,
331 I won't do it. */
332 call_data_dissector(tvb, pinfo, tree);
336 static const value_string names_reliable[] = {
337 { 0, "Non Reliable" },
338 { 1, "Reliable" },
339 { 0, NULL }
343 static void
344 dissect_quake3_GamePacket(tvbuff_t *tvb, packet_info *pinfo,
345 proto_tree *tree, int *direction)
347 proto_tree *game_tree;
348 uint32_t seq1;
349 uint32_t seq2;
350 int rel1;
351 int rel2;
352 int offset;
353 unsigned rest_length;
355 *direction = (pinfo->destport == gbl_quake3_server_port) ?
356 DIR_C2S : DIR_S2C;
358 game_tree = proto_tree_add_subtree(tree, tvb, 0, -1, ett_quake3_game, NULL, "Game");
360 offset = 0;
362 seq1 = tvb_get_letohs(tvb, offset);
363 rel1 = seq1 & 0x8000 ? 1 : 0;
364 seq1 &= ~0x8000;
365 if (game_tree) {
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);
374 offset += 2;
376 seq2 = tvb_get_letohs(tvb, offset);
377 rel2 = seq2 & 0x8000 ? 1 : 0;
378 seq2 &= ~0x8000;
379 if (game_tree) {
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);
388 offset += 2;
390 if (*direction == DIR_C2S) {
391 /* client to server */
392 uint16_t qport = tvb_get_letohs(tvb, offset);
393 if (game_tree) {
394 proto_tree_add_uint(game_tree, hf_quake3_game_qport,
395 tvb, offset, 2, qport);
397 offset +=2;
400 /* all the rest is pure game data */
401 rest_length = tvb_reported_length(tvb) - offset;
402 if (rest_length) {
403 tvbuff_t *next_tvb = tvb_new_subset_remaining(tvb, offset);
404 proto_tree *c_tree;
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);
412 else {
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);
422 static int
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;
427 int direction;
429 direction = DIR_UNKNOWN;
431 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE3");
433 if (tree) {
434 proto_item *quake3_item;
435 quake3_item = proto_tree_add_item(tree, proto_quake3,
436 tvb, 0, -1, ENC_NA);
437 quake3_tree = proto_item_add_subtree(quake3_item, ett_quake3);
439 dir_item = proto_tree_add_none_format(
440 quake3_tree,
441 hf_quake3_direction, tvb, 0, 0,
442 "Direction: %s",
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,
451 tvb, 0, 0, 1,
452 "Type: Connectionless");
453 dissect_quake3_ConnectionlessPacket(
454 tvb, pinfo, quake3_tree, &direction);
456 else {
457 col_set_str(pinfo->cinfo, COL_INFO, "Game ");
458 proto_tree_add_uint_format(quake3_tree,
459 hf_quake3_game,
460 tvb, 0, 0, 1,
461 "Type: Game");
462 dissect_quake3_GamePacket(
463 tvb, pinfo, quake3_tree, &direction);
465 if (direction != DIR_UNKNOWN && dir_item)
466 proto_item_set_text(dir_item,
467 "Direction: %s",
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);
479 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,
490 NULL, HFILL }},
491 { &hf_quake3_game,
492 { "Game", "quake3.game",
493 FT_UINT32, BASE_DEC, NULL, 0x0,
494 NULL, HFILL }},
495 { &hf_quake3_connectionless_marker,
496 { "Marker", "quake3.connectionless.marker",
497 FT_UINT32, BASE_HEX, NULL, 0x0,
498 NULL, HFILL }},
499 { &hf_quake3_connectionless_text,
500 { "Text", "quake3.connectionless.text",
501 FT_STRING, BASE_NONE, NULL, 0x0,
502 NULL, HFILL }},
503 { &hf_quake3_connectionless_command,
504 { "Command", "quake3.connectionless.command",
505 FT_STRING, BASE_NONE, NULL, 0x0,
506 NULL, HFILL }},
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[] = {
537 &ett_quake3,
538 &ett_quake3_connectionless,
539 &ett_quake3_connectionless_text,
540 &ett_quake3_server,
541 &ett_quake3_game,
542 &ett_quake3_game_seq1,
543 &ett_quake3_game_seq2,
544 &ett_quake3_game_clc,
545 &ett_quake3_game_svc
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);
569 void
570 proto_reg_handoff_quake3(void)
572 static bool initialized=false;
573 static unsigned server_port;
574 static unsigned master_port;
575 int i;
577 if (!initialized) {
578 initialized=true;
579 } else {
580 for (i=0;i<4;i++)
581 dissector_delete_uint("udp.port", server_port+i, quake3_handle);
582 for (i=0;i<4;i++)
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" */
591 for (i=0;i<4;i++)
592 dissector_add_uint("udp.port", gbl_quake3_server_port + i,
593 quake3_handle);
594 for (i=0;i<4;i++)
595 dissector_add_uint("udp.port", gbl_quake3_master_port + i,
596 quake3_handle);
600 * Editor modelines - https://www.wireshark.org/tools/modelines.html
602 * Local variables:
603 * c-basic-offset: 8
604 * tab-width: 8
605 * indent-tabs-mode: t
606 * End:
608 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
609 * :indentSize=8:tabSize=8:noTabs=false: