Don't guard col_set_str (COL_PROTOCOL) with col_check
[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         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                         }
209                 }
210
211                 /*
212                  * We don't consider this part of an ICAP message,
213                  * so we don't display it.
214                  * (Yeah, that means we don't display, say, a text/icap
215                  * page, but you can get that from the data pane.)
216                  */
217                 if (!is_icap)
218                         break;
219 is_icap_header:
220                 if (tree) {
221                         proto_tree_add_text(icap_tree, tvb, offset,
222                                 next_offset - offset, "%s",
223                                 tvb_format_text(tvb, offset,
224                                                 next_offset - offset)
225                                 );
226                 }
227                 offset = next_offset;
228         }
229
230         if (tree) {
231                 switch (icap_type) {
232
233                 case ICAP_OPTIONS:
234                         hidden_item = proto_tree_add_boolean(icap_tree,
235                                             hf_icap_options, tvb, 0, 0, 1);
236                         PROTO_ITEM_SET_HIDDEN(hidden_item);
237                         break;
238
239                 case ICAP_REQMOD:
240                         hidden_item = proto_tree_add_boolean(icap_tree,
241                                             hf_icap_reqmod, tvb, 0, 0, 1);
242                         PROTO_ITEM_SET_HIDDEN(hidden_item);
243                         break;
244
245                 case ICAP_RESPMOD:
246                         hidden_item = proto_tree_add_boolean(icap_tree,
247                                             hf_icap_respmod, tvb, 0, 0, 1);
248                         PROTO_ITEM_SET_HIDDEN(hidden_item);
249                         break;
250
251                 case ICAP_RESPONSE:
252                         hidden_item = proto_tree_add_boolean(icap_tree,
253                                             hf_icap_response, tvb, 0, 0, 1);
254                         PROTO_ITEM_SET_HIDDEN(hidden_item);
255                         break;
256
257                 case ICAP_OTHER:
258                 default:
259                         break;
260                 }
261         }
262
263         datalen = tvb_length_remaining(tvb, offset);
264         if (datalen > 0) {
265                 call_dissector(data_handle,
266                     tvb_new_subset(tvb, offset, -1, -1), pinfo, icap_tree);
267         }
268 }
269
270
271 static int
272 is_icap_message(const guchar *data, int linelen, icap_type_t *type)
273 {
274 #define ICAP_COMPARE(string, length, msgtype) {         \
275         if (strncmp(data, string, length) == 0) {       \
276                 if (*type == ICAP_OTHER)                \
277                         *type = msgtype;                \
278                 return TRUE;                            \
279         }                                               \
280 }
281         /*
282          * From draft-elson-opes-icap-01(72).txt
283          */
284         if (linelen >= 5) {
285                 ICAP_COMPARE("ICAP/", 5, ICAP_RESPONSE); /* response */
286         }
287         if (linelen >= 7) {
288                 ICAP_COMPARE("REQMOD ", 7, ICAP_REQMOD); /* request mod */
289         }
290         if (linelen >= 8) {
291                 ICAP_COMPARE("OPTIONS ", 8, ICAP_OPTIONS); /* options */
292                 ICAP_COMPARE("RESPMOD ", 8, ICAP_RESPMOD); /* response mod */
293         }
294         return FALSE;
295 #undef ICAP_COMPARE
296 }
297
298 void
299 proto_register_icap(void)
300 {
301         static hf_register_info hf[] = {
302             { &hf_icap_response,
303               { "Response",             "icap.response",
304                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
305                 "TRUE if ICAP response", HFILL }},
306             { &hf_icap_reqmod,
307               { "Reqmod",               "icap.reqmod",
308                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
309                 "TRUE if ICAP reqmod", HFILL }},
310             { &hf_icap_respmod,
311               { "Respmod",              "icap.respmod",
312                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
313                 "TRUE if ICAP respmod", HFILL }},
314             { &hf_icap_options,
315               { "Options",              "icap.options",
316                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
317                 "TRUE if ICAP options", HFILL }},
318             { &hf_icap_other,
319               { "Other",                "icap.other",
320                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
321                 "TRUE if ICAP other", HFILL }},
322         };
323         static gint *ett[] = {
324                 &ett_icap,
325         };
326
327         proto_icap = proto_register_protocol(
328                         "Internet Content Adaptation Protocol",
329                         "ICAP", "icap");
330         proto_register_field_array(proto_icap, hf, array_length(hf));
331         proto_register_subtree_array(ett, array_length(ett));
332
333 }
334
335 void
336 proto_reg_handoff_icap(void)
337 {
338         dissector_handle_t icap_handle;
339
340         data_handle = find_dissector("data");
341         icap_handle = create_dissector_handle(dissect_icap, proto_icap);
342         dissector_add("tcp.port", TCP_PORT_ICAP, icap_handle);
343 }