Removed trailing whitespaces from .h and .c files using the
[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.22 2002/08/28 21:00:17 jmayer 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
45 #define TCP_PORT_IMAP                   143
46
47 static void
48 dissect_imap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
49 {
50         gboolean        is_request;
51         proto_tree      *imap_tree, *ti;
52         gint            offset = 0;
53         const guchar    *line;
54         gint            next_offset;
55         int             linelen;
56         int             tokenlen;
57         const guchar    *next_token;
58
59         if (check_col(pinfo->cinfo, COL_PROTOCOL))
60                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IMAP");
61
62         /*
63          * Find the end of the first line.
64          *
65          * Note that "tvb_find_line_end()" will return a value that is
66          * not longer than what's in the buffer, so the "tvb_get_ptr()"
67          * call won't throw an exception.
68          */
69         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
70         line = tvb_get_ptr(tvb, offset, linelen);
71
72         if (pinfo->match_port == pinfo->destport)
73                 is_request = TRUE;
74         else
75                 is_request = FALSE;
76
77         if (check_col(pinfo->cinfo, COL_INFO)) {
78                 /*
79                  * Put the first line from the buffer into the summary
80                  * (but leave out the line terminator).
81                  */
82                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
83                     is_request ? "Request" : "Response",
84                     format_text(line, linelen));
85         }
86
87         if (tree) {
88                 ti = proto_tree_add_item(tree, proto_imap, tvb, offset, -1,
89                     FALSE);
90                 imap_tree = proto_item_add_subtree(ti, ett_imap);
91
92                 if (is_request) {
93                         proto_tree_add_boolean_hidden(imap_tree,
94                             hf_imap_request, tvb, 0, 0, TRUE);
95                 } else {
96                         proto_tree_add_boolean_hidden(imap_tree,
97                             hf_imap_response, tvb, 0, 0, TRUE);
98                 }
99
100                 /*
101                  * Show the first line as tags + requests or replies.
102                  */
103
104                 /*
105                  * Extract the first token, and, if there is a first
106                  * token, add it as the request or reply tag.
107                  */
108                 tokenlen = get_token_len(line, line + linelen, &next_token);
109                 if (tokenlen != 0) {
110                         if (is_request) {
111                                 proto_tree_add_text(imap_tree, tvb, offset,
112                                     tokenlen, "Request Tag: %s",
113                                     format_text(line, tokenlen));
114                         } else {
115                                 proto_tree_add_text(imap_tree, tvb, offset,
116                                     tokenlen, "Response Tag: %s",
117                                     format_text(line, tokenlen));
118                         }
119                         offset += next_token - line;
120                         linelen -= next_token - line;
121                         line = next_token;
122                 }
123
124                 /*
125                  * Add the rest of the line as request or reply data.
126                  */
127                 if (linelen != 0) {
128                         if (is_request) {
129                                 proto_tree_add_text(imap_tree, tvb, offset,
130                                     linelen, "Request: %s",
131                                     format_text(line, linelen));
132                         } else {
133                                 proto_tree_add_text(imap_tree, tvb, offset,
134                                     linelen, "Response: %s",
135                                     format_text(line, linelen));
136                         }
137                 }
138
139                 /*
140                  * XXX - show the rest of the frame; this requires that
141                  * we handle literals, quoted strings, continuation
142                  * responses, etc..
143                  *
144                  * This involves a state machine, and attaching
145                  * state information to the packets.
146                  */
147         }
148 }
149
150 void
151 proto_register_imap(void)
152 {
153   static hf_register_info hf[] = {
154     { &hf_imap_response,
155       { "Response",           "imap.response",
156         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
157         "TRUE if IMAP response", HFILL }},
158
159     { &hf_imap_request,
160       { "Request",            "imap.request",
161         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
162         "TRUE if IMAP request", HFILL }}
163   };
164   static gint *ett[] = {
165     &ett_imap,
166   };
167
168   proto_imap = proto_register_protocol("Internet Message Access Protocol",
169                                        "IMAP", "imap");
170   proto_register_field_array(proto_imap, hf, array_length(hf));
171   proto_register_subtree_array(ett, array_length(ett));
172 }
173
174 void
175 proto_reg_handoff_imap(void)
176 {
177   dissector_handle_t imap_handle;
178
179   imap_handle = create_dissector_handle(dissect_imap, proto_imap);
180   dissector_add("tcp.port", TCP_PORT_IMAP, imap_handle);
181 }