Give "dissect_rpc_string()" an extra "char **" argument; if it's
[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.4 2000/01/07 22:05:31 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
46 static int proto_imap = -1;
47 static int hf_imap_response = -1;
48 static int hf_imap_request = -1;
49
50 static gint ett_imap = -1;
51
52 void
53 dissect_imap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
54 {
55         proto_tree      *imap_tree, *ti;
56         gchar          rr[50], rd[1500];
57         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
58         int i2;
59         int max_data = pi.captured_len - offset;
60
61         memset(rr, '\0', sizeof(rr));
62         memset(rd, '\0', sizeof(rd));
63
64         if ((i1 > max_data) || (i1 <= 0)) {
65           
66           i1 = max_data;
67           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
68
69         }
70         else {
71
72           strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
73           i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
74           if (i2 > max_data - i1 - 1 || i2 <= 0) {
75             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\n') - (pd + offset)) - i1 - 1;
76             if (i2 > max_data - i1 - 1 || i2 <= 0)
77               i2 = max_data - i1 - 1;
78           }
79           strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
80         }
81
82         if (check_col(fd, COL_PROTOCOL))
83                 col_add_str(fd, COL_PROTOCOL, "IMAP");
84
85         if (check_col(fd, COL_INFO)) {
86
87           col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);        
88         }
89
90         if (tree) {
91
92           ti = proto_tree_add_item(tree, proto_imap, offset, END_OF_FRAME, NULL);
93           imap_tree = proto_item_add_subtree(ti, ett_imap);
94
95           if (pi.match_port == pi.destport) { /* Request */
96
97             proto_tree_add_item_hidden(imap_tree, hf_imap_request, offset, i1, TRUE);
98             proto_tree_add_text(imap_tree, offset, i1, "Request Tag: %s", rr);
99
100             proto_tree_add_text(imap_tree, offset + i1 + 1, END_OF_FRAME, "Request: %s", rd);
101
102           }
103           else {
104
105             proto_tree_add_item_hidden(imap_tree, hf_imap_response, offset, i1, TRUE);
106             proto_tree_add_text(imap_tree, offset, i1, "Response Tag: %s", rr);
107
108             proto_tree_add_text(imap_tree, offset + i1 + 1, END_OF_FRAME, "Response: %s", rd);
109           }
110
111         }
112 }
113
114 void
115 proto_register_imap(void)
116 {
117   static hf_register_info hf[] = {
118     { &hf_imap_response,
119       { "Response",           "imap.response",
120         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
121         "TRUE if IMAP response" }},
122
123     { &hf_imap_request,
124       { "Request",            "imap.request",
125         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
126         "TRUE if IMAP request" }}
127   };
128   static gint *ett[] = {
129     &ett_imap,
130   };
131
132   proto_imap = proto_register_protocol("Internet Message Access Protocol", 
133                                        "imap");
134   proto_register_field_array(proto_imap, hf, array_length(hf));
135   proto_register_subtree_array(ett, array_length(ett));
136 }