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