a08de7d73e8e14b60d38c9f8cf23df5f975f73e3
[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.53 2003/11/05 04:23:59 guy 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_wtp_sub_pdu_tree        = ETT_EMPTY;
202 static gint ett_header                          = ETT_EMPTY;
203 static gint ett_tpilist                         = ETT_EMPTY;
204 static gint ett_wsp_fragments                   = ETT_EMPTY;
205 static gint ett_wtp_fragment                    = ETT_EMPTY;
206
207 static const fragment_items wtp_frag_items = {
208     &ett_wtp_fragment,
209     &ett_wsp_fragments,
210     &hf_wtp_fragments,
211     &hf_wtp_fragment,
212     &hf_wtp_fragment_overlap,
213     &hf_wtp_fragment_overlap_conflict,
214     &hf_wtp_fragment_multiple_tails,
215     &hf_wtp_fragment_too_long_fragment,
216     &hf_wtp_fragment_error,
217     NULL,
218     "fragments"
219 };
220
221 /* Handle for WSP dissector */
222 static dissector_handle_t wsp_handle;
223
224 /*
225  * reassembly of WSP
226  */
227 static GHashTable       *wtp_fragment_table = NULL;
228
229 static void
230 wtp_defragment_init(void)
231 {
232     fragment_table_init(&wtp_fragment_table);
233 }
234
235 /*
236  * Extract some bitfields
237  */
238 #define pdu_type(octet)                 (((octet) >> 3) & 0x0F) /* Note pdu type must not be 0x00 */
239 #define transaction_class(octet)        ((octet) & 0x03)        /* ......XX */
240 #define transmission_trailer(octet)     (((octet) >> 1) & 0x01) /* ......X. */
241
242 static char retransmission_indicator(unsigned char octet)
243 {
244     switch (pdu_type(octet)) {
245         case INVOKE:
246         case RESULT:
247         case ACK:
248         case SEGMENTED_INVOKE:
249         case SEGMENTED_RESULT:
250         case NEGATIVE_ACK:
251             return octet & 0x01;        /* .......X */
252         default:
253             return 0;
254     }
255 }
256
257 /*
258  * dissect a TPI
259  */
260 static void
261 wtp_handle_tpi(proto_tree *tree, tvbuff_t *tvb)
262 {
263     int                  offset = 0;
264     unsigned char        tByte;
265     unsigned char        tType;
266     unsigned char        tLen;
267     proto_item          *subTree = NULL;
268
269     tByte = tvb_get_guint8(tvb, offset++);
270     tType = (tByte & 0x78) >> 3;
271     if (tByte & 0x04)                           /* Long TPI     */
272         tLen = tvb_get_guint8(tvb, offset++);
273     else
274         tLen = tByte & 0x03;
275     subTree = proto_tree_add_uint(tree, hf_wtp_tpi_type,
276                                   tvb, 0, tvb_length(tvb), tType);
277     proto_item_add_subtree(subTree, ett_tpilist);
278     switch (tType) {
279         case 0x00:                      /* Error*/
280             /* \todo    */
281             break;
282         case 0x01:                      /* Info */
283             /* Beware, untested case here       */
284             proto_tree_add_item(subTree, hf_wtp_tpi_info,
285                                 tvb, offset, tLen, bo_little_endian);
286             break;
287         case 0x02:                      /* Option       */
288             proto_tree_add_item(subTree, hf_wtp_tpi_opt,
289                                 tvb, offset++, 1, bo_little_endian);
290             proto_tree_add_item(subTree, hf_wtp_tpi_optval,
291                                 tvb, offset, tLen - 1, bo_little_endian);
292             break;
293         case 0x03:                      /* PSN  */
294             proto_tree_add_item(subTree, hf_wtp_tpi_psn,
295                                 tvb, offset, 1, bo_little_endian);
296             break;
297         case 0x04:                      /* SDU boundary */
298             /* \todo    */
299             break;
300         case 0x05:                      /* Frame boundary       */
301             /* \todo    */
302             break;
303         default:
304             break;
305     }
306 }
307
308 /* Code to actually dissect the packets */
309 static void
310 dissect_wtp_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
311 {
312     static GString *szInfo = NULL;
313     int         offCur          = 0; /* current offset from start of WTP data */
314
315     unsigned char  b0;
316
317     /* continuation flag */
318     unsigned char       fCon;                   /* Continue flag        */
319     unsigned char       fRID;                   /* Re-transmission indicator*/
320     unsigned char       fTTR = '\0';            /* Transmission trailer */
321     guint               cbHeader        = 0;    /* Fixed header length  */
322     guint               vHeader         = 0;    /* Variable header length*/
323     int                 abortType       = 0;
324
325     /* Set up structures we'll need to add the protocol subtree and manage it */
326     proto_item          *ti;
327     proto_tree          *wtp_tree = NULL;
328
329     char                pdut;
330     char                clsTransaction  = ' ';
331     int                 numMissing = 0;         /* Number of missing packets in a negative ack */
332     int                 i;
333     tvbuff_t            *wsp_tvb = NULL;
334     fragment_data       *fd_head = NULL;
335     guint8              psn = 0;                /* Packet sequence number*/
336     guint16             TID = 0;                /* Transaction-Id       */
337
338     if (szInfo == NULL)
339         szInfo = g_string_sized_new(32);
340
341     b0 = tvb_get_guint8 (tvb, offCur + 0);
342     /* Discover Concatenated PDUs */
343     if (b0 == 0) {
344         guint   c_fieldlen = 0;         /* Length of length-field       */
345         guint   c_pdulen = 0;           /* Length of conc. PDU  */
346
347         if (tree) {
348             ti = proto_tree_add_item(tree, proto_wtp,
349                                     tvb, offCur, 1, bo_little_endian);
350             wtp_tree = proto_item_add_subtree(ti, ett_wtp_sub_pdu_tree);
351         }
352         offCur = 1;
353         i = 1;
354         while (offCur < (int) tvb_reported_length(tvb)) {
355             b0 = tvb_get_guint8(tvb, offCur + 0);
356             if (b0 & 0x80) {
357                 c_fieldlen = 2;
358                 c_pdulen = ((b0 & 0x7f) << 8) | tvb_get_guint8(tvb, offCur + 1);
359             } else {
360                 c_fieldlen = 1;
361                 c_pdulen = b0;
362             }
363             if (tree) {
364                 proto_tree_add_uint(wtp_tree, hf_wtp_header_sub_pdu_size,
365                                     tvb, offCur, c_fieldlen, c_pdulen);
366             }
367             if (i > 1 && check_col(pinfo->cinfo, COL_INFO)) {
368                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
369             }
370             wsp_tvb = tvb_new_subset(tvb, offCur + c_fieldlen, c_pdulen, c_pdulen);
371             dissect_wtp_common(wsp_tvb, pinfo, wtp_tree);
372             offCur += c_fieldlen + c_pdulen;
373             i++;
374         }
375         return;
376     }
377     fCon = b0 & 0x80;
378     fRID = retransmission_indicator(b0);
379     pdut = pdu_type(b0);
380
381     /* Develop the string to put in the Info column */
382     g_string_sprintf(szInfo, "WTP %s",
383                     val_to_str(pdut, vals_pdu_type, "Unknown PDU type 0x%x"));
384
385     switch (pdut) {
386         case INVOKE:
387             fTTR = transmission_trailer(b0);
388             TID = tvb_get_ntohs(tvb, offCur + 1);
389             psn = 0;
390             clsTransaction = transaction_class(tvb_get_guint8(tvb, offCur + 3));
391             g_string_sprintfa(szInfo, " Class %d", clsTransaction);
392             cbHeader = 4;
393             break;
394
395         case SEGMENTED_INVOKE:
396         case SEGMENTED_RESULT:
397             fTTR = transmission_trailer(b0);
398             TID = tvb_get_ntohs(tvb, offCur + 1);
399             psn = tvb_get_guint8(tvb, offCur + 3);
400             cbHeader = 4;
401             break;
402
403         case ABORT:
404             cbHeader = 4;
405             break;
406
407         case RESULT:
408             fTTR = transmission_trailer(b0);
409             TID = tvb_get_ntohs(tvb, offCur + 1);
410             psn = 0;
411             cbHeader = 3;
412             break;
413
414         case ACK:
415             cbHeader = 3;
416             break;
417
418         case NEGATIVE_ACK:
419             /* Variable number of missing packets */
420             numMissing = tvb_get_guint8(tvb, offCur + 3);
421             cbHeader = numMissing + 4;
422             break;
423
424         default:
425             break;
426     };
427     if (fRID) {
428         g_string_append( szInfo, " R" );
429     };
430     if (fCon) {                         /* Scan variable part (TPI's),  */
431                                         /* determine length of it       */
432         unsigned char   tCon;
433         unsigned char   tByte;
434
435         do {
436             tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
437             tCon = tByte & 0x80;
438             if (tByte & 0x04)   /* Long format  */
439                 vHeader = vHeader + tvb_get_guint8(tvb,
440                                         offCur + cbHeader + vHeader + 1) + 2;
441             else
442                 vHeader = vHeader + (tByte & 0x03) + 1;
443         } while (tCon);
444     }
445
446 #ifdef DEBUG
447     fprintf( stderr, "dissect_wtp: cbHeader = %d\n", cbHeader );
448 #endif
449
450     /* Only update "Info" column when no data in this PDU will
451      * be handed off to a subsequent dissector.
452      */
453     if (check_col(pinfo->cinfo, COL_INFO) &&
454         ((tvb_length_remaining(tvb, offCur + cbHeader + vHeader) <= 0) ||
455          (pdut == ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)) ) {
456 #ifdef DEBUG
457         fprintf(stderr, "dissect_wtp: (6) About to set info_col header to %s\n", szInfo->str);
458 #endif
459         col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
460     };
461     /* In the interest of speed, if "tree" is NULL, don't do any work not
462        necessary to generate protocol tree items. */
463     if (tree) {
464 #ifdef DEBUG
465         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
466 #endif
467         ti = proto_tree_add_item(tree, proto_wtp, tvb, offCur, cbHeader + vHeader, bo_little_endian);
468 #ifdef DEBUG
469         fprintf(stderr, "dissect_wtp: (7) Returned from proto_tree_add_item\n");
470 #endif
471         wtp_tree = proto_item_add_subtree(ti, ett_wtp);
472
473 /* Code to process the packet goes here */
474 #ifdef DEBUG
475         fprintf(stderr, "dissect_wtp: cbHeader = %d\n", cbHeader);
476         fprintf(stderr, "dissect_wtp: offCur = %d\n", offCur);
477 #endif
478         /* Add common items: only CON and PDU Type */
479         proto_tree_add_item(
480                         wtp_tree,                       /* tree */
481                         hf_wtp_header_flag_continue,    /* id */
482                         tvb,
483                         offCur,                         /* start of highlight */
484                         1,                              /* length of highlight*/
485                         b0                              /* value */
486              );
487         proto_tree_add_item(wtp_tree, hf_wtp_header_pdu_type, tvb, offCur, 1, bo_little_endian);
488
489         switch(pdut) {
490             case INVOKE:
491                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
492                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
493                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
494                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
495
496                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_version , tvb, offCur + 3, 1, bo_little_endian);
497                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_TIDNew, tvb, offCur + 3, 1, bo_little_endian);
498                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_flag_UP, tvb, offCur + 3, 1, bo_little_endian);
499                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_Reserved, tvb, offCur + 3, 1, bo_little_endian);
500                 proto_tree_add_item(wtp_tree, hf_wtp_header_Inv_TransactionClass, tvb, offCur + 3, 1, bo_little_endian);
501                 break;
502
503             case RESULT:
504                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
505                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
506                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
507                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
508                 break;
509
510             case ACK:
511                 proto_tree_add_item(wtp_tree, hf_wtp_header_Ack_flag_TVETOK, tvb, offCur, 1, bo_big_endian);
512
513                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
514                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
515                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
516                 break;
517
518             case ABORT:
519                 abortType = tvb_get_guint8 (tvb, offCur) & 0x07;
520                 proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_type , tvb, offCur , 1, bo_little_endian);
521                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
522                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
523
524                 if (abortType == PROVIDER)
525                 {
526                     proto_tree_add_item( wtp_tree, hf_wtp_header_Abort_reason_provider , tvb, offCur + 3 , 1, bo_little_endian);
527                 }
528                 else if (abortType == USER)
529                 {
530                     proto_tree_add_item(wtp_tree, hf_wtp_header_Abort_reason_user , tvb, offCur + 3 , 1, bo_little_endian);
531                 }
532                 break;
533
534             case SEGMENTED_INVOKE:
535                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
536                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
537                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
538                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
539
540                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
541                 break;
542
543             case SEGMENTED_RESULT:
544                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_Trailer, tvb, offCur, 1, bo_little_endian);
545                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
546                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
547                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
548
549                 proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number , tvb, offCur + 3, 1, bo_little_endian);
550                 break;
551
552             case NEGATIVE_ACK:
553                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_RID, tvb, offCur, 1, bo_little_endian);
554                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID_response, tvb, offCur + 1, 2, bo_big_endian);
555                 proto_tree_add_item(wtp_tree, hf_wtp_header_flag_TID, tvb, offCur + 1, 2, bo_big_endian);
556
557                 proto_tree_add_item(wtp_tree, hf_wtp_header_missing_packets , tvb, offCur + 3, 1, bo_little_endian);
558                 /* Iterate through missing packets */
559                 for (i = 0; i < numMissing; i++)
560                 {
561                     proto_tree_add_item(wtp_tree, hf_wtp_header_sequence_number, tvb, offCur + 4 + i, 1, bo_little_endian);
562                 }
563                 break;
564
565             default:
566                 break;
567         };
568         if (fCon) {                     /* Now, analyze variable part   */
569             unsigned char        tCon;
570             unsigned char        tByte;
571             unsigned char        tpiLen;
572             tvbuff_t            *tmp_tvb;
573
574             vHeader = 0;                /* Start scan all over  */
575
576             do {
577                 tByte = tvb_get_guint8(tvb, offCur + cbHeader + vHeader);
578                 tCon = tByte & 0x80;
579                 if (tByte & 0x04)       /* Long TPI     */
580                     tpiLen = 2 + tvb_get_guint8(tvb,
581                                             offCur + cbHeader + vHeader + 1);
582                 else
583                     tpiLen = 1 + (tByte & 0x03);
584                 tmp_tvb = tvb_new_subset(tvb, offCur + cbHeader + vHeader,
585                                         tpiLen, tpiLen);
586                 wtp_handle_tpi(wtp_tree, tmp_tvb);
587                 vHeader += tpiLen;
588             } while (tCon);
589         } else {
590                 /* There is no variable part */
591         }       /* End of variable part of header */
592     } else {
593 #ifdef DEBUG
594         fprintf(stderr, "dissect_wtp: (4) tree was %p\n", tree);
595 #endif
596     }
597     /*
598      * Any remaining data ought to be WSP data (if not WTP ACK, NACK
599      * or ABORT pdu), so hand off (defragmented) to the WSP dissector.
600      * Note that the last packet of a fragmented WTP message needn't
601      * contain any data, so we allow payloadless packets to be
602      * reassembled.  (XXX - does the reassembly code handle this
603      * for packets other than the last packet?)
604      */
605     if ((tvb_reported_length_remaining(tvb, offCur + cbHeader + vHeader) >= 0) &&
606         ! ((pdut==ACK) || (pdut==NEGATIVE_ACK) || (pdut==ABORT)))
607     {
608         int     dataOffset = offCur + cbHeader + vHeader;
609         gint    dataLen = tvb_reported_length_remaining(tvb, dataOffset);
610         gboolean save_fragmented;
611
612         if (((pdut == SEGMENTED_INVOKE) || (pdut == SEGMENTED_RESULT) ||
613             (((pdut == INVOKE) || (pdut == RESULT)) && (!fTTR))) &&
614             tvb_bytes_exist(tvb, dataOffset, dataLen))
615         {                                       /* 1st part of segment  */
616             save_fragmented = pinfo->fragmented;
617             pinfo->fragmented = TRUE;
618             fd_head = fragment_add_seq(tvb, dataOffset, pinfo, TID,
619                             wtp_fragment_table, psn, dataLen, !fTTR);
620             if (fd_head != NULL)                /* Reassembled  */
621             {
622                 /* Reassembly is complete; show the reassembled PDU */
623                 wsp_tvb = tvb_new_real_data(fd_head->data,
624                                             fd_head->len,
625                                             fd_head->len);
626                 tvb_set_child_real_data_tvbuff(tvb, wsp_tvb);
627                 add_new_data_source(pinfo, wsp_tvb,
628                                         "Reassembled WTP");
629                 pinfo->fragmented = FALSE;
630
631                 /* show all fragments */
632                 show_fragment_seq_tree(fd_head, &wtp_frag_items,
633                                         wtp_tree, pinfo, wsp_tvb);
634
635                 call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
636             }
637             else
638             {
639                 /* Reassembly isn't complete; just show the fragment */
640                 if (check_col(pinfo->cinfo, COL_INFO))          /* Won't call WSP so display */
641                     col_append_str(pinfo->cinfo, COL_INFO, szInfo->str);
642                 if (tree != NULL)
643                     proto_tree_add_text(wtp_tree, tvb, dataOffset, -1, "Payload");
644             }
645             pinfo->fragmented = save_fragmented;
646         }
647         else
648         {
649             /*
650              * Normal packet, or not all the fragment data is available;
651              * call next dissector, unless this is a segment of a
652              * segmented invoke or result and isn't the first segment.
653              */
654             if ((pdut == SEGMENTED_INVOKE || pdut == SEGMENTED_RESULT) &&
655                         psn != 0) {
656                 /*
657                  * This is a segmented invoke or result, and not the first
658                  * segment; just show it as a segmented invoke or result,
659                  * don't try to dissect its payload.
660                  */
661                 if (check_col(pinfo->cinfo, COL_INFO))
662                     col_add_fstr(pinfo->cinfo, COL_INFO,
663                                  "WTP %s (%u)",
664                                  (pdut == SEGMENTED_INVOKE ?
665                                   "Segmented Invoke" : "Segmented Result"),
666                                  psn);
667                 if (tree != NULL)
668                     proto_tree_add_text(wtp_tree, tvb, dataOffset, -1, "Payload");
669             } else {
670                 wsp_tvb = tvb_new_subset(tvb, dataOffset, -1, -1);
671                 call_dissector(wsp_handle, wsp_tvb, pinfo, tree);
672             }
673         }
674     }
675 }
676
677 /*
678  * Called directly from UDP.
679  * Put "WTP+WSP" into the "Protocol" column.
680  */
681 static void
682 dissect_wtp_fromudp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
683 {
684     if (check_col(pinfo->cinfo, COL_PROTOCOL))
685         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTP+WSP" );
686     if (check_col(pinfo->cinfo, COL_INFO))
687         col_clear(pinfo->cinfo, COL_INFO);
688
689     dissect_wtp_common(tvb, pinfo, tree);
690 }
691
692 /*
693  * Called from a higher-level WAP dissector, presumably WTLS.
694  * Put "WTLS+WSP+WTP" to the "Protocol" column.
695  *
696  * XXX - is this supposed to be called from WTLS?  If so, we're not
697  * calling it....
698  *
699  * XXX - can this be called from any other dissector?
700  */
701 static void
702 dissect_wtp_fromwap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
703 {
704     if (check_col(pinfo->cinfo, COL_PROTOCOL))
705         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WTLS+WTP+WSP" );
706     if (check_col(pinfo->cinfo, COL_INFO))
707         col_clear(pinfo->cinfo, COL_INFO);
708
709     dissect_wtp_common(tvb, pinfo, tree);
710 }
711
712 /* Register the protocol with Ethereal */
713 void
714 proto_register_wtp(void)
715 {
716
717     /* Setup list of header fields */
718     static hf_register_info hf[] = {
719         { &hf_wtp_header_sub_pdu_size,
720             {   "Sub PDU size",
721                 "wtp.sub_pdu_size",
722                 FT_UINT16, BASE_DEC, NULL, 0x0,
723                 "Size of Sub-PDU (bytes)", HFILL
724             }
725         },
726         { &hf_wtp_header_flag_continue,
727             {   "Continue Flag",
728                 "wtp.continue_flag",
729                 FT_BOOLEAN, 8, TFS( &continue_truth ), 0x80,
730                 "Continue Flag", HFILL
731             }
732         },
733         { &hf_wtp_header_pdu_type,
734             {   "PDU Type",
735                 "wtp.pdu_type",
736                 FT_UINT8, BASE_HEX, VALS( vals_pdu_type ), 0x78,
737                 "PDU Type", HFILL
738             }
739         },
740         { &hf_wtp_header_flag_Trailer,
741             {   "Trailer Flags",
742                 "wtp.trailer_flags",
743                 FT_UINT8, BASE_HEX, VALS( vals_transaction_trailer ), 0x06,
744                 "Trailer Flags", HFILL
745             }
746         },
747         { &hf_wtp_header_flag_RID,
748             {   "Re-transmission Indicator",
749                 "wtp.RID",
750                 FT_BOOLEAN, 8, TFS( &RID_truth ), 0x01,
751                 "Re-transmission Indicator", HFILL
752             }
753         },
754         { &hf_wtp_header_flag_TID_response,
755             {   "TID Response",
756                 "wtp.TID.response",
757                 FT_BOOLEAN, 16, TFS( &tid_response_truth ), 0x8000,
758                 "TID Response", HFILL
759             }
760         },
761         { &hf_wtp_header_flag_TID,
762             {   "Transaction ID",
763                 "wtp.TID",
764                 FT_UINT16, BASE_HEX, NULL, 0x7FFF,
765                 "Transaction ID", HFILL
766             }
767         },
768         { &hf_wtp_header_Inv_version,
769             {   "Version",
770                 "wtp.header.version",
771                 FT_UINT8, BASE_HEX, VALS( vals_version ), 0xC0,
772                 "Version", HFILL
773             }
774         },
775         { &hf_wtp_header_Inv_flag_TIDNew,
776             {   "TIDNew",
777                 "wtp.header.TIDNew",
778                 FT_BOOLEAN, 8, TFS( &TIDNew_truth ), 0x20,
779                 "TIDNew", HFILL
780             }
781         },
782         { &hf_wtp_header_Inv_flag_UP,
783             {   "U/P flag",
784                 "wtp.header.UP",
785                 FT_BOOLEAN, 8, TFS( &UP_truth ), 0x10,
786                 "U/P Flag", HFILL
787             }
788         },
789         { &hf_wtp_header_Inv_Reserved,
790             {   "Reserved",
791                 "wtp.inv.reserved",
792                 FT_UINT8, BASE_HEX, NULL, 0x0C,
793                 "Reserved", HFILL
794             }
795         },
796         { &hf_wtp_header_Inv_TransactionClass,
797             {   "Transaction Class",
798                 "wtp.inv.transaction_class",
799                 FT_UINT8, BASE_HEX, VALS( vals_transaction_classes ), 0x03,
800                 "Transaction Class", HFILL
801             }
802         },
803         { &hf_wtp_header_Ack_flag_TVETOK,
804             {   "Tve/Tok flag",
805                 "wtp.ack.tvetok",
806                 FT_BOOLEAN, 8, TFS( &TVETOK_truth ), 0x04,
807                 "Tve/Tok flag", HFILL
808             }
809         },
810         { &hf_wtp_header_Abort_type,
811             {   "Abort Type",
812                 "wtp.abort.type",
813                 FT_UINT8, BASE_HEX, VALS ( vals_abort_type ), 0x07,
814                 "Abort Type", HFILL
815             }
816         },
817         { &hf_wtp_header_Abort_reason_provider,
818             {   "Abort Reason",
819                 "wtp.abort.reason.provider",
820                 FT_UINT8, BASE_HEX, VALS ( vals_abort_reason_provider ), 0x00,
821                 "Abort Reason", HFILL
822             }
823         },
824         /* Assume WSP is the user and use its reason codes */
825         { &hf_wtp_header_Abort_reason_user,
826             {   "Abort Reason",
827                 "wtp.abort.reason.user",
828                 FT_UINT8, BASE_HEX, VALS ( vals_wsp_reason_codes ), 0x00,
829                 "Abort Reason", HFILL
830             }
831         },
832         { &hf_wtp_header_sequence_number,
833             {   "Packet Sequence Number",
834                 "wtp.header.sequence",
835                 FT_UINT8, BASE_DEC, NULL, 0x00,
836                 "Packet Sequence Number", HFILL
837             }
838         },
839         { &hf_wtp_header_missing_packets,
840             {   "Missing Packets",
841                 "wtp.header.missing_packets",
842                 FT_UINT8, BASE_DEC, NULL, 0x00,
843                 "Missing Packets", HFILL
844             }
845         },
846         { &hf_wtp_header_variable_part,
847             {   "Header: Variable part",
848                 "wtp.header_variable_part",
849                 FT_BYTES, BASE_HEX, NULL, 0x0,
850                 "Variable part of the header", HFILL
851             }
852         },
853         { &hf_wtp_data,
854             {   "Data",
855                 "wtp.header_data",
856                 FT_BYTES, BASE_HEX, NULL, 0x0,
857                 "Data", HFILL
858             }
859         },
860         { &hf_wtp_tpi_type,
861             {   "TPI",
862                 "wtp.tpi",
863                 FT_UINT8, BASE_HEX, VALS(vals_tpi_type), 0x00,
864                 "Identification of the Transport Information Item", HFILL
865             }
866         },
867         { &hf_wtp_tpi_psn,
868             {   "Packet sequence number",
869                 "wtp.tpi.psn",
870                 FT_UINT8, BASE_DEC, NULL, 0x00,
871                 "Sequence number of this packet", HFILL
872             }
873         },
874         { &hf_wtp_tpi_opt,
875             {   "Option",
876                 "wtp.tpi.opt",
877                 FT_UINT8, BASE_HEX, VALS(vals_tpi_opt), 0x00,
878                 "The given option for this TPI", HFILL
879             }
880         },
881         { &hf_wtp_tpi_optval,
882             {   "Option Value",
883                 "wtp.tpi.opt.val",
884                 FT_NONE, BASE_NONE, NULL, 0x00,
885                 "The value that is supplied with this option", HFILL
886             }
887         },
888         { &hf_wtp_tpi_info,
889             {   "Information",
890                 "wtp.tpi.info",
891                 FT_NONE, BASE_NONE, NULL, 0x00,
892                 "The information being send by this TPI", HFILL
893             }
894         },
895
896         /* Fragment fields */
897         { &hf_wtp_fragment_overlap,
898             {   "Fragment overlap",
899                 "wtp.fragment.overlap",
900                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
901                 "Fragment overlaps with other fragments", HFILL
902             }
903         },
904         { &hf_wtp_fragment_overlap_conflict,
905             {   "Conflicting data in fragment overlap",
906                 "wtp.fragment.overlap.conflict",
907                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
908                 "Overlapping fragments contained conflicting data", HFILL
909             }
910         },
911         { &hf_wtp_fragment_multiple_tails,
912             {   "Multiple tail fragments found",
913                 "wtp.fragment.multipletails",
914                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
915                 "Several tails were found when defragmenting the packet", HFILL
916             }
917         },
918         { &hf_wtp_fragment_too_long_fragment,
919             {   "Fragment too long",
920                 "wtp.fragment.toolongfragment",
921                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
922                 "Fragment contained data past end of packet", HFILL
923             }
924         },
925         { &hf_wtp_fragment_error,
926             {   "Defragmentation error",
927                 "wtp.fragment.error",
928                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
929                 "Defragmentation error due to illegal fragments", HFILL
930             }
931         },
932         { &hf_wtp_fragment,
933             {   "WTP Fragment",
934                 "wtp.fragment",
935                 FT_FRAMENUM, BASE_NONE, NULL, 0x0,
936                 "WTP Fragment", HFILL
937             }
938         },
939         { &hf_wtp_fragments,
940             {   "WTP Fragments",
941                 "wtp.fragments",
942                 FT_NONE, BASE_NONE, NULL, 0x0,
943                 "WTP Fragments", HFILL
944             }
945         },
946     };
947
948     /* Setup protocol subtree array */
949     static gint *ett[] = {
950         &ett_wtp,
951         &ett_wtp_sub_pdu_tree,
952         &ett_header,
953         &ett_tpilist,
954         &ett_wsp_fragments,
955         &ett_wtp_fragment,
956     };
957
958     /* Register the protocol name and description */
959     proto_wtp = proto_register_protocol(
960         "Wireless Transaction Protocol",   /* protocol name for use by ethereal */
961         "WTP",                             /* short version of name */
962         "wap-wsp-wtp"                      /* Abbreviated protocol name, should Match IANA
963                                             < URL:http://www.isi.edu/in-notes/iana/assignments/port-numbers/ >
964                                             */
965     );
966
967     /* Required calls to register the header fields and subtrees used */
968     proto_register_field_array(proto_wtp, hf, array_length(hf));
969     proto_register_subtree_array(ett, array_length(ett));
970
971     register_dissector("wtp", dissect_wtp_fromwap, proto_wtp);
972     register_dissector("wtp-udp", dissect_wtp_fromudp, proto_wtp);
973     register_init_routine(wtp_defragment_init);
974 };
975
976 void
977 proto_reg_handoff_wtp(void)
978 {
979     dissector_handle_t wtp_fromudp_handle;
980
981     /*
982      * Get a handle for the connection-oriented WSP dissector - if WTP
983      * PDUs have data, it is WSP.
984      */
985     wsp_handle = find_dissector("wsp-co");
986
987     wtp_fromudp_handle = find_dissector("wtp-udp");
988     dissector_add("udp.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
989     dissector_add("smpp.udh.port", UDP_PORT_WTP_WSP, wtp_fromudp_handle);
990 }