Start of a PCNFSD dissector as the RPC dissector 150001.
[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.15 2001/06/18 02:17:47 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
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 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "strutil.h"
46
47 static int proto_imap = -1;
48 static int hf_imap_response = -1;
49 static int hf_imap_request = -1;
50
51 static gint ett_imap = -1;
52
53 #define TCP_PORT_IMAP                   143
54
55 static void
56 dissect_imap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
57 {
58         gboolean        is_request;
59         proto_tree      *imap_tree, *ti;
60         gint            offset = 0;
61         const u_char    *line;
62         gint            next_offset;
63         int             linelen;
64         int             tokenlen;
65         const u_char    *next_token;
66
67         if (check_col(pinfo->fd, COL_PROTOCOL))
68                 col_set_str(pinfo->fd, COL_PROTOCOL, "IMAP");
69
70         /*
71          * Find the end of the first line.
72          *
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.
76          */
77         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
78         line = tvb_get_ptr(tvb, offset, linelen);
79
80         if (pinfo->match_port == pinfo->destport)
81                 is_request = TRUE;
82         else
83                 is_request = FALSE;
84
85         if (check_col(pinfo->fd, COL_INFO)) {
86                 /*
87                  * Put the first line from the buffer into the summary
88                  * (but leave out the line terminator).
89                  */
90                 col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
91                     is_request ? "Request" : "Response",
92                     format_text(line, linelen));
93         }
94
95         if (tree) {
96                 ti = proto_tree_add_item(tree, proto_imap, tvb, offset,
97                     tvb_length_remaining(tvb, offset), FALSE);
98                 imap_tree = proto_item_add_subtree(ti, ett_imap);
99
100                 if (is_request) {
101                         proto_tree_add_boolean_hidden(imap_tree,
102                             hf_imap_request, tvb, 0, 0, TRUE);
103                 } else {
104                         proto_tree_add_boolean_hidden(imap_tree,
105                             hf_imap_response, tvb, 0, 0, TRUE);
106                 }
107
108                 /*
109                  * Show the first line as tags + requests or replies.
110                  */
111
112                 /*
113                  * Extract the first token, and, if there is a first
114                  * token, add it as the request or reply tag.
115                  */
116                 tokenlen = get_token_len(line, line + linelen, &next_token);
117                 if (tokenlen != 0) {
118                         if (is_request) {
119                                 proto_tree_add_text(imap_tree, tvb, offset,
120                                     tokenlen, "Request Tag: %s",
121                                     format_text(line, tokenlen));
122                         } else {
123                                 proto_tree_add_text(imap_tree, tvb, offset,
124                                     tokenlen, "Response Tag: %s",
125                                     format_text(line, tokenlen));
126                         }
127                         offset += next_token - line;
128                         linelen -= next_token - line;
129                         line = next_token;
130                 }
131
132                 /*
133                  * Add the rest of the line as request or reply data.
134                  */
135                 if (linelen != 0) {
136                         if (is_request) {
137                                 proto_tree_add_text(imap_tree, tvb, offset,
138                                     linelen, "Request: %s",
139                                     format_text(line, linelen));
140                         } else {
141                                 proto_tree_add_text(imap_tree, tvb, offset,
142                                     linelen, "Response: %s",
143                                     format_text(line, linelen));
144                         }
145                 }
146
147                 /*
148                  * XXX - show the rest of the frame; this requires that
149                  * we handle literals, quoted strings, continuation
150                  * responses, etc..
151                  *
152                  * This involves a state machine, and attaching
153                  * state information to the packets.
154                  */
155         }
156 }
157
158 void
159 proto_register_imap(void)
160 {
161   static hf_register_info hf[] = {
162     { &hf_imap_response,
163       { "Response",           "imap.response",
164         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
165         "TRUE if IMAP response", HFILL }},
166
167     { &hf_imap_request,
168       { "Request",            "imap.request",
169         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
170         "TRUE if IMAP request", HFILL }}
171   };
172   static gint *ett[] = {
173     &ett_imap,
174   };
175
176   proto_imap = proto_register_protocol("Internet Message Access Protocol", 
177                                        "IMAP", "imap");
178   proto_register_field_array(proto_imap, hf, array_length(hf));
179   proto_register_subtree_array(ett, array_length(ett));
180 }
181
182 void
183 proto_reg_handoff_imap(void)
184 {
185   dissector_add("tcp.port", TCP_PORT_IMAP, dissect_imap, proto_imap);
186 }