aboutsummaryrefslogtreecommitdiff
path: root/sethostent.cpp
diff options
context:
space:
mode:
authorTom Marshall <tdm.code@gmail.com>2020-08-25 08:31:32 -0700
committerGeorge Zacharia <george.zcharia@gmail.com>2024-06-01 20:21:07 +0530
commitb703b1bee76a8fb65dbcb5e45f7135f9defebb30 (patch)
tree9111924a439f36cb4b1e0896edd89f08ad7b6e82 /sethostent.cpp
parent9dbbd667dd3e7f4b889eb9c8e5caa4e37ddeead5 (diff)
DnsResolver: Sort and cache hosts file data for fast lookup
The hosts file is normally searched linearly. This is very slow when the file is large. To mitigate this, read the hosts file and sort the entries in an in-memory cache. When an address is requested via gethostbyname or getaddrinfo, binary search the cache. In case where the cache is not available, return a suitable error code and fall back to the existing lookup code. This has been written to behave as much like the existing lookup code as possible. But note bionic and glibc differ in behavior for some corner cases. Choose the most standard compliant behavior for these where possible. Otherwise choose the behavior that seems most reasonable. Note: this change is the analogue of the bionic change of the same name. Both should be kept in sync. u14.0: Replace usage of MIN with std::min Change-Id: I5926493864d4b1291ae83f8b601bf5dcc54085cd
Diffstat (limited to 'sethostent.cpp')
-rw-r--r--sethostent.cpp6
1 files changed, 6 insertions, 0 deletions
diff --git a/sethostent.cpp b/sethostent.cpp
index 80037415..419ee869 100644
--- a/sethostent.cpp
+++ b/sethostent.cpp
@@ -44,6 +44,7 @@
#include "resolv_private.h"
#include "gethostsfile.h"
+#include "hosts_cache.h"
constexpr int MAXALIASES = 35;
constexpr int MAXADDRS = 35;
@@ -71,6 +72,11 @@ int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
char* aliases[MAXALIASES];
char* addr_ptrs[MAXADDRS];
+ int rc = hc_gethtbyname(name, af, info);
+ if (rc != NETDB_INTERNAL) {
+ return (rc == NETDB_SUCCESS ? 0 : EAI_NODATA);
+ }
+
FILE* hf = NULL;
sethostent_r(&hf);
if (hf == NULL) {