Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-classicstun.c
1 /* packet-classicstun.c
2  * Routines for Simple Traversal of UDP Through NAT dissection
3  * Copyright 2003, Shiang-Ming Huang <smhuang@pcs.csie.nctu.edu.tw>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please refer to RFC 3489 for protocol detail.
24  * (supports extra message attributes described in draft-ietf-behave-rfc3489bis-00)
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30
31 #include <epan/packet.h>
32 #include <epan/conversation.h>
33 #include <epan/wmem/wmem.h>
34
35 void proto_register_classicstun(void);
36 void proto_reg_handoff_classicstun(void);
37
38 /* Initialize the protocol and registered fields */
39 static int proto_classicstun                          = -1;
40
41 static int hf_classicstun_type                        = -1; /* CLASSIC-STUN message header */
42 static int hf_classicstun_length                      = -1;
43 static int hf_classicstun_id                          = -1;
44 static int hf_classicstun_att                         = -1;
45 static int hf_classicstun_response_in                 = -1;
46 static int hf_classicstun_response_to                 = -1;
47 static int hf_classicstun_time                        = -1;
48
49
50 static int classicstun_att_type                       = -1; /* CLASSIC-STUN attribute fields */
51 static int classicstun_att_length                     = -1;
52 static int classicstun_att_value                      = -1;
53 static int classicstun_att_family                     = -1;
54 static int classicstun_att_ipv4                       = -1;
55 static int classicstun_att_ipv6                       = -1;
56 static int classicstun_att_port                       = -1;
57 static int classicstun_att_change_ip                  = -1;
58 static int classicstun_att_change_port                = -1;
59 static int classicstun_att_unknown                    = -1;
60 static int classicstun_att_error_class                = -1;
61 static int classicstun_att_error_number               = -1;
62 static int classicstun_att_error_reason               = -1;
63 static int classicstun_att_server_string              = -1;
64 static int classicstun_att_xor_ipv4                   = -1;
65 static int classicstun_att_xor_ipv6                   = -1;
66 static int classicstun_att_xor_port                   = -1;
67 static int classicstun_att_lifetime                   = -1;
68 static int classicstun_att_magic_cookie               = -1;
69 static int classicstun_att_bandwidth                  = -1;
70 static int classicstun_att_data                       = -1;
71 static int classicstun_att_connection_request_binding = -1;
72
73 /* Structure containing transaction specific information */
74 typedef struct _classicstun_transaction_t {
75     guint32  req_frame;
76     guint32  rep_frame;
77     nstime_t req_time;
78 } classicstun_transaction_t;
79
80 /* Structure containing conversation specific information */
81 typedef struct _classicstun_conv_info_t {
82     wmem_tree_t *pdus;
83 } classicstun_conv_info_t;
84
85
86 /* Message Types */
87 #define BINDING_REQUEST                       0x0001
88 #define BINDING_RESPONSE                      0x0101
89 #define BINDING_ERROR_RESPONSE                0x0111
90 #define SHARED_SECRET_REQUEST                 0x0002
91 #define SHARED_SECRET_RESPONSE                0x0102
92 #define SHARED_SECRET_ERROR_RESPONSE          0x1112
93 #define ALLOCATE_REQUEST                      0x0003
94 #define ALLOCATE_RESPONSE                     0x0103
95 #define ALLOCATE_ERROR_RESPONSE               0x0113
96 #define SEND_REQUEST                          0x0004
97 #define SEND_RESPONSE                         0x0104
98 #define SEND_ERROR_RESPONSE                   0x0114
99 #define DATA_INDICATION                       0x0115
100 #define SET_ACTIVE_DESTINATION_REQUEST        0x0006
101 #define SET_ACTIVE_DESTINATION_RESPONSE       0x0106
102 #define SET_ACTIVE_DESTINATION_ERROR_RESPONSE 0x0116
103
104
105 /* Message classes */
106 #define CLASS_MASK                            0xC110
107 #define REQUEST                               0x0000
108 #define INDICATION                            0x0001
109 #define RESPONSE                              0x0010
110 #define ERROR_RESPONSE                        0x0011
111
112 /* Attribute Types */
113 #define MAPPED_ADDRESS                        0x0001
114 #define RESPONSE_ADDRESS                      0x0002
115 #define CHANGE_REQUEST                        0x0003
116 #define SOURCE_ADDRESS                        0x0004
117 #define CHANGED_ADDRESS                       0x0005
118 #define USERNAME                              0x0006
119 #define PASSWORD                              0x0007
120 #define MESSAGE_INTEGRITY                     0x0008
121 #define ERROR_CODE                            0x0009
122 #define UNKNOWN_ATTRIBUTES                    0x000a
123 #define REFLECTED_FROM                        0x000b
124 #define LIFETIME                              0x000d
125 #define ALTERNATE_SERVER                      0x000e
126 #define MAGIC_COOKIE                          0x000f
127 #define BANDWIDTH                             0x0010
128 #define DESTINATION_ADDRESS                   0x0011
129 #define REMOTE_ADDRESS                        0x0012
130 #define DATA                                  0x0013
131 #define NONCE                                 0x0014
132 #define REALM                                 0x0015
133 #define REQUESTED_ADDRESS_TYPE                0x0016
134 #define XOR_MAPPED_ADDRESS                    0x8020
135 #define XOR_ONLY                              0x0021
136 #define SERVER                                0x8022
137 #define CONNECTION_REQUEST_BINDING            0xc001
138 #define BINDING_CHANGE                        0xc002
139
140
141
142 /* Initialize the subtree pointers */
143 static gint ett_classicstun = -1;
144 static gint ett_classicstun_att_type = -1;
145 static gint ett_classicstun_att = -1;
146
147
148 #define UDP_PORT_STUN   3478
149 #define TCP_PORT_STUN   3478
150
151
152 #define CLASSICSTUN_HDR_LEN ((guint)20) /* CLASSIC-STUN message header length */
153 #define ATTR_HDR_LEN                 4  /* CLASSIC-STUN attribute header length */
154
155
156 static const value_string messages[] = {
157     {BINDING_REQUEST                       , "Binding Request"},
158     {BINDING_RESPONSE                      , "Binding Response"},
159     {BINDING_ERROR_RESPONSE                , "Binding Error Response"},
160     {SHARED_SECRET_REQUEST                 , "Shared Secret Request"},
161     {SHARED_SECRET_RESPONSE                , "Shared Secret Response"},
162     {SHARED_SECRET_ERROR_RESPONSE          , "Shared Secret Error Response"},
163     {ALLOCATE_REQUEST                      , "Allocate Request"},
164     {ALLOCATE_RESPONSE                     , "Allocate Response"},
165     {ALLOCATE_ERROR_RESPONSE               , "Allocate Error Response"},
166     {SEND_REQUEST                          , "Send Request"},
167     {SEND_RESPONSE                         , "Send Response"},
168     {SEND_ERROR_RESPONSE                   , "Send Error Response"},
169     {DATA_INDICATION                       , "Data Indication"},
170     {SET_ACTIVE_DESTINATION_REQUEST        , "Set Active Destination Request"},
171     {SET_ACTIVE_DESTINATION_RESPONSE       , "Set Active Destination Response"},
172     {SET_ACTIVE_DESTINATION_ERROR_RESPONSE , "Set Active Destination Error Response"},
173     {0x00                                  , NULL}
174 };
175
176 static const value_string attributes[] = {
177     {MAPPED_ADDRESS                        , "MAPPED-ADDRESS"},
178     {RESPONSE_ADDRESS                      , "RESPONSE-ADDRESS"},
179     {CHANGE_REQUEST                        , "CHANGE-REQUEST"},
180     {SOURCE_ADDRESS                        , "SOURCE-ADDRESS"},
181     {CHANGED_ADDRESS                       , "CHANGED-ADDRESS"},
182     {USERNAME                              , "USERNAME"},
183     {PASSWORD                              , "PASSWORD"},
184     {MESSAGE_INTEGRITY                     , "MESSAGE-INTEGRITY"},
185     {ERROR_CODE                            , "ERROR-CODE"},
186     {REFLECTED_FROM                        , "REFLECTED-FROM"},
187     {LIFETIME                              , "LIFETIME"},
188     {ALTERNATE_SERVER                      , "ALTERNATE_SERVER"},
189     {MAGIC_COOKIE                          , "MAGIC_COOKIE"},
190     {BANDWIDTH                             , "BANDWIDTH"},
191     {DESTINATION_ADDRESS                   , "DESTINATION_ADDRESS"},
192     {REMOTE_ADDRESS                        , "REMOTE_ADDRESS"},
193     {DATA                                  , "DATA"},
194     {NONCE                                 , "NONCE"},
195     {REALM                                 , "REALM"},
196     {REQUESTED_ADDRESS_TYPE                , "REQUESTED_ADDRESS_TYPE"},
197     {XOR_MAPPED_ADDRESS                    , "XOR_MAPPED_ADDRESS"},
198     {XOR_ONLY                              , "XOR_ONLY"},
199     {SERVER                                , "SERVER"},
200     {CONNECTION_REQUEST_BINDING            , "CONNECTION-REQUEST-BINDING"},
201     {BINDING_CHANGE                        , "BINDING-CHANGE"},
202     {0x00                                  , NULL}
203 };
204
205 static const value_string attributes_family[] = {
206     {0x0001                                , "IPv4"},
207     {0x0002                                , "IPv6"},
208     {0x00                                  , NULL}
209 };
210
211 static int
212 dissect_classicstun(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
213 {
214
215     proto_item                *ti;
216     proto_item                *ta;
217     proto_tree                *classicstun_tree;
218     proto_tree                *att_type_tree;
219     proto_tree                *att_tree;
220     guint16                    msg_type;
221     guint16                    msg_length;
222     const char                *msg_type_str;
223     guint16                    att_type;
224     guint16                    att_length;
225     guint16                    offset;
226     guint                      len;
227     guint                      i;
228     conversation_t            *conversation;
229     classicstun_conv_info_t   *classicstun_info;
230     classicstun_transaction_t *classicstun_trans;
231     wmem_tree_key_t            transaction_id_key[2];
232     guint32                    transaction_id[4];
233
234
235     /*
236      * First check if the frame is really meant for us.
237      */
238     len = tvb_length(tvb);
239     /* First, make sure we have enough data to do the check. */
240     if (len < CLASSICSTUN_HDR_LEN)
241         return 0;
242
243     msg_type = tvb_get_ntohs(tvb, 0);
244
245     if (msg_type & 0xC000 || tvb_get_ntohl(tvb, 4) == 0x2112a442 /* New STUN */
246         || tvb_get_ntohl(tvb, 4) == 0x7f5a9bc7) /* XMCP */
247         return 0;
248
249     /* check if message type is correct */
250     msg_type_str = try_val_to_str(msg_type, messages);
251     if (msg_type_str == NULL)
252         return 0;
253
254     msg_length = tvb_get_ntohs(tvb, 2);
255
256     /* check if payload enough */
257     if (len != CLASSICSTUN_HDR_LEN+msg_length)
258         return 0;
259
260     /* The message seems to be a valid CLASSIC-STUN message! */
261
262     /* Create the transaction key which may be used
263        to track the conversation */
264     transaction_id[0] = tvb_get_ntohl(tvb, 4);
265     transaction_id[1] = tvb_get_ntohl(tvb, 8);
266     transaction_id[2] = tvb_get_ntohl(tvb, 12);
267     transaction_id[3] = tvb_get_ntohl(tvb, 16);
268
269     transaction_id_key[0].length = 4;
270     transaction_id_key[0].key    = transaction_id;
271     transaction_id_key[1].length = 0;
272     transaction_id_key[1].key    = NULL;
273
274     /*
275      * Do we have a conversation for this connection?
276      */
277     conversation = find_or_create_conversation(pinfo);
278
279     /*
280      * Do we already have a state structure for this conv
281      */
282     classicstun_info = (classicstun_conv_info_t *)conversation_get_proto_data(conversation, proto_classicstun);
283     if (!classicstun_info) {
284         /* No.  Attach that information to the conversation, and add
285          * it to the list of information structures.
286          */
287         classicstun_info = wmem_new(wmem_file_scope(), classicstun_conv_info_t);
288         classicstun_info->pdus=wmem_tree_new(wmem_file_scope());
289         conversation_add_proto_data(conversation, proto_classicstun, classicstun_info);
290     }
291
292     if(!pinfo->fd->flags.visited){
293         if (((msg_type & CLASS_MASK) >> 4) == REQUEST) {
294             /* This is a request */
295             classicstun_trans=wmem_new(wmem_file_scope(), classicstun_transaction_t);
296             classicstun_trans->req_frame=pinfo->fd->num;
297             classicstun_trans->rep_frame=0;
298             classicstun_trans->req_time=pinfo->fd->abs_ts;
299             wmem_tree_insert32_array(classicstun_info->pdus, transaction_id_key,
300                            (void *)classicstun_trans);
301         } else {
302             classicstun_trans=(classicstun_transaction_t *)wmem_tree_lookup32_array(classicstun_info->pdus,
303                                  transaction_id_key);
304             if(classicstun_trans){
305                 classicstun_trans->rep_frame=pinfo->fd->num;
306             }
307         }
308     } else {
309         classicstun_trans=(classicstun_transaction_t *)wmem_tree_lookup32_array(classicstun_info->pdus, transaction_id_key);
310     }
311     if(!classicstun_trans){
312         /* create a "fake" pana_trans structure */
313         classicstun_trans=wmem_new(wmem_packet_scope(), classicstun_transaction_t);
314         classicstun_trans->req_frame=0;
315         classicstun_trans->rep_frame=0;
316         classicstun_trans->req_time=pinfo->fd->abs_ts;
317     }
318
319
320
321     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CLASSIC-STUN");
322
323     col_add_fstr(pinfo->cinfo, COL_INFO, "Message: %s",
324              msg_type_str);
325
326     if (tree) {
327         guint transaction_id_first_word;
328
329         ti = proto_tree_add_item(tree, proto_classicstun, tvb, 0, -1, ENC_NA);
330
331         classicstun_tree = proto_item_add_subtree(ti, ett_classicstun);
332
333         if (((msg_type & CLASS_MASK) >> 4) == REQUEST) {
334             if (classicstun_trans->rep_frame) {
335                 proto_item *it;
336                 it=proto_tree_add_uint(classicstun_tree, hf_classicstun_response_in,
337                                tvb, 0, 0,
338                                classicstun_trans->rep_frame);
339                 PROTO_ITEM_SET_GENERATED(it);
340             }
341         }
342         else if ((((msg_type & CLASS_MASK) >> 4) == RESPONSE) ||
343              (((msg_type & CLASS_MASK) >> 4) == ERROR_RESPONSE)) {
344             /* This is a response */
345             if(classicstun_trans->req_frame){
346                 proto_item *it;
347                 nstime_t ns;
348
349                 it=proto_tree_add_uint(classicstun_tree, hf_classicstun_response_to, tvb, 0, 0, classicstun_trans->req_frame);
350                 PROTO_ITEM_SET_GENERATED(it);
351
352                 nstime_delta(&ns, &pinfo->fd->abs_ts, &classicstun_trans->req_time);
353                 it=proto_tree_add_time(classicstun_tree, hf_classicstun_time, tvb, 0, 0, &ns);
354                 PROTO_ITEM_SET_GENERATED(it);
355             }
356
357         }
358
359         proto_tree_add_uint(classicstun_tree, hf_classicstun_type, tvb, 0, 2, msg_type);
360         proto_tree_add_uint(classicstun_tree, hf_classicstun_length, tvb, 2, 2, msg_length);
361         proto_tree_add_item(classicstun_tree, hf_classicstun_id, tvb, 4, 16, ENC_NA);
362
363         /* Remember this (in host order) so we can show clear xor'd addresses */
364         transaction_id_first_word = tvb_get_ntohl(tvb, 4);
365
366         if (msg_length > 0) {
367             ta = proto_tree_add_item(classicstun_tree, hf_classicstun_att, tvb, CLASSICSTUN_HDR_LEN, msg_length, ENC_NA);
368             att_type_tree = proto_item_add_subtree(ta, ett_classicstun_att_type);
369
370             offset = CLASSICSTUN_HDR_LEN;
371
372             while( msg_length > 0) {
373                 att_type = tvb_get_ntohs(tvb, offset); /* Type field in attribute header */
374                 att_length = tvb_get_ntohs(tvb, offset+2); /* Length field in attribute header */
375
376                 ta = proto_tree_add_text(att_type_tree, tvb, offset,
377                              ATTR_HDR_LEN+att_length,
378                              "Attribute: %s",
379                              val_to_str(att_type, attributes, "Unknown (0x%04x)"));
380                 att_tree = proto_item_add_subtree(ta, ett_classicstun_att);
381
382                 proto_tree_add_uint(att_tree, classicstun_att_type, tvb,
383                             offset, 2, att_type);
384                 offset += 2;
385                 if (ATTR_HDR_LEN+att_length > msg_length) {
386                     proto_tree_add_uint_format_value(att_tree,
387                                    classicstun_att_length, tvb, offset, 2,
388                                    att_length,
389                                    "%u (bogus, goes past the end of the message)",
390                                    att_length);
391                     break;
392                 }
393                 proto_tree_add_uint(att_tree, classicstun_att_length, tvb,
394                             offset, 2, att_length);
395                 offset += 2;
396                 switch( att_type ){
397                     case MAPPED_ADDRESS:
398                     case RESPONSE_ADDRESS:
399                     case SOURCE_ADDRESS:
400                     case CHANGED_ADDRESS:
401                     case REFLECTED_FROM:
402                     case ALTERNATE_SERVER:
403                     case DESTINATION_ADDRESS:
404                     case REMOTE_ADDRESS:
405                         if (att_length < 2)
406                             break;
407                         proto_tree_add_item(att_tree, classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
408                         if (att_length < 4)
409                             break;
410                         proto_tree_add_item(att_tree, classicstun_att_port, tvb, offset+2, 2, ENC_BIG_ENDIAN);
411                         switch( tvb_get_guint8(tvb, offset+1) ){
412                             case 1:
413                                 if (att_length < 8)
414                                     break;
415                                 proto_tree_add_item(att_tree, classicstun_att_ipv4, tvb, offset+4, 4, ENC_BIG_ENDIAN);
416                                 break;
417
418                             case 2:
419                                 if (att_length < 20)
420                                     break;
421                                 proto_tree_add_item(att_tree, classicstun_att_ipv6, tvb, offset+4, 16, ENC_NA);
422                                 break;
423                         }
424                         break;
425
426                     case CHANGE_REQUEST:
427                         if (att_length < 4)
428                             break;
429                         proto_tree_add_item(att_tree, classicstun_att_change_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
430                         proto_tree_add_item(att_tree, classicstun_att_change_port, tvb, offset, 4, ENC_BIG_ENDIAN);
431                         break;
432
433                     case USERNAME:
434                     case PASSWORD:
435                     case MESSAGE_INTEGRITY:
436                     case NONCE:
437                     case REALM:
438                         if (att_length < 1)
439                             break;
440                         proto_tree_add_item(att_tree, classicstun_att_value, tvb, offset, att_length, ENC_NA);
441                         break;
442
443                     case ERROR_CODE:
444                         if (att_length < 3)
445                             break;
446                         proto_tree_add_item(att_tree, classicstun_att_error_class, tvb, offset+2, 1, ENC_BIG_ENDIAN);
447                         if (att_length < 4)
448                             break;
449                         proto_tree_add_item(att_tree, classicstun_att_error_number, tvb, offset+3, 1, ENC_BIG_ENDIAN);
450                         if (att_length < 5)
451                             break;
452                         proto_tree_add_item(att_tree, classicstun_att_error_reason, tvb, offset+4, (att_length-4), ENC_ASCII|ENC_NA);
453                         break;
454
455                     case LIFETIME:
456                         if (att_length < 4)
457                             break;
458                         proto_tree_add_item(att_tree, classicstun_att_lifetime, tvb, offset, 4, ENC_BIG_ENDIAN);
459                         break;
460
461                     case MAGIC_COOKIE:
462                         if (att_length < 4)
463                             break;
464                         proto_tree_add_item(att_tree, classicstun_att_magic_cookie, tvb, offset, 4, ENC_BIG_ENDIAN);
465                         break;
466
467                     case BANDWIDTH:
468                         if (att_length < 4)
469                             break;
470                         proto_tree_add_item(att_tree, classicstun_att_bandwidth, tvb, offset, 4, ENC_BIG_ENDIAN);
471                         break;
472
473                     case DATA:
474                         proto_tree_add_item(att_tree, classicstun_att_data, tvb, offset, att_length, ENC_NA);
475                         break;
476
477                     case UNKNOWN_ATTRIBUTES:
478                         for (i = 0; i < att_length; i += 4) {
479                             proto_tree_add_item(att_tree, classicstun_att_unknown, tvb, offset+i, 2, ENC_BIG_ENDIAN);
480                             proto_tree_add_item(att_tree, classicstun_att_unknown, tvb, offset+i+2, 2, ENC_BIG_ENDIAN);
481                         }
482                         break;
483
484                     case SERVER:
485                         proto_tree_add_item(att_tree, classicstun_att_server_string, tvb, offset, att_length, ENC_ASCII|ENC_NA);
486                         break;
487
488                     case XOR_MAPPED_ADDRESS:
489                         if (att_length < 2)
490                             break;
491                         proto_tree_add_item(att_tree, classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
492                         if (att_length < 4)
493                             break;
494                         proto_tree_add_item(att_tree, classicstun_att_xor_port, tvb, offset+2, 2, ENC_BIG_ENDIAN);
495
496                         /* Show the port 'in the clear'
497                            XOR (host order) transid with (host order) xor-port.
498                            Add host-order port into tree. */
499                         ti = proto_tree_add_uint(att_tree, classicstun_att_port, tvb, offset+2, 2,
500                                      tvb_get_ntohs(tvb, offset+2) ^
501                                      (transaction_id_first_word >> 16));
502                         PROTO_ITEM_SET_GENERATED(ti);
503
504                         if (att_length < 8)
505                             break;
506                         switch( tvb_get_guint8(tvb, offset+1) ){
507                             case 1:
508                                 if (att_length < 8)
509                                     break;
510                                 proto_tree_add_item(att_tree, classicstun_att_xor_ipv4, tvb, offset+4, 4, ENC_BIG_ENDIAN);
511
512                                 /* Show the address 'in the clear'.
513                                    XOR (host order) transid with (host order) xor-address.
514                                    Add in network order tree. */
515                                 ti = proto_tree_add_ipv4(att_tree, classicstun_att_ipv4, tvb, offset+4, 4,
516                                              tvb_get_ipv4(tvb, offset+4) ^ g_htonl(transaction_id_first_word));
517                                 PROTO_ITEM_SET_GENERATED(ti);
518                                 break;
519
520                             case 2:
521                                 if (att_length < 20)
522                                     break;
523                                 proto_tree_add_item(att_tree, classicstun_att_xor_ipv6, tvb, offset+4, 16, ENC_NA);
524                                 break;
525                         }
526                         break;
527
528                     case REQUESTED_ADDRESS_TYPE:
529                         if (att_length < 2)
530                             break;
531                         proto_tree_add_item(att_tree, classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
532                         break;
533
534                     case CONNECTION_REQUEST_BINDING:
535                         proto_tree_add_item(att_tree, classicstun_att_connection_request_binding, tvb, offset, att_length, ENC_ASCII|ENC_NA);
536                         break;
537
538                     default:
539                         break;
540                 }
541                 offset += att_length;
542                 msg_length -= ATTR_HDR_LEN+att_length;
543             }
544         }
545     }
546     return tvb_length(tvb);
547 }
548
549
550 static gboolean
551 dissect_classicstun_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
552 {
553     if (dissect_classicstun(tvb, pinfo, tree, NULL) == 0)
554         return FALSE;
555
556     return TRUE;
557 }
558
559
560
561
562 void
563 proto_register_classicstun(void)
564 {
565     static hf_register_info hf[] = {
566         { &hf_classicstun_type,
567             { "Message Type",   "classicstun.type",     FT_UINT16,
568             BASE_HEX,   VALS(messages), 0x0,    NULL,   HFILL }
569         },
570         { &hf_classicstun_length,
571             { "Message Length", "classicstun.length",   FT_UINT16,
572             BASE_HEX,   NULL,   0x0,    NULL,   HFILL }
573         },
574         { &hf_classicstun_id,
575             { "Message Transaction ID", "classicstun.id",   FT_BYTES,
576             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
577         },
578         { &hf_classicstun_att,
579             { "Attributes",     "classicstun.att",  FT_NONE,
580             BASE_NONE,      NULL,   0x0,    NULL,   HFILL }
581         },
582         { &hf_classicstun_response_in,
583             { "Response In", "classicstun.response_in",
584             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
585             "The response to this CLASSICSTUN query is in this frame", HFILL }},
586         { &hf_classicstun_response_to,
587             { "Request In", "classicstun.response_to",
588             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
589             "This is a response to the CLASSICSTUN Request in this frame", HFILL }},
590         { &hf_classicstun_time,
591             { "Time", "classicstun.time",
592             FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
593             "The time between the Request and the Response", HFILL }},
594
595         /* ////////////////////////////////////// */
596         { &classicstun_att_type,
597             { "Attribute Type", "classicstun.att.type", FT_UINT16,
598             BASE_HEX,   VALS(attributes),   0x0,    NULL,   HFILL }
599         },
600         { &classicstun_att_length,
601             { "Attribute Length",   "classicstun.att.length",   FT_UINT16,
602             BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
603         },
604         { &classicstun_att_value,
605             { "Value",  "classicstun.att.value",    FT_BYTES,
606             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
607         },
608         { &classicstun_att_family,
609             { "Protocol Family",    "classicstun.att.family",   FT_UINT16,
610             BASE_HEX,   VALS(attributes_family),    0x0,    NULL,   HFILL }
611         },
612         { &classicstun_att_ipv4,
613             { "IP",     "classicstun.att.ipv4", FT_IPv4,
614             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
615         },
616         { &classicstun_att_ipv6,
617             { "IP",     "classicstun.att.ipv6", FT_IPv6,
618             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
619         },
620         { &classicstun_att_port,
621             { "Port",   "classicstun.att.port", FT_UINT16,
622             BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
623         },
624         { &classicstun_att_change_ip,
625             { "Change IP","classicstun.att.change.ip",  FT_BOOLEAN,
626             16,     TFS(&tfs_set_notset),   0x0004, NULL,   HFILL}
627         },
628         { &classicstun_att_change_port,
629             { "Change Port","classicstun.att.change.port",  FT_BOOLEAN,
630             16,     TFS(&tfs_set_notset),   0x0002, NULL,   HFILL}
631         },
632         { &classicstun_att_unknown,
633             { "Unknown Attribute","classicstun.att.unknown",    FT_UINT16,
634             BASE_HEX,   NULL,   0x0,    NULL,   HFILL}
635         },
636         { &classicstun_att_error_class,
637             { "Error Class","classicstun.att.error.class",  FT_UINT8,
638             BASE_DEC,   NULL,   0x07,   NULL,   HFILL}
639         },
640         { &classicstun_att_error_number,
641             { "Error Code","classicstun.att.error", FT_UINT8,
642             BASE_DEC,   NULL,   0x0,    NULL,   HFILL}
643         },
644         { &classicstun_att_error_reason,
645             { "Error Reason Phase","classicstun.att.error.reason",  FT_STRING,
646             BASE_NONE,  NULL,   0x0,    NULL,   HFILL}
647         },
648         { &classicstun_att_xor_ipv4,
649             { "IP (XOR-d)",     "classicstun.att.ipv4-xord",    FT_IPv4,
650             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
651         },
652         { &classicstun_att_xor_ipv6,
653             { "IP (XOR-d)",     "classicstun.att.ipv6-xord",    FT_IPv6,
654             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
655         },
656         { &classicstun_att_xor_port,
657             { "Port (XOR-d)",   "classicstun.att.port-xord",    FT_UINT16,
658             BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
659         },
660         { &classicstun_att_server_string,
661             { "Server version","classicstun.att.server",    FT_STRING,
662             BASE_NONE,  NULL,   0x0,    NULL,   HFILL}
663         },
664         { &classicstun_att_lifetime,
665             { "Lifetime",   "classicstun.att.lifetime", FT_UINT32,
666             BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
667         },
668         { &classicstun_att_magic_cookie,
669             { "Magic Cookie",   "classicstun.att.magic.cookie", FT_UINT32,
670             BASE_HEX,   NULL,   0x0,    NULL,   HFILL }
671         },
672         { &classicstun_att_bandwidth,
673             { "Bandwidth",  "classicstun.att.bandwidth",    FT_UINT32,
674             BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
675         },
676         { &classicstun_att_data,
677             { "Data",   "classicstun.att.data", FT_BYTES,
678             BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
679         },
680         { &classicstun_att_connection_request_binding,
681             { "Connection Request Binding", "classicstun.att.connection_request_binding", FT_STRING,
682             BASE_NONE,  NULL, 0x0,  NULL,   HFILL }
683         },
684     };
685
686 /* Setup protocol subtree array */
687     static gint *ett[] = {
688         &ett_classicstun,
689         &ett_classicstun_att_type,
690         &ett_classicstun_att,
691     };
692
693 /* Register the protocol name and description */
694     proto_classicstun = proto_register_protocol("Simple Traversal of UDP Through NAT",
695                             "CLASSICSTUN", "classicstun");
696
697 /* Required function calls to register the header fields and subtrees used */
698     proto_register_field_array(proto_classicstun, hf, array_length(hf));
699     proto_register_subtree_array(ett, array_length(ett));
700
701     new_register_dissector("classicstun", dissect_classicstun, proto_classicstun);
702     new_register_dissector("classicstun-heur", dissect_classicstun_heur, proto_classicstun);
703 }
704
705
706 void
707 proto_reg_handoff_classicstun(void)
708 {
709 #if 0 /* The stun dissector registers on these ports */
710     dissector_handle_t classicstun_handle;
711
712     classicstun_handle = find_dissector("classicstun");
713
714     dissector_add_uint("tcp.port", TCP_PORT_STUN, classicstun_handle);
715     dissector_add_uint("udp.port", UDP_PORT_STUN, classicstun_handle);
716 #endif
717     heur_dissector_add("udp", dissect_classicstun_heur, proto_classicstun);
718     heur_dissector_add("tcp", dissect_classicstun_heur, proto_classicstun);
719 }