getaddrinfo.c 21 KB

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