aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-mcpe.c
blob: fba05f802b325d2a9f7d2c468cb3c09e0db01056 (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
/*
 * 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
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "config.h"
#include <epan/packet.h>
#include <epan/prefs.h>

#define MCPE_UDP_PORT_DEFAULT 19132
static guint mcpe_udp_port_requested = MCPE_UDP_PORT_DEFAULT;

static int proto_mcpe = -1;
static gint ett_mcpe = -1; /* Should this node be expanded */


/*
 * First byte gives us the packet id
 */
static int hf_mcpe_packet_id = -1;

/*
 * Custom payload encoding header.
 */
static int hf_mcpe_payload_encoding = -1;
static int hf_mcpe_general_packet_number = -1;
static int hf_mcpe_general_packet_payload = -1;
static int hf_mcpe_general_packet_payload_length = -1;
static int hf_mcpe_general_packet_payload_count = -1;

/*
 * Fields specific to a packet id type.
 */
static int hf_mcpe_0xC0_unknown = -1;
static int hf_mcpe_0xC0_single_packet = -1;

/*
 * Forward declarations.
 */
void proto_register_mcpe(void);
void proto_reg_handoff_mcpe(void);

static void
mcpe_dissect_detail_0xA0(tvbuff_t *tvb, proto_tree *raknet_tree, gint offset)
{
    gboolean single_packet;
    gint item_size;

    item_size = 2;
    proto_tree_add_item(raknet_tree, hf_mcpe_0xC0_unknown, tvb, offset,
                        item_size, ENC_BIG_ENDIAN);
    offset += item_size;

    single_packet = (tvb_get_guint8(tvb, offset) != 0);
    item_size = 1;
    proto_tree_add_item(raknet_tree, hf_mcpe_0xC0_single_packet, tvb, offset,
                        item_size, ENC_BIG_ENDIAN);
    offset += item_size;

    item_size = 3;
    proto_tree_add_item(raknet_tree, hf_mcpe_general_packet_number, tvb, offset,
                        item_size, ENC_BIG_ENDIAN);
    offset += item_size;

    if (!single_packet) {
        item_size = 3;
        proto_tree_add_item(raknet_tree, hf_mcpe_general_packet_number, tvb,
                            offset, item_size, ENC_BIG_ENDIAN);
        /*offset += item_size;*/
    }
}

/*
 * offset is updated for use by the caller.
 */
static void
mcpe_dissect_detail_payload_0x00(tvbuff_t *tvb, proto_tree *mcpe_tree,
                                gint *offset)
{
    gint item_size;

    item_size = 2;
    proto_tree_add_item(mcpe_tree, hf_mcpe_general_packet_payload_length, tvb,
                        *offset, item_size, ENC_BIG_ENDIAN);
    *offset += item_size;
}

/*
 * offset is updated for use by the caller.
 */
static void
mcpe_dissect_detail_payload_0x40(tvbuff_t *tvb, proto_tree *mcpe_tree,
                                gint *offset)
{
    gint item_size;

    item_size = 2;
    proto_tree_add_item(mcpe_tree, hf_mcpe_general_packet_payload_length, tvb,
                        *offset, item_size, ENC_BIG_ENDIAN);
    *offset += item_size;

    item_size = 3;
    proto_tree_add_item(mcpe_tree, hf_mcpe_general_packet_payload_count, tvb,
                        *offset, item_size, ENC_BIG_ENDIAN);
    *offset += item_size;
}

static void
mcpe_dissect_detail_payload(tvbuff_t *tvb, proto_tree *mcpe_tree, gint offset)
{
    gint payload_encoding;
    gint item_size;

    item_size = 3;
    proto_tree_add_item(mcpe_tree, hf_mcpe_general_packet_number, tvb, offset,
                        item_size, ENC_BIG_ENDIAN);
    offset += item_size;

    payload_encoding = tvb_get_guint8(tvb, offset);

    item_size = 1;
    proto_tree_add_item(mcpe_tree, hf_mcpe_payload_encoding, tvb, offset,
                        item_size, ENC_BIG_ENDIAN);
    offset += item_size;

    switch (payload_encoding) {
    case 0x00:
        mcpe_dissect_detail_payload_0x00(tvb, mcpe_tree, &offset);
        break;
    case 0x40:
    case 0x50:
    case 0x60:
        /*
         * 0x50 and 0x60 contain extra fields before the payload.  These fields
         * are currently unknown, so just use 0x40 for a correct partial
         * dissection.
         */
        mcpe_dissect_detail_payload_0x40(tvb, mcpe_tree, &offset);
        break;
    default:
        break;
    }

    item_size = -1; /* Read to end of buffer */
    proto_tree_add_item(mcpe_tree, hf_mcpe_general_packet_payload, tvb, offset,
                        item_size, ENC_NA);
}

/*
 * Common MCPE packet data
 */
static proto_tree *
mcpe_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint *offset)
{
    const char *packet_desc;
    guint8 packet_id;
    proto_tree *sub_tree;
    proto_item *ti;

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

    /*
     * Take buffer start 0 to end -1 as single mcpe item.
     */
    ti = proto_tree_add_item(tree, proto_mcpe, tvb, 0, -1, ENC_NA);
    sub_tree = proto_item_add_subtree(ti, ett_mcpe);

    packet_id = tvb_get_guint8(tvb, *offset);
    proto_tree_add_item(sub_tree, hf_mcpe_packet_id, tvb, *offset,
                        1, ENC_BIG_ENDIAN);
    *offset += 1;

    switch (packet_id) {
    case 0xA0:
        packet_desc = " (NACK)";
        break;
    case 0xC0:
        packet_desc = " (ACK)";
        break;
    default:
        packet_desc = "";
        break;
    }
    col_add_fstr(pinfo->cinfo, COL_INFO, "Type %#x%s", packet_id, packet_desc);

    proto_item_append_text(ti, ", Packet id %#x", packet_id);

    return sub_tree;
}

static int
mcpe_dissect(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
             void *data _U_)
{
    proto_tree *sub_tree;
    gint offset;

    offset = 0;
    sub_tree = mcpe_info(tvb, pinfo, tree, &offset);
    if (sub_tree != NULL) {
        mcpe_dissect_detail_payload(tvb, sub_tree, offset);
    }
    return tvb_reported_length(tvb);
}

static int
mcpe_dissect_0xA0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  void *data _U_)
{
    proto_tree *sub_tree;
    gint offset;

    offset = 0;
    sub_tree = mcpe_info(tvb, pinfo, tree, &offset);
    if (sub_tree != NULL) {
        mcpe_dissect_detail_0xA0(tvb, sub_tree, offset);
    }
    return tvb_reported_length(tvb);
}

static int
mcpe_dissect_0xC0(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  void *data _U_)
{
    proto_tree *sub_tree;
    gint offset;

    offset = 0;
    sub_tree = mcpe_info(tvb, pinfo, tree, &offset);
    if (sub_tree != NULL) {
        mcpe_dissect_detail_0xA0(tvb, sub_tree, offset); /* 0xA0 */
    }
    return tvb_reported_length(tvb);
}

void
proto_register_mcpe(void)
{
    /*
     * Arrays for detailed dissection, controls output formatting.
     */
    static hf_register_info hf[] = {
        { &hf_mcpe_packet_id,
            { "MCPE Packet ID", "mcpe.type",
                FT_UINT8, BASE_HEX,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_payload_encoding,
            { "MCPE Payload encoding", "mcpe.payload_encoding",
                FT_UINT8, BASE_HEX,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_general_packet_number,
            { "MCPE Packet number", "mcpe.packet_number",
                FT_UINT24, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_general_packet_payload,
            { "MCPE Packet Payload", "mcpe.packet_payload",
                FT_BYTES, BASE_NONE,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_general_packet_payload_length,
            { "MCPE Payload length in *bits*", "mcpe.payload_length_bits",
                FT_UINT16, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_general_packet_payload_count,
            { "MCPE Packet payload count", "mcpe.payload_count",
                FT_UINT24, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        /*
         * Packet ID 0xC0
         */
        { &hf_mcpe_0xC0_unknown,
            { "MCPE Unknown field", "mcpe.unknown",
                FT_UINT8, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_mcpe_0xC0_single_packet,
            { "MCPE single packet (boolean)", "mcpe.single_packet",
                FT_UINT8, BASE_DEC,
                NULL, 0x0,
                NULL, HFILL }
        }
    };

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

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

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

    /* 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 dissector_handle_t raknet_dissector = NULL;
    static guint last_server_port;
    static gboolean init_done = FALSE;

    if (init_done) {
        /*
         * Just delete the dissector before the new add.
         */
        dissector_delete_uint("udp.port", last_server_port, raknet_dissector);
    } else {
        /*
         * First time, create dissector handle, and find raknet dissector.
         */
        dissector_handle_t mcpe_gen_handle;

        init_done = TRUE;
        raknet_dissector = find_dissector("raknet");

        mcpe_gen_handle = create_dissector_handle(mcpe_dissect, proto_mcpe);
        dissector_add_uint("raknet.packet_id", 0x80, mcpe_gen_handle);
        dissector_add_uint("raknet.packet_id", 0x84, mcpe_gen_handle);
        dissector_add_uint("raknet.packet_id", 0x88, mcpe_gen_handle);
        dissector_add_uint("raknet.packet_id", 0x8C, mcpe_gen_handle);
        dissector_add_uint("raknet.packet_id", 0xA0,
                           create_dissector_handle(mcpe_dissect_0xA0,
                                                       proto_mcpe));
        dissector_add_uint("raknet.packet_id", 0xC0,
                           create_dissector_handle(mcpe_dissect_0xC0,
                                                       proto_mcpe));
    }

    last_server_port = mcpe_udp_port_requested;

    /* MCPE is the protocol that carries RakNet packets over UDP */
    dissector_add_uint("udp.port", mcpe_udp_port_requested, raknet_dissector);
}

/*
 * Editor modelines  -  http://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:
 */