WAP support, from Neil Hunter.
[obnox/wireshark/wip.git] / packet-wsp.c
1 /* packet-wsp.c (c) 2000 Neil Hunter
2  * Based on original work by Ben Fowler
3  *
4  * Routines to dissect WSP component of WAP traffic.
5  * 
6  * $Id: packet-wsp.c,v 1.1 2000/11/04 03:30:40 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Didier Jorand
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #ifdef NEED_SNPRINTF_H
43 # ifdef HAVE_STDARG_H
44 #  include <stdarg.h>
45 # else
46 #  include <varargs.h>
47 # endif
48 # include "snprintf.h"
49 #endif
50
51 #include <string.h>
52 #include <glib.h>
53 #include "packet.h"
54 #include "packet-wap.h"
55 #include "packet-wsp.h"
56
57 /* File scoped variables for the protocol and registered fields */
58 static int proto_wsp                                                    = HF_EMPTY;
59
60 /* These fields used by fixed part of header */
61 static int hf_wsp_header_tid                                    = HF_EMPTY;
62 static int hf_wsp_header_pdu_type                               = HF_EMPTY;
63 static int hf_wsp_version_major                                 = HF_EMPTY;
64 static int hf_wsp_version_minor                                 = HF_EMPTY;
65 static int hf_wsp_capability_length                             = HF_EMPTY;
66 static int hf_wsp_capabilities_section                  = HF_EMPTY;
67 static int hf_wsp_header_uri_len                                = HF_EMPTY;
68 static int hf_wsp_header_uri                                    = HF_EMPTY;
69 static int hf_wsp_server_session_id                             = HF_EMPTY;
70 static int hf_wsp_header_status                                 = HF_EMPTY;
71 static int hf_wsp_header_length                                 = HF_EMPTY;
72 static int hf_wsp_headers_section                               = HF_EMPTY;
73 static int hf_wsp_header                                                = HF_EMPTY;
74 static int hf_wsp_content_type                                  = HF_EMPTY;
75 static int hf_wsp_parameter_well_known_charset  = HF_EMPTY;
76 static int hf_wsp_reply_data                                    = HF_EMPTY;
77 static int hf_wsp_post_data                                             = HF_EMPTY;
78
79 static int hf_wsp_header_accept                                 = HF_EMPTY;
80 static int hf_wsp_header_accept_str                             = HF_EMPTY;
81 static int hf_wsp_header_accept_charset                 = HF_EMPTY;
82 static int hf_wsp_header_accept_language                = HF_EMPTY;
83 static int hf_wsp_header_accept_ranges                  = HF_EMPTY;
84 static int hf_wsp_header_cache_control                  = HF_EMPTY;
85 static int hf_wsp_header_content_length                 = HF_EMPTY;
86 static int hf_wsp_header_age                                    = HF_EMPTY;
87 static int hf_wsp_header_date                                   = HF_EMPTY;
88 static int hf_wsp_header_etag                                   = HF_EMPTY;
89 static int hf_wsp_header_expires                                = HF_EMPTY;
90 static int hf_wsp_header_last_modified                  = HF_EMPTY;
91 static int hf_wsp_header_location                               = HF_EMPTY;
92 static int hf_wsp_header_if_modified_since              = HF_EMPTY;
93 static int hf_wsp_header_server                                 = HF_EMPTY;
94 static int hf_wsp_header_user_agent                             = HF_EMPTY;
95 static int hf_wsp_header_application_header             = HF_EMPTY;
96 static int hf_wsp_header_application_value              = HF_EMPTY;
97 static int hf_wsp_header_x_wap_tod                              = HF_EMPTY;
98
99 /* Initialize the subtree pointers */
100 static gint ett_wsp                                                     = ETT_EMPTY;
101 static gint ett_header                                                  = ETT_EMPTY;
102 static gint ett_headers                                                 = ETT_EMPTY;
103 static gint ett_capabilities                                    = ETT_EMPTY;
104 static gint ett_content_type                                    = ETT_EMPTY;
105
106 static const value_string vals_pdu_type[] = {
107         { 0x00, "Reserved" },
108         { 0x01, "Connect" },
109         { 0x02, "ConnectReply" },
110         { 0x03, "Redirect" },
111         { 0x04, "Reply" },
112         { 0x05, "Disconnect" },
113         { 0x06, "Push" },
114         { 0x07, "ConfirmedPush" },
115         { 0x08, "Suspend" },
116         { 0x09, "Resume" },
117
118         /* 0x10 - 0x3F Unassigned */
119
120         { 0x40, "Get" },
121         { 0x41, "Options" },
122         { 0x42, "Head" },
123         { 0x43, "Delete" },
124         { 0x44, "Trace" },
125
126         /* 0x45 - 0x4F Unassigned (Get PDU) */
127         /* 0x50 - 0x5F Extended method (Get PDU) */
128
129         { 0x60, "Post" },
130         { 0x61, "Put" },
131
132         /* 0x62 - 0x6F Unassigned (Post PDU) */
133         /* 0x70 - 0x7F Extended method (Post PDU) */
134         /* 0x80 - 0xFF Reserved */
135
136 };
137
138 static const value_string vals_status[] = {
139         /* 0x00 - 0x0F Reserved */
140
141         { 0x10, "Continue" },
142         { 0x11, "Switching Protocols" },
143
144         { 0x20, "OK" },
145         { 0x21, "Created" },
146         { 0x22, "Accepted" },
147         { 0x23, "Non-Authoritative Information" },
148         { 0x24, "No Content" },
149         { 0x25, "Reset Content" },
150         { 0x26, "Partial Content" },
151
152         { 0x30, "Multiple Choices" },
153         { 0x31, "Moved Permanently" },
154         { 0x32, "Moved Temporarily" },
155         { 0x33, "See Other" },
156         { 0x34, "Not Modified" },
157         { 0x35, "Use Proxy" },
158
159         { 0x40, "Bad Request" },
160         { 0x41, "Unauthorised" },
161         { 0x42, "Payment Required" },
162         { 0x43, "Forbidden" },
163         { 0x44, "Not Found" },
164         { 0x45, "Method Not Allowed" },
165         { 0x46, "Not Acceptable" },
166         { 0x47, "Proxy Authentication Required" },
167         { 0x48, "Request Timeout" },
168         { 0x49, "Conflict" },
169         { 0x4A, "Gone" },
170         { 0x4B, "Length Required" },
171         { 0x4C, "Precondition Failed" },
172         { 0x4D, "Request Entity Too Large" },
173         { 0x4E, "Request-URI Too Large" },
174         { 0x4F, "Unsupported Media Type" },
175
176         { 0x60, "Internal Server Error" },
177         { 0x61, "Not Implemented" },
178         { 0x62, "Bad Gateway" },
179         { 0x63, "Service Unavailable" },
180         { 0x64, "Gateway Timeout" },
181         { 0x65, "HTTP Version Not Supported" },
182 };
183
184 static const value_string vals_content_types[] = {
185         { 0x00, "*/*" },
186         { 0x01, "text/*" },
187         { 0x02, "text/html" },
188         { 0x03, "text/plain" },
189         { 0x04, "text/x-hdml" },
190         { 0x05, "text/x-ttml" },
191         { 0x06, "text/x-vCalendar" },
192         { 0x07, "text/x-vCard" },
193         { 0x08, "text/vnd.wap.wml" },
194         { 0x09, "text/vnd.wap.wmlscript" },
195         { 0x0A, "text/vnd.wap.channel" },
196         { 0x0B, "Multipart/*" },
197         { 0x0C, "Multipart/mixed" },
198         { 0x0D, "Multipart/form-data" },
199         { 0x0E, "Multipart/byteranges" },
200         { 0x0F, "Multipart/alternative" },
201         { 0x10, "application/*" },
202         { 0x11, "application/java-vm" },
203         { 0x12, "application/x-www-form-urlencoded" },
204         { 0x13, "application/x-hdmlc" },
205         { 0x14, "application/vnd.wap.wmlc" },
206         { 0x15, "application/vnd.wap.wmlscriptc" },
207         { 0x16, "application/vnd.wap.channelc" },
208         { 0x17, "application/vnd.wap.uaprof" },
209         { 0x18, "application/vnd.wap.wtls-ca-certificate" },
210         { 0x19, "application/vnd.wap.wtls-user-certificate" },
211         { 0x1A, "application/x-x509-ca-cert" },
212         { 0x1B, "application/x-x509-user-cert" },
213         { 0x1C, "image/*" },
214         { 0x1D, "image/gif" },
215         { 0x1E, "image/jpeg" },
216         { 0x1F, "image/tiff" },
217         { 0x20, "image/png" },
218         { 0x21, "image/vnd.wap.wbmp" },
219         { 0x22, "application/vnd.wap.multipart.*" },
220         { 0x23, "application/vnd.wap.multipart.mixed" },
221         { 0x24, "application/vnd.wap.multipart.form-data" },
222         { 0x25, "application/vnd.wap.multipart.byteranges" },
223         { 0x26, "application/vnd.wap.multipart.alternative" },
224         { 0x27, "application/xml" },
225         { 0x28, "text/xml" },
226         { 0x29, "application/vnd.wap.wbxml" },
227         { 0x2A, "application/x-x968-cross-cert" },
228         { 0x2B, "application/x-x968-ca-cert" },
229         { 0x2C, "application/x-x968-user-cert" },
230         { 0x2D, "text/vnd.wap.si" },
231         { 0x2E, "application/vnd.wap.sic" },
232         { 0x2F, "text/vnd.wap.sl" },
233         { 0x30, "application/vnd.wap.slc" },
234         { 0x31, "text/vnd.wap.co" },
235         { 0x32, "application/vnd.wap.coc" },
236         { 0x33, "application/vnd.wap.multipart.related" },
237         { 0x34, "application/vnd.wap.sia" },
238 };
239
240 static const value_string vals_character_sets[] = {
241         { 0x0003, "us-ascii" },
242         { 0x0004, "iso-8859-1" },
243         { 0x0005, "iso-8859-2" },
244         { 0x0006, "iso-8859-3" },
245         { 0x0007, "iso-8859-4" },
246         { 0x0008, "iso-8859-5" },
247         { 0x0009, "iso-8859-6" },
248         { 0x000A, "iso-8859-7" },
249         { 0x000B, "iso-8859-8" },
250         { 0x000C, "iso-8859-9" },
251         { 0x0011, "shift_JIS" },
252         { 0x006A, "utf-8" },
253         { 0x03E8, "iso-10646-ucs-2" },
254         { 0x07EA, "big5" },
255 };
256
257 static const value_string vals_languages[] = {
258         { 0x19, "English (en)" },
259 };
260
261 static const value_string vals_accept_ranges[] = {
262         { 0x80, "None" },
263         { 0x81, "Bytes" },
264 };
265
266 static const value_string vals_cache_control[] = {
267         { 0x80, "No-cache" },
268         { 0x81, "No-store" },
269         { 0x82, "Max-age" },
270         { 0x83, "Max-stale" },
271         { 0x84, "Min-fresh" },
272         { 0x85, "Only-if-cached" },
273         { 0x86, "Public" },
274         { 0x87, "Private" },
275         { 0x88, "No-transform" },
276         { 0x89, "Must-revalidate" },
277         { 0x8A, "Proxy-revalidate" },
278 };
279
280 enum {
281         RESERVED                = 0x00,
282         CONNECT                 = 0x01,
283         CONNECTREPLY    = 0x02,
284         REDIRECT                = 0x03,                 /* No sample data */
285         REPLY                   = 0x04,
286         DISCONNECT              = 0x05,
287         PUSH                    = 0x06,                 /* No sample data */
288         CONFIRMEDPUSH   = 0x07,                 /* No sample data */
289         SUSPEND                 = 0x08,                 /* No sample data */
290         RESUME                  = 0x09,                 /* No sample data */
291
292         GET                             = 0x40,
293         OPTIONS                 = 0x41,                 /* No sample data */
294         HEAD                    = 0x42,                 /* No sample data */
295         DELETE                  = 0x43,                 /* No sample data */
296         TRACE                   = 0x44,                 /* No sample data */
297
298         POST                    = 0x60,
299         PUT                             = 0x61,                 /* No sample data */
300 };
301
302 void add_uri (proto_tree *, tvbuff_t *, guint, guint);
303 void add_headers (proto_tree *, tvbuff_t *);
304 void add_header (proto_tree *, tvbuff_t *, tvbuff_t *);
305 guint get_value_length (tvbuff_t *, guint, guint *);
306 guint add_content_type (proto_tree *, tvbuff_t *, guint, guint *);
307 guint add_parameter (proto_tree *, tvbuff_t *, guint);
308 guint add_parameter_charset (proto_tree *, tvbuff_t *, guint, guint);
309 void add_post_data (proto_tree *, tvbuff_t *, guint);
310 void add_post_variable (proto_tree *, tvbuff_t *, guint, guint, guint, guint);
311
312 /* 
313  * Accessor to retrieve variable length int as used in WAP protocol.
314  * The value is encoded in the lower 7 bits. If the top bit is set, then the
315  * value continues into the next byte.
316  * The octetCount parameter holds the number of bytes read in order to return
317  * the final value. Can be pre-initialised to start at offset+count.
318 */
319 guint
320 tvb_get_guintvar (tvbuff_t *tvb, guint offset, guint *octetCount)
321 {
322         guint value = 0;
323         guint octet;
324         guint counter = 0;
325         char cont = 1;
326         
327         if (octetCount != NULL)
328         {
329 #ifdef DEBUG
330                 fprintf (stderr, "dissect_wsp: Starting tvb_get_guintvar at offset %d, count=NULL\n", offset);
331 #endif
332         }
333         else
334         {
335 #ifdef DEBUG
336                 fprintf (stderr, "dissect_wsp: Starting tvb_get_guintvar at offset %d, count=%d\n", offset, *octetCount);
337 #endif
338                 counter = *octetCount;
339         }
340
341         while (cont != 0)
342         {
343                 value<<=7;      /* Value only exists in 7 of the 8 bits */
344                 octet = tvb_get_guint8 (tvb, offset+counter);
345                 counter++;
346                 value += (octet & 0x7F);
347                 cont = (octet & 0x80);
348 #ifdef DEBUG
349                 fprintf (stderr, "dissect_wsp: octet is %d (0x%02x), count=%d, value=%d, cont=%d\n", octet, octet, counter, value, cont);
350 #endif
351         }
352
353         if (octetCount != NULL)
354         {
355                 *octetCount = counter;
356 #ifdef DEBUG
357                 fprintf (stderr, "dissect_wsp: Leaving tvb_get_guintvar count=%d\n", *octetCount);
358 #endif
359         }
360
361         return (value);
362 }
363
364 void
365 dissect_wsp_no_tvbuff(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
366 {
367         packet_info     *pinfo = &pi;
368         tvbuff_t        *tvb = tvb_create_from_top(offset);
369         dissect_wsp (tvb, pinfo, tree);
370 }
371
372 /* Code to actually dissect the packets */
373 void
374 dissect_wsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
375 {
376         frame_data *fdata = pinfo->fd;
377         int offset = 0;
378
379         char szInfo[ 50 ];
380         int cchInfo;
381         char pdut;
382         guint count = 0;
383         guint value = 0;
384         guint uriLength = 0;
385         guint uriStart = 0;
386         guint capabilityLength = 0;
387         guint capabilityStart = 0;
388         guint headersLength = 0;
389         guint headerLength = 0;
390         guint headerStart = 0;
391         guint nextOffset = 0;
392         guint contentTypeStart = 0;
393         guint contentType = 0;
394         tvbuff_t *tmp_tvb;
395
396 /* Set up structures we will need to add the protocol subtree and manage it */
397         proto_item *ti;
398         proto_tree *wsp_tree;
399 /*      proto_tree *wsp_header_fixed; */
400         proto_tree *wsp_capabilities;
401         
402 /* This field shows up as the "Info" column in the display; you should make
403    it, if possible, summarize what's in the packet, so that a user looking
404    at the list of packets can tell what type of packet it is.
405    "col_add_fstr()" can be used instead of "col_add_str()"; it takes
406    "printf()"-like arguments. */
407
408         /* Display protocol type depending on the port */
409         if (check_col(fdata, COL_PROTOCOL)) 
410         {
411                 switch ( pinfo->match_port )
412                 {
413                         case UDP_PORT_WSP:
414                                 col_add_fstr(fdata, COL_PROTOCOL, "WSP" );
415                                 break;
416                         case UDP_PORT_WTLS_WSP:
417                                 col_add_fstr(fdata, COL_PROTOCOL, "WTLS+WSP" );
418                                 break;
419                 }
420         }
421     
422         /* Connection-less mode has a TID first */
423         if ((pinfo->match_port == UDP_PORT_WSP) || (pinfo->match_port == UDP_PORT_WTLS_WSP))
424         {
425                 offset++;
426         };
427
428         /* Find the PDU type */
429         pdut = tvb_get_guint8 (tvb, offset);
430
431         /* Develop the string to put in the Info column */
432         cchInfo = snprintf( szInfo, sizeof( szInfo ), "WSP %s",
433                 match_strval (( int )pdut, vals_pdu_type));
434         if (check_col(fdata, COL_INFO)) {
435                 col_add_str(fdata, COL_INFO, szInfo );
436         };
437
438 /* In the interest of speed, if "tree" is NULL, don't do any work not
439    necessary to generate protocol tree items. */
440         if (tree) {
441 /* NOTE: The offset and length values in the previous call to
442    "proto_tree_add_item()" define what data bytes to highlight in the hex
443    display window when the line in the protocol tree display
444    corresponding to that item is selected.
445
446    END_OF_FRAME is a handy way to highlight all data from the offset to
447    the end of the packet. */
448 /*              ti = proto_tree_add_item(tree, proto_wsp, NullTVB, offset, END_OF_FRAME, NULL); */
449 /*
450                 ti = proto_tree_add_item(tree, proto_wsp, tvb, offset, END_OF_FRAME, NULL);
451                 wsp_tree = proto_item_add_subtree(ti, ett_wsp);
452 */
453
454                 ti = proto_tree_add_item(tree, proto_wsp, tvb, 0, END_OF_FRAME, bo_little_endian);
455                 wsp_tree = proto_item_add_subtree(ti, ett_wsp);
456
457 /* Code to process the packet goes here */
458 /*
459                         wsp_header_fixed = proto_item_add_subtree(
460                                         ti, 
461                                         ett_header 
462                                 );
463 */
464
465                         /* Add common items: only TID and PDU Type */
466
467                         /* TID Field is always first (if it exists) */
468                         if ((pinfo->match_port == UDP_PORT_WSP) || (pinfo->match_port == UDP_PORT_WTLS_WSP))
469                         {
470                                 ti = proto_tree_add_item (wsp_tree, hf_wsp_header_tid,tvb,0,1,bo_little_endian);
471                         }
472
473                         ti = proto_tree_add_item(
474                                         wsp_tree,               /* tree */
475                                         hf_wsp_header_pdu_type,         /* id */
476                                         tvb, 
477                                         offset++,                       /* start of high light */
478                                         1,                              /* length of high light */
479                                         bo_little_endian                                /* value */
480                              );
481
482                         switch (pdut)
483                         {
484                                 case CONNECT:
485                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_version_major,tvb,offset,1,bo_little_endian);
486                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_version_minor,tvb,offset,1,bo_little_endian);
487                                         offset++;
488                                         capabilityStart = offset;
489                                         capabilityLength = tvb_get_guintvar (tvb, offset, &count);
490                                         offset += count;
491                                         ti = proto_tree_add_uint (wsp_tree, hf_wsp_capability_length,tvb,capabilityStart,count,capabilityLength);
492
493                                         headerStart = offset;
494                                         headerLength = tvb_get_guintvar (tvb, offset, &count);
495                                         offset += count;
496                                         ti = proto_tree_add_uint (wsp_tree, hf_wsp_header_length,tvb,headerStart,count,headerLength);
497                                         if (capabilityLength > 0)
498                                         {
499                                                 ti = proto_tree_add_item (wsp_tree, hf_wsp_capabilities_section,tvb,offset,capabilityLength,bo_little_endian);
500                                                 wsp_capabilities = proto_item_add_subtree( ti, ett_capabilities );
501                                                 offset += capabilityLength;
502                                         }
503
504                                         if (headerLength > 0)
505                                         {
506                                                 tmp_tvb = tvb_new_subset (tvb, offset, headerLength, headerLength);
507                                                 add_headers (wsp_tree, tmp_tvb);
508                                         }
509
510                                         break;
511
512                                 case CONNECTREPLY:
513                                         value = tvb_get_guintvar (tvb, offset, &count);
514                                         ti = proto_tree_add_uint (wsp_tree, hf_wsp_server_session_id,tvb,offset,count,value);
515                                         offset += count;
516
517                                         capabilityStart = offset;
518                                         capabilityLength = tvb_get_guintvar (tvb, offset, &count);
519                                         offset += count;
520                                         ti = proto_tree_add_uint (wsp_tree, hf_wsp_capability_length,tvb,capabilityStart,count,capabilityLength);
521
522                                         headerStart = offset;
523                                         headerLength = tvb_get_guintvar (tvb, offset, &count);
524                                         offset += count;
525                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_header_length,tvb,headerStart,count,bo_little_endian);
526                                         if (capabilityLength > 0)
527                                         {
528                                                 ti = proto_tree_add_item (wsp_tree, hf_wsp_capabilities_section,tvb,offset,capabilityLength,bo_little_endian);
529                                                 wsp_capabilities = proto_item_add_subtree( ti, ett_capabilities );
530                                                 offset += capabilityLength;
531                                         }
532
533                                         if (headerLength > 0)
534                                         {
535
536                                                 /*
537                                                 ti = proto_tree_add_item (wsp_tree, hf_wsp_headers_section,tvb,offset,headerLength,bo_little_endian);
538                                                 wsp_headers = proto_item_add_subtree( ti, ett_headers );
539                                                 */
540                                                 tmp_tvb = tvb_new_subset (tvb, offset, headerLength, headerLength);
541                                                 add_headers (wsp_tree, tmp_tvb);
542                                         }
543
544                                         break;
545
546                                 case DISCONNECT:
547                                         value = tvb_get_guintvar (tvb, offset, &count);
548                                         ti = proto_tree_add_uint (wsp_tree, hf_wsp_server_session_id,tvb,offset,count,value);
549                                         break;
550
551                                 case GET:
552                                         /* Length of URI and size of URILen field */
553                                         value = tvb_get_guintvar (tvb, offset, &count);
554                                         nextOffset = offset + count;
555                                         add_uri (wsp_tree, tvb, offset, nextOffset);
556                                         offset += (value+1);
557                                         tmp_tvb = tvb_new_subset (tvb, offset, -1, -1);
558                                         add_headers (wsp_tree, tmp_tvb);
559                                         break;
560
561                                 case POST:
562                                         uriStart = offset;
563                                         uriLength = tvb_get_guintvar (tvb, offset, &count);
564                                         headerStart = uriStart+count;
565                                         headersLength = tvb_get_guintvar (tvb, headerStart, &count);
566                                         offset = headerStart + count;
567
568                                         add_uri (wsp_tree, tvb, uriStart, offset);
569                                         offset += uriLength;
570
571                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_header_length,tvb,headerStart,count,bo_little_endian);
572
573                                         contentTypeStart = offset;
574                                         nextOffset = add_content_type (wsp_tree, tvb, offset, &contentType);
575
576                                         /* Add headers subtree that will hold the headers fields */
577                                         /* Runs from nextOffset for value-(length of content-type field)*/
578                                         headerLength = headersLength-(nextOffset-contentTypeStart);
579                                         tmp_tvb = tvb_new_subset (tvb, nextOffset, headerLength, headerLength);
580                                         add_headers (wsp_tree, tmp_tvb);
581
582                                         /* TODO: Post DATA */
583                                         /* Runs from start of headers+headerLength to END_OF_FRAME */
584                                         offset = nextOffset+headerLength;
585                                         tmp_tvb = tvb_new_subset (tvb, offset, tvb_reported_length (tvb)-offset, tvb_reported_length (tvb)-offset);
586                                         add_post_data (wsp_tree, tmp_tvb, contentType);
587                                         break;
588
589                                 case REPLY:
590                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_header_status,tvb,offset,1,bo_little_endian);
591                                         value = tvb_get_guintvar (tvb, offset+1, &count);
592                                         nextOffset = offset + 1 + count;
593                                         ti = proto_tree_add_item (wsp_tree, hf_wsp_header_length,tvb,offset+1,count,bo_little_endian);
594
595                                         contentTypeStart = nextOffset;
596                                         nextOffset = add_content_type (wsp_tree, tvb, nextOffset, &contentType);
597
598                                         /* Add headers subtree that will hold the headers fields */
599                                         /* Runs from nextOffset for value-(length of content-type field)*/
600                                         headerLength = value-(nextOffset-contentTypeStart);
601                                         tmp_tvb = tvb_new_subset (tvb, nextOffset, headerLength, headerLength);
602                                         add_headers (wsp_tree, tmp_tvb);
603                                         offset += count+value+1;
604
605                                         /* TODO: Data - decode WMLC */
606                                         /* Runs from offset+1+count+value+1 to END_OF_FRAME */
607                                         if (offset < tvb_reported_length (tvb))
608                                         {
609                                                 ti = proto_tree_add_item (wsp_tree, hf_wsp_reply_data,tvb,offset,END_OF_FRAME,bo_little_endian);
610                                         }
611                                         break;
612                         }
613         }
614 }
615
616 void
617 add_uri (proto_tree *tree, tvbuff_t *tvb, guint URILenOffset, guint URIOffset)
618 {
619         proto_item *ti;
620         guint8 terminator = 0;
621         char *newBuffer;
622
623         guint count = 0;
624         guint uriLen = tvb_get_guintvar (tvb, URILenOffset, &count);
625
626         ti = proto_tree_add_uint (tree, hf_wsp_header_uri_len,tvb,URILenOffset,count,uriLen);
627
628         /* If string doesn't end with a 0x00, we need to add one to be on the safe side */
629         terminator = tvb_get_guint8 (tvb, URIOffset+uriLen-1);
630         if (terminator != 0)
631         {
632                 newBuffer = g_malloc (uriLen+1);
633                 strncpy (newBuffer, tvb_get_ptr (tvb, URIOffset, uriLen), uriLen);
634                 newBuffer[uriLen] = 0;
635                 ti = proto_tree_add_string (tree, hf_wsp_header_uri,tvb,URIOffset,uriLen,newBuffer);
636                 g_free (newBuffer);
637         }
638         else
639         {
640                 ti = proto_tree_add_item (tree, hf_wsp_header_uri,tvb,URIOffset,uriLen,bo_little_endian);
641         }
642 }
643
644 void
645 add_headers (proto_tree *tree, tvbuff_t *tvb)
646 {
647         proto_item *ti;
648         proto_tree *wsp_headers;
649         guint offset = 0;
650         guint headersLen = tvb_reported_length (tvb);
651         guint8 headerStart = 0;
652         guint peek = 0;
653         tvbuff_t *header_buff;
654         tvbuff_t *value_buff;
655         guint count = 0;
656         guint valueStart = 0;
657         guint valueEnd = 0;
658
659 #ifdef DEBUG
660         fprintf (stderr, "dissect_wsp: Offset is %d, size is %d\n", offset, headersLen);
661 #endif
662
663         /* End of buffer */
664         if (headersLen <= 0)
665         {
666                 return;
667         }
668
669 #ifdef DEBUG
670         fprintf (stderr, "dissect_wsp: Headers to process\n");
671 #endif
672
673         ti = proto_tree_add_item (tree, hf_wsp_headers_section,tvb,offset,headersLen,bo_little_endian);
674         wsp_headers = proto_item_add_subtree( ti, ett_headers );
675
676         /* Parse Headers */
677
678         while (offset < headersLen)
679         {
680                 /* Loop round each header */
681                 headerStart = offset;
682                 peek = tvb_get_guint8 (tvb, headerStart);
683
684                 if (peek < 32)          /* Short-cut shift delimeter */
685                 {
686                         fprintf (stderr, "dissect_wsp: header: short-cut shift %d (0x%02X)\n", peek, peek);
687                         offset++;
688                 }
689                 else if (peek == 0x7F)  /* Shift delimeter */
690                 {
691                         fprintf (stderr, "dissect_wsp: header: shift delimeter %d (0x%02X)\n", peek, peek);
692                         offset++;
693                 }
694                 else if (peek < 127)
695                 {
696 #ifdef DEBUG
697                         fprintf (stderr, "dissect_wsp: header: application-header start %d (0x%02X)\n", peek, peek);
698 #endif
699                         while (tvb_get_guint8 (tvb, offset++)) { /* Do nothing, just look for NULL */ }
700                 }
701                 else if (peek & 0x80)   /* Well-known header */
702                 {
703 #ifdef DEBUG
704                         fprintf (stderr, "dissect_wsp: header: well-known %d (0x%02X)\n", peek, peek);
705 #endif
706                         offset++;
707                 }
708
709                 /* Get value part of header */
710                 valueStart = offset;
711                 peek = tvb_get_guint8 (tvb, valueStart);
712                 if (peek <= 30)
713                 {
714 #ifdef DEBUG
715                         fprintf (stderr, "dissect_wsp: Looking for %d octets\n", peek);
716 #endif
717                         valueStart++;
718                         valueEnd = offset+1+peek;
719                         offset += (peek+1);
720                 }
721                 else if (peek == 31)
722                 {
723 #ifdef DEBUG
724                         fprintf (stderr, "dissect_wsp: Looking for uintvar octets\n");
725 #endif
726                         tvb_get_guintvar (tvb, valueStart, &count);
727                         valueEnd = offset+1+count;
728                         offset += (count+1);
729                 }
730                 else if (peek <= 127)
731                 {
732 #ifdef DEBUG
733                         fprintf (stderr, "dissect_wsp: Looking for NULL-terminated string\n");
734 #endif
735                         valueEnd = valueStart+1;
736                         while (tvb_get_guint8 (tvb, valueEnd++)) { /* Do nothing, just look for NULL */ }
737                         offset = valueEnd;
738                 }
739                 else
740                 {
741 #ifdef DEBUG
742                         fprintf (stderr, "dissect_wsp: Value is %d\n", (peek & 0x7F));
743 #endif
744                         valueEnd = offset+1;
745                         offset++;
746                 }
747 #ifdef DEBUG
748                 fprintf (stderr, "dissect_wsp: Creating value buffer from offset %d, size=%d\n", headerStart, (offset-headerStart));
749 #endif
750
751                 header_buff = tvb_new_subset (tvb, headerStart, (offset-headerStart), (offset-headerStart));
752                 value_buff = tvb_new_subset (tvb, valueStart, (valueEnd-valueStart), (valueEnd-valueStart));
753
754                 add_header (wsp_headers, header_buff, value_buff);
755         }
756 }
757
758 void
759 add_header (proto_tree *tree, tvbuff_t *header_buff, tvbuff_t *value_buff)
760 {
761         guint offset = 0;
762         guint8 headerType = 0;
763         proto_item *ti;
764         guint headerLen = tvb_reported_length (header_buff);
765         guint valueLen = tvb_reported_length (value_buff);
766         guint peek = 0;
767         struct timeval timeValue;
768         guint value = 0;
769
770         headerType = tvb_get_guint8 (header_buff, 0);
771         peek = tvb_get_guint8 (value_buff, 0);
772 #ifdef DEBUG
773         fprintf (stderr, "dissect_wsp: Got header 0x%02x\n", headerType);
774         fprintf (stderr, "dissect_wsp: First value octet is 0x%02x\n", peek);
775 #endif
776
777         if (headerType == 0x7F)
778         {
779         }
780         else if (headerType < 0x1F)
781         {
782         }
783         else if (headerType & 0x80)
784         {
785                 headerType = headerType & 0x7F;
786                 switch (headerType)
787                 {
788                         case 0x00:              /* Accept */
789                                 if (peek & 0x80)
790                                 {
791                                         proto_tree_add_uint (tree, hf_wsp_header_accept, header_buff, offset, headerLen, (peek & 0x7F));
792                                 }
793                                 else
794                                 {
795                                         proto_tree_add_string (tree, hf_wsp_header_accept_str,header_buff,offset,headerLen,tvb_get_ptr (value_buff, 0, valueLen));
796                                 }
797                                 break;
798
799                         case 0x01:              /* Accept-Charset */
800                                 if (peek < 31)
801                                 {
802                                         /* Peek contains the number of octets to follow */
803                                         switch (peek)
804                                         {
805                                                 case 1:
806                                                         proto_tree_add_uint (tree, hf_wsp_header_accept_charset, header_buff, offset, headerLen, tvb_get_guint8 (value_buff, 1) );
807                                                         break;
808                                                 case 2:
809                                                         proto_tree_add_uint (tree, hf_wsp_header_accept_charset, header_buff, offset, headerLen, tvb_get_ntohs (value_buff, 1) );
810                                                         break;
811                                                 case 4:
812                                                         proto_tree_add_uint (tree, hf_wsp_header_accept_charset, header_buff, offset, headerLen, tvb_get_ntohl (value_buff, 1) );
813                                                         break;
814                                                 default:
815                                                         fprintf (stderr, "dissect_wsp: accept-charset size %d NYI\n", peek);
816                                         }
817                                 }
818                                 else if (peek & 0x80)
819                                 {
820                                         proto_tree_add_uint (tree, hf_wsp_header_accept_charset, header_buff, offset, headerLen, (peek & 0x7F) );
821                                 }
822                                 else
823                                 {
824                                         fprintf (stderr, "dissect_wsp: Accept-Charset value %d (0x%02X) NYI\n", peek, peek);
825                                 }
826                                 break;
827
828                         case 0x03:              /* Accept-Language */
829                                 proto_tree_add_uint (tree, hf_wsp_header_accept_language, header_buff, offset, headerLen, (peek & 0x7F));
830                                 break;
831
832                         case 0x04:              /* Accept-Ranges */
833                                 if ((peek == 128) || (peek == 129))
834                                 {
835                                         proto_tree_add_uint (tree, hf_wsp_header_accept_ranges, header_buff, offset, headerLen, peek);
836                                 }
837                                 else
838                                 {
839                                         fprintf (stderr, "dissect_wsp: accept-ranges NYI\n");
840                                 }
841                                 
842                                 break;
843
844                         case 0x05:              /* Age */
845                                 switch (valueLen)
846                                 {
847                                         case 1:
848                                                 proto_tree_add_uint (tree, hf_wsp_header_age, header_buff, offset, headerLen, tvb_get_guint8 (value_buff, 0));
849                                                 break;
850                                         case 2:
851                                                 proto_tree_add_uint (tree, hf_wsp_header_age, header_buff, offset, headerLen, tvb_get_ntohs (value_buff, 0));
852                                                 break;
853                                         case 3:
854                                                 proto_tree_add_uint (tree, hf_wsp_header_age, header_buff, offset, headerLen, tvb_get_ntoh24 (value_buff, 0));
855                                                 break;
856                                         case 4:
857                                                 proto_tree_add_uint (tree, hf_wsp_header_age, header_buff, offset, headerLen, tvb_get_ntohl (value_buff, 0));
858                                                 break;
859                                 };
860                                 break;
861
862                         case 0x08:              /* Cache-Control */
863                                 if (peek & 0x80)
864                                 {
865                                         if (valueLen == 1)      /* Well-known value */
866                                         {
867                                                 proto_tree_add_uint (tree, hf_wsp_header_cache_control, header_buff, offset, headerLen, peek);
868                                         }
869                                         else
870                                         {
871                                                 if ((peek == 0x82) || (peek == 0x83) || (peek == 0x84)) /* Delta seconds value to follow */
872                                                 {
873                                                         value = tvb_get_guint8 (value_buff, 1);
874                                                         if (value & 0x80)
875                                                         {
876                                                                 proto_tree_add_text (tree, header_buff, 0, headerLen, "Cache-Control: %s %d (0x%02X)",
877                                                                         match_strval ((int) peek, vals_cache_control), (value & 0x7F), peek);
878                                                         }
879                                                         else
880                                                         {
881                                                                 fprintf (stderr, "dissect_wsp: Cache-Control integer value Delta seconds NYI\n");
882                                                         }
883                                                 }
884                                                 else if ((peek == 0x80) || (peek == 0x87))      /* Fields to follow */
885                                                 {
886                                                         fprintf (stderr, "dissect_wsp: Cache-Control field values NYI\n");
887                                                 }
888                                                 else
889                                                 {
890                                                         fprintf (stderr, "dissect_wsp: Cache-Control cache extension NYI\n");
891                                                 }
892                                         }
893                                 }
894                                 else
895                                 {
896                                         fprintf (stderr, "dissect_wsp: Cache-Control cache extension NYI\n");
897                                 }
898                                 break;
899                                 
900                         case 0x0D:              /* Content-Length */
901                                 if (peek & 0x80)
902                                 {
903                                         proto_tree_add_uint (tree, hf_wsp_header_content_length, header_buff, offset, headerLen, (peek & 0x7F));
904                                 }
905                                 else
906                                 {
907                                         fprintf (stderr, "dissect_wsp: Content-Length long-integer size NYI\n");
908                                 }
909                                 break;
910                                 
911                         case 0x12:              /* Date */
912                                 timeValue.tv_sec = tvb_get_ntohl (value_buff, 0);
913                                 ti = proto_tree_add_time (tree, hf_wsp_header_date, header_buff, offset, headerLen, &timeValue);
914                                 break;
915
916                         case 0x13:              /* Etag */
917                                 ti = proto_tree_add_string (tree, hf_wsp_header_etag,header_buff,offset,headerLen,tvb_get_ptr (value_buff, 0, valueLen));
918                                 break;
919
920                         case 0x14:              /* Expires */
921                                 switch (valueLen)
922                                 {
923                                         case 1:
924                                         case 2:
925                                                 fprintf (stderr, "dissect_wsp: Expires value length %d NYI\n", valueLen);
926                                                 break;
927                                         case 3:
928                                                 timeValue.tv_sec = tvb_get_ntoh24 (value_buff, 0);
929                                                 break;
930                                         case 4:
931                                                 timeValue.tv_sec = tvb_get_ntohl (value_buff, 0);
932                                                 break;
933                                 };
934                                 ti = proto_tree_add_time (tree, hf_wsp_header_expires, header_buff, offset, headerLen, &timeValue);
935                                 break;
936
937                         case 0x17:              /* If-Modified-Since */
938                                 if (valueLen == 4)
939                                 {
940                                         timeValue.tv_sec = tvb_get_ntohl (value_buff, 0);
941                                 }
942                                 else
943                                 {
944                                         timeValue.tv_sec = 0;
945                                 }
946                                 ti = proto_tree_add_time (tree, hf_wsp_header_if_modified_since, header_buff, offset, headerLen, &timeValue);
947                                 break;
948                                 
949                         case 0x1C:              /* Location */
950                                 ti = proto_tree_add_string (tree, hf_wsp_header_location,header_buff,offset,headerLen,tvb_get_ptr (value_buff, 0, valueLen));
951                                 break;
952
953                         case 0x1D:              /* Last-Modified */
954                                 timeValue.tv_sec = tvb_get_ntohl (value_buff, 0);
955                                 ti = proto_tree_add_time (tree, hf_wsp_header_last_modified, header_buff, offset, headerLen, &timeValue);
956                                 break;
957                                 
958                         case 0x1F:              /* Pragma */
959                                 if (peek == 0x80)
960                                 {
961                                         proto_tree_add_text (tree, header_buff, 0, headerLen, "Pragma: No-cache");
962                                 }
963                                 else
964                                 {
965                                         proto_tree_add_text (tree, header_buff, 0, headerLen, "Unsupported Header (0x%02X)", (tvb_get_guint8 (header_buff, 0) & 0x7F));
966                                 }
967                                 break;
968                                 
969                         case 0x26:              /* Server */
970                                 ti = proto_tree_add_string (tree, hf_wsp_header_server,header_buff,offset,headerLen,tvb_get_ptr (value_buff, 0, valueLen));
971                                 break;
972
973                         case 0x29:              /* User-Agent */
974                                 ti = proto_tree_add_string (tree, hf_wsp_header_user_agent,header_buff,offset,headerLen,tvb_get_ptr (value_buff, 0, valueLen));
975                                 break;
976
977                         default:
978                                 ti = proto_tree_add_text (tree, header_buff, 0, headerLen, "Unsupported Header (0x%02X)", (tvb_get_guint8 (header_buff, 0) & 0x7F));
979                                 break;
980                 }
981         }
982         else
983         {
984                 /* Special case header X-WAP.TOD that is sometimes followed
985                  * by a 4-byte date value */
986                 if (strncasecmp ("x-wap.tod", tvb_get_ptr (header_buff, 0, headerLen), 9) == 0)
987                 {
988                         if (tvb_reported_length (value_buff) == 4)      /* Probably a date value */
989                         {
990                                 timeValue.tv_sec = tvb_get_ntohl (value_buff, 0);
991                                 ti = proto_tree_add_time (tree, hf_wsp_header_x_wap_tod, header_buff, offset, headerLen, &timeValue);
992                         }
993                         else
994                         {
995                                 ti = proto_tree_add_text (tree, header_buff, 0, headerLen, "%s: %s", tvb_get_ptr (header_buff, 0, headerLen), tvb_get_ptr (value_buff, 0, valueLen));
996                         }
997                 }
998                 else
999                 {
1000                         ti = proto_tree_add_text (tree, header_buff, 0, headerLen, "%s: %s", tvb_get_ptr (header_buff, 0, headerLen), tvb_get_ptr (value_buff, 0, valueLen));
1001                 }
1002         }
1003
1004 }
1005
1006 guint
1007 get_value_length (tvbuff_t *tvb, guint offset, guint *nextOffset)
1008 {
1009         guint value = 0;
1010         guint count = 0;
1011         guint octet = tvb_get_guint8 (tvb, offset);
1012
1013         if (octet <= 30)        /* Short length */
1014         {
1015                 value = octet;
1016                 *nextOffset = offset+1;
1017         }
1018         else if (octet == 31)
1019         {
1020                 value = tvb_get_guintvar (tvb, offset+1, &count);
1021                 *nextOffset = offset+1+count;
1022         }
1023         else
1024         {
1025                 fprintf (stderr, "dissect_wsp: get_value_length: case NYI\n");
1026         }
1027
1028         return (value);
1029 }
1030
1031 guint
1032 add_content_type (proto_tree *tree, tvbuff_t *tvb, guint offset, guint *contentType)
1033 {
1034         proto_tree *contentTypeTree;
1035         guint nextOffset = offset;
1036         guint fieldLength = 0;
1037         guint octet = tvb_get_guint8 (tvb, offset);
1038         guint totalSizeOfField = 0;
1039
1040         if (octet <= 31)
1041         {
1042                 fieldLength = get_value_length (tvb, offset, &nextOffset);
1043                 totalSizeOfField = (nextOffset-offset)+fieldLength;
1044         }
1045         else if (octet & 0x80)
1046         {
1047                 fieldLength = 1;
1048                 totalSizeOfField = 1;
1049         }
1050         else
1051         {
1052                 fprintf (stderr, "dissect-wsp: Content-type is un-supported\n");
1053         }
1054
1055         *contentType = (tvb_get_guint8 (tvb, nextOffset) & 0x7F);
1056         contentTypeTree = proto_tree_add_uint (tree, hf_wsp_content_type, tvb, offset, totalSizeOfField, (tvb_get_guint8(tvb,nextOffset++) & 0x7F));
1057
1058         while (nextOffset < (offset+totalSizeOfField))
1059         {
1060                 /* add_parameter */
1061                 nextOffset = add_parameter (contentTypeTree, tvb, nextOffset);
1062         }
1063
1064         return (offset+totalSizeOfField);
1065 }
1066
1067 guint
1068 add_parameter (proto_tree *tree, tvbuff_t *tvb, guint offset)
1069 {
1070         guint octet = tvb_get_guint8 (tvb, offset);
1071         if (octet & 0x80)       /* Short integer */
1072         {
1073                 offset++;
1074                 octet = octet & 0x7F;
1075                 switch ( octet )
1076                 {
1077                         case 0x01:
1078                                 offset = add_parameter_charset (tree, tvb, offset, offset-1);
1079                                 break;
1080
1081                         default:
1082                                 fprintf (stderr, "dissect-wsp: add_parameter octet=0x%02x\n", octet);
1083                 };
1084         }
1085         else
1086         {
1087                 fprintf (stderr, "dissect-wsp: add_parameter octet=0x%02x\n", octet);
1088         }
1089
1090         return (offset);
1091 }
1092
1093 guint
1094 add_parameter_charset (proto_tree *tree, tvbuff_t *tvb, guint offset, guint startOffset)
1095 {
1096         guint octet = tvb_get_guint8 (tvb, offset);
1097         if (octet < 31)
1098         {
1099                 offset += octet+1;
1100                 proto_tree_add_item (tree, hf_wsp_parameter_well_known_charset, tvb, startOffset+1, octet, bo_big_endian);
1101         }
1102         else if (octet & 0x80)
1103         {
1104                 offset++;
1105                 proto_tree_add_uint (tree, hf_wsp_parameter_well_known_charset, tvb, startOffset, offset-startOffset, (octet & 0x7F));
1106         }
1107
1108         return offset;
1109 }
1110
1111 void
1112 add_post_data (proto_tree *tree, tvbuff_t *tvb, guint contentType)
1113 {
1114         guint offset = 0;
1115         guint variableStart = 0;
1116         guint variableEnd = 0;
1117         guint valueStart = 0;
1118         guint valueEnd = 0;
1119         guint8 peek = 0;
1120         proto_item *ti;
1121         
1122         ti = proto_tree_add_item (tree, hf_wsp_post_data,tvb,offset,END_OF_FRAME,bo_little_endian);
1123
1124         if (contentType == 0x12)        /* URL Encoded data */
1125         {
1126                 /* Iterate through post data */
1127                 for (offset = 0; offset < tvb_reported_length (tvb); offset++)
1128                 {
1129                         peek = tvb_get_guint8 (tvb, offset);
1130                         if (peek == '=')
1131                         {
1132                                 variableEnd = offset-1;
1133                                 valueStart = offset+1;
1134                         }
1135                         else if (peek == '&')
1136                         {
1137                                 if (variableEnd > 0)
1138                                 {
1139                                         add_post_variable (ti, tvb, variableStart, variableEnd, valueStart, offset);
1140                                 }
1141                                 variableStart = offset+1;
1142                                 variableEnd = 0;
1143                                 valueStart = 0;
1144                                 valueEnd = 0;
1145                         }
1146                 }
1147
1148                 /* See if there's outstanding data */
1149                 if (variableEnd > 0)
1150                 {
1151                         add_post_variable (ti, tvb, variableStart, variableEnd, valueStart, offset);
1152                 }
1153         }
1154 }
1155
1156 void
1157 add_post_variable (proto_tree *tree, tvbuff_t *tvb, guint variableStart, guint variableEnd, guint valueStart, guint valueEnd)
1158 {
1159         int variableLength = variableEnd-variableStart;
1160         int valueLength = 0;
1161         char *variableBuffer;
1162         char *valueBuffer;
1163
1164         variableBuffer = g_malloc (variableLength+1);
1165         strncpy (variableBuffer, tvb_get_ptr (tvb, variableStart, variableLength), variableLength+1);
1166         variableBuffer[variableLength+1] = 0;
1167
1168         if (valueEnd == 0)
1169         {
1170                 valueBuffer = g_malloc (1);
1171                 valueBuffer[0] = 0;
1172                 valueEnd = valueStart;
1173         }
1174         else
1175         {
1176                 valueLength = valueEnd-valueStart;
1177                 valueBuffer = g_malloc (valueLength+1);
1178                 strncpy (valueBuffer, tvb_get_ptr (tvb, valueStart, valueLength), valueLength);
1179                 valueBuffer[valueLength] = 0;
1180         }
1181
1182         /* Check for variables with no value */
1183         if (valueStart >= tvb_reported_length (tvb))
1184         {
1185                 valueStart = tvb_reported_length (tvb);
1186                 valueEnd = valueStart;
1187         }
1188         valueLength = valueEnd-valueStart;
1189
1190         proto_tree_add_text (tree, tvb, variableStart, valueEnd-variableStart, "%s: %s", variableBuffer, valueBuffer);
1191
1192         g_free (variableBuffer);
1193         g_free (valueBuffer);
1194 }
1195
1196 /* Register the protocol with Ethereal */
1197 void
1198 proto_register_wsp(void)
1199 {                 
1200
1201 /* Setup list of header fields */
1202         static hf_register_info hf[] = {
1203                 { &hf_wsp_header_tid,
1204                         {       "Transmission ID",           
1205                                 "wsp.TID",
1206                                  FT_UINT8, BASE_HEX, NULL, 0x00,
1207                                 "Transmission ID" 
1208                         }
1209                 },
1210                 { &hf_wsp_header_pdu_type,
1211                         {       "PDU Type",           
1212                                 "wsp.pdu-type",
1213                                  FT_UINT8, BASE_HEX, VALS( vals_pdu_type ), 0x00,
1214                                 "PDU Type" 
1215                         }
1216                 },
1217                 { &hf_wsp_version_major,
1218                         {       "Version (Major)",           
1219                                 "wsp.version.major",
1220                                  FT_UINT8, BASE_DEC, NULL, 0xF0,
1221                                 "Version (Major)" 
1222                         }
1223                 },
1224                 { &hf_wsp_version_minor,
1225                         {       "Version (Minor)",           
1226                                 "wsp.version.minor",
1227                                  FT_UINT8, BASE_DEC, NULL, 0x0F,
1228                                 "Version (Minor)" 
1229                         }
1230                 },
1231                 { &hf_wsp_capability_length,
1232                         {       "Capability Length",           
1233                                 "wsp.capability.length",
1234                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1235                                 "Capability Length" 
1236                         }
1237                 },
1238                 { &hf_wsp_header_length,
1239                         {       "Headers Length",           
1240                                 "wsp.headers-length",
1241                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1242                                 "Headers Length" 
1243                         }
1244                 },
1245                 { &hf_wsp_capabilities_section,
1246                         {       "Capabilities",           
1247                                 "wsp.capabilities",
1248                                  FT_NONE, BASE_DEC, NULL, 0x00,
1249                                 "Capabilities" 
1250                         }
1251                 },
1252                 { &hf_wsp_headers_section,
1253                         {       "Headers",           
1254                                 "wsp.headers",
1255                                  FT_NONE, BASE_DEC, NULL, 0x00,
1256                                 "Headers" 
1257                         }
1258                 },
1259                 { &hf_wsp_header,
1260                         {       "Header",           
1261                                 "wsp.headers.header",
1262                                  FT_NONE, BASE_DEC, NULL, 0x00,
1263                                 "Header" 
1264                         }
1265                 },
1266                 { &hf_wsp_header_uri_len,
1267                         {       "URI Length",           
1268                                 "wsp.uri-length",
1269                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1270                                 "URI Length" 
1271                         }
1272                 },
1273                 { &hf_wsp_header_uri,
1274                         {       "URI",           
1275                                 "wsp.uri",
1276                                  FT_STRING, BASE_NONE, NULL, 0x00,
1277                                 "URI" 
1278                         }
1279                 },
1280                 { &hf_wsp_server_session_id,
1281                         {       "Server Session ID",           
1282                                 "wsp.server.session-id",
1283                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1284                                 "Server Session ID" 
1285                         }
1286                 },
1287                 { &hf_wsp_header_status,
1288                         {       "Status",           
1289                                 "wsp.reply.status",
1290                                  FT_UINT8, BASE_HEX, VALS( vals_status ), 0x00,
1291                                 "Status" 
1292                         }
1293                 },
1294                 { &hf_wsp_content_type,
1295                         {       "Content Type",           
1296                                 "wsp.content-type.type",
1297                                  FT_UINT8, BASE_HEX, VALS ( vals_content_types ), 0x00,
1298                                 "Content Type" 
1299                         }
1300                 },
1301                 { &hf_wsp_parameter_well_known_charset,
1302                         {       "Charset",           
1303                                 "wsp.content-type.parameter.charset",
1304                                  FT_UINT16, BASE_HEX, VALS ( vals_character_sets ), 0x00,
1305                                 "Charset" 
1306                         }
1307                 },
1308                 { &hf_wsp_reply_data,
1309                         {       "Data",           
1310                                 "wsp.reply.data",
1311                                  FT_NONE, BASE_NONE, NULL, 0x00,
1312                                 "Data" 
1313                         }
1314                 },
1315                 { &hf_wsp_header_accept,
1316                         {       "Accept",           
1317                                 "wsp.header.accept",
1318                                  /*FT_NONE, BASE_DEC, NULL, 0x00,*/
1319                                  FT_UINT8, BASE_HEX, VALS ( vals_content_types ), 0x00,
1320                                 "Accept" 
1321                         }
1322                 },
1323                 { &hf_wsp_header_accept_str,
1324                         {       "Accept",           
1325                                 "wsp.header.accept.string",
1326                                  FT_STRING, BASE_NONE, NULL, 0x00,
1327                                 "Accept" 
1328                         }
1329                 },
1330                 { &hf_wsp_header_accept_charset,
1331                         {       "Accept-Charset",           
1332                                 "wsp.header.accept-charset",
1333                                  FT_UINT16, BASE_HEX, VALS ( vals_character_sets ), 0x00,
1334                                 "Accept-Charset" 
1335                         }
1336                 },
1337                 { &hf_wsp_header_accept_language,
1338                         {       "Accept-Language",           
1339                                 "wsp.header.accept-language",
1340                                  FT_UINT8, BASE_HEX, VALS ( vals_languages ), 0x00,
1341                                 "Accept-Language" 
1342                         }
1343                 },
1344                 { &hf_wsp_header_accept_ranges,
1345                         {       "Accept-Ranges",           
1346                                 "wsp.header.accept-ranges",
1347                                  FT_UINT8, BASE_HEX, VALS ( vals_accept_ranges ), 0x00,
1348                                 "Accept-Ranges" 
1349                         }
1350                 },
1351                 { &hf_wsp_header_age,
1352                         {       "Age",           
1353                                 "wsp.header.age",
1354                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1355                                 "Age" 
1356                         }
1357                 },
1358                 { &hf_wsp_header_cache_control,
1359                         {       "Cache-Control",           
1360                                 "wsp.header.cache-control",
1361                                  FT_UINT8, BASE_HEX, VALS ( vals_cache_control ), 0x00,
1362                                 "Cache-Control" 
1363                         }
1364                 },
1365                 { &hf_wsp_header_content_length,
1366                         {       "Content-Length",           
1367                                 "wsp.header.content-length",
1368                                  FT_UINT32, BASE_DEC, NULL, 0x00,
1369                                 "Content-Length" 
1370                         }
1371                 },
1372                 { &hf_wsp_header_date,
1373                         {       "Date",           
1374                                 "wsp.header.date",
1375                                  FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1376                                 "Date" 
1377                         }
1378                 },
1379                 { &hf_wsp_header_etag,
1380                         {       "Etag",           
1381                                 "wsp.header.etag",
1382                                  /*FT_NONE, BASE_DEC, NULL, 0x00,*/
1383                                  FT_STRING, BASE_NONE, NULL, 0x00,
1384                                 "Etag" 
1385                         }
1386                 },
1387                 { &hf_wsp_header_expires,
1388                         {       "Expires",           
1389                                 "wsp.header.expires",
1390                                  FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1391                                 "Expires" 
1392                         }
1393                 },
1394                 { &hf_wsp_header_last_modified,
1395                         {       "Last-Modified",           
1396                                 "wsp.header.last-modified",
1397                                  FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1398                                 "Last-Modified" 
1399                         }
1400                 },
1401                 { &hf_wsp_header_location,
1402                         {       "Location",           
1403                                 "wsp.header.location",
1404                                  FT_STRING, BASE_NONE, NULL, 0x00,
1405                                 "Location" 
1406                         }
1407                 },
1408                 { &hf_wsp_header_if_modified_since,
1409                         {       "If-Modified-Since",           
1410                                 "wsp.header.if-modified-since",
1411                                  FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1412                                 "If-Modified-Since" 
1413                         }
1414                 },
1415                 { &hf_wsp_header_server,
1416                         {       "Server",           
1417                                 "wsp.header.server",
1418                                  /*FT_NONE, BASE_DEC, NULL, 0x00,*/
1419                                  FT_STRING, BASE_NONE, NULL, 0x00,
1420                                 "Server" 
1421                         }
1422                 },
1423                 { &hf_wsp_header_user_agent,
1424                         {       "User-Agent",           
1425                                 "wsp.header.user-agent",
1426                                  /*FT_NONE, BASE_DEC, NULL, 0x00,*/
1427                                  FT_STRING, BASE_NONE, NULL, 0x00,
1428                                 "User-Agent" 
1429                         }
1430                 },
1431                 { &hf_wsp_header_application_header,
1432                         {       "Application Header",           
1433                                 "wsp.header.application-header",
1434                                  FT_STRING, BASE_NONE, NULL, 0x00,
1435                                 "Application Header" 
1436                         }
1437                 },
1438                 { &hf_wsp_header_application_value,
1439                         {       "Application Header Value",           
1440                                 "wsp.header.application-header.value",
1441                                  FT_STRING, BASE_NONE, NULL, 0x00,
1442                                 "Application Header Value" 
1443                         }
1444                 },
1445                 { &hf_wsp_header_x_wap_tod,
1446                         {       "X-WAP.TOD",           
1447                                 "wsp.header.x_wap_tod",
1448                                  FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1449                                 "X-WAP.TOD" 
1450                         }
1451                 },
1452                 { &hf_wsp_post_data,
1453                         {       "Post Data",           
1454                                 "wsp.post.data",
1455                                  FT_NONE, BASE_NONE, NULL, 0x00,
1456                                 "Post Data" 
1457                         }
1458                 },
1459         };
1460         
1461 /* Setup protocol subtree array */
1462         static gint *ett[] = {
1463                 &ett_wsp,
1464                 &ett_header,
1465                 &ett_headers,
1466                 &ett_capabilities,
1467                 &ett_content_type,
1468         };
1469
1470 /* Register the protocol name and description */
1471         proto_wsp = proto_register_protocol(
1472                 "Wireless Session Protocol",    /* protocol name for use by ethereal */ 
1473                 "wap-wsp"                       /* Abbreviated protocol name, should Match IANA 
1474                                                     < URL:http://www.isi.edu/in-notes/iana/assignments/port-numbers/ >
1475                                                   */
1476         );
1477
1478 /* Required function calls to register the header fields and subtrees used  */
1479         proto_register_field_array(proto_wsp, hf, array_length(hf));
1480         proto_register_subtree_array(ett, array_length(ett));
1481 };
1482
1483 void
1484 proto_reg_handoff_wsp(void)
1485 {
1486         /* Only connection-less WSP has no previous handler */
1487 //      dissector_add("udp.port", UDP_PORT_WSP, dissect_wsp_no_tvbuff);
1488         dissector_add("udp.port", UDP_PORT_WSP, dissect_wsp);
1489         /* dissector_add("udp.port", UDP_PORT_WTP_WSP, dissect_wsp_no_tvbuff); */
1490         /* dissector_add("udp.port", UDP_PORT_WTLS_WSP, dissect_wsp_no_tvbuff); */
1491         /* dissector_add("udp.port", UDP_PORT_WTLS_WTP_WSP, dissect_wsp_no_tvbuff); */
1492 }