Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ce4a267 authored by Joshua Duong's avatar Joshua Duong Committed by Gerrit Code Review
Browse files

Merge changes from topic "adb-mdns"

* changes:
  adb pair: allow passing password as command-line argument.
  Change adb auto-connect to use mdns instance name.
  'adb pair' by mdns instance name.
  'adb connect' by mDNS service name.
  Add mDNS service instance name parser.
parents 64979d37 dcba5c92
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -218,6 +218,7 @@ cc_library_host_static {
        "client/usb_dispatch.cpp",
        "client/transport_local.cpp",
        "client/transport_mdns.cpp",
        "client/mdns_utils.cpp",
        "client/transport_usb.cpp",
        "client/pairing/pairing_client.cpp",
    ],
@@ -264,7 +265,10 @@ cc_library_host_static {
cc_test_host {
    name: "adb_test",
    defaults: ["adb_defaults"],
    srcs: libadb_test_srcs,
    srcs: libadb_test_srcs + [
        "client/mdns_utils_test.cpp",
    ],

    static_libs: [
        "libadb_crypto_static",
        "libadb_host",
+14 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include <optional>
#include <string>

#include "adb.h"
@@ -30,6 +31,19 @@ bool adb_wifi_is_known_host(const std::string& host);
std::string mdns_check();
std::string mdns_list_discovered_services();

struct MdnsInfo {
    std::string service_name;
    std::string service_type;
    std::string addr;
    uint16_t port = 0;

    MdnsInfo(std::string_view name, std::string_view type, std::string_view addr, uint16_t port)
        : service_name(name), service_type(type), addr(addr), port(port) {}
};

std::optional<MdnsInfo> mdns_get_connect_service_info(std::string_view name);
std::optional<MdnsInfo> mdns_get_pairing_service_info(std::string_view name);

#else  // !ADB_HOST

struct AdbdAuthContext;
+20 −12
Original line number Diff line number Diff line
@@ -179,6 +179,9 @@ bool adb_wifi_is_known_host(const std::string& host) {

void adb_wifi_pair_device(const std::string& host, const std::string& password,
                          std::string& response) {
    auto mdns_info = mdns_get_pairing_service_info(host);

    if (!mdns_info.has_value()) {
        // Check the address for a valid address and port.
        std::string parsed_host;
        std::string err;
@@ -191,6 +194,7 @@ void adb_wifi_pair_device(const std::string& host, const std::string& password,
            response = "Invalid port while parsing address [" + host + "]";
            return;
        }
    }

    auto priv_key = adb_auth_get_user_privkey();
    auto x509_cert = GenerateX509Certificate(priv_key.get());
@@ -220,7 +224,11 @@ void adb_wifi_pair_device(const std::string& host, const std::string& password,

    PairingResultWaiter waiter;
    std::unique_lock<std::mutex> lock(waiter.mutex_);
    if (!client->Start(host, waiter.OnResult, &waiter)) {
    if (!client->Start(mdns_info.has_value()
                               ? android::base::StringPrintf("%s:%d", mdns_info->addr.c_str(),
                                                             mdns_info->port)
                               : host,
                       waiter.OnResult, &waiter)) {
        response = "Failed: Unable to start pairing client.";
        return;
    }
+11 −6
Original line number Diff line number Diff line
@@ -104,7 +104,8 @@ static void help() {
        " connect HOST[:PORT]      connect to a device via TCP/IP [default port=5555]\n"
        " disconnect [HOST[:PORT]]\n"
        "     disconnect from given TCP/IP device [default port=5555], or all\n"
        " pair HOST[:PORT]         pair with a device for secure TCP/IP communication\n"
        " pair HOST[:PORT] [PAIRING CODE]\n"
        "     pair with a device for secure TCP/IP communication\n"
        " forward --list           list all forward socket connections\n"
        " forward [--no-rebind] LOCAL REMOTE\n"
        "     forward socket connection using:\n"
@@ -1735,14 +1736,18 @@ int adb_commandline(int argc, const char** argv) {
    } else if (!strcmp(argv[0], "abb")) {
        return adb_abb(argc, argv);
    } else if (!strcmp(argv[0], "pair")) {
        if (argc != 2) error_exit("usage: adb pair <host>[:<port>]");
        if (argc < 2 || argc > 3) error_exit("usage: adb pair HOST[:PORT] [PAIRING CODE]");

        std::string password;
        if (argc == 2) {
            printf("Enter pairing code: ");
            fflush(stdout);
            if (!std::getline(std::cin, password) || password.empty()) {
                error_exit("No pairing code provided");
            }
        } else {
            password = argv[2];
        }
        std::string query =
                android::base::StringPrintf("host:pair:%s:%s", password.c_str(), argv[1]);

+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.
 */

#include "client/mdns_utils.h"

#include <android-base/strings.h>

namespace mdns {

// <Instance>.<Service>.<Domain>
std::optional<MdnsInstance> mdns_parse_instance_name(std::string_view name) {
    CHECK(!name.empty());

    // Return the whole name if it doesn't fall under <Instance>.<Service>.<Domain> or
    // <Instance>.<Service>
    bool has_local_suffix = false;
    // Strip the local suffix, if any
    {
        std::string local_suffix = ".local";
        local_suffix += android::base::EndsWith(name, ".") ? "." : "";

        if (android::base::ConsumeSuffix(&name, local_suffix)) {
            if (name.empty()) {
                return std::nullopt;
            }
            has_local_suffix = true;
        }
    }

    std::string transport;
    // Strip the transport suffix, if any
    {
        std::string add_dot = (!has_local_suffix && android::base::EndsWith(name, ".")) ? "." : "";
        std::array<std::string, 2> transport_suffixes{"._tcp", "._udp"};

        for (const auto& t : transport_suffixes) {
            if (android::base::ConsumeSuffix(&name, t + add_dot)) {
                if (name.empty()) {
                    return std::nullopt;
                }
                transport = t.substr(1);
                break;
            }
        }

        if (has_local_suffix && transport.empty()) {
            return std::nullopt;
        }
    }

    if (!has_local_suffix && transport.empty()) {
        return std::make_optional<MdnsInstance>(name, "", "");
    }

    // Split the service name from the instance name
    auto pos = name.rfind(".");
    if (pos == 0 || pos == std::string::npos || pos == name.size() - 1) {
        return std::nullopt;
    }

    return std::make_optional<MdnsInstance>(name.substr(0, pos), name.substr(pos + 1), transport);
}

}  // namespace mdns
Loading