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

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

Remove nscount in ResState and NetConfig

The number of nameservers can be derived from std::vector::size().
It doesn't need a variable to store the value.

Test: cd packages/modules/DnsResolver && atest
Change-Id: Ifc2e1401b1d081595a00b60fbaf6dd96c3d44a71
parent 6407ae47
Loading
Loading
Loading
Loading
+23 −35
Original line number Diff line number Diff line
@@ -984,9 +984,10 @@ struct NetConfig {
        dns_event_subsampling_map = resolv_get_dns_event_subsampling_map();
    }

    int nameserverCount() { return nameserverSockAddrs.size(); }

    const unsigned netid;
    std::unique_ptr<Cache> cache;
    int nscount = 0;
    std::vector<std::string> nameservers;
    std::vector<IPSockAddr> nameserverSockAddrs;
    int revision_id = 0;  // # times the nameservers have been replaced
@@ -1442,7 +1443,7 @@ static void res_cache_clear_stats_locked(NetConfig* netconfig);
bool resolv_has_nameservers(unsigned netid) {
    std::lock_guard guard(cache_mutex);
    NetConfig* info = find_netconfig_locked(netid);
    return (info != nullptr) && (info->nscount > 0);
    return (info != nullptr) && (info->nameserverCount() > 0);
}

int resolv_create_cache_for_net(unsigned netid) {
@@ -1603,7 +1604,6 @@ int resolv_set_nameservers(
                      << ", addr = " << netconfig->nameservers[i];
        }
        netconfig->nameserverSockAddrs = std::move(ipSockAddrs);
        netconfig->nscount = numservers;
    } else {
        if (netconfig->params.max_samples != old_max_samples) {
            // If the maximum number of samples changes, the overhead of keeping the most recent
@@ -1655,7 +1655,6 @@ static bool resolv_is_nameservers_equal(const std::vector<std::string>& oldServe
}

static void free_nameservers_locked(NetConfig* netconfig) {
    netconfig->nscount = 0;
    netconfig->nameservers.clear();
    netconfig->nameserverSockAddrs.clear();
    res_cache_clear_stats_locked(netconfig);
@@ -1671,7 +1670,6 @@ void resolv_populate_res_for_net(ResState* statp) {
    NetConfig* info = find_netconfig_locked(statp->netid);
    if (info == nullptr) return;

    statp->nscount = static_cast<int>(info->nameserverSockAddrs.size());
    statp->nsaddrs = info->nameserverSockAddrs;
    statp->search_domains = info->search_domains;
    statp->tc_mode = info->tc_mode;
@@ -1712,42 +1710,32 @@ int android_net_res_stats_get_info_for_net(unsigned netid, int* nscount,
                                           char domains[MAXDNSRCH][MAXDNSRCHPATH],
                                           res_params* params, struct res_stats stats[MAXNS],
                                           int* wait_for_pending_req_timeout_count) {
    int revision_id = -1;
    std::lock_guard guard(cache_mutex);

    NetConfig* info = find_netconfig_locked(netid);
    if (info) {
        if (info->nscount > MAXNS) {
            LOG(INFO) << __func__ << ": nscount " << info->nscount << " > MAXNS " << MAXNS;
            errno = EFAULT;
            return -1;
        }
        int i;
        *nscount = info->nscount;
    if (!info) return -1;

        // It shouldn't happen, but just in case of buffer overflow.
        if (info->nscount != static_cast<int>(info->nameserverSockAddrs.size())) {
            LOG(INFO) << __func__ << ": nscount " << info->nscount
                      << " != " << info->nameserverSockAddrs.size();
    const int num = info->nameserverCount();
    if (num > MAXNS) {
        LOG(INFO) << __func__ << ": nscount " << num << " > MAXNS " << MAXNS;
        errno = EFAULT;
        return -1;
    }

        for (i = 0; i < info->nscount; i++) {
            servers[i] = info->nameserverSockAddrs.at(i);
    for (int i = 0; i < num; i++) {
        servers[i] = info->nameserverSockAddrs[i];
        stats[i] = info->nsstats[i];
    }

        for (i = 0; i < static_cast<int>(info->search_domains.size()); i++) {
    for (size_t i = 0; i < info->search_domains.size(); i++) {
        strlcpy(domains[i], info->search_domains[i].c_str(), MAXDNSRCHPATH);
    }
        *dcount = i;

    *nscount = num;
    *dcount = static_cast<int>(info->search_domains.size());
    *params = info->params;
        revision_id = info->revision_id;
    *wait_for_pending_req_timeout_count = info->wait_for_pending_req_timeout_count;
    }

    return revision_id;
    return info->revision_id;
}

std::vector<std::string> resolv_cache_dump_subsampling_map(unsigned netid) {
+0 −1
Original line number Diff line number Diff line
@@ -97,7 +97,6 @@ void res_init(ResState* statp, const struct android_net_context* _Nonnull netcon
    statp->netid = netcontext->dns_netid;
    statp->uid = netcontext->uid;
    statp->pid = netcontext->pid;
    statp->nscount = 0;
    statp->id = arc4random_uniform(65536);

    for (auto& sock : statp->nssocks) {
+4 −4
Original line number Diff line number Diff line
@@ -430,7 +430,7 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
        // data so the normal resolve path can do its thing
        resolv_populate_res_for_net(statp);
    }
    if (statp->nscount == 0) {
    if (statp->nameserverCount() == 0) {
        // We have no nameservers configured, so there's no point trying.
        // Tell the cache the query failed, or any retries and anyone else asking the same
        // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
@@ -470,14 +470,14 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
    }
    bool usable_servers[MAXNS];
    int usableServersCount = android_net_res_stats_get_usable_servers(
            &params, stats, statp->nscount, usable_servers);
            &params, stats, statp->nameserverCount(), usable_servers);

    if ((flags & ANDROID_RESOLV_NO_RETRY) && usableServersCount > 1) {
        auto hp = reinterpret_cast<const HEADER*>(buf);

        // Select a random server based on the query id
        int selectedServer = (hp->id % usableServersCount) + 1;
        res_set_usable_server(selectedServer, statp->nscount, usable_servers);
        res_set_usable_server(selectedServer, statp->nameserverCount(), usable_servers);
    }

    // Send request, RETRY times, or until successful.
@@ -587,7 +587,7 @@ static struct timespec get_timeout(res_state statp, const res_params* params, co
    // This has no effect with 1 or 2 nameservers
    msec = params->base_timeout_msec << ns;
    if (ns > 0) {
        msec /= statp->nscount;
        msec /= statp->nameserverCount();
    }
    // For safety, don't allow OEMs and experiments to configure a timeout shorter than 1s.
    if (msec < 1000) {
+3 −1
Original line number Diff line number Diff line
@@ -94,11 +94,13 @@ struct ResState {
            sock.reset();
        }
    }

    int nameserverCount() { return nsaddrs.size(); }

    // clang-format off
    unsigned netid;                             // NetId: cache key and socket mark
    uid_t uid;                                  // uid of the app that sent the DNS lookup
    pid_t pid;                                  // pid of the app that sent the DNS lookup
    int nscount;                                // number of name srvers
    uint16_t id;                                // current message id
    std::vector<std::string> search_domains{};  // domains to search
    std::vector<android::netdutils::IPSockAddr> nsaddrs;