aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Marshall <tdm.code@gmail.com>2020-01-16 13:07:04 -0800
committerGeorge Zacharia <george.zcharia@gmail.com>2024-06-01 20:21:07 +0530
commitbacc24e67ba66ceb9a1cc70035a08b97f04bdd5f (patch)
treea1da03a5d36116a79f1817ec50382b8a97bb8835
parentb703b1bee76a8fb65dbcb5e45f7135f9defebb30 (diff)
DnsResolver: Support wildcards in cached hosts file
If an exact name is not found in the hosts file and the host name contains at least one dot, search for entries of the form "*.domain", where domain is the portion of the host name after the first dot. If that is not found, repeat using the domain. Example: a.b.c.example.com would search for the following in turn: a.b.c.example.com *.b.c.example.com *.c.example.com *.example.com *.com Note: this change is the analogue of the bionic change of the same name. Both should be kept in sync. Change-Id: I4b0bb81699151d5b371850daebf785e35ec9b170
-rw-r--r--hosts_cache.cpp29
1 files changed, 28 insertions, 1 deletions
diff --git a/hosts_cache.cpp b/hosts_cache.cpp
index 9c0f0a0c..5a759698 100644
--- a/hosts_cache.cpp
+++ b/hosts_cache.cpp
@@ -122,7 +122,7 @@ static int cmp_hcent_name(const void *a, const void *b)
return hstrcmp(na, nb);
}
-static struct hcent *_hcfindname(const char *name)
+static struct hcent *_hcfindname_exact(const char *name)
{
size_t first, last, mid;
struct hcent *cur = NULL;
@@ -163,6 +163,33 @@ found:
return cur;
}
+static struct hcent *_hcfindname(const char *name)
+{
+ struct hcent *ent;
+ char namebuf[MAX_HOSTLEN];
+ char *p;
+ char *dot;
+
+ ent = _hcfindname_exact(name);
+ if (!ent && strlen(name) < sizeof(namebuf)) {
+ strlcpy(namebuf, name, sizeof(namebuf));
+ p = namebuf;
+ do {
+ dot = strchr(p, '.');
+ if (!dot)
+ break;
+ if (dot > p) {
+ *(dot - 1) = '*';
+ ent = _hcfindname_exact(dot - 1);
+ }
+ p = dot + 1;
+ }
+ while (!ent);
+ }
+
+ return ent;
+}
+
/*
* Find next name on line, if any.
*