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

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

Add a method makeDohIdentity to simplify setDoh

In preparation for DDR, this change makes setDoh() easier to read
the DoH server information to set up the server. No logical changes.

Bug: 240259333
Test: atest
Change-Id: Id3b578a8ff2183c6a220d64cd78dd7b88c71bf4f
parent 35833bad
Loading
Loading
Loading
Loading
+38 −31
Original line number Diff line number Diff line
@@ -610,15 +610,12 @@ int PrivateDnsConfiguration::setDoh(int32_t netId, uint32_t mark,

    initDohLocked();

    // TODO: 1. Improve how to choose the server
    // TODO: 2. Support multiple servers
    for (const auto& entry : mAvailableDoHProviders) {
        const auto& doh = entry.getDohIdentity(sortedServers, name);
        if (!doh.ok()) continue;

        // Since the DnsResolver is expected to be configured by the system server, add the
        // restriction to prevent ResolverTestProvider from being used other than testing.
        if (entry.requireRootPermission && AIBinder_getCallingUid() != AID_ROOT) continue;
    const auto& doh = makeDohIdentity(sortedServers, name);
    if (!doh.ok()) {
        LOG(INFO) << __func__ << ": No suitable DoH server found";
        clearDoh(netId);
        return 0;
    }

    auto it = mDohTracker.find(netId);
    // Skip if the same server already exists and its status == success.
@@ -642,11 +639,6 @@ int PrivateDnsConfiguration::setDoh(int32_t netId, uint32_t mark,
                       dohId.ipAddr.c_str(), mark, caCert.c_str(), &flags);
}

    LOG(INFO) << __func__ << ": No suitable DoH server found";
    clearDoh(netId);
    return 0;
}

void PrivateDnsConfiguration::clearDoh(unsigned netId) {
    LOG(DEBUG) << "PrivateDnsConfiguration::clearDoh (" << netId << ")";
    if (mDohDispatcher != nullptr) doh_net_delete(mDohDispatcher, netId);
@@ -654,6 +646,21 @@ void PrivateDnsConfiguration::clearDoh(unsigned netId) {
    resolv_stats_set_addrs(netId, PROTO_DOH, {}, kDohPort);
}

base::Result<PrivateDnsConfiguration::DohIdentity> PrivateDnsConfiguration::makeDohIdentity(
        const std::vector<std::string>& servers, const std::string& name) const {
    for (const auto& entry : mAvailableDoHProviders) {
        const auto& dohId = entry.getDohIdentity(servers, name);
        if (!dohId.ok()) continue;

        // Since the DnsResolver is expected to be configured by the system server, add the
        // restriction to prevent ResolverTestProvider from being used other than testing.
        if (entry.requireRootPermission && AIBinder_getCallingUid() != AID_ROOT) continue;

        return dohId;
    }
    return Errorf("Cannot make a DohIdentity from current DNS configuration");
}

ssize_t PrivateDnsConfiguration::dohQuery(unsigned netId, const Slice query, const Slice answer,
                                          uint64_t timeoutMs) {
    {
+5 −0
Original line number Diff line number Diff line
@@ -279,6 +279,11 @@ class PrivateDnsConfiguration {
             false},
    }};

    // Makes a DohIdentity by looking up the `mAvailableDoHProviders` by `servers` and `name`.
    base::Result<DohIdentity> makeDohIdentity(const std::vector<std::string>& servers,
                                              const std::string& name) const
            REQUIRES(mPrivateDnsLock);

    // For the metrics. Store the current DNS server list in the same order as what is passed
    // in setResolverConfiguration().
    std::map<unsigned, std::vector<std::string>> mUnorderedDnsTracker GUARDED_BY(mPrivateDnsLock);