PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libc/netbsd/resolv/res_init.c

https://github.com/AndroDevLinux/android_bionic
C | 816 lines | 617 code | 62 blank | 137 comment | 231 complexity | 2159e224f6b79e2cfe5dbe61b2e62afe MD5 | raw file
  1. /* $NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $ */
  2. /*
  3. * Copyright (c) 1985, 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. /*
  35. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36. *
  37. * Permission to use, copy, modify, and distribute this software for any
  38. * purpose with or without fee is hereby granted, provided that the above
  39. * copyright notice and this permission notice appear in all copies, and that
  40. * the name of Digital Equipment Corporation not be used in advertising or
  41. * publicity pertaining to distribution of the document or software without
  42. * specific, written prior permission.
  43. *
  44. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  47. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51. * SOFTWARE.
  52. */
  53. /*
  54. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  55. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  56. *
  57. * Permission to use, copy, modify, and distribute this software for any
  58. * purpose with or without fee is hereby granted, provided that the above
  59. * copyright notice and this permission notice appear in all copies.
  60. *
  61. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  62. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  63. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  64. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  65. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  66. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  67. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  68. */
  69. #include <sys/cdefs.h>
  70. #if defined(LIBC_SCCS) && !defined(lint)
  71. #ifdef notdef
  72. static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
  73. static const char rcsid[] = "Id: res_init.c,v 1.9.2.5.4.2 2004/03/16 12:34:18 marka Exp";
  74. #else
  75. __RCSID("$NetBSD: res_init.c,v 1.8 2006/03/19 03:10:08 christos Exp $");
  76. #endif
  77. #endif /* LIBC_SCCS and not lint */
  78. #include <sys/types.h>
  79. #include <sys/param.h>
  80. #include <sys/socket.h>
  81. #include <sys/time.h>
  82. #include <netinet/in.h>
  83. #include <arpa/inet.h>
  84. #include "arpa_nameser.h"
  85. #include <ctype.h>
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. #include <unistd.h>
  90. #include <netdb.h>
  91. #ifdef ANDROID_CHANGES
  92. #include <errno.h>
  93. #include <fcntl.h>
  94. #include <sys/system_properties.h>
  95. #endif /* ANDROID_CHANGES */
  96. #ifndef MIN
  97. #define MIN(x,y) ((x)<(y)?(x):(y))
  98. #endif
  99. /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */
  100. #ifdef ANDROID_CHANGES
  101. #include "resolv_private.h"
  102. #else
  103. #include <resolv.h>
  104. #endif
  105. #include "res_private.h"
  106. /* Options. Should all be left alone. */
  107. #ifndef DEBUG
  108. #define DEBUG
  109. #endif
  110. static void res_setoptions __P((res_state, const char *, const char *));
  111. static const char sort_mask[] = "/&";
  112. #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
  113. static u_int32_t net_mask __P((struct in_addr));
  114. #if !defined(isascii) /* XXX - could be a function */
  115. # define isascii(c) (!(c & 0200))
  116. #endif
  117. /*
  118. * Resolver state default settings.
  119. */
  120. /*
  121. * Set up default settings. If the configuration file exist, the values
  122. * there will have precedence. Otherwise, the server address is set to
  123. * INADDR_ANY and the default domain name comes from the gethostname().
  124. *
  125. * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
  126. * rather than INADDR_ANY ("0.0.0.0") as the default name server address
  127. * since it was noted that INADDR_ANY actually meant ``the first interface
  128. * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
  129. * it had to be "up" in order for you to reach your own name server. It
  130. * was later decided that since the recommended practice is to always
  131. * install local static routes through 127.0.0.1 for all your network
  132. * interfaces, that we could solve this problem without a code change.
  133. *
  134. * The configuration file should always be used, since it is the only way
  135. * to specify a default domain. If you are running a server on your local
  136. * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
  137. * in the configuration file.
  138. *
  139. * Return 0 if completes successfully, -1 on error
  140. */
  141. int
  142. res_ninit(res_state statp) {
  143. extern int __res_vinit(res_state, int);
  144. return (__res_vinit(statp, 0));
  145. }
  146. /* This function has to be reachable by res_data.c but not publicly. */
  147. int
  148. __res_vinit(res_state statp, int preinit) {
  149. register FILE *fp;
  150. register char *cp, **pp;
  151. register int n;
  152. char buf[BUFSIZ];
  153. int nserv = 0; /* number of nameserver records read from file */
  154. int haveenv = 0;
  155. int havesearch = 0;
  156. int nsort = 0;
  157. char *net;
  158. int dots;
  159. union res_sockaddr_union u[2];
  160. if ((statp->options & RES_INIT) != 0U)
  161. res_ndestroy(statp);
  162. if (!preinit) {
  163. statp->retrans = RES_TIMEOUT;
  164. statp->retry = RES_DFLRETRY;
  165. statp->options = RES_DEFAULT;
  166. statp->id = res_randomid();
  167. }
  168. memset(u, 0, sizeof(u));
  169. #ifdef USELOOPBACK
  170. u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
  171. #else
  172. u[nserv].sin.sin_addr.s_addr = INADDR_ANY;
  173. #endif
  174. u[nserv].sin.sin_family = AF_INET;
  175. u[nserv].sin.sin_port = htons(NAMESERVER_PORT);
  176. #ifdef HAVE_SA_LEN
  177. u[nserv].sin.sin_len = sizeof(struct sockaddr_in);
  178. #endif
  179. nserv++;
  180. #ifdef HAS_INET6_STRUCTS
  181. #ifdef USELOOPBACK
  182. u[nserv].sin6.sin6_addr = in6addr_loopback;
  183. #else
  184. u[nserv].sin6.sin6_addr = in6addr_any;
  185. #endif
  186. u[nserv].sin6.sin6_family = AF_INET6;
  187. u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT);
  188. #ifdef HAVE_SA_LEN
  189. u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6);
  190. #endif
  191. nserv++;
  192. #endif
  193. statp->nscount = 0;
  194. statp->ndots = 1;
  195. statp->pfcode = 0;
  196. statp->_vcsock = -1;
  197. statp->_flags = 0;
  198. statp->qhook = NULL;
  199. statp->rhook = NULL;
  200. statp->_u._ext.nscount = 0;
  201. statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext));
  202. if (statp->_u._ext.ext != NULL) {
  203. memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext));
  204. statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr;
  205. strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa");
  206. strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int");
  207. }
  208. statp->nsort = 0;
  209. res_setservers(statp, u, nserv);
  210. #if 0 /* IGNORE THE ENVIRONMENT */
  211. /* Allow user to override the local domain definition */
  212. if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  213. (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
  214. statp->defdname[sizeof(statp->defdname) - 1] = '\0';
  215. haveenv++;
  216. /*
  217. * Set search list to be blank-separated strings
  218. * from rest of env value. Permits users of LOCALDOMAIN
  219. * to still have a search list, and anyone to set the
  220. * one that they want to use as an individual (even more
  221. * important now that the rfc1535 stuff restricts searches)
  222. */
  223. cp = statp->defdname;
  224. pp = statp->dnsrch;
  225. *pp++ = cp;
  226. for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
  227. if (*cp == '\n') /* silly backwards compat */
  228. break;
  229. else if (*cp == ' ' || *cp == '\t') {
  230. *cp = 0;
  231. n = 1;
  232. } else if (n) {
  233. *pp++ = cp;
  234. n = 0;
  235. havesearch = 1;
  236. }
  237. }
  238. /* null terminate last domain if there are excess */
  239. while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
  240. cp++;
  241. *cp = '\0';
  242. *pp++ = 0;
  243. }
  244. if (nserv > 0)
  245. statp->nscount = nserv;
  246. #endif
  247. #ifndef ANDROID_CHANGES /* !ANDROID_CHANGES - IGNORE resolv.conf in Android */
  248. #define MATCH(line, name) \
  249. (!strncmp(line, name, sizeof(name) - 1) && \
  250. (line[sizeof(name) - 1] == ' ' || \
  251. line[sizeof(name) - 1] == '\t'))
  252. nserv = 0;
  253. if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  254. /* read the config file */
  255. while (fgets(buf, sizeof(buf), fp) != NULL) {
  256. /* skip comments */
  257. if (*buf == ';' || *buf == '#')
  258. continue;
  259. /* read default domain name */
  260. if (MATCH(buf, "domain")) {
  261. if (haveenv) /* skip if have from environ */
  262. continue;
  263. cp = buf + sizeof("domain") - 1;
  264. while (*cp == ' ' || *cp == '\t')
  265. cp++;
  266. if ((*cp == '\0') || (*cp == '\n'))
  267. continue;
  268. strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
  269. statp->defdname[sizeof(statp->defdname) - 1] = '\0';
  270. if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
  271. *cp = '\0';
  272. havesearch = 0;
  273. continue;
  274. }
  275. /* set search list */
  276. if (MATCH(buf, "search")) {
  277. if (haveenv) /* skip if have from environ */
  278. continue;
  279. cp = buf + sizeof("search") - 1;
  280. while (*cp == ' ' || *cp == '\t')
  281. cp++;
  282. if ((*cp == '\0') || (*cp == '\n'))
  283. continue;
  284. strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
  285. statp->defdname[sizeof(statp->defdname) - 1] = '\0';
  286. if ((cp = strchr(statp->defdname, '\n')) != NULL)
  287. *cp = '\0';
  288. /*
  289. * Set search list to be blank-separated strings
  290. * on rest of line.
  291. */
  292. cp = statp->defdname;
  293. pp = statp->dnsrch;
  294. *pp++ = cp;
  295. for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
  296. if (*cp == ' ' || *cp == '\t') {
  297. *cp = 0;
  298. n = 1;
  299. } else if (n) {
  300. *pp++ = cp;
  301. n = 0;
  302. }
  303. }
  304. /* null terminate last domain if there are excess */
  305. while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  306. cp++;
  307. *cp = '\0';
  308. *pp++ = 0;
  309. havesearch = 1;
  310. continue;
  311. }
  312. /* read nameservers to query */
  313. if (MATCH(buf, "nameserver") && nserv < MAXNS) {
  314. struct addrinfo hints, *ai;
  315. char sbuf[NI_MAXSERV];
  316. const size_t minsiz =
  317. sizeof(statp->_u._ext.ext->nsaddrs[0]);
  318. cp = buf + sizeof("nameserver") - 1;
  319. while (*cp == ' ' || *cp == '\t')
  320. cp++;
  321. cp[strcspn(cp, ";# \t\n")] = '\0';
  322. if ((*cp != '\0') && (*cp != '\n')) {
  323. memset(&hints, 0, sizeof(hints));
  324. hints.ai_family = PF_UNSPEC;
  325. hints.ai_socktype = SOCK_DGRAM; /*dummy*/
  326. hints.ai_flags = AI_NUMERICHOST;
  327. sprintf(sbuf, "%u", NAMESERVER_PORT);
  328. if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 &&
  329. ai->ai_addrlen <= minsiz) {
  330. if (statp->_u._ext.ext != NULL) {
  331. memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
  332. ai->ai_addr, ai->ai_addrlen);
  333. }
  334. if (ai->ai_addrlen <=
  335. sizeof(statp->nsaddr_list[nserv])) {
  336. memcpy(&statp->nsaddr_list[nserv],
  337. ai->ai_addr, ai->ai_addrlen);
  338. } else
  339. statp->nsaddr_list[nserv].sin_family = 0;
  340. freeaddrinfo(ai);
  341. nserv++;
  342. }
  343. }
  344. continue;
  345. }
  346. if (MATCH(buf, "sortlist")) {
  347. struct in_addr a;
  348. cp = buf + sizeof("sortlist") - 1;
  349. while (nsort < MAXRESOLVSORT) {
  350. while (*cp == ' ' || *cp == '\t')
  351. cp++;
  352. if (*cp == '\0' || *cp == '\n' || *cp == ';')
  353. break;
  354. net = cp;
  355. while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
  356. isascii(*cp) && !isspace((unsigned char)*cp))
  357. cp++;
  358. n = *cp;
  359. *cp = 0;
  360. if (inet_aton(net, &a)) {
  361. statp->sort_list[nsort].addr = a;
  362. if (ISSORTMASK(n)) {
  363. *cp++ = n;
  364. net = cp;
  365. while (*cp && *cp != ';' &&
  366. isascii(*cp) &&
  367. !isspace((unsigned char)*cp))
  368. cp++;
  369. n = *cp;
  370. *cp = 0;
  371. if (inet_aton(net, &a)) {
  372. statp->sort_list[nsort].mask = a.s_addr;
  373. } else {
  374. statp->sort_list[nsort].mask =
  375. net_mask(statp->sort_list[nsort].addr);
  376. }
  377. } else {
  378. statp->sort_list[nsort].mask =
  379. net_mask(statp->sort_list[nsort].addr);
  380. }
  381. nsort++;
  382. }
  383. *cp = n;
  384. }
  385. continue;
  386. }
  387. if (MATCH(buf, "options")) {
  388. res_setoptions(statp, buf + sizeof("options") - 1, "conf");
  389. continue;
  390. }
  391. }
  392. if (nserv > 0)
  393. statp->nscount = nserv;
  394. statp->nsort = nsort;
  395. (void) fclose(fp);
  396. }
  397. #endif /* !ANDROID_CHANGES */
  398. /*
  399. * Last chance to get a nameserver. This should not normally
  400. * be necessary
  401. */
  402. #ifdef NO_RESOLV_CONF
  403. if(nserv == 0)
  404. nserv = get_nameservers(statp);
  405. #endif
  406. if (statp->defdname[0] == 0 &&
  407. gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
  408. (cp = strchr(buf, '.')) != NULL)
  409. strcpy(statp->defdname, cp + 1);
  410. /* find components of local domain that might be searched */
  411. if (havesearch == 0) {
  412. pp = statp->dnsrch;
  413. *pp++ = statp->defdname;
  414. *pp = NULL;
  415. dots = 0;
  416. for (cp = statp->defdname; *cp; cp++)
  417. dots += (*cp == '.');
  418. cp = statp->defdname;
  419. while (pp < statp->dnsrch + MAXDFLSRCH) {
  420. if (dots < LOCALDOMAINPARTS)
  421. break;
  422. cp = strchr(cp, '.') + 1; /* we know there is one */
  423. *pp++ = cp;
  424. dots--;
  425. }
  426. *pp = NULL;
  427. #ifdef DEBUG
  428. if (statp->options & RES_DEBUG) {
  429. printf(";; res_init()... default dnsrch list:\n");
  430. for (pp = statp->dnsrch; *pp; pp++)
  431. printf(";;\t%s\n", *pp);
  432. printf(";;\t..END..\n");
  433. }
  434. #endif
  435. }
  436. if ((cp = getenv("RES_OPTIONS")) != NULL)
  437. res_setoptions(statp, cp, "env");
  438. if (nserv > 0) {
  439. statp->nscount = nserv;
  440. statp->options |= RES_INIT;
  441. }
  442. return (0);
  443. }
  444. static void
  445. res_setoptions(res_state statp, const char *options, const char *source)
  446. {
  447. const char *cp = options;
  448. int i;
  449. struct __res_state_ext *ext = statp->_u._ext.ext;
  450. #ifdef DEBUG
  451. if (statp->options & RES_DEBUG)
  452. printf(";; res_setoptions(\"%s\", \"%s\")...\n",
  453. options, source);
  454. #endif
  455. while (*cp) {
  456. /* skip leading and inner runs of spaces */
  457. while (*cp == ' ' || *cp == '\t')
  458. cp++;
  459. /* search for and process individual options */
  460. if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
  461. i = atoi(cp + sizeof("ndots:") - 1);
  462. if (i <= RES_MAXNDOTS)
  463. statp->ndots = i;
  464. else
  465. statp->ndots = RES_MAXNDOTS;
  466. #ifdef DEBUG
  467. if (statp->options & RES_DEBUG)
  468. printf(";;\tndots=%d\n", statp->ndots);
  469. #endif
  470. } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
  471. i = atoi(cp + sizeof("timeout:") - 1);
  472. if (i <= RES_MAXRETRANS)
  473. statp->retrans = i;
  474. else
  475. statp->retrans = RES_MAXRETRANS;
  476. #ifdef DEBUG
  477. if (statp->options & RES_DEBUG)
  478. printf(";;\ttimeout=%d\n", statp->retrans);
  479. #endif
  480. } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
  481. i = atoi(cp + sizeof("attempts:") - 1);
  482. if (i <= RES_MAXRETRY)
  483. statp->retry = i;
  484. else
  485. statp->retry = RES_MAXRETRY;
  486. #ifdef DEBUG
  487. if (statp->options & RES_DEBUG)
  488. printf(";;\tattempts=%d\n", statp->retry);
  489. #endif
  490. } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
  491. #ifdef DEBUG
  492. if (!(statp->options & RES_DEBUG)) {
  493. printf(";; res_setoptions(\"%s\", \"%s\")..\n",
  494. options, source);
  495. statp->options |= RES_DEBUG;
  496. }
  497. printf(";;\tdebug\n");
  498. #endif
  499. } else if (!strncmp(cp, "no_tld_query",
  500. sizeof("no_tld_query") - 1) ||
  501. !strncmp(cp, "no-tld-query",
  502. sizeof("no-tld-query") - 1)) {
  503. statp->options |= RES_NOTLDQUERY;
  504. } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
  505. statp->options |= RES_USE_INET6;
  506. } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
  507. statp->options |= RES_ROTATE;
  508. } else if (!strncmp(cp, "no-check-names",
  509. sizeof("no-check-names") - 1)) {
  510. statp->options |= RES_NOCHECKNAME;
  511. }
  512. #ifdef RES_USE_EDNS0
  513. else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
  514. statp->options |= RES_USE_EDNS0;
  515. }
  516. #endif
  517. else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
  518. statp->options |= RES_USE_DNAME;
  519. }
  520. else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) {
  521. if (ext == NULL)
  522. goto skip;
  523. cp += sizeof("nibble:") - 1;
  524. i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1);
  525. strncpy(ext->nsuffix, cp, (size_t)i);
  526. ext->nsuffix[i] = '\0';
  527. }
  528. else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) {
  529. if (ext == NULL)
  530. goto skip;
  531. cp += sizeof("nibble2:") - 1;
  532. i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1);
  533. strncpy(ext->nsuffix2, cp, (size_t)i);
  534. ext->nsuffix2[i] = '\0';
  535. }
  536. else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) {
  537. cp += sizeof("v6revmode:") - 1;
  538. /* "nibble" and "bitstring" used to be valid */
  539. if (!strncmp(cp, "single", sizeof("single") - 1)) {
  540. statp->options |= RES_NO_NIBBLE2;
  541. } else if (!strncmp(cp, "both", sizeof("both") - 1)) {
  542. statp->options &=
  543. ~RES_NO_NIBBLE2;
  544. }
  545. }
  546. else {
  547. /* XXX - print a warning here? */
  548. }
  549. skip:
  550. /* skip to next run of spaces */
  551. while (*cp && *cp != ' ' && *cp != '\t')
  552. cp++;
  553. }
  554. }
  555. /* XXX - should really support CIDR which means explicit masks always. */
  556. static u_int32_t
  557. net_mask(in) /* XXX - should really use system's version of this */
  558. struct in_addr in;
  559. {
  560. register u_int32_t i = ntohl(in.s_addr);
  561. if (IN_CLASSA(i))
  562. return (htonl(IN_CLASSA_NET));
  563. else if (IN_CLASSB(i))
  564. return (htonl(IN_CLASSB_NET));
  565. return (htonl(IN_CLASSC_NET));
  566. }
  567. #ifdef ANDROID_CHANGES
  568. static int
  569. real_randomid(u_int *random_value) {
  570. /* open the nonblocking random device, returning -1 on failure */
  571. int random_device = open("/dev/urandom", O_RDONLY);
  572. if (random_device < 0) {
  573. return -1;
  574. }
  575. /* read from the random device, returning -1 on failure (or too many retries)*/
  576. u_int retry = 5;
  577. for (retry; retry > 0; retry--) {
  578. int retval = read(random_device, random_value, sizeof(u_int));
  579. if (retval == sizeof(u_int)) {
  580. *random_value &= 0xffff;
  581. close(random_device);
  582. return 0;
  583. } else if ((retval < 0) && (errno != EINTR)) {
  584. break;
  585. }
  586. }
  587. close(random_device);
  588. return -1;
  589. }
  590. #endif /* ANDROID_CHANGES */
  591. u_int
  592. res_randomid(void) {
  593. #ifdef ANDROID_CHANGES
  594. int status = 0;
  595. u_int output = 0;
  596. status = real_randomid(&output);
  597. if (status != -1) {
  598. return output;
  599. }
  600. #endif /* ANDROID_CHANGES */
  601. struct timeval now;
  602. gettimeofday(&now, NULL);
  603. return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
  604. }
  605. /*
  606. * This routine is for closing the socket if a virtual circuit is used and
  607. * the program wants to close it. This provides support for endhostent()
  608. * which expects to close the socket.
  609. *
  610. * This routine is not expected to be user visible.
  611. */
  612. void
  613. res_nclose(res_state statp) {
  614. int ns;
  615. if (statp->_vcsock >= 0) {
  616. (void) close(statp->_vcsock);
  617. statp->_vcsock = -1;
  618. statp->_flags &= ~(RES_F_VC | RES_F_CONN);
  619. }
  620. for (ns = 0; ns < statp->_u._ext.nscount; ns++) {
  621. if (statp->_u._ext.nssocks[ns] != -1) {
  622. (void) close(statp->_u._ext.nssocks[ns]);
  623. statp->_u._ext.nssocks[ns] = -1;
  624. }
  625. }
  626. }
  627. void
  628. res_ndestroy(res_state statp) {
  629. res_nclose(statp);
  630. if (statp->_u._ext.ext != NULL)
  631. free(statp->_u._ext.ext);
  632. statp->options &= ~RES_INIT;
  633. statp->_u._ext.ext = NULL;
  634. }
  635. const char *
  636. res_get_nibblesuffix(res_state statp) {
  637. if (statp->_u._ext.ext)
  638. return (statp->_u._ext.ext->nsuffix);
  639. return ("ip6.arpa");
  640. }
  641. const char *
  642. res_get_nibblesuffix2(res_state statp) {
  643. if (statp->_u._ext.ext)
  644. return (statp->_u._ext.ext->nsuffix2);
  645. return ("ip6.int");
  646. }
  647. void
  648. res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) {
  649. int i, nserv;
  650. size_t size;
  651. /* close open servers */
  652. res_nclose(statp);
  653. /* cause rtt times to be forgotten */
  654. statp->_u._ext.nscount = 0;
  655. nserv = 0;
  656. for (i = 0; i < cnt && nserv < MAXNS; i++) {
  657. switch (set->sin.sin_family) {
  658. case AF_INET:
  659. size = sizeof(set->sin);
  660. if (statp->_u._ext.ext)
  661. memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
  662. &set->sin, size);
  663. if (size <= sizeof(statp->nsaddr_list[nserv]))
  664. memcpy(&statp->nsaddr_list[nserv],
  665. &set->sin, size);
  666. #ifdef notdef
  667. else
  668. statp->nsaddr_list[nserv].sin_family = 0;
  669. #endif
  670. nserv++;
  671. break;
  672. #ifdef HAS_INET6_STRUCTS
  673. case AF_INET6:
  674. size = sizeof(set->sin6);
  675. if (statp->_u._ext.ext)
  676. memcpy(&statp->_u._ext.ext->nsaddrs[nserv],
  677. &set->sin6, size);
  678. if (size <= sizeof(statp->nsaddr_list[nserv]))
  679. memcpy(&statp->nsaddr_list[nserv],
  680. &set->sin6, size);
  681. else
  682. statp->nsaddr_list[nserv].sin_family = 0;
  683. nserv++;
  684. break;
  685. #endif
  686. default:
  687. break;
  688. }
  689. set++;
  690. }
  691. statp->nscount = nserv;
  692. }
  693. int
  694. res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) {
  695. int i;
  696. size_t size;
  697. u_int16_t family;
  698. for (i = 0; i < statp->nscount && i < cnt; i++) {
  699. if (statp->_u._ext.ext)
  700. family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family;
  701. else
  702. family = statp->nsaddr_list[i].sin_family;
  703. switch (family) {
  704. case AF_INET:
  705. size = sizeof(set->sin);
  706. if (statp->_u._ext.ext)
  707. memcpy(&set->sin,
  708. &statp->_u._ext.ext->nsaddrs[i],
  709. size);
  710. else
  711. memcpy(&set->sin, &statp->nsaddr_list[i],
  712. size);
  713. break;
  714. #ifdef HAS_INET6_STRUCTS
  715. case AF_INET6:
  716. size = sizeof(set->sin6);
  717. if (statp->_u._ext.ext)
  718. memcpy(&set->sin6,
  719. &statp->_u._ext.ext->nsaddrs[i],
  720. size);
  721. else
  722. memcpy(&set->sin6, &statp->nsaddr_list[i],
  723. size);
  724. break;
  725. #endif
  726. default:
  727. set->sin.sin_family = 0;
  728. break;
  729. }
  730. set++;
  731. }
  732. return (statp->nscount);
  733. }
  734. #ifdef ANDROID_CHANGES
  735. void res_setiface(res_state statp, const char* iface)
  736. {
  737. if (statp != NULL) {
  738. // set interface
  739. if (iface && iface[0] != '\0') {
  740. int len = sizeof(statp->iface);
  741. strncpy(statp->iface, iface, len - 1);
  742. statp->iface[len - 1] = '\0';
  743. } else {
  744. statp->iface[0] = '\0';
  745. }
  746. }
  747. }
  748. void res_setmark(res_state statp, int mark)
  749. {
  750. if (statp != NULL) {
  751. statp->_mark = mark;
  752. }
  753. }
  754. #endif /* ANDROID_CHANGES */