1 /* packet-adb_service.c
2  * Routines for Android Debug Bridge Services
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  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/expert.h>
18 
19 #include "packet-adb_service.h"
20 
21 static int proto_adb_service                                               = -1;
22 
23 static int hf_service                                                      = -1;
24 static int hf_fragment                                                     = -1;
25 static int hf_data                                                         = -1;
26 static int hf_hex_ascii_length                                             = -1;
27 static int hf_length                                                       = -1;
28 static int hf_version                                                      = -1;
29 static int hf_hex_ascii_version                                            = -1;
30 static int hf_framebuffer_version                                          = -1;
31 static int hf_framebuffer_depth                                            = -1;
32 static int hf_framebuffer_size                                             = -1;
33 static int hf_framebuffer_width                                            = -1;
34 static int hf_framebuffer_height                                           = -1;
35 static int hf_framebuffer_red_offset                                       = -1;
36 static int hf_framebuffer_red_length                                       = -1;
37 static int hf_framebuffer_blue_offset                                      = -1;
38 static int hf_framebuffer_blue_length                                      = -1;
39 static int hf_framebuffer_green_offset                                     = -1;
40 static int hf_framebuffer_green_length                                     = -1;
41 static int hf_framebuffer_alpha_offset                                     = -1;
42 static int hf_framebuffer_alpha_length                                     = -1;
43 static int hf_framebuffer_pixel                                            = -1;
44 static int hf_framebuffer_red_5                                            = -1;
45 static int hf_framebuffer_green_6                                          = -1;
46 static int hf_framebuffer_blue_5                                           = -1;
47 static int hf_framebuffer_red                                              = -1;
48 static int hf_framebuffer_green                                            = -1;
49 static int hf_framebuffer_blue                                             = -1;
50 static int hf_framebuffer_alpha                                            = -1;
51 static int hf_framebuffer_unused                                           = -1;
52 static int hf_devices                                                      = -1;
53 static int hf_stdin                                                        = -1;
54 static int hf_stdout                                                       = -1;
55 static int hf_pids                                                         = -1;
56 static int hf_result                                                       = -1;
57 
58 static expert_field ei_incomplete_message                             = EI_INIT;
59 
60 static gint ett_adb_service                                                = -1;
61 static gint ett_length                                                     = -1;
62 static gint ett_version                                                    = -1;
63 static gint ett_pixel                                                      = -1;
64 static gint ett_data                                                       = -1;
65 
66 static dissector_handle_t  adb_service_handle;
67 static dissector_handle_t  logcat_handle;
68 
69 static gboolean pref_dissect_more_detail_framebuffer = FALSE;
70 
71 static wmem_tree_t *fragments = NULL;
72 static wmem_tree_t *framebuffer_infos = NULL;
73 static wmem_tree_t *continuation_infos = NULL;
74 
75 typedef struct _framebuffer_data_t {
76     guint32 data_in;
77     guint32 current_size;
78     guint32 completed_in_frame;
79 
80     guint32 size;
81     guint32 red_offset;
82     guint32 red_length;
83     guint32 green_offset;
84     guint32 green_length;
85     guint32 blue_offset;
86     guint32 blue_length;
87     guint32 alpha_offset;
88     guint32 alpha_length;
89 } framebuffer_data_t;
90 
91 typedef struct _fragment_t {
92     gint64    reassembled_in_frame;
93     gint      length;
94     guint8   *data;
95 } fragment_t;
96 
97 typedef struct _continuation_data_t {
98     guint32   length_in_frame;
99     guint32   completed_in_frame;
100     gint      length;
101 } continuation_data_t;
102 
103 void proto_register_adb_service(void);
104 void proto_reg_handoff_adb_service(void);
105 
106 gint
dissect_ascii_uint32(proto_tree * tree,gint hf_hex_ascii,gint ett_hex_ascii,gint hf_value,tvbuff_t * tvb,gint offset,guint32 * value)107 dissect_ascii_uint32(proto_tree *tree, gint hf_hex_ascii, gint ett_hex_ascii,
108         gint hf_value, tvbuff_t *tvb, gint offset, guint32 *value)
109 {
110     proto_item  *sub_item;
111     proto_tree  *sub_tree;
112     gchar        hex_ascii[5];
113 
114     DISSECTOR_ASSERT(value);
115 
116     tvb_memcpy(tvb, hex_ascii, offset, 4);
117     hex_ascii[4]='\0';
118 
119     sub_item = proto_tree_add_item(tree, hf_hex_ascii, tvb, offset, 4, ENC_NA | ENC_ASCII);
120     sub_tree = proto_item_add_subtree(sub_item, ett_hex_ascii);
121 
122     *value = (guint32) g_ascii_strtoull(hex_ascii, NULL, 16);
123 
124     proto_tree_add_uint(sub_tree, hf_value, tvb, offset, 4, *value);
125     offset += 4;
126 
127     return offset;
128 }
129 
130 static gint
dissect_adb_service(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)131 dissect_adb_service(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
132 {
133     proto_item          *main_item;
134     proto_tree          *main_tree;
135     proto_item          *sub_item;
136     proto_tree          *sub_tree;
137     gint                 offset = 0;
138     adb_service_data_t  *adb_service_data = (adb_service_data_t *) data;
139     const gchar         *service;
140     wmem_tree_key_t      key[5];
141     wmem_tree_t         *subtree;
142     guint32              i_key;
143 
144     main_item = proto_tree_add_item(tree, proto_adb_service, tvb, offset, -1, ENC_NA);
145     main_tree = proto_item_add_subtree(main_item, ett_adb_service);
146 
147     DISSECTOR_ASSERT(adb_service_data);
148 
149     service = adb_service_data->service;
150 
151     sub_item = proto_tree_add_string(main_tree, hf_service, tvb, offset, 0, service);
152     proto_item_set_generated(sub_item);
153 
154         if (g_strcmp0(service, "host:version") == 0) {
155             guint32               version;
156             guint32               data_length;
157             continuation_data_t  *continuation_data;
158 
159             DISSECTOR_ASSERT_HINT(adb_service_data->session_key_length + 1 <= sizeof(key) / sizeof(key[0]), "Tree session key is too small");
160             for (i_key = 0; i_key < adb_service_data->session_key_length; i_key += 1) {
161                 key[i_key].length = 1;
162                 key[i_key].key = &adb_service_data->session_key[i_key];
163             }
164             key[i_key].length = 0;
165             key[i_key].key = NULL;
166 
167             subtree = (wmem_tree_t *) wmem_tree_lookup32_array(continuation_infos, key);
168             continuation_data = (subtree) ? (continuation_data_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
169             if (continuation_data && continuation_data->completed_in_frame < pinfo->num)
170                 continuation_data = NULL;
171 
172             if (!continuation_data || (continuation_data && continuation_data->length_in_frame == pinfo->num))
173                 offset = dissect_ascii_uint32(main_tree, hf_hex_ascii_length, ett_length, hf_length, tvb, offset, &data_length);
174 
175             if (!pinfo->fd->visited && !continuation_data && tvb_reported_length_remaining(tvb, offset) < 4) {
176                 key[i_key].length = 1;
177                 key[i_key++].key = &pinfo->num;
178                 key[i_key].length = 0;
179                 key[i_key].key = NULL;
180 
181                 continuation_data = wmem_new(wmem_file_scope(), continuation_data_t);
182                 continuation_data->length_in_frame = pinfo->num;
183                 continuation_data->completed_in_frame = G_MAXUINT32;
184                 continuation_data->length = data_length;
185 
186                 wmem_tree_insert32_array(continuation_infos, key, continuation_data);
187                 continuation_data = NULL;
188             }
189 
190             if (tvb_reported_length_remaining(tvb, offset) >= 4 ||
191                         (continuation_data && continuation_data->completed_in_frame == pinfo->num)) {
192                 if (!pinfo->fd->visited && continuation_data) {
193                     continuation_data->completed_in_frame = pinfo->num;
194                 }
195                 offset = dissect_ascii_uint32(main_tree, hf_hex_ascii_version, ett_version, hf_version, tvb, offset, &version);
196 
197                 col_append_fstr(pinfo->cinfo, COL_INFO, " Version=%u", version);
198             }
199 
200         } else if (g_strcmp0(service, "host:devices") == 0 ||
201                 g_strcmp0(service, "host:devices-l") == 0 ||
202                 g_strcmp0(service, "host:track-devices") == 0) {
203             guint32  data_length;
204 
205             offset = dissect_ascii_uint32(main_tree, hf_hex_ascii_length, ett_length, hf_length, tvb, offset, &data_length);
206 
207             sub_item = proto_tree_add_item(main_tree, hf_devices, tvb, offset, -1, ENC_NA | ENC_ASCII);
208             if ((gint64) data_length < tvb_reported_length_remaining(tvb, offset)) {
209                 expert_add_info(pinfo, sub_item, &ei_incomplete_message);
210             }
211         } else if (g_strcmp0(service, "host:get-state") == 0 ||
212                 g_strcmp0(service, "host:get-serialno") == 0 ||
213                 g_strcmp0(service, "host:get-devpath") == 0 ||
214                 g_str_has_prefix(service, "connect:") ||
215                 g_str_has_prefix(service, "disconnect:")) {
216             guint32  data_length;
217 
218             offset = dissect_ascii_uint32(main_tree, hf_hex_ascii_length, ett_length, hf_length, tvb, offset, &data_length);
219 
220             sub_item = proto_tree_add_item(main_tree, hf_result, tvb, offset, -1, ENC_NA | ENC_ASCII);
221             if ((gint64) data_length < tvb_reported_length_remaining(tvb, offset)) {
222                 expert_add_info(pinfo, sub_item, &ei_incomplete_message);
223             }
224         } else if (g_str_has_prefix(service, "framebuffer:")) {
225             framebuffer_data_t  *framebuffer_data = NULL;
226 
227             DISSECTOR_ASSERT_HINT(adb_service_data->session_key_length + 1 <= sizeof(key) / sizeof(key[0]), "Tree session key is too small");
228             for (i_key = 0; i_key < adb_service_data->session_key_length; i_key += 1) {
229                 key[i_key].length = 1;
230                 key[i_key].key = &adb_service_data->session_key[i_key];
231             }
232             key[i_key].length = 0;
233             key[i_key].key = NULL;
234 
235             subtree = (wmem_tree_t *) wmem_tree_lookup32_array(framebuffer_infos, key);
236             framebuffer_data = (subtree) ? (framebuffer_data_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
237             if (framebuffer_data && framebuffer_data->completed_in_frame < pinfo->num)
238                 framebuffer_data = NULL;
239 
240             if (!pinfo->fd->visited && !framebuffer_data) {
241                 key[i_key].length = 1;
242                 key[i_key++].key = &pinfo->num;
243                 key[i_key].length = 0;
244                 key[i_key].key = NULL;
245 
246                 framebuffer_data = wmem_new(wmem_file_scope(), framebuffer_data_t);
247                 framebuffer_data->data_in      = pinfo->num;
248                 framebuffer_data->current_size = 0;
249                 framebuffer_data->completed_in_frame = G_MAXUINT32;
250                 framebuffer_data->size         = tvb_get_letohl(tvb, offset + 4 * 2);
251                 framebuffer_data->red_offset   = tvb_get_letohl(tvb, offset + 4 * 5);
252                 framebuffer_data->red_length   = tvb_get_letohl(tvb, offset + 4 * 6);
253                 framebuffer_data->green_offset = tvb_get_letohl(tvb, offset + 4 * 7);
254                 framebuffer_data->green_length = tvb_get_letohl(tvb, offset + 4 * 8);
255                 framebuffer_data->blue_offset  = tvb_get_letohl(tvb, offset + 4 * 9);
256                 framebuffer_data->blue_length  = tvb_get_letohl(tvb, offset + 4 * 10);
257                 framebuffer_data->alpha_offset = tvb_get_letohl(tvb, offset + 4 * 11);
258                 framebuffer_data->alpha_length = tvb_get_letohl(tvb, offset + 4 * 12);
259 
260                 wmem_tree_insert32_array(framebuffer_infos, key, framebuffer_data);
261             }
262 
263             if (framebuffer_data && framebuffer_data->data_in == pinfo->num) {
264                 proto_tree_add_item(main_tree, hf_framebuffer_version, tvb, offset, 4, ENC_LITTLE_ENDIAN);
265                 offset += 4;
266 
267                 proto_tree_add_item(main_tree, hf_framebuffer_depth, tvb, offset, 4, ENC_LITTLE_ENDIAN);
268                 offset += 4;
269 
270                 proto_tree_add_item(main_tree, hf_framebuffer_size, tvb, offset, 4, ENC_LITTLE_ENDIAN);
271                 offset += 4;
272 
273                 proto_tree_add_item(main_tree, hf_framebuffer_width, tvb, offset, 4, ENC_LITTLE_ENDIAN);
274                 offset += 4;
275 
276                 proto_tree_add_item(main_tree, hf_framebuffer_height, tvb, offset, 4, ENC_LITTLE_ENDIAN);
277                 offset += 4;
278 
279                 proto_tree_add_item(main_tree, hf_framebuffer_red_offset, tvb, offset, 4, ENC_LITTLE_ENDIAN);
280                 offset += 4;
281 
282                 proto_tree_add_item(main_tree, hf_framebuffer_red_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
283                 offset += 4;
284 
285                 proto_tree_add_item(main_tree, hf_framebuffer_blue_offset, tvb, offset, 4, ENC_LITTLE_ENDIAN);
286                 offset += 4;
287 
288                 proto_tree_add_item(main_tree, hf_framebuffer_blue_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
289                 offset += 4;
290 
291                 proto_tree_add_item(main_tree, hf_framebuffer_green_offset, tvb, offset, 4, ENC_LITTLE_ENDIAN);
292                 offset += 4;
293 
294                 proto_tree_add_item(main_tree, hf_framebuffer_green_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
295                 offset += 4;
296 
297                 proto_tree_add_item(main_tree, hf_framebuffer_alpha_offset, tvb, offset, 4, ENC_LITTLE_ENDIAN);
298                 offset += 4;
299 
300                 proto_tree_add_item(main_tree, hf_framebuffer_alpha_length, tvb, offset, 4, ENC_LITTLE_ENDIAN);
301                 offset += 4;
302             }
303 
304             if (tvb_reported_length_remaining(tvb, offset) > 0) {
305                 sub_item = proto_tree_add_item(main_tree, hf_data, tvb, offset, -1, ENC_NA);
306                 sub_tree = proto_item_add_subtree(sub_item, ett_data);
307 
308                 if (!pinfo->fd->visited && framebuffer_data) {
309                     framebuffer_data->current_size += tvb_captured_length_remaining(tvb, offset);
310                     if (framebuffer_data->current_size >= framebuffer_data->size)
311                         framebuffer_data->completed_in_frame = pinfo->num;
312                 }
313 
314                 if (pref_dissect_more_detail_framebuffer) {
315                     proto_item  *pixel_item;
316                     proto_tree  *pixel_tree;
317 
318                     if (framebuffer_data &&
319                         framebuffer_data->red_length == 5 &&
320                         framebuffer_data->green_length == 6 &&
321                         framebuffer_data->blue_length == 5 &&
322                         framebuffer_data->red_offset == 11 &&
323                         framebuffer_data->green_offset == 5 &&
324                         framebuffer_data->blue_offset == 0) {
325                         while (tvb_reported_length_remaining(tvb, offset) > 0) {
326                             if (tvb_reported_length_remaining(tvb, offset) < 2) {
327                                 proto_tree_add_item(main_tree, hf_fragment, tvb, offset, -1, ENC_NA);
328                                 offset += 1;
329                             }
330 
331                             pixel_item = proto_tree_add_item(sub_tree, hf_framebuffer_pixel, tvb, offset, 2, ENC_NA);
332                             pixel_tree = proto_item_add_subtree(pixel_item, ett_pixel);
333 
334                             proto_tree_add_item(pixel_tree, hf_framebuffer_blue_5, tvb, offset, 2, ENC_LITTLE_ENDIAN);
335                             proto_tree_add_item(pixel_tree, hf_framebuffer_green_6, tvb, offset, 2, ENC_LITTLE_ENDIAN);
336                             proto_tree_add_item(pixel_tree, hf_framebuffer_red_5, tvb, offset, 2, ENC_LITTLE_ENDIAN);
337                             offset += 2;
338                         }
339                     } else if (framebuffer_data &&
340                             framebuffer_data->red_length == 8 &&
341                             framebuffer_data->green_length == 8 &&
342                             framebuffer_data->blue_length == 8 &&
343                             (framebuffer_data->alpha_length == 0 ||
344                             framebuffer_data->alpha_length == 8)) {
345                         while (tvb_reported_length_remaining(tvb, offset) > 0) {
346                             if (tvb_reported_length_remaining(tvb, offset) < 3 || (tvb_reported_length_remaining(tvb, offset) < 4 && framebuffer_data->alpha_offset > 0)) {
347                                 proto_tree_add_item(main_tree, hf_fragment, tvb, offset, -1, ENC_NA);
348                                 offset = tvb_captured_length(tvb);
349                                 break;
350                             }
351 
352                             pixel_item = proto_tree_add_item(sub_tree, hf_framebuffer_pixel, tvb, offset, 3, ENC_NA);
353                             pixel_tree = proto_item_add_subtree(pixel_item, ett_pixel);
354 
355                             proto_tree_add_item(pixel_tree, hf_framebuffer_red, tvb, offset + framebuffer_data->red_offset / 8, 1, ENC_LITTLE_ENDIAN);
356                             proto_tree_add_item(pixel_tree, hf_framebuffer_green, tvb, offset + framebuffer_data->green_offset / 8, 1, ENC_LITTLE_ENDIAN);
357                             proto_tree_add_item(pixel_tree, hf_framebuffer_blue, tvb, offset + framebuffer_data->blue_offset / 8, 1, ENC_LITTLE_ENDIAN);
358 
359                             if (framebuffer_data->alpha_offset > 0) {
360                                 if (framebuffer_data->alpha_length == 0)
361                                     proto_tree_add_item(pixel_tree, hf_framebuffer_unused, tvb, offset + framebuffer_data->alpha_offset / 8, 1, ENC_LITTLE_ENDIAN);
362                                 else
363                                     proto_tree_add_item(pixel_tree, hf_framebuffer_alpha, tvb, offset + framebuffer_data->alpha_offset / 8, 1, ENC_LITTLE_ENDIAN);
364                                 offset += 1;
365                                 proto_item_set_len(pixel_item, 4);
366                             }
367                             offset += 3;
368                         }
369                     } else {
370                         offset = tvb_captured_length(tvb);
371                     }
372                 } else {
373                     offset = tvb_captured_length(tvb);
374                 }
375             }
376         } else if (g_strcmp0(service, "track-jdwp") == 0) {
377             guint32  data_length;
378 
379             offset = dissect_ascii_uint32(main_tree, hf_hex_ascii_length, ett_length, hf_length, tvb, offset, &data_length);
380 
381             if (tvb_reported_length_remaining(tvb, offset) > 0) {
382                 sub_item = proto_tree_add_item(main_tree, hf_pids, tvb, offset, -1, ENC_NA | ENC_ASCII);
383                 if ((gint64) data_length < tvb_reported_length_remaining(tvb, offset)) {
384                     expert_add_info(pinfo, sub_item, &ei_incomplete_message);
385                 }
386             }
387             offset = tvb_captured_length(tvb);
388         } else if ((g_strcmp0(service, "shell:export ANDROID_LOG_TAGS=\"\" ; exec logcat -B") == 0) ||
389                 (g_strcmp0(service, "shell:logcat -B") == 0)) {
390             tvbuff_t    *next_tvb;
391             tvbuff_t    *new_tvb;
392             guint8      *buffer = NULL;
393             gint         size = 0;
394             gint         i_offset = offset;
395             gint         old_offset;
396             gint         i_char = 0;
397             guint8       c1;
398             guint8       c2 = '\0';
399             guint16      payload_length;
400             guint16      try_header_size;
401             gint         logcat_length = 0;
402             fragment_t  *fragment;
403 
404             DISSECTOR_ASSERT_HINT(adb_service_data->session_key_length + 1 <= sizeof(key) / sizeof(key[0]), "Tree session key is too small");
405             for (i_key = 0; i_key < adb_service_data->session_key_length; i_key += 1) {
406                 key[i_key].length = 1;
407                 key[i_key].key = &adb_service_data->session_key[i_key];
408             }
409             key[i_key].length = 0;
410             key[i_key].key = NULL;
411 
412             subtree = (wmem_tree_t *) wmem_tree_lookup32_array(fragments, key);
413             fragment = (subtree) ? (fragment_t *) wmem_tree_lookup32_le(subtree, pinfo->num - 1) : NULL;
414             if (fragment) {
415                 if (!pinfo->fd->visited && fragment->reassembled_in_frame == -1)
416                     fragment->reassembled_in_frame = pinfo->num;
417 
418                 if (fragment->reassembled_in_frame == pinfo->num) {
419                     size += fragment->length;
420                     i_char += fragment->length;
421                 }
422             }
423 
424             size += tvb_reported_length_remaining(tvb, i_offset);
425             if (size > 0) {
426                 buffer = (guint8 *) wmem_alloc(pinfo->pool, size);
427                 if (fragment && i_char > 0)
428                     memcpy(buffer, fragment->data, i_char);
429 
430                 if (i_char >= 1 && buffer[i_char - 1] == '\r' && tvb_get_guint8(tvb, i_offset) == '\n') {
431                     buffer[i_char - 1] = '\n';
432                     i_offset += 1;
433                 }
434 
435                 c1 = tvb_get_guint8(tvb, i_offset);
436                 i_offset += 1;
437                 old_offset = i_offset;
438 
439                 while (tvb_reported_length_remaining(tvb, i_offset) > 0) {
440                     c2 = tvb_get_guint8(tvb, i_offset);
441 
442                     if (c1 == '\r' && c2 == '\n') {
443                         buffer[i_char] = c2;
444                         if (tvb_reported_length_remaining(tvb, i_offset) > 1) {
445                             c1 = tvb_get_guint8(tvb, i_offset + 1);
446                             i_offset += 2;
447                             i_char += 1;
448                         } else {
449                             i_offset += 1;
450                         }
451 
452                         continue;
453                     }
454 
455                     buffer[i_char] = c1;
456                     c1 = c2;
457                     i_char += 1;
458                     i_offset += 1;
459                 }
460 
461                 if (tvb_reported_length_remaining(tvb, old_offset) == 0) {
462                     buffer[i_char] = c1;
463                     i_char += 1;
464                 } else if (tvb_reported_length_remaining(tvb, old_offset) > 0) {
465                     buffer[i_char] = c2;
466                     i_char += 1;
467                 }
468 
469                 next_tvb = tvb_new_child_real_data(tvb, buffer, i_char, i_char);
470                 add_new_data_source(pinfo, next_tvb, "Logcat");
471 
472                 i_offset = 0;
473                 while (tvb_reported_length_remaining(next_tvb, i_offset) > 0) {
474                     if (tvb_reported_length_remaining(next_tvb, i_offset) >= 4) {
475                         payload_length = tvb_get_letohs(next_tvb, i_offset);
476                         try_header_size = tvb_get_letohs(next_tvb, i_offset + 2);
477 
478                         if (try_header_size != 24)
479                             logcat_length = payload_length + 20;
480                         else
481                             logcat_length = payload_length + 24;
482                     }
483 
484                     if (tvb_reported_length_remaining(next_tvb, i_offset) >= 4 && tvb_reported_length_remaining(next_tvb, i_offset) >= logcat_length) {
485                         new_tvb = tvb_new_subset_length(next_tvb, i_offset, logcat_length);
486 
487                         call_dissector(logcat_handle, new_tvb, pinfo, main_tree);
488                         i_offset += logcat_length;
489                     } else {
490 
491                         if (!pinfo->fd->visited) {
492                             DISSECTOR_ASSERT_HINT(adb_service_data->session_key_length + 2 <= sizeof(key) / sizeof(key[0]), "Tree session key is too small");
493                             for (i_key = 0; i_key < adb_service_data->session_key_length; i_key += 1) {
494                                 key[i_key].length = 1;
495                                 key[i_key].key = &adb_service_data->session_key[i_key];
496                             }
497                             key[i_key].length = 1;
498                             key[i_key++].key = &pinfo->num;
499                             key[i_key].length = 0;
500                             key[i_key].key = NULL;
501 
502                             fragment = wmem_new(wmem_file_scope(), fragment_t);
503 
504                             fragment->length = tvb_captured_length_remaining(next_tvb, i_offset);
505                             fragment->data = (guint8 *) wmem_alloc(wmem_file_scope(), fragment->length);
506                             tvb_memcpy(next_tvb, fragment->data, i_offset, fragment->length);
507                             fragment->reassembled_in_frame = -1;
508 
509                             wmem_tree_insert32_array(fragments, key, fragment);
510                         }
511 
512                         proto_tree_add_item(main_tree, hf_fragment, next_tvb, i_offset, -1, ENC_NA);
513                         i_offset = tvb_captured_length(next_tvb);
514                     }
515                 }
516             }
517 
518             offset = tvb_captured_length(tvb);
519         } else if (g_str_has_prefix(service, "shell:")) {
520             if (adb_service_data->direction == P2P_DIR_SENT) {
521                 proto_tree_add_item(main_tree, hf_stdin, tvb, offset, -1, ENC_NA | ENC_ASCII);
522                 col_append_fstr(pinfo->cinfo, COL_INFO, " Stdin=<%s>", tvb_format_text_wsp(pinfo->pool, tvb, offset, tvb_captured_length_remaining(tvb, offset)));
523 
524             } else {
525                 proto_tree_add_item(main_tree, hf_stdout, tvb, offset, -1, ENC_NA | ENC_ASCII);
526                 col_append_fstr(pinfo->cinfo, COL_INFO, " Stdout=<%s>", tvb_format_text_wsp(pinfo->pool, tvb, offset, tvb_captured_length_remaining(tvb, offset)));
527             }
528             offset = tvb_captured_length(tvb);
529         } else if (g_str_has_prefix(service, "jdwp:")) {
530 /* TODO */
531             proto_tree_add_item(main_tree, hf_data, tvb, offset, -1, ENC_NA);
532             offset = tvb_captured_length(tvb);
533         } else if (g_str_has_prefix(service, "sync:")) {
534 /* TODO */
535             proto_tree_add_item(main_tree, hf_data, tvb, offset, -1, ENC_NA);
536             offset = tvb_captured_length(tvb);
537         } else if (g_strcmp0(service, "host:list-forward") == 0 ||
538                 g_str_has_prefix(service, "root:") ||
539                 g_str_has_prefix(service, "remount:")  ||
540                 g_str_has_prefix(service, "tcpip:")  ||
541                 g_str_has_prefix(service, "usb:")) {
542             if (tvb_reported_length_remaining(tvb, offset)) {
543                 proto_tree_add_item(main_tree, hf_result, tvb, offset, -1, ENC_NA | ENC_ASCII);
544                 col_append_fstr(pinfo->cinfo, COL_INFO, " Result=<%s>", tvb_format_text_wsp(pinfo->pool, tvb, offset, tvb_captured_length_remaining(tvb, offset)));
545 
546                 offset = tvb_captured_length(tvb);
547             }
548         } else {
549             proto_tree_add_item(main_tree, hf_data, tvb, offset, -1, ENC_NA);
550             offset = tvb_captured_length(tvb);
551         }
552 
553     return offset;
554 }
555 
556 
557 void
proto_register_adb_service(void)558 proto_register_adb_service(void)
559 {
560     module_t         *module;
561     expert_module_t  *expert_module;
562 
563     static hf_register_info hf[] = {
564         { &hf_service,
565             { "Service",                         "adb_service.service",
566             FT_STRING, STR_ASCII, NULL, 0x00,
567             NULL, HFILL }
568         },
569         { &hf_fragment,
570             { "Fragment",                        "adb_service.fragment",
571             FT_NONE, BASE_NONE, NULL, 0x00,
572             NULL, HFILL }
573         },
574         { &hf_data,
575             { "Data",                            "adb_service.data",
576             FT_BYTES, BASE_NONE, NULL, 0x00,
577             NULL, HFILL }
578         },
579         { &hf_hex_ascii_length,
580             { "Hex ASCII String Length",         "adb_service.hex_ascii_length",
581             FT_STRING, STR_ASCII, NULL, 0x00,
582             NULL, HFILL }
583         },
584         { &hf_length,
585             { "Length",                          "adb_service.length",
586             FT_UINT32, BASE_DEC_HEX, NULL, 0x00,
587             NULL, HFILL }
588         },
589         { &hf_framebuffer_version,
590             { "Version",                         "adb_service.framebuffer.version",
591             FT_UINT32, BASE_DEC, NULL, 0x00,
592             NULL, HFILL }
593         },
594         { &hf_hex_ascii_version,
595             { "Hex ASCII String Version",        "adb_service.hex_ascii_version",
596             FT_STRING, STR_ASCII, NULL, 0x00,
597             NULL, HFILL }
598         },
599         { &hf_version,
600             { "Version",                         "adb_service.version",
601             FT_UINT32, BASE_DEC_HEX, NULL, 0x00,
602             NULL, HFILL }
603         },
604         { &hf_framebuffer_depth,
605             { "Depth",                           "adb_service.framebuffer.depth",
606             FT_UINT32, BASE_DEC, NULL, 0x00,
607             NULL, HFILL }
608         },
609         { &hf_framebuffer_size,
610             { "Size",                           "adb_service.framebuffer.size",
611             FT_UINT32, BASE_DEC, NULL, 0x00,
612             NULL, HFILL }
613         },
614         { &hf_framebuffer_width,
615             { "Width",                           "adb_service.framebuffer.width",
616             FT_UINT32, BASE_DEC, NULL, 0x00,
617             NULL, HFILL }
618         },
619         { &hf_framebuffer_height,
620             { "Height",                          "adb_service.framebuffer.height",
621             FT_UINT32, BASE_DEC, NULL, 0x00,
622             NULL, HFILL }
623         },
624         { &hf_framebuffer_red_offset,
625             { "Red Offset",                      "adb_service.framebuffer.red_offset",
626             FT_UINT32, BASE_DEC, NULL, 0x00,
627             NULL, HFILL }
628         },
629         { &hf_framebuffer_red_length,
630             { "Red Length",                      "adb_service.framebuffer.red_length",
631             FT_UINT32, BASE_DEC, NULL, 0x00,
632             NULL, HFILL }
633         },
634         { &hf_framebuffer_blue_offset,
635             { "Blue Offset",                     "adb_service.framebuffer.blue_offset",
636             FT_UINT32, BASE_DEC, NULL, 0x00,
637             NULL, HFILL }
638         },
639         { &hf_framebuffer_blue_length,
640             { "Blue Length",                     "adb_service.framebuffer.blue_length",
641             FT_UINT32, BASE_DEC, NULL, 0x00,
642             NULL, HFILL }
643         },
644         { &hf_framebuffer_green_offset,
645             { "Green Offset",                    "adb_service.framebuffer.green_offset",
646             FT_UINT32, BASE_DEC, NULL, 0x00,
647             NULL, HFILL }
648         },
649         { &hf_framebuffer_green_length,
650             { "Green Length",                    "adb_service.framebuffer.green_length",
651             FT_UINT32, BASE_DEC, NULL, 0x00,
652             NULL, HFILL }
653         },
654         { &hf_framebuffer_alpha_offset,
655             { "Alpha Offset",                    "adb_service.framebuffer.alpha_offset",
656             FT_UINT32, BASE_DEC, NULL, 0x00,
657             NULL, HFILL }
658         },
659         { &hf_framebuffer_alpha_length,
660             { "Alpha Length",                    "adb_service.framebuffer.alpha_length",
661             FT_UINT32, BASE_DEC, NULL, 0x00,
662             NULL, HFILL }
663         },
664         { &hf_framebuffer_pixel,
665             { "Pixel",                           "adb_service.framebuffer.pixel",
666             FT_NONE, BASE_NONE, NULL, 0x00,
667             NULL, HFILL }
668         },
669         { &hf_framebuffer_blue_5,
670             { "Blue",                            "adb_service.framebuffer.pixel.blue",
671             FT_UINT16, BASE_DEC, NULL, 0xF800,
672             NULL, HFILL }
673         },
674         { &hf_framebuffer_green_6,
675             { "Green",                           "adb_service.framebuffer.pixel.green",
676             FT_UINT16, BASE_DEC, NULL, 0x07E0,
677             NULL, HFILL }
678         },
679         { &hf_framebuffer_red_5,
680             { "Red",                             "adb_service.framebuffer.pixel.red",
681             FT_UINT16, BASE_DEC, NULL, 0x001F,
682             NULL, HFILL }
683         },
684         { &hf_framebuffer_blue,
685             { "Blue",                            "adb_service.framebuffer.pixel.blue",
686             FT_UINT8, BASE_DEC, NULL, 0x00,
687             NULL, HFILL }
688         },
689         { &hf_framebuffer_green,
690             { "Green",                           "adb_service.framebuffer.pixel.green",
691             FT_UINT8, BASE_DEC, NULL, 0x00,
692             NULL, HFILL }
693         },
694         { &hf_framebuffer_red,
695             { "Red",                             "adb_service.framebuffer.pixel.red",
696             FT_UINT8, BASE_DEC, NULL, 0x00,
697             NULL, HFILL }
698         },
699         { &hf_framebuffer_alpha,
700             { "Alpha",                           "adb_service.framebuffer.pixel.alpha",
701             FT_UINT8, BASE_DEC, NULL, 0x00,
702             NULL, HFILL }
703         },
704         { &hf_framebuffer_unused,
705             { "Unused",                          "adb_service.framebuffer.pixel.unused",
706             FT_UINT8, BASE_DEC, NULL, 0x00,
707             NULL, HFILL }
708         },
709         { &hf_devices,
710             { "Devices",                         "adb_service.devices",
711             FT_STRING, STR_ASCII, NULL, 0x00,
712             NULL, HFILL }
713         },
714         { &hf_stdin,
715             { "Stdin",                           "adb_service.stdin",
716             FT_STRING, STR_ASCII, NULL, 0x00,
717             NULL, HFILL }
718         },
719         { &hf_stdout,
720             { "Stdout",                          "adb_service.stdout",
721             FT_STRING, STR_ASCII, NULL, 0x00,
722             NULL, HFILL }
723         },
724         { &hf_result,
725             { "Result",                          "adb_service.result",
726             FT_STRING, STR_ASCII, NULL, 0x00,
727             NULL, HFILL }
728         },
729         { &hf_pids,
730             { "PIDs",                            "adb_service.pids",
731             FT_STRING, STR_ASCII, NULL, 0x00,
732             NULL, HFILL }
733         },
734     };
735 
736     static gint *ett[] = {
737         &ett_adb_service,
738         &ett_length,
739         &ett_version,
740         &ett_pixel,
741         &ett_data
742     };
743 
744     static ei_register_info ei[] = {
745         { &ei_incomplete_message,         { "adb_service.expert.incomplete_message", PI_PROTOCOL, PI_WARN, "Incomplete message", EXPFILL }},
746     };
747 
748     fragments          = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
749     framebuffer_infos  = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
750     continuation_infos = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
751 
752     proto_adb_service = proto_register_protocol("Android Debug Bridge Service", "ADB Service", "adb_service");
753     adb_service_handle = register_dissector("adb_service", dissect_adb_service, proto_adb_service);
754 
755     proto_register_field_array(proto_adb_service, hf, array_length(hf));
756     proto_register_subtree_array(ett, array_length(ett));
757     expert_module = expert_register_protocol(proto_adb_service);
758     expert_register_field_array(expert_module, ei, array_length(ei));
759 
760     module = prefs_register_protocol(proto_adb_service, NULL);
761     prefs_register_static_text_preference(module, "version",
762             "ADB Service protocol version is compatible prior to: adb 1.0.31",
763             "Version of protocol supported by this dissector.");
764 
765     prefs_register_bool_preference(module, "framebuffer_more_details",
766             "Dissect more detail for framebuffer service",
767             "Dissect more detail for framebuffer service",
768             &pref_dissect_more_detail_framebuffer);
769 }
770 
771 
772 void
proto_reg_handoff_adb_service(void)773 proto_reg_handoff_adb_service(void)
774 {
775     logcat_handle = find_dissector_add_dependency("logcat", proto_adb_service);
776 }
777 
778 /*
779  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
780  *
781  * Local variables:
782  * c-basic-offset: 4
783  * tab-width: 8
784  * indent-tabs-mode: nil
785  * End:
786  *
787  * vi: set shiftwidth=4 tabstop=8 expandtab:
788  * :indentSize=4:tabSize=8:noTabs=true:
789  */
790