getaddrinfo.c 22 KB

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