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

Commit 80a734f1 authored by Josh Gao's avatar Josh Gao Committed by Gerrit Code Review
Browse files

Merge "Allow the adb server to bind on ::1"

parents adebae2c 8539cb3f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -272,7 +272,9 @@ int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_
        if (hostname.empty() && gListenAll) {
            result = network_inaddr_any_server(port, SOCK_STREAM, error);
        } else if (tcp_host_is_local(hostname)) {
            result = network_loopback_server(port, SOCK_STREAM, error);
            result = network_loopback_server(port, SOCK_STREAM, error, true);
        } else if (hostname == "::1") {
            result = network_loopback_server(port, SOCK_STREAM, error, false);
        } else {
            // TODO: Implement me.
            *error = "listening on specified hostname currently unsupported";
+1 −1
Original line number Diff line number Diff line
@@ -19,4 +19,4 @@
#include <string>

int network_loopback_client(int port, int type, std::string* error);
int network_loopback_server(int port, int type, std::string* error);
int network_loopback_server(int port, int type, std::string* error, bool prefer_ipv4);
+7 −4
Original line number Diff line number Diff line
@@ -119,12 +119,15 @@ static int _network_loopback_server(bool ipv6, int port, int type, std::string*
    return s.release();
}

int network_loopback_server(int port, int type, std::string* error) {
    int rc = _network_loopback_server(false, port, type, error);
int network_loopback_server(int port, int type, std::string* error, bool prefer_ipv4) {
    int rc = -1;
    if (prefer_ipv4) {
        rc = _network_loopback_server(false, port, type, error);
    }

    // Only attempt to listen on IPv6 if IPv4 is unavailable.
    // Only attempt to listen on IPv6 if IPv4 is unavailable or prefer_ipv4 is false
    // We don't want to start an IPv6 server if there's already an IPv4 one running.
    if (rc == -1 && (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)) {
    if (rc == -1 && (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || !prefer_ipv4)) {
        return _network_loopback_server(true, port, type, error);
    }
    return rc;
+3 −2
Original line number Diff line number Diff line
@@ -920,7 +920,8 @@ static int _network_server(int port, int type, u_long interface_address, std::st
    return fd;
}

int network_loopback_server(int port, int type, std::string* error) {
int network_loopback_server(int port, int type, std::string* error, bool prefer_ipv4) {
    // TODO implement IPv6 support on windows
    return _network_server(port, type, INADDR_LOOPBACK, error);
}

@@ -1132,7 +1133,7 @@ int adb_socketpair(int sv[2]) {
    int local_port = -1;
    std::string error;

    server = network_loopback_server(0, SOCK_STREAM, &error);
    server = network_loopback_server(0, SOCK_STREAM, &error, true);
    if (server < 0) {
        D("adb_socketpair: failed to create server: %s", error.c_str());
        goto fail;
+31 −0
Original line number Diff line number Diff line
@@ -281,6 +281,37 @@ class ServerTest(unittest.TestCase):
            subprocess.check_output(["adb", "-P", str(port), "kill-server"],
                                    stderr=subprocess.STDOUT)

    @unittest.skipUnless(
        os.name == "posix",
        "adb doesn't yet support IPv6 on Windows",
    )
    def test_starts_on_ipv6_localhost(self):
        """
        Tests that the server can start up on ::1 and that it's accessible
        """
        server_port = 5037
        # Kill any existing server on this non-default port.
        subprocess.check_output(
            ["adb", "-P", str(server_port), "kill-server"],
            stderr=subprocess.STDOUT,
        )
        try:
            subprocess.check_output(
                ["adb", "-L", "tcp:[::1]:{}".format(server_port), "server"],
                stderr=subprocess.STDOUT,
            )
            with fake_adbd() as (port, _):
                with adb_connect(self, serial="localhost:{}".format(port)):
                    pass
        finally:
            # If we started a server, kill it.
            subprocess.check_output(
                ["adb", "-P", str(server_port), "kill-server"],
                stderr=subprocess.STDOUT,
            )




class EmulatorTest(unittest.TestCase):
    """Tests for the emulator connection."""