getaddrinfo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the project nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
  31. *
  32. * Issues to be discussed:
  33. * - Thread safe-ness must be checked.
  34. * - Return values. There are nonstandard return values defined and used
  35. * in the source code. This is because RFC2133 is silent about which error
  36. * code must be returned for which situation.
  37. * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
  38. */
  39. #if 0
  40. #include <sys/types.h>
  41. #include <sys/param.h>
  42. #include <sys/sysctl.h>
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <arpa/nameser.h>
  47. #include <netdb.h>
  48. #include <resolv.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <stddef.h>
  52. #include <ctype.h>
  53. #include <unistd.h>
  54. #include "addrinfo.h"
  55. #endif
  56. #if defined(__KAME__) && defined(ENABLE_IPV6)
  57. # define FAITH
  58. #endif
  59. #ifdef HAVE_NETDB_H
  60. #define HAVE_GETADDRINFO 1
  61. #define SUCCESS 0
  62. #define GAI_ANY 0
  63. #define YES 1
  64. #define NO 0
  65. #ifdef FAITH
  66. static int translate = NO;
  67. static struct in6_addr faith_prefix = IN6ADDR_GAI_ANY_INIT;
  68. #endif
  69. static const char in_addrany[] = { 0, 0, 0, 0 };
  70. static const char in6_addrany[] = {
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  72. };
  73. static const char in_loopback[] = { 127, 0, 0, 1 };
  74. static const char in6_loopback[] = {
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
  76. };
  77. struct sockinet {
  78. u_char si_len;
  79. u_char si_family;
  80. u_short si_port;
  81. };
  82. static struct gai_afd {
  83. int a_af;
  84. int a_addrlen;
  85. int a_socklen;
  86. int a_off;
  87. const char *a_addrany;
  88. const char *a_loopback;
  89. } gai_afdl [] = {
  90. #ifdef ENABLE_IPV6
  91. #define N_INET6 0
  92. {PF_INET6, sizeof(struct in6_addr),
  93. sizeof(struct sockaddr_in6),
  94. offsetof(struct sockaddr_in6, sin6_addr),
  95. in6_addrany, in6_loopback},
  96. #define N_INET 1
  97. #else
  98. #define N_INET 0
  99. #endif
  100. {PF_INET, sizeof(struct in_addr),
  101. sizeof(struct sockaddr_in),
  102. offsetof(struct sockaddr_in, sin_addr),
  103. in_addrany, in_loopback},
  104. {0, 0, 0, 0, NULL, NULL},
  105. };
  106. #ifdef ENABLE_IPV6
  107. #define PTON_MAX 16
  108. #else
  109. #define PTON_MAX 4
  110. #endif
  111. #ifndef IN_MULTICAST
  112. #define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U)
  113. #endif
  114. #ifndef IN_EXPERIMENTAL
  115. #define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U)
  116. #endif
  117. #ifndef IN_LOOPBACKNET
  118. #define IN_LOOPBACKNET 127
  119. #endif
  120. static int get_name(const char *, struct gai_afd *,
  121. struct addrinfo **, char *, struct addrinfo *,
  122. int);
  123. static int get_addr(const char *, int, struct addrinfo **,
  124. struct addrinfo *, int);
  125. static int str_isnumber(const char *);
  126. static const char * const ai_errlist[] = {
  127. "success.",
  128. "address family for hostname not supported.", /* EAI_ADDRFAMILY */
  129. "temporary failure in name resolution.", /* EAI_AGAIN */
  130. "invalid value for ai_flags.", /* EAI_BADFLAGS */
  131. "non-recoverable failure in name resolution.", /* EAI_FAIL */
  132. "ai_family not supported.", /* EAI_FAMILY */
  133. "memory allocation failure.", /* EAI_MEMORY */
  134. "no address associated with hostname.", /* EAI_NODATA */
  135. "hostname nor servname provided, or not known.",/* EAI_NONAME */
  136. "servname not supported for ai_socktype.", /* EAI_SERVICE */
  137. "ai_socktype not supported.", /* EAI_SOCKTYPE */
  138. "system error returned in errno.", /* EAI_SYSTEM */
  139. "invalid value for hints.", /* EAI_BADHINTS */
  140. "resolved protocol is unknown.", /* EAI_PROTOCOL */
  141. "unknown error.", /* EAI_MAX */
  142. };
  143. #define GET_CANONNAME(ai, str) \
  144. if (pai->ai_flags & AI_CANONNAME) {\
  145. if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
  146. strcpy((ai)->ai_canonname, (str));\
  147. } else {\
  148. error = EAI_MEMORY;\
  149. goto free;\
  150. }\
  151. }
  152. #ifdef HAVE_SOCKADDR_SA_LEN
  153. #define GET_AI(ai, gai_afd, addr, port) {\
  154. char *p;\
  155. if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
  156. ((gai_afd)->a_socklen)))\
  157. == NULL) goto free;\
  158. memcpy(ai, pai, sizeof(struct addrinfo));\
  159. (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
  160. memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
  161. (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\
  162. (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
  163. ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
  164. p = (char *)((ai)->ai_addr);\
  165. memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
  166. }
  167. #else
  168. #define GET_AI(ai, gai_afd, addr, port) {\
  169. char *p;\
  170. if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
  171. ((gai_afd)->a_socklen)))\
  172. == NULL) goto free;\
  173. memcpy(ai, pai, sizeof(struct addrinfo));\
  174. (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
  175. memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
  176. (ai)->ai_addrlen = (gai_afd)->a_socklen;\
  177. (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
  178. ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
  179. p = (char *)((ai)->ai_addr);\
  180. memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
  181. }
  182. #endif
  183. #define ERR(err) { error = (err); goto bad; }
  184. const char *
  185. gai_strerror(int ecode)
  186. {
  187. if (ecode < 0 || ecode > EAI_MAX)
  188. ecode = EAI_MAX;
  189. return ai_errlist[ecode];
  190. }
  191. void
  192. freeaddrinfo(struct addrinfo *ai)
  193. {
  194. struct addrinfo *next;
  195. do {
  196. next = ai->ai_next;
  197. if (ai->ai_canonname)
  198. free(ai->ai_canonname);
  199. /* no need to free(ai->ai_addr) */
  200. free(ai);
  201. } while ((ai = next) != NULL);
  202. }
  203. static int
  204. str_isnumber(const char *p)
  205. {
  206. unsigned char *q = (unsigned char *)p;
  207. while (*q) {
  208. if (! isdigit(*q))
  209. return NO;
  210. q++;
  211. }
  212. return YES;
  213. }
  214. int
  215. getaddrinfo(const char*hostname, const char*servname,
  216. const struct addrinfo *hints, struct addrinfo **res)
  217. {
  218. struct addrinfo sentinel;
  219. struct addrinfo *top = NULL;
  220. struct addrinfo *cur;
  221. int i, error = 0;
  222. char pton[PTON_MAX];
  223. struct addrinfo ai;
  224. struct addrinfo *pai;
  225. u_short port;
  226. #ifdef FAITH
  227. static int firsttime = 1;
  228. if (firsttime) {
  229. /* translator hack */
  230. {
  231. const char *q = getenv("GAI");
  232. if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
  233. translate = YES;
  234. }
  235. firsttime = 0;
  236. }
  237. #endif
  238. /* initialize file static vars */
  239. sentinel.ai_next = NULL;
  240. cur = &sentinel;
  241. pai = &ai;
  242. pai->ai_flags = 0;
  243. pai->ai_family = PF_UNSPEC;
  244. pai->ai_socktype = GAI_ANY;
  245. pai->ai_protocol = GAI_ANY;
  246. pai->ai_addrlen = 0;
  247. pai->ai_canonname = NULL;
  248. pai->ai_addr = NULL;
  249. pai->ai_next = NULL;
  250. port = GAI_ANY;
  251. if (hostname == NULL && servname == NULL)
  252. return EAI_NONAME;
  253. if (hints) {
  254. /* error check for hints */
  255. if (hints->ai_addrlen || hints->ai_canonname ||
  256. hints->ai_addr || hints->ai_next)
  257. ERR(EAI_BADHINTS); /* xxx */
  258. if (hints->ai_flags & ~AI_MASK)
  259. ERR(EAI_BADFLAGS);
  260. switch (hints->ai_family) {
  261. case PF_UNSPEC:
  262. case PF_INET:
  263. #ifdef ENABLE_IPV6
  264. case PF_INET6:
  265. #endif
  266. break;
  267. default:
  268. ERR(EAI_FAMILY);
  269. }
  270. memcpy(pai, hints, sizeof(*pai));
  271. switch (pai->ai_socktype) {
  272. case GAI_ANY:
  273. switch (pai->ai_protocol) {
  274. case GAI_ANY:
  275. break;
  276. case IPPROTO_UDP:
  277. pai->ai_socktype = SOCK_DGRAM;
  278. break;
  279. case IPPROTO_TCP:
  280. pai->ai_socktype = SOCK_STREAM;
  281. break;
  282. default:
  283. pai->ai_socktype = SOCK_RAW;
  284. break;
  285. }
  286. break;
  287. case SOCK_RAW:
  288. break;
  289. case SOCK_DGRAM:
  290. if (pai->ai_protocol != IPPROTO_UDP &&
  291. pai->ai_protocol != GAI_ANY)
  292. ERR(EAI_BADHINTS); /*xxx*/
  293. pai->ai_protocol = IPPROTO_UDP;
  294. break;
  295. case SOCK_STREAM:
  296. if (pai->ai_protocol != IPPROTO_TCP &&
  297. pai->ai_protocol != GAI_ANY)
  298. ERR(EAI_BADHINTS); /*xxx*/
  299. pai->ai_protocol = IPPROTO_TCP;
  300. break;
  301. default:
  302. ERR(EAI_SOCKTYPE);
  303. /* unreachable */
  304. }
  305. }
  306. /*
  307. * service port
  308. */
  309. if (servname) {
  310. if (str_isnumber(servname)) {
  311. if (pai->ai_socktype == GAI_ANY) {
  312. /* caller accept *GAI_ANY* socktype */
  313. pai->ai_socktype = SOCK_DGRAM;
  314. pai->ai_protocol = IPPROTO_UDP;
  315. }
  316. long maybe_port = strtol(servname, NULL, 10);
  317. if (maybe_port < 0 || maybe_port > 0xffff) {
  318. ERR(EAI_SERVICE);
  319. }
  320. port = htons((u_short)maybe_port);
  321. } else {
  322. struct servent *sp;
  323. const char *proto;
  324. proto = NULL;
  325. switch (pai->ai_socktype) {
  326. case GAI_ANY:
  327. proto = NULL;
  328. break;
  329. case SOCK_DGRAM:
  330. proto = "udp";
  331. break;
  332. case SOCK_STREAM:
  333. proto = "tcp";
  334. break;
  335. default:
  336. fprintf(stderr, "panic!\n");
  337. break;
  338. }
  339. if ((sp = getservbyname(servname, proto)) == NULL)
  340. ERR(EAI_SERVICE);
  341. port = sp->s_port;
  342. if (pai->ai_socktype == GAI_ANY) {
  343. if (strcmp(sp->s_proto, "udp") == 0) {
  344. pai->ai_socktype = SOCK_DGRAM;
  345. pai->ai_protocol = IPPROTO_UDP;
  346. } else if (strcmp(sp->s_proto, "tcp") == 0) {
  347. pai->ai_socktype = SOCK_STREAM;
  348. pai->ai_protocol = IPPROTO_TCP;
  349. } else
  350. ERR(EAI_PROTOCOL); /*xxx*/
  351. }
  352. }
  353. }
  354. /*
  355. * hostname == NULL.
  356. * passive socket -> anyaddr (0.0.0.0 or ::)
  357. * non-passive socket -> localhost (127.0.0.1 or ::1)
  358. */
  359. if (hostname == NULL) {
  360. struct gai_afd *gai_afd;
  361. for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) {
  362. if (!(pai->ai_family == PF_UNSPEC
  363. || pai->ai_family == gai_afd->a_af)) {
  364. continue;
  365. }
  366. if (pai->ai_flags & AI_PASSIVE) {
  367. GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port);
  368. /* xxx meaningless?
  369. * GET_CANONNAME(cur->ai_next, "anyaddr");
  370. */
  371. } else {
  372. GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback,
  373. port);
  374. /* xxx meaningless?
  375. * GET_CANONNAME(cur->ai_next, "localhost");
  376. */
  377. }
  378. cur = cur->ai_next;
  379. }
  380. top = sentinel.ai_next;
  381. if (top)
  382. goto good;
  383. else
  384. ERR(EAI_FAMILY);
  385. }
  386. /* hostname as numeric name */
  387. for (i = 0; gai_afdl[i].a_af; i++) {
  388. if (inet_pton(gai_afdl[i].a_af, hostname, pton)) {
  389. u_long v4a;
  390. #ifdef ENABLE_IPV6
  391. u_char pfx;
  392. #endif
  393. switch (gai_afdl[i].a_af) {
  394. case AF_INET:
  395. v4a = ((struct in_addr *)pton)->s_addr;
  396. v4a = ntohl(v4a);
  397. if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
  398. pai->ai_flags &= ~AI_CANONNAME;
  399. v4a >>= IN_CLASSA_NSHIFT;
  400. if (v4a == 0 || v4a == IN_LOOPBACKNET)
  401. pai->ai_flags &= ~AI_CANONNAME;
  402. break;
  403. #ifdef ENABLE_IPV6
  404. case AF_INET6:
  405. pfx = ((struct in6_addr *)pton)->s6_addr[0];
  406. if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
  407. pai->ai_flags &= ~AI_CANONNAME;
  408. break;
  409. #endif
  410. }
  411. if (pai->ai_family == gai_afdl[i].a_af ||
  412. pai->ai_family == PF_UNSPEC) {
  413. if (! (pai->ai_flags & AI_CANONNAME)) {
  414. GET_AI(top, &gai_afdl[i], pton, port);
  415. goto good;
  416. }
  417. /*
  418. * if AI_CANONNAME and if reverse lookup
  419. * fail, return ai anyway to pacify
  420. * calling application.
  421. *
  422. * XXX getaddrinfo() is a name->address
  423. * translation function, and it looks strange
  424. * that we do addr->name translation here.
  425. */
  426. get_name(pton, &gai_afdl[i], &top, pton, pai, port);
  427. goto good;
  428. } else
  429. ERR(EAI_FAMILY); /*xxx*/
  430. }
  431. }
  432. if (pai->ai_flags & AI_NUMERICHOST)
  433. ERR(EAI_NONAME);
  434. /* hostname as alphabetical name */
  435. error = get_addr(hostname, pai->ai_family, &top, pai, port);
  436. if (error == 0) {
  437. if (top) {
  438. good:
  439. *res = top;
  440. return SUCCESS;
  441. } else
  442. error = EAI_FAIL;
  443. }
  444. free:
  445. if (top)
  446. freeaddrinfo(top);
  447. bad:
  448. *res = NULL;
  449. return error;
  450. }
  451. static int
  452. get_name(addr, gai_afd, res, numaddr, pai, port0)
  453. const char *addr;
  454. struct gai_afd *gai_afd;
  455. struct addrinfo **res;
  456. char *numaddr;
  457. struct addrinfo *pai;
  458. int port0;
  459. {
  460. u_short port = port0 & 0xffff;
  461. struct hostent *hp;
  462. struct addrinfo *cur;
  463. int error = 0;
  464. #ifdef ENABLE_IPV6
  465. int h_error;
  466. #endif
  467. #ifdef ENABLE_IPV6
  468. hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error);
  469. #else
  470. hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET);
  471. #endif
  472. if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
  473. GET_AI(cur, gai_afd, hp->h_addr_list[0], port);
  474. GET_CANONNAME(cur, hp->h_name);
  475. } else
  476. GET_AI(cur, gai_afd, numaddr, port);
  477. #ifdef ENABLE_IPV6
  478. if (hp)
  479. freehostent(hp);
  480. #endif
  481. *res = cur;
  482. return SUCCESS;
  483. free:
  484. if (cur)
  485. freeaddrinfo(cur);
  486. #ifdef ENABLE_IPV6
  487. if (hp)
  488. freehostent(hp);
  489. #endif
  490. /* bad: */
  491. *res = NULL;
  492. return error;
  493. }
  494. static int
  495. get_addr(hostname, af, res, pai, port0)
  496. const char *hostname;
  497. int af;
  498. struct addrinfo **res;
  499. struct addrinfo *pai;
  500. int port0;
  501. {
  502. u_short port = port0 & 0xffff;
  503. struct addrinfo sentinel;
  504. struct hostent *hp;
  505. struct addrinfo *top, *cur;
  506. struct gai_afd *gai_afd;
  507. int i, error = 0, h_error;
  508. char *ap;
  509. top = NULL;
  510. sentinel.ai_next = NULL;
  511. cur = &sentinel;
  512. #ifdef ENABLE_IPV6
  513. if (af == AF_UNSPEC) {
  514. hp = getipnodebyname(hostname, AF_INET6,
  515. AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
  516. } else
  517. hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
  518. #else
  519. hp = gethostbyname(hostname);
  520. h_error = h_errno;
  521. #endif
  522. if (hp == NULL) {
  523. switch (h_error) {
  524. case HOST_NOT_FOUND:
  525. case NO_DATA:
  526. error = EAI_NODATA;
  527. break;
  528. case TRY_AGAIN:
  529. error = EAI_AGAIN;
  530. break;
  531. case NO_RECOVERY:
  532. default:
  533. error = EAI_FAIL;
  534. break;
  535. }
  536. goto free;
  537. }
  538. if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
  539. (hp->h_addr_list[0] == NULL)) {
  540. error = EAI_FAIL;
  541. goto free;
  542. }
  543. for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
  544. switch (af) {
  545. #ifdef ENABLE_IPV6
  546. case AF_INET6:
  547. gai_afd = &gai_afdl[N_INET6];
  548. break;
  549. #endif
  550. #ifndef ENABLE_IPV6
  551. default: /* AF_UNSPEC */
  552. #endif
  553. case AF_INET:
  554. gai_afd = &gai_afdl[N_INET];
  555. break;
  556. #ifdef ENABLE_IPV6
  557. default: /* AF_UNSPEC */
  558. if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
  559. ap += sizeof(struct in6_addr) -
  560. sizeof(struct in_addr);
  561. gai_afd = &gai_afdl[N_INET];
  562. } else
  563. gai_afd = &gai_afdl[N_INET6];
  564. break;
  565. #endif
  566. }
  567. #ifdef FAITH
  568. if (translate && gai_afd->a_af == AF_INET) {
  569. struct in6_addr *in6;
  570. GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port);
  571. in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
  572. memcpy(&in6->s6_addr32[0], &faith_prefix,
  573. sizeof(struct in6_addr) - sizeof(struct in_addr));
  574. memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
  575. } else
  576. #endif /* FAITH */
  577. GET_AI(cur->ai_next, gai_afd, ap, port);
  578. if (cur == &sentinel) {
  579. top = cur->ai_next;
  580. GET_CANONNAME(top, hp->h_name);
  581. }
  582. cur = cur->ai_next;
  583. }
  584. #ifdef ENABLE_IPV6
  585. freehostent(hp);
  586. #endif
  587. *res = top;
  588. return SUCCESS;
  589. free:
  590. if (top)
  591. freeaddrinfo(top);
  592. #ifdef ENABLE_IPV6
  593. if (hp)
  594. freehostent(hp);
  595. #endif
  596. /* bad: */
  597. *res = NULL;
  598. return error;
  599. }
  600. #endif // HAVE_NETDB_H