Fix up a bunch of arguments to "dissect_ber_identifier()" to match its
[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  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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         gint            offset = 0;
66         const guchar    *line;
67         gint            next_offset;
68         const guchar    *linep, *lineend;
69         int             linelen;
70         guchar          c;
71         icap_type_t     icap_type;
72         int             datalen;
73
74         if (check_col(pinfo->cinfo, COL_PROTOCOL))
75                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ICAP");
76
77         if (check_col(pinfo->cinfo, COL_INFO)) {
78                 /*
79                  * Put the first line from the buffer into the summary
80                  * if it's an ICAP header (but leave out the
81                  * line terminator).
82                  * Otherwise, just call it a continuation.
83                  *
84                  * Note that "tvb_find_line_end()" will return a value that
85                  * is not longer than what's in the buffer, so the
86                  * "tvb_get_ptr()" call won't throw an exception.
87                  */
88                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
89                     FALSE);
90                 line = tvb_get_ptr(tvb, offset, linelen);
91                 icap_type = ICAP_OTHER; /* type not known yet */
92                 if (is_icap_message(line, linelen, &icap_type))
93                         col_add_str(pinfo->cinfo, COL_INFO,
94                             format_text(line, linelen));
95                 else
96                         col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
97         }
98
99         if (tree) {
100                 ti = proto_tree_add_item(tree, proto_icap, tvb, offset, -1,
101                     FALSE);
102                 icap_tree = proto_item_add_subtree(ti, ett_icap);
103         }
104
105         /*
106          * Process the packet data, a line at a time.
107          */
108         icap_type = ICAP_OTHER; /* type not known yet */
109         while (tvb_offset_exists(tvb, offset)) {
110                 gboolean is_icap = FALSE;
111                 gboolean loop_done = FALSE;
112                 /*
113                  * Find the end of the line.
114                  */
115                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
116                     FALSE);
117
118                 /*
119                  * Get a buffer that refers to the line.
120                  */
121                 line = tvb_get_ptr(tvb, offset, linelen);
122                 lineend = line + linelen;
123
124                 /*
125                  * find header format
126                  */
127                 if (is_icap_message(line, linelen, &icap_type)) {
128                         is_icap = TRUE;
129                         goto is_icap_header;
130                 }
131
132                 /*
133                  * if it looks like a blank line, end of header perhaps?
134                  */
135                 if (linelen == 0) {
136                         is_icap = TRUE;
137                         goto is_icap_header;
138                 }
139
140                 /*
141                  * No.  Does it look like a header?
142                  */
143                 linep = line;
144                 loop_done = FALSE;
145                 while (linep < lineend && (!loop_done)) {
146                         c = *linep++;
147
148                         /*
149                          * This must be a CHAR to be part of a token; that
150                          * means it must be ASCII.
151                          */
152                         if (!isascii(c)) {
153                                 is_icap = FALSE;
154                                 break;  /* not ASCII, thus not a CHAR */
155                         }
156
157                         /*
158                          * This mustn't be a CTL to be part of a token.
159                          *
160                          * XXX - what about leading LWS on continuation
161                          * lines of a header?
162                          */
163                         if (iscntrl(c)) {
164                                 is_icap = FALSE;
165                                 break;  /* CTL, not part of a header */
166                         }
167
168                         switch (c) {
169
170                         case '(':
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                                 /*
187                                  * It's a separator, so it's not part of a
188                                  * token, so it's not a field name for the
189                                  * beginning of a header.
190                                  *
191                                  * (We don't have to check for HT; that's
192                                  * already been ruled out by "iscntrl()".)
193                                  *
194                                  * XXX - what about ' '?  HTTP's checks
195                                  * check for that.
196                                  */
197                                 is_icap = FALSE;
198                                 loop_done = TRUE;
199                                 break;
200
201                         case ':':
202                                 /*
203                                  * This ends the token; we consider this
204                                  * to be a header.
205                                  */
206                                 is_icap = TRUE;
207                                 goto is_icap_header;
208                                 break;
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                         proto_tree_add_boolean_hidden(icap_tree,
236                             hf_icap_options, tvb, 0, 0, 1);
237                         break;
238
239                 case ICAP_REQMOD:
240                         proto_tree_add_boolean_hidden(icap_tree,
241                             hf_icap_reqmod, tvb, 0, 0, 1);
242                         break;
243
244                 case ICAP_RESPMOD:
245                         proto_tree_add_boolean_hidden(icap_tree,
246                             hf_icap_respmod, tvb, 0, 0, 1);
247                         break;
248
249                 case ICAP_RESPONSE:
250                         proto_tree_add_boolean_hidden(icap_tree,
251                             hf_icap_response, tvb, 0, 0, 1);
252                         break;
253
254                 case ICAP_OTHER:
255                 default:
256                         break;
257                 }
258         }
259
260         datalen = tvb_length_remaining(tvb, offset);
261         if (datalen > 0) {
262                 call_dissector(data_handle,
263                     tvb_new_subset(tvb, offset, -1, -1), pinfo, icap_tree);
264         }
265 }
266
267
268 static int
269 is_icap_message(const guchar *data, int linelen, icap_type_t *type)
270 {
271 #define ICAP_COMPARE(string, length, msgtype) {         \
272         if (strncmp(data, string, length) == 0) {       \
273                 if (*type == ICAP_OTHER)                \
274                         *type = msgtype;                \
275                 return TRUE;                            \
276         }                                               \
277 }
278         /*
279          * From draft-elson-opes-icap-01(72).txt
280          */
281         if (linelen >= 5) {
282                 ICAP_COMPARE("ICAP/", 5, ICAP_RESPONSE); /* response */
283         }
284         if (linelen >= 7) {
285                 ICAP_COMPARE("REQMOD ", 7, ICAP_REQMOD); /* request mod */
286         }
287         if (linelen >= 8) {
288                 ICAP_COMPARE("OPTIONS ", 8, ICAP_OPTIONS); /* options */
289                 ICAP_COMPARE("RESPMOD ", 8, ICAP_RESPMOD); /* response mod */
290         }
291         return FALSE;
292 #undef ICAP_COMPARE
293 }
294
295 void
296 proto_register_icap(void)
297 {
298         static hf_register_info hf[] = {
299             { &hf_icap_response,
300               { "Response",             "icap.response",
301                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
302                 "TRUE if ICAP response", HFILL }},
303             { &hf_icap_reqmod,
304               { "Reqmod",               "icap.reqmod",
305                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
306                 "TRUE if ICAP reqmod", HFILL }},
307             { &hf_icap_respmod,
308               { "Respmod",              "icap.respmod",
309                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
310                 "TRUE if ICAP respmod", HFILL }},
311             { &hf_icap_options,
312               { "Options",              "icap.options",
313                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
314                 "TRUE if ICAP options", HFILL }},
315             { &hf_icap_other,
316               { "Other",                "icap.other",
317                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
318                 "TRUE if ICAP other", HFILL }},
319         };
320         static gint *ett[] = {
321                 &ett_icap,
322         };
323
324         proto_icap = proto_register_protocol(
325                         "Internet Content Adaptation Protocol",
326                         "ICAP", "icap");
327         proto_register_field_array(proto_icap, hf, array_length(hf));
328         proto_register_subtree_array(ett, array_length(ett));
329
330 }
331
332 void
333 proto_reg_handoff_icap(void)
334 {
335         dissector_handle_t icap_handle;
336
337         data_handle = find_dissector("data");
338         icap_handle = create_dissector_handle(dissect_icap, proto_icap);
339         dissector_add("tcp.port", TCP_PORT_ICAP, icap_handle);
340 }