Absolute and Relative times were swapped. Also add comment that there seems
[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.18 2002/01/24 09:20:48 guy 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 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include <epan/packet.h>
44
45 static int proto_irc = -1;
46 static int hf_irc_request = -1;
47 static int hf_irc_response = -1;
48 static int hf_irc_command = -1;
49
50 static gint ett_irc = -1;
51
52 #define TCP_PORT_IRC                    6667
53         /* good candidate for dynamic port specification */
54
55 static void
56 dissect_irc_request(proto_tree *tree, tvbuff_t *tvb, int offset, int len,
57     const char *line, int linelen)
58 {
59         proto_tree_add_boolean_hidden(tree, hf_irc_request, tvb, offset, len,
60             TRUE);
61         proto_tree_add_text(tree, tvb, offset, len, "Request Line: %.*s",
62             linelen, line);
63 }
64
65 static void
66 dissect_irc_response(proto_tree *tree, tvbuff_t *tvb, int offset, int len,
67     const char *line, int linelen)
68 {
69         proto_tree_add_boolean_hidden(tree, hf_irc_response, tvb, offset, len,
70             TRUE);
71         proto_tree_add_text(tree, tvb, offset, len, "Response Line: %.*s",
72             linelen, line);
73 }
74
75 static void
76 dissect_irc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
77 {
78         proto_tree      *irc_tree, *ti;
79         gint            offset = 0;
80         const u_char    *line;
81         gint            next_offset;
82         int             linelen;
83
84         if (check_col(pinfo->cinfo, COL_PROTOCOL))
85                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IRC");
86
87         if (check_col(pinfo->cinfo, COL_INFO))
88         {
89                 col_set_str(pinfo->cinfo, COL_INFO,
90                     (pinfo->match_port == pinfo->destport) ? "Request" : "Response");
91         }
92
93         if (tree)
94         {
95                 ti = proto_tree_add_item(tree, proto_irc, tvb, 0, -1, FALSE);
96                 irc_tree = proto_item_add_subtree(ti, ett_irc);
97
98                 /*
99                  * Process the packet data, a line at a time.
100                  */
101                 while (tvb_offset_exists(tvb, offset))
102                 {
103                         /*
104                          * Find the end of the line.
105                          */
106                         linelen = tvb_find_line_end(tvb, offset, -1,
107                             &next_offset);
108
109                         /*
110                          * Get a buffer that refers to the line (without
111                          * the line terminator).
112                          */
113                         line = tvb_get_ptr(tvb, offset, linelen);
114
115                         if (linelen != 0)
116                         {
117                                 if (pinfo->match_port == pinfo->destport)
118                                 {
119                                         dissect_irc_request(irc_tree, tvb,
120                                             offset, next_offset - offset,
121                                             line, linelen);
122                                 }
123                                 else
124                                 {
125                                         dissect_irc_response(irc_tree, tvb,
126                                             offset, next_offset - offset,
127                                             line, linelen);
128                                 }
129                         }
130                         offset = next_offset;
131                 }
132         }
133 }
134
135 void
136 proto_register_irc(void)
137 {
138         static hf_register_info hf[] = {
139           { &hf_irc_response,
140             { "Response",           "irc.response",
141               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
142               "TRUE if IRC response", HFILL }},
143           
144           { &hf_irc_request,
145             { "Request",            "irc.request",
146               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
147               "TRUE if IRC request", HFILL }},
148
149           { &hf_irc_command,
150             { "Command",            "irc.command",
151               FT_STRING, BASE_NONE, NULL, 0x0,
152               "Command associated with request", HFILL }}
153         };
154
155         static gint *ett[] = {
156                 &ett_irc,
157         };
158         proto_irc = proto_register_protocol("Internet Relay Chat", "IRC", "irc");
159         proto_register_field_array(proto_irc, hf, array_length(hf));
160         proto_register_subtree_array(ett, array_length(ett));
161 }
162
163 void
164 proto_reg_handoff_irc(void)
165 {
166         dissector_handle_t irc_handle;
167
168         irc_handle = create_dissector_handle(dissect_irc, proto_irc);
169         dissector_add("tcp.port", TCP_PORT_IRC, irc_handle);
170 }