Fix some aclocal warnings during autogen.sh
[obnox/wireshark/wip.git] / packet-imap.c
1 /* packet-imap.c
2  * Routines for imap packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  *
5  * $Id: packet-imap.c,v 1.23 2003/06/11 20:03:39 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 static int proto_imap = -1;
40 static int hf_imap_response = -1;
41 static int hf_imap_request = -1;
42
43 static gint ett_imap = -1;
44 static gint ett_imap_reqresp = -1;
45
46 #define TCP_PORT_IMAP                   143
47
48 static void
49 dissect_imap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
50 {
51         gboolean        is_request;
52         proto_tree      *imap_tree, *reqresp_tree;
53         proto_item      *ti;
54         gint            offset = 0;
55         const guchar    *line;
56         gint            next_offset;
57         int             linelen;
58         int             tokenlen;
59         const guchar    *next_token;
60
61         if (check_col(pinfo->cinfo, COL_PROTOCOL))
62                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IMAP");
63
64         /*
65          * Find the end of the first line.
66          *
67          * Note that "tvb_find_line_end()" will return a value that is
68          * not longer than what's in the buffer, so the "tvb_get_ptr()"
69          * call won't throw an exception.
70          */
71         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
72         line = tvb_get_ptr(tvb, offset, linelen);
73
74         if (pinfo->match_port == pinfo->destport)
75                 is_request = TRUE;
76         else
77                 is_request = FALSE;
78
79         if (check_col(pinfo->cinfo, COL_INFO)) {
80                 /*
81                  * Put the first line from the buffer into the summary
82                  * (but leave out the line terminator).
83                  */
84                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
85                     is_request ? "Request" : "Response",
86                     format_text(line, linelen));
87         }
88
89         if (tree) {
90                 ti = proto_tree_add_item(tree, proto_imap, tvb, offset, -1,
91                     FALSE);
92                 imap_tree = proto_item_add_subtree(ti, ett_imap);
93
94                 if (is_request) {
95                         proto_tree_add_boolean_hidden(imap_tree,
96                             hf_imap_request, tvb, 0, 0, TRUE);
97                 } else {
98                         proto_tree_add_boolean_hidden(imap_tree,
99                             hf_imap_response, tvb, 0, 0, TRUE);
100                 }
101
102                 /*
103                  * Put the line into the protocol tree.
104                  */
105                 ti = proto_tree_add_text(imap_tree, tvb, offset,
106                     next_offset - offset, "%s",
107                     tvb_format_text(tvb, offset, next_offset - offset));
108                 reqresp_tree = proto_item_add_subtree(ti, ett_imap_reqresp);
109
110                 /*
111                  * Show the first line as tags + requests or replies.
112                  */
113
114                 /*
115                  * Extract the first token, and, if there is a first
116                  * token, add it as the request or reply tag.
117                  */
118                 tokenlen = get_token_len(line, line + linelen, &next_token);
119                 if (tokenlen != 0) {
120                         if (is_request) {
121                                 proto_tree_add_text(reqresp_tree, tvb, offset,
122                                     tokenlen, "Request Tag: %s",
123                                     format_text(line, tokenlen));
124                         } else {
125                                 proto_tree_add_text(reqresp_tree, tvb, offset,
126                                     tokenlen, "Response Tag: %s",
127                                     format_text(line, tokenlen));
128                         }
129                         offset += next_token - line;
130                         linelen -= next_token - line;
131                         line = next_token;
132                 }
133
134                 /*
135                  * Add the rest of the line as request or reply data.
136                  */
137                 if (linelen != 0) {
138                         if (is_request) {
139                                 proto_tree_add_text(reqresp_tree, tvb, offset,
140                                     linelen, "Request: %s",
141                                     format_text(line, linelen));
142                         } else {
143                                 proto_tree_add_text(reqresp_tree, tvb, offset,
144                                     linelen, "Response: %s",
145                                     format_text(line, linelen));
146                         }
147                 }
148
149                 /*
150                  * XXX - show the rest of the frame; this requires that
151                  * we handle literals, quoted strings, continuation
152                  * responses, etc..
153                  *
154                  * This involves a state machine, and attaching
155                  * state information to the packets.
156                  */
157         }
158 }
159
160 void
161 proto_register_imap(void)
162 {
163   static hf_register_info hf[] = {
164     { &hf_imap_response,
165       { "Response",           "imap.response",
166         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
167         "TRUE if IMAP response", HFILL }},
168
169     { &hf_imap_request,
170       { "Request",            "imap.request",
171         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
172         "TRUE if IMAP request", HFILL }}
173   };
174   static gint *ett[] = {
175     &ett_imap,
176     &ett_imap_reqresp,
177   };
178
179   proto_imap = proto_register_protocol("Internet Message Access Protocol",
180                                        "IMAP", "imap");
181   proto_register_field_array(proto_imap, hf, array_length(hf));
182   proto_register_subtree_array(ett, array_length(ett));
183 }
184
185 void
186 proto_reg_handoff_imap(void)
187 {
188   dissector_handle_t imap_handle;
189
190   imap_handle = create_dissector_handle(dissect_imap, proto_imap);
191   dissector_add("tcp.port", TCP_PORT_IMAP, imap_handle);
192 }