getaddrinfo.c 22 KB

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