2 * Routines for imap packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-imap.c,v 1.19 2002/01/24 09:20:48 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>
44 #include <epan/packet.h>
45 #include <epan/strutil.h>
47 static int proto_imap = -1;
48 static int hf_imap_response = -1;
49 static int hf_imap_request = -1;
51 static gint ett_imap = -1;
53 #define TCP_PORT_IMAP 143
56 dissect_imap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
59 proto_tree *imap_tree, *ti;
65 const u_char *next_token;
67 if (check_col(pinfo->cinfo, COL_PROTOCOL))
68 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IMAP");
71 * Find the end of the first line.
73 * Note that "tvb_find_line_end()" will return a value that is
74 * not longer than what's in the buffer, so the "tvb_get_ptr()"
75 * call won't throw an exception.
77 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
78 line = tvb_get_ptr(tvb, offset, linelen);
80 if (pinfo->match_port == pinfo->destport)
85 if (check_col(pinfo->cinfo, COL_INFO)) {
87 * Put the first line from the buffer into the summary
88 * (but leave out the line terminator).
90 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
91 is_request ? "Request" : "Response",
92 format_text(line, linelen));
96 ti = proto_tree_add_item(tree, proto_imap, tvb, offset, -1,
98 imap_tree = proto_item_add_subtree(ti, ett_imap);
101 proto_tree_add_boolean_hidden(imap_tree,
102 hf_imap_request, tvb, 0, 0, TRUE);
104 proto_tree_add_boolean_hidden(imap_tree,
105 hf_imap_response, tvb, 0, 0, TRUE);
109 * Show the first line as tags + requests or replies.
113 * Extract the first token, and, if there is a first
114 * token, add it as the request or reply tag.
116 tokenlen = get_token_len(line, line + linelen, &next_token);
119 proto_tree_add_text(imap_tree, tvb, offset,
120 tokenlen, "Request Tag: %s",
121 format_text(line, tokenlen));
123 proto_tree_add_text(imap_tree, tvb, offset,
124 tokenlen, "Response Tag: %s",
125 format_text(line, tokenlen));
127 offset += next_token - line;
128 linelen -= next_token - line;
133 * Add the rest of the line as request or reply data.
137 proto_tree_add_text(imap_tree, tvb, offset,
138 linelen, "Request: %s",
139 format_text(line, linelen));
141 proto_tree_add_text(imap_tree, tvb, offset,
142 linelen, "Response: %s",
143 format_text(line, linelen));
148 * XXX - show the rest of the frame; this requires that
149 * we handle literals, quoted strings, continuation
152 * This involves a state machine, and attaching
153 * state information to the packets.
159 proto_register_imap(void)
161 static hf_register_info hf[] = {
163 { "Response", "imap.response",
164 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
165 "TRUE if IMAP response", HFILL }},
168 { "Request", "imap.request",
169 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
170 "TRUE if IMAP request", HFILL }}
172 static gint *ett[] = {
176 proto_imap = proto_register_protocol("Internet Message Access Protocol",
178 proto_register_field_array(proto_imap, hf, array_length(hf));
179 proto_register_subtree_array(ett, array_length(ett));
183 proto_reg_handoff_imap(void)
185 dissector_handle_t imap_handle;
187 imap_handle = create_dissector_handle(dissect_imap, proto_imap);
188 dissector_add("tcp.port", TCP_PORT_IMAP, imap_handle);