Squelch an MSVC++ complaint.
[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: packet-pop.c,v 1.25 2001/06/18 02:17:50 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
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 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "strutil.h"
46
47 static int proto_pop = -1;
48 static int hf_pop_response = -1;
49 static int hf_pop_request = -1;
50
51 static gint ett_pop = -1;
52
53 #define TCP_PORT_POP                    110
54
55 static gboolean response_is_continuation(const u_char *data);
56         
57 static void
58 dissect_pop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
59 {
60         gboolean        is_request;
61         gboolean        is_continuation;
62         proto_tree      *pop_tree;
63         proto_item      *ti;
64         gint            offset = 0;
65         const u_char    *line;
66         gint            next_offset;
67         int             linelen;
68         int             tokenlen;
69         const u_char    *next_token;
70
71         if (check_col(pinfo->fd, COL_PROTOCOL))
72                 col_set_str(pinfo->fd, COL_PROTOCOL, "POP");
73
74         /*
75          * Find the end of the first line.
76          *
77          * Note that "tvb_find_line_end()" will return a value that is
78          * not longer than what's in the buffer, so the "tvb_get_ptr()"
79          * call won't throw an exception.
80          */
81         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
82         line = tvb_get_ptr(tvb, offset, linelen);
83
84         if (pinfo->match_port == pinfo->destport) {
85                 is_request = TRUE;
86                 is_continuation = FALSE;
87         } else {
88                 is_request = FALSE;
89                 is_continuation = response_is_continuation(line);
90         }
91
92         if (check_col(pinfo->fd, COL_INFO)) {
93                 /*
94                  * Put the first line from the buffer into the summary
95                  * if it's a POP request or reply (but leave out the
96                  * line terminator).
97                  * Otherwise, just call it a continuation.
98                  */
99                 if (is_continuation)
100                         col_set_str(pinfo->fd, COL_INFO, "Continuation");
101                 else
102                         col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
103                             is_request ? "Request" : "Response",
104                             format_text(line, linelen));
105         }
106
107         if (tree) {
108                 ti = proto_tree_add_item(tree, proto_pop, tvb, offset,
109                     tvb_length_remaining(tvb, offset), FALSE);
110                 pop_tree = proto_item_add_subtree(ti, ett_pop);
111
112                 if (is_continuation) {
113                         /*
114                          * Put the whole packet into the tree as data.
115                          */
116                         dissect_data(tvb, 0, pinfo, pop_tree);
117                         return;
118                 }
119
120                 if (is_request) {
121                         proto_tree_add_boolean_hidden(pop_tree,
122                             hf_pop_request, tvb, 0, 0, TRUE);
123                 } else {
124                         proto_tree_add_boolean_hidden(pop_tree,
125                             hf_pop_response, tvb, 0, 0, TRUE);
126                 }
127
128                 /*
129                  * Extract the first token, and, if there is a first
130                  * token, add it as the request or reply code.
131                  */
132                 tokenlen = get_token_len(line, line + linelen, &next_token);
133                 if (tokenlen != 0) {
134                         if (is_request) {
135                                 proto_tree_add_text(pop_tree, tvb, offset,
136                                     tokenlen, "Request: %s",
137                                     format_text(line, tokenlen));
138                         } else {
139                                 proto_tree_add_text(pop_tree, tvb, offset,
140                                     tokenlen, "Response: %s",
141                                     format_text(line, tokenlen));
142                         }
143                         offset += next_token - line;
144                         linelen -= next_token - line;
145                         line = next_token;
146                 }
147
148                 /*
149                  * Add the rest of the first line as request or
150                  * reply data.
151                  */
152                 if (linelen != 0) {
153                         if (is_request) {
154                                 proto_tree_add_text(pop_tree, tvb, offset,
155                                     linelen, "Request Arg: %s",
156                                     format_text(line, linelen));
157                         } else {
158                                 proto_tree_add_text(pop_tree, tvb, offset,
159                                     linelen, "Response Arg: %s",
160                                     format_text(line, linelen));
161                         }
162                 }
163                 offset = next_offset;
164
165                 /*
166                  * Show the rest of the request or response as text,
167                  * a line at a time.
168                  */
169                 while (tvb_offset_exists(tvb, offset)) {
170                         /*
171                          * Find the end of the line.
172                          */
173                         linelen = tvb_find_line_end(tvb, offset, -1,
174                             &next_offset);
175
176                         /*
177                          * Put this line.
178                          */
179                         proto_tree_add_text(pop_tree, tvb, offset,
180                             next_offset - offset, "%s",
181                             tvb_format_text(tvb, offset, next_offset - offset));
182                         offset = next_offset;
183                 }
184         }
185 }
186
187 static gboolean response_is_continuation(const u_char *data)
188 {
189   if (strncmp(data, "+OK", strlen("+OK")) == 0)
190     return FALSE;
191
192   if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
193     return FALSE;
194
195   return TRUE;
196 }
197
198 void
199 proto_register_pop(void)
200 {
201
202   static hf_register_info hf[] = {
203     { &hf_pop_response,
204       { "Response",           "pop.response",
205         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
206         "TRUE if POP response", HFILL }},
207
208     { &hf_pop_request,
209       { "Request",            "pop.request",
210         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
211         "TRUE if POP request", HFILL }}
212   };
213   static gint *ett[] = {
214     &ett_pop,
215   };
216
217   proto_pop = proto_register_protocol("Post Office Protocol", "POP", "pop");
218   proto_register_field_array(proto_pop, hf, array_length(hf));
219   proto_register_subtree_array(ett, array_length(ett));
220 }
221
222 void
223 proto_reg_handoff_pop(void)
224 {
225   dissector_add("tcp.port", TCP_PORT_POP, dissect_pop, proto_pop);
226 }