getaddrinfo.c 21 KB

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