getaddrinfo.c 24 KB

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