getaddrinfo.c 23 KB

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