2 * Routines for Android Debug Bridge Transport Protocol
4 * Copyright 2014 Michal Labedzki for Tieto Corporation
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See thehf_class
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <epan/packet.h>
28 #include <epan/prefs.h>
29 #include <epan/expert.h>
30 #include <epan/wmem/wmem.h>
31 #include <wiretap/wtap.h>
33 #include "packet-adb_service.h"
34 #include "packet-usb.h"
36 static int proto_adb = -1;
37 static int hf_command = -1;
38 static int hf_argument_0 = -1;
39 static int hf_argument_1 = -1;
40 static int hf_data_length = -1;
41 static int hf_data_crc32 = -1;
42 static int hf_magic = -1;
43 static int hf_local_id = -1;
44 static int hf_remote_id = -1;
45 static int hf_version = -1;
46 static int hf_max_data = -1;
47 static int hf_zero = -1;
48 static int hf_sequence = -1;
49 static int hf_online = -1;
50 static int hf_auth_type = -1;
51 static int hf_data = -1;
52 static int hf_service = -1;
53 static int hf_data_fragment = -1;
54 static int hf_command_in_frame = -1;
55 static int hf_completed_in_frame = -1;
56 static int hf_service_start_in_frame = -1;
57 static int hf_close_local_in_frame = -1;
58 static int hf_close_remote_in_frame = -1;
59 static int hf_connection_info = -1;
61 static gint ett_adb = -1;
62 static gint ett_adb_arg0 = -1;
63 static gint ett_adb_arg1 = -1;
64 static gint ett_adb_crc = -1;
65 static gint ett_adb_magic = -1;
67 static expert_field ei_invalid_magic = EI_INIT;
68 static expert_field ei_invalid_crc = EI_INIT;
70 static dissector_handle_t adb_handle;
71 static dissector_handle_t adb_service_handle;
73 static gint proto_tcp = -1;
74 static gint proto_usb = -1;
76 static wmem_tree_t *command_info = NULL;
77 static wmem_tree_t *service_info = NULL;
79 typedef struct service_data_t {
80 guint32 start_in_frame;
82 guint32 close_local_in_frame;
83 guint32 close_remote_in_frame;
88 const guint8 *service;
91 typedef struct command_data_t {
94 guint32 command_in_frame;
95 guint32 response_in_frame;
103 guint32 completed_in_frame;
104 guint32 reassemble_data_length;
105 guint8 *reassemble_data;
108 static guint32 max_in_frame = G_MAXUINT32;
110 static const value_string command_vals[] = {
111 { 0x434e5953, "Synchronize" },
112 { 0x45534c43, "Close" },
113 { 0x45545257, "Write" },
114 { 0x48545541, "Authenticate" },
115 { 0x4e584e43, "Connect" },
116 { 0x4e45504f, "Open" },
117 { 0x59414b4f, "Okay" },
121 static const value_string magic_vals[] = {
122 { 0xFFFFFFFF ^ 0x434e5953, "Synchronize" },
123 { 0xFFFFFFFF ^ 0x45534c43, "Close" },
124 { 0xFFFFFFFF ^ 0x45545257, "Write" },
125 { 0xFFFFFFFF ^ 0x48545541, "Authenticate" },
126 { 0xFFFFFFFF ^ 0x4e584e43, "Connect" },
127 { 0xFFFFFFFF ^ 0x4e45504f, "Open" },
128 { 0xFFFFFFFF ^ 0x59414b4f, "Okay" },
132 static const value_string auth_type_vals[] = {
135 { 3, "RSA Public Key" },
139 #define A_SYNC 0x434e5953
140 #define A_CLSE 0x45534c43
141 #define A_WRTE 0x45545257
142 #define A_AUTH 0x48545541
143 #define A_CNXN 0x4e584e43
144 #define A_OPEN 0x4e45504f
145 #define A_OKAY 0x59414b4f
147 #define ADB_TCP_PORT 5555
149 void proto_register_adb(void);
150 void proto_reg_handoff_adb(void);
153 save_command(guint32 cmd, guint32 arg0, guint32 arg1, guint32 data_length,
154 guint32 crc32, service_data_t *service_data, gint proto, void *data,
155 packet_info *pinfo, service_data_t **returned_service_data,
156 command_data_t **returned_command_data)
158 wmem_tree_key_t key[6];
159 guint32 interface_id;
161 guint32 device_address;
163 guint32 frame_number;
164 command_data_t *command_data;
165 wmem_tree_t *wmem_tree;
166 gint direction = P2P_DIR_UNKNOWN;
167 usb_conv_info_t *usb_conv_info = (usb_conv_info_t *) data;
169 frame_number = pinfo->fd->num;
171 if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
172 interface_id = pinfo->phdr->interface_id;
176 if (proto == proto_usb) {
177 usb_conv_info = (usb_conv_info_t *) data;
178 DISSECTOR_ASSERT(usb_conv_info);
180 direction = usb_conv_info->direction;
182 bus_id = usb_conv_info->bus_id;
183 device_address = usb_conv_info->device_address;
186 key[0].key = &interface_id;
188 key[1].key = &bus_id;
190 key[2].key = &device_address;
192 key[3].key = &side_id;
194 key[4].key = &frame_number;
198 if (pinfo->destport == ADB_TCP_PORT)
199 direction = P2P_DIR_SENT;
201 direction = P2P_DIR_RECV;
204 key[0].key = &interface_id;
207 if (direction == P2P_DIR_SENT) {
208 key[1].key = &pinfo->srcport;
209 key[2].key = &pinfo->destport;
211 key[1].key = &pinfo->destport;
212 key[2].key = &pinfo->srcport;
215 key[3].key = &side_id;
217 key[4].key = &frame_number;
222 if (direction == P2P_DIR_SENT)
224 side_id = arg1; /* OUT: local id */
226 side_id = arg0; /* OUT: local id */
228 side_id = arg1; /* IN: remote id */
231 service_data = wmem_new(wmem_file_scope(), service_data_t);
233 service_data->start_in_frame = pinfo->fd->num;
234 service_data->close_local_in_frame = max_in_frame;
235 service_data->close_remote_in_frame = max_in_frame;
237 service_data->local_id = arg0;
238 service_data->remote_id = arg1;
240 service_data->service = "unknown";
242 wmem_tree_insert32_array(service_info, key, service_data);
245 command_data = wmem_new(wmem_file_scope(), command_data_t);
247 command_data->command = cmd;
248 command_data->arg0 = arg0;
249 command_data->arg1 = arg1;
251 command_data->command_in_frame = pinfo->fd->num;
252 command_data->response_in_frame = max_in_frame;
254 command_data->crc32 = crc32;
255 command_data->data_length = data_length;
256 if (data_length == 0)
257 command_data->completed_in_frame = pinfo->fd->num;
259 command_data->completed_in_frame = max_in_frame;
260 command_data->reassemble_data_length = 0;
261 command_data->reassemble_data = (guint8 *) wmem_alloc(wmem_file_scope(), command_data->data_length);
264 key[3].key = &frame_number;
267 wmem_tree_insert32_array(command_info, key, command_data);
269 if (direction == P2P_DIR_SENT)
270 if (command_data->command == A_CLSE)
271 side_id = command_data->arg1; /* OUT: local id */
273 side_id = command_data->arg0; /* OUT: local id */
275 side_id = command_data->arg1; /* IN: remote id */
278 key[3].key = &side_id;
282 wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
284 service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
289 if (direction == P2P_DIR_SENT)
290 side_id = command_data->arg0; /* OUT: local id */
292 side_id = command_data->arg1; /* IN: remote id */
294 wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
296 service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
300 if (service_data && service_data->remote_id == 0 && direction == P2P_DIR_RECV) {
301 if (direction == P2P_DIR_SENT) {
302 service_data->remote_id = arg1;
304 service_data->remote_id = arg0;
307 side_id = service_data->remote_id;
310 key[4].key = &frame_number;
314 wmem_tree_insert32_array(service_info, key, service_data);
316 } else if (cmd == A_CLSE) {
318 if (direction == P2P_DIR_RECV && service_data->local_id == arg1)
319 service_data->close_local_in_frame = pinfo->fd->num;
320 else if (direction == P2P_DIR_SENT && service_data->remote_id == arg1)
321 service_data->close_remote_in_frame = pinfo->fd->num;
325 DISSECTOR_ASSERT(returned_service_data && returned_command_data);
326 *returned_service_data = service_data;
327 *returned_command_data = command_data;
331 dissect_adb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
333 proto_item *main_item;
334 proto_tree *main_tree;
335 proto_item *arg0_item;
336 proto_tree *arg0_tree;
337 proto_item *arg1_item;
338 proto_tree *arg1_tree;
339 proto_item *magic_item;
340 proto_item *crc_item;
341 proto_tree *crc_tree = NULL;
342 proto_item *sub_item;
347 guint32 data_length = 0;
349 usb_conv_info_t *usb_conv_info = NULL;
350 wmem_tree_key_t key[5];
351 guint32 interface_id;
353 guint32 device_address;
355 guint32 frame_number;
356 gboolean is_command = TRUE;
357 gboolean is_next_fragment = FALSE;
358 gboolean is_service = FALSE;
360 gint direction = P2P_DIR_UNKNOWN;
361 wmem_tree_t *wmem_tree;
362 command_data_t *command_data = NULL;
363 service_data_t *service_data = NULL;
365 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ADB");
366 col_clear(pinfo->cinfo, COL_INFO);
368 main_item = proto_tree_add_item(tree, proto_adb, tvb, offset, -1, ENC_NA);
369 main_tree = proto_item_add_subtree(main_item, ett_adb);
371 frame_number = pinfo->fd->num;
373 /* XXX: Why? If interface is USB only first try is correct
374 * (and seems strange...), in other cases standard check for
375 * previous protocol is correct */
376 proto = (gint) GPOINTER_TO_INT(wmem_list_frame_data(/*wmem_list_frame_prev*/(wmem_list_tail(pinfo->layers))));
377 if (proto != proto_usb) {
378 proto = (gint) GPOINTER_TO_INT(wmem_list_frame_data(wmem_list_frame_prev(wmem_list_tail(pinfo->layers))));
381 if (proto == proto_usb) {
382 usb_conv_info = (usb_conv_info_t *) data;
383 DISSECTOR_ASSERT(usb_conv_info);
385 direction = usb_conv_info->direction;
386 } else if (proto == proto_tcp) {
387 if (pinfo->destport == ADB_TCP_PORT)
388 direction = P2P_DIR_SENT;
390 direction = P2P_DIR_RECV;
395 if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
396 interface_id = pinfo->phdr->interface_id;
400 if (proto == proto_usb) {
401 bus_id = usb_conv_info->bus_id;
402 device_address = usb_conv_info->device_address;
405 key[0].key = &interface_id;
407 key[1].key = &bus_id;
409 key[2].key = &device_address;
414 key[0].key = &interface_id;
417 if (direction == P2P_DIR_SENT) {
418 key[1].key = &pinfo->srcport;
419 key[2].key = &pinfo->destport;
421 key[1].key = &pinfo->destport;
422 key[2].key = &pinfo->srcport;
428 wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(command_info, key);
430 command_data = (command_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
431 if (command_data && command_data->completed_in_frame >= frame_number &&
432 command_data->command_in_frame <= frame_number) {
434 if (command_data->command_in_frame != frame_number) {
436 is_next_fragment = TRUE;
439 data_length = command_data->data_length;
440 crc32 = command_data->crc32;
442 if (direction == P2P_DIR_SENT)
443 if (command_data->command == A_CLSE)
444 side_id = command_data->arg1; /* OUT: local id */
446 side_id = command_data->arg0; /* OUT: local id */
448 if (command_data->command == A_OKAY) {
449 side_id = command_data->arg1; /* IN: remote id */
451 side_id = command_data->arg1; /* IN: remote id */
454 key[3].key = &side_id;
458 wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
460 service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
461 if (service_data && command_data->command == A_OPEN) {
468 /* Simple heuristics to check if packet is command or data */
469 if ((command_data && command_data->completed_in_frame <= frame_number) || !command_data) {
470 if (tvb_reported_length(tvb) < 24) {
472 } else if (tvb_reported_length(tvb) >= 24) {
473 command = tvb_get_letohl(tvb, offset);
475 if (command != A_SYNC && command != A_CLSE && command != A_WRTE &&
476 command != A_AUTH && command != A_CNXN && command != A_OPEN && command != A_OKAY)
478 else if (command != (0xFFFFFFFF ^ tvb_get_letohl(tvb, offset + 20)))
482 data_length = tvb_get_letohl(tvb, offset + 12);
483 crc32 = tvb_get_letohl(tvb, offset + 16);
485 if (command == A_OPEN) is_service = TRUE;
489 if (service_data && !(command_data->command == A_OPEN && is_next_fragment)) {
490 sub_item = proto_tree_add_string(main_tree, hf_service, tvb, offset, 0, service_data->service);
491 PROTO_ITEM_SET_GENERATED(sub_item);
495 sub_item = proto_tree_add_uint(main_tree, hf_service_start_in_frame, tvb, offset, 0, service_data->start_in_frame);
496 PROTO_ITEM_SET_GENERATED(sub_item);
498 if (service_data->close_local_in_frame < max_in_frame) {
499 sub_item = proto_tree_add_uint(main_tree, hf_close_local_in_frame, tvb, offset, 0, service_data->close_local_in_frame);
500 PROTO_ITEM_SET_GENERATED(sub_item);
503 if (service_data->close_remote_in_frame < max_in_frame) {
504 sub_item = proto_tree_add_uint(main_tree, hf_close_remote_in_frame, tvb, offset, 0, service_data->close_remote_in_frame);
505 PROTO_ITEM_SET_GENERATED(sub_item);
510 proto_tree_add_item(main_tree, hf_command, tvb, offset, 4, ENC_LITTLE_ENDIAN);
511 command = tvb_get_letohl(tvb, offset);
514 col_append_str(pinfo->cinfo, COL_INFO, val_to_str_const(command, command_vals, "Unknown command"));
516 arg0_item = proto_tree_add_item(main_tree, hf_argument_0, tvb, offset, 4, ENC_LITTLE_ENDIAN);
517 arg0_tree = proto_item_add_subtree(arg0_item, ett_adb_arg0);
518 arg0 = tvb_get_letohl(tvb, offset);
521 arg1_item = proto_tree_add_item(main_tree, hf_argument_1, tvb, offset, 4, ENC_LITTLE_ENDIAN);
522 arg1_tree = proto_item_add_subtree(arg1_item, ett_adb_arg1);
523 arg1 = tvb_get_letohl(tvb, offset);
528 proto_tree_add_item(arg0_tree, hf_version, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
529 proto_tree_add_item(arg1_tree, hf_max_data, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
531 col_append_fstr(pinfo->cinfo, COL_INFO, "(version=%u.%u.%u, max_data=%u)", tvb_get_guint8(tvb, offset - 5), tvb_get_guint8(tvb, offset - 6), tvb_get_letohs(tvb, offset - 7), tvb_get_letohl(tvb, offset - 4));
534 proto_tree_add_item(arg0_tree, hf_auth_type, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
535 proto_tree_add_item(arg1_tree, hf_zero, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
537 col_append_fstr(pinfo->cinfo, COL_INFO, "(type=%s, 0)", val_to_str_const(tvb_get_letohl(tvb, offset - 8), auth_type_vals, "Unknown"));
540 proto_tree_add_item(arg0_tree, hf_local_id, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
541 proto_tree_add_item(arg1_tree, hf_zero, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
543 col_append_fstr(pinfo->cinfo, COL_INFO, "(local=%u, 0)", tvb_get_letohl(tvb, offset - 8));
546 proto_tree_add_item(arg0_tree, hf_zero, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
547 proto_tree_add_item(arg1_tree, hf_remote_id, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
549 col_append_fstr(pinfo->cinfo, COL_INFO, "(0, remote=%u)", tvb_get_letohl(tvb, offset - 4));
553 proto_tree_add_item(arg0_tree, hf_local_id, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
554 proto_tree_add_item(arg1_tree, hf_remote_id, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
556 col_append_fstr(pinfo->cinfo, COL_INFO, "(local=%u, remote=%u)", tvb_get_letohl(tvb, offset - 8), tvb_get_letohl(tvb, offset - 4));
559 proto_tree_add_item(arg0_tree, hf_online, tvb, offset - 8, 4, ENC_LITTLE_ENDIAN);
560 proto_tree_add_item(arg1_tree, hf_sequence, tvb, offset - 4, 4, ENC_LITTLE_ENDIAN);
562 col_append_fstr(pinfo->cinfo, COL_INFO, "(online=%s, sequence=%u)", tvb_get_letohl(tvb, offset - 8) ? "Yes": "No", tvb_get_letohl(tvb, offset - 4));
566 proto_tree_add_item(main_tree, hf_data_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
570 col_append_fstr(pinfo->cinfo, COL_INFO, " length=%u ", data_length);
572 crc_item = proto_tree_add_item(main_tree, hf_data_crc32, tvb, offset, 4, ENC_LITTLE_ENDIAN);
573 crc_tree = proto_item_add_subtree(crc_item, ett_adb_crc);
574 crc32 = tvb_get_letohl(tvb, offset);
577 magic_item = proto_tree_add_item(main_tree, hf_magic, tvb, offset, 4, ENC_LITTLE_ENDIAN);
578 if ((tvb_get_letohl(tvb, offset) ^ 0xFFFFFFFF) != command) {
579 proto_tree *expert_tree;
581 expert_tree = proto_item_add_subtree(magic_item, ett_adb_magic);
582 proto_tree_add_expert(expert_tree, pinfo, &ei_invalid_magic, tvb, offset, 4);
585 if (!pinfo->fd->flags.visited)
586 save_command(command, arg0, arg1, data_length, crc32, service_data, proto, data, pinfo, &service_data, &command_data);
590 if (!pinfo->fd->flags.visited && command_data) {
591 if (command_data->command_in_frame != frame_number) {
593 is_next_fragment = TRUE;
596 data_length = command_data->data_length;
597 crc32 = command_data->crc32;
599 if ((command_data->command_in_frame != frame_number && tvb_captured_length(tvb) == data_length) ||
600 (command_data->command_in_frame == frame_number && tvb_captured_length(tvb) == data_length + 24)
602 command_data->reassemble_data_length = command_data->data_length;
603 command_data->completed_in_frame = frame_number;
607 if (is_next_fragment && command_data) {
608 sub_item = proto_tree_add_uint(main_tree, hf_command_in_frame, tvb, offset, 0, command_data->command_in_frame);
609 PROTO_ITEM_SET_GENERATED(sub_item);
611 sub_item = proto_tree_add_uint(main_tree, hf_command, tvb, offset, 0, command_data->command);
612 PROTO_ITEM_SET_GENERATED(sub_item);
614 sub_item = proto_tree_add_uint(main_tree, hf_data_length, tvb, offset, 0, command_data->data_length);
615 PROTO_ITEM_SET_GENERATED(sub_item);
617 crc_item = proto_tree_add_uint(main_tree, hf_data_crc32, tvb, offset, 0, command_data->crc32);
618 crc_tree = proto_item_add_subtree(crc_item, ett_adb_crc);
619 PROTO_ITEM_SET_GENERATED(crc_item);
622 if (command_data && command_data->completed_in_frame != frame_number) {
623 sub_item = proto_tree_add_uint(main_tree, hf_completed_in_frame, tvb, offset, 0, command_data->completed_in_frame);
624 PROTO_ITEM_SET_GENERATED(sub_item);
628 if (tvb_captured_length_remaining(tvb, offset) > 0 && (!is_command || data_length > 0)) {
632 if ((!pinfo->fd->flags.visited && command_data && command_data->reassemble_data_length < command_data->data_length) || data_length > (guint32) tvb_captured_length_remaining(tvb, offset)) { /* need reassemble */
633 if (!pinfo->fd->flags.visited && command_data && command_data->reassemble_data_length < command_data->data_length) {
634 tvb_memcpy(tvb, command_data->reassemble_data + command_data->reassemble_data_length, offset, tvb_captured_length_remaining(tvb, offset));
635 command_data->reassemble_data_length += tvb_captured_length_remaining(tvb, offset);
637 if (command_data->reassemble_data_length >= command_data->data_length)
638 command_data->completed_in_frame = frame_number;
641 proto_tree_add_item(main_tree, hf_data_fragment, tvb, offset, -1, ENC_NA);
642 col_append_str(pinfo->cinfo, COL_INFO, "Data Fragment");
643 offset = tvb_captured_length(tvb);
645 if (service_data && command_data && command_data->reassemble_data_length >= command_data->data_length && frame_number == command_data->completed_in_frame) {
647 adb_service_data_t adb_service_data;
649 next_tvb = tvb_new_child_real_data(tvb, command_data->reassemble_data, command_data->reassemble_data_length, command_data->reassemble_data_length);
650 add_new_data_source(pinfo, next_tvb, "ADB Reassembled Data");
652 adb_service_data.service = service_data->service;
653 adb_service_data.direction = direction;
655 adb_service_data.session_key_length = 3;
656 adb_service_data.session_key = (guint32 *) wmem_alloc(wmem_packet_scope(), adb_service_data.session_key_length * sizeof(guint32));
657 adb_service_data.session_key[0] = interface_id;
659 if (proto == proto_usb) {
660 adb_service_data.session_key[1] = usb_conv_info->bus_id;
661 adb_service_data.session_key[2] = usb_conv_info->device_address;
663 if (direction == P2P_DIR_SENT) {
664 adb_service_data.session_key[1] = pinfo->srcport;
665 adb_service_data.session_key[2] = pinfo->destport;
667 adb_service_data.session_key[1] = pinfo->destport;
668 adb_service_data.session_key[2] = pinfo->srcport;
672 call_dissector_with_data(adb_service_handle, next_tvb, pinfo, tree, &adb_service_data);
674 } else { /* full message */
675 for (i_offset = 0; i_offset < data_length; ++i_offset)
676 crc += tvb_get_guint8(tvb, offset + i_offset);
678 if (crc32 > 0 && crc32 != crc)
679 proto_tree_add_expert(crc_tree, pinfo, &ei_invalid_crc, tvb, offset, -1);
682 proto_tree_add_item(main_tree, hf_service, tvb, offset, -1, ENC_ASCII | ENC_NA);
683 if (!pinfo->fd->flags.visited && service_data) {
684 service_data->service = tvb_get_stringz_enc(wmem_file_scope(), tvb, offset, NULL, ENC_ASCII);
686 col_append_fstr(pinfo->cinfo, COL_INFO, "Service: %s", tvb_get_stringz_enc(wmem_packet_scope(), tvb, offset, NULL, ENC_ASCII));
687 offset = tvb_captured_length(tvb);
688 } else if (command_data && command_data->command == A_CNXN) {
692 info = tvb_get_stringz_enc(wmem_packet_scope(), tvb, offset, &len, ENC_ASCII);
693 col_append_fstr(pinfo->cinfo, COL_INFO, "Connection Info: %s", info);
694 proto_tree_add_item(main_tree, hf_connection_info, tvb, offset, len, ENC_ASCII | ENC_NA);
697 col_append_str(pinfo->cinfo, COL_INFO, "Data");
699 /* Decode service payload */
702 adb_service_data_t adb_service_data;
704 adb_service_data.service = service_data->service;
705 adb_service_data.direction = direction;
707 adb_service_data.session_key_length = 3;
708 adb_service_data.session_key = (guint32 *) wmem_alloc(wmem_packet_scope(), adb_service_data.session_key_length * sizeof(guint32));
709 adb_service_data.session_key[0] = interface_id;
711 if (proto == proto_usb) {
712 adb_service_data.session_key[1] = usb_conv_info->bus_id;
713 adb_service_data.session_key[2] = usb_conv_info->device_address;
715 if (direction == P2P_DIR_SENT) {
716 adb_service_data.session_key[1] = pinfo->srcport;
717 adb_service_data.session_key[2] = pinfo->destport;
719 adb_service_data.session_key[1] = pinfo->destport;
720 adb_service_data.session_key[2] = pinfo->srcport;
724 next_tvb = tvb_new_subset(tvb, offset, tvb_captured_length_remaining(tvb, offset), tvb_captured_length_remaining(tvb, offset));
725 call_dissector_with_data(adb_service_handle, next_tvb, pinfo, tree, &adb_service_data);
728 proto_item *data_item;
731 data_item = proto_tree_add_item(main_tree, hf_data, tvb, offset, data_length, ENC_NA);
732 data_str = tvb_format_text(tvb, offset, data_length);
733 proto_item_append_text(data_item, ": %s", data_str);
734 col_append_fstr(pinfo->cinfo, COL_INFO, " Raw: %s", data_str);
737 offset = tvb_captured_length(tvb);
746 proto_register_adb(void)
749 expert_module_t *expert_module;
751 static hf_register_info hf[] = {
753 { "Command", "adb.command",
754 FT_UINT32, BASE_HEX, VALS(command_vals), 0x00,
758 { "Argument 0", "adb.argument.0",
759 FT_UINT32, BASE_HEX, NULL, 0x00,
763 { "Argument 0", "adb.argument.1",
764 FT_UINT32, BASE_HEX, NULL, 0x00,
768 { "Data Length", "adb.data_length",
769 FT_UINT32, BASE_DEC, NULL, 0x00,
773 { "Data CRC32", "adb.data_crc32",
774 FT_UINT32, BASE_HEX, NULL, 0x00,
778 { "Magic", "adb.magic",
779 FT_UINT32, BASE_HEX, VALS(magic_vals), 0x00,
783 { "Version", "adb.version",
784 FT_UINT32, BASE_HEX, NULL, 0x00,
788 { "Max Data", "adb.max_data",
789 FT_UINT32, BASE_DEC, NULL, 0x00,
793 { "Type", "adb.auth_type",
794 FT_UINT32, BASE_HEX, VALS(auth_type_vals), 0x00,
798 { "Online", "adb.online",
799 FT_BOOLEAN, 32, TFS(&tfs_no_yes), 0x00,
803 { "Sequence", "adb.sequence",
804 FT_UINT32, BASE_DEC, NULL, 0x00,
808 { "Zero", "adb.zero",
809 FT_UINT32, BASE_HEX, NULL, 0x00,
813 { "Local ID", "adb.local_id",
814 FT_UINT32, BASE_DEC, NULL, 0x00,
818 { "Remote ID", "adb.remote_id",
819 FT_UINT32, BASE_DEC, NULL, 0x00,
823 { "Data", "adb.data",
824 FT_NONE, BASE_NONE, NULL, 0x00,
828 { "Service", "adb.service",
829 FT_STRING, BASE_NONE, NULL, 0x00,
833 { "Data Fragment", "adb.data_fragment",
834 FT_NONE, BASE_NONE, NULL, 0x00,
837 { &hf_service_start_in_frame,
838 { "Service Start in Frame", "adb.service_start_in_frame",
839 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
842 { &hf_close_local_in_frame,
843 { "Local Service Close in Frame", "adb.close_local_in_frame",
844 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
847 { &hf_close_remote_in_frame,
848 { "Remote Service Close in Frame", "adb.close_remote_in_frame",
849 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
852 { &hf_command_in_frame,
853 { "Command in Frame", "adb.command_in_frame",
854 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
857 { &hf_completed_in_frame,
858 { "Completed in Frame", "adb.completed_in_frame",
859 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
862 { &hf_connection_info,
863 { "Info", "adb.connection_info",
864 FT_STRINGZ, STR_ASCII, NULL, 0x00,
869 static gint *ett[] = {
877 static ei_register_info ei[] = {
878 { &ei_invalid_magic, { "adb.expert.invalid_magic", PI_PROTOCOL, PI_WARN, "Invalid Magic", EXPFILL }},
879 { &ei_invalid_crc, { "adb.expert.crc_error", PI_PROTOCOL, PI_ERROR, "CRC32 Error", EXPFILL }},
882 command_info = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
883 service_info = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
885 proto_adb = proto_register_protocol("Android Debug Bridge", "ADB", "adb");
886 adb_handle = new_register_dissector("adb", dissect_adb, proto_adb);
888 proto_register_field_array(proto_adb, hf, array_length(hf));
889 proto_register_subtree_array(ett, array_length(ett));
890 expert_module = expert_register_protocol(proto_adb);
891 expert_register_field_array(expert_module, ei, array_length(ei));
893 module = prefs_register_protocol(proto_adb, NULL);
894 prefs_register_static_text_preference(module, "version",
895 "ADB protocol version is compatibile pior to: adb 1.0.31",
896 "Version of protocol supported by this dissector.");
900 proto_reg_handoff_adb(void)
902 adb_service_handle = find_dissector("adb_service");
904 dissector_add_handle("tcp.port", adb_handle);
905 dissector_add_handle("usb.device", adb_handle);
906 dissector_add_handle("usb.product", adb_handle);
907 dissector_add_handle("usb.protocol", adb_handle);
909 proto_tcp = proto_get_id_by_filter_name("tcp");
910 proto_usb = proto_get_id_by_filter_name("usb");
914 * Editor modelines - http://www.wireshark.org/tools/modelines.html
919 * indent-tabs-mode: nil
922 * vi: set shiftwidth=4 tabstop=8 expandtab:
923 * :indentSize=4:tabSize=8:noTabs=true: