getaddrinfo.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 <arpa/inet.h>
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59. #include <sys/types.h>
  60. #include <sys/un.h>
  61. #include <sys/utsname.h>
  62. #include <net/if.h>
  63. #include <ifaddrs.h>
  64. #define GAIH_OKIFUNSPEC 0x0100
  65. #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
  66. #ifndef UNIX_PATH_MAX
  67. #define UNIX_PATH_MAX 108
  68. #endif
  69. /* Useful for having small structure members/global variables */
  70. typedef int8_t socktype_t;
  71. typedef int8_t family_t;
  72. typedef int8_t protocol_t;
  73. struct BUG_too_small {
  74. char BUG_socktype_t_too_small[(0
  75. | SOCK_STREAM
  76. | SOCK_DGRAM
  77. | SOCK_RAW
  78. ) <= 127 ? 1 : -1];
  79. char BUG_family_t_too_small[(0
  80. | AF_UNSPEC
  81. | AF_INET
  82. | AF_INET6
  83. ) <= 127 ? 1 : -1];
  84. char BUG_protocol_t_too_small[(0
  85. | IPPROTO_TCP
  86. | IPPROTO_UDP
  87. ) <= 127 ? 1 : -1];
  88. };
  89. struct gaih_service {
  90. const char *name;
  91. int num;
  92. };
  93. struct gaih_servtuple {
  94. struct gaih_servtuple *next;
  95. int socktype;
  96. int protocol;
  97. int port;
  98. };
  99. struct gaih_addrtuple {
  100. struct gaih_addrtuple *next;
  101. int family;
  102. char addr[16];
  103. uint32_t scopeid;
  104. };
  105. struct gaih_typeproto {
  106. socktype_t socktype;
  107. protocol_t protocol;
  108. int8_t protoflag;
  109. char name[4];
  110. };
  111. /* Values for `protoflag'. */
  112. #define GAI_PROTO_NOSERVICE 1
  113. #define GAI_PROTO_PROTOANY 2
  114. static const struct gaih_typeproto gaih_inet_typeproto[] = {
  115. { 0 , 0 , 0, "" },
  116. { SOCK_STREAM, IPPROTO_TCP, 0, "tcp" },
  117. { SOCK_DGRAM , IPPROTO_UDP, 0, "udp" },
  118. { SOCK_RAW , 0 , GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, "raw" },
  119. { 0 , 0 , 0, "" },
  120. };
  121. struct gaih {
  122. int family;
  123. int (*gaih)(const char *name, const struct gaih_service *service,
  124. const struct addrinfo *req, struct addrinfo **pai);
  125. };
  126. #define SEEN_IPV4 1
  127. #define SEEN_IPV6 2
  128. static unsigned __check_pf(void)
  129. {
  130. unsigned seen = 0;
  131. #if defined __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  132. struct ifaddrs *ifa;
  133. struct ifaddrs *runp;
  134. /* Get the interface list via getifaddrs. */
  135. if (getifaddrs(&ifa) != 0) {
  136. /* We cannot determine what interfaces are available.
  137. * Be optimistic. */
  138. #if defined __UCLIBC_HAS_IPV4__
  139. seen |= SEEN_IPV4;
  140. #endif
  141. #if defined __UCLIBC_HAS_IPV6__
  142. seen |= SEEN_IPV6;
  143. #endif
  144. return seen;
  145. }
  146. for (runp = ifa; runp != NULL; runp = runp->ifa_next) {
  147. if (runp->ifa_addr == NULL)
  148. continue;
  149. #if defined __UCLIBC_HAS_IPV4__
  150. if (runp->ifa_addr->sa_family == PF_INET)
  151. seen |= SEEN_IPV4;
  152. #endif
  153. #if defined __UCLIBC_HAS_IPV6__
  154. if (runp->ifa_addr->sa_family == PF_INET6)
  155. seen |= SEEN_IPV6;
  156. #endif
  157. }
  158. freeifaddrs(ifa);
  159. #else
  160. /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */
  161. #if defined __UCLIBC_HAS_IPV4__
  162. seen |= SEEN_IPV4;
  163. #endif
  164. #if defined __UCLIBC_HAS_IPV6__
  165. seen |= SEEN_IPV6;
  166. #endif
  167. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  168. return seen;
  169. }
  170. static int addrconfig(sa_family_t af)
  171. {
  172. int s;
  173. int ret;
  174. int saved_errno = errno;
  175. unsigned seen;
  176. seen = __check_pf();
  177. #if defined __UCLIBC_HAS_IPV4__
  178. if (af == AF_INET)
  179. ret = seen & SEEN_IPV4;
  180. else
  181. #endif
  182. #if defined __UCLIBC_HAS_IPV6__
  183. if (af == AF_INET6)
  184. ret = seen & SEEN_IPV6;
  185. else
  186. #endif
  187. {
  188. s = socket(af, SOCK_DGRAM, 0);
  189. ret = 1; /* Assume PF_UNIX. */
  190. if (s < 0) {
  191. if (errno != EMFILE)
  192. ret = 0;
  193. } else
  194. close(s);
  195. }
  196. __set_errno(saved_errno);
  197. return ret;
  198. }
  199. #if 0
  200. /* Using Unix sockets this way is a security risk. */
  201. static int
  202. gaih_local(const char *name, const struct gaih_service *service,
  203. const struct addrinfo *req, struct addrinfo **pai)
  204. {
  205. struct utsname utsname;
  206. struct addrinfo *ai = *pai;
  207. if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
  208. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  209. if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
  210. if (uname(&utsname) < 0)
  211. return -EAI_SYSTEM;
  212. if (name != NULL) {
  213. if (strcmp(name, "localhost") &&
  214. strcmp(name, "local") &&
  215. strcmp(name, "unix") &&
  216. strcmp(name, utsname.nodename))
  217. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  218. }
  219. if (req->ai_protocol || req->ai_socktype) {
  220. const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
  221. while (tp->name[0]
  222. && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
  223. || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  224. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
  225. ) {
  226. ++tp;
  227. }
  228. if (! tp->name[0]) {
  229. if (req->ai_socktype)
  230. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  231. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  232. }
  233. }
  234. *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
  235. + ((req->ai_flags & AI_CANONNAME)
  236. ? (strlen(utsname.nodename) + 1) : 0));
  237. if (ai == NULL)
  238. return -EAI_MEMORY;
  239. ai->ai_next = NULL;
  240. ai->ai_flags = req->ai_flags;
  241. ai->ai_family = AF_LOCAL;
  242. ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
  243. ai->ai_protocol = req->ai_protocol;
  244. ai->ai_addrlen = sizeof(struct sockaddr_un);
  245. ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
  246. #if SALEN
  247. ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
  248. #endif /* SALEN */
  249. ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
  250. memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
  251. if (service) {
  252. struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
  253. if (strchr(service->name, '/') != NULL) {
  254. if (strlen(service->name) >= sizeof(sunp->sun_path))
  255. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  256. strcpy(sunp->sun_path, service->name);
  257. } else {
  258. if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
  259. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  260. stpcpy(stpcpy(sunp->sun_path, P_tmpdir "/"), service->name);
  261. }
  262. } else {
  263. /* This is a dangerous use of the interface since there is a time
  264. window between the test for the file and the actual creation
  265. (done by the caller) in which a file with the same name could
  266. be created. */
  267. char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
  268. if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
  269. || __gen_tempname(buf, __GT_NOCREATE, 0) != 0
  270. ) {
  271. return -EAI_SYSTEM;
  272. }
  273. }
  274. ai->ai_canonname = NULL;
  275. if (req->ai_flags & AI_CANONNAME)
  276. ai->ai_canonname = strcpy((char *)(ai + 1) + sizeof(struct sockaddr_un),
  277. utsname.nodename);
  278. return 0;
  279. }
  280. #endif /* 0 */
  281. static int
  282. gaih_inet_serv(const char *servicename, const struct gaih_typeproto *tp,
  283. const struct addrinfo *req, struct gaih_servtuple *st)
  284. {
  285. struct servent *s;
  286. size_t tmpbuflen = 1024;
  287. struct servent ts;
  288. char *tmpbuf;
  289. int r;
  290. while (1) {
  291. tmpbuf = alloca(tmpbuflen);
  292. r = getservbyname_r(servicename, tp->name, &ts, tmpbuf, tmpbuflen, &s);
  293. if (r == 0 && s != NULL)
  294. break;
  295. if (r != ERANGE)
  296. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  297. tmpbuflen *= 2;
  298. }
  299. st->next = NULL;
  300. st->socktype = tp->socktype;
  301. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY) ? req->ai_protocol : tp->protocol);
  302. st->port = s->s_port;
  303. return 0;
  304. }
  305. /* NB: also uses h,pat,rc,no_data variables */
  306. #define gethosts(_family, _type) \
  307. { \
  308. int i, herrno; \
  309. size_t tmpbuflen; \
  310. struct hostent th; \
  311. char *tmpbuf; \
  312. \
  313. tmpbuflen = 512; \
  314. no_data = 0; \
  315. do { \
  316. tmpbuflen *= 2; \
  317. tmpbuf = alloca(tmpbuflen); \
  318. rc = gethostbyname2_r(name, _family, &th, tmpbuf, \
  319. tmpbuflen, &h, &herrno); \
  320. } while (rc == ERANGE && herrno == NETDB_INTERNAL); \
  321. if (rc != 0) { \
  322. if (herrno == NETDB_INTERNAL) { \
  323. __set_h_errno(herrno); \
  324. return -EAI_SYSTEM; \
  325. } \
  326. if (herrno == TRY_AGAIN) \
  327. no_data = EAI_AGAIN; \
  328. else \
  329. no_data = (herrno == NO_DATA); \
  330. } else if (h != NULL) { \
  331. for (i = 0; h->h_addr_list[i]; i++) { \
  332. if (*pat == NULL) { \
  333. *pat = alloca(sizeof(struct gaih_addrtuple)); \
  334. (*pat)->scopeid = 0; \
  335. } \
  336. (*pat)->next = NULL; \
  337. (*pat)->family = _family; \
  338. memcpy((*pat)->addr, h->h_addr_list[i], sizeof(_type)); \
  339. pat = &((*pat)->next); \
  340. } \
  341. } \
  342. }
  343. static int
  344. gaih_inet(const char *name, const struct gaih_service *service,
  345. const struct addrinfo *req, struct addrinfo **pai)
  346. {
  347. struct gaih_servtuple nullserv;
  348. const struct gaih_typeproto *tp;
  349. struct gaih_servtuple *st;
  350. struct gaih_addrtuple *at;
  351. int rc;
  352. int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6)
  353. && (req->ai_flags & AI_V4MAPPED);
  354. unsigned seen = 0;
  355. if (req->ai_flags & AI_ADDRCONFIG) {
  356. /* "seen" is only used when AI_ADDRCONFIG is specified.
  357. Avoid unnecessary call to __check_pf() otherwise
  358. since it can be costly especially when RSBAC-Net is enabled. */
  359. seen = __check_pf();
  360. }
  361. memset(&nullserv, 0, sizeof(nullserv));
  362. tp = gaih_inet_typeproto;
  363. if (req->ai_protocol || req->ai_socktype) {
  364. ++tp;
  365. while (tp->name[0]) {
  366. if ((req->ai_socktype == 0 || req->ai_socktype == tp->socktype)
  367. && (req->ai_protocol == 0 || req->ai_protocol == tp->protocol || (tp->protoflag & GAI_PROTO_PROTOANY))
  368. ) {
  369. goto found;
  370. }
  371. ++tp;
  372. }
  373. if (req->ai_socktype)
  374. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  375. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  376. found: ;
  377. }
  378. st = &nullserv;
  379. if (service != NULL) {
  380. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  381. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  382. if (service->num < 0) {
  383. if (tp->name[0]) {
  384. st = alloca(sizeof(struct gaih_servtuple));
  385. rc = gaih_inet_serv(service->name, tp, req, st);
  386. if (rc)
  387. return rc;
  388. } else {
  389. struct gaih_servtuple **pst = &st;
  390. for (tp++; tp->name[0]; tp++) {
  391. struct gaih_servtuple *newp;
  392. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  393. continue;
  394. if (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  395. continue;
  396. if (req->ai_protocol != 0
  397. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  398. && req->ai_protocol != tp->protocol)
  399. continue;
  400. newp = alloca(sizeof(struct gaih_servtuple));
  401. rc = gaih_inet_serv(service->name, tp, req, newp);
  402. if (rc) {
  403. if (rc & GAIH_OKIFUNSPEC)
  404. continue;
  405. return rc;
  406. }
  407. *pst = newp;
  408. pst = &(newp->next);
  409. }
  410. if (st == &nullserv)
  411. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  412. }
  413. } else {
  414. st = alloca(sizeof(struct gaih_servtuple));
  415. st->next = NULL;
  416. st->socktype = tp->socktype;
  417. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  418. ? req->ai_protocol : tp->protocol);
  419. st->port = htons(service->num);
  420. }
  421. } else if (req->ai_socktype || req->ai_protocol) {
  422. st = alloca(sizeof(struct gaih_servtuple));
  423. st->next = NULL;
  424. st->socktype = tp->socktype;
  425. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  426. ? req->ai_protocol : tp->protocol);
  427. st->port = 0;
  428. } else {
  429. /*
  430. * Neither socket type nor protocol is set. Return all socket types
  431. * we know about.
  432. */
  433. struct gaih_servtuple **lastp = &st;
  434. for (++tp; tp->name[0]; ++tp) {
  435. struct gaih_servtuple *newp;
  436. newp = alloca(sizeof(struct gaih_servtuple));
  437. newp->next = NULL;
  438. newp->socktype = tp->socktype;
  439. newp->protocol = tp->protocol;
  440. newp->port = 0;
  441. *lastp = newp;
  442. lastp = &newp->next;
  443. }
  444. }
  445. at = NULL;
  446. if (name != NULL) {
  447. at = alloca(sizeof(struct gaih_addrtuple));
  448. at->family = AF_UNSPEC;
  449. at->scopeid = 0;
  450. at->next = NULL;
  451. if (inet_pton(AF_INET, name, at->addr) > 0) {
  452. if (req->ai_family != AF_UNSPEC && req->ai_family != AF_INET && !v4mapped)
  453. return -EAI_FAMILY;
  454. at->family = AF_INET;
  455. }
  456. #if defined __UCLIBC_HAS_IPV6__
  457. if (at->family == AF_UNSPEC) {
  458. char *namebuf = strdupa(name);
  459. char *scope_delim;
  460. scope_delim = strchr(namebuf, SCOPE_DELIMITER);
  461. if (scope_delim != NULL)
  462. *scope_delim = '\0';
  463. if (inet_pton(AF_INET6, namebuf, at->addr) > 0) {
  464. if (req->ai_family != AF_UNSPEC && req->ai_family != AF_INET6)
  465. return -EAI_FAMILY;
  466. at->family = AF_INET6;
  467. if (scope_delim != NULL) {
  468. int try_numericscope = 0;
  469. uint32_t *a32 = (uint32_t*)at->addr;
  470. if (IN6_IS_ADDR_LINKLOCAL(a32) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
  471. at->scopeid = if_nametoindex(scope_delim + 1);
  472. if (at->scopeid == 0)
  473. try_numericscope = 1;
  474. } else
  475. try_numericscope = 1;
  476. if (try_numericscope != 0) {
  477. char *end;
  478. assert(sizeof(uint32_t) <= sizeof(unsigned long));
  479. at->scopeid = (uint32_t)strtoul(scope_delim + 1, &end, 10);
  480. if (*end != '\0')
  481. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  482. }
  483. }
  484. }
  485. }
  486. #endif
  487. if (at->family == AF_UNSPEC && !(req->ai_flags & AI_NUMERICHOST)) {
  488. struct hostent *h;
  489. struct gaih_addrtuple **pat = &at;
  490. int no_data = 0;
  491. int no_inet6_data;
  492. /*
  493. * If we are looking for both IPv4 and IPv6 address we don't want
  494. * the lookup functions to automatically promote IPv4 addresses to
  495. * IPv6 addresses.
  496. */
  497. #if defined __UCLIBC_HAS_IPV6__
  498. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  499. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6))
  500. gethosts(AF_INET6, struct in6_addr);
  501. #endif
  502. no_inet6_data = no_data;
  503. if (req->ai_family == AF_INET
  504. || (!v4mapped && req->ai_family == AF_UNSPEC)
  505. || (v4mapped && (no_inet6_data != 0 || (req->ai_flags & AI_ALL)))
  506. ) {
  507. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4))
  508. gethosts(AF_INET, struct in_addr);
  509. }
  510. if (no_data != 0 && no_inet6_data != 0) {
  511. /* If both requests timed out report this. */
  512. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  513. return -EAI_AGAIN;
  514. /*
  515. * We made requests but they turned out no data.
  516. * The name is known, though.
  517. */
  518. return (GAIH_OKIFUNSPEC | -EAI_AGAIN);
  519. }
  520. }
  521. if (at->family == AF_UNSPEC)
  522. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  523. } else {
  524. struct gaih_addrtuple *atr;
  525. atr = at = alloca(sizeof(struct gaih_addrtuple));
  526. memset(at, '\0', sizeof(struct gaih_addrtuple));
  527. if (req->ai_family == 0) {
  528. at->next = alloca(sizeof(struct gaih_addrtuple));
  529. memset(at->next, '\0', sizeof(struct gaih_addrtuple));
  530. }
  531. #if defined __UCLIBC_HAS_IPV6__
  532. if (req->ai_family == 0 || req->ai_family == AF_INET6) {
  533. at->family = AF_INET6;
  534. if ((req->ai_flags & AI_PASSIVE) == 0)
  535. memcpy(at->addr, &in6addr_loopback, sizeof(struct in6_addr));
  536. atr = at->next;
  537. }
  538. #endif
  539. if (req->ai_family == 0 || req->ai_family == AF_INET) {
  540. atr->family = AF_INET;
  541. if ((req->ai_flags & AI_PASSIVE) == 0) {
  542. uint32_t *a = (uint32_t*)atr->addr;
  543. *a = htonl(INADDR_LOOPBACK);
  544. }
  545. }
  546. }
  547. if (pai == NULL)
  548. return 0;
  549. {
  550. const char *c = NULL;
  551. struct gaih_servtuple *st2;
  552. struct gaih_addrtuple *at2 = at;
  553. size_t socklen, namelen;
  554. sa_family_t family;
  555. /*
  556. * buffer is the size of an unformatted IPv6 address in
  557. * printable format.
  558. */
  559. char buffer[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  560. while (at2 != NULL) {
  561. c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
  562. if (c) {
  563. namelen = strlen(c) + 1;
  564. } else if (req->ai_flags & AI_CANONNAME) {
  565. struct hostent *h = NULL;
  566. int herrno;
  567. struct hostent th;
  568. size_t tmpbuflen = 512;
  569. char *tmpbuf;
  570. /* Hint says numeric, but address is not */
  571. if (req->ai_flags & AI_NUMERICHOST)
  572. return -EAI_NONAME;
  573. do {
  574. tmpbuflen *= 2;
  575. tmpbuf = alloca(tmpbuflen);
  576. rc = gethostbyaddr_r(at2->addr,
  577. #ifdef __UCLIBC_HAS_IPV6__
  578. ((at2->family == AF_INET6)
  579. ? sizeof(struct in6_addr)
  580. : sizeof(struct in_addr)),
  581. #else
  582. sizeof(struct in_addr),
  583. #endif
  584. at2->family,
  585. &th, tmpbuf, tmpbuflen,
  586. &h, &herrno);
  587. } while (rc == ERANGE && herrno == NETDB_INTERNAL);
  588. if (rc != 0 && herrno == NETDB_INTERNAL) {
  589. __set_h_errno(herrno);
  590. return -EAI_SYSTEM;
  591. }
  592. if (h != NULL)
  593. c = h->h_name;
  594. if (c == NULL)
  595. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  596. namelen = strlen(c) + 1;
  597. } else
  598. namelen = 0;
  599. #if defined __UCLIBC_HAS_IPV6__
  600. if (at2->family == AF_INET6 || v4mapped) {
  601. family = AF_INET6;
  602. socklen = sizeof(struct sockaddr_in6);
  603. }
  604. #endif
  605. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  606. else
  607. #endif
  608. #if defined __UCLIBC_HAS_IPV4__
  609. {
  610. family = AF_INET;
  611. socklen = sizeof(struct sockaddr_in);
  612. }
  613. #endif
  614. for (st2 = st; st2 != NULL; st2 = st2->next) {
  615. if (req->ai_flags & AI_ADDRCONFIG) {
  616. if (family == AF_INET && !(seen & SEEN_IPV4))
  617. break;
  618. #if defined __UCLIBC_HAS_IPV6__
  619. else if (family == AF_INET6 && !(seen & SEEN_IPV6))
  620. break;
  621. #endif
  622. }
  623. *pai = malloc(sizeof(struct addrinfo) + socklen + namelen);
  624. if (*pai == NULL)
  625. return -EAI_MEMORY;
  626. (*pai)->ai_flags = req->ai_flags;
  627. (*pai)->ai_family = family;
  628. (*pai)->ai_socktype = st2->socktype;
  629. (*pai)->ai_protocol = st2->protocol;
  630. (*pai)->ai_addrlen = socklen;
  631. (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
  632. #if defined SALEN
  633. (*pai)->ai_addr->sa_len = socklen;
  634. #endif
  635. (*pai)->ai_addr->sa_family = family;
  636. #if defined __UCLIBC_HAS_IPV6__
  637. if (family == AF_INET6) {
  638. struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) (*pai)->ai_addr;
  639. sin6p->sin6_flowinfo = 0;
  640. if (at2->family == AF_INET6) {
  641. memcpy(&sin6p->sin6_addr,
  642. at2->addr, sizeof(struct in6_addr));
  643. } else {
  644. sin6p->sin6_addr.s6_addr32[0] = 0;
  645. sin6p->sin6_addr.s6_addr32[1] = 0;
  646. sin6p->sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  647. memcpy(&sin6p->sin6_addr.s6_addr32[3],
  648. at2->addr, sizeof(sin6p->sin6_addr.s6_addr32[3]));
  649. }
  650. sin6p->sin6_port = st2->port;
  651. sin6p->sin6_scope_id = at2->scopeid;
  652. }
  653. #endif
  654. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  655. else
  656. #endif
  657. #if defined __UCLIBC_HAS_IPV4__
  658. {
  659. struct sockaddr_in *sinp = (struct sockaddr_in *) (*pai)->ai_addr;
  660. memcpy(&sinp->sin_addr, at2->addr, sizeof(struct in_addr));
  661. sinp->sin_port = st2->port;
  662. memset(sinp->sin_zero, '\0', sizeof(sinp->sin_zero));
  663. }
  664. #endif
  665. if (c) {
  666. (*pai)->ai_canonname = ((void *) (*pai) +
  667. sizeof(struct addrinfo) + socklen);
  668. strcpy((*pai)->ai_canonname, c);
  669. } else {
  670. (*pai)->ai_canonname = NULL;
  671. }
  672. (*pai)->ai_next = NULL;
  673. pai = &((*pai)->ai_next);
  674. }
  675. at2 = at2->next;
  676. }
  677. }
  678. return 0;
  679. }
  680. static const struct gaih gaih[] = {
  681. #if defined __UCLIBC_HAS_IPV6__
  682. { PF_INET6, gaih_inet },
  683. #endif
  684. { PF_INET, gaih_inet },
  685. #if 0
  686. { PF_LOCAL, gaih_local },
  687. #endif
  688. { PF_UNSPEC, NULL }
  689. };
  690. void
  691. freeaddrinfo(struct addrinfo *ai)
  692. {
  693. struct addrinfo *p;
  694. while (ai != NULL) {
  695. p = ai;
  696. ai = ai->ai_next;
  697. free(p);
  698. }
  699. }
  700. libc_hidden_def(freeaddrinfo)
  701. int
  702. getaddrinfo(const char *name, const char *service,
  703. const struct addrinfo *hints, struct addrinfo **pai)
  704. {
  705. int i, j, last_i;
  706. struct addrinfo *p, **end;
  707. const struct gaih *g, *pg;
  708. struct gaih_service gaih_service, *pservice;
  709. struct addrinfo default_hints;
  710. if (name != NULL && name[0] == '*' && name[1] == 0)
  711. name = NULL;
  712. if (service != NULL && service[0] == '*' && service[1] == 0)
  713. service = NULL;
  714. if (name == NULL && service == NULL)
  715. return EAI_NONAME;
  716. if (hints == NULL) {
  717. memset(&default_hints, 0, sizeof(default_hints));
  718. if (AF_UNSPEC != 0)
  719. default_hints.ai_family = AF_UNSPEC;
  720. hints = &default_hints;
  721. }
  722. if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|
  723. AI_ADDRCONFIG|AI_V4MAPPED|AI_NUMERICSERV|AI_ALL))
  724. return EAI_BADFLAGS;
  725. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  726. return EAI_BADFLAGS;
  727. if (service && service[0]) {
  728. char *c;
  729. gaih_service.name = service;
  730. gaih_service.num = strtoul(gaih_service.name, &c, 10);
  731. if (*c != '\0') {
  732. if (hints->ai_flags & AI_NUMERICSERV)
  733. return EAI_NONAME;
  734. gaih_service.num = -1;
  735. }
  736. pservice = &gaih_service;
  737. } else
  738. pservice = NULL;
  739. g = gaih;
  740. pg = NULL;
  741. p = NULL;
  742. end = NULL;
  743. if (pai)
  744. end = &p;
  745. i = 0;
  746. last_i = 0;
  747. j = 0;
  748. while (g->gaih) {
  749. if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC) {
  750. if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family)) {
  751. ++g;
  752. continue;
  753. }
  754. j++;
  755. if (pg == NULL || pg->gaih != g->gaih) {
  756. pg = g;
  757. i = g->gaih(name, pservice, hints, end);
  758. if (i != 0) {
  759. last_i = i;
  760. if (hints->ai_family == AF_UNSPEC && (i & GAIH_OKIFUNSPEC))
  761. continue;
  762. /*if (p) - freeaddrinfo works ok on NULL too */
  763. freeaddrinfo(p);
  764. return -(i & GAIH_EAI);
  765. }
  766. if (end)
  767. while (*end)
  768. end = &((*end)->ai_next);
  769. }
  770. }
  771. ++g;
  772. }
  773. if (j == 0)
  774. return EAI_FAMILY;
  775. if (p) {
  776. *pai = p;
  777. return 0;
  778. }
  779. if (pai == NULL && last_i == 0)
  780. return 0;
  781. /* if (p) - never happens, see above */
  782. /* freeaddrinfo(p); */
  783. return last_i ? -(last_i & GAIH_EAI) : EAI_NONAME;
  784. }
  785. libc_hidden_def(getaddrinfo)