已合并
get dns cache from netsys #59
maosiping创建于 2024年10月26日
get dns cache from netsys #59
已合并
从refs/pull/59/head合入到master
共 6 个文件变更+324-241
| @@ -1,130 +0,0 @@ | |||
| 1 | -diff --git a/src/lib/ares_init.c b/src/lib/ares_init.c | ||
| 2 | -index e8902c6..3a5a5f5 100644 | ||
| 3 | ---- a/src/lib/ares_init.c | ||
| 4 | -+++ b/src/lib/ares_init.c | ||
| 5 | - | ||
| 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 5 | ||
| 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 | - 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 | - 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 | - 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 | - { | ||
| @@ -0,0 +1,317 @@ | |||
| 1 | +diff --git a/src/lib/ares_getaddrinfo.c b/src/lib/ares_getaddrinfo.c | ||
| 2 | +index 0a0225a..4019693 100644 | ||
| 3 | +--- a/src/lib/ares_getaddrinfo.c | ||
| 4 | ++++ b/src/lib/ares_getaddrinfo.c | ||
| 5 | + static int as_is_first(const struct host_query *hquery); | ||
| 6 | + static int as_is_only(const struct host_query* hquery); | ||
| 7 | + static int next_dns_lookup(struct host_query *hquery); | ||
| 8 | + | ||
| 9 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 10 | ++#define MAX_RESULTS 32 | ||
| 11 | ++#define MAX_CANON_NAME 256 | ||
| 12 | ++ | ||
| 13 | ++typedef union { | ||
| 14 | ++ struct sockaddr sa; | ||
| 15 | ++ struct sockaddr_in6 sin6; | ||
| 16 | ++ struct sockaddr_in sin; | ||
| 17 | ++} ares_align_sock_addr; | ||
| 18 | ++ | ||
| 19 | ++typedef struct { | ||
| 20 | ++ uint32_t ai_flags; | ||
| 21 | ++ uint32_t ai_family; | ||
| 22 | ++ uint32_t ai_socktype; | ||
| 23 | ++ uint32_t ai_protocol; | ||
| 24 | ++ uint32_t ai_addrlen; | ||
| 25 | ++ ares_align_sock_addr ai_addr; | ||
| 26 | ++ char ai_canonName[MAX_CANON_NAME + 1]; | ||
| 27 | ++} ares_cached_addrinfo; | ||
| 28 | ++ | ||
| 29 | ++typedef struct { | ||
| 30 | ++ char *host; | ||
| 31 | ++ char *serv; | ||
| 32 | ++ struct addrinfo *hint; | ||
| 33 | ++} ares_cache_key_param_wrapper; | ||
| 34 | ++ | ||
| 35 | ++int32_t NetSysSetResolvCache(uint16_t netid, ares_cache_key_param_wrapper param, struct addrinfo *res); | ||
| 36 | ++ | ||
| 37 | ++int32_t NetSysGetResolvCache(uint16_t netid, ares_cache_key_param_wrapper param, | ||
| 38 | ++ ares_cached_addrinfo cached_addrinfo[static MAX_RESULTS], uint32_t *num); | ||
| 39 | ++ | ||
| 40 | ++void ares_addrinfo_hints_to_addrinfo(const struct ares_addrinfo_hints *hints, struct addrinfo *out_hints) { | ||
| 41 | ++ if (hints == NULL || out_hints == NULL) { | ||
| 42 | ++ return; | ||
| 43 | ++ } | ||
| 44 | ++ memset(out_hints, 0, sizeof(struct addrinfo)); | ||
| 45 | ++ out_hints->ai_flags = hints->ai_flags; | ||
| 46 | ++ out_hints->ai_family = hints->ai_family; | ||
| 47 | ++ out_hints->ai_socktype = hints->ai_socktype; | ||
| 48 | ++ out_hints->ai_protocol = hints->ai_protocol; | ||
| 49 | ++} | ||
| 50 | ++ | ||
| 51 | ++void ares_free_posix_addrinfo(struct addrinfo *head) { | ||
| 52 | ++ while (head != NULL) { | ||
| 53 | ++ struct addrinfo *current = head; | ||
| 54 | ++ head = head->ai_next; | ||
| 55 | ++ if (current->ai_addr) { | ||
| 56 | ++ ares_free(current->ai_addr); | ||
| 57 | ++ } | ||
| 58 | ++ ares_free(current); | ||
| 59 | ++ } | ||
| 60 | ++} | ||
| 61 | ++ | ||
| 62 | ++struct addrinfo *ares_addrinfo_to_addrinfo(const struct ares_addrinfo *res) { | ||
| 63 | ++ if (res == NULL) { | ||
| 64 | ++ return NULL; | ||
| 65 | ++ } | ||
| 66 | ++ | ||
| 67 | ++ struct addrinfo *head_res = ares_malloc(sizeof(struct addrinfo)); | ||
| 68 | ++ if (head_res == NULL) { | ||
| 69 | ++ return NULL; | ||
| 70 | ++ } | ||
| 71 | ++ memset(head_res, 0, sizeof(struct addrinfo)); | ||
| 72 | ++ | ||
| 73 | ++ struct addrinfo *now_node = head_res; | ||
| 74 | ++ for (struct ares_addrinfo_node *tmp = res->nodes; tmp != NULL; tmp = tmp->ai_next) { | ||
| 75 | ++ if (tmp->ai_addrlen > sizeof(ares_align_sock_addr)) { | ||
| 76 | ++ continue; | ||
| 77 | ++ } | ||
| 78 | ++ struct addrinfo *next_node = ares_malloc(sizeof(struct addrinfo)); | ||
| 79 | ++ if (next_node == NULL) { | ||
| 80 | ++ ares_free_posix_addrinfo(head_res); | ||
| 81 | ++ return NULL; | ||
| 82 | ++ } | ||
| 83 | ++ memset(next_node, 0, sizeof(struct addrinfo)); | ||
| 84 | ++ now_node->ai_next = next_node; | ||
| 85 | ++ now_node = next_node; | ||
| 86 | ++ | ||
| 87 | ++ next_node->ai_flags = tmp->ai_flags; | ||
| 88 | ++ next_node->ai_family = tmp->ai_family; | ||
| 89 | ++ next_node->ai_socktype = tmp->ai_socktype; | ||
| 90 | ++ next_node->ai_protocol = tmp->ai_protocol; | ||
| 91 | ++ next_node->ai_addrlen = tmp->ai_addrlen; | ||
| 92 | ++ next_node->ai_addr = ares_malloc(sizeof(ares_align_sock_addr)); | ||
| 93 | ++ if (next_node->ai_addr == NULL) { | ||
| 94 | ++ ares_free_posix_addrinfo(head_res); | ||
| 95 | ++ return NULL; | ||
| 96 | ++ } | ||
| 97 | ++ memset(next_node->ai_addr, 0, sizeof(ares_align_sock_addr)); | ||
| 98 | ++ memcpy(next_node->ai_addr, tmp->ai_addr, tmp->ai_addrlen); | ||
| 99 | ++ } | ||
| 100 | ++ struct addrinfo *out_res = head_res->ai_next; | ||
| 101 | ++ ares_free(head_res); | ||
| 102 | ++ return out_res; | ||
| 103 | ++} | ||
| 104 | ++ | ||
| 105 | ++struct ares_addrinfo * | ||
| 106 | ++ares_cached_addrinfo_to_ares_addrinfo(const ares_cached_addrinfo cached_addrinfo[static MAX_RESULTS], uint32_t num) { | ||
| 107 | ++ uint32_t real_num = num > MAX_RESULTS ? MAX_RESULTS : num; | ||
| 108 | ++ if (num == 0) { | ||
| 109 | ++ return NULL; | ||
| 110 | ++ } | ||
| 111 | ++ | ||
| 112 | ++ struct ares_addrinfo_node *head_res = ares_malloc(sizeof(struct ares_addrinfo_node)); | ||
| 113 | ++ if (head_res == NULL) { | ||
| 114 | ++ return NULL; | ||
| 115 | ++ } | ||
| 116 | ++ memset(head_res, 0, sizeof(struct ares_addrinfo_node)); | ||
| 117 | ++ | ||
| 118 | ++ struct ares_addrinfo_node *now_node = head_res; | ||
| 119 | ++ for (uint32_t i = 0; i < real_num; ++i) { | ||
| 120 | ++ if (cached_addrinfo[i].ai_addrlen > sizeof(ares_align_sock_addr)) { | ||
| 121 | ++ continue; | ||
| 122 | ++ } | ||
| 123 | ++ | ||
| 124 | ++ struct ares_addrinfo_node *next_node = ares_malloc(sizeof(struct ares_addrinfo_node)); | ||
| 125 | ++ if (next_node == NULL) { | ||
| 126 | ++ ares__freeaddrinfo_nodes(head_res); | ||
| 127 | ++ return NULL; | ||
| 128 | ++ } | ||
| 129 | ++ memset(next_node, 0, sizeof(struct addrinfo)); | ||
| 130 | ++ now_node->ai_next = next_node; | ||
| 131 | ++ now_node = next_node; | ||
| 132 | ++ | ||
| 133 | ++ next_node->ai_flags = (int) cached_addrinfo[i].ai_flags; | ||
| 134 | ++ next_node->ai_family = (int) cached_addrinfo[i].ai_family; | ||
| 135 | ++ next_node->ai_socktype = (int) cached_addrinfo[i].ai_socktype; | ||
| 136 | ++ next_node->ai_protocol = (int) cached_addrinfo[i].ai_protocol; | ||
| 137 | ++ next_node->ai_addrlen = (ares_socklen_t) cached_addrinfo[i].ai_addrlen; | ||
| 138 | ++ next_node->ai_addr = ares_malloc(sizeof(ares_align_sock_addr)); | ||
| 139 | ++ if (next_node->ai_addr == NULL) { | ||
| 140 | ++ ares__freeaddrinfo_nodes(head_res); | ||
| 141 | ++ return NULL; | ||
| 142 | ++ } | ||
| 143 | ++ memset(next_node->ai_addr, 0, sizeof(ares_align_sock_addr)); | ||
| 144 | ++ memcpy(next_node->ai_addr, &cached_addrinfo[i].ai_addr, cached_addrinfo[i].ai_addrlen); | ||
| 145 | ++ } | ||
| 146 | ++ struct ares_addrinfo *info = ares_malloc(sizeof(struct ares_addrinfo)); | ||
| 147 | ++ if (info == NULL) { | ||
| 148 | ++ ares__freeaddrinfo_nodes(head_res); | ||
| 149 | ++ return NULL; | ||
| 150 | ++ } | ||
| 151 | ++ memset(info, 0, sizeof(struct ares_addrinfo)); | ||
| 152 | ++ | ||
| 153 | ++ info->nodes = head_res->ai_next; | ||
| 154 | ++ ares_free(head_res); | ||
| 155 | ++ return info; | ||
| 156 | ++} | ||
| 157 | ++ | ||
| 158 | ++struct ares_addrinfo * | ||
| 159 | ++ares_get_dns_cache(const char *host, const char *service, const struct ares_addrinfo_hints *hints) { | ||
| 160 | ++ ares_cache_key_param_wrapper param = {0}; | ||
| 161 | ++ param.host = (char *) host; | ||
| 162 | ++ param.serv = (char *) service; | ||
| 163 | ++ struct addrinfo hint = {0}; | ||
| 164 | ++ ares_addrinfo_hints_to_addrinfo(hints, &hint); | ||
| 165 | ++ param.hint = &hint; | ||
| 166 | ++ | ||
| 167 | ++ ares_cached_addrinfo cached_addrinfo[MAX_RESULTS] = {0}; | ||
| 168 | ++ memset(cached_addrinfo, 0, sizeof(ares_cached_addrinfo) * MAX_RESULTS); | ||
| 169 | ++ uint32_t num = 0; | ||
| 170 | ++ if (NetSysGetResolvCache(0, param, cached_addrinfo, &num) != 0) { | ||
| 171 | ++ return NULL; | ||
| 172 | ++ } | ||
| 173 | ++ if (num == 0) { | ||
| 174 | ++ return NULL; | ||
| 175 | ++ } | ||
| 176 | ++ return ares_cached_addrinfo_to_ares_addrinfo(cached_addrinfo, num); | ||
| 177 | ++} | ||
| 178 | ++ | ||
| 179 | ++void ares_set_dns_cache(const char *host, const char *service, const struct ares_addrinfo_hints *hints, | ||
| 180 | ++ const struct ares_addrinfo *res) { | ||
| 181 | ++ ares_cache_key_param_wrapper param = {0}; | ||
| 182 | ++ param.host = (char *) host; | ||
| 183 | ++ param.serv = (char *) service; | ||
| 184 | ++ struct addrinfo hint = {0}; | ||
| 185 | ++ ares_addrinfo_hints_to_addrinfo(hints, &hint); | ||
| 186 | ++ param.hint = &hint; | ||
| 187 | ++ | ||
| 188 | ++ struct addrinfo *posix_res = ares_addrinfo_to_addrinfo(res); | ||
| 189 | ++ if (!posix_res) { | ||
| 190 | ++ return; | ||
| 191 | ++ } | ||
| 192 | ++ | ||
| 193 | ++ NetSysSetResolvCache(0, param, posix_res); | ||
| 194 | ++ ares_free_posix_addrinfo(posix_res); | ||
| 195 | ++} | ||
| 196 | ++ | ||
| 197 | ++#endif | ||
| 198 | ++ | ||
| 199 | + struct ares_addrinfo_cname *ares__malloc_addrinfo_cname() | ||
| 200 | + { | ||
| 201 | + struct ares_addrinfo_cname *cname = ares_malloc(sizeof(struct ares_addrinfo_cname)); | ||
| 202 | + static void end_hquery(struct host_query *hquery, int status) | ||
| 203 | + hquery->ai = NULL; | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 207 | ++ char serv[12] = {0}; | ||
| 208 | ++ sprintf(serv, "%d", hquery->port); | ||
| 209 | ++ ares_set_dns_cache(hquery->name, serv, &hquery->hints, hquery->ai); | ||
| 210 | ++#endif | ||
| 211 | + hquery->callback(hquery->arg, status, hquery->timeouts, hquery->ai); | ||
| 212 | + ares_free(hquery->name); | ||
| 213 | + ares_free(hquery); | ||
| 214 | + void ares_getaddrinfo(ares_channel channel, | ||
| 215 | + const struct ares_addrinfo_hints* hints, | ||
| 216 | + ares_addrinfo_callback callback, void* arg) | ||
| 217 | + { | ||
| 218 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 219 | ++ struct ares_addrinfo *cache_res = ares_get_dns_cache(name, service, hints); | ||
| 220 | ++ if (cache_res && cache_res->nodes) { | ||
| 221 | ++ callback(arg, ARES_SUCCESS, 0, cache_res); | ||
| 222 | ++ return; | ||
| 223 | ++ } | ||
| 224 | ++#endif | ||
| 225 | + struct host_query *hquery; | ||
| 226 | + unsigned short port = 0; | ||
| 227 | + int family; | ||
| 228 | +diff --git a/src/lib/ares_init.c b/src/lib/ares_init.c | ||
| 229 | +index de5d86c..7644e71 100644 | ||
| 230 | +--- a/src/lib/ares_init.c | ||
| 231 | ++++ b/src/lib/ares_init.c | ||
| 232 | + | ||
| 233 | + #undef WIN32 /* Redefined in MingW/MSVC headers */ | ||
| 234 | + #endif | ||
| 235 | + | ||
| 236 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 237 | ++#define MAX_SERVER_NUM 5 | ||
| 238 | ++#define MAX_SERVER_LENGTH 50 | ||
| 239 | ++ | ||
| 240 | ++struct resolv_config { | ||
| 241 | ++ int32_t error; | ||
| 242 | ++ int32_t timeout_ms; | ||
| 243 | ++ uint32_t retry_count; | ||
| 244 | ++ char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1]; | ||
| 245 | ++}; | ||
| 246 | ++ | ||
| 247 | ++int32_t NetSysGetResolvConf(uint16_t netid, struct resolv_config *config); | ||
| 248 | ++#endif | ||
| 249 | ++ | ||
| 250 | + static int init_by_options(ares_channel channel, | ||
| 251 | + const struct ares_options *options, | ||
| 252 | + int optmask); | ||
| 253 | + static int init_by_resolv_conf(ares_channel channel) | ||
| 254 | + int error; | ||
| 255 | + int update_domains; | ||
| 256 | + const char *resolvconf_path; | ||
| 257 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 258 | ++ int netid = 0; | ||
| 259 | ++ int ret = 0; | ||
| 260 | ++ int status_each = -1; | ||
| 261 | ++#endif | ||
| 262 | + | ||
| 263 | + /* Don't read resolv.conf and friends if we don't have to */ | ||
| 264 | + if (ARES_CONFIG_CHECK(channel)) | ||
| 265 | + static int init_by_resolv_conf(ares_channel channel) | ||
| 266 | + resolvconf_path = PATH_RESOLV_CONF; | ||
| 267 | + } | ||
| 268 | + | ||
| 269 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 270 | ++ struct resolv_config config = {0}; | ||
| 271 | ++ ret = NetSysGetResolvConf(netid, &config); | ||
| 272 | ++ if (ret < 0) { | ||
| 273 | ++ return ARES_ENONAME; | ||
| 274 | ++ } | ||
| 275 | ++ | ||
| 276 | ++ for (int i = 0; i < MAX_SERVER_NUM; ++i) { | ||
| 277 | ++ if (config.nameservers[i] == NULL || config.nameservers[i][0] == 0) { | ||
| 278 | ++ continue; | ||
| 279 | ++ } | ||
| 280 | ++ status_each = config_nameserver(&servers, &nservers, config.nameservers[i]); | ||
| 281 | ++ if (status_each == ARES_SUCCESS) { | ||
| 282 | ++ status = ARES_SUCCESS; | ||
| 283 | ++ } | ||
| 284 | ++ } | ||
| 285 | ++ | ||
| 286 | ++ if (status == ARES_SUCCESS && nservers > 0) { | ||
| 287 | ++ goto get_conf_ok; | ||
| 288 | ++ } | ||
| 289 | ++ | ||
| 290 | ++#endif | ||
| 291 | + fp = fopen(resolvconf_path, "r"); | ||
| 292 | + if (fp) { | ||
| 293 | + while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) | ||
| 294 | + static int init_by_resolv_conf(ares_channel channel) | ||
| 295 | + ares_free(sortlist); | ||
| 296 | + return status; | ||
| 297 | + } | ||
| 298 | +- | ||
| 299 | ++#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 300 | ++get_conf_ok: | ||
| 301 | ++#endif | ||
| 302 | + /* If we got any name server entries, fill them in. */ | ||
| 303 | + if (servers) | ||
| 304 | + { | ||
| 305 | +diff --git a/src/lib/ares_process.c b/src/lib/ares_process.c | ||
| 306 | +index 87329e3..026489e 100644 | ||
| 307 | +--- a/src/lib/ares_process.c | ||
| 308 | ++++ b/src/lib/ares_process.c | ||
| 309 | + static void process_answer(ares_channel channel, unsigned char *abuf, | ||
| 310 | + * don't accept the packet, and switch the query to TCP if we hadn't | ||
| 311 | + * done so already. | ||
| 312 | + */ | ||
| 313 | +- if ((tc || alen > packetsz) && !tcp && !(channel->flags & ARES_FLAG_IGNTC)) | ||
| 314 | ++ if (tc && !tcp && !(channel->flags & ARES_FLAG_IGNTC)) | ||
| 315 | + { | ||
| 316 | + if (!query->using_tcp) | ||
| 317 | + { | ||
| @@ -1,108 +0,0 @@ | |||
| 1 | -diff --git a/src/lib/ares_getaddrinfo.c b/src/lib/ares_getaddrinfo.c | ||
| 2 | -index 0a0225a..4c3668e 100644 | ||
| 3 | ---- a/src/lib/ares_getaddrinfo.c | ||
| 4 | -+++ b/src/lib/ares_getaddrinfo.c | ||
| 5 | - | ||
| 6 | - # include "ares_platform.h" | ||
| 7 | - #endif | ||
| 8 | - | ||
| 9 | -+#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 10 | -+#include <dlfcn.h> | ||
| 11 | -+#include <stdio.h> | ||
| 12 | -+ | ||
| 13 | -+#if DNS_CONFIG_DEBUG | ||
| 14 | -+#ifndef DNS_CONFIG_PRINT | ||
| 15 | -+#define DNS_CONFIG_PRINT(fmt, ...) printf("DNS " fmt "\n", ##__VA_ARGS__) | ||
| 16 | -+#endif | ||
| 17 | -+#else | ||
| 18 | -+#define DNS_CONFIG_PRINT(fmt, ...) | ||
| 19 | -+#endif | ||
| 20 | -+ | ||
| 21 | -+#define DNS_SO_PATH "libnetsys_client.z.so" | ||
| 22 | -+#define OHOS_JUDGE_IPV6_FUNC_NAME "NetSysIsIpv6Enable" | ||
| 23 | -+typedef int (*JudgeIpv6)(uint16_t netId); | ||
| 24 | -+ | ||
| 25 | -+static void *open_dns_lib(void) | ||
| 26 | -+{ | ||
| 27 | -+ static void *lib = NULL; | ||
| 28 | -+ if (lib != NULL) { | ||
| 29 | -+ return lib; | ||
| 30 | -+ } | ||
| 31 | -+ | ||
| 32 | -+ lib = dlopen(DNS_SO_PATH, RTLD_LAZY); | ||
| 33 | -+ if (lib == NULL) { | ||
| 34 | -+ DNS_CONFIG_PRINT("%s: dlopen %s failed: %s", __func__, DNS_SO_PATH, dlerror()); | ||
| 35 | -+ return NULL; | ||
| 36 | -+ } | ||
| 37 | -+ return lib; | ||
| 38 | -+} | ||
| 39 | -+ | ||
| 40 | -+static void *load_from_dns_lib(const char *symbol) | ||
| 41 | -+{ | ||
| 42 | -+ void *lib_handle = open_dns_lib(); | ||
| 43 | -+ if (lib_handle == NULL) { | ||
| 44 | -+ return NULL; | ||
| 45 | -+ } | ||
| 46 | -+ | ||
| 47 | -+ void *sym_addr = dlsym(lib_handle, symbol); | ||
| 48 | -+ if (sym_addr == NULL) { | ||
| 49 | -+ DNS_CONFIG_PRINT("%s: loading symbol %s with dlsym failed: %s", __func__, symbol, dlerror()); | ||
| 50 | -+ } | ||
| 51 | -+ return sym_addr; | ||
| 52 | -+} | ||
| 53 | -+ | ||
| 54 | -+static JudgeIpv6 load_ipv6_judger(void) | ||
| 55 | -+{ | ||
| 56 | -+ static JudgeIpv6 ipv6_judger = NULL; | ||
| 57 | -+ if (ipv6_judger != NULL) { | ||
| 58 | -+ return ipv6_judger; | ||
| 59 | -+ } | ||
| 60 | -+ ipv6_judger = (JudgeIpv6)load_from_dns_lib(OHOS_JUDGE_IPV6_FUNC_NAME); | ||
| 61 | -+ return ipv6_judger; | ||
| 62 | -+} | ||
| 63 | -+#endif | ||
| 64 | -+ | ||
| 65 | -+static int IsIpv6Enable() | ||
| 66 | -+{ | ||
| 67 | -+ int ret = 0; | ||
| 68 | -+#if OHOS_DNS_PROXY_BY_NETSYS | ||
| 69 | -+ JudgeIpv6 func = load_ipv6_judger(); | ||
| 70 | -+ if (!func) { | ||
| 71 | -+ return 0; | ||
| 72 | -+ } | ||
| 73 | -+ | ||
| 74 | -+ uint16_t netid = 0; | ||
| 75 | -+ ret = func(netid); | ||
| 76 | -+ if (ret < 0) { | ||
| 77 | -+ return 0; | ||
| 78 | -+ } | ||
| 79 | -+#endif | ||
| 80 | -+ return ret; | ||
| 81 | -+} | ||
| 82 | -+ | ||
| 83 | - struct host_query | ||
| 84 | - { | ||
| 85 | - ares_channel channel; | ||
| 86 | - static int next_dns_lookup(struct host_query *hquery) | ||
| 87 | - ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery); | ||
| 88 | - break; | ||
| 89 | - case AF_INET6: | ||
| 90 | -- hquery->remaining += 1; | ||
| 91 | -- ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery); | ||
| 92 | -+ if (IsIpv6Enable()) { | ||
| 93 | -+ hquery->remaining += 1; | ||
| 94 | -+ ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery); | ||
| 95 | -+ } | ||
| 96 | - break; | ||
| 97 | - case AF_UNSPEC: | ||
| 98 | -- hquery->remaining += 2; | ||
| 99 | -+ hquery->remaining += 1; | ||
| 100 | - ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery); | ||
| 101 | -- ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery); | ||
| 102 | -+ if (IsIpv6Enable()) { | ||
| 103 | -+ hquery->remaining += 1; | ||
| 104 | -+ ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery); | ||
| 105 | -+ } | ||
| 106 | - break; | ||
| 107 | - default: break; | ||
| 108 | - } | ||
| @@ -155,6 +155,7 @@ ohos_shared_library("c_ares") { | |||
| 155 | "$code_dir/include", | 155 | "$code_dir/include", |
| 156 | ] | 156 | ] |
| 157 | 157 | ||
| 158 | + external_deps = [ "netmanager_base:netsys_client" ] | ||
| 158 | install_enable = true | 159 | install_enable = true |
| 159 | subsystem_name = "thirdparty" | 160 | subsystem_name = "thirdparty" |
| 160 | part_name = "cares" | 161 | part_name = "cares" |
| @@ -174,6 +175,8 @@ ohos_static_library("c_ares_static") { | |||
| 174 | "$code_dir", | 175 | "$code_dir", |
| 175 | "$code_dir/include", | 176 | "$code_dir/include", |
| 176 | ] | 177 | ] |
| 178 | + | ||
| 179 | + external_deps = [ "netmanager_base:netsys_client" ] | ||
| 177 | subsystem_name = "thirdparty" | 180 | subsystem_name = "thirdparty" |
| 178 | part_name = "cares" | 181 | part_name = "cares" |
| 179 | } | 182 | } |
| @@ -21,7 +21,9 @@ | |||
| 21 | "rom": "", | 21 | "rom": "", |
| 22 | "ram": "", | 22 | "ram": "", |
| 23 | "deps": { | 23 | "deps": { |
| 24 | - "components": [], | 24 | + "components": [ |
| 25 | + "netmanager_base" | ||
| 26 | + ], | ||
| 25 | "third_party": [] | 27 | "third_party": [] |
| 26 | }, | 28 | }, |
| 27 | "build": { | 29 | "build": { |
| @@ -46,8 +46,7 @@ _all_patchs=( | |||
| 46 | "backport-005-CVE-2023-31147.patch" | 46 | "backport-005-CVE-2023-31147.patch" |
| 47 | "backport-CVE-2023-31124.patch" | 47 | "backport-CVE-2023-31124.patch" |
| 48 | "backport-CVE-2024-25629.patch" | 48 | "backport-CVE-2024-25629.patch" |
| 49 | - "0001-ADD-OHOS-DNS-PROXY-BY-NETSYS.patch" | 49 | + "0001-OHOS-DNS-BY-NETSYS.patch" |
| 50 | - "0002-ADD-OHOS-IPV6-PROXY-BY-NETSYS.patch" | ||
| 51 | ) | 50 | ) |
| 52 | for filename in "${_all_patchs[@]}" | 51 | for filename in "${_all_patchs[@]}" |
| 53 | do | 52 | do |