getaddrinfo.c 24 KB

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