2 * Routines for pop packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-pop.c,v 1.34 2003/06/11 20:03:09 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-tftp.c
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.
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.
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.
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
39 static int proto_pop = -1;
40 static int hf_pop_response = -1;
41 static int hf_pop_request = -1;
43 static gint ett_pop = -1;
44 static gint ett_pop_reqresp = -1;
46 static dissector_handle_t data_handle;
48 #define TCP_PORT_POP 110
50 static gboolean response_is_continuation(const guchar *data);
53 dissect_pop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
56 gboolean is_continuation;
57 proto_tree *pop_tree, *reqresp_tree;
64 const guchar *next_token;
66 if (check_col(pinfo->cinfo, COL_PROTOCOL))
67 col_set_str(pinfo->cinfo, COL_PROTOCOL, "POP");
70 * Find the end of the first line.
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.
76 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
77 line = tvb_get_ptr(tvb, offset, linelen);
79 if (pinfo->match_port == pinfo->destport) {
81 is_continuation = FALSE;
84 is_continuation = response_is_continuation(line);
87 if (check_col(pinfo->cinfo, COL_INFO)) {
89 * Put the first line from the buffer into the summary
90 * if it's a POP request or reply (but leave out the
92 * Otherwise, just call it a continuation.
95 col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
97 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
98 is_request ? "Request" : "Response",
99 format_text(line, linelen));
103 ti = proto_tree_add_item(tree, proto_pop, tvb, offset, -1,
105 pop_tree = proto_item_add_subtree(ti, ett_pop);
107 if (is_continuation) {
109 * Put the whole packet into the tree as data.
111 call_dissector(data_handle,tvb, pinfo, pop_tree);
116 proto_tree_add_boolean_hidden(pop_tree,
117 hf_pop_request, tvb, 0, 0, TRUE);
119 proto_tree_add_boolean_hidden(pop_tree,
120 hf_pop_response, tvb, 0, 0, TRUE);
124 * Put the line into the protocol tree.
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);
132 * Extract the first token, and, if there is a first
133 * token, add it as the request or reply code.
135 tokenlen = get_token_len(line, line + linelen, &next_token);
138 proto_tree_add_text(reqresp_tree, tvb, offset,
139 tokenlen, "Request: %s",
140 format_text(line, tokenlen));
142 proto_tree_add_text(reqresp_tree, tvb, offset,
143 tokenlen, "Response: %s",
144 format_text(line, tokenlen));
146 offset += next_token - line;
147 linelen -= next_token - line;
152 * Add the rest of the first line as request or
157 proto_tree_add_text(reqresp_tree, tvb, offset,
158 linelen, "Request Arg: %s",
159 format_text(line, linelen));
161 proto_tree_add_text(reqresp_tree, tvb, offset,
162 linelen, "Response Arg: %s",
163 format_text(line, linelen));
166 offset = next_offset;
169 * Show the rest of the request or response as text,
172 while (tvb_offset_exists(tvb, offset)) {
174 * Find the end of the line.
176 linelen = tvb_find_line_end(tvb, offset, -1,
177 &next_offset, FALSE);
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;
190 static gboolean response_is_continuation(const guchar *data)
192 if (strncmp(data, "+OK", strlen("+OK")) == 0)
195 if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
202 proto_register_pop(void)
205 static hf_register_info hf[] = {
207 { "Response", "pop.response",
208 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
209 "TRUE if POP response", HFILL }},
212 { "Request", "pop.request",
213 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
214 "TRUE if POP request", HFILL }}
216 static gint *ett[] = {
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));
227 proto_reg_handoff_pop(void)
229 dissector_handle_t pop_handle;
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");