Android: Add ADB dissector
[metze/wireshark/wip.git] / epan / dissectors / packet-adb.c
1 /* packet-adb.c
2  * Routines for Android Debug Bridge Transport Protocol
3  *
4  * Copyright 2014 Michal Labedzki for Tieto Corporation
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include "config.h"
26
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>
32
33 #include "packet-adb_service.h"
34 #include "packet-usb.h"
35
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;
60
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;
66
67 static expert_field ei_invalid_magic                                  = EI_INIT;
68 static expert_field ei_invalid_crc                                    = EI_INIT;
69
70 static dissector_handle_t  adb_handle;
71 static dissector_handle_t  adb_service_handle;
72
73 static gint proto_tcp = -1;
74 static gint proto_usb = -1;
75
76 static wmem_tree_t *command_info = NULL;
77 static wmem_tree_t *service_info = NULL;
78
79 typedef struct service_data_t {
80     guint32  start_in_frame;
81
82     guint32  close_local_in_frame;
83     guint32  close_remote_in_frame;
84
85     guint32  local_id;
86     guint32  remote_id;
87
88     const guint8  *service;
89 } service_data_t;
90
91 typedef struct command_data_t {
92     guint32   command;
93
94     guint32   command_in_frame;
95     guint32   response_in_frame;
96
97     guint32   arg0;
98     guint32   arg1;
99
100     guint32   data_length;
101     guint32   crc32;
102
103     guint32   completed_in_frame;
104     guint32   reassemble_data_length;
105     guint8   *reassemble_data;
106 } command_data_t;
107
108 static guint32 max_in_frame = G_MAXUINT32;
109
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" },
118     { 0, NULL }
119 };
120
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" },
129     { 0, NULL }
130 };
131
132 static const value_string auth_type_vals[] = {
133     { 1,  "Token" },
134     { 2,  "Signature" },
135     { 3,  "RSA Public Key" },
136     { 0, NULL }
137 };
138
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
146
147 #define ADB_TCP_PORT  5555
148
149 void proto_register_adb(void);
150 void proto_reg_handoff_adb(void);
151
152 static 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)
157 {
158     wmem_tree_key_t  key[6];
159     guint32          interface_id;
160     guint32          bus_id;
161     guint32          device_address;
162     guint32          side_id;
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;
168
169     frame_number = pinfo->fd->num;
170
171     if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
172         interface_id = pinfo->phdr->interface_id;
173     else
174         interface_id = 0;
175
176     if (proto == proto_usb) {
177         usb_conv_info = (usb_conv_info_t *) data;
178         DISSECTOR_ASSERT(usb_conv_info);
179
180         direction = usb_conv_info->direction;
181
182         bus_id             = usb_conv_info->bus_id;
183         device_address     = usb_conv_info->device_address;
184
185         key[0].length = 1;
186         key[0].key = &interface_id;
187         key[1].length = 1;
188         key[1].key = &bus_id;
189         key[2].length = 1;
190         key[2].key = &device_address;
191         key[3].length = 1;
192         key[3].key = &side_id;
193         key[4].length = 1;
194         key[4].key = &frame_number;
195         key[5].length = 0;
196         key[5].key = NULL;
197     } else { /* tcp */
198         if (pinfo->destport == ADB_TCP_PORT)
199             direction = P2P_DIR_SENT;
200         else
201             direction = P2P_DIR_RECV;
202
203         key[0].length = 1;
204         key[0].key = &interface_id;
205         key[1].length = 1;
206         key[2].length = 1;
207         if (direction == P2P_DIR_SENT) {
208             key[1].key = &pinfo->srcport;
209             key[2].key = &pinfo->destport;
210         } else {
211             key[1].key = &pinfo->destport;
212             key[2].key = &pinfo->srcport;
213         }
214         key[3].length = 1;
215         key[3].key = &side_id;
216         key[4].length = 1;
217         key[4].key = &frame_number;
218         key[5].length = 0;
219         key[5].key = NULL;
220     }
221
222     if (direction == P2P_DIR_SENT)
223         if (cmd == A_CLSE)
224             side_id = arg1; /* OUT: local id */
225         else
226             side_id = arg0; /* OUT: local id */
227     else
228         side_id = arg1; /* IN: remote id */
229
230     if (cmd == A_OPEN) {
231         service_data = wmem_new(wmem_file_scope(), service_data_t);
232
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;
236
237         service_data->local_id = arg0;
238         service_data->remote_id = arg1;
239
240         service_data->service = "unknown";
241
242         wmem_tree_insert32_array(service_info, key, service_data);
243     }
244
245     command_data = wmem_new(wmem_file_scope(), command_data_t);
246
247     command_data->command = cmd;
248     command_data->arg0 = arg0;
249     command_data->arg1 = arg1;
250
251     command_data->command_in_frame = pinfo->fd->num;
252     command_data->response_in_frame = max_in_frame;
253
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;
258     else
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);
262
263     key[3].length = 1;
264     key[3].key = &frame_number;
265     key[4].length = 0;
266     key[4].key = NULL;
267     wmem_tree_insert32_array(command_info, key, command_data);
268
269     if (direction == P2P_DIR_SENT)
270         if (command_data->command == A_CLSE)
271             side_id = command_data->arg1; /* OUT: local id */
272         else
273             side_id = command_data->arg0; /* OUT: local id */
274     else
275         side_id = command_data->arg1; /* IN: remote id */
276
277     key[3].length = 1;
278     key[3].key = &side_id;
279     key[4].length = 0;
280     key[4].key = NULL;
281
282     wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
283     if (wmem_tree) {
284         service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
285     }
286
287     if (cmd == A_OKAY) {
288         if (!service_data) {
289             if (direction == P2P_DIR_SENT)
290                 side_id = command_data->arg0; /* OUT: local id */
291             else
292                 side_id = command_data->arg1; /* IN: remote id */
293
294             wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
295             if (wmem_tree) {
296                 service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
297             }
298         }
299
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;
303             } else {
304                 service_data->remote_id = arg0;
305             }
306
307             side_id = service_data->remote_id;
308
309             key[4].length = 1;
310             key[4].key = &frame_number;
311             key[5].length = 0;
312             key[5].key = NULL;
313
314             wmem_tree_insert32_array(service_info, key, service_data);
315         }
316     } else if (cmd == A_CLSE) {
317         if (service_data) {
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;
322         }
323     }
324
325     DISSECTOR_ASSERT(returned_service_data && returned_command_data);
326     *returned_service_data = service_data;
327     *returned_command_data = command_data;
328 }
329
330 static gint
331 dissect_adb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
332 {
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;
343     gint             offset = 0;
344     guint32          command;
345     guint32          arg0;
346     guint32          arg1;
347     guint32          data_length = 0;
348     guint32          crc32 = 0;
349     usb_conv_info_t *usb_conv_info = NULL;
350     wmem_tree_key_t  key[5];
351     guint32          interface_id;
352     guint32          bus_id;
353     guint32          device_address;
354     guint32          side_id;
355     guint32          frame_number;
356     gboolean         is_command = TRUE;
357     gboolean         is_next_fragment = FALSE;
358     gboolean         is_service = FALSE;
359     gint             proto;
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;
364
365     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ADB");
366     col_clear(pinfo->cinfo, COL_INFO);
367
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);
370
371     frame_number       = pinfo->fd->num;
372
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))));
379     }
380
381     if (proto == proto_usb) {
382         usb_conv_info = (usb_conv_info_t *) data;
383         DISSECTOR_ASSERT(usb_conv_info);
384
385         direction = usb_conv_info->direction;
386     } else if (proto == proto_tcp) {
387         if (pinfo->destport == ADB_TCP_PORT)
388             direction = P2P_DIR_SENT;
389         else
390             direction = P2P_DIR_RECV;
391     } else {
392         return offset;
393     }
394
395     if (pinfo->phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
396         interface_id = pinfo->phdr->interface_id;
397     else
398         interface_id = 0;
399
400     if (proto == proto_usb) {
401         bus_id             = usb_conv_info->bus_id;
402         device_address     = usb_conv_info->device_address;
403
404         key[0].length = 1;
405         key[0].key = &interface_id;
406         key[1].length = 1;
407         key[1].key = &bus_id;
408         key[2].length = 1;
409         key[2].key = &device_address;
410         key[3].length = 0;
411         key[3].key = NULL;
412     } else { /* tcp */
413         key[0].length = 1;
414         key[0].key = &interface_id;
415         key[1].length = 1;
416         key[2].length = 1;
417         if (direction == P2P_DIR_SENT) {
418             key[1].key = &pinfo->srcport;
419             key[2].key = &pinfo->destport;
420         } else {
421             key[1].key = &pinfo->destport;
422             key[2].key = &pinfo->srcport;
423         }
424         key[3].length = 0;
425         key[3].key = NULL;
426     }
427
428     wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(command_info, key);
429     if (wmem_tree) {
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) {
433
434             if (command_data->command_in_frame != frame_number) {
435                 is_command = FALSE;
436                 is_next_fragment = TRUE;
437             }
438
439             data_length = command_data->data_length;
440             crc32 = command_data->crc32;
441
442             if (direction == P2P_DIR_SENT)
443                 if (command_data->command == A_CLSE)
444                     side_id = command_data->arg1; /* OUT: local id */
445                 else
446                     side_id = command_data->arg0; /* OUT: local id */
447             else
448                 if (command_data->command == A_OKAY) {
449                     side_id = command_data->arg1; /* IN: remote id */
450                 } else
451                     side_id = command_data->arg1; /* IN: remote id */
452
453             key[3].length = 1;
454             key[3].key = &side_id;
455             key[4].length = 0;
456             key[4].key = NULL;
457
458             wmem_tree = (wmem_tree_t *) wmem_tree_lookup32_array(service_info, key);
459             if (wmem_tree) {
460                 service_data = (service_data_t *) wmem_tree_lookup32_le(wmem_tree, frame_number);
461                 if (service_data && command_data->command == A_OPEN) {
462                     is_service = TRUE;
463                 }
464             }
465         }
466     }
467
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) {
471             is_command = FALSE;
472         } else if (tvb_reported_length(tvb) >= 24) {
473             command = tvb_get_letohl(tvb, offset);
474
475             if (command != A_SYNC && command != A_CLSE && command != A_WRTE &&
476                     command != A_AUTH && command != A_CNXN && command != A_OPEN && command != A_OKAY)
477                 is_command = FALSE;
478             else if (command != (0xFFFFFFFF ^ tvb_get_letohl(tvb, offset + 20)))
479                 is_command = FALSE;
480
481             if (is_command) {
482                 data_length = tvb_get_letohl(tvb, offset + 12);
483                 crc32 = tvb_get_letohl(tvb, offset + 16);
484             }
485             if (command == A_OPEN) is_service = TRUE;
486         }
487     }
488
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);
492     }
493
494     if (service_data) {
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);
497
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);
501         }
502
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);
506         }
507     }
508
509     if (is_command) {
510         proto_tree_add_item(main_tree, hf_command, tvb, offset, 4, ENC_LITTLE_ENDIAN);
511         command = tvb_get_letohl(tvb, offset);
512         offset += 4;
513
514         col_append_str(pinfo->cinfo, COL_INFO, val_to_str_const(command, command_vals, "Unknown command"));
515
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);
519         offset += 4;
520
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);
524         offset += 4;
525
526         switch (command) {
527         case A_CNXN:
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);
530
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));
532             break;
533         case A_AUTH:
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);
536
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"));
538             break;
539         case A_OPEN:
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);
542
543             col_append_fstr(pinfo->cinfo, COL_INFO, "(local=%u, 0)", tvb_get_letohl(tvb, offset - 8));
544             break;
545         case A_WRTE:
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);
548
549             col_append_fstr(pinfo->cinfo, COL_INFO, "(0, remote=%u)", tvb_get_letohl(tvb, offset - 4));
550             break;
551         case A_CLSE:
552         case A_OKAY:
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);
555
556             col_append_fstr(pinfo->cinfo, COL_INFO, "(local=%u, remote=%u)", tvb_get_letohl(tvb, offset - 8), tvb_get_letohl(tvb, offset - 4));
557             break;
558         case A_SYNC:
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);
561
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));
563             break;
564         }
565
566         proto_tree_add_item(main_tree, hf_data_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
567         offset += 4;
568
569         if (data_length > 0)
570             col_append_fstr(pinfo->cinfo, COL_INFO, " length=%u ", data_length);
571
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);
575         offset += 4;
576
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;
580
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);
583         }
584
585         if (!pinfo->fd->flags.visited)
586             save_command(command, arg0, arg1, data_length, crc32, service_data, proto, data, pinfo, &service_data, &command_data);
587         offset += 4;
588     }
589
590     if (!pinfo->fd->flags.visited && command_data) {
591             if (command_data->command_in_frame != frame_number) {
592                 is_command = FALSE;
593                 is_next_fragment = TRUE;
594             }
595
596             data_length = command_data->data_length;
597             crc32 = command_data->crc32;
598
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)
601             ) {
602                 command_data->reassemble_data_length = command_data->data_length;
603                 command_data->completed_in_frame = frame_number;
604             }
605     }
606
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);
610
611         sub_item = proto_tree_add_uint(main_tree, hf_command, tvb, offset, 0, command_data->command);
612         PROTO_ITEM_SET_GENERATED(sub_item);
613
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);
616
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);
620     }
621
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);
625     }
626
627
628     if (tvb_captured_length_remaining(tvb, offset) > 0 && (!is_command || data_length > 0)) {
629         guint32 crc = 0;
630         guint32 i_offset;
631
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);
636
637                 if (command_data->reassemble_data_length >= command_data->data_length)
638                     command_data->completed_in_frame = frame_number;
639             }
640
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);
644
645             if (service_data && command_data && command_data->reassemble_data_length >= command_data->data_length && frame_number == command_data->completed_in_frame) {
646                 tvbuff_t            *next_tvb;
647                 adb_service_data_t   adb_service_data;
648
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");
651
652                 adb_service_data.service = service_data->service;
653                 adb_service_data.direction = direction;
654
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;
658
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;
662                 } else { /* tcp */
663                     if (direction == P2P_DIR_SENT) {
664                         adb_service_data.session_key[1] = pinfo->srcport;
665                         adb_service_data.session_key[2] = pinfo->destport;
666                     } else {
667                         adb_service_data.session_key[1] = pinfo->destport;
668                         adb_service_data.session_key[2] = pinfo->srcport;
669                     }
670                 }
671
672                 call_dissector_with_data(adb_service_handle, next_tvb, pinfo, tree, &adb_service_data);
673             }
674         } else { /* full message */
675             for (i_offset = 0; i_offset < data_length; ++i_offset)
676                 crc += tvb_get_guint8(tvb, offset + i_offset);
677
678             if (crc32 > 0 && crc32 != crc)
679                 proto_tree_add_expert(crc_tree, pinfo, &ei_invalid_crc, tvb, offset, -1);
680
681             if (is_service) {
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);
685                 }
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) {
689                     gchar       *info;
690                     gint         len;
691
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);
695                 offset += len;
696             } else {
697                 col_append_str(pinfo->cinfo, COL_INFO, "Data");
698
699                 /* Decode service payload */
700                 if (service_data) {
701                     tvbuff_t           *next_tvb;
702                     adb_service_data_t  adb_service_data;
703
704                     adb_service_data.service = service_data->service;
705                     adb_service_data.direction = direction;
706
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;
710
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;
714                     } else { /* tcp */
715                         if (direction == P2P_DIR_SENT) {
716                             adb_service_data.session_key[1] = pinfo->srcport;
717                             adb_service_data.session_key[2] = pinfo->destport;
718                         } else {
719                             adb_service_data.session_key[1] = pinfo->destport;
720                             adb_service_data.session_key[2] = pinfo->srcport;
721                         }
722                     }
723
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);
726
727                 } else {
728                     proto_item  *data_item;
729                     gchar       *data_str;
730
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);
735                 }
736
737                 offset = tvb_captured_length(tvb);
738             }
739         }
740     }
741
742     return offset;
743 }
744
745 void
746 proto_register_adb(void)
747 {
748     module_t         *module;
749     expert_module_t  *expert_module;
750
751     static hf_register_info hf[] = {
752         { &hf_command,
753             { "Command",                         "adb.command",
754             FT_UINT32, BASE_HEX, VALS(command_vals), 0x00,
755             NULL, HFILL }
756         },
757         { &hf_argument_0,
758             { "Argument 0",                      "adb.argument.0",
759             FT_UINT32, BASE_HEX, NULL, 0x00,
760             NULL, HFILL }
761         },
762         { &hf_argument_1,
763             { "Argument 0",                      "adb.argument.1",
764             FT_UINT32, BASE_HEX, NULL, 0x00,
765             NULL, HFILL }
766         },
767         { &hf_data_length,
768             { "Data Length",                      "adb.data_length",
769             FT_UINT32, BASE_DEC, NULL, 0x00,
770             NULL, HFILL }
771         },
772         { &hf_data_crc32,
773             { "Data CRC32",                      "adb.data_crc32",
774             FT_UINT32, BASE_HEX, NULL, 0x00,
775             NULL, HFILL }
776         },
777         { &hf_magic,
778             { "Magic",                           "adb.magic",
779             FT_UINT32, BASE_HEX, VALS(magic_vals), 0x00,
780             NULL, HFILL }
781         },
782         { &hf_version,
783             { "Version",                         "adb.version",
784             FT_UINT32, BASE_HEX, NULL, 0x00,
785             NULL, HFILL }
786         },
787         { &hf_max_data,
788             { "Max Data",                        "adb.max_data",
789             FT_UINT32, BASE_DEC, NULL, 0x00,
790             NULL, HFILL }
791         },
792         { &hf_auth_type,
793             { "Type",                            "adb.auth_type",
794             FT_UINT32, BASE_HEX, VALS(auth_type_vals), 0x00,
795             NULL, HFILL }
796         },
797         { &hf_online,
798             { "Online",                          "adb.online",
799             FT_BOOLEAN, 32, TFS(&tfs_no_yes), 0x00,
800             NULL, HFILL }
801         },
802         { &hf_sequence,
803             { "Sequence",                        "adb.sequence",
804             FT_UINT32, BASE_DEC, NULL, 0x00,
805             NULL, HFILL }
806         },
807         { &hf_zero,
808             { "Zero",                            "adb.zero",
809             FT_UINT32, BASE_HEX, NULL, 0x00,
810             NULL, HFILL }
811         },
812         { &hf_local_id,
813             { "Local ID",                        "adb.local_id",
814             FT_UINT32, BASE_DEC, NULL, 0x00,
815             NULL, HFILL }
816         },
817         { &hf_remote_id,
818             { "Remote ID",                       "adb.remote_id",
819             FT_UINT32, BASE_DEC, NULL, 0x00,
820             NULL, HFILL }
821         },
822         { &hf_data,
823             { "Data",                            "adb.data",
824             FT_NONE, BASE_NONE, NULL, 0x00,
825             NULL, HFILL }
826         },
827         { &hf_service,
828             { "Service",                         "adb.service",
829             FT_STRING, BASE_NONE, NULL, 0x00,
830             NULL, HFILL }
831         },
832         { &hf_data_fragment,
833             { "Data Fragment",                   "adb.data_fragment",
834             FT_NONE, BASE_NONE, NULL, 0x00,
835             NULL, HFILL }
836         },
837         { &hf_service_start_in_frame,
838             { "Service Start in Frame",          "adb.service_start_in_frame",
839             FT_FRAMENUM, BASE_NONE, NULL, 0x00,
840             NULL, HFILL }
841         },
842         { &hf_close_local_in_frame,
843             { "Local Service Close in Frame",    "adb.close_local_in_frame",
844             FT_FRAMENUM, BASE_NONE, NULL, 0x00,
845             NULL, HFILL }
846         },
847         { &hf_close_remote_in_frame,
848             { "Remote Service Close in Frame",   "adb.close_remote_in_frame",
849             FT_FRAMENUM, BASE_NONE, NULL, 0x00,
850             NULL, HFILL }
851         },
852         { &hf_command_in_frame,
853             { "Command in Frame",                "adb.command_in_frame",
854             FT_FRAMENUM, BASE_NONE, NULL, 0x00,
855             NULL, HFILL }
856         },
857         { &hf_completed_in_frame,
858             { "Completed in Frame",              "adb.completed_in_frame",
859             FT_FRAMENUM, BASE_NONE, NULL, 0x00,
860             NULL, HFILL }
861         },
862         { &hf_connection_info,
863             { "Info",                            "adb.connection_info",
864             FT_STRINGZ, STR_ASCII, NULL, 0x00,
865             NULL, HFILL }
866         }
867     };
868
869     static gint *ett[] = {
870         &ett_adb,
871         &ett_adb_arg0,
872         &ett_adb_arg1,
873         &ett_adb_crc,
874         &ett_adb_magic
875     };
876
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 }},
880     };
881
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());
884
885     proto_adb = proto_register_protocol("Android Debug Bridge", "ADB", "adb");
886     adb_handle = new_register_dissector("adb", dissect_adb, proto_adb);
887
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));
892
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.");
897 }
898
899 void
900 proto_reg_handoff_adb(void)
901 {
902     adb_service_handle = find_dissector("adb_service");
903
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);
908
909     proto_tcp = proto_get_id_by_filter_name("tcp");
910     proto_usb = proto_get_id_by_filter_name("usb");
911 }
912
913 /*
914  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
915  *
916  * Local variables:
917  * c-basic-offset: 4
918  * tab-width: 8
919  * indent-tabs-mode: nil
920  * End:
921  *
922  * vi: set shiftwidth=4 tabstop=8 expandtab:
923  * :indentSize=4:tabSize=8:noTabs=true:
924  */