As the gtk2 directory is no longer needed (GTK1 and 2 are using the same sources...
[obnox/wireshark/wip.git] / packet-pop.c
1 /* packet-pop.c
2  * Routines for pop packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-tftp.c
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33
34 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 static int proto_pop = -1;
40 static int hf_pop_response = -1;
41 static int hf_pop_request = -1;
42
43 static gint ett_pop = -1;
44 static gint ett_pop_reqresp = -1;
45
46 static dissector_handle_t data_handle;
47
48 #define TCP_PORT_POP                    110
49
50 static gboolean response_is_continuation(const guchar *data);
51
52 static void
53 dissect_pop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
54 {
55         gboolean        is_request;
56         gboolean        is_continuation;
57         proto_tree      *pop_tree, *reqresp_tree;
58         proto_item      *ti;
59         gint            offset = 0;
60         const guchar    *line;
61         gint            next_offset;
62         int             linelen;
63         int             tokenlen;
64         const guchar    *next_token;
65
66         if (check_col(pinfo->cinfo, COL_PROTOCOL))
67                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "POP");
68
69         /*
70          * Find the end of the first line.
71          *
72          * Note that "tvb_find_line_end()" will return a value that is
73          * not longer than what's in the buffer, so the "tvb_get_ptr()"
74          * call won't throw an exception.
75          */
76         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
77         line = tvb_get_ptr(tvb, offset, linelen);
78
79         if (pinfo->match_port == pinfo->destport) {
80                 is_request = TRUE;
81                 is_continuation = FALSE;
82         } else {
83                 is_request = FALSE;
84                 is_continuation = response_is_continuation(line);
85         }
86
87         if (check_col(pinfo->cinfo, COL_INFO)) {
88                 /*
89                  * Put the first line from the buffer into the summary
90                  * if it's a POP request or reply (but leave out the
91                  * line terminator).
92                  * Otherwise, just call it a continuation.
93                  */
94                 if (is_continuation)
95                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
96                 else
97                         col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
98                             is_request ? "Request" : "Response",
99                             format_text(line, linelen));
100         }
101
102         if (tree) {
103                 ti = proto_tree_add_item(tree, proto_pop, tvb, offset, -1,
104                     FALSE);
105                 pop_tree = proto_item_add_subtree(ti, ett_pop);
106
107                 if (is_continuation) {
108                         /*
109                          * Put the whole packet into the tree as data.
110                          */
111                         call_dissector(data_handle,tvb, pinfo, pop_tree);
112                         return;
113                 }
114
115                 if (is_request) {
116                         proto_tree_add_boolean_hidden(pop_tree,
117                             hf_pop_request, tvb, 0, 0, TRUE);
118                 } else {
119                         proto_tree_add_boolean_hidden(pop_tree,
120                             hf_pop_response, tvb, 0, 0, TRUE);
121                 }
122
123                 /*
124                  * Put the line into the protocol tree.
125                  */
126                 ti = proto_tree_add_text(pop_tree, tvb, offset,
127                     next_offset - offset, "%s",
128                     tvb_format_text(tvb, offset, next_offset - offset));
129                 reqresp_tree = proto_item_add_subtree(ti, ett_pop_reqresp);
130
131                 /*
132                  * Extract the first token, and, if there is a first
133                  * token, add it as the request or reply code.
134                  */
135                 tokenlen = get_token_len(line, line + linelen, &next_token);
136                 if (tokenlen != 0) {
137                         if (is_request) {
138                                 proto_tree_add_text(reqresp_tree, tvb, offset,
139                                     tokenlen, "Request: %s",
140                                     format_text(line, tokenlen));
141                         } else {
142                                 proto_tree_add_text(reqresp_tree, tvb, offset,
143                                     tokenlen, "Response: %s",
144                                     format_text(line, tokenlen));
145                         }
146                         offset += next_token - line;
147                         linelen -= next_token - line;
148                         line = next_token;
149                 }
150
151                 /*
152                  * Add the rest of the first line as request or
153                  * reply data.
154                  */
155                 if (linelen != 0) {
156                         if (is_request) {
157                                 proto_tree_add_text(reqresp_tree, tvb, offset,
158                                     linelen, "Request Arg: %s",
159                                     format_text(line, linelen));
160                         } else {
161                                 proto_tree_add_text(reqresp_tree, tvb, offset,
162                                     linelen, "Response Arg: %s",
163                                     format_text(line, linelen));
164                         }
165                 }
166                 offset = next_offset;
167
168                 /*
169                  * Show the rest of the request or response as text,
170                  * a line at a time.
171                  */
172                 while (tvb_offset_exists(tvb, offset)) {
173                         /*
174                          * Find the end of the line.
175                          */
176                         linelen = tvb_find_line_end(tvb, offset, -1,
177                             &next_offset, FALSE);
178
179                         /*
180                          * Put this line.
181                          */
182                         proto_tree_add_text(pop_tree, tvb, offset,
183                             next_offset - offset, "%s",
184                             tvb_format_text(tvb, offset, next_offset - offset));
185                         offset = next_offset;
186                 }
187         }
188 }
189
190 static gboolean response_is_continuation(const guchar *data)
191 {
192   if (strncmp(data, "+OK", strlen("+OK")) == 0)
193     return FALSE;
194
195   if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
196     return FALSE;
197
198   return TRUE;
199 }
200
201 void
202 proto_register_pop(void)
203 {
204
205   static hf_register_info hf[] = {
206     { &hf_pop_response,
207       { "Response",           "pop.response",
208         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
209         "TRUE if POP response", HFILL }},
210
211     { &hf_pop_request,
212       { "Request",            "pop.request",
213         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
214         "TRUE if POP request", HFILL }}
215   };
216   static gint *ett[] = {
217     &ett_pop,
218     &ett_pop_reqresp,
219   };
220
221   proto_pop = proto_register_protocol("Post Office Protocol", "POP", "pop");
222   proto_register_field_array(proto_pop, hf, array_length(hf));
223   proto_register_subtree_array(ett, array_length(ett));
224 }
225
226 void
227 proto_reg_handoff_pop(void)
228 {
229   dissector_handle_t pop_handle;
230
231   pop_handle = create_dissector_handle(dissect_pop, proto_pop);
232   dissector_add("tcp.port", TCP_PORT_POP, pop_handle);
233   data_handle = find_dissector("data");
234 }