Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions base/cvd/adb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ cc_binary(
"adb_unique_fd.h",
"adb_utils.cpp",
"adb_utils.h",
"adb_wifi.h",
"apacket_reader.cpp",
"apacket_reader.h",
"client/adb_client.cpp",
"client/adb_client.h",
"client/adb_install.cpp",
"client/adb_install.h",
"client/adb_wifi.cpp",
"client/adb_wifi.h",
"client/auth.cpp",
"client/bugreport.cpp",
"client/bugreport.h",
Expand All @@ -76,17 +76,12 @@ cc_binary(
"client/mdns_tracker.h",
"client/mdns_utils.cpp",
"client/mdns_utils.h",
"client/openscreen/mdns_service_info.cpp",
"client/openscreen/mdns_service_info.h",
"client/openscreen/platform/logging.cpp",
"client/openscreen/platform/task_runner.cpp",
"client/openscreen/platform/task_runner.h",
"client/openscreen/platform/udp_socket.cpp",
"client/pairing/pairing_client.cpp",
"client/pairing/pairing_client.h",
"client/transport_client.h",
"client/transport_emulator.cpp",
"client/transport_mdns.cpp",
"client/transport_mdns.h",
"client/transport_usb.cpp",
"client/usb.h",
"client/usb_libusb.cpp",
Expand All @@ -98,6 +93,8 @@ cc_binary(
"client/usb_libusb_inhouse_hotplug.cpp",
"client/usb_libusb_inhouse_hotplug.h",
"client/usb_linux.cpp",
"client/usb_linux_netlink.cpp",
"client/usb_linux_netlink.h",
"compression_utils.h",
"crypto/include/adb/crypto/key.h",
"crypto/include/adb/crypto/rsa_2048_key.h",
Expand All @@ -112,7 +109,6 @@ cc_binary(
"fdevent/fdevent_epoll.cpp",
"fdevent/fdevent_epoll.h",
"file_sync_protocol.h",
"incfs/util/map_ptr.h",
"libcrypto_utils/android_pubkey.cpp",
"libcrypto_utils/include/crypto_utils/android_pubkey.h",
"pairing_auth/aes_128_gcm.cpp",
Expand Down Expand Up @@ -183,13 +179,13 @@ cc_binary(
"@android_system_libbase//:no_destructor",
"@android_system_libbase//:parsebool",
"@android_system_libbase//:parsenetaddress",
"@android_system_libbase//:properties",
"@android_system_libbase//:scopeguard",
"@android_system_libbase//:thread_annotations",
"@boringssl//:crypto",
"@boringssl//:ssl",
"@brotli//:brotlidec",
"@brotli//:brotlienc",
"@libopenscreen",
"@libusb",
"@libzip",
"@lz4//:lz4_frame",
Expand Down
40 changes: 30 additions & 10 deletions base/cvd/adb/adb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ void handle_packet(apacket* p, atransport* t) {
local_socket_ack(s, acked_bytes);
} else {
D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s", p->msg.arg0,
p->msg.arg1, s->peer->id, p->msg.arg1, t->serial.c_str());
p->msg.arg1, s->peer->id, p->msg.arg1, t->name.c_str());
}
} else {
// When receiving A_OKAY from device for A_OPEN request, the host server may
Expand All @@ -606,7 +606,7 @@ void handle_packet(apacket* p, atransport* t) {
*/
if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s", p->msg.arg1,
t->serial.c_str(), s->peer->transport->serial.c_str());
t->name.c_str(), s->peer->transport->name.c_str());
} else {
s->close(s);
}
Expand Down Expand Up @@ -1252,6 +1252,10 @@ void adb_set_reject_kill_server(bool value) {
g_reject_kill_server = value;
}

bool is_usb_enabled() {
return !getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0;
}

static bool handle_mdns_request(std::string_view service, int reply_fd) {
if (!android::base::ConsumePrefix(&service, "mdns:")) {
return false;
Expand Down Expand Up @@ -1352,15 +1356,28 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty

if (service == "server-status") {
adb::proto::AdbServerStatus status;
if (is_libusb_enabled()) {
status.set_usb_backend(adb::proto::AdbServerStatus::LIBUSB);

if (is_usb_enabled()) {
if (is_libusb_enabled()) {
status.set_usb_backend(adb::proto::AdbServerStatus::LIBUSB);
} else {
status.set_usb_backend(adb::proto::AdbServerStatus::NATIVE);
}
} else {
status.set_usb_backend(adb::proto::AdbServerStatus::NATIVE);
status.set_usb_backend(adb::proto::AdbServerStatus::USB_DISABLED);
}
status.set_usb_backend_forced(getenv("ADB_LIBUSB") != nullptr);

status.set_mdns_backend(adb::proto::AdbServerStatus::OPENSCREEN);
status.set_mdns_backend_forced(false);
if (mdns::is_enabled()) {
if (mdns::should_use_openscreen()) {
status.set_mdns_backend(adb::proto::AdbServerStatus::OPENSCREEN);
} else {
status.set_mdns_backend(adb::proto::AdbServerStatus::LIBADBMDNS);
}
} else {
status.set_mdns_backend(adb::proto::AdbServerStatus::MDNS_DISABLED);
}
status.set_mdns_backend_forced(getenv("ADB_MDNS_OPENSCREEN") != nullptr);

status.set_version(std::string(PLATFORM_TOOLS_VERSION));
status.set_build(android::build::GetBuildNumber());
Expand All @@ -1371,7 +1388,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty
status.set_trace_level(get_trace_setting());
status.set_mdns_enabled(mdns::is_enabled());
status.set_keystore_path(adb_auth_get_userkey_path());
status.set_known_hosts_path(get_user_known_hosts_path());
status.set_known_hosts_path(known_wifi_hosts_file.KeyStorePath());

std::string server_status_string;
status.SerializeToString(&server_status_string);
Expand Down Expand Up @@ -1485,7 +1502,7 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty
s->transport ? s->transport
: acquire_one_transport(type, serial, transport_id, nullptr, &error);
if (t) {
SendOkay(reply_fd, !t->serial.empty() ? t->serial : "unknown");
SendOkay(reply_fd, !t->name.empty() ? t->name : "unknown");
} else {
SendFail(reply_fd, error);
}
Expand Down Expand Up @@ -1522,7 +1539,10 @@ HostRequestResult handle_host_request(std::string_view service, TransportType ty
if (!ParseUint(&port, service)) {
LOG(ERROR) << "received invalid port for emulator: " << service;
} else {
connect_emulator(port);
// We are running on the fdevent thread. Connect is a long operation which
// can potentially block 10s until connection is established. We must run
// it on another thread.
std::thread([=] { connect_emulator(port); }).detach();
}

/* we don't even need to send a reply */
Expand Down
8 changes: 3 additions & 5 deletions base/cvd/adb/adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,11 @@ void put_apacket(apacket* p);
#define ADB_DBC_CLASS 0xDC
#define ADB_DBC_SUBCLASS 0x2

bool connect_emulator(int port);
int connect_emulator_arbitrary_ports(int console_port, int adb_port, std::string* error);

extern const char* adb_device_banner;

#define CHUNK_SIZE (64 * 1024)

// Argument delimeter for adb abb command.
#define ABB_ARG_DELIMETER ('\0')
#define ABB_ARG_DELIMITER ('\0')

#if !ADB_HOST
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
Expand Down Expand Up @@ -254,6 +250,8 @@ void adb_wait_for_device_initialization();
// When ssh-forwarding to a remote adb server, kill-server is almost never what you actually want,
// and unfortunately, many other tools issue it. This adds a knob to reject kill-servers.
void adb_set_reject_kill_server(bool reject);

bool is_usb_enabled();
#endif

void usb_init();
1 change: 1 addition & 0 deletions base/cvd/adb/adb_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>

#include <openssl/rsa.h>
#include <openssl/stack.h>

/* AUTH packets first argument */
/* Request */
Expand Down
2 changes: 1 addition & 1 deletion base/cvd/adb/adb_listeners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ std::string format_listeners() EXCLUDES(listener_list_mutex) {
// Entries from "adb reverse" have no serial.
android::base::StringAppendF(
&result, "%s %s %s\n",
!l->transport->serial.empty() ? l->transport->serial.c_str() : "(reverse)",
!l->transport->name.empty() ? l->transport->name.c_str() : "(reverse)",
l->local_name.c_str(), l->connect_to.c_str());
}
return result;
Expand Down
27 changes: 14 additions & 13 deletions base/cvd/adb/adb_mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,36 @@ void config_auto_connect_services() {

} // namespace

std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type) {
for (int i = 0; i < kNumADBDNSServices; ++i) {
if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) {
return i;
}
}
return std::nullopt;
}

bool adb_DNSServiceShouldAutoConnect(std::string_view reg_type, std::string_view service_name) {
config_auto_connect_services();

// Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services.
std::optional<int> index = adb_DNSServiceIndexByName(reg_type);
if (!index ||
(index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex)) {
if (!index) {
VLOG(MDNS) << "Unknown index for '" << reg_type << "'";
return false;
}

if (!g_autoconn_allowedlist.contains(*index)) {
D("Auto-connect for reg_type '%s' disabled", reg_type.data());
VLOG(MDNS) << "Auto-connect for reg_type '" << reg_type << "' disabled";
return false;
}
// Ignore adb-EMULATOR* service names, as it interferes with the
// emulator ports that are already connected.
if (android::base::StartsWith(service_name, "adb-EMULATOR")) {
LOG(INFO) << "Ignoring emulator transport service [" << service_name << "]";
VLOG(MDNS) << "Ignoring emulator transport service [" << service_name << "]";
return false;
}
return true;
}

#endif // ADB_HOST

std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type) {
for (int i = 0; i < kNumADBDNSServices; ++i) {
if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) {
return i;
}
}
return std::nullopt;
}
18 changes: 8 additions & 10 deletions base/cvd/adb/adb_mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#define ADB_MDNS_SERVICE_TYPE "adb"
#define ADB_MDNS_TLS_PAIRING_TYPE "adb-tls-pairing"
#define ADB_MDNS_TLS_CONNECT_TYPE "adb-tls-connect"
#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp")
#define MDNS_SERVICE_PROTOCOL "_tcp"
#define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "." MDNS_SERVICE_PROTOCOL)

#define ADB_SERVICE_TCP ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE)
#define ADB_SERVICE_TLS ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)
Expand All @@ -47,31 +48,28 @@ constexpr int kADBSecureConnectServiceRefIndex = 2;
constexpr int kNumADBDNSServices = 3;

extern const char* _Nonnull kADBDNSServices[kNumADBDNSServices];
extern const char* _Nonnull kADBDNSServiceTxtRecords[kNumADBDNSServices];

#if ADB_HOST
#include "client/openscreen/mdns_service_info.h"
#include "client/discovered_services.h"
// ADB Secure DNS service interface. Used to query what ADB Secure DNS services have been
// resolved, and to run some kind of callback for each one.
using adb_secure_foreach_service_callback = std::function<void(const mdns::ServiceInfo& si)>;
using adb_secure_foreach_service_callback = std::function<void(const ServiceInfo& si)>;

// Tries to connect to a |service_name| if found. Returns true if found and
// connected, false otherwise.
bool adb_secure_connect_by_service_name(const std::string& instance_name);

// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise
// std::nullopt.
std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type);
// Returns true if auto-connect is allowed for |service_name| and |instance_name|.
// See ADB_MDNS_AUTO_CONNECT environment variable for more info.
bool adb_DNSServiceShouldAutoConnect(std::string_view service_name, std::string_view instance_name);

std::string mdns_check();
std::string mdns_list_discovered_services();

std::optional<mdns::ServiceInfo> mdns_get_connect_service_info(const std::string& name);
std::optional<mdns::ServiceInfo> mdns_get_pairing_service_info(const std::string& name);

// Return the location where adb host stores paired devices
std::string get_user_known_hosts_path();
#endif // ADB_HOST

// Returns the index in kADBDNSServices array if |reg_type| matches a service name, otherwise
// std::nullopt.
std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type);
2 changes: 2 additions & 0 deletions base/cvd/adb/adb_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static void setup_trace_mask() {
return;
}

// LINT.IfChange
std::unordered_map<std::string, int> trace_flags = {
{"1", -1},
{"all", -1},
Expand All @@ -133,6 +134,7 @@ static void setup_trace_mask() {
{"mdns", MDNS},
{"mdns-stack", MDNS_STACK},
};
// LINT.ThenChange(packages/modules/adb/docs/user/adb.1.md)

// Make sure we check for ALL enum in AdbTrace.
size_t num_flags = trace_flags.size() - 2;
Expand Down
2 changes: 1 addition & 1 deletion base/cvd/adb/adb_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ std::string adb_get_android_dir_path() {
std::string android_dir = user_dir + OS_PATH_SEPARATOR + ".android";
struct stat buf;
if (stat(android_dir.c_str(), &buf) == -1) {
if (adb_mkdir(android_dir, 0750) == -1) {
if (adb_mkdir(android_dir, 0750) == -1 && errno != EEXIST) {
PLOG(FATAL) << "Cannot mkdir '" << android_dir << "'";
}
}
Expand Down
6 changes: 3 additions & 3 deletions base/cvd/adb/client/adb_install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ static int install_app_incremental(int argc, const char** argv, bool wait, bool

auto files = incremental::Files{argv + first_apk, argv + last_apk + 1};
if (silent) {
// For a silent installation we want to do the lightweight check first and bail early and
// quietly if it fails.
if (!incremental::can_install(files)) {
// For a silent installation we want to do the lightweight check first and return early if
// we shouldn't use incremental install for the given files.
if (!incremental::should_use_incremental_by_default(files)) {
return -1;
}
}
Expand Down
Loading
Loading