2 * Routines for imap packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-imap.c,v 1.10 2000/11/19 08:53:58 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
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>
46 static int proto_imap = -1;
47 static int hf_imap_response = -1;
48 static int hf_imap_request = -1;
50 static gint ett_imap = -1;
52 #define TCP_PORT_IMAP 143
55 dissect_imap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
57 proto_tree *imap_tree, *ti;
58 gchar rr[50], rd[1500];
59 int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
61 int max_data = pi.captured_len - offset;
63 OLD_CHECK_DISPLAY_AS_DATA(proto_imap, pd, offset, fd, tree);
65 memset(rr, '\0', sizeof(rr));
66 memset(rd, '\0', sizeof(rd));
68 if ((i1 > max_data) || (i1 <= 0)) {
71 strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
76 strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
77 i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
78 if (i2 > max_data - i1 - 1 || i2 <= 0) {
79 i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\n') - (pd + offset)) - i1 - 1;
80 if (i2 > max_data - i1 - 1 || i2 <= 0)
81 i2 = max_data - i1 - 1;
83 strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
86 if (check_col(fd, COL_PROTOCOL))
87 col_set_str(fd, COL_PROTOCOL, "IMAP");
89 if (check_col(fd, COL_INFO)) {
91 col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);
96 ti = proto_tree_add_item(tree, proto_imap, NullTVB, offset, END_OF_FRAME, FALSE);
97 imap_tree = proto_item_add_subtree(ti, ett_imap);
99 if (pi.match_port == pi.destport) { /* Request */
101 proto_tree_add_boolean_hidden(imap_tree, hf_imap_request, NullTVB, offset, i1, TRUE);
102 proto_tree_add_text(imap_tree, NullTVB, offset, i1, "Request Tag: %s", rr);
104 proto_tree_add_text(imap_tree, NullTVB, offset + i1 + 1, END_OF_FRAME, "Request: %s", rd);
109 proto_tree_add_boolean_hidden(imap_tree, hf_imap_response, NullTVB, offset, i1, TRUE);
110 proto_tree_add_text(imap_tree, NullTVB, offset, i1, "Response Tag: %s", rr);
112 proto_tree_add_text(imap_tree, NullTVB, offset + i1 + 1, END_OF_FRAME, "Response: %s", rd);
119 proto_register_imap(void)
121 static hf_register_info hf[] = {
123 { "Response", "imap.response",
124 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
125 "TRUE if IMAP response" }},
128 { "Request", "imap.request",
129 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
130 "TRUE if IMAP request" }}
132 static gint *ett[] = {
136 proto_imap = proto_register_protocol("Internet Message Access Protocol",
138 proto_register_field_array(proto_imap, hf, array_length(hf));
139 proto_register_subtree_array(ett, array_length(ett));
143 proto_reg_handoff_imap(void)
145 old_dissector_add("tcp.port", TCP_PORT_IMAP, dissect_imap);