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

Commit 19192712 authored by Mike Yu's avatar Mike Yu
Browse files

Move connectTimeout to DnsTlsSocket

Because DnsTlsServer is not freshed along with creating or
destroying a network, move the flag into Experiments class
so that a new value of the flag can take effect on
already-existing networks.

Bug: 149445907
Bug: 120182528
Test: cd packages/modules/DnsResolver && atest
Change-Id: I37afed9606765e7715ecb23e4946305ac79a21e3
parent e93d9ae1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ bool AddressComparator::operator() (const DnsTlsServer& x, const DnsTlsServer& y

// Returns a tuple of references to the elements of s.
auto make_tie(const DnsTlsServer& s) {
    return std::tie(s.ss, s.name, s.protocol, s.connectTimeout);
    return std::tie(s.ss, s.name, s.protocol);
}

bool DnsTlsServer::operator <(const DnsTlsServer& other) const {
+0 −8
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

#pragma once

#include <chrono>
#include <set>
#include <string>
#include <vector>
@@ -51,13 +50,6 @@ struct DnsTlsServer {
    // Placeholder.  More protocols might be defined in the future.
    int protocol = IPPROTO_TCP;

    // The time to wait for the attempt on connecting to the server.
    // Set the default value 127 seconds to be consistent with TCP connect timeout.
    // (presume net.ipv4.tcp_syn_retries = 6)
    static constexpr std::chrono::milliseconds kDotConnectTimeoutMs =
            std::chrono::milliseconds(127 * 1000);
    std::chrono::milliseconds connectTimeout = kDotConnectTimeoutMs;

    // Exact comparison of DnsTlsServer objects
    bool operator<(const DnsTlsServer& other) const;
    bool operator==(const DnsTlsServer& other) const;
+11 −4
Original line number Diff line number Diff line
@@ -176,7 +176,14 @@ bool DnsTlsSocket::initialize() {
    mEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
    mShutdownEvent.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));

    mAsyncHandshake = Experiments::getInstance()->getFlag("dot_async_handshake", 0);
    const Experiments* const instance = Experiments::getInstance();
    mConnectTimeoutMs = instance->getFlag("dot_connect_timeout_ms", kDotConnectTimeoutMs);
    if (mConnectTimeoutMs < 1000) mConnectTimeoutMs = 1000;

    mAsyncHandshake = instance->getFlag("dot_async_handshake", 0);
    LOG(DEBUG) << "DnsTlsSocket is initialized with { mConnectTimeoutMs: " << mConnectTimeoutMs
               << ", mAsyncHandshake: " << mAsyncHandshake << " }";

    transitionState(State::UNINITIALIZED, State::INITIALIZED);

    return true;
@@ -273,7 +280,7 @@ bssl::UniquePtr<SSL> DnsTlsSocket::sslConnect(int fd) {
                // SSL_ERROR_WANT_READ is returned because the application data has been sent during
                // the TCP connection handshake, the device is waiting for the SSL handshake reply
                // from the server.
                if (int err = waitForReading(fd, mServer.connectTimeout.count()); err <= 0) {
                if (int err = waitForReading(fd, mConnectTimeoutMs); err <= 0) {
                    PLOG(WARNING) << "SSL_connect read error " << err << ", mark 0x" << std::hex
                                  << mMark;
                    return nullptr;
@@ -282,7 +289,7 @@ bssl::UniquePtr<SSL> DnsTlsSocket::sslConnect(int fd) {
            case SSL_ERROR_WANT_WRITE:
                // If no application data is sent during the TCP connection handshake, the
                // device is waiting for the connection established to perform SSL handshake.
                if (int err = waitForWriting(fd, mServer.connectTimeout.count()); err <= 0) {
                if (int err = waitForWriting(fd, mConnectTimeoutMs); err <= 0) {
                    PLOG(WARNING) << "SSL_connect write error " << err << ", mark 0x" << std::hex
                                  << mMark;
                    return nullptr;
@@ -332,7 +339,7 @@ bssl::UniquePtr<SSL> DnsTlsSocket::sslConnectV2(int fd) {
                return nullptr;
        }

        int n = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), mServer.connectTimeout.count()));
        int n = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), mConnectTimeoutMs));
        if (n <= 0) {
            PLOG(WARNING) << ((n == 0) ? "handshake timeout" : "Poll failed");
            return nullptr;
+6 −0
Original line number Diff line number Diff line
@@ -189,6 +189,12 @@ class DnsTlsSocket : public IDnsTlsSocket {
    // thread (the call to startHandshake()).
    bool mAsyncHandshake GUARDED_BY(mLock) = false;

    // The time to wait for the attempt on connecting to the server.
    // Set the default value 127 seconds to be consistent with TCP connect timeout.
    // (presume net.ipv4.tcp_syn_retries = 6)
    static constexpr int kDotConnectTimeoutMs = 127 * 1000;
    int mConnectTimeoutMs;

    // For testing.
    friend class DnsTlsSocketTest;
};
+4 −3
Original line number Diff line number Diff line
@@ -47,10 +47,11 @@ class Experiments {
    mutable std::mutex mMutex;
    std::unordered_map<std::string_view, int> mFlagsMapInt GUARDED_BY(mMutex);
    // TODO: Migrate other experiment flags to here.
    // (retry_count, retransmission_time_interval, dot_connect_timeout_ms)
    // (retry_count, retransmission_time_interval)
    static constexpr const char* const kExperimentFlagKeyList[] = {
            "keep_listening_udp", "parallel_lookup",     "parallel_lookup_sleep_time",
            "sort_nameservers", "dot_async_handshake"};
            "sort_nameservers",   "dot_async_handshake", "dot_connect_timeout_ms",
    };
    // This value is used in updateInternal as the default value if any flags can't be found.
    static constexpr int kFlagIntDefault = INT_MIN;
    // For testing.
Loading