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

Commit 4bf5a443 authored by Luke Huang's avatar Luke Huang
Browse files

Implement a new IPC setResolverOptions in DnsResolver

1. Implement the new method added in V9, setResolverOptions
2. Add its relevant binder test and update integration test to use this
new method if the remote version is new enough.

Test: atest
Bug: 194048056
Change-Id: I9a18f11769fd154e7cc6f35090e6c8fe61853d2a
parent 5322e039
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include "ResolverEventReporter.h"
#include "resolv_cache.h"

using aidl::android::net::ResolverOptionsParcel;
using aidl::android::net::ResolverParamsParcel;
using android::base::Join;
using android::base::StringPrintf;
@@ -307,5 +308,13 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num
    return statusFromErrcode(res);
}

::ndk::ScopedAStatus DnsResolverService::setResolverOptions(int32_t netId,
                                                            const ResolverOptionsParcel& options) {
    // Locking happens in res_cache.cpp functions.
    ENFORCE_NETWORK_STACK_PERMISSIONS();

    return statusFromErrcode(resolv_set_options(netId, options));
}

}  // namespace net
}  // namespace android
+2 −0
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ class DnsResolverService : public aidl::android::net::BnDnsResolver {
    ::ndk::ScopedAStatus destroyNetworkCache(int32_t netId) override;
    ::ndk::ScopedAStatus createNetworkCache(int32_t netId) override;
    ::ndk::ScopedAStatus flushNetworkCache(int32_t netId) override;
    ::ndk::ScopedAStatus setResolverOptions(
            int32_t netId, const aidl::android::net::ResolverOptionsParcel& options) override;

    // DNS64-related commands
    ::ndk::ScopedAStatus startPrefix64Discovery(int32_t netId) override;
+25 −17
Original line number Diff line number Diff line
@@ -1009,7 +1009,23 @@ struct NetConfig {
        dns_event_subsampling_map = resolv_get_dns_event_subsampling_map();
    }
    int nameserverCount() { return nameserverSockAddrs.size(); }
    int setOptions(const ResolverOptionsParcel& resolverOptions) {
        customizedTable.clear();
        for (const auto& host : resolverOptions.hosts) {
            if (!host.hostName.empty() && !host.ipAddr.empty())
                customizedTable.emplace(host.hostName, host.ipAddr);
        }

        if (resolverOptions.tcMode < aidl::android::net::IDnsResolver::TC_MODE_DEFAULT ||
            resolverOptions.tcMode > aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP) {
            LOG(WARNING) << __func__ << ": netid = " << netid
                         << ", invalid TC mode: " << resolverOptions.tcMode;
            return -EINVAL;
        }
        tc_mode = resolverOptions.tcMode;
        enforceDnsUid = resolverOptions.enforceDnsUid;
        return 0;
    }
    const unsigned netid;
    std::unique_ptr<Cache> cache;
    std::vector<std::string> nameservers;
@@ -1661,28 +1677,20 @@ int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& serve
        LOG(WARNING) << __func__ << ": netid = " << netid << ", failed to set dns stats";
        return -EINVAL;
    }
    netconfig->customizedTable.clear();

    netconfig->transportTypes = transportTypes;
    if (optionalResolverOptions.has_value()) {
        const ResolverOptionsParcel& resolverOptions = optionalResolverOptions.value();
        for (const auto& host : resolverOptions.hosts) {
            if (!host.hostName.empty() && !host.ipAddr.empty())
                netconfig->customizedTable.emplace(host.hostName, host.ipAddr);
        }

        if (resolverOptions.tcMode < aidl::android::net::IDnsResolver::TC_MODE_DEFAULT ||
            resolverOptions.tcMode > aidl::android::net::IDnsResolver::TC_MODE_UDP_TCP) {
            LOG(WARNING) << __func__ << ": netid = " << netid
                         << ", invalid TC mode: " << resolverOptions.tcMode;
            return -EINVAL;
        return netconfig->setOptions(resolverOptions);
    }
        netconfig->tc_mode = resolverOptions.tcMode;
        netconfig->enforceDnsUid = resolverOptions.enforceDnsUid;
    return 0;
}

    netconfig->transportTypes = transportTypes;
int resolv_set_options(unsigned netid, const ResolverOptionsParcel& options) {
    std::lock_guard guard(cache_mutex);
    NetConfig* netconfig = find_netconfig_locked(netid);

    return 0;
    if (netconfig == nullptr) return -ENONET;
    return netconfig->setOptions(options);
}

static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServers,
+3 −0
Original line number Diff line number Diff line
@@ -82,6 +82,9 @@ int resolv_set_nameservers(unsigned netid, const std::vector<std::string>& serve
                           std::optional<aidl::android::net::ResolverOptionsParcel> resolverOptions,
                           const std::vector<int32_t>& transportTypes = {});

// Sets options for a given network.
int resolv_set_options(unsigned netid, const aidl::android::net::ResolverOptionsParcel& options);

// Creates the cache associated with the given network.
int resolv_create_cache_for_net(unsigned netid);

+15 −0
Original line number Diff line number Diff line
@@ -626,3 +626,18 @@ TEST_F(DnsResolverBinderTest, setLogSeverity) {
    EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_WARNING).isOk());
    mExpectedLogData.push_back({"setLogSeverity(3)", "setLogSeverity.*3"});
}

TEST_F(DnsResolverBinderTest, SetResolverOptions) {
    SKIP_IF_REMOTE_VERSION_LESS_THAN(mDnsResolver.get(), 9);
    ResolverOptionsParcel options;
    options.tcMode = 1;
    options.enforceDnsUid = true;
    EXPECT_TRUE(mDnsResolver->setResolverOptions(TEST_NETID, options).isOk());
    mExpectedLogData.push_back(
            {"setResolverOptions(30, " + toString(options) + ")", "setResolverOptions.*30"});
    EXPECT_EQ(ENONET, mDnsResolver->setResolverOptions(-1, options).getServiceSpecificError());
    mExpectedLogData.push_back({"setResolverOptions(-1, " + toString(options) +
                                        ") -> ServiceSpecificException(64, \"Machine is not on the "
                                        "network\")",
                                "setResolverOptions.*-1.*64"});
}
Loading