getaddrinfo.c 21 KB

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