getaddrinfo.c 22 KB

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