2 * Routines for pop packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-pop.c,v 1.27 2001/12/03 03:59:38 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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
47 static int proto_pop = -1;
48 static int hf_pop_response = -1;
49 static int hf_pop_request = -1;
51 static gint ett_pop = -1;
53 static dissector_handle_t data_handle;
55 #define TCP_PORT_POP 110
57 static gboolean response_is_continuation(const u_char *data);
60 dissect_pop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
63 gboolean is_continuation;
71 const u_char *next_token;
73 if (check_col(pinfo->fd, COL_PROTOCOL))
74 col_set_str(pinfo->fd, COL_PROTOCOL, "POP");
77 * Find the end of the first line.
79 * Note that "tvb_find_line_end()" will return a value that is
80 * not longer than what's in the buffer, so the "tvb_get_ptr()"
81 * call won't throw an exception.
83 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
84 line = tvb_get_ptr(tvb, offset, linelen);
86 if (pinfo->match_port == pinfo->destport) {
88 is_continuation = FALSE;
91 is_continuation = response_is_continuation(line);
94 if (check_col(pinfo->fd, COL_INFO)) {
96 * Put the first line from the buffer into the summary
97 * if it's a POP request or reply (but leave out the
99 * Otherwise, just call it a continuation.
102 col_set_str(pinfo->fd, COL_INFO, "Continuation");
104 col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
105 is_request ? "Request" : "Response",
106 format_text(line, linelen));
110 ti = proto_tree_add_item(tree, proto_pop, tvb, offset,
111 tvb_length_remaining(tvb, offset), FALSE);
112 pop_tree = proto_item_add_subtree(ti, ett_pop);
114 if (is_continuation) {
116 * Put the whole packet into the tree as data.
118 call_dissector(data_handle,tvb, pinfo, pop_tree);
123 proto_tree_add_boolean_hidden(pop_tree,
124 hf_pop_request, tvb, 0, 0, TRUE);
126 proto_tree_add_boolean_hidden(pop_tree,
127 hf_pop_response, tvb, 0, 0, TRUE);
131 * Extract the first token, and, if there is a first
132 * token, add it as the request or reply code.
134 tokenlen = get_token_len(line, line + linelen, &next_token);
137 proto_tree_add_text(pop_tree, tvb, offset,
138 tokenlen, "Request: %s",
139 format_text(line, tokenlen));
141 proto_tree_add_text(pop_tree, tvb, offset,
142 tokenlen, "Response: %s",
143 format_text(line, tokenlen));
145 offset += next_token - line;
146 linelen -= next_token - line;
151 * Add the rest of the first line as request or
156 proto_tree_add_text(pop_tree, tvb, offset,
157 linelen, "Request Arg: %s",
158 format_text(line, linelen));
160 proto_tree_add_text(pop_tree, tvb, offset,
161 linelen, "Response Arg: %s",
162 format_text(line, linelen));
165 offset = next_offset;
168 * Show the rest of the request or response as text,
171 while (tvb_offset_exists(tvb, offset)) {
173 * Find the end of the line.
175 linelen = tvb_find_line_end(tvb, offset, -1,
181 proto_tree_add_text(pop_tree, tvb, offset,
182 next_offset - offset, "%s",
183 tvb_format_text(tvb, offset, next_offset - offset));
184 offset = next_offset;
189 static gboolean response_is_continuation(const u_char *data)
191 if (strncmp(data, "+OK", strlen("+OK")) == 0)
194 if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
201 proto_register_pop(void)
204 static hf_register_info hf[] = {
206 { "Response", "pop.response",
207 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
208 "TRUE if POP response", HFILL }},
211 { "Request", "pop.request",
212 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
213 "TRUE if POP request", HFILL }}
215 static gint *ett[] = {
219 proto_pop = proto_register_protocol("Post Office Protocol", "POP", "pop");
220 proto_register_field_array(proto_pop, hf, array_length(hf));
221 proto_register_subtree_array(ett, array_length(ett));
225 proto_reg_handoff_pop(void)
227 dissector_handle_t pop_handle;
229 pop_handle = create_dissector_handle(dissect_pop, proto_pop);
230 dissector_add("tcp.port", TCP_PORT_POP, pop_handle);
231 data_handle = find_dissector("data");