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

Commit 9e8cf8d5 authored by Mike Yu's avatar Mike Yu
Browse files

Initialize DnsTlsDispatcher instance only when needed

Because the instance is used only when there are any validated DoT
servers, it doesn't need to be initialized along with netd starting.

Fix: 130686826
Test: cd packages/modules/DnsResolver && atest
Change-Id: Ie58e105b40f881038c80ee37f2f901f46840f6d0
parent b764f735
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -41,6 +41,11 @@ DnsTlsDispatcher::DnsTlsDispatcher() {
    mFactory.reset(new DnsTlsSocketFactory());
}

DnsTlsDispatcher& DnsTlsDispatcher::getInstance() {
    static DnsTlsDispatcher instance;
    return instance;
}

std::list<DnsTlsServer> DnsTlsDispatcher::getOrderedServerList(
        const std::list<DnsTlsServer> &tlsServers, unsigned mark) const {
    // Our preferred DnsTlsServer order is:
+4 −3
Original line number Diff line number Diff line
@@ -37,13 +37,12 @@ namespace net {
// Queries made here are dispatched to an existing or newly constructed DnsTlsTransport.
class DnsTlsDispatcher {
  public:
    // Default constructor.
    DnsTlsDispatcher();

    // Constructor with dependency injection for testing.
    explicit DnsTlsDispatcher(std::unique_ptr<IDnsTlsSocketFactory> factory)
        : mFactory(std::move(factory)) {}

    static DnsTlsDispatcher& getInstance();

    // Enqueues |query| for resolution via the given |tlsServers| on the
    // network indicated by |mark|; writes the response into |ans|, and stores
    // the count of bytes written in |resplen|. Returns a success or error code.
@@ -62,6 +61,8 @@ class DnsTlsDispatcher {
                                    int* _Nonnull resplen, bool* _Nonnull connectTriggered);

  private:
    DnsTlsDispatcher();

    // This lock is static so that it can be used to annotate the Transport struct.
    // DnsTlsDispatcher is a singleton in practice, so making this static does not change
    // the locking behavior.
+2 −4
Original line number Diff line number Diff line
@@ -144,8 +144,6 @@ using android::netdutils::IPSockAddr;
using android::netdutils::Slice;
using android::netdutils::Stopwatch;

static DnsTlsDispatcher sDnsTlsDispatcher;

static int send_vc(res_state statp, res_params* params, const uint8_t* buf, int buflen,
                   uint8_t* ans, int anssiz, int* terrno, size_t ns, time_t* at, int* rcode,
                   int* delay);
@@ -1255,8 +1253,8 @@ static int res_tls_send(res_state statp, const Slice query, const Slice answer,

    LOG(INFO) << __func__ << ": performing query over TLS";

    const auto response = sDnsTlsDispatcher.query(privateDnsStatus.validatedServers(), statp, query,
                                                  answer, &resplen);
    const auto response = DnsTlsDispatcher::getInstance().query(privateDnsStatus.validatedServers(),
                                                                statp, query, answer, &resplen);

    LOG(INFO) << __func__ << ": TLS query result: " << static_cast<int>(response);