1 #include <stdlib.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <netdb.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 #include <endian.h>
9 #include <errno.h>
10 #include "lookup.h"
11
12 #define DNS_QUERY_SUCCESS 0
13 #define DNS_QUERY_COMMOM_FAIL (-1)
14 #define GETADDRINFO_PRINT_DEBUG(...)
15
reportdnsresult(int netid,char * name,int usedtime,int queryret,struct addrinfo * res,struct queryparam * param)16 int reportdnsresult(int netid, char* name, int usedtime, int queryret, struct addrinfo *res, struct queryparam *param)
17 {
18 #if OHOS_DNS_PROXY_BY_NETSYS
19 if (dns_post_result_to_netsys_cache(netid, name, usedtime, queryret, res, param) == 0) {
20 GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext reportdnsresult fail\n");
21 }
22 #endif
23 return 0;
24 }
25
26 static custom_dns_resolver g_customdnsresolvehook;
27 static pthread_key_t g_recursiveKey;
28 static int* g_recursive;
29
setdnsresolvehook(custom_dns_resolver hookfunc)30 int setdnsresolvehook(custom_dns_resolver hookfunc)
31 {
32 int ret = -1;
33 if (g_customdnsresolvehook) {
34 return ret;
35 }
36 if (hookfunc) {
37 g_customdnsresolvehook = hookfunc;
38 pthread_key_create(&g_recursiveKey, NULL);
39 ret = 0;
40 }
41 return ret;
42 }
43
removednsresolvehook()44 int removednsresolvehook()
45 {
46 g_customdnsresolvehook = NULL;
47 if (g_recursive) {
48 free(g_recursive);
49 g_recursive = NULL;
50 }
51 if (g_recursiveKey) {
52 pthread_key_delete(g_recursiveKey);
53 g_recursiveKey = NULL;
54 }
55 return 0;
56 }
57
getaddrinfo(const char * restrict host,const char * restrict serv,const struct addrinfo * restrict hint,struct addrinfo ** restrict res)58 int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res)
59 {
60 struct queryparam param = {0, 0, 0, 0, NULL};
61 return getaddrinfo_ext(host, serv, hint, res, ¶m);
62 }
63
getaddrinfo_ext(const char * restrict host,const char * restrict serv,const struct addrinfo * restrict hint,struct addrinfo ** restrict res,struct queryparam * restrict param)64 int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint,
65 struct addrinfo **restrict res, struct queryparam *restrict param)
66 {
67 int netid = 0;
68 int type = 0;
69 int usedtime = 0;
70 time_t t_start, t_end;
71
72 if (!host && !serv) return EAI_NONAME;
73 if (!param) {
74 netid = 0;
75 type = 0;
76 } else {
77 netid = param->qp_netid;
78 type = param->qp_type;
79 }
80
81 if (g_customdnsresolvehook) {
82 g_recursive = pthread_getspecific(g_recursiveKey);
83 if (g_recursive == NULL) {
84 int *newRecursive = malloc(sizeof(int));
85 *newRecursive = 0;
86 pthread_setspecific(g_recursiveKey, newRecursive);
87 g_recursive = newRecursive;
88 }
89 if (*g_recursive == 0) {
90 ++(*g_recursive);
91 int ret = g_customdnsresolvehook(host, serv, hint, res);
92 --(*g_recursive);
93 return ret;
94 }
95 }
96
97 #if OHOS_DNS_PROXY_BY_NETSYS
98 GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext netid:%{public}d type:%{public}d \n", netid, type);
99 if (type == QEURY_TYPE_NORMAL && predefined_host_is_contain_host(host) == 0) {
100 if (dns_get_addr_info_from_netsys_cache2(netid, host, serv, hint, res) == 0) {
101 GETADDRINFO_PRINT_DEBUG("getaddrinfo_ext get from netsys cache OK\n");
102 reportdnsresult(netid, host, 0, DNS_QUERY_SUCCESS, *res, param);
103 return 0;
104 }
105 }
106 #endif
107
108 struct service ports[MAXSERVS];
109 struct address addrs[MAXADDRS];
110 char canon[256], *outcanon;
111 int nservs, naddrs, nais, canon_len, i, j, k;
112 int family = AF_UNSPEC, flags = 0, proto = 0, socktype = 0;
113 struct aibuf *out;
114
115 if (hint) {
116 family = hint->ai_family;
117 flags = hint->ai_flags;
118 proto = hint->ai_protocol;
119 socktype = hint->ai_socktype;
120
121 const int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST |
122 AI_V4MAPPED | AI_ALL | AI_ADDRCONFIG | AI_NUMERICSERV;
123 if ((flags & mask) != flags)
124 return EAI_BADFLAGS;
125
126 switch (family) {
127 case AF_INET:
128 case AF_INET6:
129 case AF_UNSPEC:
130 break;
131 default:
132 return EAI_FAMILY;
133 }
134 }
135
136 if (flags & AI_ADDRCONFIG) {
137 /* Define the "an address is configured" condition for address
138 * families via ability to create a socket for the family plus
139 * routability of the loopback address for the family. */
140 static const struct sockaddr_in lo4 = {
141 .sin_family = AF_INET, .sin_port = 65535,
142 .sin_addr.s_addr = __BYTE_ORDER == __BIG_ENDIAN
143 ? 0x7f000001 : 0x0100007f
144 };
145 static const struct sockaddr_in6 lo6 = {
146 .sin6_family = AF_INET6, .sin6_port = 65535,
147 .sin6_addr = IN6ADDR_LOOPBACK_INIT
148 };
149 int tf[2] = { AF_INET, AF_INET6 };
150 const void *ta[2] = { &lo4, &lo6 };
151 socklen_t tl[2] = { sizeof lo4, sizeof lo6 };
152 for (i=0; i<2; i++) {
153 if (family==tf[1-i]) continue;
154 int s = socket(tf[i], SOCK_CLOEXEC|SOCK_DGRAM,
155 IPPROTO_UDP);
156 if (s>=0) {
157 int cs;
158 pthread_setcancelstate(
159 PTHREAD_CANCEL_DISABLE, &cs);
160 int r = connect(s, ta[i], tl[i]);
161 pthread_setcancelstate(cs, 0);
162 close(s);
163 if (!r) continue;
164 }
165 switch (errno) {
166 case EADDRNOTAVAIL:
167 case EAFNOSUPPORT:
168 case EHOSTUNREACH:
169 case ENETDOWN:
170 case ENETUNREACH:
171 break;
172 default:
173 return EAI_SYSTEM;
174 }
175 if (family == tf[i]) return EAI_NONAME;
176 family = tf[1-i];
177 }
178 }
179
180 t_start = time(NULL);
181 nservs = __lookup_serv(ports, serv, proto, socktype, flags);
182 if (nservs < 0) return nservs;
183
184 naddrs = lookup_name_ext(addrs, canon, host, family, flags, netid);
185 t_end = time(NULL);
186 if (naddrs < 0) {
187 reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_COMMOM_FAIL, NULL, param);
188 return naddrs;
189 }
190
191 nais = nservs * naddrs;
192 canon_len = strlen(canon);
193 out = calloc(1, nais * sizeof(*out) + canon_len + 1);
194 if (!out) return EAI_MEMORY;
195
196 if (canon_len) {
197 outcanon = (void *)&out[nais];
198 memcpy(outcanon, canon, canon_len+1);
199 } else {
200 outcanon = 0;
201 }
202
203 for (k=i=0; i<naddrs; i++) for (j=0; j<nservs; j++, k++) {
204 out[k].slot = k;
205 out[k].ai = (struct addrinfo){
206 .ai_family = addrs[i].family,
207 .ai_socktype = ports[j].socktype,
208 .ai_protocol = ports[j].proto,
209 .ai_addrlen = addrs[i].family == AF_INET
210 ? sizeof(struct sockaddr_in)
211 : sizeof(struct sockaddr_in6),
212 .ai_addr = (void *)&out[k].sa,
213 .ai_canonname = outcanon };
214 if (k) out[k-1].ai.ai_next = &out[k].ai;
215 switch (addrs[i].family) {
216 case AF_INET:
217 out[k].sa.sin.sin_family = AF_INET;
218 out[k].sa.sin.sin_port = htons(ports[j].port);
219 memcpy(&out[k].sa.sin.sin_addr, &addrs[i].addr, 4);
220 break;
221 case AF_INET6:
222 out[k].sa.sin6.sin6_family = AF_INET6;
223 out[k].sa.sin6.sin6_port = htons(ports[j].port);
224 out[k].sa.sin6.sin6_scope_id = addrs[i].scopeid;
225 memcpy(&out[k].sa.sin6.sin6_addr, &addrs[i].addr, 16);
226 break;
227 }
228 }
229 out[0].ref = nais;
230 *res = &out->ai;
231
232 reportdnsresult(netid, host, difftime(t_end, t_start), DNS_QUERY_SUCCESS, *res, param);
233 int cnt = predefined_host_is_contain_host(host);
234 #if OHOS_DNS_PROXY_BY_NETSYS
235 if (type == QEURY_TYPE_NORMAL && cnt == 0) {
236 dns_set_addr_info_to_netsys_cache2(netid, host, serv, hint, *res);
237 }
238 #endif
239 return 0;
240 }
241