getaddrinfo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*
  2. * Copyright 1996 by Craig Metz
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /* $USAGI: getaddrinfo.c,v 1.16 2001/10/04 09:52:03 sekiya Exp $ */
  8. /* The Inner Net License, Version 2.00
  9. The author(s) grant permission for redistribution and use in source and
  10. binary forms, with or without modification, of the software and documentation
  11. provided that the following conditions are met:
  12. 0. If you receive a version of the software that is specifically labelled
  13. as not being for redistribution (check the version message and/or README),
  14. you are not permitted to redistribute that version of the software in any
  15. way or form.
  16. 1. All terms of the all other applicable copyrights and licenses must be
  17. followed.
  18. 2. Redistributions of source code must retain the authors' copyright
  19. notice(s), this list of conditions, and the following disclaimer.
  20. 3. Redistributions in binary form must reproduce the authors' copyright
  21. notice(s), this list of conditions, and the following disclaimer in the
  22. documentation and/or other materials provided with the distribution.
  23. 4. All advertising materials mentioning features or use of this software
  24. must display the following acknowledgement with the name(s) of the
  25. authors as specified in the copyright notice(s) substituted where
  26. indicated:
  27. This product includes software developed by <name(s)>, The Inner
  28. Net, and other contributors.
  29. 5. Neither the name(s) of the author(s) nor the names of its contributors
  30. may be used to endorse or promote products derived from this software
  31. without specific prior written permission.
  32. THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
  33. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  34. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35. DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
  36. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  39. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. If these license terms cause you a real problem, contact the author. */
  43. #define _GNU_SOURCE
  44. #define __FORCE_GLIBC
  45. #include <features.h>
  46. #include <assert.h>
  47. #include <errno.h>
  48. #include <netdb.h>
  49. #include <resolv.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54. #include <arpa/inet.h>
  55. #include <sys/socket.h>
  56. #include <netinet/in.h>
  57. #include <sys/types.h>
  58. #include <sys/un.h>
  59. #include <sys/utsname.h>
  60. #include <net/if.h>
  61. libc_hidden_proto(memcpy)
  62. libc_hidden_proto(memset)
  63. /* libc_hidden_proto(strcmp) */
  64. /* libc_hidden_proto(stpcpy) */
  65. libc_hidden_proto(strchr)
  66. libc_hidden_proto(strcpy)
  67. libc_hidden_proto(strlen)
  68. libc_hidden_proto(socket)
  69. libc_hidden_proto(close)
  70. libc_hidden_proto(getservbyname_r)
  71. libc_hidden_proto(gethostbyname_r)
  72. libc_hidden_proto(gethostbyname2_r)
  73. libc_hidden_proto(gethostbyaddr_r)
  74. libc_hidden_proto(inet_pton)
  75. libc_hidden_proto(inet_ntop)
  76. libc_hidden_proto(strtoul)
  77. libc_hidden_proto(if_nametoindex)
  78. libc_hidden_proto(__h_errno_location)
  79. /* libc_hidden_proto(uname) */
  80. #ifdef __UCLIBC_HAS_IPV6__
  81. libc_hidden_proto(in6addr_loopback)
  82. #endif
  83. /* The following declarations and definitions have been removed from
  84. * the public header since we don't want people to use them. */
  85. #define AI_V4MAPPED 0x0008 /* IPv4-mapped addresses are acceptable. */
  86. #define AI_ALL 0x0010 /* Return both IPv4 and IPv6 addresses. */
  87. #define AI_ADDRCONFIG 0x0020 /* Use configuration of this host to choose
  88. returned address type. */
  89. #define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG)
  90. #define GAIH_OKIFUNSPEC 0x0100
  91. #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
  92. #ifndef UNIX_PATH_MAX
  93. #define UNIX_PATH_MAX 108
  94. #endif
  95. struct gaih_service
  96. {
  97. const char *name;
  98. int num;
  99. };
  100. struct gaih_servtuple
  101. {
  102. struct gaih_servtuple *next;
  103. int socktype;
  104. int protocol;
  105. int port;
  106. };
  107. static const struct gaih_servtuple nullserv;
  108. struct gaih_addrtuple
  109. {
  110. struct gaih_addrtuple *next;
  111. int family;
  112. char addr[16];
  113. uint32_t scopeid;
  114. };
  115. struct gaih_typeproto
  116. {
  117. int socktype;
  118. int protocol;
  119. char name[4];
  120. int protoflag;
  121. };
  122. /* Values for `protoflag'. */
  123. #define GAI_PROTO_NOSERVICE 1
  124. #define GAI_PROTO_PROTOANY 2
  125. static const struct gaih_typeproto gaih_inet_typeproto[] =
  126. {
  127. { 0, 0, "", 0 },
  128. { SOCK_STREAM, IPPROTO_TCP, "tcp", 0 },
  129. { SOCK_DGRAM, IPPROTO_UDP, "udp", 0 },
  130. { SOCK_RAW, 0, "raw", GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE },
  131. { 0, 0, "", 0 }
  132. };
  133. struct gaih
  134. {
  135. int family;
  136. int (*gaih)(const char *name, const struct gaih_service *service,
  137. const struct addrinfo *req, struct addrinfo **pai);
  138. };
  139. #if PF_UNSPEC == 0
  140. static const struct addrinfo default_hints;
  141. #else
  142. static const struct addrinfo default_hints =
  143. { 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL };
  144. #endif
  145. static int addrconfig (sa_family_t af)
  146. {
  147. int s;
  148. int ret;
  149. int saved_errno = errno;
  150. s = socket(af, SOCK_DGRAM, 0);
  151. if (s < 0)
  152. ret = (errno == EMFILE) ? 1 : 0;
  153. else
  154. {
  155. close(s);
  156. ret = 1;
  157. }
  158. __set_errno (saved_errno);
  159. return ret;
  160. }
  161. #if 0
  162. /* Using Unix sockets this way is a security risk. */
  163. static int
  164. gaih_local (const char *name, const struct gaih_service *service,
  165. const struct addrinfo *req, struct addrinfo **pai)
  166. {
  167. struct utsname utsname;
  168. if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
  169. return GAIH_OKIFUNSPEC | -EAI_NONAME;
  170. if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
  171. if (uname (&utsname) < 0)
  172. return -EAI_SYSTEM;
  173. if (name != NULL)
  174. {
  175. if (strcmp(name, "localhost") &&
  176. strcmp(name, "local") &&
  177. strcmp(name, "unix") &&
  178. strcmp(name, utsname.nodename))
  179. return GAIH_OKIFUNSPEC | -EAI_NONAME;
  180. }
  181. if (req->ai_protocol || req->ai_socktype)
  182. {
  183. const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
  184. while (tp->name[0]
  185. && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
  186. || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  187. || (req->ai_protocol != 0
  188. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  189. && req->ai_protocol != tp->protocol)))
  190. ++tp;
  191. if (! tp->name[0])
  192. {
  193. if (req->ai_socktype)
  194. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  195. else
  196. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  197. }
  198. }
  199. *pai = malloc (sizeof (struct addrinfo) + sizeof (struct sockaddr_un)
  200. + ((req->ai_flags & AI_CANONNAME)
  201. ? (strlen(utsname.nodename) + 1): 0));
  202. if (*pai == NULL)
  203. return -EAI_MEMORY;
  204. (*pai)->ai_next = NULL;
  205. (*pai)->ai_flags = req->ai_flags;
  206. (*pai)->ai_family = AF_LOCAL;
  207. (*pai)->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
  208. (*pai)->ai_protocol = req->ai_protocol;
  209. (*pai)->ai_addrlen = sizeof (struct sockaddr_un);
  210. (*pai)->ai_addr = (void *) (*pai) + sizeof (struct addrinfo);
  211. #if SALEN
  212. ((struct sockaddr_un *) (*pai)->ai_addr)->sun_len =
  213. sizeof (struct sockaddr_un);
  214. #endif /* SALEN */
  215. ((struct sockaddr_un *)(*pai)->ai_addr)->sun_family = AF_LOCAL;
  216. memset(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
  217. if (service)
  218. {
  219. struct sockaddr_un *sunp = (struct sockaddr_un *) (*pai)->ai_addr;
  220. if (strchr (service->name, '/') != NULL)
  221. {
  222. if (strlen (service->name) >= sizeof (sunp->sun_path))
  223. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  224. strcpy (sunp->sun_path, service->name);
  225. }
  226. else
  227. {
  228. if (strlen (P_tmpdir "/") + 1 + strlen (service->name) >=
  229. sizeof (sunp->sun_path))
  230. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  231. stpcpy (stpcpy (sunp->sun_path, P_tmpdir "/"), service->name);
  232. }
  233. }
  234. else
  235. {
  236. /* This is a dangerous use of the interface since there is a time
  237. window between the test for the file and the actual creation
  238. (done by the caller) in which a file with the same name could
  239. be created. */
  240. char *buf = ((struct sockaddr_un *) (*pai)->ai_addr)->sun_path;
  241. if (__builtin_expect (__path_search (buf, L_tmpnam, NULL, NULL, 0),
  242. 0) != 0
  243. || __builtin_expect (__gen_tempname (buf, __GT_NOCREATE), 0) != 0)
  244. return -EAI_SYSTEM;
  245. }
  246. if (req->ai_flags & AI_CANONNAME)
  247. (*pai)->ai_canonname = strcpy ((char *) *pai + sizeof (struct addrinfo)
  248. + sizeof (struct sockaddr_un),
  249. utsname.nodename);
  250. else
  251. (*pai)->ai_canonname = NULL;
  252. return 0;
  253. }
  254. #endif /* 0 */
  255. static int
  256. gaih_inet_serv (const char *servicename, const struct gaih_typeproto *tp,
  257. const struct addrinfo *req, struct gaih_servtuple *st)
  258. {
  259. struct servent *s;
  260. size_t tmpbuflen = 1024;
  261. struct servent ts;
  262. char *tmpbuf;
  263. int r;
  264. do
  265. {
  266. tmpbuf = alloca (tmpbuflen);
  267. r = getservbyname_r (servicename, tp->name, &ts, tmpbuf, tmpbuflen,
  268. &s);
  269. if (r != 0 || s == NULL)
  270. {
  271. if (r == ERANGE)
  272. tmpbuflen *= 2;
  273. else
  274. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  275. }
  276. }
  277. while (r);
  278. st->next = NULL;
  279. st->socktype = tp->socktype;
  280. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  281. ? req->ai_protocol : tp->protocol);
  282. st->port = s->s_port;
  283. return 0;
  284. }
  285. #define gethosts(_family, _type) \
  286. { \
  287. int i, herrno; \
  288. size_t tmpbuflen; \
  289. struct hostent th; \
  290. char *tmpbuf; \
  291. tmpbuflen = 512; \
  292. no_data = 0; \
  293. do { \
  294. tmpbuflen *= 2; \
  295. tmpbuf = alloca (tmpbuflen); \
  296. rc = gethostbyname2_r (name, _family, &th, tmpbuf, \
  297. tmpbuflen, &h, &herrno); \
  298. } while (rc == ERANGE && herrno == NETDB_INTERNAL); \
  299. if (rc != 0) \
  300. { \
  301. if (herrno == NETDB_INTERNAL) \
  302. { \
  303. __set_h_errno (herrno); \
  304. return -EAI_SYSTEM; \
  305. } \
  306. if (herrno == TRY_AGAIN) \
  307. no_data = EAI_AGAIN; \
  308. else \
  309. no_data = herrno == NO_DATA; \
  310. } \
  311. else if (h != NULL) \
  312. { \
  313. for (i = 0; h->h_addr_list[i]; i++) \
  314. { \
  315. if (*pat == NULL) { \
  316. *pat = alloca (sizeof(struct gaih_addrtuple)); \
  317. (*pat)->scopeid = 0; \
  318. } \
  319. (*pat)->next = NULL; \
  320. (*pat)->family = _family; \
  321. memcpy ((*pat)->addr, h->h_addr_list[i], \
  322. sizeof(_type)); \
  323. pat = &((*pat)->next); \
  324. } \
  325. } \
  326. }
  327. static int
  328. gaih_inet (const char *name, const struct gaih_service *service,
  329. const struct addrinfo *req, struct addrinfo **pai)
  330. {
  331. const struct gaih_typeproto *tp = gaih_inet_typeproto;
  332. struct gaih_servtuple *st = (struct gaih_servtuple *) &nullserv;
  333. struct gaih_addrtuple *at = NULL;
  334. int rc;
  335. int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6) &&
  336. (req->ai_flags & AI_V4MAPPED);
  337. if (req->ai_protocol || req->ai_socktype)
  338. {
  339. ++tp;
  340. while (tp->name[0]
  341. && ((req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  342. || (req->ai_protocol != 0
  343. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  344. && req->ai_protocol != tp->protocol)))
  345. ++tp;
  346. if (! tp->name[0])
  347. {
  348. if (req->ai_socktype)
  349. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  350. else
  351. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  352. }
  353. }
  354. if (service != NULL)
  355. {
  356. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  357. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  358. if (service->num < 0)
  359. {
  360. if (tp->name[0])
  361. {
  362. st = (struct gaih_servtuple *)
  363. alloca (sizeof (struct gaih_servtuple));
  364. if ((rc = gaih_inet_serv (service->name, tp, req, st)))
  365. return rc;
  366. }
  367. else
  368. {
  369. struct gaih_servtuple **pst = &st;
  370. for (tp++; tp->name[0]; tp++)
  371. {
  372. struct gaih_servtuple *newp;
  373. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  374. continue;
  375. if (req->ai_socktype != 0
  376. && req->ai_socktype != tp->socktype)
  377. continue;
  378. if (req->ai_protocol != 0
  379. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  380. && req->ai_protocol != tp->protocol)
  381. continue;
  382. newp = (struct gaih_servtuple *)
  383. alloca (sizeof (struct gaih_servtuple));
  384. if ((rc = gaih_inet_serv (service->name, tp, req, newp)))
  385. {
  386. if (rc & GAIH_OKIFUNSPEC)
  387. continue;
  388. return rc;
  389. }
  390. *pst = newp;
  391. pst = &(newp->next);
  392. }
  393. if (st == (struct gaih_servtuple *) &nullserv)
  394. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  395. }
  396. }
  397. else
  398. {
  399. st = alloca (sizeof (struct gaih_servtuple));
  400. st->next = NULL;
  401. st->socktype = tp->socktype;
  402. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  403. ? req->ai_protocol : tp->protocol);
  404. st->port = htons (service->num);
  405. }
  406. }
  407. else if (req->ai_socktype || req->ai_protocol)
  408. {
  409. st = alloca (sizeof (struct gaih_servtuple));
  410. st->next = NULL;
  411. st->socktype = tp->socktype;
  412. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  413. ? req->ai_protocol : tp->protocol);
  414. st->port = 0;
  415. }
  416. else
  417. {
  418. /*
  419. * Neither socket type nor protocol is set. Return all socket types
  420. * we know about.
  421. */
  422. struct gaih_servtuple **lastp = &st;
  423. for (++tp; tp->name[0]; ++tp)
  424. {
  425. struct gaih_servtuple *newp;
  426. newp = alloca (sizeof (struct gaih_servtuple));
  427. newp->next = NULL;
  428. newp->socktype = tp->socktype;
  429. newp->protocol = tp->protocol;
  430. newp->port = 0;
  431. *lastp = newp;
  432. lastp = &newp->next;
  433. }
  434. }
  435. if (name != NULL)
  436. {
  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. {
  443. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET || v4mapped)
  444. at->family = AF_INET;
  445. else
  446. return -EAI_FAMILY;
  447. }
  448. #if __UCLIBC_HAS_IPV6__
  449. if (at->family == AF_UNSPEC)
  450. {
  451. char *namebuf = strdupa (name);
  452. char *scope_delim;
  453. scope_delim = strchr (namebuf, SCOPE_DELIMITER);
  454. if (scope_delim != NULL)
  455. *scope_delim = '\0';
  456. if (inet_pton (AF_INET6, namebuf, at->addr) > 0)
  457. {
  458. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  459. at->family = AF_INET6;
  460. else
  461. return -EAI_FAMILY;
  462. if (scope_delim != NULL)
  463. {
  464. int try_numericscope = 0;
  465. if (IN6_IS_ADDR_LINKLOCAL (at->addr)
  466. || IN6_IS_ADDR_MC_LINKLOCAL (at->addr))
  467. {
  468. at->scopeid = if_nametoindex (scope_delim + 1);
  469. if (at->scopeid == 0)
  470. try_numericscope = 1;
  471. }
  472. else
  473. try_numericscope = 1;
  474. if (try_numericscope != 0)
  475. {
  476. char *end;
  477. assert (sizeof (uint32_t) <= sizeof (unsigned long));
  478. at->scopeid = (uint32_t) strtoul (scope_delim + 1, &end,
  479. 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) == 0)
  488. {
  489. struct hostent *h;
  490. struct gaih_addrtuple **pat = &at;
  491. int no_data = 0;
  492. int no_inet6_data;
  493. /*
  494. * If we are looking for both IPv4 and IPv6 address we don't want
  495. * the lookup functions to automatically promote IPv4 addresses to
  496. * IPv6 addresses.
  497. */
  498. #if __UCLIBC_HAS_IPV6__
  499. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  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. gethosts (AF_INET, struct in_addr);
  507. if (no_data != 0 && no_inet6_data != 0)
  508. {
  509. /* If both requests timed out report this. */
  510. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  511. return -EAI_AGAIN;
  512. /*
  513. * We made requests but they turned out no data.
  514. * The name is known, though.
  515. */
  516. return (GAIH_OKIFUNSPEC | -EAI_AGAIN);
  517. }
  518. }
  519. if (at->family == AF_UNSPEC)
  520. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  521. }
  522. else
  523. {
  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. {
  529. at->next = alloca (sizeof (struct gaih_addrtuple));
  530. memset (at->next, '\0', sizeof (struct gaih_addrtuple));
  531. }
  532. #if __UCLIBC_HAS_IPV6__
  533. if (req->ai_family == 0 || req->ai_family == AF_INET6)
  534. {
  535. at->family = AF_INET6;
  536. if ((req->ai_flags & AI_PASSIVE) == 0)
  537. memcpy (at->addr, &in6addr_loopback, sizeof (struct in6_addr));
  538. atr = at->next;
  539. }
  540. #endif
  541. if (req->ai_family == 0 || req->ai_family == AF_INET)
  542. {
  543. atr->family = AF_INET;
  544. if ((req->ai_flags & AI_PASSIVE) == 0)
  545. *(uint32_t *) atr->addr = htonl (INADDR_LOOPBACK);
  546. }
  547. }
  548. if (pai == NULL)
  549. return 0;
  550. {
  551. const char *c = NULL;
  552. struct gaih_servtuple *st2;
  553. struct gaih_addrtuple *at2 = at;
  554. size_t socklen, namelen;
  555. sa_family_t family;
  556. /*
  557. * buffer is the size of an unformatted IPv6 address in
  558. * printable format.
  559. */
  560. char buffer[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
  561. while (at2 != NULL)
  562. {
  563. if (req->ai_flags & AI_CANONNAME)
  564. {
  565. struct hostent *h = NULL;
  566. int herrno;
  567. struct hostent th;
  568. size_t tmpbuflen = 512;
  569. char *tmpbuf;
  570. do
  571. {
  572. tmpbuflen *= 2;
  573. tmpbuf = alloca (tmpbuflen);
  574. if (tmpbuf == NULL)
  575. return -EAI_MEMORY;
  576. rc = gethostbyaddr_r (at2->addr,
  577. ((at2->family == AF_INET6)
  578. ? sizeof(struct in6_addr)
  579. : sizeof(struct in_addr)),
  580. at2->family, &th, tmpbuf, tmpbuflen,
  581. &h, &herrno);
  582. }
  583. while (rc == errno && herrno == NETDB_INTERNAL);
  584. if (rc != 0 && herrno == NETDB_INTERNAL)
  585. {
  586. __set_h_errno (herrno);
  587. return -EAI_SYSTEM;
  588. }
  589. if (h == NULL)
  590. c = inet_ntop (at2->family, at2->addr, buffer, sizeof(buffer));
  591. else
  592. c = h->h_name;
  593. if (c == NULL)
  594. return GAIH_OKIFUNSPEC | -EAI_NONAME;
  595. namelen = strlen (c) + 1;
  596. }
  597. else
  598. namelen = 0;
  599. #if __UCLIBC_HAS_IPV6__
  600. if (at2->family == AF_INET6 || v4mapped)
  601. {
  602. family = AF_INET6;
  603. socklen = sizeof (struct sockaddr_in6);
  604. }
  605. else
  606. #endif
  607. {
  608. family = AF_INET;
  609. socklen = sizeof (struct sockaddr_in);
  610. }
  611. for (st2 = st; st2 != NULL; st2 = st2->next)
  612. {
  613. *pai = malloc (sizeof (struct addrinfo) + socklen + namelen);
  614. if (*pai == NULL)
  615. return -EAI_MEMORY;
  616. (*pai)->ai_flags = req->ai_flags;
  617. (*pai)->ai_family = family;
  618. (*pai)->ai_socktype = st2->socktype;
  619. (*pai)->ai_protocol = st2->protocol;
  620. (*pai)->ai_addrlen = socklen;
  621. (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
  622. #if SALEN
  623. (*pai)->ai_addr->sa_len = socklen;
  624. #endif /* SALEN */
  625. (*pai)->ai_addr->sa_family = family;
  626. #if __UCLIBC_HAS_IPV6__
  627. if (family == AF_INET6)
  628. {
  629. struct sockaddr_in6 *sin6p =
  630. (struct sockaddr_in6 *) (*pai)->ai_addr;
  631. sin6p->sin6_flowinfo = 0;
  632. if (at2->family == AF_INET6)
  633. {
  634. memcpy (&sin6p->sin6_addr,
  635. at2->addr, sizeof (struct in6_addr));
  636. }
  637. else
  638. {
  639. sin6p->sin6_addr.s6_addr32[0] = 0;
  640. sin6p->sin6_addr.s6_addr32[1] = 0;
  641. sin6p->sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  642. memcpy(&sin6p->sin6_addr.s6_addr32[3],
  643. at2->addr, sizeof (sin6p->sin6_addr.s6_addr32[3]));
  644. }
  645. sin6p->sin6_port = st2->port;
  646. sin6p->sin6_scope_id = at2->scopeid;
  647. }
  648. else
  649. #endif
  650. {
  651. struct sockaddr_in *sinp =
  652. (struct sockaddr_in *) (*pai)->ai_addr;
  653. memcpy (&sinp->sin_addr,
  654. at2->addr, sizeof (struct in_addr));
  655. sinp->sin_port = st2->port;
  656. memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
  657. }
  658. if (c)
  659. {
  660. (*pai)->ai_canonname = ((void *) (*pai) +
  661. sizeof (struct addrinfo) + socklen);
  662. strcpy ((*pai)->ai_canonname, c);
  663. }
  664. else
  665. (*pai)->ai_canonname = NULL;
  666. (*pai)->ai_next = NULL;
  667. pai = &((*pai)->ai_next);
  668. }
  669. at2 = at2->next;
  670. }
  671. }
  672. return 0;
  673. }
  674. static struct gaih gaih[] =
  675. {
  676. #if __UCLIBC_HAS_IPV6__
  677. { PF_INET6, gaih_inet },
  678. #endif
  679. { PF_INET, gaih_inet },
  680. #if 0
  681. { PF_LOCAL, gaih_local },
  682. #endif
  683. { PF_UNSPEC, NULL }
  684. };
  685. libc_hidden_proto(freeaddrinfo)
  686. void
  687. freeaddrinfo (struct addrinfo *ai)
  688. {
  689. struct addrinfo *p;
  690. while (ai != NULL)
  691. {
  692. p = ai;
  693. ai = ai->ai_next;
  694. free (p);
  695. }
  696. }
  697. libc_hidden_def(freeaddrinfo)
  698. libc_hidden_proto(getaddrinfo)
  699. int
  700. getaddrinfo (const char *name, const char *service,
  701. const struct addrinfo *hints, struct addrinfo **pai)
  702. {
  703. int i = 0, j = 0, last_i = 0;
  704. struct addrinfo *p = NULL, **end;
  705. struct gaih *g = gaih, *pg = NULL;
  706. struct gaih_service gaih_service, *pservice;
  707. if (name != NULL && name[0] == '*' && name[1] == 0)
  708. name = NULL;
  709. if (service != NULL && service[0] == '*' && service[1] == 0)
  710. service = NULL;
  711. if (name == NULL && service == NULL)
  712. return EAI_NONAME;
  713. if (hints == NULL)
  714. hints = &default_hints;
  715. if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|
  716. AI_ADDRCONFIG|AI_V4MAPPED|AI_ALL))
  717. return EAI_BADFLAGS;
  718. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  719. return EAI_BADFLAGS;
  720. if (service && service[0])
  721. {
  722. char *c;
  723. gaih_service.name = service;
  724. gaih_service.num = strtoul (gaih_service.name, &c, 10);
  725. if (*c)
  726. gaih_service.num = -1;
  727. else
  728. /*
  729. * Can't specify a numerical socket unless a protocol
  730. * family was given.
  731. */
  732. if (hints->ai_socktype == 0 && hints->ai_protocol == 0)
  733. return EAI_SERVICE;
  734. pservice = &gaih_service;
  735. }
  736. else
  737. pservice = NULL;
  738. if (pai)
  739. end = &p;
  740. else
  741. end = NULL;
  742. while (g->gaih)
  743. {
  744. if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC)
  745. {
  746. if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family))
  747. continue;
  748. j++;
  749. if (pg == NULL || pg->gaih != g->gaih)
  750. {
  751. pg = g;
  752. i = g->gaih (name, pservice, hints, end);
  753. if (i != 0)
  754. {
  755. last_i = i;
  756. if (hints->ai_family == AF_UNSPEC && (i & GAIH_OKIFUNSPEC))
  757. continue;
  758. if (p)
  759. freeaddrinfo (p);
  760. return -(i & GAIH_EAI);
  761. }
  762. if (end)
  763. while(*end) end = &((*end)->ai_next);
  764. }
  765. }
  766. ++g;
  767. }
  768. if (j == 0)
  769. return EAI_FAMILY;
  770. if (p)
  771. {
  772. *pai = p;
  773. return 0;
  774. }
  775. if (pai == NULL && last_i == 0)
  776. return 0;
  777. if (p)
  778. freeaddrinfo (p);
  779. return last_i ? -(last_i & GAIH_EAI) : EAI_NONAME;
  780. }
  781. libc_hidden_def(getaddrinfo)