2 * Routines for quake packet dissection
4 * Uwe Girlich <uwe@planetquake.com>
5 * http://www.idsoftware.com/q1source/q1source.zip
7 * $Id: packet-quake.c,v 1.3 2000/08/07 03:21:02 guy Exp $
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@zing.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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
44 #include "conversation.h"
46 static int proto_quake = -1;
47 static int hf_quake_header_flags = -1;
48 static int hf_quake_header_length = -1;
49 static int hf_quake_header_sequence = -1;
50 static int hf_quake_control_command = -1;
52 static int hf_quake_CCREQ_CONNECT_game = -1;
53 static int hf_quake_CCREQ_CONNECT_version = -1;
54 static int hf_quake_CCREQ_SERVER_INFO_game = -1;
55 static int hf_quake_CCREQ_SERVER_INFO_version = -1;
56 static int hf_quake_CCREQ_PLAYER_INFO_player = -1;
57 static int hf_quake_CCREQ_RULE_INFO_lastrule = -1;
59 static int hf_quake_CCREP_ACCEPT_port = -1;
60 static int hf_quake_CCREP_REJECT_reason = -1;
61 static int hf_quake_CCREP_SERVER_INFO_address = -1;
62 static int hf_quake_CCREP_SERVER_INFO_server = -1;
63 static int hf_quake_CCREP_SERVER_INFO_map = -1;
64 static int hf_quake_CCREP_SERVER_INFO_num_player = -1;
65 static int hf_quake_CCREP_SERVER_INFO_max_player = -1;
66 static int hf_quake_CCREP_PLAYER_INFO_name = -1;
67 static int hf_quake_CCREP_PLAYER_INFO_colors = -1;
68 static int hf_quake_CCREP_PLAYER_INFO_colors_shirt = -1;
69 static int hf_quake_CCREP_PLAYER_INFO_colors_pants = -1;
70 static int hf_quake_CCREP_PLAYER_INFO_frags = -1;
71 static int hf_quake_CCREP_PLAYER_INFO_connect_time = -1;
72 static int hf_quake_CCREP_PLAYER_INFO_address = -1;
73 static int hf_quake_CCREP_RULE_INFO_rule = -1;
74 static int hf_quake_CCREP_RULE_INFO_value = -1;
77 static gint ett_quake = -1;
78 static gint ett_quake_control = -1;
79 static gint ett_quake_control_colors = -1;
80 static gint ett_quake_flags = -1;
83 /* I took these names directly out of the Q1 source. */
84 #define NETFLAG_LENGTH_MASK 0x0000ffff
85 #define NET_HEADERSIZE 8
86 #define DEFAULTnet_hostport 26000
88 #define NETFLAG_LENGTH_MASK 0x0000ffff
89 #define NETFLAG_DATA 0x00010000
90 #define NETFLAG_ACK 0x00020000
91 #define NETFLAG_NAK 0x00040000
92 #define NETFLAG_EOM 0x00080000
93 #define NETFLAG_UNRELIABLE 0x00100000
94 #define NETFLAG_CTL 0x80000000
97 #define CCREQ_CONNECT 0x01
98 #define CCREQ_SERVER_INFO 0x02
99 #define CCREQ_PLAYER_INFO 0x03
100 #define CCREQ_RULE_INFO 0x04
102 #define CCREP_ACCEPT 0x81
103 #define CCREP_REJECT 0x82
104 #define CCREP_SERVER_INFO 0x83
105 #define CCREP_PLAYER_INFO 0x84
106 #define CCREP_RULE_INFO 0x85
108 static const value_string names_control_command[] = {
109 { CCREQ_CONNECT, "connect" },
110 { CCREQ_SERVER_INFO, "server_info" },
111 { CCREQ_PLAYER_INFO, "player_info" },
112 { CCREQ_RULE_INFO, "rule_info" },
113 { CCREP_ACCEPT, "accept" },
114 { CCREP_REJECT, "reject" },
115 { CCREP_SERVER_INFO, "server_info" },
116 { CCREP_PLAYER_INFO, "player_info" },
117 { CCREP_RULE_INFO, "rule_info" },
124 #define QUAKE_MAXSTRING 0x800
126 static const value_string names_control_direction[] = {
127 { CCREQ, "Request" },
133 static const value_string names_colors[] = {
154 static void dissect_quake(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
158 tvb_get_stringz(tvbuff_t *tvb, gint offset, gint maxlength, guint8* buffer)
161 char* zeropos = NULL;
164 if (maxlength == 0) {
169 tvb_memcpy(tvb, buffer, offset, maxlength);
170 for (i=0 ; i<maxlength ; i++) {
171 if (buffer[i] == 0) {
177 if (zeropos == NULL) {
178 buffer[maxlength-1] = 0;
186 dissect_quake_CCREQ_CONNECT
187 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
190 char game[QUAKE_MAXSTRING];
194 maxbufsize = MIN(sizeof(game), tvb_length(tvb));
195 len = tvb_get_stringz(tvb, 0, maxbufsize, game);
196 version = tvb_get_guint8(tvb, len + 1);
199 proto_tree_add_string(tree, hf_quake_CCREQ_CONNECT_game,
200 tvb, 0, len + 1, game);
201 proto_tree_add_uint(tree, hf_quake_CCREQ_CONNECT_version,
202 tvb, len + 1, 1, version);
208 dissect_quake_CCREQ_SERVER_INFO
209 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
212 char game[QUAKE_MAXSTRING];
216 maxbufsize = MIN(sizeof(game), tvb_length(tvb));
217 len = tvb_get_stringz(tvb, 0, maxbufsize, game);
218 version = tvb_get_guint8(tvb, len + 1);
221 proto_tree_add_string(tree, hf_quake_CCREQ_SERVER_INFO_game,
222 tvb, 0, len + 1, game);
223 proto_tree_add_uint(tree, hf_quake_CCREQ_SERVER_INFO_version,
224 tvb, len + 1, 1, version);
230 dissect_quake_CCREQ_PLAYER_INFO
231 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
235 player = tvb_get_guint8(tvb, 0);
237 proto_tree_add_uint(tree, hf_quake_CCREQ_PLAYER_INFO_player,
244 dissect_quake_CCREQ_RULE_INFO
245 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
247 char rule[QUAKE_MAXSTRING];
251 maxbufsize = MIN(sizeof(rule), tvb_length(tvb));
252 len = tvb_get_stringz(tvb, 0, maxbufsize, rule);
254 proto_tree_add_string(tree, hf_quake_CCREQ_RULE_INFO_lastrule,
255 tvb, 0, len + 1, rule);
261 dissect_quake_CCREP_ACCEPT
262 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
267 port = tvb_get_letohl(tvb, 0);
268 c = conversation_new( &pi.src, &pi.dst, PT_UDP, port, pi.destport, NULL);
270 c->is_old_dissector = FALSE;
271 c->dissector.new = dissect_quake;
274 proto_tree_add_uint(tree, hf_quake_CCREP_ACCEPT_port,
281 dissect_quake_CCREP_REJECT
282 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
285 char reason[QUAKE_MAXSTRING];
288 maxbufsize = MIN(sizeof(reason), tvb_length(tvb));
289 len = tvb_get_stringz(tvb, 0, maxbufsize, reason);
292 proto_tree_add_string(tree, hf_quake_CCREP_REJECT_reason,
293 tvb, 0, len + 1, reason);
299 dissect_quake_CCREP_SERVER_INFO
300 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
305 char address[QUAKE_MAXSTRING];
306 char server[QUAKE_MAXSTRING];
307 char map[QUAKE_MAXSTRING];
315 maxbufsize = MIN(sizeof(address), tvb_length_remaining(tvb, offset));
316 len = tvb_get_stringz(tvb, offset, maxbufsize, address);
318 proto_tree_add_string(tree, hf_quake_CCREP_SERVER_INFO_address,
319 tvb, offset, len + 1, address);
323 maxbufsize = MIN(sizeof(server), tvb_length_remaining(tvb, offset));
324 len = tvb_get_stringz(tvb, offset, maxbufsize, server);
326 proto_tree_add_string(tree, hf_quake_CCREP_SERVER_INFO_server,
327 tvb, offset, len + 1, server);
331 maxbufsize = MIN(sizeof(map), tvb_length_remaining(tvb, offset));
332 len = tvb_get_stringz(tvb, offset, maxbufsize, map);
334 proto_tree_add_string(tree, hf_quake_CCREP_SERVER_INFO_map,
335 tvb, offset, len + 1, map);
339 num_player = tvb_get_guint8(tvb, offset + 0);
340 max_player = tvb_get_guint8(tvb, offset + 1);
341 version = tvb_get_guint8(tvb, offset + 2);
344 proto_tree_add_uint(tree, hf_quake_CCREP_SERVER_INFO_num_player,
345 tvb, offset + 0, 1, num_player);
346 proto_tree_add_uint(tree, hf_quake_CCREP_SERVER_INFO_max_player,
347 tvb, offset + 1, 1, max_player);
348 proto_tree_add_uint(tree, hf_quake_CCREQ_SERVER_INFO_version,
349 tvb, offset + 2, 1, version);
355 dissect_quake_CCREP_PLAYER_INFO
356 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
362 char name[QUAKE_MAXSTRING];
367 guint32 connect_time;
368 char address[QUAKE_MAXSTRING];
372 player = tvb_get_guint8(tvb, offset);
374 proto_tree_add_uint(tree, hf_quake_CCREQ_PLAYER_INFO_player,
375 tvb, offset, 1, player);
379 maxbufsize = MIN(sizeof(name), tvb_length_remaining(tvb, offset));
380 len = tvb_get_stringz(tvb, offset, maxbufsize, name);
382 proto_tree_add_string(tree, hf_quake_CCREP_PLAYER_INFO_name,
383 tvb, offset, len + 1, name);
387 colors = tvb_get_letohl(tvb, offset + 0);
388 color_shirt = (colors >> 4) & 0x0f;
389 color_pants = (colors ) & 0x0f;
390 frags = tvb_get_letohl(tvb, offset + 4);
391 connect_time = tvb_get_letohl(tvb, offset + 8);
393 proto_item *colors_item;
394 proto_tree *colors_tree;
396 colors_item = proto_tree_add_uint(tree,
397 hf_quake_CCREP_PLAYER_INFO_colors,
398 tvb, offset + 0, 4, colors);
400 colors_tree = proto_item_add_subtree(colors_item,
401 ett_quake_control_colors);
402 proto_tree_add_uint(colors_tree,
403 hf_quake_CCREP_PLAYER_INFO_colors_shirt,
404 tvb, offset + 0, 1, color_shirt);
405 proto_tree_add_uint(colors_tree,
406 hf_quake_CCREP_PLAYER_INFO_colors_pants,
407 tvb, offset + 0, 1, color_pants);
409 proto_tree_add_uint(tree, hf_quake_CCREP_PLAYER_INFO_frags,
410 tvb, offset + 4, 4, frags);
411 proto_tree_add_uint(tree, hf_quake_CCREP_PLAYER_INFO_connect_time,
412 tvb, offset + 8, 4, connect_time);
416 maxbufsize = MIN(sizeof(address), tvb_length_remaining(tvb, offset));
417 len = tvb_get_stringz(tvb, offset, maxbufsize, address);
419 proto_tree_add_string(tree, hf_quake_CCREP_PLAYER_INFO_address,
420 tvb, offset, len + 1, address);
427 dissect_quake_CCREP_RULE_INFO
428 (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
430 char rule[QUAKE_MAXSTRING];
431 char value[QUAKE_MAXSTRING];
436 if (tvb_length(tvb) == 0) return;
440 maxbufsize = MIN(sizeof(rule), tvb_length_remaining(tvb, offset));
441 len = tvb_get_stringz(tvb, offset, maxbufsize, rule);
443 proto_tree_add_string(tree, hf_quake_CCREP_RULE_INFO_rule,
444 tvb, offset, len + 1, rule);
448 maxbufsize = MIN(sizeof(value), tvb_length_remaining(tvb, offset));
449 len = tvb_get_stringz(tvb, offset, maxbufsize, value);
451 proto_tree_add_string(tree, hf_quake_CCREP_RULE_INFO_value,
452 tvb, offset, len + 1, value);
459 dissect_quake_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
463 proto_item *control_item = NULL;
464 proto_tree *control_tree = NULL;
468 command = tvb_get_guint8(tvb, 0);
469 direction = (command & 0x80) ? CCREP : CCREQ;
471 if (check_col(pinfo->fd, COL_INFO)) {
472 col_add_fstr(pinfo->fd, COL_INFO, "%s %s",
473 val_to_str(command,names_control_command, "%u"),
474 val_to_str(direction,names_control_direction,"%u"));
478 control_item = proto_tree_add_text(tree, tvb,
479 0, tvb_length(tvb), "Control %s: %s",
480 val_to_str(direction, names_control_direction, "%u"),
481 val_to_str(command, names_control_command, "%u"));
483 control_tree = proto_item_add_subtree(control_item,
485 proto_tree_add_uint(control_tree, hf_quake_control_command,
489 rest_length = tvb_reported_length(tvb) - 1;
490 next_tvb = tvb_new_subset(tvb, 1, rest_length , rest_length);
493 dissect_quake_CCREQ_CONNECT
494 (next_tvb, pinfo, control_tree);
496 case CCREQ_SERVER_INFO:
497 dissect_quake_CCREQ_SERVER_INFO
498 (next_tvb, pinfo, control_tree);
500 case CCREQ_PLAYER_INFO:
501 dissect_quake_CCREQ_PLAYER_INFO
502 (next_tvb, pinfo, control_tree);
504 case CCREQ_RULE_INFO:
505 dissect_quake_CCREQ_RULE_INFO
506 (next_tvb, pinfo, control_tree);
509 dissect_quake_CCREP_ACCEPT
510 (next_tvb, pinfo, control_tree);
513 dissect_quake_CCREP_REJECT
514 (next_tvb, pinfo, control_tree);
516 case CCREP_SERVER_INFO:
517 dissect_quake_CCREP_SERVER_INFO
518 (next_tvb, pinfo, control_tree);
520 case CCREP_PLAYER_INFO:
521 dissect_quake_CCREP_PLAYER_INFO
522 (next_tvb, pinfo, control_tree);
524 case CCREP_RULE_INFO:
525 dissect_quake_CCREP_RULE_INFO
526 (next_tvb, pinfo, control_tree);
529 dissect_data(next_tvb, pinfo, control_tree);
536 dissect_quake(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
538 proto_tree *quake_tree = NULL;
539 proto_item *quake_item = NULL;
542 guint32 sequence = 0;
546 if (!tvb_bytes_exist(tvb, 0, 4)) return;
548 length = tvb_get_ntohl(tvb, 0);
549 flags = length & (~NETFLAG_LENGTH_MASK);
550 length &= NETFLAG_LENGTH_MASK;
552 if (check_col(pinfo->fd, COL_PROTOCOL))
553 col_add_str(pinfo->fd, COL_PROTOCOL, "QUAKE");
556 quake_item = proto_tree_add_item(tree, proto_quake,
557 tvb, 0, tvb_length(tvb), FALSE);
559 quake_tree = proto_item_add_subtree(quake_item, ett_quake);
563 proto_item* flags_item = NULL;
564 proto_tree* flags_tree = NULL;
566 flags_item = proto_tree_add_uint(quake_tree, hf_quake_header_flags,
569 flags_tree = proto_item_add_subtree(flags_item, ett_quake_flags);
573 proto_tree_add_text(flags_tree, tvb, 0, 2,
574 decode_boolean_bitfield(flags, NETFLAG_DATA, 32,
576 proto_tree_add_text(flags_tree, tvb, 0, 2,
577 decode_boolean_bitfield(flags, NETFLAG_ACK, 32,
578 "Acknowledgment","-"));
579 proto_tree_add_text(flags_tree, tvb, 0, 2,
580 decode_boolean_bitfield(flags, NETFLAG_NAK, 32,
581 "No Acknowledgment","-"));
582 proto_tree_add_text(flags_tree, tvb, 0, 2,
583 decode_boolean_bitfield(flags, NETFLAG_EOM, 32,
584 "End Of Message","-"));
585 proto_tree_add_text(flags_tree, tvb, 0, 2,
586 decode_boolean_bitfield(flags, NETFLAG_UNRELIABLE, 32,
588 proto_tree_add_text(flags_tree, tvb, 0, 2,
589 decode_boolean_bitfield(flags, NETFLAG_CTL, 32,
592 proto_tree_add_uint(quake_tree, hf_quake_header_length,
596 if (flags == NETFLAG_CTL) {
597 rest_length = tvb_reported_length(tvb) - 4;
598 next_tvb = tvb_new_subset(tvb, 4, rest_length , rest_length);
599 dissect_quake_control(next_tvb, pinfo, quake_tree);
603 sequence = tvb_get_ntohl(tvb, 4);
604 if (check_col(pinfo->fd, COL_INFO)) {
605 col_add_fstr(pinfo->fd, COL_INFO, "seq 0x%x", sequence);
608 proto_tree_add_uint(quake_tree, hf_quake_header_sequence,
609 tvb, 4, 4, sequence);
612 rest_length = tvb_reported_length(tvb) - 8;
613 next_tvb = tvb_new_subset(tvb, 8, rest_length , rest_length);
614 dissect_data(next_tvb, pinfo, quake_tree);
618 proto_register_quake(void)
621 static hf_register_info hf[] = {
622 { &hf_quake_header_flags,
623 { "Flags", "quake.header.flags",
624 FT_UINT16, BASE_HEX, NULL, 0x0,
626 { &hf_quake_header_length,
627 { "Length", "quake.header.length",
628 FT_UINT16, BASE_DEC, NULL, 0x0,
629 "full data length" }},
630 { &hf_quake_header_sequence,
631 { "Sequence", "quake.header.sequence",
632 FT_UINT32, BASE_HEX, NULL, 0x0,
633 "Sequence Number" }},
634 { &hf_quake_control_command,
635 { "Command", "quake.control.command",
636 FT_UINT8, BASE_HEX, VALS(names_control_command), 0x0,
637 "Control Command" }},
638 { &hf_quake_CCREQ_CONNECT_game,
639 { "Game", "quake.control.connect.game",
640 FT_STRING, BASE_DEC, NULL, 0x0,
642 { &hf_quake_CCREQ_CONNECT_version,
643 { "Version", "quake.control.connect.version",
644 FT_UINT8, BASE_DEC, NULL, 0x0,
645 "Game Protocol Version Number" }},
646 { &hf_quake_CCREQ_SERVER_INFO_game,
647 { "Game", "quake.control.server_info.game",
648 FT_STRING, BASE_DEC, NULL, 0x0,
650 { &hf_quake_CCREQ_SERVER_INFO_version,
651 { "Version", "quake.control.server_info.version",
652 FT_UINT8, BASE_DEC, NULL, 0x0,
653 "Game Protocol Version Number" }},
654 { &hf_quake_CCREQ_PLAYER_INFO_player,
655 { "Player", "quake.control.player_info.player",
656 FT_UINT8, BASE_DEC, NULL, 0x0,
658 { &hf_quake_CCREQ_RULE_INFO_lastrule,
659 { "Last Rule", "quake.control.rule_info.lastrule",
660 FT_STRING, BASE_DEC, NULL, 0x0,
662 { &hf_quake_CCREP_ACCEPT_port,
663 { "Port", "quake.control.accept.port",
664 FT_UINT32, BASE_DEC, NULL, 0x0,
666 { &hf_quake_CCREP_REJECT_reason,
667 { "Reason", "quake.control.reject.reason",
668 FT_STRING, BASE_DEC, NULL, 0x0,
670 { &hf_quake_CCREP_SERVER_INFO_address,
671 { "Address", "quake.control.server_info.address",
672 FT_STRING, BASE_DEC, NULL, 0x0,
674 { &hf_quake_CCREP_SERVER_INFO_server,
675 { "Server", "quake.control.server_info.server",
676 FT_STRING, BASE_DEC, NULL, 0x0,
678 { &hf_quake_CCREP_SERVER_INFO_map,
679 { "Map", "quake.control.server_info.map",
680 FT_STRING, BASE_DEC, NULL, 0x0,
682 { &hf_quake_CCREP_SERVER_INFO_num_player,
683 { "Number of Players", "quake.control.server_info.num_player",
684 FT_UINT8, BASE_DEC, NULL, 0x0,
685 "Current Number of Players" }},
686 { &hf_quake_CCREP_SERVER_INFO_max_player,
687 { "Maximal Number of Players", "quake.control.server_info.max_player",
688 FT_UINT8, BASE_DEC, NULL, 0x0,
689 "Maximal Number of Players" }},
690 { &hf_quake_CCREP_PLAYER_INFO_name,
691 { "Name", "quake.control.player_info.name",
692 FT_STRING, BASE_DEC, NULL, 0x0,
694 { &hf_quake_CCREP_PLAYER_INFO_colors,
695 { "Colors", "quake.control.player_info.colors",
696 FT_UINT32, BASE_HEX, NULL, 0x0,
698 { &hf_quake_CCREP_PLAYER_INFO_colors_shirt,
699 { "Shirt", "quake.control.player_info.colors.shirt",
700 FT_UINT8, BASE_DEC, VALS(names_colors), 0x0,
702 { &hf_quake_CCREP_PLAYER_INFO_colors_pants,
703 { "Pants", "quake.control.player_info.colors.pants",
704 FT_UINT8, BASE_DEC, VALS(names_colors), 0x0,
706 { &hf_quake_CCREP_PLAYER_INFO_frags,
707 { "Frags", "quake.control.player_info.frags",
708 FT_UINT32, BASE_DEC, NULL, 0x0,
710 { &hf_quake_CCREP_PLAYER_INFO_connect_time,
711 { "Connect Time", "quake.control.player_info.connect_time",
712 FT_UINT32, BASE_DEC, NULL, 0x0,
713 "Player Connect Time" }},
714 { &hf_quake_CCREP_PLAYER_INFO_address,
715 { "Address", "quake.control.player_info.address",
716 FT_STRING, BASE_DEC, NULL, 0x0,
718 { &hf_quake_CCREP_RULE_INFO_rule,
719 { "Rule", "quake.control.rule_info.rule",
720 FT_STRING, BASE_DEC, NULL, 0x0,
722 { &hf_quake_CCREP_RULE_INFO_value,
723 { "Value", "quake.control.rule_info.value",
724 FT_STRING, BASE_DEC, NULL, 0x0,
727 static gint *ett[] = {
730 &ett_quake_control_colors,
734 proto_quake = proto_register_protocol("Quake Network Protocol", "quake");
735 proto_register_field_array(proto_quake, hf, array_length(hf));
736 proto_register_subtree_array(ett, array_length(ett));
741 proto_reg_handoff_quake(void)
743 dissector_add("udp.port", DEFAULTnet_hostport, dissect_quake);