aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli B <abittin@gmail.com>2020-12-07 10:57:29 +0100
committerGerrit Code Review <gerrit@box1>2020-12-07 10:57:29 +0100
commit6a7bd233c5ae53ac52818794cad431850b509f3a (patch)
tree387a3fd994c68254741fc1f3c577291977d1d833
parent3334a5e0fc7b049e3e550da97014112f415a0736 (diff)
parenta56c2362d97fd198f32b6d8dd8ff6f10cec4b833 (diff)
Merge "Toggle-able adblock hosts file [1/3]" into r11.0r11.0
-rw-r--r--Android.bp1
-rw-r--r--getaddrinfo.cpp6
-rw-r--r--gethostsfile.cpp16
-rw-r--r--gethostsfile.h4
-rw-r--r--sethostent.cpp4
5 files changed, 28 insertions, 3 deletions
diff --git a/Android.bp b/Android.bp
index 6f06eb6e..7ea7389d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -97,6 +97,7 @@ cc_library {
srcs: [
"getaddrinfo.cpp",
"gethnamaddr.cpp",
+ "gethostsfile.cpp",
"sethostent.cpp",
"res_cache.cpp",
"res_comp.cpp",
diff --git a/getaddrinfo.cpp b/getaddrinfo.cpp
index b9da5f48..9342b2a4 100644
--- a/getaddrinfo.cpp
+++ b/getaddrinfo.cpp
@@ -67,6 +67,8 @@
#include "resolv_private.h"
#include "util.h"
+#include "gethostsfile.h"
+
#define ANY 0
using android::net::NetworkDnsEventReported;
@@ -1472,7 +1474,7 @@ static int dns_getaddrinfo(const char* name, const addrinfo* pai,
static void _sethtent(FILE** hostf) {
if (!*hostf)
- *hostf = fopen(_PATH_HOSTS, "re");
+ *hostf = fopen(gethostsfile(), "re");
else
rewind(*hostf);
}
@@ -1495,7 +1497,7 @@ static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct a
assert(name != NULL);
assert(pai != NULL);
- if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
+ if (!*hostf && !(*hostf = fopen(gethostsfile(), "re"))) return (NULL);
again:
if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
if (*p == '#') goto again;
diff --git a/gethostsfile.cpp b/gethostsfile.cpp
new file mode 100644
index 00000000..72faa471
--- /dev/null
+++ b/gethostsfile.cpp
@@ -0,0 +1,16 @@
+#include <string.h>
+#include <netdb.h>
+#include <sys/system_properties.h>
+
+#include "gethostsfile.h"
+
+const char* gethostsfile() {
+ char hosts_setting[128];
+ int prop_len = __system_property_get(AICP_HOSTS_SETTING_PROP,
+ hosts_setting);
+ if (prop_len > 0 && strncmp(hosts_setting, "true", 4) == 0) {
+ return AICP_PATH_ADBLOCK_HOSTS;
+ } else {
+ return _PATH_HOSTS;
+ }
+}
diff --git a/gethostsfile.h b/gethostsfile.h
new file mode 100644
index 00000000..214d2579
--- /dev/null
+++ b/gethostsfile.h
@@ -0,0 +1,4 @@
+#define AICP_PATH_ADBLOCK_HOSTS "/system/etc/hosts.aicp_adblock"
+#define AICP_HOSTS_SETTING_PROP "persist.aicp.hosts_block"
+
+const char* gethostsfile();
diff --git a/sethostent.cpp b/sethostent.cpp
index de94fce9..1074fc04 100644
--- a/sethostent.cpp
+++ b/sethostent.cpp
@@ -43,6 +43,8 @@
#include "hostent.h"
#include "resolv_private.h"
+#include "gethostsfile.h"
+
constexpr int MAXALIASES = 35;
constexpr int MAXADDRS = 35;
@@ -51,7 +53,7 @@ constexpr int MAXADDRS = 35;
static void sethostent_r(FILE** hf) {
if (!*hf)
- *hf = fopen(_PATH_HOSTS, "re");
+ *hf = fopen(gethostsfile(), "re");
else
rewind(*hf);
}