MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-multipart.c
blobb413667e0255992983c471d6695ff2d98e6677ff
1 /* packet-multipart.c
2 * Routines for multipart media encapsulation dissection
3 * Copyright 2004, Anders Broman.
4 * Copyright 2004, Olivier Biot.
6 * $Id$
8 * Refer to the AUTHORS file or the AUTHORS section in the man page
9 * for contacting the author(s) of this file.
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 * References for "media-type multipart/mixed :
31 * http://www.iana.org/assignments/media-types/index.html
32 * http://www.ietf.org/rfc/rfc2045.txt?number=2045
33 * http://www.rfc-editor.org/rfc/rfc2046.txt
34 * http://www.rfc-editor.org/rfc/rfc2047.txt
35 * http://www.rfc-editor.org/rfc/rfc2048.txt
36 * http://www.rfc-editor.org/rfc/rfc2049.txt
38 * Part of the code is modeled from the SIP and HTTP dissectors
40 * General format of a MIME multipart document:
41 * [ preamble line-end ]
42 * dash-boundary transport-padding line-end
43 * body-part
44 * *encapsulation
45 * close-delimiter transport-padding
46 * [ line-end epilogue ]
48 * Where:
49 * dash-boundary := "--" boundary
50 * encapsulation := delimiter transport-padding line-end body-part
51 * delimiter := line-end body-part
52 * close-delimiter := delimiter "--"
53 * body-part := MIME-part-headers [ line-end *OCTET ]
54 * transport-padding := *LWSP-char
56 * Note that line-end is often a LF instead of a CRLF.
59 #include "config.h"
61 #include <string.h>
62 #include <ctype.h>
64 #include <glib.h>
66 #include <epan/packet.h>
67 #include <epan/prefs.h>
68 #include <epan/base64.h>
69 #include <epan/wmem/wmem.h>
71 #include "packet-imf.h"
73 /* Dissector table for media requiring special attention in multipart
74 * encapsulation. */
75 static dissector_table_t multipart_media_subdissector_table;
77 /* Initialize the protocol and registered fields */
78 static int proto_multipart = -1;
80 /* Initialize the subtree pointers */
81 static gint ett_multipart = -1;
82 static gint ett_multipart_main = -1;
83 static gint ett_multipart_body = -1;
85 /* Not sure that compact_name exists for multipart, but choose to keep
86 * the structure from SIP dissector, all the content- is also from SIP */
89 typedef struct {
90 const char *name;
91 const char *compact_name;
92 } multipart_header_t;
94 static const multipart_header_t multipart_headers[] = {
95 { "Unknown-header", NULL }, /* Pad so that the real headers start at index 1 */
96 { "Content-Disposition", NULL },
97 { "Content-Encoding", "e" },
98 { "Content-Id", NULL },
99 { "Content-Language", NULL },
100 { "Content-Length", "l" },
101 { "Content-Transfer-Encoding", NULL },
102 { "Content-Type", "c" },
105 #define POS_CONTENT_DISPOSITION 1
106 #define POS_CONTENT_ENCODING 2
107 #define POS_CONTENT_ID 3
108 #define POS_CONTENT_LANGUAGE 4
109 #define POS_CONTENT_LENGTH 5
110 #define POS_CONTENT_TRANSFER_ENCODING 6
111 #define POS_CONTENT_TYPE 7
113 /* Initialize the header fields */
114 static gint hf_multipart_type = -1;
115 static gint hf_multipart_part = -1;
117 static gint hf_header_array[] = {
118 -1, /* "Unknown-header" - Pad so that the real headers start at index 1 */
119 -1, /* "Content-Disposition" */
120 -1, /* "Content-Encoding" */
121 -1, /* "Content-Id" */
122 -1, /* "Content-Language" */
123 -1, /* "Content-Length" */
124 -1, /* "Content-Transfer-Encoding" */
125 -1, /* "Content-Type" */
128 /* Define media_type/Content type table */
129 static dissector_table_t media_type_dissector_table;
131 /* Data and media dissector handles */
132 static dissector_handle_t data_handle;
133 static dissector_handle_t media_handle;
135 /* Determins if bodies with no media type dissector shoud be displayed
136 * as raw text, may cause problems with images sound etc
137 * TODO improve to check for different content types ?
139 static gboolean display_unknown_body_as_text = FALSE;
140 static gboolean remove_base64_encoding = FALSE;
143 typedef struct {
144 const char *type; /* Type of multipart */
145 char *boundary; /* Boundary string (enclosing quotes removed if any) */
146 guint boundary_length; /* Length of the boundary string */
147 } multipart_info_t;
151 static gint
152 find_first_boundary(tvbuff_t *tvb, gint start, const guint8 *boundary,
153 gint boundary_len, gint *boundary_line_len, gboolean *last_boundary);
154 static gint
155 find_next_boundary(tvbuff_t *tvb, gint start, const guint8 *boundary,
156 gint boundary_len, gint *boundary_line_len, gboolean *last_boundary);
157 static gint
158 process_preamble(proto_tree *tree, tvbuff_t *tvb, const guint8 *boundary,
159 gint boundary_len, gboolean *last_boundary);
160 static gint
161 process_body_part(proto_tree *tree, tvbuff_t *tvb, const guint8 *boundary,
162 gint boundary_len, packet_info *pinfo, gint start,
163 gboolean *last_boundary);
164 static gint
165 is_known_multipart_header(const char *header_str, guint len);
166 static gint
167 index_of_char(const char *str, const char c);
170 /* Return a tvb that contains the binary representation of a base64
171 string */
173 static tvbuff_t *
174 base64_decode(packet_info *pinfo, tvbuff_t *b64_tvb, char *name)
176 char *data;
177 tvbuff_t *tvb;
178 data = tvb_get_string(wmem_packet_scope(), b64_tvb, 0, tvb_length(b64_tvb));
180 tvb = base64_to_tvb(b64_tvb, data);
181 add_new_data_source(pinfo, tvb, name);
183 return tvb;
187 * Unfold and clean up a MIME-like header, and process LWS as follows:
188 * o Preserves LWS in quoted text
189 * o Remove LWS before and after a separator
190 * o Remove trailing LWS
191 * o Replace other LWS with a single space
192 * Set value to the start of the value
193 * Return the cleaned-up RFC2822 header (buffer must be freed).
195 static char *
196 unfold_and_compact_mime_header(const char *lines, gint *first_colon_offset)
198 const char *p = lines;
199 char c;
200 char *ret, *q;
201 char sep_seen = 0; /* Did we see a separator ":;," */
202 char lws = FALSE; /* Did we see LWS (incl. folding) */
203 gint colon = -1;
205 if (! lines) return NULL;
207 c = *p;
208 ret = (char *)wmem_alloc(wmem_packet_scope(), strlen(lines) + 1);
209 q = ret;
211 while (c) {
212 if (c == ':') {
213 lws = FALSE; /* Prevent leading LWS from showing up */
214 if (colon == -1) {/* First colon */
215 colon = (gint) (q - ret);
217 *(q++) = sep_seen = c;
218 p++;
219 } else if (c == ';' || c == ',' || c == '=') {
220 lws = FALSE; /* Prevent leading LWS from showing up */
221 *(q++) = sep_seen = c;
222 p++;
223 } else if (c == ' ' || c == '\t') {
224 lws = TRUE;
225 p++;
226 } else if (c == '\n') {
227 lws = FALSE; /* Skip trailing LWS */
228 if ((c = *(p+1))) {
229 if (c == ' ' || c == '\t') { /* Header unfolding */
230 lws = TRUE;
231 p += 2;
232 } else {
233 *q = c = 0; /* Stop */
236 } else if (c == '\r') {
237 lws = FALSE;
238 if ((c = *(p+1))) {
239 if (c == '\n') {
240 if ((c = *(p+2))) {
241 if (c == ' ' || c == '\t') { /* Header unfolding */
242 lws = TRUE;
243 p += 3;
244 } else {
245 *q = c = 0; /* Stop */
248 } else if (c == ' ' || c == '\t') { /* Header unfolding */
249 lws = TRUE;
250 p += 2;
251 } else {
252 *q = c = 0; /* Stop */
255 } else if (c == '"') { /* Start of quoted-string */
256 lws = FALSE;
257 *(q++) = c;
258 while (c) {
259 c = *(q++) = *(++p);
260 if (c == '"') {
261 p++; /* Skip closing quote */
262 break;
265 /* if already zero terminated now, rewind one char to avoid an "off by one" */
266 if(c == 0) {
267 q--;
269 } else { /* Regular character */
270 if (sep_seen) {
271 sep_seen = 0;
272 } else {
273 if (lws) {
274 *(q++) = ' ';
277 lws = FALSE;
278 *(q++) = c;
279 p++; /* OK */
282 if (c) {
283 c = *p;
286 *q = 0;
288 *first_colon_offset = colon;
289 return (ret);
292 /* Return the index of a given char in the given string,
293 * or -1 if not found.
295 static gint
296 index_of_char(const char *str, const char c)
298 gint len = 0;
299 const char *p = str;
301 while (*p && *p != c) {
302 p++;
303 len++;
306 if (*p)
307 return len;
308 return -1;
311 static char *find_parameter(char *parameters, const char *key, int *retlen)
313 char *start, *p;
314 int keylen = 0;
315 int len = 0;
317 if(!parameters || !*parameters || !key || strlen(key) == 0)
318 /* we won't be able to find anything */
319 return NULL;
321 keylen = (int) strlen(key);
322 p = parameters;
324 while (*p) {
326 while ((*p) && isspace((guchar)*p))
327 p++; /* Skip white space */
329 if (g_ascii_strncasecmp(p, key, keylen) == 0)
330 break;
331 /* Skip to next parameter */
332 p = strchr(p, ';');
333 if (p == NULL)
335 return NULL;
337 p++; /* Skip semicolon */
340 start = p + keylen;
341 if (start[0] == 0) {
342 return NULL;
346 * Process the parameter value
348 if (start[0] == '"') {
350 * Parameter value is a quoted-string
352 start++; /* Skip the quote */
353 len = index_of_char(start, '"');
354 if (len < 0) {
356 * No closing quote
358 return NULL;
360 } else {
362 * Look for end of boundary
364 p = start;
365 while (*p) {
366 if (*p == ';' || isspace((guchar)*p))
367 break;
368 p++;
369 len++;
373 if(retlen)
374 (*retlen) = len;
376 return start;
379 /* Retrieve the media information from pinfo->private_data,
380 * and compute the boundary string and its length.
381 * Return a pointer to a filled-in multipart_info_t, or NULL on failure.
383 * Boundary delimiters must not appear within the encapsulated material,
384 * and must be no longer than 70 characters, not counting the two
385 * leading hyphens. (quote from rfc2046)
387 static multipart_info_t *
388 get_multipart_info(packet_info *pinfo)
390 const char *start;
391 int len = 0;
392 multipart_info_t *m_info = NULL;
393 const char *type = pinfo->match_string;
394 char *parameters;
395 gint dummy;
397 if ((type == NULL) || (pinfo->private_data == NULL)) {
399 * We need both a content type AND parameters
400 * for multipart dissection.
402 return NULL;
405 /* Clean up the parameters */
406 parameters = unfold_and_compact_mime_header((const char *)pinfo->private_data, &dummy);
408 start = find_parameter(parameters, "boundary=", &len);
410 if(!start) {
411 return NULL;
415 * There is a value for the boundary string
417 m_info = (multipart_info_t *)g_malloc(sizeof(multipart_info_t));
418 m_info->type = type;
419 m_info->boundary = g_strndup(start, len);
420 m_info->boundary_length = len;
422 return m_info;
425 static void
426 cleanup_multipart_info(void *data)
428 multipart_info_t *m_info = (multipart_info_t *)data;
429 if (m_info) {
430 g_free(m_info->boundary);
431 g_free(m_info);
436 * The first boundary does not implicitly contain the leading
437 * line-end sequence.
439 * Return the offset to the 1st byte of the boundary delimiter line.
440 * Set boundary_line_len to the length of the entire boundary delimiter.
441 * Set last_boundary to TRUE if we've seen the last-boundary delimiter.
443 static gint
444 find_first_boundary(tvbuff_t *tvb, gint start, const guint8 *boundary,
445 gint boundary_len, gint *boundary_line_len, gboolean *last_boundary)
447 gint offset = start, next_offset, line_len, boundary_start;
449 while (tvb_length_remaining(tvb, offset + 2 + boundary_len) > 0) {
450 boundary_start = offset;
451 if (((tvb_strneql(tvb, offset, (const guint8 *)"--", 2) == 0)
452 && (tvb_strneql(tvb, offset + 2, boundary, boundary_len) == 0)))
454 /* Boundary string; now check if last */
455 if ((tvb_length_remaining(tvb, offset + 2 + boundary_len + 2) >= 0)
456 && (tvb_strneql(tvb, offset + 2 + boundary_len,
457 (const guint8 *)"--", 2) == 0)) {
458 *last_boundary = TRUE;
459 } else {
460 *last_boundary = FALSE;
462 /* Look for line end of the boundary line */
463 line_len = tvb_find_line_end(tvb, offset, -1, &offset, FALSE);
464 if (line_len == -1) {
465 *boundary_line_len = -1;
466 } else {
467 *boundary_line_len = offset - boundary_start;
469 return boundary_start;
471 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
472 if (line_len == -1) {
473 return -1;
475 offset = next_offset;
478 return -1;
482 * Unless the first boundary, subsequent boundaries include a line-end sequence
483 * before the dashed boundary string.
485 * Return the offset to the 1st byte of the boundary delimiter line.
486 * Set boundary_line_len to the length of the entire boundary delimiter.
487 * Set last_boundary to TRUE if we've seen the last-boundary delimiter.
489 static gint
490 find_next_boundary(tvbuff_t *tvb, gint start, const guint8 *boundary,
491 gint boundary_len, gint *boundary_line_len, gboolean *last_boundary)
493 gint offset = start, next_offset, line_len, boundary_start;
495 while (tvb_length_remaining(tvb, offset + 2 + boundary_len) > 0) {
496 line_len = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
497 if (line_len == -1) {
498 return -1;
500 boundary_start = offset + line_len;
501 if (((tvb_strneql(tvb, next_offset, (const guint8 *)"--", 2) == 0)
502 && (tvb_strneql(tvb, next_offset + 2, boundary, boundary_len) == 0)))
504 /* Boundary string; now check if last */
505 if ((tvb_length_remaining(tvb, next_offset + 2 + boundary_len + 2) >= 0)
506 && (tvb_strneql(tvb, next_offset + 2 + boundary_len,
507 (const guint8 *)"--", 2) == 0)) {
508 *last_boundary = TRUE;
509 } else {
510 *last_boundary = FALSE;
512 /* Look for line end of the boundary line */
513 line_len = tvb_find_line_end(tvb, next_offset, -1, &offset, FALSE);
514 if (line_len == -1) {
515 *boundary_line_len = -1;
516 } else {
517 *boundary_line_len = offset - boundary_start;
519 return boundary_start;
521 offset = next_offset;
524 return -1;
528 * Process the multipart preamble:
529 * [ preamble line-end ] dashed-boundary transport-padding line-end
531 * Return the offset to the start of the first body-part.
533 static gint
534 process_preamble(proto_tree *tree, tvbuff_t *tvb, const guint8 *boundary,
535 gint boundary_len, gboolean *last_boundary)
537 gint boundary_start, boundary_line_len;
539 boundary_start = find_first_boundary(tvb, 0, boundary, boundary_len,
540 &boundary_line_len, last_boundary);
541 if (boundary_start == 0) {
542 if (tree) {
543 proto_tree_add_text(tree, tvb, boundary_start, boundary_line_len,
544 "First boundary: %s",
545 tvb_format_text(tvb, boundary_start, boundary_line_len));
547 return boundary_start + boundary_line_len;
548 } else if (boundary_start > 0) {
549 if (boundary_line_len > 0) {
550 gint body_part_start = boundary_start + boundary_line_len;
552 if (tree) {
553 if (body_part_start > 0) {
554 proto_tree_add_text(tree, tvb, 0, body_part_start,
555 "Preamble");
557 proto_tree_add_text(tree, tvb, boundary_start,
558 boundary_line_len, "First boundary: %s",
559 tvb_format_text(tvb, boundary_start,
560 boundary_line_len));
562 return body_part_start;
565 return -1;
569 * Process a multipart body-part:
570 * MIME-part-headers [ line-end *OCTET ]
571 * line-end dashed-boundary transport-padding line-end
573 * If applicable, call a media subdissector.
575 * Return the offset to the start of the next body-part.
577 static gint
578 process_body_part(proto_tree *tree, tvbuff_t *tvb, const guint8 *boundary,
579 gint boundary_len, packet_info *pinfo, gint start,
580 gboolean *last_boundary)
582 proto_tree *subtree = NULL;
583 proto_item *ti = NULL;
584 gint offset = start, next_offset = 0;
585 char *parameters = NULL;
586 gint body_start, boundary_start, boundary_line_len;
588 char *content_type_str = NULL;
589 char *content_encoding_str = NULL;
590 char *filename = NULL;
591 char *mimetypename = NULL;
592 int len = 0;
593 gboolean last_field = FALSE;
595 if (tree) {
596 ti = proto_tree_add_item(tree, hf_multipart_part, tvb, start, 0, ENC_ASCII|ENC_NA);
597 subtree = proto_item_add_subtree(ti, ett_multipart_body);
600 * Process the MIME-part-headers
603 while (!last_field)
605 gint colon_offset;
606 char *hdr_str;
607 char *header_str;
609 /* Look for the end of the header (denoted by cr)
610 * 3:d argument to imf_find_field_end() maxlen; must be last offset in the tvb.
612 next_offset = imf_find_field_end(tvb, offset, tvb_length_remaining(tvb, offset)+offset, &last_field);
613 /* If cr not found, won't have advanced - get out to avoid infinite loop! */
614 if (next_offset == offset) {
615 break;
618 hdr_str = tvb_get_string(wmem_packet_scope(), tvb, offset, next_offset - offset);
620 header_str = unfold_and_compact_mime_header(hdr_str, &colon_offset);
621 if (colon_offset <= 0) {
622 if (tree) {
623 proto_tree_add_text(subtree, tvb, offset, next_offset - offset,
624 "%s",
625 tvb_format_text(tvb, offset, next_offset - offset));
627 } else {
628 gint hf_index;
630 /* Split header name from header value */
631 header_str[colon_offset] = '\0';
632 hf_index = is_known_multipart_header(header_str, colon_offset);
634 if (hf_index == -1) {
635 if (tree) {
636 proto_tree_add_text(subtree, tvb, offset,
637 next_offset - offset,
638 "%s",
639 tvb_format_text(tvb, offset, next_offset - offset));
641 } else {
642 char *value_str = header_str + colon_offset + 1;
644 if (tree) {
645 proto_tree_add_string_format(subtree,
646 hf_header_array[hf_index], tvb,
647 offset, next_offset - offset,
648 (const char *)value_str, "%s",
649 tvb_format_text(tvb, offset, next_offset - offset));
652 switch (hf_index) {
653 case POS_CONTENT_TYPE:
655 /* The Content-Type starts at colon_offset + 1 */
656 gint semicolon_offset = index_of_char(
657 value_str, ';');
659 if (semicolon_offset > 0) {
660 value_str[semicolon_offset] = '\0';
661 parameters = wmem_strdup(wmem_packet_scope(), value_str + semicolon_offset + 1);
662 } else {
663 parameters = NULL;
666 content_type_str = g_ascii_strdown(value_str, -1);
668 /* Show content-type in root 'part' label */
669 proto_item_append_text(ti, " (%s)", content_type_str);
671 /* find the "name" parameter in case we don't find a content disposition "filename" */
672 if((mimetypename = find_parameter(parameters, "name=", &len)) != NULL) {
673 mimetypename = g_strndup(mimetypename, len);
678 break;
679 case POS_CONTENT_TRANSFER_ENCODING:
681 /* The Content-Transfeing starts at colon_offset + 1 */
682 gint cr_offset = index_of_char(value_str, '\r');
684 if (cr_offset > 0) {
685 value_str[cr_offset] = '\0';
688 content_encoding_str = g_ascii_strdown(value_str, -1);
690 break;
691 case POS_CONTENT_DISPOSITION:
693 /* find the "filename" parameter */
694 if((filename = find_parameter(value_str, "filename=", &len)) != NULL) {
695 filename = g_strndup(filename, len);
698 break;
699 default:
700 break;
704 offset = next_offset;
707 body_start = next_offset;
710 * Process the body
713 boundary_start = find_next_boundary(tvb, body_start, boundary, boundary_len,
714 &boundary_line_len, last_boundary);
715 if (boundary_start > 0) {
716 gint body_len = boundary_start - body_start;
717 tvbuff_t *tmp_tvb = tvb_new_subset(tvb, body_start,
718 body_len, body_len);
720 if (content_type_str) {
723 * subdissection
725 void *save_private_data = pinfo->private_data;
726 gboolean dissected;
729 * Try and remove any content transfer encoding so that each sub-dissector
730 * doesn't have to do it itself
734 if(content_encoding_str && remove_base64_encoding) {
736 if(!g_ascii_strncasecmp(content_encoding_str, "base64", 6))
737 tmp_tvb = base64_decode(pinfo, tmp_tvb, filename ? filename : (mimetypename ? mimetypename : content_type_str));
741 pinfo->private_data = parameters;
743 * First try the dedicated multipart dissector table
745 dissected = dissector_try_string(multipart_media_subdissector_table,
746 content_type_str, tmp_tvb, pinfo, subtree, NULL);
747 if (! dissected) {
749 * Fall back to the default media dissector table
751 dissected = dissector_try_string(media_type_dissector_table,
752 content_type_str, tmp_tvb, pinfo, subtree, NULL);
754 if (! dissected) {
755 const char *save_match_string = pinfo->match_string;
756 pinfo->match_string = content_type_str;
757 call_dissector(media_handle, tmp_tvb, pinfo, subtree);
758 pinfo->match_string = save_match_string;
760 pinfo->private_data = save_private_data;
761 g_free(content_type_str);
762 content_type_str = NULL;
763 parameters = NULL; /* Shares same memory as content_type_str */
764 } else {
765 call_dissector(data_handle, tmp_tvb, pinfo, subtree);
767 if (tree) {
768 proto_item_set_len(ti, boundary_start - start);
769 if (*last_boundary == TRUE) {
770 proto_tree_add_text(tree, tvb,
771 boundary_start, boundary_line_len,
772 "Last boundary: %s",
773 tvb_format_text(tvb, boundary_start,
774 boundary_line_len));
775 } else {
776 proto_tree_add_text(tree, tvb,
777 boundary_start, boundary_line_len,
778 "Boundary: %s",
779 tvb_format_text(tvb, boundary_start,
780 boundary_line_len));
784 g_free(filename);
785 g_free(mimetypename);
787 return boundary_start + boundary_line_len;
790 g_free(filename);
791 g_free(mimetypename);
793 return -1;
797 * Call this method to actually dissect the multipart body.
798 * NOTE - Only do so if a boundary string has been found!
800 static int dissect_multipart(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
802 proto_tree *subtree = NULL;
803 proto_item *ti = NULL;
804 multipart_info_t *m_info = get_multipart_info(pinfo);
805 gint header_start = 0;
806 guint8 *boundary;
807 gint boundary_len;
808 gboolean last_boundary = FALSE;
810 if (m_info == NULL) {
812 * We can't get the required multipart information
814 proto_tree_add_text(tree, tvb, 0, -1,
815 "The multipart dissector could not find "
816 "the required boundary parameter.");
817 call_dissector(data_handle, tvb, pinfo, tree);
818 return tvb_length(tvb);
820 boundary = (guint8 *)m_info->boundary;
821 boundary_len = m_info->boundary_length;
822 /* Clean up the memory if an exception is thrown */
823 /* CLEANUP_PUSH(cleanup_multipart_info, m_info); */
825 /* Add stuff to the protocol tree */
826 if (tree) {
827 proto_item *type_ti;
828 ti = proto_tree_add_item(tree, proto_multipart,
829 tvb, 0, -1, ENC_NA);
830 subtree = proto_item_add_subtree(ti, ett_multipart);
831 proto_item_append_text(ti, ", Type: %s, Boundary: \"%s\"",
832 m_info->type, m_info->boundary);
834 /* Show multi-part type as a generated field */
835 type_ti = proto_tree_add_string(subtree, hf_multipart_type,
836 tvb, 0, 0, pinfo->match_string);
837 PROTO_ITEM_SET_GENERATED(type_ti);
841 * Make no entries in Protocol column and Info column on summary display,
842 * but stop sub-dissectors from clearing entered text in summary display.
844 col_set_fence(pinfo->cinfo, COL_INFO);
847 * Process the multipart preamble
849 header_start = process_preamble(subtree, tvb, boundary,
850 boundary_len, &last_boundary);
851 if (header_start == -1) {
852 call_dissector(data_handle, tvb, pinfo, subtree);
853 /* Clean up the dynamically allocated memory */
854 cleanup_multipart_info(m_info);
855 return tvb_length(tvb);
858 * Process the encapsulated bodies
860 while (last_boundary == FALSE) {
861 header_start = process_body_part(subtree, tvb, boundary, boundary_len,
862 pinfo, header_start, &last_boundary);
863 if (header_start == -1) {
864 /* Clean up the dynamically allocated memory */
865 cleanup_multipart_info(m_info);
866 return tvb_length(tvb);
870 * Process the multipart trailer
872 if (tree) {
873 if (tvb_length_remaining(tvb, header_start) > 0) {
874 proto_tree_add_text(subtree, tvb, header_start, -1, "Trailer");
877 /* Clean up the dynamically allocated memory */
878 cleanup_multipart_info(m_info);
879 return tvb_length(tvb);
882 /* Returns index of method in multipart_headers */
883 static gint
884 is_known_multipart_header(const char *header_str, guint len)
886 guint i;
888 for (i = 1; i < array_length(multipart_headers); i++) {
889 if (len == strlen(multipart_headers[i].name) &&
890 g_ascii_strncasecmp(header_str, multipart_headers[i].name, len) == 0)
891 return i;
892 if (multipart_headers[i].compact_name != NULL &&
893 len == strlen(multipart_headers[i].compact_name) &&
894 g_ascii_strncasecmp(header_str, multipart_headers[i].compact_name, len) == 0)
895 return i;
898 return -1;
902 * Register the protocol with Wireshark.
904 * This format is required because a script is used to build the C function
905 * that calls all the protocol registration.
908 void
909 proto_register_multipart(void)
912 /* Setup list of header fields See Section 1.6.1 for details */
913 static hf_register_info hf[] = {
914 { &hf_multipart_type,
915 { "Type",
916 "mime_multipart.type",
917 FT_STRING, BASE_NONE, NULL, 0x00,
918 "MIME multipart encapsulation type", HFILL
921 { &hf_multipart_part,
922 { "Encapsulated multipart part",
923 "mime_multipart.part",
924 FT_STRING, BASE_NONE, NULL, 0x00,
925 NULL, HFILL
928 { &hf_header_array[POS_CONTENT_DISPOSITION],
929 { "Content-Disposition",
930 "mime_multipart.header.content-disposition",
931 FT_STRING, BASE_NONE, NULL, 0x00,
932 "RFC 2183: Content-Disposition Header", HFILL
935 { &hf_header_array[POS_CONTENT_ENCODING],
936 { "Content-Encoding",
937 "mime_multipart.header.content-encoding",
938 FT_STRING, BASE_NONE, NULL, 0x00,
939 "Content-Encoding Header", HFILL
942 { &hf_header_array[POS_CONTENT_ID],
943 { "Content-Id",
944 "mime_multipart.header.content-id",
945 FT_STRING, BASE_NONE, NULL, 0x00,
946 "RFC 2045: Content-Id Header", HFILL
949 { &hf_header_array[POS_CONTENT_LANGUAGE],
950 { "Content-Language",
951 "mime_multipart.header.content-language",
952 FT_STRING, BASE_NONE, NULL, 0x00,
953 "Content-Language Header", HFILL
956 { &hf_header_array[POS_CONTENT_LENGTH],
957 { "Content-Length",
958 "mime_multipart.header.content-length",
959 FT_STRING, BASE_NONE, NULL, 0x0,
960 "Content-Length Header", HFILL
963 { &hf_header_array[POS_CONTENT_TRANSFER_ENCODING],
964 { "Content-Transfer-Encoding",
965 "mime_multipart.header.content-transfer-encoding",
966 FT_STRING, BASE_NONE, NULL, 0x00,
967 "RFC 2045: Content-Transfer-Encoding Header", HFILL
970 { &hf_header_array[POS_CONTENT_TYPE],
971 { "Content-Type",
972 "mime_multipart.header.content-type",
973 FT_STRING, BASE_NONE,NULL,0x0,
974 "Content-Type Header", HFILL
980 * Preferences
982 module_t *multipart_module;
985 * Setup protocol subtree array
987 static gint *ett[] = {
988 &ett_multipart,
989 &ett_multipart_main,
990 &ett_multipart_body,
994 * Register the protocol name and description
996 proto_multipart = proto_register_protocol(
997 "MIME Multipart Media Encapsulation",
998 "MIME multipart",
999 "mime_multipart");
1002 * Required function calls to register
1003 * the header fields and subtrees used.
1005 proto_register_field_array(proto_multipart, hf, array_length(hf));
1006 proto_register_subtree_array(ett, array_length(ett));
1008 multipart_module = prefs_register_protocol(proto_multipart, NULL);
1010 prefs_register_bool_preference(multipart_module,
1011 "display_unknown_body_as_text",
1012 "Display bodies without media type as text",
1013 "Display multipart bodies with no media type dissector"
1014 " as raw text (may cause problems with binary data).",
1015 &display_unknown_body_as_text);
1017 prefs_register_bool_preference(multipart_module,
1018 "remove_base64_encoding",
1019 "Remove base64 encoding from bodies",
1020 "Remove any base64 content-transfer encoding from bodies. "
1021 "This supports export of the body and its further dissection.",
1022 &remove_base64_encoding);
1025 * Dissectors requiring different behavior in cases where the media
1026 * is contained in a multipart entity should register their multipart
1027 * dissector in the dissector table below, which is similar to the
1028 * "media_type" dissector table defined in the HTTP dissector code.
1030 multipart_media_subdissector_table = register_dissector_table(
1031 "multipart_media_type",
1032 "Internet media type (for multipart processing)",
1033 FT_STRING, BASE_NONE);
1037 /* If this dissector uses sub-dissector registration add a registration routine.
1038 This format is required because a script is used to find these routines and
1039 create the code that calls these routines.
1041 void
1042 proto_reg_handoff_multipart(void)
1044 dissector_handle_t multipart_handle;
1047 * When we cannot display the data, call the data dissector.
1048 * When there is no dissector for the given media, call the media dissector.
1050 data_handle = find_dissector("data");
1051 media_handle = find_dissector("media");
1054 * Get the content type and Internet media type table
1056 media_type_dissector_table = find_dissector_table("media_type");
1059 * Handle for multipart dissection
1061 multipart_handle = new_create_dissector_handle(
1062 dissect_multipart, proto_multipart);
1064 dissector_add_string("media_type",
1065 "multipart/mixed", multipart_handle);
1066 dissector_add_string("media_type",
1067 "multipart/related", multipart_handle);
1068 dissector_add_string("media_type",
1069 "multipart/alternative", multipart_handle);
1070 dissector_add_string("media_type",
1071 "multipart/form-data", multipart_handle);
1072 dissector_add_string("media_type",
1073 "multipart/report", multipart_handle);
1076 * Supply an entry to use for unknown multipart subtype.
1077 * See RFC 2046, section 5.1.3
1079 dissector_add_string("media_type",
1080 "multipart/", multipart_handle);