Add the RFC 3203 FORCERENEW message type, as suggested by Suresh K.
[obnox/wireshark/wip.git] / packet-wtp.c
1 /* packet-wtp.c
2  *
3  * Routines to dissect WTP component of WAP traffic.
4  *
5  * $Id: packet-wtp.c,v 1.42 2002/12/19 11:22:38 sahlberg Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * WAP dissector based on original work by Ben Fowler
12  * Updated by Neil Hunter <neil.hunter@energis-squared.com>
13  * WTLS support by Alexandre P. Ferreira (Splice IP)
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #ifdef NEED_SNPRINTF_H
38 # include "snprintf.h"
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include <epan/packet.h>
44 #include "reassemble.h"
45 #include "packet-wap.h"
46 #include "packet-wtp.h"
47 #include "packet-wsp.h"
48
49 static const true_false_string continue_truth = {
50     "TPI Present" ,
51     "No TPI"
52 };
53
54 static const true_false_string RID_truth = {
55     "Re-Transmission",
56     "First transmission"
57 };
58
59 static const true_false_string TIDNew_truth = {
60     "TID is new" ,
61     "TID is valid"
62 };
63
64 static const true_false_string tid_response_truth = {
65     "Response" ,
66     "Original"
67 };
68
69 static const true_false_string UP_truth = {
70     "User Acknowledgement required" ,
71     "User Acknowledgement optional"
72 };
73
74 static const true_false_string TVETOK_truth = {
75     "True",
76     "False"
77 };
78
79 static const value_string vals_pdu_type[] = {
80     { 0, "Not Allowed" },
81     { 1, "Invoke" },
82     { 2, "Result" },
83     { 3, "Ack" },
84     { 4, "Abort" },
85     { 5, "Segmented Invoke" },
86     { 6, "Segmented Result" },
87     { 7, "Negative Ack" },
88     { 0, NULL }
89 };
90
91 static const value_string vals_transaction_trailer[] = {
92     { 0, "Not last packet" },
93     { 1, "Last packet of message" },
94     { 2, "Last packet of group" },
95     { 3, "Re-assembly not supported" },
96     { 0, NULL }
97 };
98
99 static const value_string vals_version[] = {
100     { 0, "Current" },
101     { 1, "Undefined" },
102     { 2, "Undefined" },
103     { 3, "Undefined" },
104     { 0, NULL }
105 };
106
107 static const value_string vals_abort_type[] = {
108     { 0, "Provider" },
109     { 1, "User (WSP)" },
110     { 0, NULL }
111 };
112
113 static const value_string vals_abort_reason_provider[] = {
114     { 0x00, "Unknown" },
115     { 0x01, "Protocol Error" },
116     { 0x02, "Invalid TID" },
117     { 0x03, "Not Implemented Class 2" },
118     { 0x04, "Not Implemented SAR" },
119     { 0x05, "Not Implemented User Acknowledgement" },
120     { 0x06, "WTP Version Zero" },
121     { 0x07, "Capacity Temporarily Exceeded" },
122     { 0x08, "No Response" },
123     { 0x09, "Message Too Large" },
124     { 0x00, NULL }
125 };
126
127 static const value_string vals_transaction_classes[] = {
128     { 0x00, "Unreliable Invoke without Result" },
129     { 0x01, "Reliable Invoke without Result" },
130     { 0x02, "Reliable Invoke with Reliable Result" },
131     { 0x00, NULL }
132 };
133
134 static const value_string vals_tpi_type[] = {
135     { 0x00, "Error" },
136     { 0x01, "Info" },
137     { 0x02, "Option" },
138     { 0x03, "Packet sequence number" },
139     { 0x04, "SDU boundary" },
140     { 0x05, "Frame boundary" },
141     { 0x00, NULL }
142 };
143
144 static const value_string vals_tpi_opt[] = {
145     { 0x01, "Maximum receive unit" },
146     { 0x02, "Total message size" },
147     { 0x03, "Delay transmission timer" },
148     { 0x04, "Maximum group" },
149     { 0x05, "Current TID" },
150     { 0x06, "No cached TID" },
151     { 0x00, NULL }
152 };
153
154 /* File scoped variables for the protocol and registered fields */
155 static int proto_wtp                            = HF_EMPTY;
156
157 /* These fields used by fixed part of header */
158 static int hf_wtp_header_sub_pdu_size           = HF_EMPTY;
159 static int hf_wtp_header_flag_continue          = HF_EMPTY;
160 static int hf_wtp_header_pdu_type               = HF_EMPTY;
161 static int hf_wtp_header_flag_Trailer           = HF_EMPTY;
162 static int hf_wtp_header_flag_RID               = HF_EMPTY;
163 static int hf_wtp_header_flag_TID               = HF_EMPTY;
164 static int hf_wtp_header_flag_TID_response      = HF_EMPTY;
165
166 /* These fields used by Invoke packets */
167 static int hf_wtp_header_Inv_version            = HF_EMPTY;
168 static int hf_wtp_header_Inv_flag_TIDNew        = HF_EMPTY;
169 static int hf_wtp_header_Inv_flag_UP            = HF_EMPTY;
170 static int hf_wtp_header_Inv_Reserved           = HF_EMPTY;
171 static int hf_wtp_header_Inv_TransactionClass   = HF_EMPTY;
172
173
174 static int hf_wtp_header_variable_part          = HF_EMPTY;
175 static int hf_wtp_data                          = HF_EMPTY;
176
177 static int hf_wtp_tpi_type                      = HF_EMPTY;
178 static int hf_wtp_tpi_psn                       = HF_EMPTY;
179 static int hf_wtp_tpi_opt                       = HF_EMPTY;
180 static int hf_wtp_tpi_optval                    = HF_EMPTY;
181 static int hf_wtp_tpi_info                      = HF_EMPTY;
182
183 static int hf_wtp_header_Ack_flag_TVETOK        = HF_EMPTY;
184 static int hf_wtp_header_Abort_type             = HF_EMPTY;
185 static int hf_wtp_header_Abort_reason_provider  = HF_EMPTY;
186 static int hf_wtp_header_Abort_reason_user      = HF_EMPTY;
187 static int hf_wtp_header_sequence_number        = HF_EMPTY;
188 static int hf_wtp_header_missing_packets        = HF_EMPTY;
189
190 /* These fields used when reassembling WTP fragments */
191 static int hf_wtp_fragments                     = HF_EMPTY;
192 static int hf_wtp_fragment                      = HF_EMPTY;
193 static int hf_wtp_fragment_overlap              = HF_EMPTY;
194 static int hf_wtp_fragment_overlap_conflict     = HF_EMPTY;
195 static int hf_wtp_fragment_multiple_tails       = HF_EMPTY;
196 static int hf_wtp_fragment_too_long_fragment    = HF_EMPTY;
197 static int hf_wtp_fragment_error                = HF_EMPTY;
198
199 /* Initialize the subtree pointers */
200 static gint ett_wtp                             = ETT_EMPTY;
201 static gint ett_header                          = ETT_EMPTY;
202 static gint ett_tpilist                         = ETT_EMPTY;
203 static gint ett_wsp_fragments                   = ETT_EMPTY;
204 static gint ett_wtp_fragment                    = ETT_EMPTY;
205
206 static const fragment_items wtp_frag_items = {
207     &ett_wtp_fragment,
208     &ett_wsp_fragments,
209     &hf_wtp_fragments,
210     &hf_wtp_fragment,
211     &hf_wtp_fragment_overlap,
212     &hf_wtp_fragment_overlap_conflict,
213     &hf_wtp_fragment_multiple_tails,
214     &hf_wtp_fragment_too_long_fragment,
215     &hf_wtp_fragment_error,
216     "fragments"
217 };
218
219 /* Handle for WSP dissector */
220 static dissector_handle_t wsp_handle;
221
222 /*
223  * reassembly of WSP
224  */
225 static GHashTable       *wtp_fragment_table = NULL;
226
227 static void
228 wtp_defragment_init(void)
229 {
230     fragment_table_init(&wtp_fragment_table);
231 }
232
233 /*
234  * Extract some bitfields
235  */
236 #define pdu_type(octet)                 (((octet) >> 3) & 0x0F) /* Note pdu type must not be 0x00 */
237 #define transaction_class(octet)        ((octet) & 0x03)        /* ......XX */
238 #define transmission_trailer(octet)     (((octet) >> 1) & 0x01) /* ......X. */
239
240 static char retransmission_indicator(unsigned char octet)
241 {
242     switch (pdu_type(octet)) {
243         case INVOKE:
244         case RESULT:
245         case ACK:
246         case SEGMENTED_INVOKE:
247         case SEGMENTED_RESULT:
248         case NEGATIVE_ACK:
249             return octet & 0x01;        /* .......X */
250         default:
251             return 0;
252     }
253 }
254
255 /*
256  * dissect a TPI
257  */
258 static void
259 wtp_handle_tpi(proto_tree *tree, tvbuff_t *tvb)
260 {
261     int                  offset = 0;
262     unsigned char        tByte;
263     unsigned char        tType;
264     unsigned char        tLen;
265     proto_item          *subTree = NULL;
266
267     tByte = tvb_get_guint8(tvb, offset++);
268     tType = (tByte & 0x78) >> 3;
269     if (tByte & 0x04)                           /* Long TPI     */
270         tLen = tvb_get_guint8(tvb, offset++);
271     else
272         tLen = tByte & 0x03;
273     subTree = proto_tree_add_uint(tree, hf_wtp_tpi_type,
274                                   tvb, 0, tvb_length(tvb), tType);
275     proto_item_add_subtree(subTree, ett_tpilist);
276     switch (tType) {
277         case 0x00:                      /* Error*/
278             /* \todo    */
279             break;
280         case 0x01:                      /* Info */
281             /* Beware, untested case here       */
282             proto_tree_add_item(subTree, hf_wtp_tpi_info,
283                                 tvb, offset, tLen, bo_little_endian);
284             break;
285         case 0x02:                      /* Option       */
286             proto_tree_add_item(subTree, hf_wtp_tpi_opt,
287                                 tvb, offset++, 1, bo_little_endian);
288             proto_tree_add_item(subTree, hf_wtp_tpi_optval,
289                                 tvb, offset, tLen - 1, bo_little_endian);
290             break;
291         case 0x03:                      /* PSN  */
292             proto_tree_add_item(subTree, hf_wtp_tpi_psn,
293                                 tvb, offset, 1, bo_little_endian);
294             break;
295         case 0x04:                      /* SDU boundary */
296             /* \todo    */
297             break;
298         case 0x05:                      /* Frame boundary       */
299             /* \todo    */
300             break;
301         default:
302             break;
303     }
304 }
305
306 /* Code to actually dissect the packets */
307 static void
308 dissect_wtp_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
309 {
310     char        szInfo[50];
311     int         offCur          = 0; /* current offset from start of WTP data */
312
313     unsigned char  b0;
314
315     /* continuation flag */
316     unsigned char       fCon;                   /* Continue flag        */
317     unsigned char       fRID;                   /* Re-transmission indicator*/
318     unsigned char       fTTR = '\0';            /* Transmission trailer */
319     guint               cbHeader        = 0;    /* Fixed header length  */
320     guint               vHeader         = 0;    /* Variable header length*/
321     int                 abortType       = 0;
322
323     /* Set up structures we'll need to add the protocol subtree and manage it */
324     proto_item          *ti;
325     proto_tree          *wtp_tree = NULL;
326
327     char                pdut;
328     char                clsTransaction  = ' ';
329     int                 cchInfo;
330     int                 numMissing = 0;         /* Number of missing packets in a negative ack */
331     int                 i;
332     tvbuff_t            *wsp_tvb = NULL;
333     fragment_data       *fd_head = NULL;
334     guint8              psn = 0;                /* Packet sequence number*/
335     guint16             TID = 0;                /* Transaction-Id       */
336
337     b0 = tvb_get_guint8 (tvb, offCur + 0);
338     /* Discover Concatenated PDUs */
339     if (b0 == 0) {
340         guint   c_fieldlen = 0;         /* Length of length-field       */
341         guint   c_pdulen = 0;           /* Length of conc. PDU  */
342
343         if (tree) {
344             ti = proto_tree_add_item(tree, proto_wtp,
345                                     tvb, offCur, 1, bo_little_endian);
346             wtp_tree = proto_item_add_subtree(ti, ett_wtp);
347         }
348         offCur = 1;
349         i = 1;
350         while (offCur < (int) tvb_reported_length(tvb)) {
351             b0 = tvb_get_guint8(tvb, offCur + 0);
352             if (b0 & 0x80) {
353                 c_fieldlen = 2;
354                 c_pdulen = ((b0 & 0x7f) << 8) | tvb_get_guint8(tvb, offCur + 1);
355             } else {
356                 c_fieldlen = 1;
357                 c_pdulen = b0;
358             }
359             if (tree) {
360                 proto_tree_add_item(wtp_tree, hf_wtp_header_sub_pdu_size,
361                                     tvb, offCur, c_fieldlen, bo_big_endian);
362             }
363             if (i > 1 && check_col(pinfo->cinfo, COL_INFO)) {
364                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
365             }
366             wsp_tvb = tvb_new_subset(tvb, offCur + c_fieldlen, -1, c_pdulen);
367             dissect_wtp_common(wsp_tvb, pinfo, wtp_tree);
368             offCur += c_fieldlen + c_pdulen;
369             i++;
370         }
371         return;
372     }
373     fCon = b0 & 0x80;
374     fRID = retransmission_indicator(b0);
375     pdut = pdu_type(b0);
376
377     /* Develop the string to put in the Info column */
378     cchInfo = snprintf(szInfo, sizeof( szInfo ), "WTP %s",
379                     val_to_str(pdut, vals_pdu_type, "Unknown PDU type 0x%x"));
380
381     switch (pdut) {
382         case INVOKE:
383             fTTR = transmission_trailer(b0);
384             TID = tvb_get_ntohs(tvb, offCur + 1);
385             psn = 0;
386             clsTransaction = transaction_class(tvb_get_guint8(tvb, offCur + 3));
387             snprintf(szInfo + cchInfo, sizeof(szInfo) - cchInfo,
388                      " Class %d", clsTransaction);
389             cbHeader = 4;
390             break;
391
392         case SEGMENTED_INVOKE:
393         case SEGMENTED_RESULT:
394             fTTR = transmission_trailer(b0);
395             TID = tvb_get_ntohs(tvb, offCur + 1);
396             psn = tvb_get_guint8(tvb, offCur + 3);
397             cbHeader = 4;
398             break;
399
400         case ABORT:
401             cbHeader = 4;
402             break;
403
404         case RESULT:
405             fTTR = transmission_trailer(b0);
406             TID = tvb_get_ntohs(tvb, offCur + 1);
407             psn = 0;
408             cbHeader = 3;
409             break;
410
411         case ACK:
412             cbHeader = 3;
413             break;
414
415         case NEGATIVE_ACK:
416             /* Variable number of missing packets */
417             numMissing = tvb_get_guint8(tvb, offCur + 3);
418             cbHeader = numMissing + 4;
419             break;
420
421         default:
422             break;
423     };
424     if (fRID) {
425         strcat( szInfo, " R" );
426     };
427     if (fCon) {                         /* Scan variable part (TPI's),  */
428                                         /* determine length of it       */
429         unsigned char   tCon;
430         unsigned char   tByte;
431
432         do {
433             tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
434             tCon = tByte & 0x80;
435             if (tByte & 0x04)   /* Long format  */
436                 vHeader = vHeader + tvb_get_guint8(tvb,
437                                         offCur + cbHeader + vHeader + 1) + 2;
438             else
439                 vHeader = vHeader + (tByte & 0x03) + 1;
440         } while (tCon);
441     }
442
443 #ifdef DEBUG
444     fprintf( stderr, "dissect_wtp: cbHeader = %d\n", cbHeader );
445 #endif
446
447     /* Only update "Info" column when no data in this PDU will
448      * be handed off to a subsequent dissector.
449      */
450     if (check_col(pinfo->cinfo, COL_INFO) &&
451         ((tvb_length_remaining(tvb, offCur + cbHeader + vHeader) <= 0) ||
452          (pdut == ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)) ) {
453 #ifdef DEBUG
454         fprintf(stderr, "dissect_wtp: (6) About to set info_col header to %s\n", szInfo);
455 #endif
456         col_append_str(pinfo->cinfo, COL_INFO, szInfo);
457     };
458     /* In the interest of speed, if "tree" is NULL, don't do any work not
459        necessary to generate protocol tree items. */
460     if (tree) {
461 #ifdef DEBUG
462         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
463 #endif
464         ti = proto_tree_add_item(tree, proto_wtp, tvb, offCur, cbHeader + vHeader, bo_little_endian);
465 #ifdef DEBUG
466         fprintf(stderr, "dissect_wtp: (7) Returned from proto_tree_add_item\n");
467 #endif
468         wtp_tree = proto_item_add_subtree(ti, ett_wtp);
469
470 /* Code to process the packet goes here */
471 #ifdef DEBUG
472         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
473         fprintf(stderr, "dissect_wtp: offCur = %d\n", offCur);
474 #endif
475         /* Add common items: only CON and PDU Type */
476         proto_tree_add_item(
477                         wtp_tree,                       /* tree */
478                         hf_wtp_header_flag_continue,    /* id */
479                         tvb,
480                         offCur,                         /* start of highlight */
481                         1,                              /* length of highlight*/
482                         b0                              /* value */
483              );
484         proto_tree_add_item(wtp_tree, hf_wtp_header_pdu_type, tvb, offCur, 1, bo_little_endian);
485
486         switch(pdut) {
487             case INVOKE:
488                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
489                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
490                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
491                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
492
493                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_version , tvb, offCur + 3, 1, bo_little_endian);
494                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_TIDNew, tvb, offCur + 3, 1, bo_little_endian);
495                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_UP, tvb, offCur + 3, 1, bo_little_endian);
496                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_Reserved, tvb, offCur + 3, 1, bo_little_endian);
497                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_TransactionClass, tvb, offCur + 3, 1, bo_little_endian);
498                 break;
499
500             case RESULT:
501                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
502                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
503                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
504                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
505                 break;
506
507             case ACK:
508                 proto_tree_add_item(wtp_tree, hf_wtp_header_Ack_flag_TVETOK, tvb, offCur, 1, bo_big_endian);
509
510                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
511                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
512                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
513                 break;
514
515             case ABORT:
516                 abortType = tvb_get_guint8 (tvb, offCur) & 0x07;
517                 proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_type , tvb, offCur , 1, bo_little_endian);
518                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
519                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
520
521                 if (abortType == PROVIDER)
522                 {
523                     proto_tree_add_item( wtp_tree, hf_wtp_header_Abort_reason_provider , tvb, offCur + 3 , 1, bo_little_endian);
524                 }
525                 else if (abortType == USER)
526                 {
527                     proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_reason_user , tvb, offCur + 3 , 1, bo_little_endian);
528                 }
529                 break;
530
531             case SEGMENTED_INVOKE:
532                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
533                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
534                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
535                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
536
537                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
538                 break;
539
540             case SEGMENTED_RESULT:
541                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
542                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
543                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
544                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
545
546                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
547                 break;
548
549             case NEGATIVE_ACK:
550                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
551                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
552                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
553
554                 proto_tree_add_item(wtp_tree, hf_wtp_header_missing_packets , tvb, offCur + 3, 1, bo_little_endian);
555                 /* Iterate through missing packets */
556                 for (i = 0; i < numMissing; i++)
557                 {
558                     proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number, tvb, offCur + i, 1, bo_little_endian);
559                 }
560                 break;
561
562             default:
563                 break;
564         };
565         if (fCon) {                     /* Now, analyze variable part   */
566             unsigned char        tCon;
567             unsigned char        tByte;
568             unsigned char        tpiLen;
569             tvbuff_t            *tmp_tvb;
570
571             vHeader = 0;                /* Start scan all over  */
572
573             do {
574                 tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
575                 tCon = tByte & 0x80;
576                 if (tByte & 0x04)       /* Long TPI     */
577                     tpiLen = 2 + tvb_get_guint8(tvb,
578                                             offCur + cbHeader + vHeader + 1);
579                 else
580                     tpiLen = 1 + (tByte & 0x03);
581                 tmp_tvb = tvb_new_subset(tvb, offCur + cbHeader + vHeader,
582                                         tpiLen, tpiLen);
583                 wtp_handle_tpi(wtp_tree, tmp_tvb);
584                 vHeader += tpiLen;
585             } while (tCon);
586         } else {
587                 /* There is no variable part */
588         }       /* End of variable part of header */
589     } else {
590 #ifdef DEBUG
591         fprintf(stderr, "dissect_wtp: (4) tree was %p\n", tree);
592 #endif
593     }
594     /*
595      * Any remaining data ought to be WSP data (if not WTP ACK, NACK
596      * or ABORT pdu), so hand off (defragmented) to the WSP dissector
597      */
598     if ((tvb_length_remaining(tvb, offCur + cbHeader + vHeader) > 0) &&
599         ! ((pdut==ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)))
600     {
601         int     dataOffset = offCur + cbHeader + vHeader;
602         guint32 dataLen = tvb_length_remaining(tvb, offCur + cbHeader + vHeader);
603         gboolean save_fragmented;
604
605         if ((pdut == SEGMENTED_INVOKE) || (pdut == SEGMENTED_RESULT) ||
606             (((pdut == INVOKE) || (pdut == RESULT)) && (!fTTR)))
607         {                                       /* 1st part of segment  */
608             save_fragmented = pinfo->fragmented;
609             pinfo->fragmented = TRUE;
610             fd_head = fragment_add_seq(tvb, dataOffset, pinfo, TID,
611                             wtp_fragment_table, psn, dataLen, !fTTR);
612             if (fd_head != NULL)                /* Reassembled  */
613             {
614                 wsp_tvb = tvb_new_real_data(fd_head->data,
615                                             fd_head->len,
616                                             fd_head->len);
617                 tvb_set_child_real_data_tvbuff(tvb, wsp_tvb);
618                 add_new_data_source(pinfo, wsp_tvb,
619                                         "Reassembled WTP");
620                 pinfo->fragmented = FALSE;
621
622                 /* show all fragments */
623                 show_fragment_seq_tree(fd_head, &wtp_frag_items,
624                                         wtp_tree, pinfo, wsp_tvb);
625
626                 call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
627             }
628             else
629             {
630                 if (check_col(pinfo->cinfo, COL_INFO))          /* Won't call WSP so display */
631                     col_append_str(pinfo->cinfo, COL_INFO, szInfo);
632             }
633             pinfo->fragmented = save_fragmented;
634         }
635         else                    /* Normal packet, call next dissector   */
636         {
637             wsp_tvb = tvb_new_subset(tvb, dataOffset, -1, dataLen);
638             call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
639         }
640     }
641 }
642
643 /*
644  * Called directly from UDP.
645  * Put "WTP+WSP" into the "Protocol" column.
646  */
647 static void
648 dissect_wtp_fromudp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
649 {
650     if (check_col(pinfo->cinfo, COL_PROTOCOL))
651         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTP+WSP" );
652     if (check_col(pinfo->cinfo, COL_INFO))
653         col_clear(pinfo->cinfo, COL_INFO);
654
655     dissect_wtp_common(tvb, pinfo, tree);
656 }
657
658 /*
659  * Called from a higher-level WAP dissector, presumably WTLS.
660  * Put "WTLS+WSP+WTP" to the "Protocol" column.
661  *
662  * XXX - is this supposed to be called from WTLS?  If so, we're not
663  * calling it....
664  *
665  * XXX - can this be called from any other dissector?
666  */
667 static void
668 dissect_wtp_fromwap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
669 {
670     if (check_col(pinfo->cinfo, COL_PROTOCOL))
671         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTLS+WTP+WSP" );
672     if (check_col(pinfo->cinfo, COL_INFO))
673         col_clear(pinfo->cinfo, COL_INFO);
674
675     dissect_wtp_common(tvb, pinfo, tree);
676 }
677
678 /* Register the protocol with Ethereal */
679 void
680 proto_register_wtp(void)
681 {
682
683     /* Setup list of header fields */
684     static hf_register_info hf[] = {
685         { &hf_wtp_header_sub_pdu_size,
686             {   "Sub PDU size",
687                 "wtp.sub_pdu_size",
688                 FT_BYTES, BASE_HEX, NULL, 0x0,
689                 "Size of Sub-PDU", HFILL
690             }
691         },
692         { &hf_wtp_header_flag_continue,
693             {   "Continue Flag",
694                 "wtp.continue_flag",
695                 FT_BOOLEAN, 8, TFS( &continue_truth ), 0x80,
696                 "Continue Flag", HFILL
697             }
698         },
699         { &hf_wtp_header_pdu_type,
700             {   "PDU Type",
701                 "wtp.pdu_type",
702                 FT_UINT8, BASE_HEX, VALS( vals_pdu_type ), 0x78,
703                 "PDU Type", HFILL
704             }
705         },
706         { &hf_wtp_header_flag_Trailer,
707             {   "Trailer Flags",
708                 "wtp.trailer_flags",
709                 FT_UINT8, BASE_HEX, VALS( vals_transaction_trailer ), 0x06,
710                 "Trailer Flags", HFILL
711             }
712         },
713         { &hf_wtp_header_flag_RID,
714             {   "Re-transmission Indicator",
715                 "wtp.RID",
716                 FT_BOOLEAN, 8, TFS( &RID_truth ), 0x01,
717                 "Re-transmission Indicator", HFILL
718             }
719         },
720         { &hf_wtp_header_flag_TID_response,
721             {   "TID Response",
722                 "wtp.TID.response",
723                 FT_BOOLEAN, 16, TFS( &tid_response_truth ), 0x8000,
724                 "TID Response", HFILL
725             }
726         },
727         { &hf_wtp_header_flag_TID,
728             {   "Transaction ID",
729                 "wtp.TID",
730                 FT_UINT16, BASE_HEX, NULL, 0x7FFF,
731                 "Transaction ID", HFILL
732             }
733         },
734         { &hf_wtp_header_Inv_version,
735             {   "Version",
736                 "wtp.header.version",
737                 FT_UINT8, BASE_HEX, VALS( vals_version ), 0xC0,
738                 "Version", HFILL
739             }
740         },
741         { &hf_wtp_header_Inv_flag_TIDNew,
742             {   "TIDNew",
743                 "wtp.header.TIDNew",
744                 FT_BOOLEAN, 8, TFS( &TIDNew_truth ), 0x20,
745                 "TIDNew", HFILL
746             }
747         },
748         { &hf_wtp_header_Inv_flag_UP,
749             {   "U/P flag",
750                 "wtp.header.UP",
751                 FT_BOOLEAN, 8, TFS( &UP_truth ), 0x10,
752                 "U/P Flag", HFILL
753             }
754         },
755         { &hf_wtp_header_Inv_Reserved,
756             {   "Reserved",
757                 "wtp.inv.reserved",
758                 FT_UINT8, BASE_HEX, NULL, 0x0C,
759                 "Reserved", HFILL
760             }
761         },
762         { &hf_wtp_header_Inv_TransactionClass,
763             {   "Transaction Class",
764                 "wtp.inv.transaction_class",
765                 FT_UINT8, BASE_HEX, VALS( vals_transaction_classes ), 0x03,
766                 "Transaction Class", HFILL
767             }
768         },
769         { &hf_wtp_header_Ack_flag_TVETOK,
770             {   "Tve/Tok flag",
771                 "wtp.ack.tvetok",
772                 FT_BOOLEAN, 8, TFS( &TVETOK_truth ), 0x04,
773                 "Tve/Tok flag", HFILL
774             }
775         },
776         { &hf_wtp_header_Abort_type,
777             {   "Abort Type",
778                 "wtp.abort.type",
779                 FT_UINT8, BASE_HEX, VALS ( vals_abort_type ), 0x07,
780                 "Abort Type", HFILL
781             }
782         },
783         { &hf_wtp_header_Abort_reason_provider,
784             {   "Abort Reason",
785                 "wtp.abort.reason.provider",
786                 FT_UINT8, BASE_HEX, VALS ( vals_abort_reason_provider ), 0x00,
787                 "Abort Reason", HFILL
788             }
789         },
790         /* Assume WSP is the user and use its reason codes */
791         { &hf_wtp_header_Abort_reason_user,
792             {   "Abort Reason",
793                 "wtp.abort.reason.user",
794                 FT_UINT8, BASE_HEX, VALS ( vals_wsp_reason_codes ), 0x00,
795                 "Abort Reason", HFILL
796             }
797         },
798         { &hf_wtp_header_sequence_number,
799             {   "Packet Sequence Number",
800                 "wtp.header.sequence",
801                 FT_UINT8, BASE_DEC, NULL, 0x00,
802                 "Packet Sequence Number", HFILL
803             }
804         },
805         { &hf_wtp_header_missing_packets,
806             {   "Missing Packets",
807                 "wtp.header.missing_packets",
808                 FT_UINT8, BASE_DEC, NULL, 0x00,
809                 "Missing Packets", HFILL
810             }
811         },
812         { &hf_wtp_header_variable_part,
813             {   "Header: Variable part",
814                 "wtp.header_variable_part",
815                 FT_BYTES, BASE_HEX, NULL, 0x0,
816                 "Variable part of the header", HFILL
817             }
818         },
819         { &hf_wtp_data,
820             {   "Data",
821                 "wtp.header_data",
822                 FT_BYTES, BASE_HEX, NULL, 0x0,
823                 "Data", HFILL
824             }
825         },
826         { &hf_wtp_tpi_type,
827             {   "TPI",
828                 "wtp.tpi",
829                 FT_UINT8, BASE_HEX, VALS(vals_tpi_type), 0x00,
830                 "Identification of the Transport Information Item", HFILL
831             }
832         },
833         { &hf_wtp_tpi_psn,
834             {   "Packet sequence number",
835                 "wtp.tpi.psn",
836                 FT_UINT8, BASE_DEC, NULL, 0x00,
837                 "Sequence number of this packet", HFILL
838             }
839         },
840         { &hf_wtp_tpi_opt,
841             {   "Option",
842                 "wtp.tpi.opt",
843                 FT_UINT8, BASE_HEX, VALS(vals_tpi_opt), 0x00,
844                 "The given option for this TPI", HFILL
845             }
846         },
847         { &hf_wtp_tpi_optval,
848             {   "Option Value",
849                 "wtp.tpi.opt.val",
850                 FT_NONE, BASE_NONE, NULL, 0x00,
851                 "The value that is supplied with this option", HFILL
852             }
853         },
854         { &hf_wtp_tpi_info,
855             {   "Information",
856                 "wtp.tpi.info",
857                 FT_NONE, BASE_NONE, NULL, 0x00,
858                 "The information being send by this TPI", HFILL
859             }
860         },
861
862         /* Fragment fields */
863         { &hf_wtp_fragment_overlap,
864             {   "Fragment overlap",
865                 "wtp.fragment.overlap",
866                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
867                 "Fragment overlaps with other fragments", HFILL
868             }
869         },
870         { &hf_wtp_fragment_overlap_conflict,
871             {   "Conflicting data in fragment overlap",
872                 "wtp.fragment.overlap.conflict",
873                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
874                 "Overlapping fragments contained conflicting data", HFILL
875             }
876         },
877         { &hf_wtp_fragment_multiple_tails,
878             {   "Multiple tail fragments found",
879                 "wtp.fragment.multipletails",
880                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
881                 "Several tails were found when defragmenting the packet", HFILL
882             }
883         },
884         { &hf_wtp_fragment_too_long_fragment,
885             {   "Fragment too long",
886                 "wtp.fragment.toolongfragment",
887                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
888                 "Fragment contained data past end of packet", HFILL
889             }
890         },
891         { &hf_wtp_fragment_error,
892             {   "Defragmentation error",
893                 "wtp.fragment.error",
894                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
895                 "Defragmentation error due to illegal fragments", HFILL
896             }
897         },
898         { &hf_wtp_fragment,
899             {   "WTP Fragment",
900                 "wtp.fragment",
901                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
902                 "WTP Fragment", HFILL
903             }
904         },
905         { &hf_wtp_fragments,
906             {   "WTP Fragments",
907                 "wtp.fragments",
908                 FT_NONE, BASE_NONE, NULL, 0x0,
909                 "WTP Fragments", HFILL
910             }
911         },
912     };
913
914     /* Setup protocol subtree array */
915     static gint *ett[] = {
916         &ett_wtp,
917         &ett_header,
918         &ett_tpilist,
919         &ett_wsp_fragments,
920         &ett_wtp_fragment,
921     };
922
923     /* Register the protocol name and description */
924     proto_wtp = proto_register_protocol(
925         "Wireless Transaction Protocol",   /* protocol name for use by ethereal */
926         "WTP",                             /* short version of name */
927         "wap-wsp-wtp"                      /* Abbreviated protocol name, should Match IANA
928                                             < URL:http://www.isi.edu/in-notes/iana/assignments/port-numbers/ >
929                                             */
930     );
931
932     /* Required calls to register the header fields and subtrees used */
933     proto_register_field_array(proto_wtp, hf, array_length(hf));
934     proto_register_subtree_array(ett, array_length(ett));
935
936     register_dissector("wtp", dissect_wtp_fromwap, proto_wtp);
937     register_dissector("wtp-udp", dissect_wtp_fromudp, proto_wtp);
938     register_init_routine(wtp_defragment_init);
939 };
940
941 void
942 proto_reg_handoff_wtp(void)
943 {
944     dissector_handle_t wtp_fromudp_handle;
945
946     /*
947      * Get a handle for the connection-oriented WSP dissector - if WTP
948      * PDUs have data, it is WSP.
949      */
950     wsp_handle = find_dissector("wsp-co");
951
952     wtp_fromudp_handle = find_dissector("wtp-udp");
953     dissector_add("udp.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
954 }