fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / packet-mmse.c
1 /* packet-mmse.c
2  * Routines for MMS Message Encapsulation dissection
3  * Copyright 2001, Tom Uijldert <tom.uijldert@cmg.nl>
4  * Copyright 2004, Olivier Biot
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  * ----------
26  *
27  * Dissector of an encoded Multimedia message PDU, as defined by the WAPForum
28  * (http://www.wapforum.org) in "WAP-209-MMSEncapsulation-20020105-a".
29  * Subsequent releases of MMS are in control of the Open Mobile Alliance (OMA):
30  * Dissection of MMS 1.1 as in OMA-MMS-ENC-v1.1.
31  * Dissection of MMS 1.2 as in OMA-MMS-ENC-v1.2 (not finished yet).
32  */
33
34 /* This file has been edited with 8-space tabs and 4-space indentation */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <glib.h>
45
46 #include <epan/packet.h>
47 #include <epan/strutil.h>
48 #include "packet-wap.h"
49 #include "packet-wsp.h"
50 /* #include "packet-mmse.h" */          /* We autoregister      */
51
52 #define MM_QUOTE                0x7F    /* Quoted string        */
53
54 #define MMS_CONTENT_TYPE        0x3E    /* WINA-value for mms-message   */
55
56 /* General-purpose debug logger.
57  * Requires double parentheses because of variable arguments of printf().
58  *
59  * Enable debug logging for MMSE by defining AM_CFLAGS
60  * so that it contains "-DDEBUG_mmse"
61  */
62 #ifdef DEBUG_mmse
63 #define DebugLog(x) \
64         printf("%s:%u: ", __FILE__, __LINE__); \
65         printf x; \
66         fflush(stdout)
67 #else
68 #define DebugLog(x) ;
69 #endif
70
71
72 /*
73  * Forward declarations
74  */
75 static void dissect_mmse_standalone(tvbuff_t *, packet_info *, proto_tree *);
76 static void dissect_mmse_encapsulated(tvbuff_t *, packet_info *, proto_tree *);
77 static void dissect_mmse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
78         guint8 pdut, char *message_type);
79
80 /*
81  * Header field values
82  */
83 /* MMS 1.0 */
84 #define MM_BCC_HDR              0x81    /* Bcc                  */
85 #define MM_CC_HDR               0x82    /* Cc                   */
86 #define MM_CLOCATION_HDR        0x83    /* X-Mms-Content-Location       */
87 #define MM_CTYPE_HDR            0x84    /* Content-Type         */
88 #define MM_DATE_HDR             0x85    /* Date                         */
89 #define MM_DREPORT_HDR          0x86    /* X-Mms-Delivery-Report        */
90 #define MM_DTIME_HDR            0x87    /* X-Mms-Delivery-Time          */
91 #define MM_EXPIRY_HDR           0x88    /* X-Mms-Expiry                 */
92 #define MM_FROM_HDR             0x89    /* From                         */
93 #define MM_MCLASS_HDR           0x8A    /* X-Mms-Message-Class          */
94 #define MM_MID_HDR              0x8B    /* Message-ID                   */
95 #define MM_MTYPE_HDR            0x8C    /* X-Mms-Message-Type           */
96 #define MM_VERSION_HDR          0x8D    /* X-Mms-MMS-Version            */
97 #define MM_MSIZE_HDR            0x8E    /* X-Mms-Message-Size           */
98 #define MM_PRIORITY_HDR         0x8F    /* X-Mms-Priority               */
99 #define MM_RREPLY_HDR           0x90    /* X-Mms-Read-Reply             */
100 #define MM_RALLOWED_HDR         0x91    /* X-Mms-Report-Allowed         */
101 #define MM_RSTATUS_HDR          0x92    /* X-Mms-Response-Status        */
102 #define MM_RTEXT_HDR            0x93    /* X-Mms-Response-Text          */
103 #define MM_SVISIBILITY_HDR      0x94    /* X-Mms-Sender-Visibility      */
104 #define MM_STATUS_HDR           0x95    /* X-Mms-Status                 */
105 #define MM_SUBJECT_HDR          0x96    /* Subject                      */
106 #define MM_TO_HDR               0x97    /* To                           */
107 #define MM_TID_HDR              0x98    /* X-Mms-Transaction-Id         */
108 /* MMS 1.1 */
109 #define MM_RETRIEVE_STATUS_HDR  0x99    /* X-Mms-Retrieve-Status        */
110 #define MM_RETRIEVE_TEXT_HDR    0x9A    /* X-Mms-Retrieve-Text          */
111 #define MM_READ_STATUS_HDR      0x9B    /* X-Mms-Read-Status            */
112 #define MM_REPLY_CHARGING_HDR   0x9C    /* X-Mms-Reply-Charging         */
113 #define MM_REPLY_CHARGING_DEADLINE_HDR  \
114                                 0x9D    /* X-Mms-Reply-Charging-Deadline*/
115 #define MM_REPLY_CHARGING_ID_HDR        \
116                                 0x9E    /* X-Mms-Reply-Charging-ID      */
117 #define MM_REPLY_CHARGING_SIZE_HDR      \
118                                 0x9F    /* X-Mms-Reply-Charging-Size    */
119 #define MM_PREV_SENT_BY_HDR     0xA0    /* X-Mms-Previously-Sent-By     */
120 #define MM_PREV_SENT_DATE_HDR   0xA1    /* X-Mms-Previously-Sent-Date   */
121 /* MMS 1.2 */
122 #define MM_STORE_HDR            0xA2    /* X-Mms-Store                  */
123 #define MM_MM_STATE_HDR         0xA3    /* X-Mms-MM-State               */
124 #define MM_MM_FLAGS_HDR         0xA4    /* X-Mms-MM-Flags               */
125 #define MM_STORE_STATUS_HDR     0xA5    /* X-Mms-Store-Status           */
126 #define MM_STORE_STATUS_TEXT_HDR        \
127                                 0xA6    /* X-Mms-Store-Status-Text      */
128 #define MM_STORED_HDR           0xA7    /* X-Mms-Stored                 */
129 #define MM_ATTRIBUTES_HDR       0xA8    /* X-Mms-Attributes             */
130 #define MM_TOTALS_HDR           0xA9    /* X-Mms-Totals                 */
131 #define MM_MBOX_TOTALS_HDR      0xAA    /* X-Mms-Mbox-Totals            */
132 #define MM_QUOTAS_HDR           0xAB    /* X-Mms-Quotas                 */
133 #define MM_MBOX_QUOTAS_HDR      0xAC    /* X-Mms-Mbox-Quotas            */
134 #define MM_MBOX_MSG_COUNT_HDR   0xAD    /* X-Mms-Message-Count          */
135 #define MM_CONTENT_HDR          0xAE    /* Content                      */
136 #define MM_START_HDR            0xAF    /* X-Mms-Start                  */
137 #define MM_ADDITIONAL_HDR       0xB0    /* Additional-headers           */
138 #define MM_DISTRIBUION_IND_HDR  0xB1    /* X-Mms-Distribution-Indcator  */
139 #define MM_ELEMENT_DESCR_HDR    0xB2    /* X-Mms-Element-Descriptor     */
140 #define MM_LIMIT_HDR            0xB3    /* X-Mms-Limit                  */
141
142 static const value_string vals_mm_header_names[] = {
143         /* MMS 1.0 */
144         { MM_BCC_HDR,                   "Bcc" },
145         { MM_CC_HDR,                    "Cc" },
146         { MM_CLOCATION_HDR,             "X-Mms-Content-Location" },
147         { MM_CTYPE_HDR,                 "X-Mms-Content-Type" },
148         { MM_DATE_HDR,                  "Date" },
149         { MM_DREPORT_HDR,               "X-Mms-Delivery-Report" },
150         { MM_DTIME_HDR,                 "X-Mms-Delivery-Time" },
151         { MM_EXPIRY_HDR,                "X-Mms-Expiry" },
152         { MM_FROM_HDR,                  "From" },
153         { MM_MCLASS_HDR,                "X-Mms-Message-Class" },
154         { MM_MID_HDR,                   "Message-ID" },
155         { MM_MTYPE_HDR,                 "X-Mms-Message-Type" },
156         { MM_VERSION_HDR,               "X-Mms-MMS-Version" },
157         { MM_MSIZE_HDR,                 "X-Mms-Message-Size" },
158         { MM_PRIORITY_HDR,              "X-Mms-Priority" },
159         { MM_RREPLY_HDR,                "X-Mms-Read-Reply" },
160         { MM_RALLOWED_HDR,              "X-Mms-Report-Allowed" },
161         { MM_RSTATUS_HDR,               "X-Mms-Response-Status" },
162         { MM_RTEXT_HDR,                 "X-Mms-Response-Text" },
163         { MM_SVISIBILITY_HDR,           "X-Mms-Sender-Visibility" },
164         { MM_STATUS_HDR,                "X-Mms-Status" },
165         { MM_SUBJECT_HDR,               "Subject" },
166         { MM_TO_HDR,                    "To" },
167         { MM_TID_HDR,                   "X-Mms-Transaction-Id" },
168         /* MMS 1.1 */
169         { MM_RETRIEVE_STATUS_HDR,       "X-Mms-Retrieve-Status" },
170         { MM_RETRIEVE_TEXT_HDR,         "X-Mms-Retrieve-Text" },
171         { MM_READ_STATUS_HDR,           "X-Mms-Read-Status" },
172         { MM_REPLY_CHARGING_HDR,        "X-Mms-Reply-Charging" },
173         { MM_REPLY_CHARGING_DEADLINE_HDR,
174                                         "X-Mms-Reply-Charging-Deadline" },
175         { MM_REPLY_CHARGING_ID_HDR,     "X-Mms-Reply-Charging-ID" },
176         { MM_REPLY_CHARGING_SIZE_HDR,   "X-Mms-Reply-Charging-Size" },
177         { MM_PREV_SENT_BY_HDR,          "X-Mms-Previously-Sent-By" },
178         { MM_PREV_SENT_DATE_HDR,        "X-Mms-Previously-Sent-Date" },
179         /* MMS 1.2 */
180         { MM_STORE_HDR,                 "X-Mms-Store" },
181         { MM_MM_STATE_HDR,              "X-Mms-MM-State " },
182         { MM_MM_FLAGS_HDR,              "X-Mms-MM-Flags " },
183         { MM_STORE_STATUS_HDR,          "X-Mms-Store-Status" },
184         { MM_STORE_STATUS_TEXT_HDR,     "X-Mms-Store-Status-Text" },
185         { MM_STORED_HDR,                "X-Mms-Stored" },
186         { MM_ATTRIBUTES_HDR,            "X-Mms-Attributes" },
187         { MM_TOTALS_HDR,                "X-Mms-Totals" },
188         { MM_MBOX_TOTALS_HDR,           "X-Mms-Mbox-Totals" },
189         { MM_QUOTAS_HDR,                "X-Mms-Quotas" },
190         { MM_MBOX_QUOTAS_HDR,           "X-Mms-Mbox-Quotas" },
191         { MM_MBOX_MSG_COUNT_HDR,        "X-Mms-Message-Count" },
192         { MM_CONTENT_HDR,               "Content" },
193         { MM_START_HDR,                 "X-Mms-Start" },
194         { MM_ADDITIONAL_HDR,            "Additional-headers" },
195         { MM_DISTRIBUION_IND_HDR,       "X-Mms-Distribution-Indcator" },
196         { MM_ELEMENT_DESCR_HDR,         "X-Mms-Element-Descriptor" },
197         { MM_LIMIT_HDR,                 "X-Mms-Limit" },
198
199         { 0x00, NULL },
200 };
201 /*
202  * Initialize the protocol and registered fields
203  */
204 static int proto_mmse = -1;
205
206 static int hf_mmse_message_type         = -1;
207 static int hf_mmse_transaction_id       = -1;
208 static int hf_mmse_mms_version          = -1;
209 static int hf_mmse_bcc                  = -1;
210 static int hf_mmse_cc                   = -1;
211 static int hf_mmse_content_location     = -1;
212 static int hf_mmse_date                 = -1;
213 static int hf_mmse_delivery_report      = -1;
214 static int hf_mmse_delivery_time_abs    = -1;
215 static int hf_mmse_delivery_time_rel    = -1;
216 static int hf_mmse_expiry_abs           = -1;
217 static int hf_mmse_expiry_rel           = -1;
218 static int hf_mmse_from                 = -1;
219 static int hf_mmse_message_class_id     = -1;
220 static int hf_mmse_message_class_str    = -1;
221 static int hf_mmse_message_id           = -1;
222 static int hf_mmse_message_size         = -1;
223 static int hf_mmse_priority             = -1;
224 static int hf_mmse_read_reply           = -1;
225 static int hf_mmse_report_allowed       = -1;
226 static int hf_mmse_response_status      = -1;
227 static int hf_mmse_response_text        = -1;
228 static int hf_mmse_sender_visibility    = -1;
229 static int hf_mmse_status               = -1;
230 static int hf_mmse_subject              = -1;
231 static int hf_mmse_to                   = -1;
232 static int hf_mmse_content_type         = -1;
233 static int hf_mmse_ffheader             = -1;
234 /* MMSE 1.1 */
235 static int hf_mmse_read_report          = -1;
236 static int hf_mmse_retrieve_status      = -1;
237 static int hf_mmse_retrieve_text        = -1;
238 static int hf_mmse_read_status          = -1;
239 static int hf_mmse_reply_charging       = -1;
240 static int hf_mmse_reply_charging_deadline      = -1;
241 static int hf_mmse_reply_charging_id    = -1;
242 static int hf_mmse_reply_charging_size  = -1;
243 static int hf_mmse_prev_sent_by = -1;
244 static int hf_mmse_prev_sent_by_fwd_count       = -1;
245 static int hf_mmse_prev_sent_by_address = -1;
246 static int hf_mmse_prev_sent_date       = -1;
247 static int hf_mmse_prev_sent_date_fwd_count     = -1;
248 static int hf_mmse_prev_sent_date_date  = -1;
249
250 /*
251  * Initialize the subtree pointers
252  */
253 static gint ett_mmse                    = -1;
254 static gint ett_mmse_hdr_details        = -1;
255
256 /*
257  * Valuestrings for PDU types
258  */
259 /* MMS 1.0 */
260 #define PDU_M_SEND_REQ          0x80
261 #define PDU_M_SEND_CONF         0x81
262 #define PDU_M_NOTIFICATION_IND  0x82
263 #define PDU_M_NOTIFYRESP_IND    0x83
264 #define PDU_M_RETRIEVE_CONF     0x84
265 #define PDU_M_ACKNOWLEDGE_IND   0x85
266 #define PDU_M_DELIVERY_IND      0x86
267 /* MMS 1.1 */
268 #define PDU_M_READ_REC_IND      0x87
269 #define PDU_M_READ_ORIG_IND     0x88
270 #define PDU_M_FORWARD_REQ       0x89
271 #define PDU_M_FORWARD_CONF      0x8A
272 /* MMS 1.2 */
273 #define PDU_M_MBOX_STORE_REQ    0x8B
274 #define PDU_M_MBOX_STORE_CONF   0x8C
275 #define PDU_M_MBOX_VIEW_REQ     0x8D
276 #define PDU_M_MBOX_VIEW_CONF    0x8E
277 #define PDU_M_MBOX_UPLOAD_REQ   0x8F
278 #define PDU_M_MBOX_UPLOAD_CONF  0x90
279 #define PDU_M_MBOX_DELETE_REQ   0x91
280 #define PDU_M_MBOX_DELETE_CONF  0x92
281 #define PDU_M_MBOX_DESCR        0x93
282
283 #define pdu_has_content(pdut) \
284         (  ((pdut) == PDU_M_SEND_REQ) \
285         || ((pdut) == PDU_M_DELIVERY_IND) \
286         || ((pdut) == PDU_M_RETRIEVE_CONF) \
287         || ((pdut) == PDU_M_MBOX_VIEW_CONF) \
288         || ((pdut) == PDU_M_MBOX_DESCR) \
289         || ((pdut) == PDU_M_MBOX_UPLOAD_REQ) \
290         )
291
292 static const value_string vals_message_type[] = {
293     /* MMS 1.0 */
294     { PDU_M_SEND_REQ,           "m-send-req" },
295     { PDU_M_SEND_CONF,          "m-send-conf" },
296     { PDU_M_NOTIFICATION_IND,   "m-notification-ind" },
297     { PDU_M_NOTIFYRESP_IND,     "m-notifyresp-ind" },
298     { PDU_M_RETRIEVE_CONF,      "m-retrieve-conf" },
299     { PDU_M_ACKNOWLEDGE_IND,    "m-acknowledge-ind" },
300     { PDU_M_DELIVERY_IND,       "m-delivery-ind" },
301     /* MMS 1.1 */
302     { PDU_M_READ_REC_IND,       "m-read-rec-ind" },
303     { PDU_M_READ_ORIG_IND,      "m-read-orig-ind" },
304     { PDU_M_FORWARD_REQ,        "m-forward-req" },
305     { PDU_M_FORWARD_CONF,       "m-forward-conf" },
306     /* MMS 1.2 */
307     { PDU_M_MBOX_STORE_REQ,     "m-mbox-store-req" },
308     { PDU_M_MBOX_STORE_CONF,    "m-mbox-store-conf" },
309     { PDU_M_MBOX_VIEW_REQ,      "m-mbox-view-req" },
310     { PDU_M_MBOX_VIEW_CONF,     "m-mbox-view-conf" },
311     { PDU_M_MBOX_UPLOAD_REQ,    "m-mbox-upload-req" },
312     { PDU_M_MBOX_UPLOAD_CONF,   "m-mbox-upload-conf" },
313     { PDU_M_MBOX_DELETE_REQ,    "m-mbox-delete-req" },
314     { PDU_M_MBOX_DELETE_CONF,   "m-mbox-delete-conf" },
315     { PDU_M_MBOX_DESCR,         "m-mbox-descr" },
316     { 0x00, NULL },
317 };
318
319 static const value_string vals_yes_no[] = {
320     { 0x80, "Yes" },
321     { 0x81, "No" },
322     { 0x00, NULL },
323 };
324
325 static const value_string vals_message_class[] = {
326     { 0x80, "Personal" },
327     { 0x81, "Advertisement" },
328     { 0x82, "Informational" },
329     { 0x83, "Auto" },
330     { 0x00, NULL },
331 };
332
333 static const value_string vals_priority[] = {
334     { 0x80, "Low" },
335     { 0x81, "Normal" },
336     { 0x82, "High" },
337     { 0x00, NULL },
338 };
339
340 static const value_string vals_response_status[] = {
341     /* MMS 1.0 - obsolete as from MMS 1.1 */
342     { 0x80, "Ok" },
343     { 0x81, "Unspecified" },
344     { 0x82, "Service denied" },
345     { 0x83, "Message format corrupt" },
346     { 0x84, "Sending address unresolved" },
347     { 0x85, "Message not found" },
348     { 0x86, "Network problem" },
349     { 0x87, "Content not accepted" },
350     { 0x88, "Unsupported message" },
351
352     /*
353      * Transient errors
354      */
355     /* MMS 1.1 */
356     { 0xC0, "Transient failure" },
357     { 0xC1, "Transient: Sending address unresolved" },
358     { 0xC2, "Transient: Message not found" },
359     { 0xC3, "Transient: Network problem" },
360     /* MMS 1.2 */
361     { 0xC4, "Transient: Partial success" },
362
363     /*
364      * Permanent errors
365      */
366     /* MMS 1.1 */
367     { 0xE0, "Permanent failure" },
368     { 0xE1, "Permanent: Service denied" },
369     { 0xE2, "Permanent: Message format corrupt" },
370     { 0xE3, "Permanent: Sending address unresolved" },
371     { 0xE4, "Permanent: Message not found" },
372     { 0xE5, "Permanent: Content not accepted" },
373     { 0xE6, "Permanent: Reply charging limitations not met" },
374     { 0xE7, "Permanent: Reply charging request not accepted" },
375     { 0xE8, "Permanent: Reply charging forwarding denied" },
376     { 0xE9, "Permanent: Reply charging not supported" },
377     /* MMS 1.2 */
378     { 0xEA, "Permanent: Address hiding not supported" },
379     
380     { 0x00, NULL },
381 };
382
383 static const value_string vals_sender_visibility[] = {
384     { 0x80, "Hide" },
385     { 0x81, "Show" },
386     { 0x00, NULL },
387 };
388
389 static const value_string vals_message_status[] = {
390     /* MMS 1.0 */
391     { 0x80, "Expired" },
392     { 0x81, "Retrieved" },
393     { 0x82, "Rejected" },
394     { 0x83, "Deferred" },
395     { 0x84, "Unrecognized" },
396     /* MMS 1.1 */
397     { 0x85, "Indeterminate" },
398     { 0x86, "Forwarded" },
399     /* MMS 1.2 */
400     { 0x87, "Unreachable" },
401     
402     { 0x00, NULL },
403 };
404
405 static const value_string vals_retrieve_status[] = {
406     /*
407      * Transient errors
408      */
409     /* MMS 1.1 */
410     { 0xC0, "Transient failure" },
411     { 0xC1, "Transient: Message not found" },
412     { 0xC2, "Transient: Network problem" },
413
414     /*
415      * Permanent errors
416      */
417     /* MMS 1.1 */
418     { 0xE0, "Permanent failure" },
419     { 0xE1, "Permanent: Service denied" },
420     { 0xE2, "Permanent: Message not found" },
421     { 0xE3, "Permanent: Content unsupported" },
422
423     { 0x00, NULL },
424 };
425
426 static const value_string vals_read_status[] = {
427     { 0x80, "Read" },
428     { 0x81, "Deleted without being read" },
429
430     { 0x00, NULL },
431 };
432
433 static const value_string vals_reply_charging[] = {
434     { 0x80, "Requested" },
435     { 0x81, "Requested text only" },
436     { 0x82, "Accepted" },
437     { 0x83, "Accepted text only" },
438
439     { 0x00, NULL },
440 };
441
442 static const value_string vals_reply_charging_deadline[] = {
443     { 0x80, "Absolute" },
444     { 0x81, "Relative" },
445
446     { 0x00, NULL },
447 };
448
449 /*!
450  * Decodes a Text-string from the protocol data
451  *      Text-string = [Quote] *TEXT End-of-string
452  *      Quote       = <Octet 127>
453  *      End-of-string = <Octet 0>
454  *
455  * \todo Shouldn't we be sharing this with WSP (packet-wap.c)?
456  *
457  * \param       tvb     The buffer with PDU-data
458  * \param       offset  Offset within that buffer
459  * \param       strval  Pointer to variable into which to put pointer to
460  *                      buffer allocated to hold the text; must be freed
461  *                      when no longer used
462  *
463  * \return              The length in bytes of the entire field
464  */
465 static guint
466 get_text_string(tvbuff_t *tvb, guint offset, char **strval)
467 {
468     guint        len;
469
470     DebugLog(("get_text_string(tvb = %p, offset = %u, **strval) - start\n", 
471                 tvb, offset));
472     len = tvb_strsize(tvb, offset);
473     DebugLog((" [1] tvb_strsize(tvb, offset) == %u\n", len));
474     if (tvb_get_guint8(tvb, offset) == MM_QUOTE)
475         *strval = (char *)tvb_memdup(tvb, offset + 1, len - 1);
476     else
477         *strval = (char *)tvb_memdup(tvb, offset, len);
478     DebugLog((" [3] Return(len) == %u\n", len));
479     return len;
480 }
481
482 /*!
483  * Decodes a Value-length from the protocol data.
484  *      Value-length = Short-length | (Length-quote Length)
485  *      Short-length = <Any octet 0-30>
486  *      Length-quote = <Octet 31>
487  *      Length       = Uintvar-integer
488  *
489  * \todo Shouldn't we be sharing this with WSP (packet-wap.c)?
490  *
491  * \param       tvb             The buffer with PDU-data
492  * \param       offset          Offset within that buffer
493  * \param       byte_count      Returns the length in bytes of
494  *                              the "Value-length" field.
495  *
496  * \return                      The actual value of "Value-length"
497  */
498 static guint
499 get_value_length(tvbuff_t *tvb, guint offset, guint *byte_count)
500 {
501     guint        field;
502
503     field = tvb_get_guint8(tvb, offset++);
504     if (field < 31)
505         *byte_count = 1;
506     else {                      /* Must be 31 so, Uintvar follows       */
507         field = tvb_get_guintvar(tvb, offset, byte_count);
508         (*byte_count)++;
509     }
510     return field;
511 }
512
513 /*!
514  * Decodes an Encoded-string-value from the protocol data
515  *      Encoded-string-value = Text-string | Value-length Char-set Text-string
516  *
517  * \param       tvb     The buffer with PDU-data
518  * \param       offset  Offset within that buffer
519  * \param       strval  Pointer to variable into which to put pointer to
520  *                      buffer allocated to hold the text; must be freed
521  *                      when no longer used
522  *
523  * \return              The length in bytes of the entire field
524  */
525 static guint
526 get_encoded_strval(tvbuff_t *tvb, guint offset, char **strval)
527 {
528     guint        field;
529     guint        length;
530     guint        count;
531
532     field = tvb_get_guint8(tvb, offset);
533
534     if (field < 32) {
535         length = get_value_length(tvb, offset, &count);
536         if (length < 2) {
537             *strval = g_strdup("");
538         } else {
539             /* \todo    Something with "Char-set", skip for now */
540             *strval = (char *)tvb_get_string(tvb, offset + count + 1, length - 1);
541         }
542         return count + length;
543     } else
544         return get_text_string(tvb, offset, strval);
545 }
546
547 /*!
548  * Decodes a Long-integer from the protocol data
549  *      Long-integer = Short-length Multi-octet-integer
550  *      Short-length = <Any octet 0-30>
551  *      Multi-octet-integer = 1*30OCTET
552  *
553  * \todo Shouldn't we be sharing this with WSP (packet-wap.c)?
554  *
555  * \param       tvb             The buffer with PDU-data
556  * \param       offset          Offset within that buffer
557  * \param       byte_count      Returns the length in bytes of the field
558  *
559  * \return                      The value of the Long-integer
560  *
561  * \note        A maximum of 4-byte integers will be handled.
562  */
563 static guint
564 get_long_integer(tvbuff_t *tvb, guint offset, guint *byte_count)
565 {
566     guint        val;
567
568     *byte_count = tvb_get_guint8(tvb, offset++);
569     switch (*byte_count) {
570         case 1:
571             val = tvb_get_guint8(tvb, offset);
572             break;
573         case 2:
574             val = tvb_get_ntohs(tvb, offset);
575             break;
576         case 3:
577             val = tvb_get_ntoh24(tvb, offset);
578             break;
579         case 4:
580             val = tvb_get_ntohl(tvb, offset);
581             break;
582         default:
583             val = 0;
584             break;
585     }
586     (*byte_count)++;
587     return val;
588 }
589
590 /*!
591  * Decodes an Integer-value from the protocol data
592  *      Integer-value = Short-integer | Long-integer
593  *      Short-integer = OCTET
594  *      Long-integer = Short-length Multi-octet-integer
595  *      Short-length = <Any octet 0-30>
596  *      Multi-octet-integer = 1*30OCTET
597  *
598  * \todo Shouldn't we be sharing this with WSP (packet-wap.c)?
599  *
600  * \param       tvb             The buffer with PDU-data
601  * \param       offset          Offset within that buffer
602  * \param       byte_count      Returns the length in bytes of the field
603  *
604  * \return                      The value of the Long-integer
605  *
606  * \note        A maximum of 4-byte integers will be handled.
607  */
608 static guint
609 get_integer_value(tvbuff_t *tvb, guint offset, guint *byte_count)
610 {
611     guint        val;
612     guint8 peek;
613
614     peek = tvb_get_guint8(tvb, offset++);
615     if (peek & 0x80) {
616         val = peek & 0x7F;
617         *byte_count = 1;
618         return val;
619     } else {
620         *byte_count = peek;
621         switch (peek) {
622         case 1:
623             val = tvb_get_guint8(tvb, offset);
624             break;
625         case 2:
626             val = tvb_get_ntohs(tvb, offset);
627             break;
628         case 3:
629             val = tvb_get_ntoh24(tvb, offset);
630             break;
631         case 4:
632             val = tvb_get_ntohl(tvb, offset);
633             break;
634         default:
635             val = 0;
636             break;
637         }
638     }
639     (*byte_count)++;
640     return val;
641 }
642
643 /* Code to actually dissect the packets */
644 static gboolean
645 dissect_mmse_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
646 {
647     guint8       pdut;
648
649         DebugLog(("dissect_mmse_heur()\n"));
650     /*
651      * Check if data makes sense for it to be dissected as MMSE:  Message-type
652      * field must make sense and followed by either Transaction-Id
653      * or MMS-Version header
654      */
655     if (tvb_get_guint8(tvb, 0) != MM_MTYPE_HDR)
656         return FALSE;
657     pdut = tvb_get_guint8(tvb, 1);
658     if (match_strval(pdut, vals_message_type) == NULL)
659         return FALSE;
660     if ((tvb_get_guint8(tvb, 2) != MM_TID_HDR) &&
661         (tvb_get_guint8(tvb, 2) != MM_VERSION_HDR))
662         return FALSE;
663     dissect_mmse_standalone(tvb, pinfo, tree);
664     return TRUE;
665 }
666
667 static void
668 dissect_mmse_standalone(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
669 {
670     guint8       pdut;
671     char         *message_type;
672
673     DebugLog(("dissect_mmse_standalone() - START (Packet %u)\n",
674                 pinfo->fd->num));
675
676     pdut = tvb_get_guint8(tvb, 1);
677     message_type = match_strval(pdut, vals_message_type);
678
679     /* Make entries in Protocol column and Info column on summary display */
680     if (check_col(pinfo->cinfo, COL_PROTOCOL))
681         col_set_str(pinfo->cinfo, COL_PROTOCOL, "MMSE");
682
683     if (check_col(pinfo->cinfo, COL_INFO)) {
684         col_clear(pinfo->cinfo, COL_INFO);
685         col_add_fstr(pinfo->cinfo, COL_INFO, "MMS %s", message_type);
686     }
687
688     dissect_mmse(tvb, pinfo, tree, pdut, message_type);
689 }
690
691 static void
692 dissect_mmse_encapsulated(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
693 {
694     guint8       pdut;
695     char         *message_type;
696
697     DebugLog(("dissect_mmse_encapsulated() - START (Packet %u)\n",
698                 pinfo->fd->num));
699
700     pdut = tvb_get_guint8(tvb, 1);
701     message_type = match_strval(pdut, vals_message_type);
702
703     /* Make entries in Info column on summary display */
704     if (check_col(pinfo->cinfo, COL_INFO)) {
705         col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(MMS %s)",
706                 message_type);
707     }
708
709     dissect_mmse(tvb, pinfo, tree, pdut, message_type);
710 }
711
712 static void
713 dissect_mmse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint8 pdut,
714         char *message_type)
715 {
716     guint        offset;
717     guint8       field = 0;
718     char         *strval;
719     guint        length;
720     guint        count;
721     guint8       version = 0x80; /* Default to MMSE 1.0 */
722
723     /* Set up structures needed to add the protocol subtree and manage it */
724     proto_item  *ti = NULL;
725     proto_tree  *mmse_tree = NULL;
726
727     DebugLog(("dissect_mmse() - START (Packet %u)\n", pinfo->fd->num));
728
729     /* If tree == NULL then we are only interested in protocol dissection
730      * up to reassembly and handoff to subdissectors if applicable; the
731      * columns must be set appropriately too.
732      * If tree != NULL then we also want to display the protocol tree
733      * with its fields.
734      * 
735      * In the interest of speed, skip protocol tree item generation
736      * if tree is NULL.
737      */
738     if (tree) {
739         DebugLog(("tree != NULL\n"));
740
741         ti = proto_tree_add_item(tree, proto_mmse, tvb, 0, -1, FALSE);
742         proto_item_append_text(ti, ", Type: %s", message_type);
743         /* create display subtree for the protocol */
744         mmse_tree = proto_item_add_subtree(ti, ett_mmse);
745
746         /* Report PDU-type      */
747         proto_tree_add_uint(mmse_tree, hf_mmse_message_type, tvb, 0, 2, pdut);
748     }
749
750     offset = 2;                 /* Skip Message-Type    */
751
752     /*
753      * Cycle through MMS-headers
754      *
755      * NOTE - some PDUs may convey content which can be handed off
756      *        to subdissectors.
757      */
758     if (tree || pdu_has_content(pdut)) {
759         while ((offset < tvb_reported_length(tvb)) &&
760                (field = tvb_get_guint8(tvb, offset++)) != MM_CTYPE_HDR)
761         {
762             DebugLog(("\tField =  0x%02X (offset = %u): %s\n",
763                         field, offset,
764                         val_to_str(field, vals_mm_header_names,
765                             "Unknown MMS header 0x%02X")));
766             switch (field)
767             {
768                 case MM_TID_HDR:                /* Text-string  */
769                     length = get_text_string(tvb, offset, &strval);
770                     if (tree) {
771                         proto_tree_add_string(mmse_tree, hf_mmse_transaction_id,
772                                 tvb, offset - 1, length + 1,strval);
773                     }
774                     g_free(strval);
775                     offset += length;
776                     break;
777                 case MM_VERSION_HDR:            /* nibble-Major/nibble-minor*/
778                     version = tvb_get_guint8(tvb, offset++);
779                     if (tree) {
780                         guint8   major, minor;
781
782                         major = (version & 0x70) >> 4;
783                         minor = version & 0x0F;
784                         if (minor == 0x0F)
785                             strval = g_strdup_printf("%u", major);
786                         else
787                             strval = g_strdup_printf("%u.%u", major, minor);
788                         proto_tree_add_string(mmse_tree, hf_mmse_mms_version,
789                                 tvb, offset - 2, 2, strval);
790                         g_free(strval);
791                     }
792                     break;
793                 case MM_BCC_HDR:                /* Encoded-string-value */
794                     length = get_encoded_strval(tvb, offset, &strval);
795                     if (tree) {
796                         proto_tree_add_string(mmse_tree, hf_mmse_bcc, tvb,
797                                 offset - 1, length + 1, strval);
798                     }
799                     g_free(strval);
800                     offset += length;
801                     break;
802                 case MM_CC_HDR:                 /* Encoded-string-value */
803                     length = get_encoded_strval(tvb, offset, &strval);
804                     if (tree) {
805                         proto_tree_add_string(mmse_tree, hf_mmse_cc, tvb,
806                                 offset - 1, length + 1, strval);
807                     }
808                     g_free(strval);
809                     offset += length;
810                     break;
811                 case MM_CLOCATION_HDR:          /* Uri-value            */
812                     if (pdut == PDU_M_MBOX_DELETE_CONF) {
813                         /* General form with length */
814                         length = tvb_get_guint8(tvb, offset);
815                         if (length == 0x1F) {
816                             guint length_len = 0;
817                             length = tvb_get_guintvar(tvb, offset + 1,
818                                     &length_len);
819                             length += 1 + length_len;
820                         } else {
821                             length += 1;
822                         }
823                         if (tree) {
824                             proto_tree_add_string(mmse_tree,
825                                     hf_mmse_content_location,
826                                     tvb, offset - 1, length + 1,
827                                     "<Undecoded value for m-mbox-delete-conf>");
828                         }
829                     } else {
830                         length = get_text_string(tvb, offset, &strval);
831                         if (tree) {
832                             proto_tree_add_string(mmse_tree,
833                                     hf_mmse_content_location,
834                                     tvb, offset - 1, length + 1, strval);
835                         }
836                         g_free(strval);
837                     }
838                     offset += length;
839                     break;
840                 case MM_DATE_HDR:               /* Long-integer         */
841                     {
842                         guint            tval;
843                         nstime_t         tmptime;
844
845                         tval = get_long_integer(tvb, offset, &count);
846                         tmptime.secs = tval;
847                         tmptime.nsecs = 0;
848                         if (tree) {
849                             proto_tree_add_time(mmse_tree, hf_mmse_date, tvb,
850                                     offset - 1, count + 1, &tmptime);
851                         }
852                     }
853                     offset += count;
854                     break;
855                 case MM_DREPORT_HDR:            /* Yes|No               */
856                     field = tvb_get_guint8(tvb, offset++);
857                     if (tree) {
858                         proto_tree_add_uint(mmse_tree,
859                                 hf_mmse_delivery_report,
860                                 tvb, offset - 2, 2, field);
861                     }
862                     break;
863                 case MM_DTIME_HDR:
864                     /*
865                      * Value-length(Absolute-token Date-value|
866                      *              Relative-token Delta-seconds-value)
867                      */
868                     length = get_value_length(tvb, offset, &count);
869                     field = tvb_get_guint8(tvb, offset + count);
870                     if (tree) {
871                         guint            tval;
872                         nstime_t         tmptime;
873                         guint            cnt;
874
875                         tval =  get_long_integer(tvb, offset + count + 1, &cnt);
876                         tmptime.secs = tval;
877                         tmptime.nsecs = 0;
878
879                         if (field == 0x80)
880                             proto_tree_add_time(mmse_tree,
881                                     hf_mmse_delivery_time_abs,
882                                     tvb, offset - 1,
883                                     length + count + 1, &tmptime);
884                         else
885                             proto_tree_add_time(mmse_tree,
886                                     hf_mmse_delivery_time_rel,
887                                     tvb, offset - 1,
888                                     length + count + 1, &tmptime);
889                     }
890                     offset += length + count;
891                     break;
892                 case MM_EXPIRY_HDR:
893                     /*
894                      * Value-length(Absolute-token Date-value|
895                      *              Relative-token Delta-seconds-value)
896                      */
897                     length = get_value_length(tvb, offset, &count);
898                     field = tvb_get_guint8(tvb, offset + count);
899                     if (tree) {
900                         guint            tval;
901                         nstime_t         tmptime;
902                         guint            cnt;
903
904                         tval = get_long_integer(tvb, offset + count + 1, &cnt);
905                         tmptime.secs = tval;
906                         tmptime.nsecs = 0;
907
908                         if (field == 0x80)
909                             proto_tree_add_time(mmse_tree, hf_mmse_expiry_abs,
910                                     tvb, offset - 1,
911                                     length + count + 1, &tmptime);
912                         else
913                             proto_tree_add_time(mmse_tree, hf_mmse_expiry_rel,
914                                     tvb, offset - 1,
915                                     length + count + 1, &tmptime);
916                     }
917                     offset += length + count;
918                     break;
919                 case MM_FROM_HDR:
920                     /*
921                      * Value-length(Address-present-token Encoded-string-value
922                      *              |Insert-address-token)
923                      */
924                     length = get_value_length(tvb, offset, &count);
925                     if (tree) {
926                         field = tvb_get_guint8(tvb, offset + count);
927                         if (field == 0x81) {
928                             proto_tree_add_string(mmse_tree, hf_mmse_from, tvb,
929                                     offset-1, length + count + 1,
930                                     "<insert address>");
931                         } else {
932                             (void) get_encoded_strval(tvb, offset + count + 1,
933                                                       &strval);
934                             proto_tree_add_string(mmse_tree, hf_mmse_from, tvb,
935                                     offset-1, length + count + 1, strval);
936                             g_free(strval);
937                         }
938                     }
939                     offset += length + count;
940                     break;
941                 case MM_MCLASS_HDR:
942                     /*
943                      * Class-identifier|Text-string
944                      */
945                     field = tvb_get_guint8(tvb, offset);
946                     if (field & 0x80) {
947                         offset++;
948                         if (tree) {
949                             proto_tree_add_uint(mmse_tree,
950                                     hf_mmse_message_class_id,
951                                     tvb, offset - 2, 2, field);
952                         }
953                     } else {
954                         length = get_text_string(tvb, offset, &strval);
955                         if (tree) {
956                             proto_tree_add_string(mmse_tree,
957                                     hf_mmse_message_class_str,
958                                     tvb, offset - 1, length + 1,
959                                     strval);
960                         }
961                         g_free(strval);
962                         offset += length;
963                     }
964                     break;
965                 case MM_MID_HDR:                /* Text-string          */
966                     length = get_text_string(tvb, offset, &strval);
967                     if (tree) {
968                         proto_tree_add_string(mmse_tree, hf_mmse_message_id,
969                                 tvb, offset - 1, length + 1, strval);
970                     }
971                     g_free(strval);
972                     offset += length;
973                     break;
974                 case MM_MSIZE_HDR:              /* Long-integer         */
975                     length = get_long_integer(tvb, offset, &count);
976                     if (tree) {
977                         proto_tree_add_uint(mmse_tree, hf_mmse_message_size,
978                                 tvb, offset - 1, count + 1, length);
979                     }
980                     offset += count;
981                     break;
982                 case MM_PRIORITY_HDR:           /* Low|Normal|High      */
983                     field = tvb_get_guint8(tvb, offset++);
984                     if (tree) {
985                         proto_tree_add_uint(mmse_tree, hf_mmse_priority, tvb,
986                                 offset - 2, 2, field);
987                     }
988                     break;
989                 case MM_RREPLY_HDR:             /* Yes|No               */
990                     field = tvb_get_guint8(tvb, offset++);
991                     if (tree) {
992                         if (version == 0x80) { /* MMSE 1.0 */
993                             proto_tree_add_uint(mmse_tree, hf_mmse_read_reply,
994                                     tvb, offset - 2, 2, field);
995                         } else {
996                             proto_tree_add_uint(mmse_tree, hf_mmse_read_report,
997                                     tvb, offset - 2, 2, field);
998                         }
999                     }
1000                     break;
1001                 case MM_RALLOWED_HDR:           /* Yes|No               */
1002                     field = tvb_get_guint8(tvb, offset++);
1003                     if (tree) {
1004                         proto_tree_add_uint(mmse_tree, hf_mmse_report_allowed,
1005                                 tvb, offset - 2, 2, field);
1006                     }
1007                     break;
1008                 case MM_RSTATUS_HDR:
1009                     field = tvb_get_guint8(tvb, offset++);
1010                     if (tree) {
1011                         proto_tree_add_uint(mmse_tree, hf_mmse_response_status,
1012                                 tvb, offset - 2, 2, field);
1013                     }
1014                     break;
1015                 case MM_RTEXT_HDR:              /* Encoded-string-value */
1016                     if (pdut == PDU_M_MBOX_DELETE_CONF) {
1017                         /* General form with length */
1018                         length = tvb_get_guint8(tvb, offset);
1019                         if (length == 0x1F) {
1020                             guint length_len = 0;
1021                             length = tvb_get_guintvar(tvb, offset + 1,
1022                                     &length_len);
1023                             length += 1 + length_len;
1024                         } else {
1025                             length += 1;
1026                         }
1027                         if (tree) {
1028                             proto_tree_add_string(mmse_tree,
1029                                     hf_mmse_content_location,
1030                                     tvb, offset - 1, length + 1,
1031                                     "<Undecoded value for m-mbox-delete-conf>");
1032                         }
1033                     } else {
1034                         length = get_encoded_strval(tvb, offset, &strval);
1035                         if (tree) {
1036                             proto_tree_add_string(mmse_tree,
1037                                     hf_mmse_response_text, tvb, offset - 1,
1038                                     length + 1, strval);
1039                         }
1040                         g_free(strval);
1041                     }
1042                     offset += length;
1043                     break;
1044                 case MM_SVISIBILITY_HDR:        /* Hide|Show            */
1045                     field = tvb_get_guint8(tvb, offset++);
1046                     if (tree) {
1047                         proto_tree_add_uint(mmse_tree,hf_mmse_sender_visibility,
1048                                 tvb, offset - 2, 2, field);
1049                     }
1050                     break;
1051                 case MM_STATUS_HDR:
1052                     field = tvb_get_guint8(tvb, offset++);
1053                     if (tree) {
1054                         proto_tree_add_uint(mmse_tree, hf_mmse_status, tvb,
1055                                 offset - 2, 2, field);
1056                     }
1057                     break;
1058                 case MM_SUBJECT_HDR:            /* Encoded-string-value */
1059                     length = get_encoded_strval(tvb, offset, &strval);
1060                     if (tree) {
1061                         proto_tree_add_string(mmse_tree, hf_mmse_subject, tvb,
1062                                 offset - 1, length + 1, strval);
1063                     }
1064                     g_free(strval);
1065                     offset += length;
1066                     break;
1067                 case MM_TO_HDR:                 /* Encoded-string-value */
1068                     length = get_encoded_strval(tvb, offset, &strval);
1069                     if (tree) {
1070                         proto_tree_add_string(mmse_tree, hf_mmse_to, tvb,
1071                                 offset - 1, length + 1, strval);
1072                     }
1073                     g_free(strval);
1074                     offset += length;
1075                     break;
1076
1077                 /*
1078                  * MMS Encapsulation 1.1
1079                  */
1080                 case MM_RETRIEVE_STATUS_HDR:    /* Well-known-value */
1081                     field = tvb_get_guint8(tvb, offset++);
1082                     if (tree) {
1083                         proto_tree_add_uint(mmse_tree, hf_mmse_retrieve_status,
1084                                 tvb, offset - 2, 2, field);
1085                     }
1086                     break;
1087                 case MM_RETRIEVE_TEXT_HDR:
1088                     if (pdut == PDU_M_MBOX_DELETE_CONF) {
1089                         /* General form with length */
1090                         length = tvb_get_guint8(tvb, offset);
1091                         if (length == 0x1F) {
1092                             guint length_len = 0;
1093                             length = tvb_get_guintvar(tvb, offset + 1,
1094                                     &length_len);
1095                             length += 1 + length_len;
1096                         } else {
1097                             length += 1;
1098                         }
1099                         if (tree) {
1100                             proto_tree_add_string(mmse_tree,
1101                                     hf_mmse_content_location,
1102                                     tvb, offset - 1, length + 1,
1103                                     "<Undecoded value for m-mbox-delete-conf>");
1104                         }
1105                     } else {
1106                         /* Encoded-string-value */
1107                         length = get_encoded_strval(tvb, offset, &strval);
1108                         if (tree) {
1109                             proto_tree_add_string(mmse_tree,
1110                                     hf_mmse_retrieve_text, tvb, offset - 1,
1111                                     length + 1, strval);
1112                         }
1113                         g_free(strval);
1114                     }
1115                     offset += length;
1116                     break;
1117                 case MM_READ_STATUS_HDR:        /* Well-known-value */
1118                     field = tvb_get_guint8(tvb, offset++);
1119                     if (tree) {
1120                         proto_tree_add_uint(mmse_tree, hf_mmse_read_status,
1121                                 tvb, offset - 2, 2, field);
1122                     }
1123                     break;
1124                 case MM_REPLY_CHARGING_HDR:     /* Well-known-value */
1125                     field = tvb_get_guint8(tvb, offset++);
1126                     if (tree) {
1127                         proto_tree_add_uint(mmse_tree, hf_mmse_reply_charging,
1128                                 tvb, offset - 2, 2, field);
1129                     }
1130                     break;
1131                 case MM_REPLY_CHARGING_DEADLINE_HDR:    /* Well-known-value */
1132                     field = tvb_get_guint8(tvb, offset++);
1133                     if (tree) {
1134                         proto_tree_add_uint(mmse_tree,
1135                                 hf_mmse_reply_charging_deadline,
1136                                 tvb, offset - 2, 2, field);
1137                     }
1138                     break;
1139                 case MM_REPLY_CHARGING_ID_HDR:  /* Text-string */
1140                     length = get_text_string(tvb, offset, &strval);
1141                     if (tree) {
1142                         proto_tree_add_string(mmse_tree,
1143                                 hf_mmse_reply_charging_id,
1144                                 tvb, offset - 1, length + 1, strval);
1145                     }
1146                     g_free(strval);
1147                     offset += length;
1148                     break;
1149                 case MM_REPLY_CHARGING_SIZE_HDR:        /* Long-integer */
1150                     length = get_long_integer(tvb, offset, &count);
1151                     if (tree) {
1152                         proto_tree_add_uint(mmse_tree,
1153                                 hf_mmse_reply_charging_size,
1154                                 tvb, offset - 1, count + 1, length);
1155                     }
1156                     offset += count;
1157                     break;
1158                 case MM_PREV_SENT_BY_HDR:
1159                     /* Value-length Integer-value Encoded-string-value */
1160                     length = get_value_length(tvb, offset, &count);
1161                     if (tree) {
1162                         guint32 fwd_count, count1, count2;                      
1163                         proto_tree *subtree = NULL;
1164                         proto_item *ti = NULL;
1165                         /* 1. Forwarded-count-value := Integer-value */
1166                         fwd_count = get_integer_value(tvb, offset + count,
1167                             &count1);
1168                         /* 2. Encoded-string-value */
1169                         count2 = get_encoded_strval(tvb,
1170                                 offset + count + count1, &strval);
1171                         /* Now render the fields */
1172                         ti = proto_tree_add_string_format(mmse_tree,
1173                                 hf_mmse_prev_sent_by,
1174                                 tvb, offset - 1, 1 + count + length,
1175                                 strval, "%s (Forwarded-count=%u)",
1176                                 format_text(strval, strlen(strval)),
1177                                 fwd_count);
1178                         subtree = proto_item_add_subtree(ti,
1179                                 ett_mmse_hdr_details);
1180                         proto_tree_add_uint(subtree,
1181                                 hf_mmse_prev_sent_by_fwd_count,
1182                                 tvb, offset + count, count1, fwd_count);
1183                         proto_tree_add_string(subtree,
1184                                 hf_mmse_prev_sent_by_address,
1185                                 tvb, offset + count + count1, count2, strval);
1186                         g_free(strval);
1187                     }
1188                     offset += length + count;
1189                     break;
1190                 case MM_PREV_SENT_DATE_HDR:
1191                     /* Value-Length Forwarded-count-value Date-value */
1192                     length = get_value_length(tvb, offset, &count);
1193                     if (tree) {
1194                         guint32 fwd_count, count1, count2;                      
1195                         guint            tval;
1196                         nstime_t         tmptime;
1197                         proto_tree *subtree = NULL;
1198                         proto_item *ti = NULL;
1199                         /* 1. Forwarded-count-value := Integer-value */
1200                         fwd_count = get_integer_value(tvb, offset + count,
1201                             &count1);
1202                         /* 2. Date-value := Long-integer */
1203                         tval = get_long_integer(tvb, offset + count + count1,
1204                                 &count2);
1205                         tmptime.secs = tval;
1206                         tmptime.nsecs = 0;
1207                         strval = abs_time_to_str(&tmptime);
1208                         /* Now render the fields */
1209                         ti = proto_tree_add_string_format(mmse_tree,
1210                                 hf_mmse_prev_sent_date,
1211                                 tvb, offset - 1, 1 + count + length,
1212                                 strval, "%s (Forwarded-count=%u)",
1213                                 format_text(strval, strlen(strval)),
1214                                 fwd_count);
1215                         subtree = proto_item_add_subtree(ti,
1216                                 ett_mmse_hdr_details);
1217                         proto_tree_add_uint(subtree,
1218                                 hf_mmse_prev_sent_date_fwd_count,
1219                                 tvb, offset + count, count1, fwd_count);
1220                         proto_tree_add_string(subtree,
1221                                 hf_mmse_prev_sent_date_date,
1222                                 tvb, offset + count + count1, count2, strval);
1223                     }
1224                     offset += length + count;
1225                     break;
1226
1227                 /* MMS Encapsulation 1.2 */
1228
1229                 default:
1230                     if (field & 0x80) { /* Well-known WSP header encoding */
1231                         guint8 peek = tvb_get_guint8(tvb, offset);
1232                         char *hdr_name = val_to_str(field, vals_mm_header_names,
1233                                 "Unknown field (0x%02x)");
1234                         DebugLog(("\t\tUndecoded well-known header: %s\n",
1235                                     hdr_name));
1236
1237                         if (peek & 0x80) { /* Well-known value */
1238                             length = 1;
1239                             if (tree) {
1240                                 proto_tree_add_text(mmse_tree, tvb, offset - 1,
1241                                         length + 1,
1242                                         "%s: <Well-known value 0x%02x>"
1243                                         " (not decoded)",
1244                                         hdr_name, peek);
1245                             }
1246                         } else if ((peek == 0) || (peek >= 0x20)) { /* Text */
1247                             length = get_text_string(tvb, offset, &strval);
1248                             if (tree) {
1249                                 proto_tree_add_text(mmse_tree, tvb, offset - 1,
1250                                         length + 1, "%s: %s (Not decoded)",
1251                                         hdr_name,
1252                                         format_text(strval, strlen(strval)));
1253                             }
1254                             g_free(strval);
1255                         } else { /* General form with length */
1256                             if (peek == 0x1F) { /* Value length in guintvar */
1257                                 guint length_len = 0;
1258                                 length = 1 + tvb_get_guintvar(tvb, offset + 1,
1259                                         &length_len);
1260                                 length += length_len;
1261                             } else { /* Value length in octet */
1262                                 length = 1 + tvb_get_guint8(tvb, offset);
1263                             }
1264                             if (tree) {
1265                                 proto_tree_add_text(mmse_tree, tvb, offset - 1,
1266                                         length + 1, "%s: "
1267                                         "<Value in general form> (not decoded)",
1268                                         hdr_name);
1269                             }
1270                         }
1271                         offset += length;
1272                     } else { /* Literal WSP header encoding */
1273                         guint    length2;
1274                         char     *strval2;
1275
1276                         --offset;
1277                         length = get_text_string(tvb, offset, &strval);
1278                         DebugLog(("\t\tUndecoded literal header: %s\n",
1279                                     strval));
1280                         CLEANUP_PUSH(g_free, strval);
1281                         length2= get_text_string(tvb, offset+length, &strval2);
1282
1283                         if (tree) {
1284                             proto_tree_add_string_format(mmse_tree,
1285                                     hf_mmse_ffheader, tvb, offset,
1286                                     length + length2,
1287                                     (const char *) tvb_get_ptr(
1288                                             tvb, offset, length + length2),
1289                                     "%s: %s",
1290                                     format_text(strval, strlen(strval)),
1291                                     format_text(strval2, strlen(strval2)));
1292                         }
1293                         g_free(strval2);
1294                         offset += length + length2;
1295                         CLEANUP_CALL_AND_POP;
1296                     }
1297                     break;
1298             }
1299             DebugLog(("\tEnd(case)\n"));
1300         }
1301         DebugLog(("\tEnd(switch)\n"));
1302         if (field == MM_CTYPE_HDR) {
1303             /*
1304              * Eeehh, we're now actually back to good old WSP content-type
1305              * encoding. Let's steal that from the WSP-dissector.
1306              */
1307             tvbuff_t    *tmp_tvb;
1308             guint        type;
1309             const char  *type_str;
1310
1311             DebugLog(("Content-Type: [from WSP dissector]\n"));
1312             DebugLog(("Calling add_content_type() in WSP dissector\n"));
1313             offset = add_content_type(mmse_tree, tvb, offset, &type, &type_str);
1314             DebugLog(("Generating new TVB subset (offset = %u)\n", offset));
1315             tmp_tvb = tvb_new_subset(tvb, offset, -1, -1);
1316             DebugLog(("Add POST data\n"));
1317             add_post_data(mmse_tree, tmp_tvb, type, type_str, pinfo);
1318             DebugLog(("Done!\n"));
1319         }
1320     } else {
1321         DebugLog(("tree == NULL and PDU has no potential content\n"));
1322     }
1323
1324     /* If this protocol has a sub-dissector call it here, see section 1.8 */
1325     DebugLog(("dissect_mmse() - END\n"));
1326 }
1327
1328
1329 /* Register the protocol with Ethereal */
1330
1331 /* this format is required because a script is used to build the C function
1332  * that calls all the protocol registration.
1333  */
1334 void
1335 proto_register_mmse(void)
1336 {
1337     /* Setup list of header fields  See Section 1.6.1 for details       */
1338     static hf_register_info hf[] = {
1339         {   &hf_mmse_message_type,
1340             {   "X-Mms-Message-Type", "mmse.message_type",
1341                 FT_UINT8, BASE_HEX, VALS(vals_message_type), 0x00,
1342                 "Specifies the transaction type. Effectively defines PDU.",
1343                 HFILL
1344             }
1345         },
1346         {   &hf_mmse_transaction_id,
1347             {   "X-Mms-Transaction-ID", "mmse.transaction_id",
1348                 FT_STRING, BASE_NONE, NULL, 0x00,
1349                 "A unique identifier for this transaction. "
1350                 "Identifies request and corresponding response only.",
1351                 HFILL
1352             }
1353         },
1354         {   &hf_mmse_mms_version,
1355             {   "X-Mms-MMS-Version", "mmse.mms_version",
1356                 FT_STRING, BASE_NONE, NULL, 0x00,
1357                 "Version of the protocol used.",
1358                 HFILL
1359             }
1360         },
1361         {   &hf_mmse_bcc,
1362             {   "Bcc", "mmse.bcc",
1363                 FT_STRING, BASE_NONE, NULL, 0x00,
1364                 "Blind carbon copy.",
1365                 HFILL
1366             }
1367         },
1368         {   &hf_mmse_cc,
1369             {   "Cc", "mmse.cc",
1370                 FT_STRING, BASE_NONE, NULL, 0x00,
1371                 "Carbon copy.",
1372                 HFILL
1373             }
1374         },
1375         {   &hf_mmse_content_location,
1376             {   "X-Mms-Content-Location", "mmse.content_location",
1377                 FT_STRING, BASE_NONE, NULL, 0x00,
1378                 "Defines the location of the message.",
1379                 HFILL
1380             }
1381         },
1382         {   &hf_mmse_date,
1383             {   "Date", "mmse.date",
1384                 FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x00,
1385                 "Arrival timestamp of the message or sending timestamp.",
1386                 HFILL
1387             }
1388         },
1389         {   &hf_mmse_delivery_report,
1390             {   "X-Mms-Delivery-Report", "mmse.delivery_report",
1391                 FT_UINT8, BASE_HEX, VALS(vals_yes_no), 0x00,
1392                 "Whether a report of message delivery is wanted or not.",
1393                 HFILL
1394             }
1395         },
1396         {   &hf_mmse_delivery_time_abs,
1397             {   "X-Mms-Delivery-Time", "mmse.delivery_time.abs",
1398                 FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x00,
1399                 "The time at which message delivery is desired.",
1400                 HFILL
1401             }
1402         },
1403         {   &hf_mmse_delivery_time_rel,
1404             {   "X-Mms-Delivery-Time", "mmse.delivery_time.rel",
1405                 FT_RELATIVE_TIME, BASE_NONE, NULL, 0x00,
1406                 "The desired message delivery delay.",
1407                 HFILL
1408             }
1409         },
1410         {   &hf_mmse_expiry_abs,
1411             {   "X-Mms-Expiry", "mmse.expiry.abs",
1412                 FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x00,
1413                 "Time when message expires and need not be delivered anymore.",
1414                 HFILL
1415             }
1416         },
1417         {   &hf_mmse_expiry_rel,
1418             {   "X-Mms-Expiry", "mmse.expiry.rel",
1419                 FT_RELATIVE_TIME, BASE_NONE, NULL, 0x00,
1420                 "Delay before message expires and need not be delivered anymore.",
1421                 HFILL
1422             }
1423         },
1424         {   &hf_mmse_from,
1425             {   "From", "mmse.from",
1426                 FT_STRING, BASE_NONE, NULL, 0x00,
1427                 "Address of the message sender.",
1428                 HFILL
1429             }
1430         },
1431         {   &hf_mmse_message_class_id,
1432             {   "X-Mms-Message-Class", "mmse.message_class.id",
1433                 FT_UINT8, BASE_HEX, VALS(vals_message_class), 0x00,
1434                 "Of what category is the message.",
1435                 HFILL
1436             }
1437         },
1438         {   &hf_mmse_message_class_str,
1439             {   "X-Mms-Message-Class", "mmse.message_class.str",
1440                 FT_STRING, BASE_NONE, NULL, 0x00,
1441                 "Of what category is the message.",
1442                 HFILL
1443             }
1444         },
1445         {   &hf_mmse_message_id,
1446             {   "Message-Id", "mmse.message_id",
1447                 FT_STRING, BASE_NONE, NULL, 0x00,
1448                 "Unique identification of the message.",
1449                 HFILL
1450             }
1451         },
1452         {   &hf_mmse_message_size,
1453             {   "X-Mms-Message-Size", "mmse.message_size",
1454                 FT_UINT32, BASE_DEC, NULL, 0x00,
1455                 "The size of the message in octets.",
1456                 HFILL
1457             }
1458         },
1459         {   &hf_mmse_priority,
1460             {   "X-Mms-Priority", "mmse.priority",
1461                 FT_UINT8, BASE_HEX, VALS(vals_priority), 0x00,
1462                 "Priority of the message.",
1463                 HFILL
1464             }
1465         },
1466         {   &hf_mmse_read_reply,
1467             {   "X-Mms-Read-Reply", "mmse.read_reply",
1468                 FT_UINT8, BASE_HEX, VALS(vals_yes_no), 0x00,
1469                 "Whether a read report from every recipient is wanted.",
1470                 HFILL
1471             }
1472         },
1473         {   &hf_mmse_read_report,
1474             {   "X-Mms-Read-Report", "mmse.read_report",
1475                 FT_UINT8, BASE_HEX, VALS(vals_yes_no), 0x00,
1476                 "Whether a read report from every recipient is wanted.",
1477                 HFILL
1478             }
1479         },
1480         {   &hf_mmse_report_allowed,
1481             {   "X-Mms-Report-Allowed", "mmse.report_allowed",
1482                 FT_UINT8, BASE_HEX, VALS(vals_yes_no), 0x00,
1483                 "Sending of delivery report allowed or not.",
1484                 HFILL
1485             }
1486         },
1487         {   &hf_mmse_response_status,
1488             {   "Response-Status", "mmse.response_status",
1489                 FT_UINT8, BASE_HEX, VALS(vals_response_status), 0x00,
1490                 "MMS-specific result of a message submission or retrieval.",
1491                 HFILL
1492             }
1493         },
1494         {   &hf_mmse_response_text,
1495             {   "Response-Text", "mmse.response_text",
1496                 FT_STRING, BASE_NONE, NULL, 0x00,
1497                 "Additional information on MMS-specific result.",
1498                 HFILL
1499             }
1500         },
1501         {   &hf_mmse_sender_visibility,
1502             {   "Sender-Visibility", "mmse.sender_visibility",
1503                 FT_UINT8, BASE_HEX, VALS(vals_sender_visibility), 0x00,
1504                 "Disclose sender identity to receiver or not.",
1505                 HFILL
1506             }
1507         },
1508         {   &hf_mmse_status,
1509             {   "Status", "mmse.status",
1510                 FT_UINT8, BASE_HEX, VALS(vals_message_status), 0x00,
1511                 "Current status of the message.",
1512                 HFILL
1513             }
1514         },
1515         {   &hf_mmse_subject,
1516             {   "Subject", "mmse.subject",
1517                 FT_STRING, BASE_NONE, NULL, 0x00,
1518                 "Subject of the message.",
1519                 HFILL
1520             }
1521         },
1522         {   &hf_mmse_to,
1523             {   "To", "mmse.to",
1524                 FT_STRING, BASE_NONE, NULL, 0x00,
1525                 "Recipient(s) of the message.",
1526                 HFILL
1527             }
1528         },
1529         {   &hf_mmse_content_type,
1530             {   "Data", "mmse.content_type",
1531                 FT_NONE, BASE_NONE, NULL, 0x00,
1532                 "Media content of the message.",
1533                 HFILL
1534             }
1535         },
1536         {   &hf_mmse_ffheader,
1537             {   "Free format (not encoded) header", "mmse.ffheader",
1538                 FT_STRING, BASE_NONE, NULL, 0x00,
1539                 "Application header without corresponding encoding.",
1540                 HFILL
1541             }
1542         },
1543         /* MMSE 1.1 */
1544         {   &hf_mmse_retrieve_status,
1545             {   "X-Mms-Retrieve-Status", "mmse.retrieve_status",
1546                 FT_UINT8, BASE_HEX, VALS(vals_retrieve_status), 0x00,
1547                 "MMS-specific result of a message retrieval.",
1548                 HFILL
1549             }
1550         },
1551         {   &hf_mmse_retrieve_text,
1552             {   "X-Mms-Retrieve-Text", "mmse.retrieve_text",
1553                 FT_STRING, BASE_NONE, NULL, 0x00,
1554                 "Status text of a MMS message retrieval.",
1555                 HFILL
1556             }
1557         },
1558         {   &hf_mmse_read_status,
1559             {   "X-Mms-Read-Status", "mmse.read_status",
1560                 FT_UINT8, BASE_HEX, VALS(vals_read_status), 0x00,
1561                 "MMS-specific message read status.",
1562                 HFILL
1563             }
1564         },
1565         {   &hf_mmse_reply_charging,
1566             {   "X-Mms-Reply-Charging", "mmse.reply_charging",
1567                 FT_UINT8, BASE_HEX, VALS(vals_reply_charging), 0x00,
1568                 "MMS-specific message reply charging method.",
1569                 HFILL
1570             }
1571         },
1572         {   &hf_mmse_reply_charging_deadline,
1573             {   "X-Mms-Reply-Charging-Deadline", "mmse.reply_charging_deadline",
1574                 FT_UINT8, BASE_HEX, VALS(vals_reply_charging_deadline), 0x00,
1575                 "MMS-specific message reply charging deadline type.",
1576                 HFILL
1577             }
1578         },
1579         {   &hf_mmse_reply_charging_id,
1580             {   "X-Mms-Reply-Charging-Id", "mmse.reply_charging_id",
1581                 FT_STRING, BASE_NONE, NULL, 0x00,
1582                 "Unique reply charging identification of the message.",
1583                 HFILL
1584             }
1585         },
1586         {   &hf_mmse_reply_charging_size,
1587             {   "X-Mms-Reply-Charging-Size", "mmse.reply_charging_size",
1588                 FT_UINT32, BASE_DEC, NULL, 0x00,
1589                 "The size of the reply charging in octets.",
1590                 HFILL
1591             }
1592         },
1593         {   &hf_mmse_prev_sent_by,
1594             {   "X-Mms-Previously-Sent-By", "mmse.previously_sent_by",
1595                 FT_STRING, BASE_NONE, NULL, 0x00,
1596                 "Indicates that the MM has been previously sent by this user.",
1597                 HFILL
1598             }
1599         },
1600         {   &hf_mmse_prev_sent_by_fwd_count,
1601             {   "Forward Count", "mmse.previously_sent_by.forward_count",
1602                 FT_UINT32, BASE_DEC, NULL, 0x00,
1603                 "Forward count of the previously sent MM.",
1604                 HFILL
1605             }
1606         },
1607         {   &hf_mmse_prev_sent_by_address,
1608             {   "Address", "mmse.previously_sent_by.address",
1609                 FT_STRING, BASE_NONE, NULL, 0x00,
1610                 "Indicates from whom the MM has been previously sent.",
1611                 HFILL
1612             }
1613         },
1614         {   &hf_mmse_prev_sent_date,
1615             {   "X-Mms-Previously-Sent-Date", "mmse.previously_sent_date",
1616                 FT_STRING, BASE_NONE, NULL, 0x00,
1617                 "Indicates the date that the MM has been previously sent.",
1618                 HFILL
1619             }
1620         },
1621         {   &hf_mmse_prev_sent_date_fwd_count,
1622             {   "Forward Count", "mmse.previously_sent_date.forward_count",
1623                 FT_UINT32, BASE_DEC, NULL, 0x00,
1624                 "Forward count of the previously sent MM.",
1625                 HFILL
1626             }
1627         },
1628         {   &hf_mmse_prev_sent_date_date,
1629             {   "Date", "mmse.previously_sent_date.date",
1630                 FT_STRING, BASE_NONE, NULL, 0x00,
1631                 "Time when the MM has been previously sent.",
1632                 HFILL
1633             }
1634         },
1635
1636
1637
1638     };
1639     /* Setup protocol subtree array */
1640     static gint *ett[] = {
1641         &ett_mmse,
1642         &ett_mmse_hdr_details,
1643     };
1644
1645     /* Register the protocol name and description */
1646     proto_mmse = proto_register_protocol("MMS Message Encapsulation",
1647                                          "MMSE", "mmse");
1648
1649     /* Required function calls to register header fields and subtrees used */
1650     proto_register_field_array(proto_mmse, hf, array_length(hf));
1651     proto_register_subtree_array(ett, array_length(ett));
1652 }
1653
1654 /* If this dissector uses sub-dissector registration add registration routine.
1655  * This format is required because a script is used to find these routines and
1656  * create the code that calls these routines.
1657  */
1658 void
1659 proto_reg_handoff_mmse(void)
1660 {
1661     dissector_handle_t mmse_standalone_handle;
1662     dissector_handle_t mmse_encapsulated_handle;
1663
1664     heur_dissector_add("wsp", dissect_mmse_heur, proto_mmse);
1665     mmse_standalone_handle = create_dissector_handle(
1666             dissect_mmse_standalone, proto_mmse);
1667     mmse_encapsulated_handle = create_dissector_handle(
1668             dissect_mmse_encapsulated, proto_mmse);
1669         /* As the media types for WSP and HTTP are the same, the WSP dissector
1670          * uses the same string dissector table as the HTTP protocol. */
1671     dissector_add_string("media_type",
1672             "application/vnd.wap.mms-message", mmse_standalone_handle);
1673     dissector_add_string("multipart_media_type",
1674             "application/vnd.wap.mms-message", mmse_encapsulated_handle);
1675 }