Loading res_cache.cpp +23 −35 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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) { Loading Loading @@ -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 Loading Loading @@ -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); Loading @@ -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; Loading Loading @@ -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) { Loading res_init.cpp +0 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading res_send.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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( ¶ms, stats, statp->nscount, usable_servers); ¶ms, 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. Loading Loading @@ -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) { Loading resolv_private.h +3 −1 Original line number Diff line number Diff line Loading @@ -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; Loading Loading
res_cache.cpp +23 −35 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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) { Loading Loading @@ -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 Loading Loading @@ -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); Loading @@ -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; Loading Loading @@ -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) { Loading
res_init.cpp +0 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading
res_send.cpp +4 −4 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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( ¶ms, stats, statp->nscount, usable_servers); ¶ms, 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. Loading Loading @@ -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) { Loading
resolv_private.h +3 −1 Original line number Diff line number Diff line Loading @@ -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; Loading