getaddrinfo.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * Copyright 1996 by Craig Metz
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. * Portions from the GNU C library,
  5. * Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. /* $USAGI: getaddrinfo.c,v 1.16 2001/10/04 09:52:03 sekiya Exp $ */
  10. /* The Inner Net License, Version 2.00
  11. The author(s) grant permission for redistribution and use in source and
  12. binary forms, with or without modification, of the software and documentation
  13. provided that the following conditions are met:
  14. 0. If you receive a version of the software that is specifically labelled
  15. as not being for redistribution (check the version message and/or README),
  16. you are not permitted to redistribute that version of the software in any
  17. way or form.
  18. 1. All terms of the all other applicable copyrights and licenses must be
  19. followed.
  20. 2. Redistributions of source code must retain the authors' copyright
  21. notice(s), this list of conditions, and the following disclaimer.
  22. 3. Redistributions in binary form must reproduce the authors' copyright
  23. notice(s), this list of conditions, and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. 4. All advertising materials mentioning features or use of this software
  26. must display the following acknowledgement with the name(s) of the
  27. authors as specified in the copyright notice(s) substituted where
  28. indicated:
  29. This product includes software developed by <name(s)>, The Inner
  30. Net, and other contributors.
  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. #include <assert.h>
  46. #include <errno.h>
  47. #include <netdb.h>
  48. #ifdef __UCLIBC_HAS_TLS__
  49. #include <tls.h>
  50. #endif
  51. #include <resolv.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <stdbool.h>
  57. #include <arpa/inet.h>
  58. #include <sys/socket.h>
  59. #include <netinet/in.h>
  60. #include <sys/types.h>
  61. #include <sys/un.h>
  62. #include <sys/utsname.h>
  63. #include <net/if.h>
  64. #include <ifaddrs.h>
  65. #include "internal/parse_config.h"
  66. #define GAIH_OKIFUNSPEC 0x0100
  67. #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
  68. #ifndef UNIX_PATH_MAX
  69. #define UNIX_PATH_MAX 108
  70. #endif
  71. /* Useful for having small structure members/global variables */
  72. typedef int8_t socktype_t;
  73. typedef int8_t family_t;
  74. typedef int8_t protocol_t;
  75. struct BUG_too_small {
  76. char BUG_socktype_t_too_small[(0
  77. | SOCK_STREAM
  78. | SOCK_DGRAM
  79. | SOCK_RAW
  80. ) <= 127 ? 1 : -1];
  81. char BUG_family_t_too_small[(0
  82. | AF_UNSPEC
  83. | AF_INET
  84. | AF_INET6
  85. ) <= 127 ? 1 : -1];
  86. char BUG_protocol_t_too_small[(0
  87. | IPPROTO_TCP
  88. | IPPROTO_UDP
  89. ) <= 127 ? 1 : -1];
  90. };
  91. struct gaih_service {
  92. const char *name;
  93. int num;
  94. };
  95. struct gaih_servtuple {
  96. struct gaih_servtuple *next;
  97. int socktype;
  98. int protocol;
  99. int port;
  100. };
  101. struct gaih_addrtuple {
  102. struct gaih_addrtuple *next;
  103. int family;
  104. char addr[16];
  105. uint32_t scopeid;
  106. };
  107. struct gaih_typeproto {
  108. socktype_t socktype;
  109. protocol_t protocol;
  110. int8_t protoflag;
  111. char name[4];
  112. };
  113. /* Values for `protoflag'. */
  114. #define GAI_PROTO_NOSERVICE 1
  115. #define GAI_PROTO_PROTOANY 2
  116. static const struct gaih_typeproto gaih_inet_typeproto[] = {
  117. { 0 , 0 , 0, "" },
  118. { SOCK_STREAM, IPPROTO_TCP, 0, "tcp" },
  119. { SOCK_DGRAM , IPPROTO_UDP, 0, "udp" },
  120. { SOCK_RAW , 0 , GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, "raw" },
  121. { 0 , 0 , 0, "" },
  122. };
  123. struct gaih {
  124. int family;
  125. int (*gaih)(const char *name, const struct gaih_service *service,
  126. const struct addrinfo *req, struct addrinfo **pai);
  127. };
  128. #define SEEN_IPV4 1
  129. #define SEEN_IPV6 2
  130. static unsigned __check_pf(void)
  131. {
  132. unsigned seen = 0;
  133. #if defined __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  134. struct ifaddrs *ifa;
  135. struct ifaddrs *runp;
  136. /* Get the interface list via getifaddrs. */
  137. if (getifaddrs(&ifa) != 0) {
  138. /* We cannot determine what interfaces are available.
  139. * Be optimistic. */
  140. #if defined __UCLIBC_HAS_IPV4__
  141. seen |= SEEN_IPV4;
  142. #endif
  143. #if defined __UCLIBC_HAS_IPV6__
  144. seen |= SEEN_IPV6;
  145. #endif
  146. return seen;
  147. }
  148. for (runp = ifa; runp != NULL; runp = runp->ifa_next) {
  149. if (runp->ifa_addr == NULL)
  150. continue;
  151. #if defined __UCLIBC_HAS_IPV4__
  152. if (runp->ifa_addr->sa_family == PF_INET)
  153. seen |= SEEN_IPV4;
  154. #endif
  155. #if defined __UCLIBC_HAS_IPV6__
  156. if (runp->ifa_addr->sa_family == PF_INET6)
  157. seen |= SEEN_IPV6;
  158. #endif
  159. }
  160. freeifaddrs(ifa);
  161. #else
  162. /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */
  163. #if defined __UCLIBC_HAS_IPV4__
  164. seen |= SEEN_IPV4;
  165. #endif
  166. #if defined __UCLIBC_HAS_IPV6__
  167. seen |= SEEN_IPV6;
  168. #endif
  169. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  170. return seen;
  171. }
  172. static int addrconfig(sa_family_t af)
  173. {
  174. int s;
  175. int ret;
  176. int saved_errno = errno;
  177. unsigned seen;
  178. seen = __check_pf();
  179. #if defined __UCLIBC_HAS_IPV4__
  180. if (af == AF_INET)
  181. ret = seen & SEEN_IPV4;
  182. else
  183. #endif
  184. #if defined __UCLIBC_HAS_IPV6__
  185. if (af == AF_INET6)
  186. ret = seen & SEEN_IPV6;
  187. else
  188. #endif
  189. {
  190. s = socket(af, SOCK_DGRAM, 0);
  191. ret = 1; /* Assume PF_UNIX. */
  192. if (s < 0) {
  193. if (errno != EMFILE)
  194. ret = 0;
  195. } else
  196. close(s);
  197. }
  198. __set_errno(saved_errno);
  199. return ret;
  200. }
  201. #if 0
  202. /* Using Unix sockets this way is a security risk. */
  203. static int
  204. gaih_local(const char *name, const struct gaih_service *service,
  205. const struct addrinfo *req, struct addrinfo **pai)
  206. {
  207. struct utsname utsname;
  208. struct addrinfo *ai = *pai;
  209. if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
  210. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  211. if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
  212. if (uname(&utsname) < 0)
  213. return -EAI_SYSTEM;
  214. if (name != NULL) {
  215. if (strcmp(name, "localhost") &&
  216. strcmp(name, "local") &&
  217. strcmp(name, "unix") &&
  218. strcmp(name, utsname.nodename))
  219. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  220. }
  221. if (req->ai_protocol || req->ai_socktype) {
  222. const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
  223. while (tp->name[0]
  224. && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
  225. || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  226. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
  227. ) {
  228. ++tp;
  229. }
  230. if (! tp->name[0]) {
  231. if (req->ai_socktype)
  232. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  233. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  234. }
  235. }
  236. *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
  237. + ((req->ai_flags & AI_CANONNAME)
  238. ? (strlen(utsname.nodename) + 1) : 0));
  239. if (ai == NULL)
  240. return -EAI_MEMORY;
  241. ai->ai_next = NULL;
  242. ai->ai_flags = req->ai_flags;
  243. ai->ai_family = AF_LOCAL;
  244. ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
  245. ai->ai_protocol = req->ai_protocol;
  246. ai->ai_addrlen = sizeof(struct sockaddr_un);
  247. ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
  248. #if 0 /* SALEN */
  249. ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
  250. #endif /* SALEN */
  251. ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
  252. memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
  253. if (service) {
  254. struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
  255. if (strchr(service->name, '/') != NULL) {
  256. if (strlen(service->name) >= sizeof(sunp->sun_path))
  257. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  258. strcpy(sunp->sun_path, service->name);
  259. } else {
  260. if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
  261. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  262. stpcpy(stpcpy(sunp->sun_path, P_tmpdir "/"), service->name);
  263. }
  264. } else {
  265. /* This is a dangerous use of the interface since there is a time
  266. window between the test for the file and the actual creation
  267. (done by the caller) in which a file with the same name could
  268. be created. */
  269. char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
  270. if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
  271. || __gen_tempname(buf, __GT_NOCREATE, 0, 0) != 0
  272. ) {
  273. return -EAI_SYSTEM;
  274. }
  275. }
  276. ai->ai_canonname = NULL;
  277. if (req->ai_flags & AI_CANONNAME)
  278. ai->ai_canonname = strcpy((char *)(ai + 1) + sizeof(struct sockaddr_un),
  279. utsname.nodename);
  280. return 0;
  281. }
  282. #endif /* 0 */
  283. static int
  284. gaih_inet_serv(const char *servicename, const struct gaih_typeproto *tp,
  285. const struct addrinfo *req, struct gaih_servtuple *st)
  286. {
  287. struct servent *s;
  288. size_t tmpbuflen = 1024;
  289. struct servent ts;
  290. char *tmpbuf;
  291. int r;
  292. while (1) {
  293. tmpbuf = alloca(tmpbuflen);
  294. r = getservbyname_r(servicename, tp->name, &ts, tmpbuf, tmpbuflen, &s);
  295. if (r == 0 && s != NULL)
  296. break;
  297. if (r != ERANGE)
  298. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  299. tmpbuflen *= 2;
  300. }
  301. st->next = NULL;
  302. st->socktype = tp->socktype;
  303. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY) ? req->ai_protocol : tp->protocol);
  304. st->port = s->s_port;
  305. return 0;
  306. }
  307. #if defined __UCLIBC_HAS_IPV6__
  308. static uint8_t __gai_precedence = 0; /* =1 - IPv6, IPv4
  309. =2 - IPv4, IPv6 */
  310. #endif
  311. /* NB: also uses h,pat,rc,no_data variables */
  312. #define gethosts(_family, _type) \
  313. { \
  314. int i, herrno; \
  315. size_t tmpbuflen; \
  316. struct hostent th; \
  317. char *tmpbuf; \
  318. \
  319. tmpbuflen = 512; \
  320. no_data = 0; \
  321. do { \
  322. tmpbuflen *= 2; \
  323. tmpbuf = alloca(tmpbuflen); \
  324. rc = gethostbyname2_r(name, _family, &th, tmpbuf, \
  325. tmpbuflen, &h, &herrno); \
  326. } while (rc == ERANGE && herrno == NETDB_INTERNAL); \
  327. if (rc != 0) { \
  328. if (herrno == NETDB_INTERNAL) { \
  329. __set_h_errno(herrno); \
  330. return -EAI_SYSTEM; \
  331. } \
  332. if (herrno == TRY_AGAIN) \
  333. no_data = EAI_AGAIN; \
  334. else \
  335. no_data = (herrno == NO_DATA); \
  336. } else if (h != NULL) { \
  337. for (i = 0; h->h_addr_list[i]; i++) { \
  338. if (*pat == NULL) { \
  339. *pat = alloca(sizeof(struct gaih_addrtuple)); \
  340. (*pat)->scopeid = 0; \
  341. } \
  342. (*pat)->next = NULL; \
  343. (*pat)->family = _family; \
  344. memcpy((*pat)->addr, h->h_addr_list[i], sizeof(_type)); \
  345. pat = &((*pat)->next); \
  346. } \
  347. if (_family == AF_INET6 && i > 0) { \
  348. got_ipv6 = true; \
  349. } \
  350. } \
  351. }
  352. static int
  353. gaih_inet(const char *name, const struct gaih_service *service,
  354. const struct addrinfo *req, struct addrinfo **pai)
  355. {
  356. struct gaih_servtuple nullserv;
  357. const struct gaih_typeproto *tp;
  358. struct gaih_servtuple *st;
  359. struct gaih_addrtuple *at;
  360. int rc;
  361. bool got_ipv6 = false;
  362. int v4mapped = req->ai_family == PF_INET6 && (req->ai_flags & AI_V4MAPPED);
  363. unsigned seen = 0;
  364. if (req->ai_flags & AI_ADDRCONFIG) {
  365. /* "seen" is only used when AI_ADDRCONFIG is specified.
  366. Avoid unnecessary call to __check_pf() otherwise
  367. since it can be costly especially when RSBAC-Net is enabled. */
  368. seen = __check_pf();
  369. }
  370. memset(&nullserv, 0, sizeof(nullserv));
  371. tp = gaih_inet_typeproto;
  372. if (req->ai_protocol || req->ai_socktype) {
  373. ++tp;
  374. while (tp->name[0]) {
  375. if ((req->ai_socktype == 0 || req->ai_socktype == tp->socktype)
  376. && (req->ai_protocol == 0 || req->ai_protocol == tp->protocol || (tp->protoflag & GAI_PROTO_PROTOANY))
  377. ) {
  378. goto found;
  379. }
  380. ++tp;
  381. }
  382. if (req->ai_socktype)
  383. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  384. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  385. found: ;
  386. }
  387. st = &nullserv;
  388. if (service != NULL) {
  389. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  390. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  391. if (service->num < 0) {
  392. if (tp->name[0]) {
  393. st = alloca(sizeof(struct gaih_servtuple));
  394. rc = gaih_inet_serv(service->name, tp, req, st);
  395. if (rc)
  396. return rc;
  397. } else {
  398. struct gaih_servtuple **pst = &st;
  399. for (tp++; tp->name[0]; tp++) {
  400. struct gaih_servtuple *newp;
  401. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  402. continue;
  403. if (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  404. continue;
  405. if (req->ai_protocol != 0
  406. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  407. && req->ai_protocol != tp->protocol)
  408. continue;
  409. newp = alloca(sizeof(struct gaih_servtuple));
  410. rc = gaih_inet_serv(service->name, tp, req, newp);
  411. if (rc) {
  412. if (rc & GAIH_OKIFUNSPEC)
  413. continue;
  414. return rc;
  415. }
  416. *pst = newp;
  417. pst = &(newp->next);
  418. }
  419. if (st == &nullserv)
  420. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  421. }
  422. } else {
  423. st = alloca(sizeof(struct gaih_servtuple));
  424. st->next = NULL;
  425. st->socktype = tp->socktype;
  426. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  427. ? req->ai_protocol : tp->protocol);
  428. st->port = htons(service->num);
  429. }
  430. } else if (req->ai_socktype || req->ai_protocol) {
  431. st = alloca(sizeof(struct gaih_servtuple));
  432. st->next = NULL;
  433. st->socktype = tp->socktype;
  434. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  435. ? req->ai_protocol : tp->protocol);
  436. st->port = 0;
  437. } else {
  438. /*
  439. * Neither socket type nor protocol is set. Return all socket types
  440. * we know about.
  441. */
  442. struct gaih_servtuple **lastp = &st;
  443. for (++tp; tp->name[0]; ++tp) {
  444. struct gaih_servtuple *newp;
  445. newp = alloca(sizeof(struct gaih_servtuple));
  446. newp->next = NULL;
  447. newp->socktype = tp->socktype;
  448. newp->protocol = tp->protocol;
  449. newp->port = 0;
  450. *lastp = newp;
  451. lastp = &newp->next;
  452. }
  453. }
  454. at = NULL;
  455. if (name != NULL) {
  456. at = alloca(sizeof(struct gaih_addrtuple));
  457. at->family = AF_UNSPEC;
  458. at->scopeid = 0;
  459. at->next = NULL;
  460. if (inet_pton(AF_INET, name, at->addr) > 0) {
  461. if (req->ai_family != AF_UNSPEC && req->ai_family != AF_INET && !v4mapped)
  462. return -EAI_FAMILY;
  463. at->family = AF_INET;
  464. }
  465. #if defined __UCLIBC_HAS_IPV6__
  466. if (at->family == AF_UNSPEC) {
  467. char *namebuf = strdupa(name);
  468. char *scope_delim;
  469. scope_delim = strchr(namebuf, SCOPE_DELIMITER);
  470. if (scope_delim != NULL)
  471. *scope_delim = '\0';
  472. if (inet_pton(AF_INET6, namebuf, at->addr) > 0) {
  473. if (req->ai_family != AF_UNSPEC && req->ai_family != AF_INET6)
  474. return -EAI_FAMILY;
  475. at->family = AF_INET6;
  476. if (scope_delim != NULL) {
  477. int try_numericscope = 0;
  478. uint32_t *a32 = (uint32_t*)at->addr;
  479. if (IN6_IS_ADDR_LINKLOCAL(a32) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
  480. at->scopeid = if_nametoindex(scope_delim + 1);
  481. if (at->scopeid == 0)
  482. try_numericscope = 1;
  483. } else
  484. try_numericscope = 1;
  485. if (try_numericscope != 0) {
  486. char *end;
  487. assert(sizeof(uint32_t) <= sizeof(unsigned long));
  488. at->scopeid = (uint32_t)strtoul(scope_delim + 1, &end, 10);
  489. if (*end != '\0')
  490. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  491. }
  492. }
  493. }
  494. }
  495. #endif
  496. if (at->family == AF_UNSPEC && !(req->ai_flags & AI_NUMERICHOST)) {
  497. struct hostent *h;
  498. struct gaih_addrtuple **pat = &at;
  499. int no_data, no_inet6_data;
  500. #if defined __UCLIBC_HAS_IPV6__
  501. bool first_try = true;
  502. #endif
  503. /*
  504. * If we are looking for both IPv4 and IPv6 address we don't want
  505. * the lookup functions to automatically promote IPv4 addresses to
  506. * IPv6 addresses.
  507. */
  508. no_inet6_data = no_data = 0;
  509. #if defined __UCLIBC_HAS_IPV6__
  510. if (__gai_precedence == 2)
  511. goto try_v4;
  512. try_v6:
  513. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  514. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6))
  515. gethosts(AF_INET6, struct in6_addr);
  516. no_inet6_data = no_data;
  517. if (!first_try)
  518. goto tried_all;
  519. first_try = false;
  520. try_v4:
  521. #endif
  522. if (req->ai_family == AF_INET
  523. || (!v4mapped && req->ai_family == AF_UNSPEC)
  524. || (v4mapped && (!got_ipv6 || (req->ai_flags & AI_ALL)))
  525. ) {
  526. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4))
  527. gethosts(AF_INET, struct in_addr);
  528. }
  529. #if defined __UCLIBC_HAS_IPV6__
  530. if (first_try) {
  531. first_try = false;
  532. goto try_v6;
  533. }
  534. tried_all:
  535. #endif
  536. if (no_data != 0 && no_inet6_data != 0) {
  537. /* If both requests timed out report this. */
  538. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  539. return -EAI_AGAIN;
  540. /*
  541. * We made requests but they turned out no data.
  542. * The name is known, though.
  543. */
  544. return (GAIH_OKIFUNSPEC | -EAI_AGAIN);
  545. }
  546. }
  547. if (at->family == AF_UNSPEC)
  548. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  549. } else {
  550. struct gaih_addrtuple *atr;
  551. atr = at = alloca(sizeof(struct gaih_addrtuple));
  552. memset(at, '\0', sizeof(struct gaih_addrtuple));
  553. if (req->ai_family == 0) {
  554. at->next = alloca(sizeof(struct gaih_addrtuple));
  555. memset(at->next, '\0', sizeof(struct gaih_addrtuple));
  556. }
  557. #if defined __UCLIBC_HAS_IPV6__
  558. if (req->ai_family == 0 || req->ai_family == AF_INET6) {
  559. at->family = AF_INET6;
  560. if ((req->ai_flags & AI_PASSIVE) == 0)
  561. memcpy(at->addr, &in6addr_loopback, sizeof(struct in6_addr));
  562. atr = at->next;
  563. }
  564. #endif
  565. if (req->ai_family == 0 || req->ai_family == AF_INET) {
  566. atr->family = AF_INET;
  567. if ((req->ai_flags & AI_PASSIVE) == 0) {
  568. uint32_t *a = (uint32_t*)atr->addr;
  569. *a = htonl(INADDR_LOOPBACK);
  570. }
  571. }
  572. }
  573. if (pai == NULL)
  574. return 0;
  575. {
  576. const char *c = NULL;
  577. struct gaih_servtuple *st2;
  578. struct gaih_addrtuple *at2 = at;
  579. size_t socklen, namelen;
  580. sa_family_t family;
  581. /*
  582. * buffer is the size of an unformatted IPv6 address in
  583. * printable format.
  584. */
  585. char buffer[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  586. while (at2 != NULL) {
  587. c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
  588. if (c) {
  589. namelen = strlen(c) + 1;
  590. } else if (req->ai_flags & AI_CANONNAME) {
  591. struct hostent *h = NULL;
  592. int herrno;
  593. struct hostent th;
  594. size_t tmpbuflen = 512;
  595. char *tmpbuf;
  596. /* Hint says numeric, but address is not */
  597. if (req->ai_flags & AI_NUMERICHOST)
  598. return -EAI_NONAME;
  599. do {
  600. tmpbuflen *= 2;
  601. tmpbuf = alloca(tmpbuflen);
  602. rc = gethostbyaddr_r(at2->addr,
  603. #ifdef __UCLIBC_HAS_IPV6__
  604. ((at2->family == AF_INET6)
  605. ? sizeof(struct in6_addr)
  606. : sizeof(struct in_addr)),
  607. #else
  608. sizeof(struct in_addr),
  609. #endif
  610. at2->family,
  611. &th, tmpbuf, tmpbuflen,
  612. &h, &herrno);
  613. } while (rc == ERANGE && herrno == NETDB_INTERNAL);
  614. if (rc != 0 && herrno == NETDB_INTERNAL) {
  615. __set_h_errno(herrno);
  616. return -EAI_SYSTEM;
  617. }
  618. if (h != NULL)
  619. c = h->h_name;
  620. if (c == NULL)
  621. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  622. namelen = strlen(c) + 1;
  623. } else
  624. namelen = 0;
  625. #if defined __UCLIBC_HAS_IPV6__
  626. if (at2->family == AF_INET6 || v4mapped) {
  627. family = AF_INET6;
  628. socklen = sizeof(struct sockaddr_in6);
  629. /* If we looked up IPv4 mapped address discard them here if
  630. the caller isn't interested in all address and we have
  631. found at least one IPv6 address. */
  632. if (got_ipv6
  633. && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED
  634. && IN6_IS_ADDR_V4MAPPED (at2->addr))
  635. goto ignore;
  636. }
  637. #endif
  638. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  639. else
  640. #endif
  641. #if defined __UCLIBC_HAS_IPV4__
  642. {
  643. family = AF_INET;
  644. socklen = sizeof(struct sockaddr_in);
  645. }
  646. #endif
  647. for (st2 = st; st2 != NULL; st2 = st2->next) {
  648. if (req->ai_flags & AI_ADDRCONFIG) {
  649. if (family == AF_INET && !(seen & SEEN_IPV4))
  650. break;
  651. #if defined __UCLIBC_HAS_IPV6__
  652. else if (family == AF_INET6 && !(seen & SEEN_IPV6))
  653. break;
  654. #endif
  655. }
  656. *pai = malloc(sizeof(struct addrinfo) + socklen + namelen);
  657. if (*pai == NULL)
  658. return -EAI_MEMORY;
  659. (*pai)->ai_flags = req->ai_flags;
  660. (*pai)->ai_family = family;
  661. (*pai)->ai_socktype = st2->socktype;
  662. (*pai)->ai_protocol = st2->protocol;
  663. (*pai)->ai_addrlen = socklen;
  664. (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
  665. #if 0 /* SALEN */
  666. (*pai)->ai_addr->sa_len = socklen;
  667. #endif
  668. (*pai)->ai_addr->sa_family = family;
  669. #if defined __UCLIBC_HAS_IPV6__
  670. if (family == AF_INET6) {
  671. struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) (*pai)->ai_addr;
  672. sin6p->sin6_flowinfo = 0;
  673. if (at2->family == AF_INET6) {
  674. memcpy(&sin6p->sin6_addr,
  675. at2->addr, sizeof(struct in6_addr));
  676. } else {
  677. sin6p->sin6_addr.s6_addr32[0] = 0;
  678. sin6p->sin6_addr.s6_addr32[1] = 0;
  679. sin6p->sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  680. memcpy(&sin6p->sin6_addr.s6_addr32[3],
  681. at2->addr, sizeof(sin6p->sin6_addr.s6_addr32[3]));
  682. }
  683. sin6p->sin6_port = st2->port;
  684. sin6p->sin6_scope_id = at2->scopeid;
  685. }
  686. #endif
  687. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  688. else
  689. #endif
  690. #if defined __UCLIBC_HAS_IPV4__
  691. {
  692. struct sockaddr_in *sinp = (struct sockaddr_in *) (*pai)->ai_addr;
  693. memcpy(&sinp->sin_addr, at2->addr, sizeof(struct in_addr));
  694. sinp->sin_port = st2->port;
  695. memset(sinp->sin_zero, '\0', sizeof(sinp->sin_zero));
  696. }
  697. #endif
  698. if (c) {
  699. (*pai)->ai_canonname = ((void *) (*pai) +
  700. sizeof(struct addrinfo) + socklen);
  701. strcpy((*pai)->ai_canonname, c);
  702. } else {
  703. (*pai)->ai_canonname = NULL;
  704. }
  705. (*pai)->ai_next = NULL;
  706. pai = &((*pai)->ai_next);
  707. }
  708. ignore:
  709. at2 = at2->next;
  710. }
  711. }
  712. return 0;
  713. }
  714. static const struct gaih gaih[] = {
  715. #if defined __UCLIBC_HAS_IPV6__
  716. { PF_INET6, gaih_inet },
  717. #endif
  718. { PF_INET, gaih_inet },
  719. #if 0
  720. { PF_LOCAL, gaih_local },
  721. #endif
  722. { PF_UNSPEC, NULL }
  723. };
  724. #if defined __UCLIBC_HAS_IPV6__
  725. /*
  726. * A call to getaddrinfo might return multiple answers. To provide
  727. * possibility to change the sorting we must use /etc/gai.conf file,
  728. * like glibc.
  729. *
  730. * gai.conf format:
  731. *
  732. * label <netmask> <precedence>
  733. * The value is added to the label table used in
  734. * the RFC 3484 sorting. If any label definition
  735. * is present in the configuration file is present,
  736. * the default table is not used. All the label
  737. * definitions of the default table which are to
  738. * be maintained have to be duplicated.
  739. * precedence <netmask> <precedence>
  740. * This keyword is similar to label, but instead
  741. * the value is added to the precedence table as
  742. * specified in RFC 3484. Once again, the presence
  743. * of a single precedence line in the configuration
  744. * file causes the default table to not be used.
  745. *
  746. * The simplified uclibc's implementation allows to change the IPv4/IPv6
  747. * sorting order for a whole address space only, i.e
  748. * "precedence ::ffff:0:0/96 100" is only supported.
  749. */
  750. static void __gai_conf_parse(void)
  751. {
  752. /* NO reread of /etc/gai.conf on change. */
  753. if (__gai_precedence != 0)
  754. return;
  755. __gai_precedence = 1; /* default IPv6 */
  756. parser_t *parser;
  757. char **tok = NULL;
  758. parser = config_open("/etc/gai.conf");
  759. if (!parser)
  760. return;
  761. while (config_read(parser, &tok, 3, 3, "# \t", PARSE_NORMAL)) {
  762. if (strcmp(tok[0], "precedence") == 0) {
  763. char *pfx;
  764. struct in6_addr mask;
  765. pfx = strchr(tok[1], '/');
  766. if (!pfx)
  767. continue;
  768. *(pfx++) = 0;
  769. if (inet_pton(AF_INET6, tok[1], &mask) <= 0)
  770. continue;
  771. if (IN6_IS_ADDR_V4MAPPED(&mask)
  772. && mask.s6_addr32[3] == 0
  773. && atoi(pfx) == 96 && atoi(tok[2]) == 100)
  774. __gai_precedence = 2; /* IPv4 first */
  775. }
  776. }
  777. config_close(parser);
  778. }
  779. #else
  780. # define __gai_conf_parse(x)
  781. #endif /* __UCLIBC_HAS_IPV6__ */
  782. void
  783. freeaddrinfo(struct addrinfo *ai)
  784. {
  785. struct addrinfo *p;
  786. while (ai != NULL) {
  787. p = ai;
  788. ai = ai->ai_next;
  789. free(p);
  790. }
  791. }
  792. libc_hidden_def(freeaddrinfo)
  793. int
  794. getaddrinfo(const char *name, const char *service,
  795. const struct addrinfo *hints, struct addrinfo **pai)
  796. {
  797. int i, j, last_i;
  798. struct addrinfo *p, **end;
  799. const struct gaih *g, *pg;
  800. struct gaih_service gaih_service, *pservice;
  801. struct addrinfo default_hints;
  802. if (name != NULL && name[0] == '*' && name[1] == 0)
  803. name = NULL;
  804. if (service != NULL && service[0] == '*' && service[1] == 0)
  805. service = NULL;
  806. if (name == NULL && service == NULL)
  807. return EAI_NONAME;
  808. if (hints == NULL) {
  809. memset(&default_hints, 0, sizeof(default_hints));
  810. if (AF_UNSPEC != 0)
  811. default_hints.ai_family = AF_UNSPEC;
  812. hints = &default_hints;
  813. }
  814. if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|
  815. AI_ADDRCONFIG|AI_V4MAPPED|AI_NUMERICSERV|AI_ALL))
  816. return EAI_BADFLAGS;
  817. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  818. return EAI_BADFLAGS;
  819. if (service && service[0]) {
  820. char *c;
  821. gaih_service.name = service;
  822. gaih_service.num = strtoul(gaih_service.name, &c, 10);
  823. if (*c != '\0') {
  824. if (hints->ai_flags & AI_NUMERICSERV)
  825. return EAI_NONAME;
  826. gaih_service.num = -1;
  827. }
  828. pservice = &gaih_service;
  829. } else
  830. pservice = NULL;
  831. __gai_conf_parse();
  832. g = gaih;
  833. pg = NULL;
  834. p = NULL;
  835. end = NULL;
  836. if (pai)
  837. end = &p;
  838. i = 0;
  839. last_i = 0;
  840. j = 0;
  841. while (g->gaih) {
  842. if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC) {
  843. if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family)) {
  844. ++g;
  845. continue;
  846. }
  847. j++;
  848. if (pg == NULL || pg->gaih != g->gaih) {
  849. pg = g;
  850. i = g->gaih(name, pservice, hints, end);
  851. if (i != 0) {
  852. last_i = i;
  853. if (hints->ai_family == AF_UNSPEC && (i & GAIH_OKIFUNSPEC))
  854. continue;
  855. /*if (p) - freeaddrinfo works ok on NULL too */
  856. freeaddrinfo(p);
  857. return -(i & GAIH_EAI);
  858. }
  859. if (end)
  860. while (*end)
  861. end = &((*end)->ai_next);
  862. }
  863. }
  864. ++g;
  865. }
  866. if (j == 0)
  867. return EAI_FAMILY;
  868. if (p) {
  869. *pai = p;
  870. return 0;
  871. }
  872. if (pai == NULL && last_i == 0)
  873. return 0;
  874. /* if (p) - never happens, see above */
  875. /* freeaddrinfo(p); */
  876. return last_i ? -(last_i & GAIH_EAI) : EAI_NONAME;
  877. }
  878. libc_hidden_def(getaddrinfo)