1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define  TRACE_TAG   TRACE_ADB
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <ctype.h>
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdarg.h>
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
24c7993af64baec271a238646bc20aaa846866c4a9Scott Anderson#include <stddef.h>
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <time.h>
271f546e6d1f6ccd1964336ddf0d8e8b3b11b1e945Mike Lockwood#include <sys/time.h>
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "sysdeps.h"
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "adb.h"
31d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby#include "adb_auth.h"
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
33e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
34e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#if !ADB_HOST
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <private/android_filesystem_config.h>
375f4b051235c3e9e9e5b34b4af885e42a1c711fc4Mike Lockwood#include <linux/capability.h>
385f4b051235c3e9e9e5b34b4af885e42a1c711fc4Mike Lockwood#include <linux/prctl.h>
39885342a0f2c834a6b680284047c47c9d04b32565Jeff Sharkey#include <sys/mount.h>
40a09fbd164d2e088bc5433d310e25640ae048d47dXavier Ducrohet#else
41a09fbd164d2e088bc5433d310e25640ae048d47dXavier Ducrohet#include "usb_vendors.h"
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall#if ADB_TRACE
45408fa57864c01113deaa213e5c1848a9c594ae92JP AbgrallADB_MUTEX_DEFINE( D_lock );
46408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall#endif
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint HOST = 0;
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
50d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobystatic int auth_enabled = 0;
51d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
52e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#if !ADB_HOST
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *adb_device_banner = "device";
54e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#endif
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid fatal(const char *fmt, ...)
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_list ap;
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_start(ap, fmt);
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "error: ");
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vfprintf(stderr, fmt, ap);
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "\n");
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_end(ap);
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    exit(-1);
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid fatal_errno(const char *fmt, ...)
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_list ap;
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_start(ap, fmt);
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "error: %s: ", strerror(errno));
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    vfprintf(stderr, fmt, ap);
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "\n");
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    va_end(ap);
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    exit(-1);
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint   adb_trace_mask;
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* read a comma/space/colum/semi-column separated list of tags
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * from the ADB_TRACE environment variable and build the trace
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * mask from it. note that '1' and 'all' are special cases to
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * enable all tracing
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid  adb_trace_init(void)
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char*  p = getenv("ADB_TRACE");
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char*  q;
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    static const struct {
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        const char*  tag;
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int           flag;
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } tags[] = {
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "1", 0 },
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "all", 0 },
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "adb", TRACE_ADB },
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "sockets", TRACE_SOCKETS },
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "packets", TRACE_PACKETS },
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "rwx", TRACE_RWX },
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "usb", TRACE_USB },
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "sync", TRACE_SYNC },
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "sysdeps", TRACE_SYSDEPS },
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "transport", TRACE_TRANSPORT },
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { "jdwp", TRACE_JDWP },
105408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        { "services", TRACE_SERVICES },
106d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        { "auth", TRACE_AUTH },
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        { NULL, 0 }
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    };
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (p == NULL)
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return;
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* use a comma/column/semi-colum/space separated list */
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while (*p) {
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int  len, tagn;
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        q = strpbrk(p, " ,:;");
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (q == NULL) {
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            q = p + strlen(p);
120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
121dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = q - p;
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for (tagn = 0; tags[tagn].tag != NULL; tagn++)
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        {
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            int  taglen = strlen(tags[tagn].tag);
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            {
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                int  flag = tags[tagn].flag;
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if (flag == 0) {
131dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    adb_trace_mask = ~0;
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    return;
133dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                }
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                adb_trace_mask |= (1 << flag);
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
136dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
137dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
138dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        p = q;
139dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (*p)
140dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            p++;
141dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
142dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
143dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
14428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#if !ADB_HOST
14528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine/*
14628781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine * Implements ADB tracing inside the emulator.
14728781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine */
14828781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
14928781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#include <stdarg.h>
15028781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
15128781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine/*
15228781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine * Redefine open and write for qemu_pipe.h that contains inlined references
15328781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine * to those routines. We will redifine them back after qemu_pipe.h inclusion.
15428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine */
15528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
15628781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#undef open
15728781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#undef write
15828781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#define open    adb_open
15928781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#define write   adb_write
16028781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#include <hardware/qemu_pipe.h>
16128781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#undef open
16228781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#undef write
16328781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#define open    ___xxx_open
16428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#define write   ___xxx_write
16528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
16628781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine/* A handle to adb-debug qemud service in the emulator. */
16728781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkineint   adb_debug_qemu = -1;
16828781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
16928781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine/* Initializes connection with the adb-debug qemud service in the emulator. */
17028781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkinestatic int adb_qemu_trace_init(void)
17128781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine{
17228781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    char con_name[32];
17328781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
17428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    if (adb_debug_qemu >= 0) {
17528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine        return 0;
17628781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    }
17728781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
17828781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    /* adb debugging QEMUD service connection request. */
17928781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    snprintf(con_name, sizeof(con_name), "qemud:adb-debug");
18028781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    adb_debug_qemu = qemu_pipe_open(con_name);
18128781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    return (adb_debug_qemu >= 0) ? 0 : -1;
18228781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine}
18328781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
18428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkinevoid adb_qemu_trace(const char* fmt, ...)
18528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine{
18628781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    va_list args;
18728781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    va_start(args, fmt);
18828781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    char msg[1024];
18928781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine
19028781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    if (adb_debug_qemu >= 0) {
19128781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine        vsnprintf(msg, sizeof(msg), fmt, args);
19228781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine        adb_write(adb_debug_qemu, msg, strlen(msg));
19328781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine    }
19428781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine}
19528781b0a52dfb5ad19121afcc6aef56918992b30Vladimir Chtchetkine#endif  /* !ADB_HOST */
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
197dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectapacket *get_apacket(void)
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    apacket *p = malloc(sizeof(apacket));
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(p == 0) fatal("failed to allocate an apacket");
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return p;
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
204dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid put_apacket(apacket *p)
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(p);
208dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
209dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
210d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobyvoid handle_online(atransport *t)
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("adb: online\n");
213d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    t->online = 1;
214dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
215dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
216dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid handle_offline(atransport *t)
217dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
218dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("adb: offline\n");
219dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    //Close the associated usb
220d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    t->online = 0;
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    run_transport_disconnects(t);
222dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
223dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
224d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby#if DEBUG_PACKETS
225dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define DUMPMAX 32
226dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid print_packet(const char *label, apacket *p)
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
228dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *tag;
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *x;
230dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned count;
231dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
232dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    switch(p->msg.command){
233dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_SYNC: tag = "SYNC"; break;
234dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_CNXN: tag = "CNXN" ; break;
235dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_OPEN: tag = "OPEN"; break;
236dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_OKAY: tag = "OKAY"; break;
237dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_CLSE: tag = "CLSE"; break;
238dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_WRTE: tag = "WRTE"; break;
239d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    case A_AUTH: tag = "AUTH"; break;
240dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    default: tag = "????"; break;
241dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
242dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
243dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "%s: %s %08x %08x %04x \"",
244dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
245dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    count = p->msg.data_length;
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    x = (char*) p->data;
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(count > DUMPMAX) {
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        count = DUMPMAX;
249dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        tag = "\n";
250dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
251dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        tag = "\"\n";
252dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
253dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(count-- > 0){
254dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if((*x >= ' ') && (*x < 127)) {
255dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fputc(*x, stderr);
256dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
257dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fputc('.', stderr);
258dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
259dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        x++;
260dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
261d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    fputs(tag, stderr);
262dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
263dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
264dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
265dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void send_ready(unsigned local, unsigned remote, atransport *t)
266dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
267dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("Calling send_ready \n");
268dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    apacket *p = get_apacket();
269dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.command = A_OKAY;
270dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.arg0 = local;
271dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.arg1 = remote;
272dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    send_packet(p, t);
273dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
274dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
275dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void send_close(unsigned local, unsigned remote, atransport *t)
276dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
277dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("Calling send_close \n");
278dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    apacket *p = get_apacket();
279dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.command = A_CLSE;
280dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.arg0 = local;
281dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p->msg.arg1 = remote;
282dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    send_packet(p, t);
283dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
284dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
285e82c2db05cae70a0490a1f84b7211ef42c329671Scott Andersonstatic size_t fill_connect_data(char *buf, size_t bufsize)
286e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson{
287e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#if ADB_HOST
288e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    return snprintf(buf, bufsize, "host::") + 1;
289e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#else
290e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    static const char *cnxn_props[] = {
291e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        "ro.product.name",
292e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        "ro.product.model",
293e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        "ro.product.device",
294e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    };
295e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
296e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    int i;
297e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    size_t remaining = bufsize;
298e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    size_t len;
299e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
300e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    len = snprintf(buf, remaining, "%s::", adb_device_banner);
301e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    remaining -= len;
302e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    buf += len;
303e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    for (i = 0; i < num_cnxn_props; i++) {
304e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        char value[PROPERTY_VALUE_MAX];
305e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        property_get(cnxn_props[i], value, "");
306e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
307e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        remaining -= len;
308e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        buf += len;
309e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    }
310e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
311e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    return bufsize - remaining + 1;
312e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson#endif
313e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson}
314e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
315dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void send_connect(atransport *t)
316dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("Calling send_connect \n");
318dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    apacket *cp = get_apacket();
319dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    cp->msg.command = A_CNXN;
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    cp->msg.arg0 = A_VERSION;
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    cp->msg.arg1 = MAX_PAYLOAD;
322e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    cp->msg.data_length = fill_connect_data((char *)cp->data,
323e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                                            sizeof(cp->data));
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    send_packet(cp, t);
325d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby}
326d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
327d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobystatic void send_auth_request(atransport *t)
328d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby{
329d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    D("Calling send_auth_request\n");
330d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    apacket *p;
331d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    int ret;
332d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
333d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    ret = adb_auth_generate_token(t->token, sizeof(t->token));
334d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    if (ret != sizeof(t->token)) {
335d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        D("Error generating token ret=%d\n", ret);
336d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        return;
337d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    }
338d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
339d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p = get_apacket();
340d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    memcpy(p->data, t->token, ret);
341d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.command = A_AUTH;
342d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.arg0 = ADB_AUTH_TOKEN;
343d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.data_length = ret;
344d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    send_packet(p, t);
345d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby}
346d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
347d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobystatic void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
348d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby{
349d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    D("Calling send_auth_response\n");
350d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    apacket *p = get_apacket();
351d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    int ret;
352d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
353d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    ret = adb_auth_sign(t->key, token, token_size, p->data);
354d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    if (!ret) {
355d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        D("Error signing the token\n");
356d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        put_apacket(p);
357d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        return;
358d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    }
359d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
360d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.command = A_AUTH;
361d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.arg0 = ADB_AUTH_SIGNATURE;
362d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.data_length = ret;
363d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    send_packet(p, t);
364d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby}
365d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
366d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobystatic void send_auth_publickey(atransport *t)
367d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby{
368d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    D("Calling send_auth_publickey\n");
369d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    apacket *p = get_apacket();
370d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    int ret;
371d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
372d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    ret = adb_auth_get_userkey(p->data, sizeof(p->data));
373d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    if (!ret) {
374d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        D("Failed to get user public key\n");
375d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        put_apacket(p);
376d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        return;
377d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    }
378d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
379d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.command = A_AUTH;
380d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
381d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    p->msg.data_length = ret;
382d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    send_packet(p, t);
383d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby}
384d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
385d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Gobyvoid adb_auth_verified(atransport *t)
386d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby{
387d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    handle_online(t);
388d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    send_connect(t);
389dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
390dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
391dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *connection_state_name(atransport *t)
392dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
393dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (t == NULL) {
394dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return "unknown";
395dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
396dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
397dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    switch(t->connection_state) {
398dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case CS_BOOTLOADER:
399dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return "bootloader";
400dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case CS_DEVICE:
401dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return "device";
402dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case CS_OFFLINE:
403dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return "offline";
404dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    default:
405dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return "unknown";
406dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
407dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
408dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
409e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson/* qual_overwrite is used to overwrite a qualifier string.  dst is a
410e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson * pointer to a char pointer.  It is assumed that if *dst is non-NULL, it
4112ca3e6b35f79136418ebc32fef57580698dbd045Scott Anderson * was malloc'ed and needs to freed.  *dst will be set to a dup of src.
412e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson */
413e82c2db05cae70a0490a1f84b7211ef42c329671Scott Andersonstatic void qual_overwrite(char **dst, const char *src)
414e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson{
415e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    if (!dst)
416e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        return;
417e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
418e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    free(*dst);
419e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    *dst = NULL;
420e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
421e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    if (!src || !*src)
422e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        return;
423e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
424e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    *dst = strdup(src);
425e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson}
426e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson
427dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid parse_banner(char *banner, atransport *t)
428dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
429e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    static const char *prop_seps = ";";
430e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    static const char key_val_sep = '=';
4312ca3e6b35f79136418ebc32fef57580698dbd045Scott Anderson    char *cp;
4322ca3e6b35f79136418ebc32fef57580698dbd045Scott Anderson    char *type;
433dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
434dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    D("parse_banner: %s\n", banner);
435dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    type = banner;
436e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    cp = strchr(type, ':');
437e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson    if (cp) {
438e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        *cp++ = 0;
439e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        /* Nothing is done with second field. */
440e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        cp = strchr(cp, ':');
441e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        if (cp) {
442e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson            char *save;
443e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson            char *key;
4441b7a7e81195ff06a7482f81cb92b094bb3481cb1Scott Anderson            key = adb_strtok_r(cp + 1, prop_seps, &save);
445e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson            while (key) {
446e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                cp = strchr(key, key_val_sep);
447e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                if (cp) {
448e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                    *cp++ = '\0';
449e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                    if (!strcmp(key, "ro.product.name"))
450e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                        qual_overwrite(&t->product, cp);
451e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                    else if (!strcmp(key, "ro.product.model"))
452e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                        qual_overwrite(&t->model, cp);
453e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                    else if (!strcmp(key, "ro.product.device"))
454e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                        qual_overwrite(&t->device, cp);
455e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson                }
4561b7a7e81195ff06a7482f81cb92b094bb3481cb1Scott Anderson                key = adb_strtok_r(NULL, prop_seps, &save);
457e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson            }
458e82c2db05cae70a0490a1f84b7211ef42c329671Scott Anderson        }
459dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
460dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
461dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(type, "bootloader")){
462dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("setting connection_state to CS_BOOTLOADER\n");
463dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        t->connection_state = CS_BOOTLOADER;
464dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        update_transports();
465dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return;
466dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
467dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
468dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(type, "device")) {
469dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("setting connection_state to CS_DEVICE\n");
470dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        t->connection_state = CS_DEVICE;
471dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        update_transports();
472dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return;
473dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
474dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
475dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(type, "recovery")) {
476dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        D("setting connection_state to CS_RECOVERY\n");
477dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        t->connection_state = CS_RECOVERY;
478dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        update_transports();
479dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return;
480dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
481dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
482447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(!strcmp(type, "sideload")) {
483447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        D("setting connection_state to CS_SIDELOAD\n");
484447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        t->connection_state = CS_SIDELOAD;
485447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        update_transports();
486447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        return;
487447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
488447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
489dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    t->connection_state = CS_HOST;
490dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
491dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
492dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid handle_packet(apacket *p, atransport *t)
493dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
494dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    asocket *s;
495dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
496899913f8168b54e00971c0e8d4ae16d06a4651feViral Mehta    D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
497899913f8168b54e00971c0e8d4ae16d06a4651feViral Mehta            ((char*) (&(p->msg.command)))[1],
498899913f8168b54e00971c0e8d4ae16d06a4651feViral Mehta            ((char*) (&(p->msg.command)))[2],
499899913f8168b54e00971c0e8d4ae16d06a4651feViral Mehta            ((char*) (&(p->msg.command)))[3]);
500dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    print_packet("recv", p);
501dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
502dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    switch(p->msg.command){
503dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_SYNC:
504dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(p->msg.arg0){
505dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            send_packet(p, t);
506dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(HOST) send_connect(t);
507dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
508dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            t->connection_state = CS_OFFLINE;
509dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            handle_offline(t);
510dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            send_packet(p, t);
511dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
512dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return;
513dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
514dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
515dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /* XXX verify version, etc */
516dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(t->connection_state != CS_OFFLINE) {
517dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            t->connection_state = CS_OFFLINE;
518dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            handle_offline(t);
519dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
520d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
521dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        parse_banner((char*) p->data, t);
522d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
523d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        if (HOST || !auth_enabled) {
524d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            handle_online(t);
525d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            if(!HOST) send_connect(t);
526d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        } else {
527d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            send_auth_request(t);
528d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        }
529d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        break;
530d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby
531d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby    case A_AUTH:
532d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        if (p->msg.arg0 == ADB_AUTH_TOKEN) {
533d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            t->key = adb_auth_nextkey(t->key);
534d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            if (t->key) {
535d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                send_auth_response(p->data, p->msg.data_length, t);
536d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            } else {
537d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                /* No more private keys to try, send the public key */
538d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                send_auth_publickey(t);
539d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            }
540d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby        } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
541d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
542d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                adb_auth_verified(t);
543d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                t->failed_auth_attempts = 0;
544d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby            } else {
545d5fcafaf41f8ec90986c813f75ec78402096af2dBenoit Goby                if (t->failed_auth_attempts++ > 10)
546