From Olivier Biot: have a separate subtree ett_ value for concatenated
[obnox/wireshark/wip.git] / packet-irc.c
1 /* packet-irc.c
2  * Routines for IRC packet dissection
3  *
4  * $Id: packet-irc.c,v 1.21 2002/08/28 21:00:18 jmayer Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-tftp.c
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32
33 #include <string.h>
34 #include <glib.h>
35 #include <epan/packet.h>
36
37 static int proto_irc = -1;
38 static int hf_irc_request = -1;
39 static int hf_irc_response = -1;
40 static int hf_irc_command = -1;
41
42 static gint ett_irc = -1;
43
44 #define TCP_PORT_IRC                    6667
45         /* good candidate for dynamic port specification */
46
47 static void
48 dissect_irc_request(proto_tree *tree, tvbuff_t *tvb, int offset, int len,
49     const char *line, int linelen)
50 {
51         proto_tree_add_boolean_hidden(tree, hf_irc_request, tvb, offset, len,
52             TRUE);
53         proto_tree_add_text(tree, tvb, offset, len, "Request Line: %.*s",
54             linelen, line);
55 }
56
57 static void
58 dissect_irc_response(proto_tree *tree, tvbuff_t *tvb, int offset, int len,
59     const char *line, int linelen)
60 {
61         proto_tree_add_boolean_hidden(tree, hf_irc_response, tvb, offset, len,
62             TRUE);
63         proto_tree_add_text(tree, tvb, offset, len, "Response Line: %.*s",
64             linelen, line);
65 }
66
67 static void
68 dissect_irc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
69 {
70         proto_tree      *irc_tree, *ti;
71         gint            offset = 0;
72         const guchar    *line;
73         gint            next_offset;
74         int             linelen;
75
76         if (check_col(pinfo->cinfo, COL_PROTOCOL))
77                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IRC");
78
79         if (check_col(pinfo->cinfo, COL_INFO))
80         {
81                 col_set_str(pinfo->cinfo, COL_INFO,
82                     (pinfo->match_port == pinfo->destport) ? "Request" : "Response");
83         }
84
85         if (tree)
86         {
87                 ti = proto_tree_add_item(tree, proto_irc, tvb, 0, -1, FALSE);
88                 irc_tree = proto_item_add_subtree(ti, ett_irc);
89
90                 /*
91                  * Process the packet data, a line at a time.
92                  */
93                 while (tvb_offset_exists(tvb, offset))
94                 {
95                         /*
96                          * Find the end of the line.
97                          */
98                         linelen = tvb_find_line_end(tvb, offset, -1,
99                             &next_offset, FALSE);
100
101                         /*
102                          * Get a buffer that refers to the line (without
103                          * the line terminator).
104                          */
105                         line = tvb_get_ptr(tvb, offset, linelen);
106
107                         if (linelen != 0)
108                         {
109                                 if (pinfo->match_port == pinfo->destport)
110                                 {
111                                         dissect_irc_request(irc_tree, tvb,
112                                             offset, next_offset - offset,
113                                             line, linelen);
114                                 }
115                                 else
116                                 {
117                                         dissect_irc_response(irc_tree, tvb,
118                                             offset, next_offset - offset,
119                                             line, linelen);
120                                 }
121                         }
122                         offset = next_offset;
123                 }
124         }
125 }
126
127 void
128 proto_register_irc(void)
129 {
130         static hf_register_info hf[] = {
131           { &hf_irc_response,
132             { "Response",           "irc.response",
133               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
134               "TRUE if IRC response", HFILL }},
135
136           { &hf_irc_request,
137             { "Request",            "irc.request",
138               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
139               "TRUE if IRC request", HFILL }},
140
141           { &hf_irc_command,
142             { "Command",            "irc.command",
143               FT_STRING, BASE_NONE, NULL, 0x0,
144               "Command associated with request", HFILL }}
145         };
146
147         static gint *ett[] = {
148                 &ett_irc,
149         };
150         proto_irc = proto_register_protocol("Internet Relay Chat", "IRC", "irc");
151         proto_register_field_array(proto_irc, hf, array_length(hf));
152         proto_register_subtree_array(ett, array_length(ett));
153 }
154
155 void
156 proto_reg_handoff_irc(void)
157 {
158         dissector_handle_t irc_handle;
159
160         irc_handle = create_dissector_handle(dissect_irc, proto_irc);
161         dissector_add("tcp.port", TCP_PORT_IRC, irc_handle);
162 }