getaddrinfo.c 23 KB

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