• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include <sys/socket.h>
3 #include <sys/time.h>
4 #include <netinet/in.h>
5 #include <netdb.h>
6 #include <string.h>
7 #include <pthread.h>
8 #include <unistd.h>
9 #include <endian.h>
10 #include <errno.h>
11 #include "lookup.h"
12 
13 #define COST_FOR_MS 1000
14 #define COST_FOR_NANOSEC 1000000
15 #define DNS_QUERY_SUCCESS 0
16 #define DNS_QUERY_COMMOM_FAIL (-1)
17 #define GETADDRINFO_PRINT_DEBUG(...)
18 #define DNS_FAIL_REASON2_ROUND 99
19 #define DNS_FAIL_REASON3_ROUND 299
20 #define DNS_FAIL_REASON11_ROUND 99
21 
reportdnsresult(int netid,char * name,int usedtime,int queryret,struct addrinfo * res,struct queryparam * param)22 int reportdnsresult(int netid, char* name, int usedtime, int queryret, struct addrinfo *res, struct queryparam *param)
23 {
24 #if OHOS_DNS_PROXY_BY_NETSYS
25 	if (dns_post_result_to_netsys_cache(netid, name, usedtime, queryret, res, param) == 0) {
26 		GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext reportdnsresult fail\n");
27 	}
28 #endif
29 	return 0;
30 }
31 
32 static custom_dns_resolver g_customdnsresolvehook;
33 static pthread_key_t g_recursiveKey;
34 static int* g_recursive;
35 
setdnsresolvehook(custom_dns_resolver hookfunc)36 int setdnsresolvehook(custom_dns_resolver hookfunc)
37 {
38 	int ret = -1;
39 	if (g_customdnsresolvehook) {
40 		return ret;
41 	}
42 	if (hookfunc) {
43 		g_customdnsresolvehook = hookfunc;
44 		pthread_key_create(&g_recursiveKey, NULL);
45 		ret = 0;
46 	}
47 	return ret;
48 }
49 
removednsresolvehook()50 int removednsresolvehook()
51 {
52 	g_customdnsresolvehook = NULL;
53 	if (g_recursive) {
54 		free(g_recursive);
55 		g_recursive = NULL;
56 	}
57 	if (g_recursiveKey) {
58 		pthread_key_delete(g_recursiveKey);
59 		g_recursiveKey = NULL;
60 	}
61 	return 0;
62 }
63 
getaddrinfo_hook(const char * host,const char * serv,const struct addrinfo * hints,struct addrinfo ** res)64 int getaddrinfo_hook(const char* host, const char* serv, const struct addrinfo* hints,
65     struct addrinfo** res)
66 {
67     if (g_customdnsresolvehook) {
68         int ret = g_customdnsresolvehook(host, serv, hints, res);
69         if (ret == 0) {
70             return ret;
71         }
72     }
73     return predefined_host_lookup_ip(host, serv, hints, res);
74 }
75 
getaddrinfo(const char * restrict host,const char * restrict serv,const struct addrinfo * restrict hint,struct addrinfo ** restrict res)76 int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res)
77 {
78 	struct queryparam param = {0, 0, 0, 0, NULL};
79 	return getaddrinfo_ext(host, serv, hint, res, &param);
80 }
81 
getaddrinfo_ext(const char * restrict host,const char * restrict serv,const struct addrinfo * restrict hint,struct addrinfo ** restrict res,struct queryparam * restrict param)82 int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint,
83 					struct addrinfo **restrict res, struct queryparam *restrict param)
84 {
85 	int netid = 0;
86 	int type = 0;
87 	struct timeval timeStart, timeEnd;
88 
89 	if (!host && !serv) return EAI_NONAME;
90 	if (!param) {
91 		netid = 0;
92 		type = 0;
93 	} else {
94 		netid = param->qp_netid;
95 		type = param->qp_type;
96 	}
97 
98 	if (g_customdnsresolvehook) {
99 		g_recursive = pthread_getspecific(g_recursiveKey);
100 		if (g_recursive == NULL) {
101 			int *newRecursive = malloc(sizeof(int));
102 			*newRecursive = 0;
103 			pthread_setspecific(g_recursiveKey, newRecursive);
104 			g_recursive = newRecursive;
105 		}
106 		if (*g_recursive == 0) {
107 			++(*g_recursive);
108 			int ret = g_customdnsresolvehook(host, serv, hint, res);
109 			--(*g_recursive);
110 			return ret;
111 		}
112 	}
113 
114 #if OHOS_DNS_PROXY_BY_NETSYS
115 	GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext netid:%{public}d type:%{public}d \n", netid, type);
116 	if (type == QEURY_TYPE_NORMAL && predefined_host_is_contain_host(host) == 0) {
117 		if (dns_get_addr_info_from_netsys_cache2(netid, host, serv, hint, res) == 0) {
118 			GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext get from netsys cache OK\n");
119 			reportdnsresult(netid, (char *)host, 0, DNS_QUERY_SUCCESS, *res, param);
120 			return 0;
121 		}
122 	}
123 #endif
124 
125 	struct service ports[MAXSERVS];
126 	struct address addrs[MAXADDRS];
127 	char canon[256], *outcanon;
128 	int nservs, naddrs, nais, canon_len, i, j, k;
129 	int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0;
130 	struct aibuf *out;
131 
132 	if (hint) {
133 		family = hint->ai_family;
134 		flags = hint->ai_flags;
135 		proto = hint->ai_protocol;
136 		socktype = hint->ai_socktype;
137 
138 		const int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST |
139 			AI_V4MAPPED | AI_ALL | AI_ADDRCONFIG | AI_NUMERICSERV;
140 		if ((flags & mask) != flags) {
141 #ifndef __LITEOS__
142 			MUSL_LOGE("%{public}s: %{public}d: bad hint ai_flag: %{public}d", __func__, __LINE__, flags);
143 #endif
144 			return EAI_BADFLAGS;
145 		}
146 
147 		switch (family) {
148 		case AF_INET:
149 		case AF_INET6:
150 		case AF_UNSPEC:
151 			break;
152 		default:
153 #ifndef __LITEOS__
154 			MUSL_LOGE("%{public}s: %{public}d: wrong family in hint: %{public}d", __func__, __LINE__, family);
155 #endif
156 			return EAI_FAMILY;
157 		}
158 	}
159 
160 	if (flags & AI_ADDRCONFIG) {
161 		/* Define the "an address is configured" condition for address
162 		 * families via ability to create a socket for the family plus
163 		 * routability of the loopback address for the family. */
164 		static const struct sockaddr_in lo4 = {
165 			.sin_family = AF_INET, .sin_port = 65535,
166 			.sin_addr.s_addr = __BYTE_ORDER == __BIG_ENDIAN
167 				? 0x7f000001 : 0x0100007f
168 		};
169 		static const struct sockaddr_in6 lo6 = {
170 			.sin6_family = AF_INET6, .sin6_port = 65535,
171 			.sin6_addr = IN6ADDR_LOOPBACK_INIT
172 		};
173 		int tf[2] = { AF_INET, AF_INET6 };
174 		const void *ta[2] = { &lo4, &lo6 };
175 		socklen_t tl[2] = { sizeof lo4, sizeof lo6 };
176 		for (i = 0; i < 2; i++) {
177 			if (family == tf[1 - i]) continue;
178 			int s = socket(tf[i], SOCK_CLOEXEC | SOCK_DGRAM,
179 				IPPROTO_UDP);
180 #ifndef __LITEOS__
181 			if (s < 0) {
182 				MUSL_LOGE("%{public}s: %{public}d: create socket failed for family: %{public}d, errno: %{public}d",
183 					__func__, __LINE__, tf[i], errno);
184 			}
185 #endif
186 			if (s >= 0) {
187 				int cs;
188 				pthread_setcancelstate(
189 					PTHREAD_CANCEL_DISABLE, &cs);
190 				int r = connect(s, ta[i], tl[i]);
191 				int saved_errno = errno;
192 				pthread_setcancelstate(cs, 0);
193 				close(s);
194 				if (!r) continue;
195 				errno = saved_errno;
196 			}
197 			switch (errno) {
198 			case EADDRNOTAVAIL:
199 			case EAFNOSUPPORT:
200 			case EHOSTUNREACH:
201 			case ENETDOWN:
202 			case ENETUNREACH:
203 				break;
204 			default:
205 #ifndef __LITEOS__
206 				MUSL_LOGE("%{public}s: %{public}d: connect to local address failed: %{public}d",
207 					__func__, __LINE__, errno);
208 #endif
209 				return EAI_SYSTEM;
210 			}
211 			if (family == tf[i]) {
212 #ifndef __LITEOS__
213 				MUSL_LOGE("%{public}s: %{public}d: family mismatch: %{public}d", __func__, __LINE__, EAI_NONAME);
214 #endif
215                 return EAI_NONAME;
216 			}
217 			family = tf[1 - i];
218 		}
219 	}
220 
221 	int timeStartRet = gettimeofday(&timeStart, NULL);
222 	nservs = __lookup_serv(ports, serv, proto, socktype, flags);
223 	if (nservs < 0) return nservs;
224 
225 	naddrs = lookup_name_ext(addrs, canon, host, family, flags, netid);
226 	int timeEndRet = gettimeofday(&timeEnd, NULL);
227 	int t_cost = 0;
228 	if (timeStartRet == 0 && timeEndRet == 0) {
229 		t_cost = COST_FOR_NANOSEC * (timeEnd.tv_sec - timeStart.tv_sec) + (timeEnd.tv_usec - timeStart.tv_usec);
230 		t_cost /= COST_FOR_MS;
231 	}
232 	if (naddrs < 0) {
233 		reportdnsresult(netid, (char *)host, t_cost, naddrs, NULL, param);
234 #ifndef __LITEOS__
235 		MUSL_LOGE("%{public}s: %{public}d: reportdnsresult: %{public}d in process %{public}d",
236 			__func__, __LINE__, naddrs, getpid());
237 #endif
238 		naddrs = revert_dns_fail_cause(naddrs);
239 		return naddrs;
240 	}
241 
242 	nais = nservs * naddrs;
243 	canon_len = strlen(canon);
244 	out = calloc(1, nais * sizeof(*out) + canon_len + 1);
245 	if (!out) return EAI_MEMORY;
246 
247 	if (canon_len) {
248 		outcanon = (void *)&out[nais];
249 		memcpy(outcanon, canon, canon_len + 1);
250 	} else {
251 		outcanon = 0;
252 	}
253 
254 	for (k = i = 0; i < naddrs; i++) for (j = 0; j < nservs; j++, k++) {
255 		out[k].slot = k;
256 		out[k].ai = (struct addrinfo) {
257 			.ai_family = addrs[i].family,
258 			.ai_socktype = ports[j].socktype,
259 			.ai_protocol = ports[j].proto,
260 			.ai_addrlen = addrs[i].family == AF_INET
261 				? sizeof(struct sockaddr_in)
262 				: sizeof(struct sockaddr_in6),
263 			.ai_addr = (void *)&out[k].sa,
264 			.ai_canonname = outcanon };
265 		if (k) out[k-1].ai.ai_next = &out[k].ai;
266 		switch (addrs[i].family) {
267 		case AF_INET:
268 			out[k].sa.sin.sin_family = AF_INET;
269 			out[k].sa.sin.sin_port = htons(ports[j].port);
270 			memcpy(&out[k].sa.sin.sin_addr, &addrs[i].addr, 4);
271 			break;
272 		case AF_INET6:
273 			out[k].sa.sin6.sin6_family = AF_INET6;
274 			out[k].sa.sin6.sin6_port = htons(ports[j].port);
275 			out[k].sa.sin6.sin6_scope_id = addrs[i].scopeid;
276 			memcpy(&out[k].sa.sin6.sin6_addr, &addrs[i].addr, 16);
277 			break;
278 		}
279 	}
280 	out[0].ref = nais;
281 	*res = &out->ai;
282 
283 	reportdnsresult(netid, (char *)host, t_cost, DNS_QUERY_SUCCESS, *res, param);
284 	int cnt = predefined_host_is_contain_host(host);
285 #if OHOS_DNS_PROXY_BY_NETSYS
286 	if (type == QEURY_TYPE_NORMAL && cnt == 0) {
287 		dns_set_addr_info_to_netsys_cache2(netid, host, serv, hint, *res);
288 	}
289 #endif
290 	return 0;
291 }
292 
revert_dns_fail_cause(int cause)293 hidden int revert_dns_fail_cause(int cause)
294 {
295 	if (cause <= DNS_FAIL_REASON_PARAM_INVALID && cause > DNS_FAIL_REASON_PARAM_INVALID - DNS_FAIL_REASON2_ROUND) {
296 		return EAI_NONAME;
297 	}
298 	if (cause <= DNS_FAIL_REASON_ROUTE_CONFIG_ERR && cause > DNS_FAIL_REASON_ROUTE_CONFIG_ERR -
299 		DNS_FAIL_REASON3_ROUND) {
300 		return EAI_AGAIN;
301 	}
302 	if (cause <= DNS_FAIL_REASON_LACK_V6_SUPPORT && cause > DNS_FAIL_REASON_LACK_V6_SUPPORT -
303 		DNS_FAIL_REASON2_ROUND) {
304 		return EAI_SYSTEM;
305 	}
306 	return cause;
307 }
308