getaddrinfo.c 22 KB

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