getaddrinfo.c 21 KB

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