2 * Copyright (C) 2016 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <netinet/in6.h>
34 #include <arpa/inet.h>
37 #include "resolv_private.h"
39 #define MAX_ADDRLEN (INET6_ADDRSTRLEN - (1 + 5))
40 #define MAX_HOSTLEN MAXHOSTNAMELEN
42 #define ESTIMATED_LINELEN 32
43 #define HCFILE_ALLOC_SIZE 256
45 /* From sethostent.c */
46 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
47 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
50 * Host cache entry for hcfile.c_data.
51 * Offsets are into hcfile.h_data.
52 * Strings are *not* terminated by NULL, but by whitespace (isspace) or '#'.
53 * Use hstr* functions with these.
62 * Overall host cache file state.
74 static struct hcfile hcfile;
75 static pthread_mutex_t hclock = PTHREAD_MUTEX_INITIALIZER;
77 static size_t hstrlen(const char *s)
80 while (*p && *p != '#' && !isspace(*p))
85 static int hstrcmp(const char *a, const char *b)
87 size_t alen = hstrlen(a);
88 size_t blen = hstrlen(b);
89 int res = strncmp(a, b, MIN(alen, blen));
95 static char *hstrcpy(char *dest, const char *src)
97 size_t len = hstrlen(src);
98 memcpy(dest, src, len);
103 static char *hstrdup(const char *s)
105 size_t len = hstrlen(s);
106 char *dest = (char *)malloc(len + 1);
109 memcpy(dest, s, len);
114 static int cmp_hcent_name(const void *a, const void *b)
116 struct hcent *ea = (struct hcent *)a;
117 const char *na = hcfile.h_data + ea->name;
118 struct hcent *eb = (struct hcent *)b;
119 const char *nb = hcfile.h_data + eb->name;
121 return hstrcmp(na, nb);
124 static struct hcent *_hcfindname(const char *name)
126 size_t first, last, mid;
127 struct hcent *cur = NULL;
130 if (hcfile.c_len == 0)
134 last = hcfile.c_len - 1;
135 mid = (first + last) / 2;
136 while (first <= last) {
137 cur = hcfile.c_data + mid;
138 cmp = hstrcmp(hcfile.h_data + cur->name, name);
149 mid = (first + last) / 2;
154 while (cur > hcfile.c_data) {
155 struct hcent *prev = cur - 1;
156 cmp = cmp_hcent_name(cur, prev);
166 * Find next name on line, if any.
168 * Assumes that line is terminated by LF.
170 static const char *_hcnextname(const char *name)
172 while (!isspace(*name)) {
177 while (isspace(*name)) {
187 static int _hcfilemmap(void)
192 const char *p, *pend;
195 h_fd = open(_PATH_HOSTS, O_RDONLY);
198 if (flock(h_fd, LOCK_EX) != 0) {
204 memset(&st, 0, sizeof(st));
205 if (fstat(h_fd, &st) == 0) {
206 if (st.st_size == hcfile.h_st.st_size &&
207 st.st_mtime == hcfile.h_st.st_mtime) {
208 flock(h_fd, LOCK_UN);
214 munmap(hcfile.h_data, hcfile.h_st.st_size);
216 memset(&hcfile, 0, sizeof(struct hcfile));
219 if (fstat(h_fd, &st) != 0) {
220 flock(h_fd, LOCK_UN);
224 h_addr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, h_fd, 0);
225 if (h_addr == MAP_FAILED) {
226 flock(h_fd, LOCK_UN);
233 hcfile.h_data = h_addr;
237 * Do an initial allocation if the file is "large". Estimate
238 * 32 bytes per line and define "large" as more than half of
239 * the alloc growth size (256 entries).
241 if (st.st_size >= ESTIMATED_LINELEN * HCFILE_ALLOC_SIZE / 2) {
242 c_alloc = st.st_size / ESTIMATED_LINELEN;
243 hcfile.c_data = malloc(c_alloc * sizeof(struct hcent));
244 if (!hcfile.c_data) {
249 p = (const char *)h_addr;
250 pend = p + st.st_size;
252 const char *eol, *addr, *name;
255 eol = memchr(p, '\n', pend - p);
259 if (*addr == '#' || *addr == '\n')
262 if (len > MAX_ADDRLEN)
265 while (name < eol && isspace(*name))
271 if (len < MAX_HOSTLEN) {
273 if (c_alloc <= hcfile.c_len) {
274 struct hcent *c_data;
275 c_alloc += HCFILE_ALLOC_SIZE;
276 c_data = realloc(hcfile.c_data, c_alloc * sizeof(struct hcent));
280 hcfile.c_data = c_data;
282 ent = hcfile.c_data + hcfile.c_len;
283 ent->addr = addr - h_addr;
284 ent->name = name - h_addr;
288 while (name < eol && isspace(*name))
293 qsort(hcfile.c_data, hcfile.c_len,
294 sizeof(struct hcent), cmp_hcent_name);
296 flock(h_fd, LOCK_UN);
302 munmap(hcfile.h_data, hcfile.h_st.st_size);
303 flock(hcfile.h_fd, LOCK_UN);
305 memset(&hcfile, 0, sizeof(struct hcfile));
310 * Caching version of getaddrinfo.
312 * If we find the requested host name in the cache, use getaddrinfo to
313 * populate the result for each address we find.
315 * Note glibc and bionic differ in the handling of ai_canonname. POSIX
316 * says that ai_canonname is only populated in the first result entry.
317 * glibc does this. bionic populates ai_canonname in all result entries.
318 * We choose the POSIX/glibc way here.
320 int hc_getaddrinfo(const char *host, const char *service,
321 const struct addrinfo *hints,
322 struct addrinfo **result)
325 struct hcent *ent, *cur;
327 struct addrinfo rhints;
328 struct addrinfo *last;
332 if (getenv("ANDROID_HOSTS_CACHE_DISABLE") != NULL)
335 /* Avoid needless work and recursion */
336 if (hints && (hints->ai_flags & AI_NUMERICHOST))
341 pthread_mutex_lock(&hclock);
343 if (_hcfilemmap() != 0) {
347 ent = _hcfindname(host);
354 canonname = (hints->ai_flags & AI_CANONNAME);
355 memcpy(&rhints, hints, sizeof(rhints));
356 rhints.ai_flags &= ~AI_CANONNAME;
359 memset(&rhints, 0, sizeof(rhints));
361 rhints.ai_flags |= AI_NUMERICHOST;
366 char addrstr[MAX_ADDRLEN];
367 struct addrinfo *res;
369 hstrcpy(addrstr, hcfile.h_data + cur->addr);
371 if (getaddrinfo(addrstr, service, &rhints, &res) == 0) {
373 (*result)->ai_next = res;
377 while (last->ai_next)
378 last = last->ai_next;
381 if(cur + 1 >= hcfile.c_data + hcfile.c_len)
383 cmp = cmp_hcent_name(cur, cur + 1);
389 /* This check is equivalent to (*result)->ai_next == NULL */
395 ai = (*result)->ai_next;
396 free(ai->ai_canonname);
397 ai->ai_canonname = hstrdup(hcfile.h_data + ent->name);
401 pthread_mutex_unlock(&hclock);
406 * Caching version of gethtbyname.
408 * Note glibc and bionic differ in the handling of aliases. glibc returns
409 * all aliases for all entries, regardless of whether they match h_addrtype.
410 * bionic returns only the aliases for the first hosts entry. We return all
411 * aliases for all IPv4 entries.
413 * Additionally, if an alias is IPv6 and the primary name for an alias also
414 * has an IPv4 entry, glibc will return the IPv4 address(es), but bionic
415 * will not. Neither do we.
417 int hc_gethtbyname(const char *host, int af, struct getnamaddr *info)
419 int ret = NETDB_SUCCESS;
420 struct hcent *ent, *cur;
423 unsigned int naliases = 0;
424 char *aliases[MAXALIASES];
425 unsigned int naddrs = 0;
426 char *addr_ptrs[MAXADDRS];
429 if (getenv("ANDROID_HOSTS_CACHE_DISABLE") != NULL)
430 return NETDB_INTERNAL;
433 case AF_INET: addrlen = NS_INADDRSZ; break;
434 case AF_INET6: addrlen = NS_IN6ADDRSZ; break;
436 return NETDB_INTERNAL;
439 pthread_mutex_lock(&hclock);
441 if (_hcfilemmap() != 0) {
442 ret = NETDB_INTERNAL;
446 ent = _hcfindname(host);
448 ret = HOST_NOT_FOUND;
455 char addrstr[MAX_ADDRLEN];
456 char namestr[MAX_HOSTLEN];
459 hstrcpy(addrstr, hcfile.h_data + cur->addr);
460 if (inet_pton(af, addrstr, &addr) == 1) {
462 /* First match is considered the official hostname */
464 hstrcpy(namestr, hcfile.h_data + cur->name);
465 HENT_SCOPY(info->hp->h_name, namestr, info->buf, info->buflen);
467 for (name = hcfile.h_data + cur->name; name; name = _hcnextname(name)) {
468 if (!hstrcmp(name, host))
470 hstrcpy(namestr, name);
471 HENT_SCOPY(aliases[naliases], namestr, info->buf, info->buflen);
473 if (naliases >= MAXALIASES)
476 aligned = (char *)ALIGN(info->buf);
477 if (info->buf != aligned) {
478 if ((ptrdiff_t)info->buflen < (aligned - info->buf))
480 info->buflen -= (aligned - info->buf);
483 HENT_COPY(addr_ptrs[naddrs], addr, addrlen, info->buf, info->buflen);
485 if (naddrs >= MAXADDRS)
489 if(cur + 1 >= hcfile.c_data + hcfile.c_len)
491 cmp = cmp_hcent_name(cur, cur + 1);
497 ret = HOST_NOT_FOUND;
501 addr_ptrs[naddrs++] = NULL;
502 aliases[naliases++] = NULL;
504 /* hp->h_name already populated */
505 HENT_ARRAY(info->hp->h_aliases, naliases, info->buf, info->buflen);
506 for (n = 0; n < naliases; ++n) {
507 info->hp->h_aliases[n] = aliases[n];
509 info->hp->h_addrtype = af;
510 info->hp->h_length = addrlen;
511 HENT_ARRAY(info->hp->h_addr_list, naddrs, info->buf, info->buflen);
512 for (n = 0; n < naddrs; ++n) {
513 info->hp->h_addr_list[n] = addr_ptrs[n];
517 pthread_mutex_unlock(&hclock);
522 ret = NETDB_INTERNAL;