aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-mcpe.c
blob: 78a956e8907acb7a47f34c36c2a4cf793c8ee584 (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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
 * packet-mcpe.c
 *
 * Routines for Minecraft Pocket Edition protocol packet disassembly.
 *
 * Nick Carter <ncarter100@gmail.com>
 * Copyright 2014 Nick Carter
 *
 * Using info found at:
 *   http://wiki.vg/Pocket_Minecraft_Protocol#Packet_Encapsulation
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include "config.h"

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

#include "packet-raknet.h"

/* Minecraft Pocket Edition Protocol
 *
 * See also:
 * http://wiki.vg/Pocket_Edition_Protocol_Documentation
 */

#define MCPE_UDP_PORT_DEFAULT 19132 /* Not IANA registered */
static guint mcpe_udp_port_requested = MCPE_UDP_PORT_DEFAULT;

static int proto_mcpe;
static gint ett_mcpe; /* Should this node be expanded */
static gint ett_mcpe_batch;
static gint ett_mcpe_batch_record;
static gint ett_mcpe_login;
static gint ett_mcpe_string;

/*
 * Dissectors
 */
static dissector_handle_t mcpe_handle = NULL;
static dissector_table_t mcpe_packet_dissectors = NULL;

/*
 * Expert fields
 */
static expert_field ei_mcpe_unknown_packet_id;
static expert_field ei_mcpe_decompression_failed;
static expert_field ei_mcpe_encrypted_packet;

/*
 * Common Header fields
 */
static int hf_mcpe_message_id;
static int hf_mcpe_packet_id;
static int hf_mcpe_string_length;
static int hf_mcpe_UTF8_string;
static int hf_mcpe_byte_string;

/*
 * Fields specific to a packet ID
 */
static int hf_mcpe_protocol_version;
static int hf_mcpe_login_data_length;
static int hf_mcpe_login_data;
static int hf_mcpe_login;
static int hf_mcpe_chain_JSON;
static int hf_mcpe_client_data_JWT;
static int hf_mcpe_public_key;
static int hf_mcpe_server_token;

static int hf_mcpe_batch_length;
static int hf_mcpe_batch_body;
static int hf_mcpe_batch_records;
static int hf_mcpe_batch_record_length;
static int hf_mcpe_batch_record;

/*
 * RakNet Message ID
 */
static const value_string mcpe_message_names[] = {
    { 0xFE, "Wrapper" },
    { 0, NULL }
};

/*
 * Forward declarations
 */
void proto_register_mcpe(void);
void proto_reg_handoff_mcpe(void);
static int mcpe_dissect_login(tvbuff_t*, packet_info*, proto_tree*, void*);
static int mcpe_dissect_server_to_client_handshake(tvbuff_t*, packet_info*, proto_tree*, void*);
static int mcpe_dissect_batch(tvbuff_t*, packet_info*, proto_tree*, void*);

/*
 * Protocol definition and handlers.
 */
struct mcpe_handler_entry {
    value_string vs;
    dissector_t dissector_fp;
};

static const struct mcpe_handler_entry mcpe_packet_handlers[] = {
    { { 0x01, "Login" },
      mcpe_dissect_login },
    { { 0x03, "Server to Client Handshake" },
      mcpe_dissect_server_to_client_handshake },
    { { 0x06, "Batch" },
      mcpe_dissect_batch },
};

/*
 * Look up table from packet ID to name
 */
static value_string mcpe_packet_names[array_length(mcpe_packet_handlers)+1];

/*
 * Session state
 */
typedef struct mcpe_session_state {
    gboolean encrypted;
    guint32 encryption_starts_after; /* Frame number */
} mcpe_session_state_t;

static mcpe_session_state_t*
mcpe_get_session_state(packet_info *pinfo) {
    conversation_t* conversation;
    mcpe_session_state_t* state;

    conversation = find_or_create_conversation(pinfo);
    state = (mcpe_session_state_t*)conversation_get_proto_data(conversation, proto_mcpe);

    if (state == NULL) {
        state = wmem_new(wmem_file_scope(), mcpe_session_state_t);
        state->encrypted = FALSE;
        state->encryption_starts_after = 0;

        conversation_add_proto_data(conversation, proto_mcpe, state);
    }

    return state;
}

/*
 * Packet dissectors
 */
static void
mcpe_dissect_string(packet_info *pinfo, proto_tree *tree, int hf, tvbuff_t *tvb, gint *offset, guint encoding) {
    proto_item *ti;
    proto_tree *string_tree;
    guint32 length;
    guint32 length_width;

    if (encoding & ENC_LITTLE_ENDIAN) {
        /*
         * Yes it's crazy. Lengths of string come with two flavors:
         * big-endian uint16 and little-endian uint32.
         */
        length = tvb_get_letohl(tvb, *offset);
        length_width = 4;
    }
    else {
        length = tvb_get_ntohs(tvb, *offset);
        length_width = 2;
    }

    if (encoding & ENC_UTF_8) {
        guint8 *string;

        string = tvb_get_string_enc(pinfo->pool, tvb, *offset + length_width, length, ENC_UTF_8);

        ti = proto_tree_add_string(tree, hf, tvb, *offset, length + length_width, string);
        string_tree = proto_item_add_subtree(ti, ett_mcpe_string);

        proto_tree_add_item(string_tree, hf_mcpe_string_length, tvb,
                            *offset, length_width, encoding);
        *offset += length_width;

        proto_tree_add_item(string_tree, hf_mcpe_UTF8_string, tvb,
                            *offset, length, ENC_UTF_8);
        *offset += length;
    }
    else {
        guint8 *bytes;

        bytes = (guint8*)tvb_memdup(pinfo->pool, tvb, *offset + length_width, length);

        ti = proto_tree_add_bytes_with_length(tree, hf, tvb, *offset, length + length_width, bytes, length);
        string_tree = proto_item_add_subtree(ti, ett_mcpe_string);

        proto_tree_add_item(string_tree, hf_mcpe_string_length, tvb,
                            *offset, length_width, encoding);
        *offset += length_width;

        proto_tree_add_item(string_tree, hf_mcpe_byte_string, tvb,
                            *offset, length, ENC_NA);
        *offset += length;
    }
}

static int
mcpe_dissect_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    if (tree) {
        gint item_size;
        gint offset = 1;
        guint32 comp_length;
        proto_item *ti;
        tvbuff_t *login_tvb;

        item_size = 4;
        proto_tree_add_item(tree, hf_mcpe_protocol_version, tvb,
                            offset, item_size, ENC_BIG_ENDIAN);
        offset += item_size;

        item_size = 4;
        proto_tree_add_item_ret_uint(tree, hf_mcpe_login_data_length, tvb,
                                     offset, item_size, ENC_BIG_ENDIAN, &comp_length);
        offset += item_size;

        item_size = comp_length;
        ti = proto_tree_add_item(tree, hf_mcpe_login_data, tvb,
                                 offset, item_size, ENC_NA);

        login_tvb = tvb_child_uncompress(tvb, tvb, offset, comp_length);
        if (login_tvb) {
            guint32 decomp_length;
            proto_tree *login_tree;

            add_new_data_source(pinfo, login_tvb, "MCPE Decompressed login data");
            decomp_length = tvb_captured_length(login_tvb);

            offset = 0;
            item_size = decomp_length;
            ti = proto_tree_add_item(tree, hf_mcpe_login, login_tvb,
                                     offset, item_size, ENC_NA);
            login_tree = proto_item_add_subtree(ti, ett_mcpe_login);
            proto_item_append_text(ti, " (%u octets)", decomp_length);
            proto_item_set_generated(ti);

            mcpe_dissect_string(pinfo, login_tree, hf_mcpe_chain_JSON     , login_tvb, &offset, ENC_LITTLE_ENDIAN | ENC_UTF_8);
            mcpe_dissect_string(pinfo, login_tree, hf_mcpe_client_data_JWT, login_tvb, &offset, ENC_LITTLE_ENDIAN | ENC_UTF_8);
        }
        else {
            expert_add_info(pinfo, ti, &ei_mcpe_decompression_failed);
        }
    }
    return tvb_reported_length(tvb);
}

static int
mcpe_dissect_server_to_client_handshake(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    if (tree) {
        gint offset = 1;
        mcpe_session_state_t *state;

        mcpe_dissect_string(pinfo, tree, hf_mcpe_public_key, tvb, &offset, ENC_BIG_ENDIAN | ENC_UTF_8);
        mcpe_dissect_string(pinfo, tree, hf_mcpe_server_token, tvb, &offset, ENC_BIG_ENDIAN);

        /*
         * Everything will be encrypted once the server sends this.
         */
        state = mcpe_get_session_state(pinfo);
        state->encrypted = TRUE;
        state->encryption_starts_after = pinfo->num;
    }
    return tvb_reported_length(tvb);
}

static int
mcpe_dissect_batch(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
    if (tree) {
        guint32 item_size;
        guint32 offset = 1;
        proto_item *ti;
        guint32 comp_length;
        tvbuff_t *batch_tvb;

        item_size = 4;
        proto_tree_add_item_ret_uint(tree, hf_mcpe_batch_length, tvb,
                                     offset, item_size, ENC_BIG_ENDIAN, &comp_length);
        offset += item_size;

        item_size = comp_length;
        ti = proto_tree_add_item(tree, hf_mcpe_batch_body, tvb,
                                 offset, item_size, ENC_NA);

        batch_tvb = tvb_child_uncompress(tvb, tvb, offset, comp_length);
        if (batch_tvb) {
            guint32 decomp_length;
            proto_tree *batch_tree;

            add_new_data_source(pinfo, batch_tvb, "MCPE Decompressed batch");
            decomp_length = tvb_captured_length(batch_tvb);

            offset = 0;
            item_size = decomp_length;
            ti = proto_tree_add_item(tree, hf_mcpe_batch_records, batch_tvb,
                                     offset, item_size, ENC_NA);
            batch_tree = proto_item_add_subtree(ti, ett_mcpe_batch);
            proto_item_append_text(ti, " (%u octets)", decomp_length);
            proto_item_set_generated(ti);

            col_append_str(pinfo->cinfo, COL_INFO, " [");

            while (TRUE) {
                guint32 record_length;
                tvbuff_t *record_tvb;
                proto_tree *record_tree;
                guint32 packet_id;
                gint dissected;

                item_size = 4;
                proto_tree_add_item_ret_uint(batch_tree, hf_mcpe_batch_record_length, batch_tvb,
                                             offset, item_size, ENC_BIG_ENDIAN, &record_length);
                offset += item_size;

                record_tvb = tvb_new_subset_length(batch_tvb, offset, record_length);
                offset += record_length;

                /*
                 * Take the whole buffer as a single MCPE packet.
                 */
                ti = proto_tree_add_item(batch_tree, hf_mcpe_batch_record, record_tvb,
                                         0, -1, ENC_NA);
                record_tree = proto_item_add_subtree(ti, ett_mcpe_batch_record);

                /*
                 * The first octet is the packet ID.
                 */
                proto_tree_add_item_ret_uint(record_tree, hf_mcpe_packet_id,
                                             record_tvb, 0, 1, ENC_NA, &packet_id);

                proto_item_append_text(ti, " (%s)",
                                       val_to_str(packet_id, mcpe_packet_names, "Unknown ID: %#x"));
                col_append_str(pinfo->cinfo, COL_INFO,
                               val_to_str(packet_id, mcpe_packet_names, "Unknown packet ID: %#x"));

                dissected =
                    dissector_try_uint_new(mcpe_packet_dissectors, packet_id,
                                           record_tvb, pinfo, record_tree, TRUE, data);
                if (!dissected) {
                    expert_add_info(pinfo, ti, &ei_mcpe_unknown_packet_id);
                }

                if (offset < decomp_length) {
                    col_append_str(pinfo->cinfo, COL_INFO, ", ");
                }
                else {
                    break;
                }
            }

            col_append_str(pinfo->cinfo, COL_INFO, "]");
        }
        else {
            expert_add_info(pinfo, ti, &ei_mcpe_decompression_failed);
        }
    }
    return tvb_reported_length(tvb);
}

static void
mcpe_init_message_names(void)
{
    unsigned int i;

    for (i = 0; i < array_length(mcpe_packet_handlers); i++) {
        mcpe_packet_names[i].value  = mcpe_packet_handlers[i].vs.value;
        mcpe_packet_names[i].strptr = mcpe_packet_handlers[i].vs.strptr;
    }
    mcpe_packet_names[array_length(mcpe_packet_handlers)].value  = 0;
    mcpe_packet_names[array_length(mcpe_packet_handlers)].strptr = NULL;
}

static gboolean
test_mcpe_heur(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree _U_, void* data _U_)
{
    /*
     * 0xFE "Wrapper" is the only message ID that MCPE uses. The sole
     * purpose of Wrapper message is to make a RakNet message out of a
     * game packet.
     */
    if (tvb_strneql(tvb, 0, "\xFE", 1) == 0) {
        /*
         * Does the message have a packet ID?
         */
        if (tvb_captured_length(tvb) >= 2) {
            /*
             * Inspect the packet ID. If it's known to us the message
             * can be considered to be an MCPE packet.
             */
            gint8 packet_id = tvb_get_guint8(tvb, 1);

            *(dissector_handle_t*)data =
                dissector_get_uint_handle(mcpe_packet_dissectors, packet_id);

            if (*(dissector_handle_t*)data) {
                return TRUE;
            }
        }
    }
    return FALSE;
}

static gboolean
dissect_mcpe_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    dissector_handle_t handle;

    if (test_mcpe_heur(tvb, pinfo, tree, &handle)) {
        proto_item *ti;
        proto_tree *mcpe_tree;
        guint32 message_id;
        guint32 packet_id;
        tvbuff_t *packet_tvb;

        raknet_conversation_set_dissector(pinfo, mcpe_handle);
        col_set_str(pinfo->cinfo, COL_PROTOCOL, "MCPE");
        col_clear(pinfo->cinfo, COL_INFO);

        /*
         * Take the whole buffer as a single MCPE packet.
         */
        ti = proto_tree_add_item(tree, proto_mcpe, tvb, 0, -1, ENC_NA);
        mcpe_tree = proto_item_add_subtree(ti, ett_mcpe);

        /*
         * The first octet is always 0xFE (Wrapper). We intentionally
         * use DISSECTOR_ASSERT() here because test_mcpe_heur() has
         * already tested it.
         */
        proto_tree_add_item_ret_uint(mcpe_tree, hf_mcpe_message_id,
                                     tvb, 0, 1, ENC_NA, &message_id);
        DISSECTOR_ASSERT(message_id == 0xFE);

        /*
         * The next octet is the packet ID.
         */
        proto_tree_add_item_ret_uint(mcpe_tree, hf_mcpe_packet_id,
                                     tvb, 1, 1, ENC_NA, &packet_id);

        proto_item_append_text(ti, " (%s)",
                               val_to_str(packet_id, mcpe_packet_names, "Unknown ID: %#x"));
        col_add_str(pinfo->cinfo, COL_INFO,
                    val_to_str(packet_id, mcpe_packet_names, "Unknown packet ID: %#x"));

        packet_tvb = tvb_new_subset_remaining(tvb, 1);
        return call_dissector_only(handle, packet_tvb, pinfo, mcpe_tree, data) > 0;
    }
    else {
        return FALSE;
    }
}

static int
dissect_mcpe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    mcpe_session_state_t *state;

    state = mcpe_get_session_state(pinfo);
    if (state->encrypted && pinfo->num > state->encryption_starts_after) {
        /*
         * Encrypted packets don't even have any headers indicating
         * they are encrypted. And we don't support the cipher they
         * use.
         */
        proto_item *ti;
        gint packet_size;

        col_set_str(pinfo->cinfo, COL_PROTOCOL, "MCPE");
        col_add_str(pinfo->cinfo, COL_INFO, "Encrypted packet");

        packet_size = tvb_reported_length(tvb);
        ti = proto_tree_add_item(tree, proto_mcpe, tvb, 0, packet_size, ENC_NA);
        proto_item_append_text(ti, ", Encrypted packet (%d octets)", packet_size);
        expert_add_info(pinfo, ti, &ei_mcpe_encrypted_packet);

        return tvb_captured_length(tvb);
    }
    else {
        /*
         * We reuse our heuristic dissector here because we have no reason
         * to implement almost the same dissector twice.
         */
        if (dissect_mcpe_heur(tvb, pinfo, tree, data)) {
            return tvb_captured_length(tvb);
        }
        else {
            return 0;
        }
    }
}

void
proto_register_mcpe(void)
{
    static hf_register_info hf[] = {
        /*
         * Common Header fields
         */
        { &hf_mcpe_message_id,
            { "MCPE Message ID", "mcpe.message.id",
                FT_UINT8, BASE_HEX,
                VALS(mcpe_message_names), 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_packet_id,
            { "MCPE Packet ID", "mcpe.packet.id",
                FT_UINT8, BASE_HEX,
                VALS(mcpe_packet_names), 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_string_length,
            { "MCPE String length", "mcpe.string.length",
                FT_UINT32, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_UTF8_string,
            { "MCPE UTF-8 String", "mcpe.string.UTF8",
                FT_STRING, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_byte_string,
            { "MCPE Byte string", "mcpe.string.bytes",
                FT_BYTES, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        /*
         * Fields specific to a packet ID
         */
        { &hf_mcpe_protocol_version,
            { "MCPE Protocol version", "mcpe.protocol.version",
                FT_UINT32, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_login_data_length,
            { "MCPE Compressed login data length", "mcpe.login.data.length",
                FT_UINT32, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_login_data,
            { "MCPE Compressed login data", "mcpe.login.data",
                FT_NONE, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_login,
            { "MCPE Decompressed login data", "mcpe.login",
                FT_NONE, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_chain_JSON,
            { "MCPE Chain JSON", "mcpe.chain.JSON",
                FT_STRING, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_client_data_JWT,
            { "MCPE Client data JWT", "mcpe.client.data.JWT",
                FT_STRING, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_public_key,
            { "MCPE Public key", "mcpe.public.key",
                FT_STRING, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_server_token,
            { "MCPE Server token", "mcpe.server.token",
                FT_BYTES, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_batch_length,
            { "MCPE Compressed batch length", "mcpe.batch.length",
                FT_UINT32, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_batch_body,
            { "MCPE Compressed batch body", "mcpe.batch.body",
                FT_NONE, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_batch_records,
            { "MCPE Decompressed batch records", "mcpe.batch.records",
                FT_NONE, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_batch_record_length,
            { "MCPE Batch record length", "mcpe.batch.record.length",
                FT_UINT32, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_batch_record,
            { "MCPE Batch record", "mcpe.batch.record",
                FT_NONE, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
    };

    /*
     * Setup protocol subtree array
     */
    static gint *ett[] = {
        &ett_mcpe,
        &ett_mcpe_batch,
        &ett_mcpe_batch_record,
        &ett_mcpe_login,
        &ett_mcpe_string,
    };
    module_t *mcpe_module;

    /*
     * Set up expert info.
     */
    static ei_register_info ei[] = {
        { &ei_mcpe_unknown_packet_id,
          { "mcpe.unknown.id", PI_UNDECODED, PI_WARN,
            "MCPE unknown packet ID",
            EXPFILL }
        },
        { &ei_mcpe_decompression_failed,
          { "mcpe.decompression.failed", PI_MALFORMED, PI_ERROR,
            "MCPE packet decompression failed",
            EXPFILL }
        },
        { &ei_mcpe_encrypted_packet,
          { "mcpe.encrypted", PI_DECRYPTION, PI_NOTE,
            "MCPE encrypted packet",
            EXPFILL }
        },
    };
    expert_module_t *expert_mcpe;

    /*
     * Init data structs.
     */
    mcpe_init_message_names();

    /*
     * Register the protocol with wireshark.
     */
    proto_mcpe = proto_register_protocol ("Minecraft Pocket Edition", "MCPE", "mcpe");

    /*
     * Register expert support.
     */
    expert_mcpe = expert_register_protocol(proto_mcpe);
    expert_register_field_array(expert_mcpe, ei, array_length(ei));

    /*
     * Register detailed dissection arrays.
     */
    proto_register_field_array(proto_mcpe, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    /*
     * Register dissectors.
     */
    mcpe_handle =
        register_dissector("mcpe", dissect_mcpe, proto_mcpe);

    mcpe_packet_dissectors =
        register_dissector_table("mcpe.packet.id", "MCPE packets",
                                 proto_mcpe, FT_UINT8, BASE_HEX);

    /* Register a configuration option for UDP port */
    mcpe_module =
        prefs_register_protocol(proto_mcpe, proto_reg_handoff_mcpe);

    prefs_register_uint_preference(mcpe_module, "udp.port",
            "MCPE Server UDP Port",
            "Set the UDP port for the MCPE Server",
            10, &mcpe_udp_port_requested);
}

void
proto_reg_handoff_mcpe(void)
{
    static guint last_server_port;
    static gboolean init_done = FALSE;

    if (init_done) {
        raknet_delete_udp_dissector(last_server_port, mcpe_handle);
    }
    else {
        unsigned int i;

        for (i = 0; i < array_length(mcpe_packet_handlers); i++) {
            dissector_add_uint(
                "mcpe.packet.id",
                mcpe_packet_handlers[i].vs.value,
                create_dissector_handle(
                    mcpe_packet_handlers[i].dissector_fp, proto_mcpe));
        }

        heur_dissector_add("raknet", dissect_mcpe_heur,
                           "MCPE over RakNet", "mcpe_raknet", proto_mcpe, HEURISTIC_ENABLE);
    }

    last_server_port = mcpe_udp_port_requested;
    init_done = TRUE;

    /* MCPE is a protocol that carries RakNet packets over UDP */
    raknet_add_udp_dissector(mcpe_udp_port_requested, mcpe_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */