• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff --git a/src/lib/ares_init.c b/src/lib/ares_init.c
2index e8902c6..3a5a5f5 100644
3--- a/src/lib/ares_init.c
4+++ b/src/lib/ares_init.c
5@@ -58,6 +58,64 @@
6 #undef WIN32  /* Redefined in MingW/MSVC headers */
7 #endif
8
9+
10+#if OHOS_DNS_PROXY_BY_NETSYS
11+#include <dlfcn.h>
12+#include <stdio.h>
13+
14+#if DNS_CONFIG_DEBUG
15+#ifndef DNS_CONFIG_PRINT
16+#define DNS_CONFIG_PRINT(fmt, ...) printf("DNS " fmt "\n", ##__VA_ARGS__)
17+#endif
18+#else
19+#define DNS_CONFIG_PRINT(fmt, ...)
20+#endif
21+
22+#define DNS_SO_PATH "libnetsys_client.z.so"
23+#define OHOS_GET_CONFIG_FUNC_NAME "NetSysGetResolvConf"
24+#define MAX_SERVER_NUM 4
25+#define MAX_SERVER_LENGTH 50
26+
27+struct resolv_config {
28+  int32_t error;
29+  int32_t timeout_ms;
30+  uint32_t retry_count;
31+  char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1];
32+};
33+
34+typedef int32_t (*GetConfig)(uint16_t netId, struct resolv_config *config);
35+
36+static void *open_dns_lib(void)
37+{
38+  static void *lib = NULL;
39+  if (lib != NULL) {
40+    return lib;
41+  }
42+  lib = dlopen(DNS_SO_PATH, RTLD_LAZY);
43+  if (lib == NULL) {
44+    DNS_CONFIG_PRINT("%s: dlopen %s failed: %s",
45+      __func__, DNS_SO_PATH, dlerror());
46+    return NULL;
47+  }
48+  return lib;
49+}
50+
51+static void *load_from_dns_lib(const char *symbol)
52+{
53+  void *lib_handle = open_dns_lib();
54+  if (lib_handle == NULL) {
55+    return NULL;
56+  }
57+
58+  void *sym_addr = dlsym(lib_handle, symbol);
59+  if (sym_addr == NULL) {
60+    DNS_CONFIG_PRINT("%s: loading symbol %s with dlsym failed: %s",
61+      __func__, symbol, dlerror());
62+  }
63+  return sym_addr;
64+}
65+#endif
66+
67 static int init_by_options(ares_channel channel,
68                            const struct ares_options *options,
69                            int optmask);
70@@ -1705,6 +1763,12 @@ static int init_by_resolv_conf(ares_channel channel)
71     int error;
72     int update_domains;
73     const char *resolvconf_path;
74+#if OHOS_DNS_PROXY_BY_NETSYS
75+    int netid = 0;
76+    int ret = 0;
77+    int status_each = -1;
78+    GetConfig func = NULL;
79+#endif
80
81     /* Don't read resolv.conf and friends if we don't have to */
82     if (ARES_CONFIG_CHECK(channel))
83@@ -1720,6 +1784,36 @@ static int init_by_resolv_conf(ares_channel channel)
84       resolvconf_path = PATH_RESOLV_CONF;
85     }
86
87+#if OHOS_DNS_PROXY_BY_NETSYS
88+    *(void **) &func = load_from_dns_lib(OHOS_GET_CONFIG_FUNC_NAME);
89+    if (!func) {
90+      DNS_CONFIG_PRINT("%s: loading %s failed, use %s as a fallback",
91+        __func__, OHOS_GET_CONFIG_FUNC_NAME, DNS_RESOLV_CONF_PATH);
92+      goto etc_resolv_conf;
93+    }
94+    struct resolv_config config = {0};
95+    ret = func(netid, &config);
96+    if (ret < 0) {
97+      DNS_CONFIG_PRINT("__get_resolv_conf OHOS_GET_CONFIG_FUNC_NAME err %d\n", ret);
98+      return ARES_ENONAME;
99+    }
100+
101+    for (int i = 0; i < MAX_SERVER_NUM; ++i) {
102+      if (config.nameservers[i] == NULL || config.nameservers[i][0] == 0) {
103+        continue;
104+      }
105+      status_each = config_nameserver(&servers, &nservers, config.nameservers[i]);
106+      if (status_each == ARES_SUCCESS) {
107+        status = ARES_SUCCESS;
108+      }
109+    }
110+
111+    if (status == ARES_SUCCESS && nservers > 0) {
112+      goto get_conf_ok;
113+    }
114+
115+etc_resolv_conf:
116+#endif
117     fp = fopen(resolvconf_path, "r");
118     if (fp) {
119       while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
120@@ -1866,7 +1960,9 @@ static int init_by_resolv_conf(ares_channel channel)
121         ares_free(sortlist);
122       return status;
123     }
124-
125+#if OHOS_DNS_PROXY_BY_NETSYS
126+get_conf_ok:
127+#endif
128   /* If we got any name server entries, fill them in. */
129   if (servers)
130     {
131