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