8653df54be6da1e6dfdc7bc95224e7ee0bb6fb99
[obnox/wireshark/wip.git] / epan / dissectors / packet-icap.c
1 /* packet-icap.c
2  * Routines for ICAP packet disassembly
3  * RFC 3507
4  *
5  * Srishylam Simharajan simha@netapp.com
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
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 <string.h>
33 #include <ctype.h>
34
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 typedef enum _icap_type {
40         ICAP_OPTIONS,
41         ICAP_REQMOD,
42         ICAP_RESPMOD,
43         ICAP_RESPONSE,
44         ICAP_OTHER
45 } icap_type_t;
46
47 static int proto_icap = -1;
48 static int hf_icap_response = -1;
49 static int hf_icap_reqmod = -1;
50 static int hf_icap_respmod = -1;
51 static int hf_icap_options = -1;
52 static int hf_icap_other = -1;
53
54 static gint ett_icap = -1;
55
56 static dissector_handle_t data_handle;
57
58 #define TCP_PORT_ICAP                   1344
59 static int is_icap_message(const guchar *data, int linelen, icap_type_t *type);
60 static void
61 dissect_icap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
62 {
63         proto_tree      *icap_tree = NULL;
64         proto_item      *ti = NULL;
65         proto_item      *hidden_item;
66         gint            offset = 0;
67         const guchar    *line;
68         gint            next_offset;
69         const guchar    *linep, *lineend;
70         int             linelen;
71         guchar          c;
72         icap_type_t     icap_type;
73         int             datalen;
74
75         if (check_col(pinfo->cinfo, COL_PROTOCOL))
76                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ICAP");
77
78         if (check_col(pinfo->cinfo, COL_INFO)) {
79                 /*
80                  * Put the first line from the buffer into the summary
81                  * if it's an ICAP header (but leave out the
82                  * line terminator).
83                  * Otherwise, just call it a continuation.
84                  *
85                  * Note that "tvb_find_line_end()" will return a value that
86                  * is not longer than what's in the buffer, so the
87                  * "tvb_get_ptr()" call won't throw an exception.
88                  */
89                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
90                     FALSE);
91                 line = tvb_get_ptr(tvb, offset, linelen);
92                 icap_type = ICAP_OTHER; /* type not known yet */
93                 if (is_icap_message(line, linelen, &icap_type))
94                         col_add_str(pinfo->cinfo, COL_INFO,
95                             format_text(line, linelen));
96                 else
97                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
98         }
99
100         if (tree) {
101                 ti = proto_tree_add_item(tree, proto_icap, tvb, offset, -1,
102                     FALSE);
103                 icap_tree = proto_item_add_subtree(ti, ett_icap);
104         }
105
106         /*
107          * Process the packet data, a line at a time.
108          */
109         icap_type = ICAP_OTHER; /* type not known yet */
110         while (tvb_offset_exists(tvb, offset)) {
111                 gboolean is_icap = FALSE;
112                 gboolean loop_done = FALSE;
113                 /*
114                  * Find the end of the line.
115                  */
116                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
117                     FALSE);
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 header?
143                  */
144                 linep = line;
145                 loop_done = FALSE;
146                 while (linep < lineend && (!loop_done)) {
147                         c = *linep++;
148
149                         /*
150                          * This must be a CHAR to be part of a token; that
151                          * means it must be ASCII.
152                          */
153                         if (!isascii(c)) {
154                                 is_icap = FALSE;
155                                 break;  /* not ASCII, thus not a CHAR */
156                         }
157
158                         /*
159                          * This mustn't be a CTL to be part of a token.
160                          *
161                          * XXX - what about leading LWS on continuation
162                          * lines of a header?
163                          */
164                         if (iscntrl(c)) {
165                                 is_icap = FALSE;
166                                 break;  /* CTL, not part of a header */
167                         }
168
169                         switch (c) {
170
171                         case '(':
172                         case ')':
173                         case '<':
174                         case '>':
175                         case '@':
176                         case ',':
177                         case ';':
178                         case '\\':
179                         case '"':
180                         case '/':
181                         case '[':
182                         case ']':
183                         case '?':
184                         case '=':
185                         case '{':
186                         case '}':
187                                 /*
188                                  * It's a separator, so it's not part of a
189                                  * token, so it's not a field name for the
190                                  * beginning of a header.
191                                  *
192                                  * (We don't have to check for HT; that's
193                                  * already been ruled out by "iscntrl()".)
194                                  *
195                                  * XXX - what about ' '?  HTTP's checks
196                                  * check for that.
197                                  */
198                                 is_icap = FALSE;
199                                 loop_done = TRUE;
200                                 break;
201
202                         case ':':
203                                 /*
204                                  * This ends the token; we consider this
205                                  * to be a header.
206                                  */
207                                 is_icap = TRUE;
208                                 goto is_icap_header;
209                         }
210                 }
211
212                 /*
213                  * We don't consider this part of an ICAP message,
214                  * so we don't display it.
215                  * (Yeah, that means we don't display, say, a text/icap
216                  * page, but you can get that from the data pane.)
217                  */
218                 if (!is_icap)
219                         break;
220 is_icap_header:
221                 if (tree) {
222                         proto_tree_add_text(icap_tree, tvb, offset,
223                                 next_offset - offset, "%s",
224                                 tvb_format_text(tvb, offset,
225                                                 next_offset - offset)
226                                 );
227                 }
228                 offset = next_offset;
229         }
230
231         if (tree) {
232                 switch (icap_type) {
233
234                 case ICAP_OPTIONS:
235                         hidden_item = proto_tree_add_boolean(icap_tree,
236                                             hf_icap_options, tvb, 0, 0, 1);
237                         PROTO_ITEM_SET_HIDDEN(hidden_item);
238                         break;
239
240                 case ICAP_REQMOD:
241                         hidden_item = proto_tree_add_boolean(icap_tree,
242                                             hf_icap_reqmod, tvb, 0, 0, 1);
243                         PROTO_ITEM_SET_HIDDEN(hidden_item);
244                         break;
245
246                 case ICAP_RESPMOD:
247                         hidden_item = proto_tree_add_boolean(icap_tree,
248                                             hf_icap_respmod, tvb, 0, 0, 1);
249                         PROTO_ITEM_SET_HIDDEN(hidden_item);
250                         break;
251
252                 case ICAP_RESPONSE:
253                         hidden_item = proto_tree_add_boolean(icap_tree,
254                                             hf_icap_response, tvb, 0, 0, 1);
255                         PROTO_ITEM_SET_HIDDEN(hidden_item);
256                         break;
257
258                 case ICAP_OTHER:
259                 default:
260                         break;
261                 }
262         }
263
264         datalen = tvb_length_remaining(tvb, offset);
265         if (datalen > 0) {
266                 call_dissector(data_handle,
267                     tvb_new_subset(tvb, offset, -1, -1), pinfo, icap_tree);
268         }
269 }
270
271
272 static int
273 is_icap_message(const guchar *data, int linelen, icap_type_t *type)
274 {
275 #define ICAP_COMPARE(string, length, msgtype) {         \
276         if (strncmp(data, string, length) == 0) {       \
277                 if (*type == ICAP_OTHER)                \
278                         *type = msgtype;                \
279                 return TRUE;                            \
280         }                                               \
281 }
282         /*
283          * From draft-elson-opes-icap-01(72).txt
284          */
285         if (linelen >= 5) {
286                 ICAP_COMPARE("ICAP/", 5, ICAP_RESPONSE); /* response */
287         }
288         if (linelen >= 7) {
289                 ICAP_COMPARE("REQMOD ", 7, ICAP_REQMOD); /* request mod */
290         }
291         if (linelen >= 8) {
292                 ICAP_COMPARE("OPTIONS ", 8, ICAP_OPTIONS); /* options */
293                 ICAP_COMPARE("RESPMOD ", 8, ICAP_RESPMOD); /* response mod */
294         }
295         return FALSE;
296 #undef ICAP_COMPARE
297 }
298
299 void
300 proto_register_icap(void)
301 {
302         static hf_register_info hf[] = {
303             { &hf_icap_response,
304               { "Response",             "icap.response",
305                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
306                 "TRUE if ICAP response", HFILL }},
307             { &hf_icap_reqmod,
308               { "Reqmod",               "icap.reqmod",
309                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
310                 "TRUE if ICAP reqmod", HFILL }},
311             { &hf_icap_respmod,
312               { "Respmod",              "icap.respmod",
313                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
314                 "TRUE if ICAP respmod", HFILL }},
315             { &hf_icap_options,
316               { "Options",              "icap.options",
317                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
318                 "TRUE if ICAP options", HFILL }},
319             { &hf_icap_other,
320               { "Other",                "icap.other",
321                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
322                 "TRUE if ICAP other", HFILL }},
323         };
324         static gint *ett[] = {
325                 &ett_icap,
326         };
327
328         proto_icap = proto_register_protocol(
329                         "Internet Content Adaptation Protocol",
330                         "ICAP", "icap");
331         proto_register_field_array(proto_icap, hf, array_length(hf));
332         proto_register_subtree_array(ett, array_length(ett));
333
334 }
335
336 void
337 proto_reg_handoff_icap(void)
338 {
339         dissector_handle_t icap_handle;
340
341         data_handle = find_dissector("data");
342         icap_handle = create_dissector_handle(dissect_icap, proto_icap);
343         dissector_add("tcp.port", TCP_PORT_ICAP, icap_handle);
344 }