2 * Routines for Quake packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
5 * http://www.idsoftware.com/q1source/q1source.zip
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * Copied from packet-tftp.c
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <epan/packet.h>
36 #include <epan/conversation.h>
37 #include <epan/prefs.h>
39 static int proto_quake = -1;
40 static int hf_quake_header_flags = -1;
41 static int hf_quake_header_length = -1;
42 static int hf_quake_header_sequence = -1;
43 static int hf_quake_control_command = -1;
45 static int hf_quake_CCREQ_CONNECT_game = -1;
46 static int hf_quake_CCREQ_CONNECT_version = -1;
47 static int hf_quake_CCREQ_SERVER_INFO_game = -1;
48 static int hf_quake_CCREQ_SERVER_INFO_version = -1;
49 static int hf_quake_CCREQ_PLAYER_INFO_player = -1;
50 static int hf_quake_CCREQ_RULE_INFO_lastrule = -1;
52 static int hf_quake_CCREP_ACCEPT_port = -1;
53 static int hf_quake_CCREP_REJECT_reason = -1;
54 static int hf_quake_CCREP_SERVER_INFO_address = -1;
55 static int hf_quake_CCREP_SERVER_INFO_server = -1;
56 static int hf_quake_CCREP_SERVER_INFO_map = -1;
57 static int hf_quake_CCREP_SERVER_INFO_num_player = -1;
58 static int hf_quake_CCREP_SERVER_INFO_max_player = -1;
59 static int hf_quake_CCREP_PLAYER_INFO_name = -1;
60 static int hf_quake_CCREP_PLAYER_INFO_colors = -1;
61 static int hf_quake_CCREP_PLAYER_INFO_colors_shirt = -1;
62 static int hf_quake_CCREP_PLAYER_INFO_colors_pants = -1;
63 static int hf_quake_CCREP_PLAYER_INFO_frags = -1;
64 static int hf_quake_CCREP_PLAYER_INFO_connect_time = -1;
65 static int hf_quake_CCREP_PLAYER_INFO_address = -1;
66 static int hf_quake_CCREP_RULE_INFO_rule = -1;
67 static int hf_quake_CCREP_RULE_INFO_value = -1;
70 static gint ett_quake = -1;
71 static gint ett_quake_control = -1;
72 static gint ett_quake_control_colors = -1;
73 static gint ett_quake_flags = -1;
75 static dissector_handle_t quake_handle;
76 static dissector_handle_t data_handle;
78 /* I took these names directly out of the Q1 source. */
79 #define NETFLAG_LENGTH_MASK 0x0000ffff
80 #define NET_HEADERSIZE 8
81 #define DEFAULTnet_hostport 26000
82 static unsigned int gbl_quakeServerPort=DEFAULTnet_hostport;
84 #define NETFLAG_LENGTH_MASK 0x0000ffff
85 #define NETFLAG_DATA 0x00010000
86 #define NETFLAG_ACK 0x00020000
87 #define NETFLAG_NAK 0x00040000
88 #define NETFLAG_EOM 0x00080000
89 #define NETFLAG_UNRELIABLE 0x00100000
90 #define NETFLAG_CTL 0x80000000
93 #define CCREQ_CONNECT 0x01
94 #define CCREQ_SERVER_INFO 0x02
95 #define CCREQ_PLAYER_INFO 0x03
96 #define CCREQ_RULE_INFO 0x04
98 #define CCREP_ACCEPT 0x81
99 #define CCREP_REJECT 0x82
100 #define CCREP_SERVER_INFO 0x83
101 #define CCREP_PLAYER_INFO 0x84
102 #define CCREP_RULE_INFO 0x85
104 static const value_string names_control_command[] = {
105 { CCREQ_CONNECT, "connect" },
106 { CCREQ_SERVER_INFO, "server_info" },
107 { CCREQ_PLAYER_INFO, "player_info" },
108 { CCREQ_RULE_INFO, "rule_info" },
109 { CCREP_ACCEPT, "accept" },
110 { CCREP_REJECT, "reject" },
111 { CCREP_SERVER_INFO, "server_info" },
112 { CCREP_PLAYER_INFO, "player_info" },
113 { CCREP_RULE_INFO, "rule_info" },
120 #define QUAKE_MAXSTRING 0x800
122 static const value_string names_control_direction[] = {
123 { CCREQ, "Request" },
129 static const value_string names_colors[] = {
150 static void dissect_quake(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
155 dissect_quake_CCREQ_CONNECT
156 (tvbuff_t *tvb, proto_tree *tree)
164 ti = proto_tree_add_item(tree, hf_quake_CCREQ_CONNECT_game,
165 tvb, offset, -1, TRUE);
166 offset += proto_item_get_len(ti);
168 proto_tree_add_item(tree, hf_quake_CCREQ_CONNECT_version,
169 tvb, offset, 1, TRUE);
175 dissect_quake_CCREQ_SERVER_INFO
176 (tvbuff_t *tvb, proto_tree *tree)
184 ti = proto_tree_add_item(tree, hf_quake_CCREQ_SERVER_INFO_game,
185 tvb, offset, -1, TRUE);
186 offset += proto_item_get_len(ti);
187 proto_tree_add_item(tree, hf_quake_CCREQ_SERVER_INFO_version,
188 tvb, offset, 1, TRUE);
194 dissect_quake_CCREQ_PLAYER_INFO
195 (tvbuff_t *tvb, proto_tree *tree)
198 proto_tree_add_item(tree, hf_quake_CCREQ_PLAYER_INFO_player,
205 dissect_quake_CCREQ_RULE_INFO
206 (tvbuff_t *tvb, proto_tree *tree)
209 proto_tree_add_item(tree, hf_quake_CCREQ_RULE_INFO_lastrule,
216 dissect_quake_CCREP_ACCEPT
217 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
222 port = tvb_get_letohl(tvb, 0);
223 c = conversation_new( pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP, port,
226 conversation_set_dissector(c, quake_handle);
229 proto_tree_add_uint(tree, hf_quake_CCREP_ACCEPT_port,
236 dissect_quake_CCREP_REJECT
237 (tvbuff_t *tvb, proto_tree *tree)
240 proto_tree_add_item(tree, hf_quake_CCREP_REJECT_reason,
247 dissect_quake_CCREP_SERVER_INFO
248 (tvbuff_t *tvb, proto_tree *tree)
256 ti = proto_tree_add_item(tree,
257 hf_quake_CCREP_SERVER_INFO_address, tvb, offset, -1,
259 offset += proto_item_get_len(ti);
261 ti = proto_tree_add_item(tree,
262 hf_quake_CCREP_SERVER_INFO_server, tvb, offset, -1,
264 offset += proto_item_get_len(ti);
266 ti = proto_tree_add_item(tree, hf_quake_CCREP_SERVER_INFO_map,
267 tvb, offset, -1, TRUE);
268 offset += proto_item_get_len(ti);
270 proto_tree_add_item(tree, hf_quake_CCREP_SERVER_INFO_num_player,
271 tvb, offset, 1, TRUE);
273 proto_tree_add_item(tree, hf_quake_CCREP_SERVER_INFO_max_player,
274 tvb, offset, 1, TRUE);
276 proto_tree_add_item(tree, hf_quake_CCREQ_SERVER_INFO_version,
277 tvb, offset, 1, TRUE);
283 dissect_quake_CCREP_PLAYER_INFO
284 (tvbuff_t *tvb, proto_tree *tree)
291 proto_item *colors_item;
292 proto_tree *colors_tree;
297 proto_tree_add_item(tree, hf_quake_CCREQ_PLAYER_INFO_player,
298 tvb, offset, 1, TRUE);
301 ti = proto_tree_add_item(tree, hf_quake_CCREP_PLAYER_INFO_name,
302 tvb, offset, -1, TRUE);
303 offset += proto_item_get_len(ti);
305 colors = tvb_get_letohl(tvb, offset + 0);
306 color_shirt = (colors >> 4) & 0x0f;
307 color_pants = (colors ) & 0x0f;
309 colors_item = proto_tree_add_uint(tree,
310 hf_quake_CCREP_PLAYER_INFO_colors,
311 tvb, offset, 4, colors);
312 colors_tree = proto_item_add_subtree(colors_item,
313 ett_quake_control_colors);
314 proto_tree_add_uint(colors_tree,
315 hf_quake_CCREP_PLAYER_INFO_colors_shirt,
316 tvb, offset, 1, color_shirt);
317 proto_tree_add_uint(colors_tree,
318 hf_quake_CCREP_PLAYER_INFO_colors_pants,
319 tvb, offset, 1, color_pants);
321 proto_tree_add_item(tree, hf_quake_CCREP_PLAYER_INFO_frags,
322 tvb, offset, 4, TRUE);
324 proto_tree_add_item(tree, hf_quake_CCREP_PLAYER_INFO_connect_time,
325 tvb, offset, 4, TRUE);
328 proto_tree_add_item(tree, hf_quake_CCREP_PLAYER_INFO_address,
329 tvb, offset, -1, TRUE);
335 dissect_quake_CCREP_RULE_INFO
336 (tvbuff_t *tvb, proto_tree *tree)
341 if (tvb_reported_length(tvb) == 0) return;
346 ti = proto_tree_add_item(tree, hf_quake_CCREP_RULE_INFO_rule,
347 tvb, offset, -1, TRUE);
348 offset += proto_item_get_len(ti);
350 proto_tree_add_item(tree, hf_quake_CCREP_RULE_INFO_value,
351 tvb, offset, -1, TRUE);
357 dissect_quake_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
361 proto_item *control_item = NULL;
362 proto_tree *control_tree = NULL;
366 command = tvb_get_guint8(tvb, 0);
367 direction = (command & 0x80) ? CCREP : CCREQ;
369 if (check_col(pinfo->cinfo, COL_INFO)) {
370 col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s",
371 val_to_str(command,names_control_command, "%u"),
372 val_to_str(direction,names_control_direction,"%u"));
376 control_item = proto_tree_add_text(tree, tvb,
377 0, -1, "Control %s: %s",
378 val_to_str(direction, names_control_direction, "%u"),
379 val_to_str(command, names_control_command, "%u"));
381 control_tree = proto_item_add_subtree(control_item,
383 proto_tree_add_uint(control_tree, hf_quake_control_command,
387 rest_length = tvb_reported_length(tvb) - 1;
388 next_tvb = tvb_new_subset(tvb, 1, rest_length , rest_length);
391 dissect_quake_CCREQ_CONNECT
392 (next_tvb, control_tree);
394 case CCREQ_SERVER_INFO:
395 dissect_quake_CCREQ_SERVER_INFO
396 (next_tvb, control_tree);
398 case CCREQ_PLAYER_INFO:
399 dissect_quake_CCREQ_PLAYER_INFO
400 (next_tvb, control_tree);
402 case CCREQ_RULE_INFO:
403 dissect_quake_CCREQ_RULE_INFO
404 (next_tvb, control_tree);
407 dissect_quake_CCREP_ACCEPT
408 (next_tvb, pinfo, control_tree);
411 dissect_quake_CCREP_REJECT
412 (next_tvb, control_tree);
414 case CCREP_SERVER_INFO:
415 dissect_quake_CCREP_SERVER_INFO
416 (next_tvb, control_tree);
418 case CCREP_PLAYER_INFO:
419 dissect_quake_CCREP_PLAYER_INFO
420 (next_tvb, control_tree);
422 case CCREP_RULE_INFO:
423 dissect_quake_CCREP_RULE_INFO
424 (next_tvb, control_tree);
427 call_dissector(data_handle,next_tvb, pinfo, control_tree);
434 dissect_quake(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
436 proto_tree *quake_tree = NULL;
437 proto_item *quake_item = NULL;
440 guint32 sequence = 0;
444 if (check_col(pinfo->cinfo, COL_PROTOCOL))
445 col_set_str(pinfo->cinfo, COL_PROTOCOL, "QUAKE");
446 if (check_col(pinfo->cinfo, COL_INFO))
447 col_clear(pinfo->cinfo, COL_INFO);
449 length = tvb_get_ntohl(tvb, 0);
450 flags = length & (~NETFLAG_LENGTH_MASK);
451 length &= NETFLAG_LENGTH_MASK;
454 quake_item = proto_tree_add_item(tree, proto_quake,
457 quake_tree = proto_item_add_subtree(quake_item, ett_quake);
461 proto_item* flags_item = NULL;
462 proto_tree* flags_tree = NULL;
464 flags_item = proto_tree_add_uint(quake_tree, hf_quake_header_flags,
467 flags_tree = proto_item_add_subtree(flags_item, ett_quake_flags);
471 proto_tree_add_text(flags_tree, tvb, 0, 2,
472 decode_boolean_bitfield(flags, NETFLAG_DATA, 32,
474 proto_tree_add_text(flags_tree, tvb, 0, 2,
475 decode_boolean_bitfield(flags, NETFLAG_ACK, 32,
476 "Acknowledgment","-"));
477 proto_tree_add_text(flags_tree, tvb, 0, 2,
478 decode_boolean_bitfield(flags, NETFLAG_NAK, 32,
479 "No Acknowledgment","-"));
480 proto_tree_add_text(flags_tree, tvb, 0, 2,
481 decode_boolean_bitfield(flags, NETFLAG_EOM, 32,
482 "End Of Message","-"));
483 proto_tree_add_text(flags_tree, tvb, 0, 2,
484 decode_boolean_bitfield(flags, NETFLAG_UNRELIABLE, 32,
486 proto_tree_add_text(flags_tree, tvb, 0, 2,
487 decode_boolean_bitfield(flags, NETFLAG_CTL, 32,
490 proto_tree_add_uint(quake_tree, hf_quake_header_length,
494 if (flags == NETFLAG_CTL) {
495 rest_length = tvb_reported_length(tvb) - 4;
496 next_tvb = tvb_new_subset(tvb, 4, rest_length , rest_length);
497 dissect_quake_control(next_tvb, pinfo, quake_tree);
501 sequence = tvb_get_ntohl(tvb, 4);
502 if (check_col(pinfo->cinfo, COL_INFO)) {
503 col_add_fstr(pinfo->cinfo, COL_INFO, "seq 0x%x", sequence);
506 proto_tree_add_uint(quake_tree, hf_quake_header_sequence,
507 tvb, 4, 4, sequence);
510 rest_length = tvb_reported_length(tvb) - 8;
511 next_tvb = tvb_new_subset(tvb, 8, rest_length , rest_length);
512 call_dissector(data_handle,next_tvb, pinfo, quake_tree);
517 proto_reg_handoff_quake(void)
519 static int Initialized=FALSE;
520 static int ServerPort=0;
523 dissector_delete("udp.port", ServerPort, quake_handle);
528 /* set port for future deletes */
529 ServerPort=gbl_quakeServerPort;
531 dissector_add("udp.port", gbl_quakeServerPort, quake_handle);
532 data_handle = find_dissector("data");
537 proto_register_quake(void)
539 static hf_register_info hf[] = {
540 { &hf_quake_header_flags,
541 { "Flags", "quake.header.flags",
542 FT_UINT16, BASE_HEX, NULL, 0x0,
544 { &hf_quake_header_length,
545 { "Length", "quake.header.length",
546 FT_UINT16, BASE_DEC, NULL, 0x0,
547 "full data length", HFILL }},
548 { &hf_quake_header_sequence,
549 { "Sequence", "quake.header.sequence",
550 FT_UINT32, BASE_HEX, NULL, 0x0,
551 "Sequence Number", HFILL }},
552 { &hf_quake_control_command,
553 { "Command", "quake.control.command",
554 FT_UINT8, BASE_HEX, VALS(names_control_command), 0x0,
555 "Control Command", HFILL }},
556 { &hf_quake_CCREQ_CONNECT_game,
557 { "Game", "quake.control.connect.game",
558 FT_STRINGZ, BASE_NONE, NULL, 0x0,
559 "Game Name", HFILL }},
560 { &hf_quake_CCREQ_CONNECT_version,
561 { "Version", "quake.control.connect.version",
562 FT_UINT8, BASE_DEC, NULL, 0x0,
563 "Game Protocol Version Number", HFILL }},
564 { &hf_quake_CCREQ_SERVER_INFO_game,
565 { "Game", "quake.control.server_info.game",
566 FT_STRINGZ, BASE_NONE, NULL, 0x0,
567 "Game Name", HFILL }},
568 { &hf_quake_CCREQ_SERVER_INFO_version,
569 { "Version", "quake.control.server_info.version",
570 FT_UINT8, BASE_DEC, NULL, 0x0,
571 "Game Protocol Version Number", HFILL }},
572 { &hf_quake_CCREQ_PLAYER_INFO_player,
573 { "Player", "quake.control.player_info.player",
574 FT_UINT8, BASE_DEC, NULL, 0x0,
576 { &hf_quake_CCREQ_RULE_INFO_lastrule,
577 { "Last Rule", "quake.control.rule_info.lastrule",
578 FT_STRINGZ, BASE_NONE, NULL, 0x0,
579 "Last Rule Name", HFILL }},
580 { &hf_quake_CCREP_ACCEPT_port,
581 { "Port", "quake.control.accept.port",
582 FT_UINT32, BASE_DEC, NULL, 0x0,
583 "Game Data Port", HFILL }},
584 { &hf_quake_CCREP_REJECT_reason,
585 { "Reason", "quake.control.reject.reason",
586 FT_STRINGZ, BASE_NONE, NULL, 0x0,
587 "Reject Reason", HFILL }},
588 { &hf_quake_CCREP_SERVER_INFO_address,
589 { "Address", "quake.control.server_info.address",
590 FT_STRINGZ, BASE_NONE, NULL, 0x0,
591 "Server Address", HFILL }},
592 { &hf_quake_CCREP_SERVER_INFO_server,
593 { "Server", "quake.control.server_info.server",
594 FT_STRINGZ, BASE_NONE, NULL, 0x0,
595 "Server Name", HFILL }},
596 { &hf_quake_CCREP_SERVER_INFO_map,
597 { "Map", "quake.control.server_info.map",
598 FT_STRINGZ, BASE_NONE, NULL, 0x0,
599 "Map Name", HFILL }},
600 { &hf_quake_CCREP_SERVER_INFO_num_player,
601 { "Number of Players", "quake.control.server_info.num_player",
602 FT_UINT8, BASE_DEC, NULL, 0x0,
603 "Current Number of Players", HFILL }},
604 { &hf_quake_CCREP_SERVER_INFO_max_player,
605 { "Maximal Number of Players", "quake.control.server_info.max_player",
606 FT_UINT8, BASE_DEC, NULL, 0x0,
607 "Maximal Number of Players", HFILL }},
608 { &hf_quake_CCREP_PLAYER_INFO_name,
609 { "Name", "quake.control.player_info.name",
610 FT_STRINGZ, BASE_NONE, NULL, 0x0,
611 "Player Name", HFILL }},
612 { &hf_quake_CCREP_PLAYER_INFO_colors,
613 { "Colors", "quake.control.player_info.colors",
614 FT_UINT32, BASE_HEX, NULL, 0x0,
615 "Player Colors", HFILL }},
616 { &hf_quake_CCREP_PLAYER_INFO_colors_shirt,
617 { "Shirt", "quake.control.player_info.colors.shirt",
618 FT_UINT8, BASE_DEC, VALS(names_colors), 0x0,
619 "Shirt Color", HFILL }},
620 { &hf_quake_CCREP_PLAYER_INFO_colors_pants,
621 { "Pants", "quake.control.player_info.colors.pants",
622 FT_UINT8, BASE_DEC, VALS(names_colors), 0x0,
623 "Pants Color", HFILL }},
624 { &hf_quake_CCREP_PLAYER_INFO_frags,
625 { "Frags", "quake.control.player_info.frags",
626 FT_UINT32, BASE_DEC, NULL, 0x0,
627 "Player Frags", HFILL }},
628 { &hf_quake_CCREP_PLAYER_INFO_connect_time,
629 { "Connect Time", "quake.control.player_info.connect_time",
630 FT_UINT32, BASE_DEC, NULL, 0x0,
631 "Player Connect Time", HFILL }},
632 { &hf_quake_CCREP_PLAYER_INFO_address,
633 { "Address", "quake.control.player_info.address",
634 FT_STRINGZ, BASE_NONE, NULL, 0x0,
635 "Player Address", HFILL }},
636 { &hf_quake_CCREP_RULE_INFO_rule,
637 { "Rule", "quake.control.rule_info.rule",
638 FT_STRINGZ, BASE_NONE, NULL, 0x0,
639 "Rule Name", HFILL }},
640 { &hf_quake_CCREP_RULE_INFO_value,
641 { "Value", "quake.control.rule_info.value",
642 FT_STRINGZ, BASE_NONE, NULL, 0x0,
643 "Rule Value", HFILL }},
645 static gint *ett[] = {
648 &ett_quake_control_colors,
651 module_t *quake_module;
653 proto_quake = proto_register_protocol("Quake Network Protocol",
655 proto_register_field_array(proto_quake, hf, array_length(hf));
656 proto_register_subtree_array(ett, array_length(ett));
658 quake_handle = create_dissector_handle(dissect_quake, proto_quake);
660 /* Register a configuration option for port */
661 quake_module = prefs_register_protocol(proto_quake,
662 proto_reg_handoff_quake);
663 prefs_register_uint_preference(quake_module, "udp.port",
664 "Quake Server UDP Port",
665 "Set the UDP port for the Quake Server",
666 10, &gbl_quakeServerPort);