getaddrinfo.c 21 KB

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