Clean up the descriptions of reassembly preferences.
[obnox/wireshark/wip.git] / packet-icap.c
1 /* packet-icap.c
2  * Routines for ICAP packet disassembly
3  *
4  * Srishylam Simharajan simha@netapp.com
5  *
6  * packet-icap.c Mon Aug 13 17:50:19 PDT 2001 simha 
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include <ctype.h>
37
38 #include <glib.h>
39 #include <epan/packet.h>
40 #include <epan/strutil.h>
41
42 typedef enum _icap_type {
43         ICAP_OPTIONS,
44         ICAP_REQMOD,
45         ICAP_RESPMOD,
46         ICAP_RESPONSE,
47         ICAP_OTHER
48 } icap_type_t;
49
50 static int proto_icap = -1;
51 static int hf_icap_response = -1;
52 static int hf_icap_reqmod = -1;
53 static int hf_icap_respmod = -1;
54 static int hf_icap_options = -1;
55 static int hf_icap_other = -1;
56
57 static gint ett_icap = -1;
58
59 static dissector_handle_t data_handle;
60
61 #define TCP_PORT_ICAP                   1344
62 static int is_icap_message(const u_char *data, int linelen, icap_type_t *type);
63 static void
64 dissect_icap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
65 {
66         proto_tree      *icap_tree = NULL;
67         proto_item      *ti = NULL;
68         gint            offset = 0;
69         const u_char    *line;
70         gint            next_offset;
71         const u_char    *linep, *lineend;
72         int             linelen;
73         u_char          c;
74         icap_type_t     icap_type;
75         int             datalen;
76
77         if (check_col(pinfo->cinfo, COL_PROTOCOL))
78                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ICAP");
79
80         if (check_col(pinfo->cinfo, COL_INFO)) {
81                 /*
82                  * Put the first line from the buffer into the summary
83                  * if it's an ICAP header (but leave out the
84                  * line terminator).
85                  * Otherwise, just call it a continuation.
86                  *
87                  * Note that "tvb_find_line_end()" will return a value that
88                  * is not longer than what's in the buffer, so the
89                  * "tvb_get_ptr()" call won't throw an exception.
90                  */
91                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
92                 line = tvb_get_ptr(tvb, offset, linelen);
93                 icap_type = ICAP_OTHER; /* type not known yet */
94                 if (is_icap_message(line, linelen, &icap_type))
95                         col_add_str(pinfo->cinfo, COL_INFO,
96                             format_text(line, linelen));
97                 else
98                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
99         }
100
101         if (tree) {
102                 ti = proto_tree_add_item(tree, proto_icap, tvb, offset, -1,
103                     FALSE);
104                 icap_tree = proto_item_add_subtree(ti, ett_icap);
105         }
106
107         /*
108          * Process the packet data, a line at a time.
109          */
110         icap_type = ICAP_OTHER; /* type not known yet */
111         while (tvb_offset_exists(tvb, offset)) {
112                 gboolean is_icap = FALSE;
113                 gboolean loop_done = FALSE;
114                 /*
115                  * Find the end of the line.
116                  */
117                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
118
119                 /*
120                  * Get a buffer that refers to the line.
121                  */
122                 line = tvb_get_ptr(tvb, offset, linelen);
123                 lineend = line + linelen;
124
125                 /*
126                  * find header format
127                  */
128                 if (is_icap_message(line, linelen, &icap_type)) {
129                         is_icap = TRUE;
130                         goto is_icap_header;
131                 }
132
133                 /*
134                  * if it looks like a blank line, end of header perhaps?
135                  */
136                 if (linelen == 0) {
137                         is_icap = TRUE;
138                         goto is_icap_header;
139                 }
140
141                 /*
142                  * No.  Does it look like a MIME header?
143                  */
144                 linep = line;
145                 loop_done = FALSE;
146                 while (linep < lineend && (!loop_done)) {
147                         c = *linep++;
148                         if (!isprint(c)) {
149                                 is_icap = FALSE;
150                                 break;  /* not printable, not a MIME header */
151                         }
152                         switch (c) {
153                         case ':':
154                                 is_icap = TRUE;
155                                 goto is_icap_header;
156                                 break;
157                         case '(':
158                         case ')':
159                         case '<':
160                         case '>':
161                         case '@':
162                         case ',':
163                         case ';':
164                         case '\\':
165                         case '"':
166                         case '/':
167                         case '[':
168                         case ']':
169                         case '?':
170                         case '=':
171                         case '{':
172                         case '}':
173                                 is_icap = FALSE;
174                                 loop_done = TRUE;
175                                 break;
176                         }
177                 }
178
179                 /*
180                  * We don't consider this part of an ICAP message,
181                  * so we don't display it.
182                  * (Yeah, that means we don't display, say, a text/icap
183                  * page, but you can get that from the data pane.)
184                  */
185                 if (!is_icap)
186                         break;
187 is_icap_header:
188                 if (tree) {
189                         proto_tree_add_text(icap_tree, tvb, offset,
190                                 next_offset - offset, "%s",
191                                 tvb_format_text(tvb, offset,
192                                                 next_offset - offset)
193                                 );
194                 }
195                 offset = next_offset;
196         }
197
198         if (tree) {
199                 switch (icap_type) {
200
201                 case ICAP_OPTIONS:
202                         proto_tree_add_boolean_hidden(icap_tree,
203                             hf_icap_options, tvb, 0, 0, 1);
204                         break;
205
206                 case ICAP_REQMOD:
207                         proto_tree_add_boolean_hidden(icap_tree,
208                             hf_icap_reqmod, tvb, 0, 0, 1);
209                         break;
210
211                 case ICAP_RESPMOD:
212                         proto_tree_add_boolean_hidden(icap_tree,
213                             hf_icap_respmod, tvb, 0, 0, 1);
214                         break;
215
216                 case ICAP_RESPONSE:
217                         proto_tree_add_boolean_hidden(icap_tree,
218                             hf_icap_response, tvb, 0, 0, 1);
219                         break;
220
221                 case ICAP_OTHER:
222                 default:
223                         break;
224                 }
225         }
226
227         datalen = tvb_length_remaining(tvb, offset);
228         if (datalen > 0) {
229                 call_dissector(data_handle,tvb_new_subset(tvb, offset, -1, tvb_reported_length_remaining(tvb,offset)), pinfo, icap_tree);
230         }
231 }
232
233         
234 static int
235 is_icap_message(const u_char *data, int linelen, icap_type_t *type)
236 {
237 #define ICAP_COMPARE(string, length, msgtype) {         \
238         if (strncmp(data, string, length) == 0) {       \
239                 if (*type == ICAP_OTHER)                \
240                         *type = msgtype;                \
241                 return TRUE;                            \
242         }                                               \
243 }
244         /*
245          * From draft-elson-opes-icap-01(72).txt
246          */
247         if (linelen >= 5) {
248                 ICAP_COMPARE("ICAP/", 5, ICAP_RESPONSE); /* response */
249         } 
250         if (linelen >= 7) {
251                 ICAP_COMPARE("REQMOD ", 7, ICAP_REQMOD); /* request mod */
252         } 
253         if (linelen >= 8) {
254                 ICAP_COMPARE("OPTIONS ", 8, ICAP_OPTIONS); /* options */
255                 ICAP_COMPARE("RESPMOD ", 8, ICAP_RESPMOD); /* response mod */
256         }
257         return FALSE;
258 #undef ICAP_COMPARE
259 }
260
261 void
262 proto_register_icap(void)
263 {
264         static hf_register_info hf[] = {
265             { &hf_icap_response,
266               { "Response",             "icap.response",  
267                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
268                 "TRUE if ICAP response", HFILL }},
269             { &hf_icap_reqmod,
270               { "Reqmod",               "icap.reqmod",
271                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
272                 "TRUE if ICAP reqmod", HFILL }},
273             { &hf_icap_respmod,
274               { "Respmod",              "icap.respmod",
275                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
276                 "TRUE if ICAP respmod", HFILL }},
277             { &hf_icap_options,
278               { "Options",              "icap.options",
279                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
280                 "TRUE if ICAP options", HFILL }},
281             { &hf_icap_other,
282               { "Other",                "icap.other",
283                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
284                 "TRUE if ICAP other", HFILL }},
285         };
286         static gint *ett[] = {
287                 &ett_icap,
288         };
289
290         proto_icap = proto_register_protocol(
291                         "Internet Content Adaptation Protocol", 
292                         "ICAP", "icap");
293         proto_register_field_array(proto_icap, hf, array_length(hf));
294         proto_register_subtree_array(ett, array_length(ett));
295
296 }
297
298 void
299 proto_reg_handoff_icap(void)
300 {
301         dissector_handle_t icap_handle;
302
303         data_handle = find_dissector("data");
304         icap_handle = create_dissector_handle(dissect_icap, proto_icap);
305         dissector_add("tcp.port", TCP_PORT_ICAP, icap_handle);
306 }