PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sysdeps/posix/getaddrinfo.c

https://github.com/hjl-tools/glibc
C | 1914 lines | 1474 code | 241 blank | 199 comment | 561 complexity | 56ce82fa6a45d125364ed79f5c63aaa7 MD5 | raw file
  1. /* Host and service name lookups using Name Service Switch modules.
  2. Copyright (C) 1996-2020 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The Inner Net License, Version 2.00
  16. The author(s) grant permission for redistribution and use in source and
  17. binary forms, with or without modification, of the software and documentation
  18. provided that the following conditions are met:
  19. 0. If you receive a version of the software that is specifically labelled
  20. as not being for redistribution (check the version message and/or README),
  21. you are not permitted to redistribute that version of the software in any
  22. way or form.
  23. 1. All terms of the all other applicable copyrights and licenses must be
  24. followed.
  25. 2. Redistributions of source code must retain the authors' copyright
  26. notice(s), this list of conditions, and the following disclaimer.
  27. 3. Redistributions in binary form must reproduce the authors' copyright
  28. notice(s), this list of conditions, and the following disclaimer in the
  29. documentation and/or other materials provided with the distribution.
  30. 4. [The copyright holder has authorized the removal of this clause.]
  31. 5. Neither the name(s) of the author(s) nor the names of its contributors
  32. may be used to endorse or promote products derived from this software
  33. without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
  35. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
  38. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  39. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. If these license terms cause you a real problem, contact the author. */
  45. /* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
  46. #include <assert.h>
  47. #include <ctype.h>
  48. #include <errno.h>
  49. #include <ifaddrs.h>
  50. #include <netdb.h>
  51. #include <nss.h>
  52. #include <resolv/resolv-internal.h>
  53. #include <resolv/resolv_context.h>
  54. #include <stdbool.h>
  55. #include <stdio.h>
  56. #include <stdio_ext.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <stdint.h>
  60. #include <arpa/inet.h>
  61. #include <net/if.h>
  62. #include <netinet/in.h>
  63. #include <sys/socket.h>
  64. #include <sys/stat.h>
  65. #include <sys/types.h>
  66. #include <sys/un.h>
  67. #include <sys/utsname.h>
  68. #include <unistd.h>
  69. #include <nsswitch.h>
  70. #include <libc-lock.h>
  71. #include <not-cancel.h>
  72. #include <nscd/nscd-client.h>
  73. #include <nscd/nscd_proto.h>
  74. #include <scratch_buffer.h>
  75. #include <inet/net-internal.h>
  76. /* Former AI_IDN_ALLOW_UNASSIGNED and AI_IDN_USE_STD3_ASCII_RULES
  77. flags, now ignored. */
  78. #define DEPRECATED_AI_IDN 0x300
  79. #if IS_IN (libc)
  80. # define feof_unlocked(fp) __feof_unlocked (fp)
  81. #endif
  82. struct gaih_service
  83. {
  84. const char *name;
  85. int num;
  86. };
  87. struct gaih_servtuple
  88. {
  89. struct gaih_servtuple *next;
  90. int socktype;
  91. int protocol;
  92. int port;
  93. };
  94. static const struct gaih_servtuple nullserv;
  95. struct gaih_typeproto
  96. {
  97. int socktype;
  98. int protocol;
  99. uint8_t protoflag;
  100. bool defaultflag;
  101. char name[8];
  102. };
  103. /* Values for `protoflag'. */
  104. #define GAI_PROTO_NOSERVICE 1
  105. #define GAI_PROTO_PROTOANY 2
  106. static const struct gaih_typeproto gaih_inet_typeproto[] =
  107. {
  108. { 0, 0, 0, false, "" },
  109. { SOCK_STREAM, IPPROTO_TCP, 0, true, "tcp" },
  110. { SOCK_DGRAM, IPPROTO_UDP, 0, true, "udp" },
  111. #if defined SOCK_DCCP && defined IPPROTO_DCCP
  112. { SOCK_DCCP, IPPROTO_DCCP, 0, false, "dccp" },
  113. #endif
  114. #ifdef IPPROTO_UDPLITE
  115. { SOCK_DGRAM, IPPROTO_UDPLITE, 0, false, "udplite" },
  116. #endif
  117. #ifdef IPPROTO_SCTP
  118. { SOCK_STREAM, IPPROTO_SCTP, 0, false, "sctp" },
  119. { SOCK_SEQPACKET, IPPROTO_SCTP, 0, false, "sctp" },
  120. #endif
  121. { SOCK_RAW, 0, GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, true, "raw" },
  122. { 0, 0, 0, false, "" }
  123. };
  124. static const struct addrinfo default_hints =
  125. {
  126. .ai_flags = AI_DEFAULT,
  127. .ai_family = PF_UNSPEC,
  128. .ai_socktype = 0,
  129. .ai_protocol = 0,
  130. .ai_addrlen = 0,
  131. .ai_addr = NULL,
  132. .ai_canonname = NULL,
  133. .ai_next = NULL
  134. };
  135. static int
  136. gaih_inet_serv (const char *servicename, const struct gaih_typeproto *tp,
  137. const struct addrinfo *req, struct gaih_servtuple *st,
  138. struct scratch_buffer *tmpbuf)
  139. {
  140. struct servent *s;
  141. struct servent ts;
  142. int r;
  143. do
  144. {
  145. r = __getservbyname_r (servicename, tp->name, &ts,
  146. tmpbuf->data, tmpbuf->length, &s);
  147. if (r != 0 || s == NULL)
  148. {
  149. if (r == ERANGE)
  150. {
  151. if (!scratch_buffer_grow (tmpbuf))
  152. return -EAI_MEMORY;
  153. }
  154. else
  155. return -EAI_SERVICE;
  156. }
  157. }
  158. while (r);
  159. st->next = NULL;
  160. st->socktype = tp->socktype;
  161. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  162. ? req->ai_protocol : tp->protocol);
  163. st->port = s->s_port;
  164. return 0;
  165. }
  166. /* Convert struct hostent to a list of struct gaih_addrtuple objects.
  167. h_name is not copied, and the struct hostent object must not be
  168. deallocated prematurely. *RESULT must be NULL or a pointer to a
  169. linked-list. The new addresses are appended at the end. */
  170. static bool
  171. convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
  172. int family,
  173. struct hostent *h,
  174. struct gaih_addrtuple **result)
  175. {
  176. while (*result)
  177. result = &(*result)->next;
  178. /* Count the number of addresses in h->h_addr_list. */
  179. size_t count = 0;
  180. for (char **p = h->h_addr_list; *p != NULL; ++p)
  181. ++count;
  182. /* Report no data if no addresses are available, or if the incoming
  183. address size is larger than what we can store. */
  184. if (count == 0 || h->h_length > sizeof (((struct gaih_addrtuple) {}).addr))
  185. return true;
  186. struct gaih_addrtuple *array = calloc (count, sizeof (*array));
  187. if (array == NULL)
  188. return false;
  189. for (size_t i = 0; i < count; ++i)
  190. {
  191. if (family == AF_INET && req->ai_family == AF_INET6)
  192. {
  193. /* Perform address mapping. */
  194. array[i].family = AF_INET6;
  195. memcpy(array[i].addr + 3, h->h_addr_list[i], sizeof (uint32_t));
  196. array[i].addr[2] = htonl (0xffff);
  197. }
  198. else
  199. {
  200. array[i].family = family;
  201. memcpy (array[i].addr, h->h_addr_list[i], h->h_length);
  202. }
  203. array[i].next = array + i + 1;
  204. }
  205. array[0].name = h->h_name;
  206. array[count - 1].next = NULL;
  207. *result = array;
  208. return true;
  209. }
  210. #define gethosts(_family, _type) \
  211. { \
  212. struct hostent th; \
  213. char *localcanon = NULL; \
  214. no_data = 0; \
  215. while (1) \
  216. { \
  217. status = DL_CALL_FCT (fct, (name, _family, &th, \
  218. tmpbuf->data, tmpbuf->length, \
  219. &errno, &h_errno, NULL, &localcanon)); \
  220. if (status != NSS_STATUS_TRYAGAIN || h_errno != NETDB_INTERNAL \
  221. || errno != ERANGE) \
  222. break; \
  223. if (!scratch_buffer_grow (tmpbuf)) \
  224. { \
  225. __resolv_context_put (res_ctx); \
  226. result = -EAI_MEMORY; \
  227. goto free_and_return; \
  228. } \
  229. } \
  230. if (status == NSS_STATUS_NOTFOUND \
  231. || status == NSS_STATUS_TRYAGAIN || status == NSS_STATUS_UNAVAIL) \
  232. { \
  233. if (h_errno == NETDB_INTERNAL) \
  234. { \
  235. __resolv_context_put (res_ctx); \
  236. result = -EAI_SYSTEM; \
  237. goto free_and_return; \
  238. } \
  239. if (h_errno == TRY_AGAIN) \
  240. no_data = EAI_AGAIN; \
  241. else \
  242. no_data = h_errno == NO_DATA; \
  243. } \
  244. else if (status == NSS_STATUS_SUCCESS) \
  245. { \
  246. if (!convert_hostent_to_gaih_addrtuple (req, _family, &th, &addrmem)) \
  247. { \
  248. __resolv_context_put (res_ctx); \
  249. result = -EAI_SYSTEM; \
  250. goto free_and_return; \
  251. } \
  252. *pat = addrmem; \
  253. \
  254. if (localcanon != NULL && canon == NULL) \
  255. { \
  256. canonbuf = __strdup (localcanon); \
  257. if (canonbuf == NULL) \
  258. { \
  259. result = -EAI_SYSTEM; \
  260. goto free_and_return; \
  261. } \
  262. canon = canonbuf; \
  263. } \
  264. if (_family == AF_INET6 && *pat != NULL) \
  265. got_ipv6 = true; \
  266. } \
  267. }
  268. typedef enum nss_status (*nss_gethostbyname4_r)
  269. (const char *name, struct gaih_addrtuple **pat,
  270. char *buffer, size_t buflen, int *errnop,
  271. int *h_errnop, int32_t *ttlp);
  272. typedef enum nss_status (*nss_gethostbyname3_r)
  273. (const char *name, int af, struct hostent *host,
  274. char *buffer, size_t buflen, int *errnop,
  275. int *h_errnop, int32_t *ttlp, char **canonp);
  276. typedef enum nss_status (*nss_getcanonname_r)
  277. (const char *name, char *buffer, size_t buflen, char **result,
  278. int *errnop, int *h_errnop);
  279. /* This function is called if a canonical name is requested, but if
  280. the service function did not provide it. It tries to obtain the
  281. name using getcanonname_r from the same service NIP. If the name
  282. cannot be canonicalized, return a copy of NAME. Return NULL on
  283. memory allocation failure. The returned string is allocated on the
  284. heap; the caller has to free it. */
  285. static char *
  286. getcanonname (service_user *nip, struct gaih_addrtuple *at, const char *name)
  287. {
  288. nss_getcanonname_r cfct = __nss_lookup_function (nip, "getcanonname_r");
  289. char *s = (char *) name;
  290. if (cfct != NULL)
  291. {
  292. char buf[256];
  293. if (DL_CALL_FCT (cfct, (at->name ?: name, buf, sizeof (buf),
  294. &s, &errno, &h_errno)) != NSS_STATUS_SUCCESS)
  295. /* If the canonical name cannot be determined, use the passed
  296. string. */
  297. s = (char *) name;
  298. }
  299. return __strdup (name);
  300. }
  301. static int
  302. gaih_inet (const char *name, const struct gaih_service *service,
  303. const struct addrinfo *req, struct addrinfo **pai,
  304. unsigned int *naddrs, struct scratch_buffer *tmpbuf)
  305. {
  306. const struct gaih_typeproto *tp = gaih_inet_typeproto;
  307. struct gaih_servtuple *st = (struct gaih_servtuple *) &nullserv;
  308. struct gaih_addrtuple *at = NULL;
  309. bool got_ipv6 = false;
  310. const char *canon = NULL;
  311. const char *orig_name = name;
  312. /* Reserve stack memory for the scratch buffer in the getaddrinfo
  313. function. */
  314. size_t alloca_used = sizeof (struct scratch_buffer);
  315. if (req->ai_protocol || req->ai_socktype)
  316. {
  317. ++tp;
  318. while (tp->name[0]
  319. && ((req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  320. || (req->ai_protocol != 0
  321. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  322. && req->ai_protocol != tp->protocol)))
  323. ++tp;
  324. if (! tp->name[0])
  325. {
  326. if (req->ai_socktype)
  327. return -EAI_SOCKTYPE;
  328. else
  329. return -EAI_SERVICE;
  330. }
  331. }
  332. int port = 0;
  333. if (service != NULL)
  334. {
  335. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  336. return -EAI_SERVICE;
  337. if (service->num < 0)
  338. {
  339. if (tp->name[0])
  340. {
  341. st = (struct gaih_servtuple *)
  342. alloca_account (sizeof (struct gaih_servtuple), alloca_used);
  343. int rc = gaih_inet_serv (service->name, tp, req, st, tmpbuf);
  344. if (__glibc_unlikely (rc != 0))
  345. return rc;
  346. }
  347. else
  348. {
  349. struct gaih_servtuple **pst = &st;
  350. for (tp++; tp->name[0]; tp++)
  351. {
  352. struct gaih_servtuple *newp;
  353. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  354. continue;
  355. if (req->ai_socktype != 0
  356. && req->ai_socktype != tp->socktype)
  357. continue;
  358. if (req->ai_protocol != 0
  359. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  360. && req->ai_protocol != tp->protocol)
  361. continue;
  362. newp = (struct gaih_servtuple *)
  363. alloca_account (sizeof (struct gaih_servtuple),
  364. alloca_used);
  365. if (gaih_inet_serv (service->name,
  366. tp, req, newp, tmpbuf) != 0)
  367. continue;
  368. *pst = newp;
  369. pst = &(newp->next);
  370. }
  371. if (st == (struct gaih_servtuple *) &nullserv)
  372. return -EAI_SERVICE;
  373. }
  374. }
  375. else
  376. {
  377. port = htons (service->num);
  378. goto got_port;
  379. }
  380. }
  381. else
  382. {
  383. got_port:
  384. if (req->ai_socktype || req->ai_protocol)
  385. {
  386. st = alloca_account (sizeof (struct gaih_servtuple), alloca_used);
  387. st->next = NULL;
  388. st->socktype = tp->socktype;
  389. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  390. ? req->ai_protocol : tp->protocol);
  391. st->port = port;
  392. }
  393. else
  394. {
  395. /* Neither socket type nor protocol is set. Return all socket types
  396. we know about. */
  397. struct gaih_servtuple **lastp = &st;
  398. for (++tp; tp->name[0]; ++tp)
  399. if (tp->defaultflag)
  400. {
  401. struct gaih_servtuple *newp;
  402. newp = alloca_account (sizeof (struct gaih_servtuple),
  403. alloca_used);
  404. newp->next = NULL;
  405. newp->socktype = tp->socktype;
  406. newp->protocol = tp->protocol;
  407. newp->port = port;
  408. *lastp = newp;
  409. lastp = &newp->next;
  410. }
  411. }
  412. }
  413. bool malloc_name = false;
  414. struct gaih_addrtuple *addrmem = NULL;
  415. char *canonbuf = NULL;
  416. int result = 0;
  417. if (name != NULL)
  418. {
  419. at = alloca_account (sizeof (struct gaih_addrtuple), alloca_used);
  420. at->family = AF_UNSPEC;
  421. at->scopeid = 0;
  422. at->next = NULL;
  423. if (req->ai_flags & AI_IDN)
  424. {
  425. char *out;
  426. result = __idna_to_dns_encoding (name, &out);
  427. if (result != 0)
  428. return -result;
  429. name = out;
  430. malloc_name = true;
  431. }
  432. if (__inet_aton_exact (name, (struct in_addr *) at->addr) != 0)
  433. {
  434. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET)
  435. at->family = AF_INET;
  436. else if (req->ai_family == AF_INET6 && (req->ai_flags & AI_V4MAPPED))
  437. {
  438. at->addr[3] = at->addr[0];
  439. at->addr[2] = htonl (0xffff);
  440. at->addr[1] = 0;
  441. at->addr[0] = 0;
  442. at->family = AF_INET6;
  443. }
  444. else
  445. {
  446. result = -EAI_ADDRFAMILY;
  447. goto free_and_return;
  448. }
  449. if (req->ai_flags & AI_CANONNAME)
  450. canon = name;
  451. }
  452. else if (at->family == AF_UNSPEC)
  453. {
  454. char *scope_delim = strchr (name, SCOPE_DELIMITER);
  455. int e;
  456. if (scope_delim == NULL)
  457. e = inet_pton (AF_INET6, name, at->addr);
  458. else
  459. e = __inet_pton_length (AF_INET6, name, scope_delim - name,
  460. at->addr);
  461. if (e > 0)
  462. {
  463. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  464. at->family = AF_INET6;
  465. else if (req->ai_family == AF_INET
  466. && IN6_IS_ADDR_V4MAPPED (at->addr))
  467. {
  468. at->addr[0] = at->addr[3];
  469. at->family = AF_INET;
  470. }
  471. else
  472. {
  473. result = -EAI_ADDRFAMILY;
  474. goto free_and_return;
  475. }
  476. if (scope_delim != NULL
  477. && __inet6_scopeid_pton ((struct in6_addr *) at->addr,
  478. scope_delim + 1,
  479. &at->scopeid) != 0)
  480. {
  481. result = -EAI_NONAME;
  482. goto free_and_return;
  483. }
  484. if (req->ai_flags & AI_CANONNAME)
  485. canon = name;
  486. }
  487. }
  488. if (at->family == AF_UNSPEC && (req->ai_flags & AI_NUMERICHOST) == 0)
  489. {
  490. struct gaih_addrtuple **pat = &at;
  491. int no_data = 0;
  492. int no_inet6_data = 0;
  493. service_user *nip;
  494. enum nss_status inet6_status = NSS_STATUS_UNAVAIL;
  495. enum nss_status status = NSS_STATUS_UNAVAIL;
  496. int no_more;
  497. struct resolv_context *res_ctx = NULL;
  498. /* If we do not have to look for IPv6 addresses or the canonical
  499. name, use the simple, old functions, which do not support
  500. IPv6 scope ids, nor retrieving the canonical name. */
  501. if (req->ai_family == AF_INET
  502. && (req->ai_flags & AI_CANONNAME) == 0)
  503. {
  504. int rc;
  505. struct hostent th;
  506. struct hostent *h;
  507. while (1)
  508. {
  509. rc = __gethostbyname2_r (name, AF_INET, &th,
  510. tmpbuf->data, tmpbuf->length,
  511. &h, &h_errno);
  512. if (rc != ERANGE || h_errno != NETDB_INTERNAL)
  513. break;
  514. if (!scratch_buffer_grow (tmpbuf))
  515. {
  516. result = -EAI_MEMORY;
  517. goto free_and_return;
  518. }
  519. }
  520. if (rc == 0)
  521. {
  522. if (h != NULL)
  523. {
  524. /* We found data, convert it. */
  525. if (!convert_hostent_to_gaih_addrtuple
  526. (req, AF_INET, h, &addrmem))
  527. {
  528. result = -EAI_MEMORY;
  529. goto free_and_return;
  530. }
  531. *pat = addrmem;
  532. }
  533. else
  534. {
  535. if (h_errno == NO_DATA)
  536. result = -EAI_NODATA;
  537. else
  538. result = -EAI_NONAME;
  539. goto free_and_return;
  540. }
  541. }
  542. else
  543. {
  544. if (h_errno == NETDB_INTERNAL)
  545. result = -EAI_SYSTEM;
  546. else if (h_errno == TRY_AGAIN)
  547. result = -EAI_AGAIN;
  548. else
  549. /* We made requests but they turned out no data.
  550. The name is known, though. */
  551. result = -EAI_NODATA;
  552. goto free_and_return;
  553. }
  554. goto process_list;
  555. }
  556. #ifdef USE_NSCD
  557. if (__nss_not_use_nscd_hosts > 0
  558. && ++__nss_not_use_nscd_hosts > NSS_NSCD_RETRY)
  559. __nss_not_use_nscd_hosts = 0;
  560. if (!__nss_not_use_nscd_hosts
  561. && !__nss_database_custom[NSS_DBSIDX_hosts])
  562. {
  563. /* Try to use nscd. */
  564. struct nscd_ai_result *air = NULL;
  565. int err = __nscd_getai (name, &air, &h_errno);
  566. if (air != NULL)
  567. {
  568. /* Transform into gaih_addrtuple list. */
  569. bool added_canon = (req->ai_flags & AI_CANONNAME) == 0;
  570. char *addrs = air->addrs;
  571. addrmem = calloc (air->naddrs, sizeof (*addrmem));
  572. if (addrmem == NULL)
  573. {
  574. result = -EAI_MEMORY;
  575. goto free_and_return;
  576. }
  577. struct gaih_addrtuple *addrfree = addrmem;
  578. for (int i = 0; i < air->naddrs; ++i)
  579. {
  580. socklen_t size = (air->family[i] == AF_INET
  581. ? INADDRSZ : IN6ADDRSZ);
  582. if (!((air->family[i] == AF_INET
  583. && req->ai_family == AF_INET6
  584. && (req->ai_flags & AI_V4MAPPED) != 0)
  585. || req->ai_family == AF_UNSPEC
  586. || air->family[i] == req->ai_family))
  587. {
  588. /* Skip over non-matching result. */
  589. addrs += size;
  590. continue;
  591. }
  592. if (*pat == NULL)
  593. {
  594. *pat = addrfree++;
  595. (*pat)->scopeid = 0;
  596. }
  597. uint32_t *pataddr = (*pat)->addr;
  598. (*pat)->next = NULL;
  599. if (added_canon || air->canon == NULL)
  600. (*pat)->name = NULL;
  601. else if (canonbuf == NULL)
  602. {
  603. canonbuf = __strdup (air->canon);
  604. if (canonbuf == NULL)
  605. {
  606. result = -EAI_MEMORY;
  607. goto free_and_return;
  608. }
  609. canon = (*pat)->name = canonbuf;
  610. }
  611. if (air->family[i] == AF_INET
  612. && req->ai_family == AF_INET6
  613. && (req->ai_flags & AI_V4MAPPED))
  614. {
  615. (*pat)->family = AF_INET6;
  616. pataddr[3] = *(uint32_t *) addrs;
  617. pataddr[2] = htonl (0xffff);
  618. pataddr[1] = 0;
  619. pataddr[0] = 0;
  620. pat = &((*pat)->next);
  621. added_canon = true;
  622. }
  623. else if (req->ai_family == AF_UNSPEC
  624. || air->family[i] == req->ai_family)
  625. {
  626. (*pat)->family = air->family[i];
  627. memcpy (pataddr, addrs, size);
  628. pat = &((*pat)->next);
  629. added_canon = true;
  630. if (air->family[i] == AF_INET6)
  631. got_ipv6 = true;
  632. }
  633. addrs += size;
  634. }
  635. free (air);
  636. if (at->family == AF_UNSPEC)
  637. {
  638. result = -EAI_NONAME;
  639. goto free_and_return;
  640. }
  641. goto process_list;
  642. }
  643. else if (err == 0)
  644. /* The database contains a negative entry. */
  645. goto free_and_return;
  646. else if (__nss_not_use_nscd_hosts == 0)
  647. {
  648. if (h_errno == NETDB_INTERNAL && errno == ENOMEM)
  649. result = -EAI_MEMORY;
  650. else if (h_errno == TRY_AGAIN)
  651. result = -EAI_AGAIN;
  652. else
  653. result = -EAI_SYSTEM;
  654. goto free_and_return;
  655. }
  656. }
  657. #endif
  658. if (__nss_hosts_database == NULL)
  659. no_more = __nss_database_lookup2 ("hosts", NULL,
  660. "dns [!UNAVAIL=return] files",
  661. &__nss_hosts_database);
  662. else
  663. no_more = 0;
  664. nip = __nss_hosts_database;
  665. /* If we are looking for both IPv4 and IPv6 address we don't
  666. want the lookup functions to automatically promote IPv4
  667. addresses to IPv6 addresses, so we use the no_inet6
  668. function variant. */
  669. res_ctx = __resolv_context_get ();
  670. if (res_ctx == NULL)
  671. no_more = 1;
  672. while (!no_more)
  673. {
  674. no_data = 0;
  675. nss_gethostbyname4_r fct4 = NULL;
  676. /* gethostbyname4_r sends out parallel A and AAAA queries and
  677. is thus only suitable for PF_UNSPEC. */
  678. if (req->ai_family == PF_UNSPEC)
  679. fct4 = __nss_lookup_function (nip, "gethostbyname4_r");
  680. if (fct4 != NULL)
  681. {
  682. while (1)
  683. {
  684. status = DL_CALL_FCT (fct4, (name, pat,
  685. tmpbuf->data, tmpbuf->length,
  686. &errno, &h_errno,
  687. NULL));
  688. if (status == NSS_STATUS_SUCCESS)
  689. break;
  690. if (status != NSS_STATUS_TRYAGAIN
  691. || errno != ERANGE || h_errno != NETDB_INTERNAL)
  692. {
  693. if (h_errno == TRY_AGAIN)
  694. no_data = EAI_AGAIN;
  695. else
  696. no_data = h_errno == NO_DATA;
  697. break;
  698. }
  699. if (!scratch_buffer_grow (tmpbuf))
  700. {
  701. __resolv_context_put (res_ctx);
  702. result = -EAI_MEMORY;
  703. goto free_and_return;
  704. }
  705. }
  706. if (status == NSS_STATUS_SUCCESS)
  707. {
  708. assert (!no_data);
  709. no_data = 1;
  710. if ((req->ai_flags & AI_CANONNAME) != 0 && canon == NULL)
  711. canon = (*pat)->name;
  712. while (*pat != NULL)
  713. {
  714. if ((*pat)->family == AF_INET
  715. && req->ai_family == AF_INET6
  716. && (req->ai_flags & AI_V4MAPPED) != 0)
  717. {
  718. uint32_t *pataddr = (*pat)->addr;
  719. (*pat)->family = AF_INET6;
  720. pataddr[3] = pataddr[0];
  721. pataddr[2] = htonl (0xffff);
  722. pataddr[1] = 0;
  723. pataddr[0] = 0;
  724. pat = &((*pat)->next);
  725. no_data = 0;
  726. }
  727. else if (req->ai_family == AF_UNSPEC
  728. || (*pat)->family == req->ai_family)
  729. {
  730. pat = &((*pat)->next);
  731. no_data = 0;
  732. if (req->ai_family == AF_INET6)
  733. got_ipv6 = true;
  734. }
  735. else
  736. *pat = ((*pat)->next);
  737. }
  738. }
  739. no_inet6_data = no_data;
  740. }
  741. else
  742. {
  743. nss_gethostbyname3_r fct = NULL;
  744. if (req->ai_flags & AI_CANONNAME)
  745. /* No need to use this function if we do not look for
  746. the canonical name. The function does not exist in
  747. all NSS modules and therefore the lookup would
  748. often fail. */
  749. fct = __nss_lookup_function (nip, "gethostbyname3_r");
  750. if (fct == NULL)
  751. /* We are cheating here. The gethostbyname2_r
  752. function does not have the same interface as
  753. gethostbyname3_r but the extra arguments the
  754. latter takes are added at the end. So the
  755. gethostbyname2_r code will just ignore them. */
  756. fct = __nss_lookup_function (nip, "gethostbyname2_r");
  757. if (fct != NULL)
  758. {
  759. if (req->ai_family == AF_INET6
  760. || req->ai_family == AF_UNSPEC)
  761. {
  762. gethosts (AF_INET6, struct in6_addr);
  763. no_inet6_data = no_data;
  764. inet6_status = status;
  765. }
  766. if (req->ai_family == AF_INET
  767. || req->ai_family == AF_UNSPEC
  768. || (req->ai_family == AF_INET6
  769. && (req->ai_flags & AI_V4MAPPED)
  770. /* Avoid generating the mapped addresses if we
  771. know we are not going to need them. */
  772. && ((req->ai_flags & AI_ALL) || !got_ipv6)))
  773. {
  774. gethosts (AF_INET, struct in_addr);
  775. if (req->ai_family == AF_INET)
  776. {
  777. no_inet6_data = no_data;
  778. inet6_status = status;
  779. }
  780. }
  781. /* If we found one address for AF_INET or AF_INET6,
  782. don't continue the search. */
  783. if (inet6_status == NSS_STATUS_SUCCESS
  784. || status == NSS_STATUS_SUCCESS)
  785. {
  786. if ((req->ai_flags & AI_CANONNAME) != 0
  787. && canon == NULL)
  788. {
  789. canonbuf = getcanonname (nip, at, name);
  790. if (canonbuf == NULL)
  791. {
  792. __resolv_context_put (res_ctx);
  793. result = -EAI_MEMORY;
  794. goto free_and_return;
  795. }
  796. canon = canonbuf;
  797. }
  798. status = NSS_STATUS_SUCCESS;
  799. }
  800. else
  801. {
  802. /* We can have different states for AF_INET and
  803. AF_INET6. Try to find a useful one for both. */
  804. if (inet6_status == NSS_STATUS_TRYAGAIN)
  805. status = NSS_STATUS_TRYAGAIN;
  806. else if (status == NSS_STATUS_UNAVAIL
  807. && inet6_status != NSS_STATUS_UNAVAIL)
  808. status = inet6_status;
  809. }
  810. }
  811. else
  812. {
  813. /* Could not locate any of the lookup functions.
  814. The NSS lookup code does not consistently set
  815. errno, so we need to supply our own error
  816. code here. The root cause could either be a
  817. resource allocation failure, or a missing
  818. service function in the DSO (so it should not
  819. be listed in /etc/nsswitch.conf). Assume the
  820. former, and return EBUSY. */
  821. status = NSS_STATUS_UNAVAIL;
  822. __set_h_errno (NETDB_INTERNAL);
  823. __set_errno (EBUSY);
  824. }
  825. }
  826. if (nss_next_action (nip, status) == NSS_ACTION_RETURN)
  827. break;
  828. if (nip->next == NULL)
  829. no_more = -1;
  830. else
  831. nip = nip->next;
  832. }
  833. __resolv_context_put (res_ctx);
  834. /* If we have a failure which sets errno, report it using
  835. EAI_SYSTEM. */
  836. if ((status == NSS_STATUS_TRYAGAIN || status == NSS_STATUS_UNAVAIL)
  837. && h_errno == NETDB_INTERNAL)
  838. {
  839. result = -EAI_SYSTEM;
  840. goto free_and_return;
  841. }
  842. if (no_data != 0 && no_inet6_data != 0)
  843. {
  844. /* If both requests timed out report this. */
  845. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  846. result = -EAI_AGAIN;
  847. else
  848. /* We made requests but they turned out no data. The name
  849. is known, though. */
  850. result = -EAI_NODATA;
  851. goto free_and_return;
  852. }
  853. }
  854. process_list:
  855. if (at->family == AF_UNSPEC)
  856. {
  857. result = -EAI_NONAME;
  858. goto free_and_return;
  859. }
  860. }
  861. else
  862. {
  863. struct gaih_addrtuple *atr;
  864. atr = at = alloca_account (sizeof (struct gaih_addrtuple), alloca_used);
  865. memset (at, '\0', sizeof (struct gaih_addrtuple));
  866. if (req->ai_family == AF_UNSPEC)
  867. {
  868. at->next = __alloca (sizeof (struct gaih_addrtuple));
  869. memset (at->next, '\0', sizeof (struct gaih_addrtuple));
  870. }
  871. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  872. {
  873. at->family = AF_INET6;
  874. if ((req->ai_flags & AI_PASSIVE) == 0)
  875. memcpy (at->addr, &in6addr_loopback, sizeof (struct in6_addr));
  876. atr = at->next;
  877. }
  878. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET)
  879. {
  880. atr->family = AF_INET;
  881. if ((req->ai_flags & AI_PASSIVE) == 0)
  882. atr->addr[0] = htonl (INADDR_LOOPBACK);
  883. }
  884. }
  885. {
  886. struct gaih_servtuple *st2;
  887. struct gaih_addrtuple *at2 = at;
  888. size_t socklen;
  889. sa_family_t family;
  890. /*
  891. buffer is the size of an unformatted IPv6 address in printable format.
  892. */
  893. while (at2 != NULL)
  894. {
  895. /* Only the first entry gets the canonical name. */
  896. if (at2 == at && (req->ai_flags & AI_CANONNAME) != 0)
  897. {
  898. if (canon == NULL)
  899. /* If the canonical name cannot be determined, use
  900. the passed in string. */
  901. canon = orig_name;
  902. bool do_idn = req->ai_flags & AI_CANONIDN;
  903. if (do_idn)
  904. {
  905. char *out;
  906. int rc = __idna_from_dns_encoding (canon, &out);
  907. if (rc == 0)
  908. canon = out;
  909. else if (rc == EAI_IDN_ENCODE)
  910. /* Use the punycode name as a fallback. */
  911. do_idn = false;
  912. else
  913. {
  914. result = -rc;
  915. goto free_and_return;
  916. }
  917. }
  918. if (!do_idn)
  919. {
  920. if (canonbuf != NULL)
  921. /* We already allocated the string using malloc, but
  922. the buffer is now owned by canon. */
  923. canonbuf = NULL;
  924. else
  925. {
  926. canon = __strdup (canon);
  927. if (canon == NULL)
  928. {
  929. result = -EAI_MEMORY;
  930. goto free_and_return;
  931. }
  932. }
  933. }
  934. }
  935. family = at2->family;
  936. if (family == AF_INET6)
  937. {
  938. socklen = sizeof (struct sockaddr_in6);
  939. /* If we looked up IPv4 mapped address discard them here if
  940. the caller isn't interested in all address and we have
  941. found at least one IPv6 address. */
  942. if (got_ipv6
  943. && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED
  944. && IN6_IS_ADDR_V4MAPPED (at2->addr))
  945. goto ignore;
  946. }
  947. else
  948. socklen = sizeof (struct sockaddr_in);
  949. for (st2 = st; st2 != NULL; st2 = st2->next)
  950. {
  951. struct addrinfo *ai;
  952. ai = *pai = malloc (sizeof (struct addrinfo) + socklen);
  953. if (ai == NULL)
  954. {
  955. free ((char *) canon);
  956. result = -EAI_MEMORY;
  957. goto free_and_return;
  958. }
  959. ai->ai_flags = req->ai_flags;
  960. ai->ai_family = family;
  961. ai->ai_socktype = st2->socktype;
  962. ai->ai_protocol = st2->protocol;
  963. ai->ai_addrlen = socklen;
  964. ai->ai_addr = (void *) (ai + 1);
  965. /* We only add the canonical name once. */
  966. ai->ai_canonname = (char *) canon;
  967. canon = NULL;
  968. #ifdef _HAVE_SA_LEN
  969. ai->ai_addr->sa_len = socklen;
  970. #endif /* _HAVE_SA_LEN */
  971. ai->ai_addr->sa_family = family;
  972. /* In case of an allocation error the list must be NULL
  973. terminated. */
  974. ai->ai_next = NULL;
  975. if (family == AF_INET6)
  976. {
  977. struct sockaddr_in6 *sin6p =
  978. (struct sockaddr_in6 *) ai->ai_addr;
  979. sin6p->sin6_port = st2->port;
  980. sin6p->sin6_flowinfo = 0;
  981. memcpy (&sin6p->sin6_addr,
  982. at2->addr, sizeof (struct in6_addr));
  983. sin6p->sin6_scope_id = at2->scopeid;
  984. }
  985. else
  986. {
  987. struct sockaddr_in *sinp =
  988. (struct sockaddr_in *) ai->ai_addr;
  989. sinp->sin_port = st2->port;
  990. memcpy (&sinp->sin_addr,
  991. at2->addr, sizeof (struct in_addr));
  992. memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
  993. }
  994. pai = &(ai->ai_next);
  995. }
  996. ++*naddrs;
  997. ignore:
  998. at2 = at2->next;
  999. }
  1000. }
  1001. free_and_return:
  1002. if (malloc_name)
  1003. free ((char *) name);
  1004. free (addrmem);
  1005. free (canonbuf);
  1006. return result;
  1007. }
  1008. struct sort_result
  1009. {
  1010. struct addrinfo *dest_addr;
  1011. /* Using sockaddr_storage is for now overkill. We only support IPv4
  1012. and IPv6 so far. If this changes at some point we can adjust the
  1013. type here. */
  1014. struct sockaddr_in6 source_addr;
  1015. uint8_t source_addr_len;
  1016. bool got_source_addr;
  1017. uint8_t source_addr_flags;
  1018. uint8_t prefixlen;
  1019. uint32_t index;
  1020. int32_t native;
  1021. };
  1022. struct sort_result_combo
  1023. {
  1024. struct sort_result *results;
  1025. int nresults;
  1026. };
  1027. #if __BYTE_ORDER == __BIG_ENDIAN
  1028. # define htonl_c(n) n
  1029. #else
  1030. # define htonl_c(n) __bswap_constant_32 (n)
  1031. #endif
  1032. static const struct scopeentry
  1033. {
  1034. union
  1035. {
  1036. char addr[4];
  1037. uint32_t addr32;
  1038. };
  1039. uint32_t netmask;
  1040. int32_t scope;
  1041. } default_scopes[] =
  1042. {
  1043. /* Link-local addresses: scope 2. */
  1044. { { { 169, 254, 0, 0 } }, htonl_c (0xffff0000), 2 },
  1045. { { { 127, 0, 0, 0 } }, htonl_c (0xff000000), 2 },
  1046. /* Default: scope 14. */
  1047. { { { 0, 0, 0, 0 } }, htonl_c (0x00000000), 14 }
  1048. };
  1049. /* The label table. */
  1050. static const struct scopeentry *scopes;
  1051. static int
  1052. get_scope (const struct sockaddr_in6 *in6)
  1053. {
  1054. int scope;
  1055. if (in6->sin6_family == PF_INET6)
  1056. {
  1057. if (! IN6_IS_ADDR_MULTICAST (&in6->sin6_addr))
  1058. {
  1059. if (IN6_IS_ADDR_LINKLOCAL (&in6->sin6_addr)
  1060. /* RFC 4291 2.5.3 says that the loopback address is to be
  1061. treated like a link-local address. */
  1062. || IN6_IS_ADDR_LOOPBACK (&in6->sin6_addr))
  1063. scope = 2;
  1064. else if (IN6_IS_ADDR_SITELOCAL (&in6->sin6_addr))
  1065. scope = 5;
  1066. else
  1067. /* XXX Is this the correct default behavior? */
  1068. scope = 14;
  1069. }
  1070. else
  1071. scope = in6->sin6_addr.s6_addr[1] & 0xf;
  1072. }
  1073. else if (in6->sin6_family == PF_INET)
  1074. {
  1075. const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
  1076. size_t cnt = 0;
  1077. while (1)
  1078. {
  1079. if ((in->sin_addr.s_addr & scopes[cnt].netmask)
  1080. == scopes[cnt].addr32)
  1081. return scopes[cnt].scope;
  1082. ++cnt;
  1083. }
  1084. /* NOTREACHED */
  1085. }
  1086. else
  1087. /* XXX What is a good default? */
  1088. scope = 15;
  1089. return scope;
  1090. }
  1091. struct prefixentry
  1092. {
  1093. struct in6_addr prefix;
  1094. unsigned int bits;
  1095. int val;
  1096. };
  1097. /* The label table. */
  1098. static const struct prefixentry *labels;
  1099. /* Default labels. */
  1100. static const struct prefixentry default_labels[] =
  1101. {
  1102. /* See RFC 3484 for the details. */
  1103. { { .__in6_u
  1104. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
  1106. }, 128, 0 },
  1107. { { .__in6_u
  1108. = { .__u6_addr8 = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1110. }, 16, 2 },
  1111. { { .__in6_u
  1112. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1114. }, 96, 3 },
  1115. { { .__in6_u
  1116. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1117. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } }
  1118. }, 96, 4 },
  1119. /* The next two entries differ from RFC 3484. We need to treat
  1120. IPv6 site-local addresses special because they are never NATed,
  1121. unlike site-locale IPv4 addresses. If this would not happen, on
  1122. machines which have only IPv4 and IPv6 site-local addresses, the
  1123. sorting would prefer the IPv6 site-local addresses, causing
  1124. unnecessary delays when trying to connect to a global IPv6 address
  1125. through a site-local IPv6 address. */
  1126. { { .__in6_u
  1127. = { .__u6_addr8 = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1129. }, 10, 5 },
  1130. { { .__in6_u
  1131. = { .__u6_addr8 = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1133. }, 7, 6 },
  1134. /* Additional rule for Teredo tunnels. */
  1135. { { .__in6_u
  1136. = { .__u6_addr8 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1138. }, 32, 7 },
  1139. { { .__in6_u
  1140. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1142. }, 0, 1 }
  1143. };
  1144. /* The precedence table. */
  1145. static const struct prefixentry *precedence;
  1146. /* The default precedences. */
  1147. static const struct prefixentry default_precedence[] =
  1148. {
  1149. /* See RFC 3484 for the details. */
  1150. { { .__in6_u
  1151. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
  1153. }, 128, 50 },
  1154. { { .__in6_u
  1155. = { .__u6_addr8 = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1157. }, 16, 30 },
  1158. { { .__in6_u
  1159. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1161. }, 96, 20 },
  1162. { { .__in6_u
  1163. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1164. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } }
  1165. }, 96, 10 },
  1166. { { .__in6_u
  1167. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1169. }, 0, 40 }
  1170. };
  1171. static int
  1172. match_prefix (const struct sockaddr_in6 *in6,
  1173. const struct prefixentry *list, int default_val)
  1174. {
  1175. int idx;
  1176. struct sockaddr_in6 in6_mem;
  1177. if (in6->sin6_family == PF_INET)
  1178. {
  1179. const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
  1180. /* Construct a V4-to-6 mapped address. */
  1181. in6_mem.sin6_family = PF_INET6;
  1182. in6_mem.sin6_port = in->sin_port;
  1183. in6_mem.sin6_flowinfo = 0;
  1184. memset (&in6_mem.sin6_addr, '\0', sizeof (in6_mem.sin6_addr));
  1185. in6_mem.sin6_addr.s6_addr16[5] = 0xffff;
  1186. in6_mem.sin6_addr.s6_addr32[3] = in->sin_addr.s_addr;
  1187. in6_mem.sin6_scope_id = 0;
  1188. in6 = &in6_mem;
  1189. }
  1190. else if (in6->sin6_family != PF_INET6)
  1191. return default_val;
  1192. for (idx = 0; ; ++idx)
  1193. {
  1194. unsigned int bits = list[idx].bits;
  1195. const uint8_t *mask = list[idx].prefix.s6_addr;
  1196. const uint8_t *val = in6->sin6_addr.s6_addr;
  1197. while (bits >= 8)
  1198. {
  1199. if (*mask != *val)
  1200. break;
  1201. ++mask;
  1202. ++val;
  1203. bits -= 8;
  1204. }
  1205. if (bits < 8)
  1206. {
  1207. if ((*mask & (0xff00 >> bits)) == (*val & (0xff00 >> bits)))
  1208. /* Match! */
  1209. break;
  1210. }
  1211. }
  1212. return list[idx].val;
  1213. }
  1214. static int
  1215. get_label (const struct sockaddr_in6 *in6)
  1216. {
  1217. /* XXX What is a good default value? */
  1218. return match_prefix (in6, labels, INT_MAX);
  1219. }
  1220. static int
  1221. get_precedence (const struct sockaddr_in6 *in6)
  1222. {
  1223. /* XXX What is a good default value? */
  1224. return match_prefix (in6, precedence, 0);
  1225. }
  1226. /* Find last bit set in a word. */
  1227. static int
  1228. fls (uint32_t a)
  1229. {
  1230. uint32_t mask;
  1231. int n;
  1232. for (n = 0, mask = 1 << 31; n < 32; mask >>= 1, ++n)
  1233. if ((a & mask) != 0)
  1234. break;
  1235. return n;
  1236. }
  1237. static int
  1238. rfc3484_sort (const void *p1, const void *p2, void *arg)
  1239. {
  1240. const size_t idx1 = *(const size_t *) p1;
  1241. const size_t idx2 = *(const size_t *) p2;
  1242. struct sort_result_combo *src = (struct sort_result_combo *) arg;
  1243. struct sort_result *a1 = &src->results[idx1];
  1244. struct sort_result *a2 = &src->results[idx2];
  1245. /* Rule 1: Avoid unusable destinations.
  1246. We have the got_source_addr flag set if the destination is reachable. */
  1247. if (a1->got_source_addr && ! a2->got_source_addr)
  1248. return -1;
  1249. if (! a1->got_source_addr && a2->got_source_addr)
  1250. return 1;
  1251. /* Rule 2: Prefer matching scope. Only interesting if both
  1252. destination addresses are IPv6. */
  1253. int a1_dst_scope
  1254. = get_scope ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1255. int a2_dst_scope
  1256. = get_scope ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1257. if (a1->got_source_addr)
  1258. {
  1259. int a1_src_scope = get_scope (&a1->source_addr);
  1260. int a2_src_scope = get_scope (&a2->source_addr);
  1261. if (a1_dst_scope == a1_src_scope && a2_dst_scope != a2_src_scope)
  1262. return -1;
  1263. if (a1_dst_scope != a1_src_scope && a2_dst_scope == a2_src_scope)
  1264. return 1;
  1265. }
  1266. /* Rule 3: Avoid deprecated addresses. */
  1267. if (a1->got_source_addr)
  1268. {
  1269. if (!(a1->source_addr_flags & in6ai_deprecated)
  1270. && (a2->source_addr_flags & in6ai_deprecated))
  1271. return -1;
  1272. if ((a1->source_addr_flags & in6ai_deprecated)
  1273. && !(a2->source_addr_flags & in6ai_deprecated))
  1274. return 1;
  1275. }
  1276. /* Rule 4: Prefer home addresses. */
  1277. if (a1->got_source_addr)
  1278. {
  1279. if (!(a1->source_addr_flags & in6ai_homeaddress)
  1280. && (a2->source_addr_flags & in6ai_homeaddress))
  1281. return 1;
  1282. if ((a1->source_addr_flags & in6ai_homeaddress)
  1283. && !(a2->source_addr_flags & in6ai_homeaddress))
  1284. return -1;
  1285. }
  1286. /* Rule 5: Prefer matching label. */
  1287. if (a1->got_source_addr)
  1288. {
  1289. int a1_dst_label
  1290. = get_label ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1291. int a1_src_label = get_label (&a1->source_addr);
  1292. int a2_dst_label
  1293. = get_label ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1294. int a2_src_label = get_label (&a2->source_addr);
  1295. if (a1_dst_label == a1_src_label && a2_dst_label != a2_src_label)
  1296. return -1;
  1297. if (a1_dst_label != a1_src_label && a2_dst_label == a2_src_label)
  1298. return 1;
  1299. }
  1300. /* Rule 6: Prefer higher precedence. */
  1301. int a1_prec
  1302. = get_precedence ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1303. int a2_prec
  1304. = get_precedence ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1305. if (a1_prec > a2_prec)
  1306. return -1;
  1307. if (a1_prec < a2_prec)
  1308. return 1;
  1309. /* Rule 7: Prefer native transport. */
  1310. if (a1->got_source_addr)
  1311. {
  1312. /* The same interface index means the same interface which means
  1313. there is no difference in transport. This should catch many
  1314. (most?) cases. */
  1315. if (a1->index != a2->index)
  1316. {
  1317. int a1_native = a1->native;
  1318. int a2_native = a2->native;
  1319. if (a1_native == -1 || a2_native == -1)
  1320. {
  1321. uint32_t a1_index;
  1322. if (a1_native == -1)
  1323. {
  1324. /* If we do not have the information use 'native' as
  1325. the default. */
  1326. a1_native = 0;
  1327. a1_index = a1->index;
  1328. }
  1329. else
  1330. a1_index = 0xffffffffu;
  1331. uint32_t a2_index;
  1332. if (a2_native == -1)
  1333. {
  1334. /* If we do not have the information use 'native' as
  1335. the default. */
  1336. a2_native = 0;
  1337. a2_index = a2->index;
  1338. }
  1339. else
  1340. a2_index = 0xffffffffu;
  1341. __check_native (a1_index, &a1_native, a2_index, &a2_native);
  1342. /* Fill in the results in all the records. */
  1343. for (int i = 0; i < src->nresults; ++i)
  1344. if (a1_index != -1 && src->results[i].index == a1_index)
  1345. {
  1346. assert (src->results[i].native == -1
  1347. || src->results[i].native == a1_native);
  1348. src->results[i].native = a1_native;
  1349. }
  1350. else if (a2_index != -1 && src->results[i].index == a2_index)
  1351. {
  1352. assert (src->results[i].native == -1
  1353. || src->results[i].native == a2_native);
  1354. src->results[i].native = a2_native;
  1355. }
  1356. }
  1357. if (a1_native && !a2_native)
  1358. return -1;
  1359. if (!a1_native && a2_native)
  1360. return 1;
  1361. }
  1362. }
  1363. /* Rule 8: Prefer smaller scope. */
  1364. if (a1_dst_scope < a2_dst_scope)
  1365. return -1;
  1366. if (a1_dst_scope > a2_dst_scope)
  1367. return 1;
  1368. /* Rule 9: Use longest matching prefix. */
  1369. if (a1->got_source_addr
  1370. && a1->dest_addr->ai_family == a2->dest_addr->ai_family)
  1371. {
  1372. int bit1 = 0;
  1373. int bit2 = 0;
  1374. if (a1->dest_addr->ai_family == PF_INET)
  1375. {
  1376. assert (a1->source_addr.sin6_family == PF_INET);
  1377. assert (a2->source_addr.sin6_family == PF_INET);
  1378. /* Outside of subnets, as defined by the network masks,
  1379. common address prefixes for IPv4 addresses make no sense.
  1380. So, define a non-zero value only if source and
  1381. destination address are on the same subnet. */
  1382. struct sockaddr_in *in1_dst
  1383. = (struct sockaddr_in *) a1->dest_addr->ai_addr;
  1384. in_addr_t in1_dst_addr = ntohl (in1_dst->sin_addr.s_addr);
  1385. struct sockaddr_in *in1_src
  1386. = (struct sockaddr_in *) &a1->source_addr;
  1387. in_addr_t in1_src_addr = ntohl (in1_src->sin_addr.s_addr);
  1388. in_addr_t netmask1 = 0xffffffffu << (32 - a1->prefixlen);
  1389. if ((in1_src_addr & netmask1) == (in1_dst_addr & netmask1))
  1390. bit1 = fls (in1_dst_addr ^ in1_src_addr);
  1391. struct sockaddr_in *in2_dst
  1392. = (struct sockaddr_in *) a2->dest_addr->ai_addr;
  1393. in_addr_t in2_dst_addr = ntohl (in2_dst->sin_addr.s_addr);
  1394. struct sockaddr_in *in2_src
  1395. = (struct sockaddr_in *) &a2->source_addr;
  1396. in_addr_t in2_src_addr = ntohl (in2_src->sin_addr.s_addr);
  1397. in_addr_t netmask2 = 0xffffffffu << (32 - a2->prefixlen);
  1398. if ((in2_src_addr & netmask2) == (in2_dst_addr & netmask2))
  1399. bit2 = fls (in2_dst_addr ^ in2_src_addr);
  1400. }
  1401. else if (a1->dest_addr->ai_family == PF_INET6)
  1402. {
  1403. assert (a1->source_addr.sin6_family == PF_INET6);
  1404. assert (a2->source_addr.sin6_family == PF_INET6);
  1405. struct sockaddr_in6 *in1_dst;
  1406. struct sockaddr_in6 *in1_src;
  1407. struct sockaddr_in6 *in2_dst;
  1408. struct sockaddr_in6 *in2_src;
  1409. in1_dst = (struct sockaddr_in6 *) a1->dest_addr->ai_addr;
  1410. in1_src = (struct sockaddr_in6 *) &a1->source_addr;
  1411. in2_dst = (struct sockaddr_in6 *) a2->dest_addr->ai_addr;
  1412. in2_src = (struct sockaddr_in6 *) &a2->source_addr;
  1413. int i;
  1414. for (i = 0; i < 4; ++i)
  1415. if (in1_dst->sin6_addr.s6_addr32[i]
  1416. != in1_src->sin6_addr.s6_addr32[i]
  1417. || (in2_dst->sin6_addr.s6_addr32[i]
  1418. != in2_src->sin6_addr.s6_addr32[i]))
  1419. break;
  1420. if (i < 4)
  1421. {
  1422. bit1 = fls (ntohl (in1_dst->sin6_addr.s6_addr32[i]
  1423. ^ in1_src->sin6_addr.s6_addr32[i]));
  1424. bit2 = fls (ntohl (in2_dst->sin6_addr.s6_addr32[i]
  1425. ^ in2_src->sin6_addr.s6_addr32[i]));
  1426. }
  1427. }
  1428. if (bit1 > bit2)
  1429. return -1;
  1430. if (bit1 < bit2)
  1431. return 1;
  1432. }
  1433. /* Rule 10: Otherwise, leave the order unchanged. To ensure this
  1434. compare with the value indicating the order in which the entries
  1435. have been received from the services. NB: no two entries can have
  1436. the same order so the test will never return zero. */
  1437. return idx1 < idx2 ? -1 : 1;
  1438. }
  1439. static int
  1440. in6aicmp (const void *p1, const void *p2)
  1441. {
  1442. struct in6addrinfo *a1 = (struct in6addrinfo *) p1;
  1443. struct in6addrinfo *a2 = (struct in6addrinfo *) p2;
  1444. return memcmp (a1->addr, a2->addr, sizeof (a1->addr));
  1445. }
  1446. /* Name of the config file for RFC 3484 sorting (for now). */
  1447. #define GAICONF_FNAME "/etc/gai.conf"
  1448. /* Non-zero if we are supposed to reload the config file automatically
  1449. whenever it changed. */
  1450. static int gaiconf_reload_flag;
  1451. /* Non-zero if gaiconf_reload_flag was ever set to true. */
  1452. static int gaiconf_reload_flag_ever_set;
  1453. /* Last modification time. */
  1454. #ifdef _STATBUF_ST_NSEC
  1455. static struct timespec gaiconf_mtime;
  1456. static inline void
  1457. save_gaiconf_mtime (const struct stat64 *st)
  1458. {
  1459. gaiconf_mtime = st->st_mtim;
  1460. }
  1461. static inline bool
  1462. check_gaiconf_mtime (const struct stat64 *st)
  1463. {
  1464. return (st->st_mtim.tv_sec == gaiconf_mtime.tv_sec
  1465. && st->st_mtim.tv_nsec == gaiconf_mtime.tv_nsec);
  1466. }
  1467. #else
  1468. static time_t gaiconf_mtime;
  1469. static inline void
  1470. save_gaiconf_mtime (const struct stat64 *st)
  1471. {
  1472. gaiconf_mtime = st->st_mtime;
  1473. }
  1474. static inline bool
  1475. check_gaiconf_mtime (const struct stat64 *st)
  1476. {
  1477. return st->st_mtime == gaiconf_mtime;
  1478. }
  1479. #endif
  1480. libc_freeres_fn(fini)
  1481. {
  1482. if (labels != default_labels)
  1483. {
  1484. const struct prefixentry *old = labels;
  1485. labels = default_labels;
  1486. free ((void *) old);
  1487. }
  1488. if (precedence != default_precedence)
  1489. {
  1490. const struct prefixentry *old = precedence;
  1491. precedence = default_precedence;
  1492. free ((void *) old);
  1493. }
  1494. if (scopes != default_scopes)
  1495. {
  1496. const struct scopeentry *old = scopes;
  1497. scopes = default_scopes;
  1498. free ((void *) old);
  1499. }
  1500. }
  1501. struct prefixlist
  1502. {
  1503. struct prefixentry entry;
  1504. struct prefixlist *next;
  1505. };
  1506. struct scopelist
  1507. {
  1508. struct scopeentry entry;
  1509. struct scopelist *next;
  1510. };
  1511. static void
  1512. free_prefixlist (struct prefixlist *list)
  1513. {
  1514. while (list != NULL)
  1515. {
  1516. struct prefixlist *oldp = list;
  1517. list = list->next;
  1518. free (oldp);
  1519. }
  1520. }
  1521. static void
  1522. free_scopelist (struct scopelist *list)
  1523. {
  1524. while (list != NULL)
  1525. {
  1526. struct scopelist *oldp = list;
  1527. list = list->next;
  1528. free (oldp);
  1529. }
  1530. }
  1531. static int
  1532. prefixcmp (const void *p1, const void *p2)
  1533. {
  1534. const struct prefixentry *e1 = (const struct prefixentry *) p1;
  1535. const struct prefixentry *e2 = (const struct prefixentry *) p2;
  1536. if (e1->bits < e2->bits)
  1537. return 1;
  1538. if (e1->bits == e2->bits)
  1539. return 0;
  1540. return -1;
  1541. }
  1542. static int
  1543. scopecmp (const void *p1, const void *p2)
  1544. {
  1545. const struct scopeentry *e1 = (const struct scopeentry *) p1;
  1546. const struct scopeentry *e2 = (const struct scopeentry *) p2;
  1547. if (e1->netmask > e2->netmask)
  1548. return -1;
  1549. if (e1->netmask == e2->netmask)
  1550. return 0;
  1551. return 1;
  1552. }
  1553. static void
  1554. gaiconf_init (void)
  1555. {
  1556. struct prefixlist *labellist = NULL;
  1557. size_t nlabellist = 0;
  1558. bool labellist_nullbits = false;
  1559. struct prefixlist *precedencelist = NULL;
  1560. size_t nprecedencelist = 0;
  1561. bool precedencelist_nullbits = false;
  1562. struct scopelist *scopelist = NULL;
  1563. size_t nscopelist = 0;
  1564. bool scopelist_nullbits = false;
  1565. FILE *fp = fopen (GAICONF_FNAME, "rce");
  1566. if (fp != NULL)
  1567. {
  1568. struct stat64 st;
  1569. if (__fxstat64 (_STAT_VER, fileno (fp), &st) != 0)
  1570. {
  1571. fclose (fp);
  1572. goto no_file;
  1573. }
  1574. char *line = NULL;
  1575. size_t linelen = 0;
  1576. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  1577. while (!feof_unlocked (fp))
  1578. {
  1579. ssize_t n = __getline (&line, &linelen, fp);
  1580. if (n <= 0)
  1581. break;
  1582. /* Handle comments. No escaping possible so this is easy. */
  1583. char *cp = strchr (line, '#');
  1584. if (cp != NULL)
  1585. *cp = '\0';
  1586. cp = line;
  1587. while (isspace (*cp))
  1588. ++cp;
  1589. char *cmd = cp;
  1590. while (*cp != '\0' && !isspace (*cp))
  1591. ++cp;
  1592. size_t cmdlen = cp - cmd;
  1593. if (*cp != '\0')
  1594. *cp++ = '\0';
  1595. while (isspace (*cp))
  1596. ++cp;
  1597. char *val1 = cp;
  1598. while (*cp != '\0' && !isspace (*cp))
  1599. ++cp;
  1600. size_t val1len = cp - cmd;
  1601. /* We always need at least two values. */
  1602. if (val1len == 0)
  1603. continue;
  1604. if (*cp != '\0')
  1605. *cp++ = '\0';
  1606. while (isspace (*cp))
  1607. ++cp;
  1608. char *val2 = cp;
  1609. while (*cp != '\0' && !isspace (*cp))
  1610. ++cp;
  1611. /* Ignore the rest of the line. */
  1612. *cp = '\0';
  1613. struct prefixlist **listp;
  1614. size_t *lenp;
  1615. bool *nullbitsp;
  1616. switch (cmdlen)
  1617. {
  1618. case 5:
  1619. if (strcmp (cmd, "label") == 0)
  1620. {
  1621. struct in6_addr prefix;
  1622. unsigned long int bits;
  1623. unsigned long int val;
  1624. char *endp;
  1625. listp = &labellist;
  1626. lenp = &nlabellist;
  1627. nullbitsp = &labellist_nullbits;
  1628. new_elem:
  1629. bits = 128;
  1630. __set_errno (0);
  1631. cp = strchr (val1, '/');
  1632. if (cp != NULL)
  1633. *cp++ = '\0';
  1634. if (inet_pton (AF_INET6, val1, &prefix)
  1635. && (cp == NULL
  1636. || (bits = strtoul (cp, &endp, 10)) != ULONG_MAX
  1637. || errno != ERANGE)
  1638. && *endp == '\0'
  1639. && bits <= 128
  1640. && ((val = strtoul (val2, &endp, 10)) != ULONG_MAX
  1641. || errno != ERANGE)
  1642. && *endp == '\0'
  1643. && val <= INT_MAX)
  1644. {
  1645. struct prefixlist *newp = malloc (sizeof (*newp));
  1646. if (newp == NULL)
  1647. {
  1648. free (line);
  1649. fclose (fp);
  1650. goto no_file;
  1651. }
  1652. memcpy (&newp->entry.prefix, &prefix, sizeof (prefix));
  1653. newp->entry.bits = bits;
  1654. newp->entry.val = val;
  1655. newp->next = *listp;
  1656. *listp = newp;
  1657. ++*lenp;
  1658. *nullbitsp |= bits == 0;
  1659. }
  1660. }
  1661. break;
  1662. case 6:
  1663. if (strcmp (cmd, "reload") == 0)
  1664. {
  1665. gaiconf_reload_flag = strcmp (val1, "yes") == 0;
  1666. if (gaiconf_reload_flag)