2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG TRANSPORT
|
2015-02-25 00:51:19 +01:00
|
|
|
|
2015-03-19 23:21:08 +01:00
|
|
|
#include "sysdeps.h"
|
2018-03-08 01:51:08 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
|
|
|
|
2015-02-21 02:24:58 +01:00
|
|
|
#include <ctype.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <errno.h>
|
2017-08-17 01:57:01 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-11-08 03:51:54 +01:00
|
|
|
#include <algorithm>
|
2018-02-16 22:24:58 +01:00
|
|
|
#include <deque>
|
2015-05-19 01:46:31 +02:00
|
|
|
#include <list>
|
2018-08-08 19:33:24 +02:00
|
|
|
#include <memory>
|
2016-09-21 21:37:10 +02:00
|
|
|
#include <mutex>
|
2018-08-10 23:28:24 +02:00
|
|
|
#include <set>
|
2017-04-13 02:00:49 +02:00
|
|
|
#include <thread>
|
2015-05-19 01:46:31 +02:00
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
#include <adb/crypto/rsa_2048_key.h>
|
|
|
|
#include <adb/crypto/x509_generator.h>
|
|
|
|
#include <adb/tls/tls_connection.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
2016-03-01 17:58:26 +01:00
|
|
|
#include <android-base/parsenetaddress.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2017-08-17 01:57:01 +02:00
|
|
|
#include <android-base/thread_annotations.h>
|
2015-05-01 02:32:03 +02:00
|
|
|
|
2018-01-02 21:01:43 +01:00
|
|
|
#include <diagnose_usb.h>
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "adb.h"
|
2016-06-30 02:42:01 +02:00
|
|
|
#include "adb_auth.h"
|
2018-01-29 05:32:46 +01:00
|
|
|
#include "adb_io.h"
|
2016-11-29 18:40:29 +01:00
|
|
|
#include "adb_trace.h"
|
2015-05-01 02:32:03 +02:00
|
|
|
#include "adb_utils.h"
|
2019-06-28 22:50:37 +02:00
|
|
|
#include "fdevent/fdevent.h"
|
2018-07-31 23:12:59 +02:00
|
|
|
#include "sysdeps/chrono.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
using namespace adb::crypto;
|
|
|
|
using namespace adb::tls;
|
2019-02-12 22:59:03 +01:00
|
|
|
using android::base::ScopedLockAssertion;
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
using TlsError = TlsConnection::TlsError;
|
2019-02-12 22:59:03 +01:00
|
|
|
|
2018-04-20 19:31:29 +02:00
|
|
|
static void remove_transport(atransport* transport);
|
2019-12-09 22:45:31 +01:00
|
|
|
static void transport_destroy(atransport* transport);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
// TODO: unordered_map<TransportId, atransport*>
|
2015-11-12 02:56:12 +01:00
|
|
|
static auto& transport_list = *new std::list<atransport*>();
|
|
|
|
static auto& pending_list = *new std::list<atransport*>();
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2017-08-17 22:50:51 +02:00
|
|
|
static auto& transport_lock = *new std::recursive_mutex();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-11-10 01:03:25 +01:00
|
|
|
const char* const kFeatureShell2 = "shell_v2";
|
|
|
|
const char* const kFeatureCmd = "cmd";
|
2016-12-06 02:11:34 +01:00
|
|
|
const char* const kFeatureStat2 = "stat_v2";
|
2019-08-07 23:23:17 +02:00
|
|
|
const char* const kFeatureLs2 = "ls_v2";
|
2017-02-23 02:07:01 +01:00
|
|
|
const char* const kFeatureLibusb = "libusb";
|
2017-05-23 23:30:00 +02:00
|
|
|
const char* const kFeaturePushSync = "push_sync";
|
2018-10-04 17:26:40 +02:00
|
|
|
const char* const kFeatureApex = "apex";
|
2018-10-22 22:00:05 +02:00
|
|
|
const char* const kFeatureFixedPushMkdir = "fixed_push_mkdir";
|
2019-01-17 22:13:56 +01:00
|
|
|
const char* const kFeatureAbb = "abb";
|
2019-02-20 22:01:40 +01:00
|
|
|
const char* const kFeatureFixedPushSymlinkTimestamp = "fixed_push_symlink_timestamp";
|
2019-02-21 23:22:51 +01:00
|
|
|
const char* const kFeatureAbbExec = "abb_exec";
|
2019-07-11 23:15:32 +02:00
|
|
|
const char* const kFeatureRemountShell = "remount_shell";
|
2020-02-14 02:01:39 +01:00
|
|
|
const char* const kFeatureTrackApp = "track_app";
|
2020-03-05 04:34:08 +01:00
|
|
|
const char* const kFeatureSendRecv2 = "sendrecv_v2";
|
|
|
|
const char* const kFeatureSendRecv2Brotli = "sendrecv_v2_brotli";
|
2020-03-27 06:02:03 +01:00
|
|
|
const char* const kFeatureSendRecv2LZ4 = "sendrecv_v2_lz4";
|
2020-03-31 08:25:16 +02:00
|
|
|
const char* const kFeatureSendRecv2DryRunSend = "sendrecv_v2_dry_run_send";
|
2015-11-10 01:03:25 +01:00
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
namespace {
|
|
|
|
|
2018-08-01 03:28:32 +02:00
|
|
|
#if ADB_HOST
|
2018-04-20 19:31:29 +02:00
|
|
|
// Tracks and handles atransport*s that are attempting reconnection.
|
|
|
|
class ReconnectHandler {
|
|
|
|
public:
|
|
|
|
ReconnectHandler() = default;
|
|
|
|
~ReconnectHandler() = default;
|
|
|
|
|
|
|
|
// Starts the ReconnectHandler thread.
|
|
|
|
void Start();
|
|
|
|
|
|
|
|
// Requests the ReconnectHandler thread to stop.
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
// Adds the atransport* to the queue of reconnect attempts.
|
|
|
|
void TrackTransport(atransport* transport);
|
|
|
|
|
2018-08-10 23:44:54 +02:00
|
|
|
// Wake up the ReconnectHandler thread to have it check for kicked transports.
|
|
|
|
void CheckForKicked();
|
|
|
|
|
2018-04-20 19:31:29 +02:00
|
|
|
private:
|
|
|
|
// The main thread loop.
|
|
|
|
void Run();
|
|
|
|
|
|
|
|
// Tracks a reconnection attempt.
|
|
|
|
struct ReconnectAttempt {
|
|
|
|
atransport* transport;
|
2018-07-31 03:51:55 +02:00
|
|
|
std::chrono::steady_clock::time_point reconnect_time;
|
2018-04-20 19:31:29 +02:00
|
|
|
size_t attempts_left;
|
2018-07-31 23:12:59 +02:00
|
|
|
|
|
|
|
bool operator<(const ReconnectAttempt& rhs) const {
|
2018-08-10 23:28:24 +02:00
|
|
|
if (reconnect_time == rhs.reconnect_time) {
|
|
|
|
return reinterpret_cast<uintptr_t>(transport) <
|
|
|
|
reinterpret_cast<uintptr_t>(rhs.transport);
|
|
|
|
}
|
|
|
|
return reconnect_time < rhs.reconnect_time;
|
2018-07-31 23:12:59 +02:00
|
|
|
}
|
2018-04-20 19:31:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Only retry for up to one minute.
|
2018-07-31 23:12:59 +02:00
|
|
|
static constexpr const std::chrono::seconds kDefaultTimeout = 10s;
|
2018-04-20 19:31:29 +02:00
|
|
|
static constexpr const size_t kMaxAttempts = 6;
|
|
|
|
|
|
|
|
// Protects all members.
|
|
|
|
std::mutex reconnect_mutex_;
|
|
|
|
bool running_ GUARDED_BY(reconnect_mutex_) = true;
|
|
|
|
std::thread handler_thread_;
|
|
|
|
std::condition_variable reconnect_cv_;
|
2018-08-10 23:28:24 +02:00
|
|
|
std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_);
|
2018-04-20 19:31:29 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ReconnectHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
void ReconnectHandler::Start() {
|
|
|
|
check_main_thread();
|
|
|
|
handler_thread_ = std::thread(&ReconnectHandler::Run, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReconnectHandler::Stop() {
|
|
|
|
check_main_thread();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(reconnect_mutex_);
|
|
|
|
running_ = false;
|
|
|
|
}
|
|
|
|
reconnect_cv_.notify_one();
|
|
|
|
handler_thread_.join();
|
|
|
|
|
|
|
|
// Drain the queue to free all resources.
|
|
|
|
std::lock_guard<std::mutex> lock(reconnect_mutex_);
|
|
|
|
while (!reconnect_queue_.empty()) {
|
2018-08-10 23:28:24 +02:00
|
|
|
ReconnectAttempt attempt = *reconnect_queue_.begin();
|
|
|
|
reconnect_queue_.erase(reconnect_queue_.begin());
|
2018-04-20 19:31:29 +02:00
|
|
|
remove_transport(attempt.transport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReconnectHandler::TrackTransport(atransport* transport) {
|
|
|
|
check_main_thread();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(reconnect_mutex_);
|
|
|
|
if (!running_) return;
|
2018-07-31 23:12:59 +02:00
|
|
|
// Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited.
|
|
|
|
auto reconnect_time = std::chrono::steady_clock::now() + 250ms;
|
|
|
|
reconnect_queue_.emplace(
|
|
|
|
ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts});
|
2018-04-20 19:31:29 +02:00
|
|
|
}
|
|
|
|
reconnect_cv_.notify_one();
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:44:54 +02:00
|
|
|
void ReconnectHandler::CheckForKicked() {
|
|
|
|
reconnect_cv_.notify_one();
|
|
|
|
}
|
|
|
|
|
2018-04-20 19:31:29 +02:00
|
|
|
void ReconnectHandler::Run() {
|
|
|
|
while (true) {
|
|
|
|
ReconnectAttempt attempt;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(reconnect_mutex_);
|
2019-02-12 22:59:03 +01:00
|
|
|
ScopedLockAssertion assume_lock(reconnect_mutex_);
|
2018-04-20 19:31:29 +02:00
|
|
|
|
2018-07-31 03:51:55 +02:00
|
|
|
if (!reconnect_queue_.empty()) {
|
|
|
|
// FIXME: libstdc++ (used on Windows) implements condition_variable with
|
|
|
|
// system_clock as its clock, so we're probably hosed if the clock changes,
|
|
|
|
// even if we use steady_clock throughout. This problem goes away once we
|
|
|
|
// switch to libc++.
|
2018-08-10 23:28:24 +02:00
|
|
|
reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time);
|
2018-07-31 03:51:55 +02:00
|
|
|
} else {
|
|
|
|
reconnect_cv_.wait(lock);
|
|
|
|
}
|
2018-04-20 19:31:29 +02:00
|
|
|
|
|
|
|
if (!running_) return;
|
2018-08-10 23:44:54 +02:00
|
|
|
|
|
|
|
// Scan the whole list for kicked transports, so that we immediately handle an explicit
|
|
|
|
// disconnect request.
|
|
|
|
bool kicked = false;
|
|
|
|
for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) {
|
|
|
|
if (it->transport->kicked()) {
|
|
|
|
D("transport %s was kicked. giving up on it.", it->transport->serial.c_str());
|
|
|
|
remove_transport(it->transport);
|
|
|
|
it = reconnect_queue_.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
kicked = true;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:51:55 +02:00
|
|
|
if (reconnect_queue_.empty()) continue;
|
|
|
|
|
2018-08-10 23:44:54 +02:00
|
|
|
// Go back to sleep if we either woke up spuriously, or we were woken up to remove
|
|
|
|
// a kicked transport, and the first transport isn't ready for reconnection yet.
|
2018-07-31 03:51:55 +02:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2018-08-10 23:28:24 +02:00
|
|
|
if (reconnect_queue_.begin()->reconnect_time > now) {
|
2018-07-31 03:51:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:28:24 +02:00
|
|
|
attempt = *reconnect_queue_.begin();
|
|
|
|
reconnect_queue_.erase(reconnect_queue_.begin());
|
2018-04-20 19:31:29 +02:00
|
|
|
}
|
2018-07-19 06:18:27 +02:00
|
|
|
D("attempting to reconnect %s", attempt.transport->serial.c_str());
|
2018-04-20 19:31:29 +02:00
|
|
|
|
2018-08-30 20:37:00 +02:00
|
|
|
switch (attempt.transport->Reconnect()) {
|
|
|
|
case ReconnectResult::Retry: {
|
|
|
|
D("attempting to reconnect %s failed.", attempt.transport->serial.c_str());
|
|
|
|
if (attempt.attempts_left == 0) {
|
|
|
|
D("transport %s exceeded the number of retry attempts. giving up on it.",
|
|
|
|
attempt.transport->serial.c_str());
|
|
|
|
remove_transport(attempt.transport);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(reconnect_mutex_);
|
|
|
|
reconnect_queue_.emplace(ReconnectAttempt{
|
|
|
|
attempt.transport,
|
|
|
|
std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout,
|
|
|
|
attempt.attempts_left - 1});
|
2018-04-20 19:31:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-30 20:37:00 +02:00
|
|
|
case ReconnectResult::Success:
|
|
|
|
D("reconnection to %s succeeded.", attempt.transport->serial.c_str());
|
|
|
|
register_transport(attempt.transport);
|
|
|
|
continue;
|
2018-04-20 19:31:29 +02:00
|
|
|
|
2018-08-30 20:37:00 +02:00
|
|
|
case ReconnectResult::Abort:
|
|
|
|
D("cancelling reconnection attempt to %s.", attempt.transport->serial.c_str());
|
|
|
|
remove_transport(attempt.transport);
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-20 19:31:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto& reconnect_handler = *new ReconnectHandler();
|
|
|
|
|
2018-08-01 03:28:32 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
} // namespace
|
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
TransportId NextTransportId() {
|
|
|
|
static std::atomic<TransportId> next(1);
|
|
|
|
return next++;
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:47:44 +01:00
|
|
|
void Connection::Reset() {
|
|
|
|
LOG(INFO) << "Connection::Reset(): stopping";
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:24:58 +01:00
|
|
|
BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection)
|
|
|
|
: underlying_(std::move(connection)) {}
|
|
|
|
|
|
|
|
BlockingConnectionAdapter::~BlockingConnectionAdapter() {
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing";
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingConnectionAdapter::Start() {
|
2018-04-03 21:55:18 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (started_) {
|
|
|
|
LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_
|
|
|
|
<< "): started multiple times";
|
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
StartReadThread();
|
2018-02-16 22:24:58 +01:00
|
|
|
|
|
|
|
write_thread_ = std::thread([this]() {
|
|
|
|
LOG(INFO) << this->transport_name_ << ": write thread spawning";
|
|
|
|
while (true) {
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2019-02-12 22:59:03 +01:00
|
|
|
ScopedLockAssertion assume_locked(mutex_);
|
2018-04-03 21:55:18 +02:00
|
|
|
cv_.wait(lock, [this]() REQUIRES(mutex_) {
|
|
|
|
return this->stopped_ || !this->write_queue_.empty();
|
|
|
|
});
|
|
|
|
|
2018-02-16 22:24:58 +01:00
|
|
|
if (this->stopped_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<apacket> packet = std::move(this->write_queue_.front());
|
|
|
|
this->write_queue_.pop_front();
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
if (!this->underlying_->Write(packet.get())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); });
|
|
|
|
});
|
2018-04-03 21:55:18 +02:00
|
|
|
|
|
|
|
started_ = true;
|
2018-02-16 22:24:58 +01:00
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
void BlockingConnectionAdapter::StartReadThread() {
|
|
|
|
read_thread_ = std::thread([this]() {
|
|
|
|
LOG(INFO) << this->transport_name_ << ": read thread spawning";
|
|
|
|
while (true) {
|
|
|
|
auto packet = std::make_unique<apacket>();
|
|
|
|
if (!underlying_->Read(packet.get())) {
|
|
|
|
PLOG(INFO) << this->transport_name_ << ": read failed";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool got_stls_cmd = false;
|
|
|
|
if (packet->msg.command == A_STLS) {
|
|
|
|
got_stls_cmd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_callback_(this, std::move(packet));
|
|
|
|
|
|
|
|
// If we received the STLS packet, we are about to perform the TLS
|
|
|
|
// handshake. So this read thread must stop and resume after the
|
|
|
|
// handshake completes otherwise this will interfere in the process.
|
|
|
|
if (got_stls_cmd) {
|
|
|
|
LOG(INFO) << this->transport_name_
|
|
|
|
<< ": Received STLS packet. Stopping read thread.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockingConnectionAdapter::DoTlsHandshake(RSA* key, std::string* auth_key) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (read_thread_.joinable()) {
|
|
|
|
read_thread_.join();
|
|
|
|
}
|
|
|
|
bool success = this->underlying_->DoTlsHandshake(key, auth_key);
|
|
|
|
StartReadThread();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:47:44 +01:00
|
|
|
void BlockingConnectionAdapter::Reset() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (!started_) {
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stopped_) {
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_
|
|
|
|
<< "): already stopped";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): resetting";
|
|
|
|
this->underlying_->Reset();
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:24:58 +01:00
|
|
|
void BlockingConnectionAdapter::Stop() {
|
2018-04-03 21:55:18 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (!started_) {
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started";
|
|
|
|
return;
|
|
|
|
}
|
2018-02-16 22:24:58 +01:00
|
|
|
|
2018-04-03 21:55:18 +02:00
|
|
|
if (stopped_) {
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_
|
|
|
|
<< "): already stopped";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopped_ = true;
|
|
|
|
}
|
2018-02-16 22:24:58 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping";
|
|
|
|
|
|
|
|
this->underlying_->Close();
|
|
|
|
this->cv_.notify_one();
|
2018-04-03 21:55:18 +02:00
|
|
|
|
|
|
|
// Move the threads out into locals with the lock taken, and then unlock to let them exit.
|
|
|
|
std::thread read_thread;
|
|
|
|
std::thread write_thread;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
read_thread = std::move(read_thread_);
|
|
|
|
write_thread = std::move(write_thread_);
|
|
|
|
}
|
|
|
|
|
|
|
|
read_thread.join();
|
|
|
|
write_thread.join();
|
2018-02-16 22:24:58 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped";
|
|
|
|
std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) {
|
|
|
|
{
|
2018-04-03 21:55:18 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2018-02-16 22:24:58 +01:00
|
|
|
write_queue_.emplace_back(std::move(packet));
|
|
|
|
}
|
|
|
|
|
|
|
|
cv_.notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
FdConnection::FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
|
|
|
|
|
|
|
|
FdConnection::~FdConnection() {}
|
|
|
|
|
|
|
|
bool FdConnection::DispatchRead(void* buf, size_t len) {
|
|
|
|
if (tls_ != nullptr) {
|
|
|
|
// The TlsConnection doesn't allow 0 byte reads
|
|
|
|
if (len == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return tls_->ReadFully(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReadFdExactly(fd_.get(), buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FdConnection::DispatchWrite(void* buf, size_t len) {
|
|
|
|
if (tls_ != nullptr) {
|
|
|
|
// The TlsConnection doesn't allow 0 byte writes
|
|
|
|
if (len == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return tls_->WriteFully(std::string_view(reinterpret_cast<const char*>(buf), len));
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteFdExactly(fd_.get(), buf, len);
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:32:46 +01:00
|
|
|
bool FdConnection::Read(apacket* packet) {
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
if (!DispatchRead(&packet->msg, sizeof(amessage))) {
|
2018-01-29 05:32:46 +01:00
|
|
|
D("remote local: read terminated (message)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
if (packet->msg.data_length > MAX_PAYLOAD) {
|
2018-02-02 23:38:04 +01:00
|
|
|
D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
packet->payload.resize(packet->msg.data_length);
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
if (!DispatchRead(&packet->payload[0], packet->payload.size())) {
|
2018-01-29 05:32:46 +01:00
|
|
|
D("remote local: terminated (data)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FdConnection::Write(apacket* packet) {
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
if (!DispatchWrite(&packet->msg, sizeof(packet->msg))) {
|
2018-01-29 05:32:46 +01:00
|
|
|
D("remote local: write terminated");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
if (packet->msg.data_length) {
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
if (!DispatchWrite(&packet->payload[0], packet->msg.data_length)) {
|
2018-02-06 03:49:10 +01:00
|
|
|
D("remote local: write terminated");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:32:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
bool FdConnection::DoTlsHandshake(RSA* key, std::string* auth_key) {
|
|
|
|
bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new());
|
|
|
|
if (!EVP_PKEY_set1_RSA(evp_pkey.get(), key)) {
|
|
|
|
LOG(ERROR) << "EVP_PKEY_set1_RSA failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto x509 = GenerateX509Certificate(evp_pkey.get());
|
|
|
|
auto x509_str = X509ToPEMString(x509.get());
|
|
|
|
auto evp_str = Key::ToPEMString(evp_pkey.get());
|
2020-03-24 17:50:59 +01:00
|
|
|
|
2020-03-27 02:16:36 +01:00
|
|
|
int osh = cast_handle_to_int(adb_get_os_handle(fd_));
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
#if ADB_HOST
|
|
|
|
tls_ = TlsConnection::Create(TlsConnection::Role::Client,
|
|
|
|
#else
|
|
|
|
tls_ = TlsConnection::Create(TlsConnection::Role::Server,
|
|
|
|
#endif
|
2020-03-24 17:50:59 +01:00
|
|
|
x509_str, evp_str, osh);
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
CHECK(tls_);
|
|
|
|
#if ADB_HOST
|
|
|
|
// TLS 1.3 gives the client no message if the server rejected the
|
|
|
|
// certificate. This will enable a check in the tls connection to check
|
|
|
|
// whether the client certificate got rejected. Note that this assumes
|
|
|
|
// that, on handshake success, the server speaks first.
|
|
|
|
tls_->EnableClientPostHandshakeCheck(true);
|
|
|
|
// Add callback to set the certificate when server issues the
|
|
|
|
// CertificateRequest.
|
|
|
|
tls_->SetCertificateCallback(adb_tls_set_certificate);
|
|
|
|
// Allow any server certificate
|
|
|
|
tls_->SetCertVerifyCallback([](X509_STORE_CTX*) { return 1; });
|
|
|
|
#else
|
|
|
|
// Add callback to check certificate against a list of known public keys
|
|
|
|
tls_->SetCertVerifyCallback(
|
|
|
|
[auth_key](X509_STORE_CTX* ctx) { return adbd_tls_verify_cert(ctx, auth_key); });
|
|
|
|
// Add the list of allowed client CA issuers
|
|
|
|
auto ca_list = adbd_tls_client_ca_list();
|
|
|
|
tls_->SetClientCAList(ca_list.get());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto err = tls_->DoHandshake();
|
|
|
|
if (err == TlsError::Success) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tls_.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:32:46 +01:00
|
|
|
void FdConnection::Close() {
|
|
|
|
adb_shutdown(fd_.get());
|
|
|
|
fd_.reset();
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:31:44 +02:00
|
|
|
void send_packet(apacket* p, atransport* t) {
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.magic = p->msg.command ^ 0xffffffff;
|
2017-12-07 20:40:00 +01:00
|
|
|
// compute a checksum for connection/auth packets for compatibility reasons
|
|
|
|
if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
|
|
|
|
p->msg.data_check = 0;
|
|
|
|
} else {
|
|
|
|
p->msg.data_check = calculate_apacket_checksum(p);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-14 03:15:16 +02:00
|
|
|
if (t == nullptr) {
|
2018-10-19 22:59:44 +02:00
|
|
|
LOG(FATAL) << "Transport is null";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-02-16 22:24:58 +01:00
|
|
|
if (t->Write(p) != 0) {
|
2018-07-19 06:18:27 +02:00
|
|
|
D("%s: failed to enqueue packet, closing transport", t->serial.c_str());
|
2018-02-16 22:24:58 +01:00
|
|
|
t->Kick();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:47:44 +01:00
|
|
|
void kick_transport(atransport* t, bool reset) {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2016-04-05 22:50:44 +02:00
|
|
|
// As kick_transport() can be called from threads without guarantee that t is valid,
|
|
|
|
// check if the transport is in transport_list first.
|
2017-08-17 01:57:01 +02:00
|
|
|
//
|
|
|
|
// TODO(jmgao): WTF? Is this actually true?
|
2016-04-05 22:50:44 +02:00
|
|
|
if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
|
2019-03-28 23:47:44 +01:00
|
|
|
if (reset) {
|
|
|
|
t->Reset();
|
|
|
|
} else {
|
|
|
|
t->Kick();
|
|
|
|
}
|
2016-04-05 22:50:44 +02:00
|
|
|
}
|
2018-08-10 23:44:54 +02:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
reconnect_handler.CheckForKicked();
|
|
|
|
#endif
|
2015-08-27 21:03:11 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
static int transport_registration_send = -1;
|
|
|
|
static int transport_registration_recv = -1;
|
2018-05-14 20:14:33 +02:00
|
|
|
static fdevent* transport_registration_fde;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
|
|
|
|
/* this adds support required by the 'track-devices' service.
|
|
|
|
* this is used to send the content of "list_transport" to any
|
|
|
|
* number of client connections that want it through a single
|
|
|
|
* live TCP connection
|
|
|
|
*/
|
|
|
|
struct device_tracker {
|
2016-11-22 23:32:34 +01:00
|
|
|
asocket socket;
|
2018-02-13 02:24:00 +01:00
|
|
|
bool update_needed = false;
|
|
|
|
bool long_output = false;
|
|
|
|
device_tracker* next = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* linked list of all device trackers */
|
2016-11-22 23:32:34 +01:00
|
|
|
static device_tracker* device_tracker_list;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static void device_tracker_remove(device_tracker* tracker) {
|
|
|
|
device_tracker** pnode = &device_tracker_list;
|
|
|
|
device_tracker* node = *pnode;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2009-03-04 04:32:55 +01:00
|
|
|
while (node) {
|
|
|
|
if (node == tracker) {
|
|
|
|
*pnode = node->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pnode = &node->next;
|
2016-11-22 23:32:34 +01:00
|
|
|
node = *pnode;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static void device_tracker_close(asocket* socket) {
|
|
|
|
device_tracker* tracker = (device_tracker*)socket;
|
|
|
|
asocket* peer = socket->peer;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
D("device tracker %p removed", tracker);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (peer) {
|
2018-07-14 03:15:16 +02:00
|
|
|
peer->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
peer->close(peer);
|
|
|
|
}
|
|
|
|
device_tracker_remove(tracker);
|
2018-02-13 02:24:00 +01:00
|
|
|
delete tracker;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
static int device_tracker_enqueue(asocket* socket, apacket::payload_type) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* you can't read from a device tracker, close immediately */
|
|
|
|
device_tracker_close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
static int device_tracker_send(device_tracker* tracker, const std::string& string) {
|
|
|
|
asocket* peer = tracker->socket.peer;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
apacket::payload_type data;
|
2018-02-01 22:17:50 +01:00
|
|
|
data.resize(4 + string.size());
|
|
|
|
char buf[5];
|
|
|
|
snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
|
|
|
|
memcpy(&data[0], buf, 4);
|
|
|
|
memcpy(&data[4], string.data(), string.size());
|
|
|
|
return peer->enqueue(peer, std::move(data));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
static void device_tracker_ready(asocket* socket) {
|
|
|
|
device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
// We want to send the device list when the tracker connects
|
|
|
|
// for the first time, even if no update occurred.
|
2017-08-15 03:57:54 +02:00
|
|
|
if (tracker->update_needed) {
|
|
|
|
tracker->update_needed = false;
|
2019-07-31 23:13:57 +02:00
|
|
|
device_tracker_send(tracker, list_transports(tracker->long_output));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 03:57:54 +02:00
|
|
|
asocket* create_device_tracker(bool long_output) {
|
2018-02-13 02:24:00 +01:00
|
|
|
device_tracker* tracker = new device_tracker();
|
2018-10-19 22:59:44 +02:00
|
|
|
if (tracker == nullptr) LOG(FATAL) << "cannot allocate device tracker";
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
D("device tracker %p created", tracker);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
tracker->socket.enqueue = device_tracker_enqueue;
|
2016-11-22 23:32:34 +01:00
|
|
|
tracker->socket.ready = device_tracker_ready;
|
|
|
|
tracker->socket.close = device_tracker_close;
|
2017-08-15 03:57:54 +02:00
|
|
|
tracker->update_needed = true;
|
|
|
|
tracker->long_output = long_output;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
tracker->next = device_tracker_list;
|
2009-03-04 04:32:55 +01:00
|
|
|
device_tracker_list = tracker;
|
|
|
|
|
|
|
|
return &tracker->socket;
|
|
|
|
}
|
|
|
|
|
2017-05-04 07:37:10 +02:00
|
|
|
// Check if all of the USB transports are connected.
|
|
|
|
bool iterate_transports(std::function<bool(const atransport*)> fn) {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2017-05-04 07:37:10 +02:00
|
|
|
for (const auto& t : transport_list) {
|
|
|
|
if (!fn(t)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& t : pending_list) {
|
|
|
|
if (!fn(t)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
// Call this function each time the transport list has changed.
|
|
|
|
void update_transports() {
|
2017-05-04 07:37:10 +02:00
|
|
|
update_transport_status();
|
|
|
|
|
|
|
|
// Notify `adb track-devices` clients.
|
2015-05-01 02:32:03 +02:00
|
|
|
device_tracker* tracker = device_tracker_list;
|
|
|
|
while (tracker != nullptr) {
|
|
|
|
device_tracker* next = tracker->next;
|
|
|
|
// This may destroy the tracker if the connection is closed.
|
2019-07-31 23:13:57 +02:00
|
|
|
device_tracker_send(tracker, list_transports(tracker->long_output));
|
2009-03-04 04:32:55 +01:00
|
|
|
tracker = next;
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 02:32:03 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#else
|
2015-05-01 02:32:03 +02:00
|
|
|
|
|
|
|
void update_transports() {
|
|
|
|
// Nothing to do on the device side.
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-01 02:32:03 +02:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
#endif // ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
struct tmsg {
|
|
|
|
atransport* transport;
|
|
|
|
int action;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static int transport_read_action(int fd, struct tmsg* m) {
|
|
|
|
char* p = (char*)m;
|
|
|
|
int len = sizeof(*m);
|
|
|
|
int r;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
while (len > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
r = adb_read(fd, p, len);
|
2016-11-22 23:32:34 +01:00
|
|
|
if (r > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= r;
|
2016-11-22 23:32:34 +01:00
|
|
|
p += r;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("transport_read_action: on fd %d: %s", fd, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static int transport_write_action(int fd, struct tmsg* m) {
|
|
|
|
char* p = (char*)m;
|
|
|
|
int len = sizeof(*m);
|
|
|
|
int r;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
while (len > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
r = adb_write(fd, p, len);
|
2016-11-22 23:32:34 +01:00
|
|
|
if (r > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
len -= r;
|
2016-11-22 23:32:34 +01:00
|
|
|
p += r;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("transport_write_action: on fd %d: %s", fd, strerror(errno));
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:24:58 +01:00
|
|
|
static void transport_registration_func(int _fd, unsigned ev, void*) {
|
2009-03-04 04:32:55 +01:00
|
|
|
tmsg m;
|
2016-11-22 23:32:34 +01:00
|
|
|
atransport* t;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
if (!(ev & FDE_READ)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
if (transport_read_action(_fd, &m)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
PLOG(FATAL) << "cannot read transport registration socket";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t = m.transport;
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
if (m.action == 0) {
|
2018-07-19 06:18:27 +02:00
|
|
|
D("transport: %s deleting", t->serial.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-09-21 21:37:10 +02:00
|
|
|
{
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2016-09-21 21:37:10 +02:00
|
|
|
transport_list.remove(t);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-19 01:46:31 +02:00
|
|
|
delete t;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
update_transports();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-08 18:37:44 +02:00
|
|
|
/* don't create transport threads for inaccessible devices */
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
if (t->GetConnectionState() != kCsNoPerm) {
|
2018-04-20 19:31:29 +02:00
|
|
|
// The connection gets a reference to the atransport. It will release it
|
|
|
|
// upon a read/write error.
|
2018-04-25 17:56:41 +02:00
|
|
|
t->connection()->SetTransportName(t->serial_name());
|
|
|
|
t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) {
|
2018-02-16 22:24:58 +01:00
|
|
|
if (!check_header(p.get(), t)) {
|
2018-07-19 06:18:27 +02:00
|
|
|
D("%s: remote read: bad header", t->serial.c_str());
|
2018-02-16 22:24:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-08 18:37:44 +02:00
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get());
|
2018-02-16 22:24:58 +01:00
|
|
|
apacket* packet = p.release();
|
|
|
|
|
|
|
|
// TODO: Does this need to run on the main thread?
|
|
|
|
fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); });
|
|
|
|
return true;
|
|
|
|
});
|
2018-04-25 17:56:41 +02:00
|
|
|
t->connection()->SetErrorCallback([t](Connection*, const std::string& error) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
LOG(INFO) << t->serial_name() << ": connection terminated: " << error;
|
2018-02-16 22:24:58 +01:00
|
|
|
fdevent_run_on_main_thread([t]() {
|
|
|
|
handle_offline(t);
|
2019-12-09 22:45:31 +01:00
|
|
|
transport_destroy(t);
|
2018-02-16 22:24:58 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-25 17:56:41 +02:00
|
|
|
t->connection()->Start();
|
2018-02-16 22:24:58 +01:00
|
|
|
#if ADB_HOST
|
|
|
|
send_connect(t);
|
|
|
|
#endif
|
2009-08-08 18:37:44 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-09-21 21:37:10 +02:00
|
|
|
{
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2018-04-20 19:31:29 +02:00
|
|
|
auto it = std::find(pending_list.begin(), pending_list.end(), t);
|
|
|
|
if (it != pending_list.end()) {
|
|
|
|
pending_list.remove(t);
|
|
|
|
transport_list.push_front(t);
|
|
|
|
}
|
2016-09-21 21:37:10 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
update_transports();
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:28:32 +02:00
|
|
|
#if ADB_HOST
|
2018-04-20 19:31:29 +02:00
|
|
|
void init_reconnect_handler(void) {
|
|
|
|
reconnect_handler.Start();
|
|
|
|
}
|
2018-08-01 03:28:32 +02:00
|
|
|
#endif
|
2018-04-20 19:31:29 +02:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
void init_transport_registration(void) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int s[2];
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
if (adb_socketpair(s)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
PLOG(FATAL) << "cannot open transport registration socketpair";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("socketpair: (%d,%d)", s[0], s[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
transport_registration_send = s[0];
|
|
|
|
transport_registration_recv = s[1];
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
transport_registration_fde =
|
2018-07-14 03:15:16 +02:00
|
|
|
fdevent_create(transport_registration_recv, transport_registration_func, nullptr);
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_set(transport_registration_fde, FDE_READ);
|
2017-05-09 22:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void kick_all_transports() {
|
2018-08-01 03:28:32 +02:00
|
|
|
#if ADB_HOST
|
2018-04-20 19:31:29 +02:00
|
|
|
reconnect_handler.Stop();
|
2018-08-01 03:28:32 +02:00
|
|
|
#endif
|
2017-05-09 22:43:35 +02:00
|
|
|
// To avoid only writing part of a packet to a transport after exit, kick all transports.
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2017-05-09 22:43:35 +02:00
|
|
|
for (auto t : transport_list) {
|
|
|
|
t->Kick();
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
void kick_all_tcp_tls_transports() {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
|
|
|
for (auto t : transport_list) {
|
|
|
|
if (t->IsTcpDevice() && t->use_tls) {
|
|
|
|
t->Kick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
void kick_all_transports_by_auth_key(std::string_view auth_key) {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
|
|
|
for (auto t : transport_list) {
|
|
|
|
if (auth_key == t->auth_key) {
|
|
|
|
t->Kick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* the fdevent select pump is single threaded */
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
void register_transport(atransport* transport) {
|
2009-03-04 04:32:55 +01:00
|
|
|
tmsg m;
|
|
|
|
m.transport = transport;
|
|
|
|
m.action = 1;
|
2018-07-19 06:18:27 +02:00
|
|
|
D("transport: %s registered", transport->serial.c_str());
|
2016-11-22 23:32:34 +01:00
|
|
|
if (transport_write_action(transport_registration_send, &m)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
PLOG(FATAL) << "cannot write transport registration socket";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static void remove_transport(atransport* transport) {
|
2009-03-04 04:32:55 +01:00
|
|
|
tmsg m;
|
|
|
|
m.transport = transport;
|
|
|
|
m.action = 0;
|
2018-07-19 06:18:27 +02:00
|
|
|
D("transport: %s removed", transport->serial.c_str());
|
2016-11-22 23:32:34 +01:00
|
|
|
if (transport_write_action(transport_registration_send, &m)) {
|
2018-10-19 22:59:44 +02:00
|
|
|
PLOG(FATAL) << "cannot write transport registration socket";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:45:31 +01:00
|
|
|
static void transport_destroy(atransport* t) {
|
2018-04-20 19:31:29 +02:00
|
|
|
check_main_thread();
|
2015-08-27 21:03:11 +02:00
|
|
|
CHECK(t != nullptr);
|
2016-09-21 21:37:10 +02:00
|
|
|
|
2017-09-13 22:40:57 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2019-12-09 22:45:31 +01:00
|
|
|
LOG(INFO) << "destroying transport " << t->serial_name();
|
|
|
|
t->connection()->Stop();
|
2018-08-01 03:28:32 +02:00
|
|
|
#if ADB_HOST
|
2019-12-09 22:45:31 +01:00
|
|
|
if (t->IsTcpDevice() && !t->kicked()) {
|
|
|
|
D("transport: %s destroy (attempting reconnection)", t->serial.c_str());
|
2018-12-04 10:07:50 +01:00
|
|
|
|
2019-12-09 22:45:31 +01:00
|
|
|
// We need to clear the transport's keys, so that on the next connection, it tries
|
|
|
|
// again from the beginning.
|
|
|
|
t->ResetKeys();
|
|
|
|
reconnect_handler.TrackTransport(t);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-01 03:28:32 +02:00
|
|
|
#endif
|
|
|
|
|
2019-12-09 22:45:31 +01:00
|
|
|
D("transport: %s destroy (kicking and closing)", t->serial.c_str());
|
|
|
|
remove_transport(t);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual,
|
2016-11-22 23:32:34 +01:00
|
|
|
bool sanitize_qual) {
|
2018-07-19 06:18:27 +02:00
|
|
|
if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */
|
|
|
|
return qual.empty();
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
if (qual.empty()) return 0;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
const char* ptr = to_test.c_str();
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
if (prefix) {
|
|
|
|
while (*prefix) {
|
2018-07-19 06:18:27 +02:00
|
|
|
if (*prefix++ != *ptr++) return 0;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
for (char ch : qual) {
|
2016-11-22 23:32:34 +01:00
|
|
|
if (sanitize_qual && !isalnum(ch)) ch = '_';
|
2018-07-19 06:18:27 +02:00
|
|
|
if (ch != *ptr++) return 0;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
/* Everything matched so far. Return true if *ptr is a NUL. */
|
|
|
|
return !*ptr;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
|
|
|
|
bool* is_ambiguous, std::string* error_out,
|
|
|
|
bool accept_any_state) {
|
2015-10-07 23:55:10 +02:00
|
|
|
atransport* result = nullptr;
|
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
if (transport_id != 0) {
|
|
|
|
*error_out =
|
|
|
|
android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
|
|
|
|
} else if (serial) {
|
2015-10-07 23:55:10 +02:00
|
|
|
*error_out = android::base::StringPrintf("device '%s' not found", serial);
|
|
|
|
} else if (type == kTransportLocal) {
|
|
|
|
*error_out = "no emulators found";
|
|
|
|
} else if (type == kTransportAny) {
|
|
|
|
*error_out = "no devices/emulators found";
|
|
|
|
} else {
|
|
|
|
*error_out = "no devices found";
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 22:50:51 +02:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(transport_lock);
|
2015-10-07 23:55:10 +02:00
|
|
|
for (const auto& t : transport_list) {
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
if (t->GetConnectionState() == kCsNoPerm) {
|
2015-12-03 00:14:31 +01:00
|
|
|
*error_out = UsbNoPermissionsLongHelpText();
|
2009-08-08 19:53:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-08-08 18:37:44 +02:00
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
if (transport_id) {
|
|
|
|
if (t->id == transport_id) {
|
|
|
|
result = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (serial) {
|
2016-03-01 17:58:26 +01:00
|
|
|
if (t->MatchesTarget(serial)) {
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
if (result) {
|
2015-06-23 22:00:32 +02:00
|
|
|
*error_out = "more than one device";
|
2015-10-07 23:55:10 +02:00
|
|
|
if (is_ambiguous) *is_ambiguous = true;
|
|
|
|
result = nullptr;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-04-20 20:21:14 +02:00
|
|
|
result = t;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-05-05 22:10:43 +02:00
|
|
|
if (type == kTransportUsb && t->type == kTransportUsb) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (result) {
|
2015-06-23 22:00:32 +02:00
|
|
|
*error_out = "more than one device";
|
2015-10-07 23:55:10 +02:00
|
|
|
if (is_ambiguous) *is_ambiguous = true;
|
|
|
|
result = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = t;
|
2015-05-05 22:10:43 +02:00
|
|
|
} else if (type == kTransportLocal && t->type == kTransportLocal) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (result) {
|
2015-06-23 22:00:32 +02:00
|
|
|
*error_out = "more than one emulator";
|
2015-10-07 23:55:10 +02:00
|
|
|
if (is_ambiguous) *is_ambiguous = true;
|
|
|
|
result = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = t;
|
2015-05-05 22:10:43 +02:00
|
|
|
} else if (type == kTransportAny) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (result) {
|
2015-06-23 22:00:32 +02:00
|
|
|
*error_out = "more than one device/emulator";
|
2015-10-07 23:55:10 +02:00
|
|
|
if (is_ambiguous) *is_ambiguous = true;
|
|
|
|
result = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 21:37:10 +02:00
|
|
|
lock.unlock();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-05-05 01:04:49 +02:00
|
|
|
if (result && !accept_any_state) {
|
|
|
|
// The caller requires an active transport.
|
|
|
|
// Make sure that we're actually connected.
|
|
|
|
ConnectionState state = result->GetConnectionState();
|
|
|
|
switch (state) {
|
|
|
|
case kCsConnecting:
|
|
|
|
*error_out = "device still connecting";
|
|
|
|
result = nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCsAuthorizing:
|
|
|
|
*error_out = "device still authorizing";
|
|
|
|
result = nullptr;
|
|
|
|
break;
|
2015-04-17 07:54:44 +02:00
|
|
|
|
2018-05-05 01:04:49 +02:00
|
|
|
case kCsUnauthorized: {
|
|
|
|
*error_out = "device unauthorized.\n";
|
|
|
|
char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
|
|
|
|
*error_out += "This adb server's $ADB_VENDOR_KEYS is ";
|
|
|
|
*error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
|
|
|
|
*error_out += "\n";
|
|
|
|
*error_out += "Try 'adb kill-server' if that seems wrong.\n";
|
|
|
|
*error_out += "Otherwise check for a confirmation dialog on your device.";
|
|
|
|
result = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case kCsOffline:
|
|
|
|
*error_out = "device offline";
|
|
|
|
result = nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
2015-06-23 22:00:32 +02:00
|
|
|
*error_out = "success";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) {
|
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2019-02-12 22:59:03 +01:00
|
|
|
ScopedLockAssertion assume_locked(mutex_);
|
2018-04-17 23:25:04 +02:00
|
|
|
return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) {
|
|
|
|
return connection_established_ready_;
|
|
|
|
}) && connection_established_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionWaitable::SetConnectionEstablished(bool success) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (connection_established_ready_) return;
|
|
|
|
connection_established_ready_ = true;
|
|
|
|
connection_established_ = success;
|
|
|
|
D("connection established with %d", success);
|
|
|
|
}
|
|
|
|
cv_.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
atransport::~atransport() {
|
|
|
|
// If the connection callback had not been run before, run it now.
|
|
|
|
SetConnectionEstablished(false);
|
|
|
|
}
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
int atransport::Write(apacket* p) {
|
2018-04-25 17:56:41 +02:00
|
|
|
return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1;
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 23:47:44 +01:00
|
|
|
void atransport::Reset() {
|
|
|
|
if (!kicked_.exchange(true)) {
|
|
|
|
LOG(INFO) << "resetting transport " << this << " " << this->serial;
|
|
|
|
this->connection()->Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 20:22:34 +02:00
|
|
|
void atransport::Kick() {
|
2018-04-20 19:31:29 +02:00
|
|
|
if (!kicked_.exchange(true)) {
|
2019-03-28 23:47:44 +01:00
|
|
|
LOG(INFO) << "kicking transport " << this << " " << this->serial;
|
2018-04-25 17:56:41 +02:00
|
|
|
this->connection()->Stop();
|
2016-04-18 20:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
ConnectionState atransport::GetConnectionState() const {
|
|
|
|
return connection_state_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void atransport::SetConnectionState(ConnectionState state) {
|
|
|
|
check_main_thread();
|
|
|
|
connection_state_ = state;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:56:41 +02:00
|
|
|
void atransport::SetConnection(std::unique_ptr<Connection> connection) {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
connection_ = std::shared_ptr<Connection>(std::move(connection));
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:44:23 +01:00
|
|
|
std::string atransport::connection_state_name() const {
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
ConnectionState state = GetConnectionState();
|
|
|
|
switch (state) {
|
2016-11-22 23:32:34 +01:00
|
|
|
case kCsOffline:
|
|
|
|
return "offline";
|
|
|
|
case kCsBootloader:
|
|
|
|
return "bootloader";
|
|
|
|
case kCsDevice:
|
|
|
|
return "device";
|
|
|
|
case kCsHost:
|
|
|
|
return "host";
|
|
|
|
case kCsRecovery:
|
|
|
|
return "recovery";
|
2019-04-08 08:24:03 +02:00
|
|
|
case kCsRescue:
|
|
|
|
return "rescue";
|
2016-11-22 23:32:34 +01:00
|
|
|
case kCsNoPerm:
|
|
|
|
return UsbNoPermissionsShortHelpText();
|
|
|
|
case kCsSideload:
|
|
|
|
return "sideload";
|
|
|
|
case kCsUnauthorized:
|
|
|
|
return "unauthorized";
|
2018-05-05 01:04:49 +02:00
|
|
|
case kCsAuthorizing:
|
|
|
|
return "authorizing";
|
|
|
|
case kCsConnecting:
|
|
|
|
return "connecting";
|
2016-11-22 23:32:34 +01:00
|
|
|
default:
|
|
|
|
return "unknown";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:12:28 +02:00
|
|
|
void atransport::update_version(int version, size_t payload) {
|
|
|
|
protocol_version = std::min(version, A_VERSION);
|
|
|
|
max_payload = std::min(payload, MAX_PAYLOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
int atransport::get_protocol_version() const {
|
|
|
|
return protocol_version;
|
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
int atransport::get_tls_version() const {
|
|
|
|
return tls_version;
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:12:28 +02:00
|
|
|
size_t atransport::get_max_payload() const {
|
|
|
|
return max_payload;
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
const FeatureSet& supported_features() {
|
2015-09-22 19:43:08 +02:00
|
|
|
// Local static allocation to avoid global non-POD variables.
|
|
|
|
static const FeatureSet* features = new FeatureSet{
|
2019-02-20 22:01:40 +01:00
|
|
|
kFeatureShell2,
|
|
|
|
kFeatureCmd,
|
|
|
|
kFeatureStat2,
|
2019-08-07 23:23:17 +02:00
|
|
|
kFeatureLs2,
|
2019-02-20 22:01:40 +01:00
|
|
|
kFeatureFixedPushMkdir,
|
|
|
|
kFeatureApex,
|
|
|
|
kFeatureAbb,
|
|
|
|
kFeatureFixedPushSymlinkTimestamp,
|
2019-02-21 23:22:51 +01:00
|
|
|
kFeatureAbbExec,
|
2019-07-11 23:15:32 +02:00
|
|
|
kFeatureRemountShell,
|
2020-02-14 02:01:39 +01:00
|
|
|
kFeatureTrackApp,
|
2020-03-05 04:34:08 +01:00
|
|
|
kFeatureSendRecv2,
|
|
|
|
kFeatureSendRecv2Brotli,
|
2020-03-27 06:02:03 +01:00
|
|
|
kFeatureSendRecv2LZ4,
|
2020-03-31 08:25:16 +02:00
|
|
|
kFeatureSendRecv2DryRunSend,
|
2018-11-09 07:39:43 +01:00
|
|
|
// Increment ADB_SERVER_VERSION when adding a feature that adbd needs
|
|
|
|
// to know about. Otherwise, the client can be stuck running an old
|
|
|
|
// version of the server even after upgrading their copy of adb.
|
|
|
|
// (http://b/24370690)
|
2015-09-22 19:43:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return *features;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FeatureSetToString(const FeatureSet& features) {
|
2018-09-05 21:13:11 +02:00
|
|
|
return android::base::Join(features, ',');
|
2015-09-22 19:43:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FeatureSet StringToFeatureSet(const std::string& features_string) {
|
2015-09-25 22:04:21 +02:00
|
|
|
if (features_string.empty()) {
|
|
|
|
return FeatureSet();
|
|
|
|
}
|
|
|
|
|
2018-09-05 21:13:11 +02:00
|
|
|
auto names = android::base::Split(features_string, ",");
|
2015-09-22 19:43:08 +02:00
|
|
|
return FeatureSet(names.begin(), names.end());
|
2015-05-18 22:06:53 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 22:35:42 +02:00
|
|
|
bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
|
2016-11-22 23:32:34 +01:00
|
|
|
return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
|
2015-09-30 22:35:42 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:06:53 +02:00
|
|
|
bool atransport::has_feature(const std::string& feature) const {
|
|
|
|
return features_.count(feature) > 0;
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:43:08 +02:00
|
|
|
void atransport::SetFeatures(const std::string& features_string) {
|
|
|
|
features_ = StringToFeatureSet(features_string);
|
2015-05-18 22:06:53 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 00:09:44 +02:00
|
|
|
void atransport::AddDisconnect(adisconnect* disconnect) {
|
|
|
|
disconnects_.push_back(disconnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void atransport::RemoveDisconnect(adisconnect* disconnect) {
|
|
|
|
disconnects_.remove(disconnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void atransport::RunDisconnects() {
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& disconnect : disconnects_) {
|
2015-08-29 00:09:44 +02:00
|
|
|
disconnect->func(disconnect->opaque, this);
|
|
|
|
}
|
|
|
|
disconnects_.clear();
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:58:26 +01:00
|
|
|
bool atransport::MatchesTarget(const std::string& target) const {
|
2018-07-19 06:18:27 +02:00
|
|
|
if (!serial.empty()) {
|
2016-03-01 17:58:26 +01:00
|
|
|
if (target == serial) {
|
|
|
|
return true;
|
|
|
|
} else if (type == kTransportLocal) {
|
|
|
|
// Local transports can match [tcp:|udp:]<hostname>[:port].
|
|
|
|
const char* local_target_ptr = target.c_str();
|
|
|
|
|
|
|
|
// For fastboot compatibility, ignore protocol prefixes.
|
|
|
|
if (android::base::StartsWith(target, "tcp:") ||
|
2016-11-22 23:32:34 +01:00
|
|
|
android::base::StartsWith(target, "udp:")) {
|
2016-03-01 17:58:26 +01:00
|
|
|
local_target_ptr += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse our |serial| and the given |target| to check if the hostnames and ports match.
|
|
|
|
std::string serial_host, error;
|
|
|
|
int serial_port = -1;
|
2016-11-22 23:32:34 +01:00
|
|
|
if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
|
2016-03-01 17:58:26 +01:00
|
|
|
// |target| may omit the port to default to ours.
|
|
|
|
std::string target_host;
|
|
|
|
int target_port = serial_port;
|
|
|
|
if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
|
|
|
|
nullptr, &error) &&
|
2016-11-22 23:32:34 +01:00
|
|
|
serial_host == target_host && serial_port == target_port) {
|
2016-03-01 17:58:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
return (target == devpath) || qual_match(target, "product:", product, false) ||
|
|
|
|
qual_match(target, "model:", model, true) ||
|
|
|
|
qual_match(target, "device:", device, false);
|
2016-03-01 17:58:26 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
void atransport::SetConnectionEstablished(bool success) {
|
|
|
|
connection_waitable_->SetConnectionEstablished(success);
|
|
|
|
}
|
|
|
|
|
2018-08-30 20:37:00 +02:00
|
|
|
ReconnectResult atransport::Reconnect() {
|
2018-04-20 19:31:29 +02:00
|
|
|
return reconnect_(this);
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
// We use newline as our delimiter, make sure to never output it.
|
|
|
|
static std::string sanitize(std::string str, bool alphanumeric) {
|
|
|
|
auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
|
|
|
|
: [](const char c) { return c == '\n'; };
|
|
|
|
std::replace_if(str.begin(), str.end(), pred, '_');
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-07-19 06:18:27 +02:00
|
|
|
static void append_transport_info(std::string* result, const char* key, const std::string& value,
|
2017-08-17 01:57:01 +02:00
|
|
|
bool alphanumeric) {
|
2018-07-19 06:18:27 +02:00
|
|
|
if (value.empty()) {
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
return;
|
2015-05-01 02:32:03 +02:00
|
|
|
}
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
*result += ' ';
|
|
|
|
*result += key;
|
2017-08-17 01:57:01 +02:00
|
|
|
*result += sanitize(value, alphanumeric);
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
static void append_transport(const atransport* t, std::string* result, bool long_listing) {
|
2018-07-19 06:18:27 +02:00
|
|
|
std::string serial = t->serial;
|
|
|
|
if (serial.empty()) {
|
2015-05-07 01:48:52 +02:00
|
|
|
serial = "(no serial number)";
|
2015-05-01 02:32:03 +02:00
|
|
|
}
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
|
|
|
|
if (!long_listing) {
|
2015-05-01 02:32:03 +02:00
|
|
|
*result += serial;
|
|
|
|
*result += '\t';
|
|
|
|
*result += t->connection_state_name();
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
} else {
|
2018-07-19 06:18:27 +02:00
|
|
|
android::base::StringAppendF(result, "%-22s %s", serial.c_str(),
|
|
|
|
t->connection_state_name().c_str());
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
append_transport_info(result, "", t->devpath, false);
|
|
|
|
append_transport_info(result, "product:", t->product, false);
|
|
|
|
append_transport_info(result, "model:", t->model, true);
|
|
|
|
append_transport_info(result, "device:", t->device, false);
|
2017-08-17 01:57:01 +02:00
|
|
|
|
|
|
|
// Put id at the end, so that anyone parsing the output here can always find it by scanning
|
|
|
|
// backwards from newlines, even with hypothetical devices named 'transport_id:1'.
|
|
|
|
*result += " transport_id:";
|
|
|
|
*result += std::to_string(t->id);
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
2015-05-01 02:32:03 +02:00
|
|
|
*result += '\n';
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
std::string list_transports(bool long_listing) {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2017-12-17 11:56:07 +01:00
|
|
|
|
|
|
|
auto sorted_transport_list = transport_list;
|
|
|
|
sorted_transport_list.sort([](atransport*& x, atransport*& y) {
|
|
|
|
if (x->type != y->type) {
|
|
|
|
return x->type < y->type;
|
|
|
|
}
|
2018-07-19 06:18:27 +02:00
|
|
|
return x->serial < y->serial;
|
2017-12-17 11:56:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
for (const auto& t : sorted_transport_list) {
|
2015-05-01 02:32:03 +02:00
|
|
|
append_transport(t, &result, long_listing);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-01 02:32:03 +02:00
|
|
|
return result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 23:47:44 +01:00
|
|
|
void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset) {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2016-10-27 23:01:08 +02:00
|
|
|
for (auto& t : transport_list) {
|
|
|
|
if (predicate(t)) {
|
2019-03-28 23:47:44 +01:00
|
|
|
if (reset) {
|
|
|
|
t->Reset();
|
|
|
|
} else {
|
|
|
|
t->Kick();
|
|
|
|
}
|
2016-10-27 23:01:08 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 23:01:08 +02:00
|
|
|
|
|
|
|
/* hack for osx */
|
2019-03-28 23:47:44 +01:00
|
|
|
void close_usb_devices(bool reset) {
|
|
|
|
close_usb_devices([](const atransport*) { return true; }, reset);
|
2016-10-27 23:01:08 +02:00
|
|
|
}
|
2016-11-22 23:32:34 +01:00
|
|
|
#endif // ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-08-09 01:20:14 +02:00
|
|
|
bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
atransport::ReconnectCallback reconnect, bool use_tls, int* error) {
|
2018-04-20 19:31:29 +02:00
|
|
|
atransport* t = new atransport(std::move(reconnect), kCsOffline);
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
t->use_tls = use_tls;
|
2011-01-06 14:11:07 +01:00
|
|
|
|
2018-08-02 22:58:24 +02:00
|
|
|
D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port);
|
2018-07-26 02:21:49 +02:00
|
|
|
if (init_socket_transport(t, std::move(s), port, local) < 0) {
|
2015-05-19 01:46:31 +02:00
|
|
|
delete t;
|
2018-08-09 01:20:14 +02:00
|
|
|
if (error) *error = errno;
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2017-08-17 22:50:51 +02:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(transport_lock);
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& transport : pending_list) {
|
2018-08-02 22:58:24 +02:00
|
|
|
if (serial == transport->serial) {
|
2016-04-30 01:53:52 +02:00
|
|
|
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
2016-11-22 23:32:34 +01:00
|
|
|
<< " is already in pending_list and fails to register";
|
2015-05-19 01:46:31 +02:00
|
|
|
delete t;
|
2018-08-09 01:20:14 +02:00
|
|
|
if (error) *error = EALREADY;
|
|
|
|
return false;
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& transport : transport_list) {
|
2018-08-02 22:58:24 +02:00
|
|
|
if (serial == transport->serial) {
|
2016-04-30 01:53:52 +02:00
|
|
|
VLOG(TRANSPORT) << "socket transport " << transport->serial
|
2016-11-22 23:32:34 +01:00
|
|
|
<< " is already in transport_list and fails to register";
|
2015-05-19 01:46:31 +02:00
|
|
|
delete t;
|
2018-08-09 01:20:14 +02:00
|
|
|
if (error) *error = EALREADY;
|
|
|
|
return false;
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:58:24 +02:00
|
|
|
t->serial = std::move(serial);
|
2015-05-19 01:46:31 +02:00
|
|
|
pending_list.push_front(t);
|
2016-09-21 21:37:10 +02:00
|
|
|
|
|
|
|
lock.unlock();
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2018-04-17 23:25:04 +02:00
|
|
|
auto waitable = t->connection_waitable();
|
2009-03-04 04:32:55 +01:00
|
|
|
register_transport(t);
|
2018-04-17 23:25:04 +02:00
|
|
|
|
2018-05-02 02:12:16 +02:00
|
|
|
if (local == 1) {
|
|
|
|
// Do not wait for emulator transports.
|
2018-08-09 01:20:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!waitable->WaitForConnection(std::chrono::seconds(10))) {
|
|
|
|
if (error) *error = ETIMEDOUT;
|
|
|
|
return false;
|
2018-05-02 02:12:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 01:20:14 +02:00
|
|
|
if (t->GetConnectionState() == kCsUnauthorized) {
|
|
|
|
if (error) *error = EPERM;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2009-10-12 05:04:18 +02:00
|
|
|
#if ADB_HOST
|
2016-11-22 23:32:34 +01:00
|
|
|
atransport* find_transport(const char* serial) {
|
2015-05-19 01:46:31 +02:00
|
|
|
atransport* result = nullptr;
|
2009-10-12 05:04:18 +02:00
|
|
|
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2015-08-27 21:03:11 +02:00
|
|
|
for (auto& t : transport_list) {
|
2018-07-19 06:18:27 +02:00
|
|
|
if (strcmp(serial, t->serial.c_str()) == 0) {
|
2015-05-19 01:46:31 +02:00
|
|
|
result = t;
|
2009-10-12 05:04:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-05-19 01:46:31 +02:00
|
|
|
}
|
2009-10-12 05:04:18 +02:00
|
|
|
|
2015-05-19 01:46:31 +02:00
|
|
|
return result;
|
2009-10-12 05:04:18 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:03:11 +02:00
|
|
|
void kick_all_tcp_devices() {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2015-08-27 21:03:11 +02:00
|
|
|
for (auto& t : transport_list) {
|
2016-04-30 01:53:52 +02:00
|
|
|
if (t->IsTcpDevice()) {
|
2015-08-31 20:50:24 +02:00
|
|
|
// Kicking breaks the read_transport thread of this transport out of any read, then
|
|
|
|
// the read_transport thread will notify the main thread to make this transport
|
|
|
|
// offline. Then the main thread will notify the write_transport thread to exit.
|
2015-08-27 21:03:11 +02:00
|
|
|
// Finally, this transport will be closed and freed in the main thread.
|
2016-04-18 20:22:34 +02:00
|
|
|
t->Kick();
|
2010-05-24 16:44:35 +02:00
|
|
|
}
|
2015-05-19 01:46:31 +02:00
|
|
|
}
|
2018-08-10 23:44:54 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
reconnect_handler.CheckForKicked();
|
|
|
|
#endif
|
2010-05-24 16:44:35 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 05:04:18 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-28 02:09:56 +01:00
|
|
|
#if ADB_HOST
|
2016-11-22 23:32:34 +01:00
|
|
|
void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
|
|
|
|
unsigned writeable) {
|
2018-04-20 19:31:29 +02:00
|
|
|
atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm);
|
2015-05-19 01:46:31 +02:00
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
init_usb_transport(t, usb);
|
2016-11-22 23:32:34 +01:00
|
|
|
if (serial) {
|
2018-07-19 06:18:27 +02:00
|
|
|
t->serial = serial;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-19 01:46:31 +02:00
|
|
|
|
|
|
|
if (devpath) {
|
2018-07-19 06:18:27 +02:00
|
|
|
t->devpath = devpath;
|
2012-04-20 20:21:14 +02:00
|
|
|
}
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2016-09-21 21:37:10 +02:00
|
|
|
{
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2016-09-21 21:37:10 +02:00
|
|
|
pending_list.push_front(t);
|
|
|
|
}
|
2013-03-30 02:22:36 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
register_transport(t);
|
|
|
|
}
|
2020-03-28 02:09:56 +01:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
#if ADB_HOST
|
2015-05-19 01:43:57 +02:00
|
|
|
// This should only be used for transports with connection_state == kCsNoPerm.
|
2016-11-22 23:32:34 +01:00
|
|
|
void unregister_usb_transport(usb_handle* usb) {
|
2017-08-17 22:50:51 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(transport_lock);
|
2018-01-29 05:32:46 +01:00
|
|
|
transport_list.remove_if([usb](atransport* t) {
|
2018-12-11 22:11:52 +01:00
|
|
|
return t->GetUsbHandle() == usb && t->GetConnectionState() == kCsNoPerm;
|
2018-01-29 05:32:46 +01:00
|
|
|
});
|
2009-08-08 18:37:44 +02:00
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-05-17 00:02:45 +02:00
|
|
|
bool check_header(apacket* p, atransport* t) {
|
2016-11-22 23:32:34 +01:00
|
|
|
if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
|
|
|
|
<< ", magic = " << p->msg.magic;
|
2017-05-17 00:02:45 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:32:34 +01:00
|
|
|
if (p->msg.data_length > t->get_max_payload()) {
|
|
|
|
VLOG(RWX) << "check_header(): " << p->msg.data_length
|
|
|
|
<< " atransport::max_payload = " << t->get_max_payload();
|
2017-05-17 00:02:45 +02:00
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 00:02:45 +02:00
|
|
|
return true;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-06 04:02:29 +02:00
|
|
|
#if ADB_HOST
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
std::shared_ptr<RSA> atransport::Key() {
|
|
|
|
if (keys_.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<RSA> result = keys_[0];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-19 07:00:12 +02:00
|
|
|
std::shared_ptr<RSA> atransport::NextKey() {
|
2018-12-04 10:07:50 +01:00
|
|
|
if (keys_.empty()) {
|
|
|
|
LOG(INFO) << "fetching keys for transport " << this->serial_name();
|
|
|
|
keys_ = adb_auth_get_private_keys();
|
|
|
|
|
|
|
|
// We should have gotten at least one key: the one that's automatically generated.
|
|
|
|
CHECK(!keys_.empty());
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
} else {
|
|
|
|
keys_.pop_front();
|
2018-12-04 10:07:50 +01:00
|
|
|
}
|
2016-06-30 02:42:01 +02:00
|
|
|
|
2016-08-19 07:00:12 +02:00
|
|
|
std::shared_ptr<RSA> result = keys_[0];
|
2016-06-30 02:42:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
2018-12-04 10:07:50 +01:00
|
|
|
|
|
|
|
void atransport::ResetKeys() {
|
|
|
|
keys_.clear();
|
|
|
|
}
|
2016-10-06 04:02:29 +02:00
|
|
|
#endif
|