• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "lookup.h"
2 #include "stdio_impl.h"
3 #include <ctype.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <netinet/in.h>
8 
9 #include "hilog_adapter.h"
10 
11 #if 1
12 #define SIGCHAIN_LOG_DOMAIN 0xD003F00
13 #define SIGCHAIN_LOG_TAG "ressolvconf"
14 #define GETADDRINFO_PRINT_DEBUG(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \
15 	SIGCHAIN_LOG_DOMAIN, SIGCHAIN_LOG_TAG, __VA_ARGS__))
16 #else
17 #define GETADDRINFO_PRINT_DEBUG(...)
18 #endif
19 
20 #define DNS_RESOLV_CONF_PATH "/etc/resolv.conf"
21 
22 #if OHOS_DNS_PROXY_BY_NETSYS | OHOS_FWMARK_CLIENT_BY_NETSYS
23 #include "atomic.h"
24 
25 #include <dlfcn.h>
26 
open_dns_lib(void)27 static void *open_dns_lib(void)
28 {
29 	static void *dns_lib_handle = NULL;
30 	if (dns_lib_handle != NULL) {
31 		a_barrier();
32 		return dns_lib_handle;
33 	}
34 
35 	void *lib = dlopen(DNS_SO_PATH, RTLD_LAZY);
36 	if (lib == NULL) {
37 		DNS_CONFIG_PRINT("%s: dlopen %s failed: %s",
38 			__func__, DNS_SO_PATH, dlerror());
39 		return NULL;
40 	}
41 
42 	void *old_lib = a_cas_p(&dns_lib_handle, NULL, lib);
43 	if (old_lib == NULL) {
44 		DNS_CONFIG_PRINT("%s: %s loaded", __func__, DNS_SO_PATH);
45 		return lib;
46 	} else {
47 		/* Another thread has already loaded the library,
48 		 * dlclose is invoked to make refcount correct */
49 		DNS_CONFIG_PRINT("%s: %s has been loaded by another thread",
50 			__func__, DNS_SO_PATH);
51 		if (dlclose(lib)) {
52 			DNS_CONFIG_PRINT("%s: dlclose %s failed: %s",
53 				__func__, DNS_SO_PATH, dlerror());
54 		}
55 		return old_lib;
56 	}
57 }
58 
load_from_dns_lib(const char * symbol)59 static void *load_from_dns_lib(const char *symbol)
60 {
61 	void *lib_handle = open_dns_lib();
62 	if (lib_handle == NULL) {
63 		return NULL;
64 	}
65 
66 	void *sym_addr = dlsym(lib_handle, symbol);
67 	if (sym_addr == NULL) {
68 		DNS_CONFIG_PRINT("%s: loading symbol %s with dlsym failed: %s",
69 			__func__, symbol, dlerror());
70 	}
71 	return sym_addr;
72 }
73 
resolve_dns_sym(void ** holder,const char * symbol)74 void resolve_dns_sym(void **holder, const char *symbol)
75 {
76 	if (*holder != NULL) {
77 		a_barrier();
78 		return;
79 	}
80 
81 	void *ptr = load_from_dns_lib(symbol);
82 	if (ptr == NULL) {
83 		return;
84 	}
85 
86 	void *old_ptr = a_cas_p(holder, NULL, ptr);
87 	if (old_ptr != NULL) {
88 		DNS_CONFIG_PRINT("%s: %s has been found by another thread",
89 			__func__, symbol);
90 	} else {
91 		DNS_CONFIG_PRINT("%s: %s found", __func__, symbol);
92 	}
93 }
94 
load_config_getter(void)95 static GetConfig load_config_getter(void)
96 {
97 	static GetConfig config_getter = NULL;
98 	resolve_dns_sym((void **) &config_getter, OHOS_GET_CONFIG_FUNC_NAME);
99 	return config_getter;
100 }
101 
102 #endif
103 
__get_resolv_conf(struct resolvconf * conf,char * search,size_t search_sz)104 int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz)
105 {
106        return get_resolv_conf_ext(conf, search, search_sz, 0);
107 }
108 
get_resolv_conf_ext(struct resolvconf * conf,char * search,size_t search_sz,int netid)109 int get_resolv_conf_ext(struct resolvconf *conf, char *search, size_t search_sz, int netid)
110 {
111 	char line[256];
112 	unsigned char _buf[256];
113 	FILE *f, _f;
114 	int nns = 0;
115 
116 	conf->ndots = 1;
117 	conf->timeout = 5;
118 	conf->attempts = 2;
119 	if (search) *search = 0;
120 
121 #if OHOS_DNS_PROXY_BY_NETSYS
122 	GetConfig func = load_config_getter();
123 	if (!func) {
124 		DNS_CONFIG_PRINT("%s: loading %s failed, use %s as a fallback",
125 			__func__, OHOS_GET_CONFIG_FUNC_NAME, DNS_RESOLV_CONF_PATH);
126 		goto etc_resolv_conf;
127 	}
128 
129 	struct resolv_config config = {0};
130 	int ret = func(netid, &config);
131 	if (ret < 0) {
132 		DNS_CONFIG_PRINT("__get_resolv_conf OHOS_GET_CONFIG_FUNC_NAME err %d\n", ret);
133 		return EAI_NONAME;
134 	}
135 	int32_t timeout_second = config.timeout_ms / 1000;
136 
137 netsys_conf:
138 	if (timeout_second > 0) {
139 		if (timeout_second >= 60) {
140 			conf->timeout = 60;
141 		} else {
142 			conf->timeout = timeout_second;
143 		}
144 	}
145 	if (config.retry_count > 0) {
146 		if (config.retry_count >= 10) {
147 			conf->attempts = 10;
148 		} else {
149 			conf->attempts = config.retry_count;
150 		}
151 	}
152 	for (int i = 0; i < MAX_SERVER_NUM; ++i) {
153 		if (config.nameservers[i] == NULL || config.nameservers[i][0] == 0 || nns >= MAXNS) {
154 			continue;
155 		}
156 		if (__lookup_ipliteral(conf->ns + nns, config.nameservers[i], AF_UNSPEC) > 0) {
157 			nns++;
158 		}
159 	}
160 
161         if (nns != 0) {
162             goto get_conf_ok;
163         }
164 
165 etc_resolv_conf:
166 #endif
167 	f = __fopen_rb_ca(DNS_RESOLV_CONF_PATH, &_f, _buf, sizeof _buf);
168 	if (!f) switch (errno) {
169 	case ENOENT:
170 	case ENOTDIR:
171 	case EACCES:
172 		goto no_resolv_conf;
173 	default:
174 		return -1;
175 	}
176 
177 	while (fgets(line, sizeof line, f)) {
178 		char *p, *z;
179 		if (!strchr(line, '\n') && !feof(f)) {
180 			/* Ignore lines that get truncated rather than
181 			 * potentially misinterpreting them. */
182 			int c;
183 			do c = getc(f);
184 			while (c != '\n' && c != EOF);
185 			continue;
186 		}
187 		if (!strncmp(line, "options", 7) && isspace(line[7])) {
188 			p = strstr(line, "ndots:");
189 			if (p && isdigit(p[6])) {
190 				p += 6;
191 				unsigned long x = strtoul(p, &z, 10);
192 				if (z != p) conf->ndots = x > 15 ? 15 : x;
193 			}
194 			p = strstr(line, "attempts:");
195 			if (p && isdigit(p[9])) {
196 				p += 9;
197 				unsigned long x = strtoul(p, &z, 10);
198 				if (z != p) conf->attempts = x > 10 ? 10 : x;
199 			}
200 			p = strstr(line, "timeout:");
201 			if (p && (isdigit(p[8]) || p[8]=='.')) {
202 				p += 8;
203 				unsigned long x = strtoul(p, &z, 10);
204 				if (z != p) conf->timeout = x > 60 ? 60 : x;
205 			}
206 			continue;
207 		}
208 		if (!strncmp(line, "nameserver", 10) && isspace(line[10])) {
209 			if (nns >= MAXNS) continue;
210 			for (p=line+11; isspace(*p); p++);
211 			for (z=p; *z && !isspace(*z); z++);
212 			*z=0;
213 			if (__lookup_ipliteral(conf->ns+nns, p, AF_UNSPEC) > 0)
214 				nns++;
215 			continue;
216 		}
217 
218 		if (!search) continue;
219 		if ((strncmp(line, "domain", 6) && strncmp(line, "search", 6))
220 		    || !isspace(line[6]))
221 			continue;
222 		for (p=line+7; isspace(*p); p++);
223 		size_t l = strlen(p);
224 		/* This can never happen anyway with chosen buffer sizes. */
225 		if (l >= search_sz) continue;
226 		memcpy(search, p, l+1);
227 	}
228 
229 	__fclose_ca(f);
230 
231 no_resolv_conf:
232 	if (!nns) {
233 		__lookup_ipliteral(conf->ns, "127.0.0.1", AF_UNSPEC);
234 		nns = 1;
235 	}
236 
237 get_conf_ok:
238 	conf->nns = nns;
239 
240 	return 0;
241 }
242 
res_bind_socket(int fd,int netid)243 int res_bind_socket(int fd, int netid)
244 {
245 	int ret = -1;
246 	void* libhandler;
247 
248 	GETADDRINFO_PRINT_DEBUG("res_bind_socket netid:%{public}d \n", netid);
249 
250 #ifdef OHOS_FWMARK_CLIENT_BY_NETSYS
251 	libhandler = dlopen(FWMARKCLIENT_SO_PATH, RTLD_LAZY);
252 	if (libhandler == NULL) {
253 		GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlopen err %s\n", dlerror());
254 		return -1;
255 	}
256 
257 	BindSocket_Ext func = dlsym(libhandler, OHOS_BIND_SOCKET_FUNC_NAME);
258 	if (func == NULL) {
259 		GETADDRINFO_PRINT_DEBUG("dns_get_addr_info_from_netsys_cache dlsym err %s\n", dlerror());
260 		dlclose(libhandler);
261 		return -1;
262 	} else {
263 		ret = func(fd, netid);
264 		GETADDRINFO_PRINT_DEBUG("res_bind_socket ret %{public}d \n", ret);
265 		dlclose(libhandler);
266 	}
267 #endif
268 	return ret;
269 }
270 
271