There's no need to register port 631 twice for IPP; do so only once.
[obnox/wireshark/wip.git] / packet-http.c
1 /* packet-http.c
2  * Routines for HTTP packet disassembly
3  *
4  * Guy Harris <guy@alum.mit.edu>
5  *
6  * $Id: packet-http.c,v 1.34 2001/01/11 05:36:09 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
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  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #include <string.h>
39 #include <ctype.h>
40
41 #include <glib.h>
42 #include "packet.h"
43 #include "strutil.h"
44
45 typedef enum _http_type {
46         HTTP_REQUEST,
47         HTTP_RESPONSE,
48         HTTP_NOTIFICATION,
49         HTTP_OTHERS
50 } http_type_t;
51
52 static int proto_http = -1;
53 static int hf_http_notification = -1;
54 static int hf_http_response = -1;
55 static int hf_http_request = -1;
56
57 static gint ett_http = -1;
58
59 #define TCP_PORT_HTTP                   80
60 #define TCP_PORT_PROXY_HTTP             3128
61 #define TCP_PORT_PROXY_ADMIN_HTTP       3132
62 #define TCP_ALT_PORT_HTTP               8080
63
64 #define TCP_PORT_IPP                    631
65
66 #define TCP_PORT_SSDP                   1900
67 #define UDP_PORT_SSDP                   1900
68
69 /*
70  * Protocols implemented atop HTTP.
71  */
72 typedef enum {
73         PROTO_HTTP,             /* just HTTP */
74         PROTO_IPP,              /* Internet Printing Protocol */
75         PROTO_SSDP              /* Simple Service Discovery Protocol */
76 } http_proto_t;
77
78 static int is_http_request_or_reply(const u_char *data, int linelen, http_type_t *type);
79
80 static dissector_handle_t ipp_handle;
81
82 void
83 dissect_http(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
84 {
85         http_proto_t    proto;
86         char            *proto_tag;
87         proto_tree      *http_tree = NULL;
88         proto_item      *ti = NULL;
89         gint            offset = 0;
90         const u_char    *line;
91         gint            next_offset;
92         const u_char    *linep, *lineend;
93         int             linelen;
94         u_char          c;
95         http_type_t     http_type;
96         int             datalen;
97
98         CHECK_DISPLAY_AS_DATA(proto_http, tvb, pinfo, tree);
99
100         pinfo->current_proto = "HTTP";
101
102         switch (pinfo->match_port) {
103
104         case TCP_PORT_IPP:
105                 proto = PROTO_IPP;
106                 proto_tag = "IPP";
107                 break;
108
109         case TCP_PORT_SSDP:     /* TCP_PORT_SSDP = UDP_PORT_SSDP */
110                 proto = PROTO_SSDP;
111                 proto_tag = "SSDP";
112                 break;
113
114         default:
115                 proto = PROTO_HTTP;
116                 proto_tag = "HTTP";
117                 break;
118         }
119         
120         if (check_col(pinfo->fd, COL_PROTOCOL))
121                 col_set_str(pinfo->fd, COL_PROTOCOL, proto_tag);
122         if (check_col(pinfo->fd, COL_INFO)) {
123                 /*
124                  * Put the first line from the buffer into the summary
125                  * if it's an HTTP request or reply (but leave out the
126                  * line terminator).
127                  * Otherwise, just call it a continuation.
128                  */
129                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
130                 line = tvb_get_ptr(tvb, offset, linelen);
131                 http_type = HTTP_OTHERS;        /* type not known yet */
132                 if (is_http_request_or_reply(line, linelen, &http_type))
133                         col_add_str(pinfo->fd, COL_INFO,
134                             format_text(line, linelen));
135                 else
136                         col_set_str(pinfo->fd, COL_INFO, "Continuation");
137         }
138
139         if (tree) {
140                 ti = proto_tree_add_item(tree, proto_http, tvb, offset,
141                     tvb_length_remaining(tvb, offset), FALSE);
142                 http_tree = proto_item_add_subtree(ti, ett_http);
143
144                 /*
145                  * Process the packet data, a line at a time.
146                  */
147                 http_type = HTTP_OTHERS;        /* type not known yet */
148                 while (tvb_offset_exists(tvb, offset)) {
149                         /*
150                          * Find the end of the line.
151                          */
152                         linelen = tvb_find_line_end(tvb, offset, -1,
153                             &next_offset);
154
155                         /*
156                          * Get a buffer that refers to the line.
157                          */
158                         line = tvb_get_ptr(tvb, offset, linelen);
159                         lineend = line + linelen;
160
161                         /*
162                          * OK, does it look like an HTTP request or
163                          * response?
164                          */
165                         if (is_http_request_or_reply(line, linelen, &http_type))
166                                 goto is_http;
167
168                         /*
169                          * No.  Does it look like a blank line (as would
170                          * appear at the end of an HTTP request)?
171                          */
172                         if (linelen == 0)
173                                 goto is_http;
174
175                         /*
176                          * No.  Does it look like a MIME header?
177                          */
178                         linep = line;
179                         while (linep < lineend) {
180                                 c = *linep++;
181                                 if (!isprint(c))
182                                         break;  /* not printable, not a MIME header */
183                                 switch (c) {
184
185                                 case '(':
186                                 case ')':
187                                 case '<':
188                                 case '>':
189                                 case '@':
190                                 case ',':
191                                 case ';':
192                                 case '\\':
193                                 case '"':
194                                 case '/':
195                                 case '[':
196                                 case ']':
197                                 case '?':
198                                 case '=':
199                                 case '{':
200                                 case '}':
201                                         /*
202                                          * It's a tspecial, so it's not
203                                          * part of a token, so it's not
204                                          * a field name for the beginning
205                                          * of a MIME header.
206                                          */
207                                         goto not_http;
208
209                                 case ':':
210                                         /*
211                                          * This ends the token; we consider
212                                          * this to be a MIME header.
213                                          */
214                                         goto is_http;
215                                 }
216                         }
217
218                 not_http:
219                         /*
220                          * We don't consider this part of an HTTP request or
221                          * reply, so we don't display it.
222                          * (Yeah, that means we don't display, say, a
223                          * text/http page, but you can get that from the
224                          * data pane.)
225                          */
226                         break;
227
228                 is_http:
229                         /*
230                          * Put this line.
231                          */
232                         proto_tree_add_text(http_tree, tvb, offset,
233                             next_offset - offset, "%s",
234                             tvb_format_text(tvb, offset, next_offset - offset));
235                         offset = next_offset;
236                 }
237
238                 switch (http_type) {
239
240                 case HTTP_NOTIFICATION:
241                         proto_tree_add_boolean_hidden(http_tree, 
242                             hf_http_notification, tvb, 0, 0, 1);
243                         break;
244
245                 case HTTP_RESPONSE:
246                         proto_tree_add_boolean_hidden(http_tree, 
247                             hf_http_response, tvb, 0, 0, 1);
248                         break;
249
250                 case HTTP_REQUEST:
251                         proto_tree_add_boolean_hidden(http_tree, 
252                             hf_http_request, tvb, 0, 0, 1);
253                         break;
254
255                 case HTTP_OTHERS:
256                 default:
257                         break;
258                 }
259         }
260
261         datalen = tvb_length_remaining(tvb, offset);
262         if (datalen > 0) {
263                 if (proto == PROTO_IPP) {
264                         tvbuff_t *new_tvb = tvb_new_subset(tvb, offset, -1, -1);
265
266                         /*
267                          * Fix up the top-level item so that it doesn't
268                          * include the IPP stuff.
269                          */
270                         if (ti != NULL)
271                                 proto_item_set_len(ti, offset);
272
273                         call_dissector(ipp_handle, new_tvb, pinfo, tree);
274                 } else
275                         dissect_data(tvb, offset, pinfo, http_tree);
276         }
277 }
278
279 /*
280  * XXX - this won't handle HTTP 0.9 replies, but they're all data
281  * anyway.
282  */
283 static int
284 is_http_request_or_reply(const u_char *data, int linelen, http_type_t *type)
285 {
286         /*
287          * From RFC 2774 - An HTTP Extension Framework
288          *
289          * Support the command prefix that identifies the presence of
290          * a "mandatory" header.
291          */
292         if (strncmp(data, "M-", 2) == 0) {
293                 data += 2;
294                 linelen -= 2;
295         }
296
297         /*
298          * From draft-cohen-gena-client-01.txt, available from the uPnP forum:
299          *      NOTIFY, SUBSCRIBE, UNSUBSCRIBE
300          *
301          * From draft-ietf-dasl-protocol-00.txt, a now vanished Microsoft draft:
302          *      SEARCH
303          */
304         if (linelen >= 4) {
305                 if (strncmp(data, "GET ", 4) == 0 ||
306                     strncmp(data, "PUT ", 4) == 0) {
307                         if (*type == HTTP_OTHERS)
308                                 *type = HTTP_REQUEST;
309                         return TRUE;
310                 }
311         }
312         if (linelen >= 5) {
313                 if (strncmp(data, "HEAD ", 5) == 0 ||
314                     strncmp(data, "POST ", 5) == 0) {
315                         if (*type == HTTP_OTHERS)
316                                 *type = HTTP_REQUEST;
317                         return TRUE;
318                 }
319                 if (strncmp(data, "HTTP/", 5) == 0) {
320                         if (*type == HTTP_OTHERS)
321                                 *type = HTTP_RESPONSE;
322                         return TRUE;    /* response */
323                 }
324         }
325         if (linelen >= 6) {
326                 if (strncmp(data, "TRACE ", 6) == 0) {
327                         if (*type == HTTP_OTHERS)
328                                 *type = HTTP_REQUEST;
329                         return TRUE;
330                 }
331         }
332         if (linelen >= 7) {
333                 if (strncmp(data, "DELETE ", 7) == 0) {
334                         if (*type == HTTP_OTHERS)
335                                 *type = HTTP_REQUEST;
336                         return TRUE;
337                 }
338                 if (strncmp(data, "NOTIFY ", 7) == 0 ||
339                     strncmp(data, "SEARCH ", 7) == 0) {
340                         if (*type == HTTP_OTHERS)
341                                 *type = HTTP_NOTIFICATION;
342                         return TRUE;
343                 }
344         }
345         if (linelen >= 8) {
346                 if (strncmp(data, "OPTIONS ", 8) == 0 ||
347                     strncmp(data, "CONNECT ", 8) == 0) {
348                         if (*type == HTTP_OTHERS)
349                                 *type = HTTP_REQUEST;
350                         return TRUE;
351                 }
352         }
353         if (linelen >= 10) {
354                 if (strncmp(data, "SUBSCRIBE ", 10) == 0) {
355                         if (*type == HTTP_OTHERS)
356                                 *type = HTTP_NOTIFICATION;
357                         return TRUE;
358                 }
359         }
360         if (linelen >= 12) {
361                 if (strncmp(data, "UNSUBSCRIBE ", 10) == 0) {
362                         if (*type == HTTP_OTHERS)
363                                 *type = HTTP_NOTIFICATION;
364                         return TRUE;
365                 }
366         }
367         return FALSE;
368 }
369
370 void
371 proto_register_http(void)
372 {
373         static hf_register_info hf[] = {
374             { &hf_http_notification,
375               { "Notification",         "http.notification",  
376                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
377                 "TRUE if HTTP notification" }},
378             { &hf_http_response,
379               { "Response",             "http.response",  
380                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
381                 "TRUE if HTTP response" }},
382             { &hf_http_request,
383               { "Request",              "http.request",
384                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
385                 "TRUE if HTTP request" }},
386         };
387         static gint *ett[] = {
388                 &ett_http,
389         };
390
391         proto_http = proto_register_protocol("Hypertext Transfer Protocol",
392             "HTTP", "http");
393         proto_register_field_array(proto_http, hf, array_length(hf));
394         proto_register_subtree_array(ett, array_length(ett));
395 }
396
397 void
398 proto_reg_handoff_http(void)
399 {
400         dissector_add("tcp.port", TCP_PORT_HTTP, dissect_http, proto_http);
401         dissector_add("tcp.port", TCP_ALT_PORT_HTTP, dissect_http, proto_http);
402         dissector_add("tcp.port", TCP_PORT_PROXY_HTTP, dissect_http,
403             proto_http);
404         dissector_add("tcp.port", TCP_PORT_PROXY_ADMIN_HTTP, dissect_http,
405             proto_http);
406
407         /*
408          * XXX - this is a bit ugly; we probably really want to have
409          * protocols based atop HTTP call a routine to register
410          * themselves with the HTTP dissector, giving an optional
411          * port number (if the port number is "missing", e.g. -1 or 0,
412          * the protocol would be assumed to use a standard HTTP port),
413          * and an optional Content-Type: value (or some other way for
414          * the dissector to tell what the next protocol is).
415          *
416          * The HTTP dissector would register itself for the port in
417          * question (if it's not missing), and would use either the
418          * port number or the Content-Type: (or whatever) value to
419          * determine whether to hand the payload to that dissector.
420          *
421          * It would also pass a protocol number, so we could arrange
422          * that the HTTP part of an IPP packet be dissected iff HTTP
423          * is enabled, and the IPP part be dissected iff IPP is enabled.
424          */
425         dissector_add("tcp.port", TCP_PORT_IPP, dissect_http, proto_http);
426
427         /*
428          * XXX - is there anything to dissect in the body of an SSDP
429          * request or reply?  I.e., should there be an SSDP dissector?
430          */
431         dissector_add("tcp.port", TCP_PORT_SSDP, dissect_http, proto_http);
432         dissector_add("udp.port", UDP_PORT_SSDP, dissect_http, proto_http);
433
434         /*
435          * Get a handle for the IPP dissector.
436          */
437         ipp_handle = find_dissector("ipp");
438 }