summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-bt-tracker.c
blob: 1f856c3a13ec62b166842aa5414240583bdec791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* packet-bt-tracker.c
 * Routines for BitTorrent Tracker over UDP dissection
 * Copyright 2023, Ivan Nardi <nardi.ivan@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/to_str.h>
#include <epan/prefs.h>

void proto_register_bt_tracker(void);
void proto_reg_handoff_bt_tracker(void);

/* Specifications:
 * https://www.bittorrent.org/beps/bep_0015.html BEP 15 UDP Tracker Protocol for BitTorrent
 * https://www.bittorrent.org/beps/bep_0041.html BEP 41 UDP Tracker Protocol Extensions
 */

enum {
  ACTION_CONNECT = 0,
  ACTION_ANNOUNCE = 1,
  ACTION_SCRAPE = 2,
  ACTION_ERROR = 3,
};

enum {
  MSG_TYPE_CONNECT_REQUEST,
  MSG_TYPE_CONNECT_RESPONSE,
  MSG_TYPE_ANNOUNCE_REQUEST,
  MSG_TYPE_ANNOUNCE_RESPONSE,
  MSG_TYPE_SCRAPE_REQUEST,
  MSG_TYPE_SCRAPE_RESPONSE,
  MSG_TYPE_ERROR_RESPONSE,

  MSG_TYPE_UNKNOWN,
};

static const value_string bt_tracker_msg_type_vals[] = {
  { MSG_TYPE_CONNECT_REQUEST,   "Connection Request"  },
  { MSG_TYPE_CONNECT_RESPONSE,  "Connection Response"  },
  { MSG_TYPE_ANNOUNCE_REQUEST,  "Announce Request"  },
  { MSG_TYPE_ANNOUNCE_RESPONSE, "Announce Response"  },
  { MSG_TYPE_SCRAPE_REQUEST,    "Scrape Request"  },
  { MSG_TYPE_SCRAPE_RESPONSE,   "Scrape Response"  },
  { MSG_TYPE_ERROR_RESPONSE,    "Error Response"  },
  { 0, NULL }
};

static const value_string bt_tracker_event_vals[] = {
  { 0, "None"  },
  { 1, "Completed"  },
  { 2, "Started"  },
  { 3, "Stopped"  },
  { 0, NULL }
};

static const value_string bt_tracker_action_vals[] = {
  { ACTION_CONNECT,  "Connect"  },
  { ACTION_ANNOUNCE, "Announce"   },
  { ACTION_SCRAPE,   "Scrape" },
  { ACTION_ERROR,    "Error" },
  { 0, NULL }
};

enum {
  EXT_END_OF_OPTIONS  = 0,
  EXT_NOP             = 1,
  EXT_URLDATA         = 2,
  EXT_MAX
};

static const value_string bt_tracker_extension_type_vals[] = {
  { EXT_END_OF_OPTIONS, "End of Options" },
  { EXT_NOP,            "NOP" },
  { EXT_URLDATA,        "URL Data" },
  { 0, NULL }
};

static int proto_bt_tracker = -1;
static dissector_handle_t bt_tracker_handle;

static int hf_bt_tracker_msg_type = -1;
static int hf_bt_tracker_protocol_id = -1;
static int hf_bt_tracker_action = -1;
static int hf_bt_tracker_transaction_id = -1;
static int hf_bt_tracker_connection_id = -1;
static int hf_bt_tracker_info_hash = -1;
static int hf_bt_tracker_peer_id = -1;
static int hf_bt_tracker_downloaded = -1;
static int hf_bt_tracker_left = -1;
static int hf_bt_tracker_uploaded = -1;
static int hf_bt_tracker_event = -1;
static int hf_bt_tracker_ip_address = -1;
static int hf_bt_tracker_key = -1;
static int hf_bt_tracker_num_want = -1;
static int hf_bt_tracker_port = -1;
static int hf_bt_tracker_interval = -1;
static int hf_bt_tracker_leechers = -1;
static int hf_bt_tracker_seeders = -1;
static int hf_bt_tracker_trackers = -1;
static int hf_bt_tracker_tracker = -1;
static int hf_bt_tracker_tr_ip = -1;
static int hf_bt_tracker_tr_ip6 = -1;
static int hf_bt_tracker_tr_port = -1;
static int hf_bt_tracker_completed = -1;
static int hf_bt_tracker_error_msg = -1;
static int hf_bt_tracker_extension = -1;
static int hf_bt_tracker_extension_type = -1;
static int hf_bt_tracker_extension_len = -1;
static int hf_bt_tracker_extension_unknown = -1;
static int hf_bt_tracker_extension_urldata = -1;

static gint ett_bt_tracker = -1;
static gint ett_bt_tracker_trackers = -1;
static gint ett_bt_tracker_extension = -1;

#define MAGIC_CONSTANT 0x41727101980

static guint
get_message_type(tvbuff_t *tvb)
{
  if (tvb_get_ntoh64(tvb, 0) == MAGIC_CONSTANT &&
      tvb_get_ntohl(tvb, 8) == ACTION_CONNECT)
    return MSG_TYPE_CONNECT_REQUEST;
  if (tvb_get_ntohl(tvb, 0) == ACTION_CONNECT)
    return MSG_TYPE_CONNECT_RESPONSE;
  if (tvb_get_ntohl(tvb, 8) == ACTION_ANNOUNCE)
    return MSG_TYPE_ANNOUNCE_REQUEST;
  if (tvb_get_ntohl(tvb, 0) == ACTION_ANNOUNCE)
    return MSG_TYPE_ANNOUNCE_RESPONSE;
  if (tvb_get_ntohl(tvb, 8) == ACTION_SCRAPE)
    return MSG_TYPE_SCRAPE_REQUEST;
  if (tvb_get_ntohl(tvb, 0) == ACTION_SCRAPE)
    return MSG_TYPE_SCRAPE_RESPONSE;
  if (tvb_get_ntohl(tvb, 0) == ACTION_ERROR)
    return MSG_TYPE_ERROR_RESPONSE;

  return MSG_TYPE_UNKNOWN;
}

static gboolean
is_ipv4_format(packet_info *pinfo)
{
  wmem_list_frame_t *cur;
  gint cur_proto;
  const char *cur_name;

  /* Format of Announce Response message depends on IPv4 vs IPv6
     "Which format is used is determined by the address family of the underlying UDP packet.
      I.e. packets from a v4 address use the v4 format, those from a v6 address use the v6 format."
     Check the innermost IP layer, to take into account tunnels
  */

  cur = wmem_list_frame_prev(wmem_list_tail(pinfo->layers));
  while (cur != NULL) {
    cur_proto = (gint)GPOINTER_TO_UINT(wmem_list_frame_data(cur));
    cur_name = proto_get_protocol_filter_name(cur_proto);
    if (!strcmp(cur_name, "ip"))
      return TRUE;
    if (!strcmp(cur_name, "ipv6"))
      return FALSE;
    cur = wmem_list_frame_prev(cur);
  }
  return TRUE;
}
static int
dissect_bt_tracker_extension(tvbuff_t *tvb, packet_info _U_*pinfo, proto_tree *tree, int offset)
{
  proto_item *ti;
  proto_tree *ext_tree;
  guint8 extension_type;
  guint32 extension_length;
  gint32 tot_length;

  while (offset < (int)tvb_reported_length(tvb)) {
    extension_type = tvb_get_guint8(tvb, offset);

    tot_length = 1;
    if (extension_type == EXT_URLDATA) {
      tot_length += 1 + tvb_get_guint8(tvb, offset + 1);
    } else if (extension_type >= EXT_MAX) {
      tot_length = -1;
    }

    ti = proto_tree_add_none_format(tree, hf_bt_tracker_extension, tvb, offset, tot_length, "Extension: %s", val_to_str_const(extension_type, bt_tracker_extension_type_vals, "Unknown"));
    ext_tree = proto_item_add_subtree(ti, ett_bt_tracker_extension);
    proto_tree_add_item(ext_tree, hf_bt_tracker_extension_type, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset += 1;

    switch (extension_type) {
      case EXT_END_OF_OPTIONS:
        /* Option parsing continues until either the end of the packet is reached, or an EndOfOptions option is encountered, whichever happens first */
        return offset;
      case EXT_NOP:
        /* A special case option that has a fixed-length of one byte. It is not followed by a length field, or associated data.
           A NOP has no affect on option parsing. It is used only if optional padding is necessary in the future. */
        break;
      case EXT_URLDATA:
        proto_tree_add_item_ret_uint(ext_tree, hf_bt_tracker_extension_len, tvb, offset, 1, ENC_BIG_ENDIAN, &extension_length);
        offset += 1;
        proto_tree_add_item(ext_tree, hf_bt_tracker_extension_urldata, tvb, offset, extension_length, ENC_ASCII);
        offset += extension_length;
        break;
      default:
        proto_tree_add_item(ext_tree, hf_bt_tracker_extension_unknown, tvb, offset, -1, ENC_NA);
        return offset;
    }
  }
  return offset;
}

static int
dissect_bt_tracker_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, guint msg_type)
{
  guint node_index = 0;
  gint stride_length;
  proto_item *ti;
  proto_tree *sub_tree;
  gboolean is_ipv6;

  ti = proto_tree_add_uint(tree, hf_bt_tracker_msg_type, tvb, 0, 0, msg_type);
  proto_item_set_generated(ti);

  switch (msg_type) {
  case MSG_TYPE_CONNECT_REQUEST:
    proto_tree_add_item(tree, hf_bt_tracker_protocol_id, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    break;

  case MSG_TYPE_CONNECT_RESPONSE:
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_connection_id, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    break;

  case MSG_TYPE_ANNOUNCE_REQUEST:
    proto_tree_add_item(tree, hf_bt_tracker_connection_id, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_info_hash, tvb, offset, 20, ENC_NA);
    offset += 20;
    proto_tree_add_item(tree, hf_bt_tracker_peer_id, tvb, offset, 20, ENC_NA);
    offset += 20;
    proto_tree_add_item(tree, hf_bt_tracker_downloaded, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_left, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_uploaded, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_event, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_ip_address, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_key, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_num_want, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_port, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    offset = dissect_bt_tracker_extension(tvb, pinfo, tree, offset);
    break;

  case MSG_TYPE_ANNOUNCE_RESPONSE:
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_interval, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_leechers, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_seeders, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;

    if (tvb_captured_length_remaining(tvb, offset) > 0)
    {
      stride_length = 6;
      is_ipv6 = !is_ipv4_format(pinfo);
      if (is_ipv6)
        stride_length = 18;
      ti = proto_tree_add_item(tree, hf_bt_tracker_trackers, tvb, offset, -1, ENC_NA);
      sub_tree = proto_item_add_subtree(ti, ett_bt_tracker_trackers);

      while (tvb_captured_length_remaining(tvb, offset) >= stride_length)
      {
        proto_item *node_ti;
        proto_tree *node_tree;

        node_index += 1;

        node_ti = proto_tree_add_item(sub_tree, hf_bt_tracker_tracker, tvb, offset, stride_length, ENC_NA);
        proto_item_append_text(node_ti, " %d", node_index);
        node_tree = proto_item_add_subtree(node_ti, ett_bt_tracker_trackers);

        if (is_ipv6)
        {
          proto_tree_add_item( node_tree, hf_bt_tracker_tr_ip6, tvb, offset, 16, ENC_NA);
          proto_item_append_text(node_ti, ", IPv6/Port: [%s]", tvb_ip6_to_str(pinfo->pool, tvb, offset));

          proto_tree_add_item( node_tree, hf_bt_tracker_tr_port, tvb, offset + 16, 2, ENC_BIG_ENDIAN);
          proto_item_append_text(node_ti, ":%u", tvb_get_ntohs( tvb, offset + 16 ));
        }
        else
        {
          proto_tree_add_item( node_tree, hf_bt_tracker_tr_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
          proto_item_append_text(node_ti, ", IPv4/Port: %s", tvb_ip_to_str(pinfo->pool, tvb, offset));

          proto_tree_add_item( node_tree, hf_bt_tracker_tr_port, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
          proto_item_append_text(node_ti, ":%u", tvb_get_ntohs( tvb, offset + 4 ));
        }

        offset += stride_length;
      }
      proto_item_set_text(ti, "Trackers: %d trackers", node_index);
      col_append_fstr(pinfo->cinfo, COL_INFO, ": %d trackers", node_index);
    }

    break;

  case MSG_TYPE_SCRAPE_REQUEST:
    proto_tree_add_item(tree, hf_bt_tracker_connection_id, tvb, offset, 8, ENC_BIG_ENDIAN);
    offset += 8;
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;

    while (tvb_captured_length_remaining(tvb, offset) >= 20)
    {
      proto_tree_add_item(tree, hf_bt_tracker_info_hash, tvb, offset, 20, ENC_NA);
      offset += 20;
    }
    break;

  case MSG_TYPE_SCRAPE_RESPONSE:
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;

    while (tvb_captured_length_remaining(tvb, offset) >= 12)
    {
      proto_tree_add_item(tree, hf_bt_tracker_seeders, tvb, offset, 4, ENC_BIG_ENDIAN);
      offset += 4;
      proto_tree_add_item(tree, hf_bt_tracker_completed, tvb, offset, 4, ENC_BIG_ENDIAN);
      offset += 4;
      proto_tree_add_item(tree, hf_bt_tracker_leechers, tvb, offset, 4, ENC_BIG_ENDIAN);
      offset += 4;
    }

    break;

  case MSG_TYPE_ERROR_RESPONSE:
    proto_tree_add_item(tree, hf_bt_tracker_action, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_transaction_id, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_bt_tracker_error_msg, tvb, offset, -1, ENC_ASCII);
    offset = tvb_captured_length(tvb);
    break;

  }

  return offset;

}
static int
dissect_bt_tracker(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
  guint msg_type;
  proto_item *ti;
  proto_tree *sub_tree;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "BT-Tracker");
  col_clear(pinfo->cinfo, COL_INFO);

  msg_type = get_message_type(tvb);

  col_append_str(pinfo->cinfo, COL_INFO, val_to_str_const(msg_type, bt_tracker_msg_type_vals, " Unknown Msg Type"));

  ti = proto_tree_add_item(tree, proto_bt_tracker, tvb, 0, -1, ENC_NA);
  sub_tree = proto_item_add_subtree(ti, ett_bt_tracker);

  return dissect_bt_tracker_msg(tvb, pinfo, sub_tree, 0, msg_type);
}


static gboolean
dissect_bt_tracker_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
  conversation_t *conversation;

  /* Look for a Connect Request */
  if (tvb_captured_length_remaining(tvb, 0) < 16)
    return FALSE;
  if (tvb_get_ntoh64(tvb, 0) != MAGIC_CONSTANT)
    return FALSE;
  if (tvb_get_ntohl(tvb, 8) != ACTION_CONNECT)
    return FALSE;

  conversation = find_or_create_conversation(pinfo);
  conversation_set_dissector_from_frame_number(conversation, pinfo->num, bt_tracker_handle);

  dissect_bt_tracker(tvb, pinfo, tree, data);
  return TRUE;
}

void
proto_register_bt_tracker(void)
{
  static hf_register_info hf[] = {
    { &hf_bt_tracker_protocol_id,
      { "Protocol", "bt-tracker.proto_id",
      FT_UINT64, BASE_HEX, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_action,
      { "Action", "bt-tracker.action",
      FT_UINT32, BASE_DEC, VALS(bt_tracker_action_vals), 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_transaction_id,
      { "Transaction Id", "bt-tracker.transaction_id",
      FT_UINT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_connection_id,
      { "Connection Id", "bt-tracker.connection_id",
      FT_UINT64, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_msg_type,
      { "Message Type", "bt-tracker.msg_type",
      FT_UINT8, BASE_DEC, VALS(bt_tracker_msg_type_vals), 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_info_hash,
      { "Info Hash", "bt-tracker.info_hash",
      FT_BYTES, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_peer_id,
      { "Peer Id", "bt-tracker.peer_id",
      FT_BYTES, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_downloaded,
      { "Downloaded", "bt-tracker.downloaded",
      FT_UINT64, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_left,
      { "Left", "bt-tracker.left",
      FT_UINT64, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_uploaded,
      { "Uploaded", "bt-tracker.uploaded",
      FT_UINT64, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_event,
      { "Event", "bt-tracker.event",
      FT_UINT32, BASE_DEC, VALS(bt_tracker_event_vals), 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_ip_address,
      { "IP Address", "bt-tracker.ip_address",
      FT_IPv4, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_key,
      { "Key", "bt-tracker.key",
      FT_UINT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_num_want,
      { "Num Want", "bt-tracker.num_want",
      FT_INT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_port,
      { "Port", "bt-tracker.port",
      FT_UINT16, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_interval,
      { "Interval", "bt-tracker.interval",
      FT_INT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_leechers,
      { "Leechers", "bt-tracker.leechers",
      FT_INT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_seeders,
      { "Seeders", "bt-tracker.seeders",
      FT_INT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_trackers,
      { "Trackers", "bt-tracker.trackers",
      FT_NONE, BASE_NONE, NULL, 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_tracker,
      { "Tracker", "bt-tracker.tracker",
      FT_NONE, BASE_NONE, NULL, 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_tr_ip,
      { "IP", "bt-tracker.tracker.ip",
      FT_IPv4, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_tr_ip6,
      { "IPv6", "bt-tracker.tracker.ip6",
      FT_IPv6, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_tr_port,
      { "(TCP) Port", "bt-tracker.tracker.port",
      FT_UINT16, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_completed,
      { "Completed", "bt-tracker.completed",
      FT_INT32, BASE_DEC, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_error_msg,
      { "Error message", "bt-tracker.error_msg",
      FT_STRING, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
    { &hf_bt_tracker_extension,
      { "Extension", "bt-tracker.extension",
      FT_NONE, BASE_NONE, NULL, 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_extension_type,
      { "Extension Type", "bt-tracker.extension_type",
      FT_UINT8, BASE_HEX, VALS(bt_tracker_extension_type_vals), 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_extension_len,
      { "Extension Length", "bt-tracker.extension_len",
      FT_UINT8, BASE_DEC|BASE_UNIT_STRING, &units_byte_bytes, 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_extension_unknown,
      { "Extension Unknown", "bt-tracker.extension_unknown",
      FT_BYTES, BASE_NONE, NULL, 0x0,
      NULL, HFILL }
    },
    { &hf_bt_tracker_extension_urldata,
      { "URL Data", "bt-tracker.extension.urldata",
      FT_STRING, BASE_NONE, NULL, 0x00,
      NULL, HFILL }
    },
  };

  /* Setup protocol subtree array */
  static gint *ett[] = { &ett_bt_tracker, &ett_bt_tracker_trackers, &ett_bt_tracker_extension};
  module_t *bt_tracker_module;

  /* Register protocol */
  proto_bt_tracker = proto_register_protocol ("BitTorrent Tracker", "BT-Tracker", "bt-tracker");

  bt_tracker_module = prefs_register_protocol(proto_bt_tracker, NULL);
  prefs_register_obsolete_preference(bt_tracker_module, "enable");

  proto_register_field_array(proto_bt_tracker, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_bt_tracker(void)
{
  heur_dissector_add("udp", dissect_bt_tracker_heur, "BitTorrent Tracker over UDP", "bt_tracker_udp", proto_bt_tracker, HEURISTIC_ENABLE);

  bt_tracker_handle = create_dissector_handle(dissect_bt_tracker, proto_bt_tracker);
  dissector_add_for_decode_as_with_preference("udp.port", bt_tracker_handle);
}