ext/socket/getaddrinfo.c


DEFINITIONS

This source file includes following functions.
  1. gai_strerror
  2. freeaddrinfo
  3. str_isnumber
  4. inet_pton
  5. getaddrinfo
  6. get_name
  7. get_addr


   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  /*
  31   * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
  32   *
  33   * Issues to be discussed:
  34   * - Thread safe-ness must be checked.
  35   * - Return values.  There are nonstandard return values defined and used
  36   *   in the source code.  This is because RFC2133 is silent about which error
  37   *   code must be returned for which situation.
  38   * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
  39   */
  40  
  41  #include "config.h"
  42  #include <sys/types.h>
  43  #ifndef NT
  44  #include <sys/param.h>
  45  #if defined(__BEOS__)
  46  # include <net/socket.h>
  47  #else
  48  # include <sys/socket.h>
  49  #endif
  50  #include <netinet/in.h>
  51  #if defined(HAVE_ARPA_INET_H)
  52  #include <arpa/inet.h>
  53  #endif
  54  #if defined(HAVE_ARPA_NAMESER_H)
  55  #include <arpa/nameser.h>
  56  #endif
  57  #include <netdb.h>
  58  #if defined(HAVE_RESOLV_H)
  59  #include <resolv.h>
  60  #endif
  61  #include <unistd.h>
  62  #else
  63  #include <winsock2.h>
  64  #include <io.h>
  65  #endif
  66  #include <string.h>
  67  #include <stdio.h>
  68  #include <stdlib.h>
  69  #include <stddef.h>
  70  #include <ctype.h>
  71  
  72  #ifdef SOCKS5
  73  #include <socks.h>
  74  #endif
  75  
  76  #include "addrinfo.h"
  77  #include "sockport.h"
  78  
  79  #if defined(__KAME__) && defined(INET6)
  80  # define FAITH
  81  #endif
  82  
  83  #define SUCCESS 0
  84  #define ANY 0
  85  #define YES 1
  86  #define NO  0
  87  
  88  #ifdef FAITH
  89  static int translate = NO;
  90  static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
  91  #endif
  92  
  93  static const char in_addrany[] = { 0, 0, 0, 0 };
  94  static const char in6_addrany[] = {
  95          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  96  };
  97  static const char in_loopback[] = { 127, 0, 0, 1 }; 
  98  static const char in6_loopback[] = {
  99          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
 100  };
 101  
 102  struct sockinet {
 103          u_char  si_len;
 104          u_char  si_family;
 105          u_short si_port;
 106  };
 107  
 108  static struct afd {
 109          int a_af;
 110          int a_addrlen;
 111          int a_socklen;
 112          int a_off;
 113          const char *a_addrany;
 114          const char *a_loopback; 
 115  } afdl [] = {
 116  #ifdef INET6
 117  #define N_INET6 0
 118          {PF_INET6, sizeof(struct in6_addr),
 119           sizeof(struct sockaddr_in6),
 120           offsetof(struct sockaddr_in6, sin6_addr),
 121           in6_addrany, in6_loopback},
 122  #define N_INET  1
 123  #else
 124  #define N_INET  0
 125  #endif
 126          {PF_INET, sizeof(struct in_addr),
 127           sizeof(struct sockaddr_in),
 128           offsetof(struct sockaddr_in, sin_addr),
 129           in_addrany, in_loopback},
 130          {0, 0, 0, 0, NULL, NULL},
 131  };
 132  
 133  #ifdef INET6
 134  #define PTON_MAX        16
 135  #else
 136  #define PTON_MAX        4
 137  #endif
 138  
 139  static int get_name __P((const char *, struct afd *,
 140                            struct addrinfo **, char *, struct addrinfo *,
 141                            int));
 142  static int get_addr __P((const char *, int, struct addrinfo **,
 143                          struct addrinfo *, int));
 144  static int str_isnumber __P((const char *));
 145          
 146  static const char *ai_errlist[] = {
 147          "success.",
 148          "address family for hostname not supported.",   /* EAI_ADDRFAMILY */
 149          "temporary failure in name resolution.",        /* EAI_AGAIN      */
 150          "invalid value for ai_flags.",                  /* EAI_BADFLAGS   */
 151          "non-recoverable failure in name resolution.",  /* EAI_FAIL       */
 152          "ai_family not supported.",                     /* EAI_FAMILY     */
 153          "memory allocation failure.",                   /* EAI_MEMORY     */
 154          "no address associated with hostname.",         /* EAI_NODATA     */
 155          "hostname nor servname provided, or not known.",/* EAI_NONAME     */
 156          "servname not supported for ai_socktype.",      /* EAI_SERVICE    */
 157          "ai_socktype not supported.",                   /* EAI_SOCKTYPE   */
 158          "system error returned in errno.",              /* EAI_SYSTEM     */
 159          "invalid value for hints.",                     /* EAI_BADHINTS   */
 160          "resolved protocol is unknown.",                /* EAI_PROTOCOL   */
 161          "unknown error.",                               /* EAI_MAX        */
 162  };
 163  
 164  #define GET_CANONNAME(ai, str) \
 165  if (pai->ai_flags & AI_CANONNAME) {\
 166          if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
 167                  strcpy((ai)->ai_canonname, (str));\
 168          } else {\
 169                  error = EAI_MEMORY;\
 170                  goto free;\
 171          }\
 172  }
 173  
 174  #define GET_AI(ai, afd, addr, port) {\
 175          char *p;\
 176          if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
 177                                                ((afd)->a_socklen)))\
 178              == NULL) {\
 179                  error = EAI_MEMORY;\
 180                  goto free;\
 181          }\
 182          memcpy(ai, pai, sizeof(struct addrinfo));\
 183          (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
 184          memset((ai)->ai_addr, 0, (afd)->a_socklen);\
 185          SET_SA_LEN((ai)->ai_addr, (ai)->ai_addrlen = (afd)->a_socklen);\
 186          (ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
 187          ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
 188          p = (char *)((ai)->ai_addr);\
 189          memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
 190  }
 191  
 192  #define ERR(err) { error = (err); goto bad; }
 193  
 194  #if defined __UCLIBC__
 195  const
 196  #endif
 197  char *
 198  gai_strerror(ecode)
 199          int ecode;
 200  {
 201          if (ecode < 0 || ecode > EAI_MAX)
 202                  ecode = EAI_MAX;
 203          return (char *)ai_errlist[ecode];
 204  }
 205  
 206  void
 207  freeaddrinfo(ai)
 208          struct addrinfo *ai;
 209  {
 210          struct addrinfo *next;
 211  
 212          do {
 213                  next = ai->ai_next;
 214                  if (ai->ai_canonname)
 215                          free(ai->ai_canonname);
 216                  /* no need to free(ai->ai_addr) */
 217                  free(ai);
 218          } while ((ai = next) != NULL);
 219  }
 220  
 221  static int
 222  str_isnumber(p)
 223          const char *p;
 224  {
 225          char *q = (char *)p;
 226          while (*q) {
 227                  if (! isdigit(*q))
 228                          return NO;
 229                  q++;
 230          }
 231          return YES;
 232  }
 233  
 234  #ifndef HAVE_INET_PTON
 235  
 236  static int
 237  inet_pton(af, hostname, pton)
 238          int af;
 239          const char *hostname;
 240          void *pton;
 241  {
 242          struct in_addr in;
 243  
 244  #ifdef HAVE_INET_ATON
 245          if (!inet_aton(hostname, &in))
 246              return 0;
 247  #else
 248          int d1, d2, d3, d4;
 249          char ch;
 250  
 251          if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
 252              0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
 253              0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
 254              in.s_addr = htonl(
 255                  ((long) d1 << 24) | ((long) d2 << 16) |
 256                  ((long) d3 << 8) | ((long) d4 << 0));
 257          }
 258          else {
 259              return 0;
 260          }
 261  #endif
 262          memcpy(pton, &in, sizeof(in));
 263          return 1;
 264  }
 265  #endif
 266  
 267  int
 268  getaddrinfo(hostname, servname, hints, res)
 269          const char *hostname, *servname;
 270          const struct addrinfo *hints;
 271          struct addrinfo **res;
 272  {
 273          struct addrinfo sentinel;
 274          struct addrinfo *top = NULL;
 275          struct addrinfo *cur;
 276          int i, error = 0;
 277          char pton[PTON_MAX];
 278          struct addrinfo ai;
 279          struct addrinfo *pai;
 280          u_short port;
 281  
 282  #ifdef FAITH
 283          static int firsttime = 1;
 284  
 285          if (firsttime) {
 286                  /* translator hack */
 287                  {
 288                          char *q = getenv("GAI");
 289                          if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
 290                                  translate = YES;
 291                  }
 292                  firsttime = 0;
 293          }
 294  #endif
 295  
 296          /* initialize file static vars */
 297          sentinel.ai_next = NULL;
 298          cur = &sentinel;
 299          pai = &ai;
 300          pai->ai_flags = 0;
 301          pai->ai_family = PF_UNSPEC;
 302          pai->ai_socktype = ANY;
 303          pai->ai_protocol = ANY;
 304          pai->ai_addrlen = 0;
 305          pai->ai_canonname = NULL;
 306          pai->ai_addr = NULL;
 307          pai->ai_next = NULL;
 308          port = ANY;
 309          
 310          if (hostname == NULL && servname == NULL)
 311                  return EAI_NONAME;
 312          if (hints) {
 313                  /* error check for hints */
 314                  if (hints->ai_addrlen || hints->ai_canonname ||
 315                      hints->ai_addr || hints->ai_next)
 316                          ERR(EAI_BADHINTS); /* xxx */
 317                  if (hints->ai_flags & ~AI_MASK)
 318                          ERR(EAI_BADFLAGS);
 319                  switch (hints->ai_family) {
 320                  case PF_UNSPEC:
 321                  case PF_INET:
 322  #ifdef INET6
 323                  case PF_INET6:
 324  #endif
 325                          break;
 326                  default:
 327                          ERR(EAI_FAMILY);
 328                  }
 329                  memcpy(pai, hints, sizeof(*pai));
 330                  switch (pai->ai_socktype) {
 331                  case ANY:
 332                          switch (pai->ai_protocol) {
 333                          case ANY:
 334                                  break;
 335                          case IPPROTO_UDP:
 336                                  pai->ai_socktype = SOCK_DGRAM;
 337                                  break;
 338                          case IPPROTO_TCP:
 339                                  pai->ai_socktype = SOCK_STREAM;
 340                                  break;
 341                          default:
 342  #if defined(SOCK_RAW)
 343                                  pai->ai_socktype = SOCK_RAW;
 344  #endif
 345                                  break;
 346                          }
 347                          break;
 348  #if defined(SOCK_RAW)
 349                  case SOCK_RAW:
 350                          break;
 351  #endif
 352                  case SOCK_DGRAM:
 353                          if (pai->ai_protocol != IPPROTO_UDP &&
 354                              pai->ai_protocol != ANY)
 355                                  ERR(EAI_BADHINTS);      /*xxx*/
 356                          pai->ai_protocol = IPPROTO_UDP;
 357                          break;
 358                  case SOCK_STREAM:
 359                          if (pai->ai_protocol != IPPROTO_TCP &&
 360                              pai->ai_protocol != ANY)
 361                                  ERR(EAI_BADHINTS);      /*xxx*/
 362                          pai->ai_protocol = IPPROTO_TCP;
 363                          break;
 364                  default:
 365                          ERR(EAI_SOCKTYPE);
 366                          break;
 367                  }
 368          }
 369  
 370          /*
 371           * service port
 372           */
 373          if (servname) {
 374                  if (str_isnumber(servname)) {
 375                          if (pai->ai_socktype == ANY) {
 376                                  /* caller accept *ANY* socktype */
 377                                  pai->ai_socktype = SOCK_DGRAM;
 378                                  pai->ai_protocol = IPPROTO_UDP;
 379                          }
 380                          port = htons((unsigned short)atoi(servname));
 381                  } else {
 382                          struct servent *sp;
 383                          char *proto;
 384  
 385                          proto = NULL;
 386                          switch (pai->ai_socktype) {
 387                          case ANY:
 388                                  proto = NULL;
 389                                  break;
 390                          case SOCK_DGRAM:
 391                                  proto = "udp";
 392                                  break;
 393                          case SOCK_STREAM:
 394                                  proto = "tcp";
 395                                  break;
 396                          default:
 397                                  fprintf(stderr, "panic!\n");
 398                                  break;
 399                          }
 400                          if ((sp = getservbyname(servname, proto)) == NULL)
 401                                  ERR(EAI_SERVICE);
 402                          port = sp->s_port;
 403                          if (pai->ai_socktype == ANY)
 404                                  if (strcmp(sp->s_proto, "udp") == 0) {
 405                                          pai->ai_socktype = SOCK_DGRAM;
 406                                          pai->ai_protocol = IPPROTO_UDP;
 407                                  } else if (strcmp(sp->s_proto, "tcp") == 0) {
 408                                          pai->ai_socktype = SOCK_STREAM;
 409                                          pai->ai_protocol = IPPROTO_TCP;
 410                                  } else
 411                                          ERR(EAI_PROTOCOL);      /*xxx*/
 412                  }
 413          }
 414          
 415          /*
 416           * hostname == NULL.
 417           * passive socket -> anyaddr (0.0.0.0 or ::)
 418           * non-passive socket -> localhost (127.0.0.1 or ::1)
 419           */
 420          if (hostname == NULL) {
 421                  struct afd *afd;
 422                  int s;
 423  
 424                  for (afd = &afdl[0]; afd->a_af; afd++) {
 425                          if (!(pai->ai_family == PF_UNSPEC
 426                             || pai->ai_family == afd->a_af)) {
 427                                  continue;
 428                          }
 429  
 430                          /*
 431                           * filter out AFs that are not supported by the kernel
 432                           * XXX errno?
 433                           */
 434                          s = socket(afd->a_af, SOCK_DGRAM, 0);
 435                          if (s < 0)
 436                                  continue;
 437  #if defined(HAVE_CLOSESOCKET)
 438                          closesocket(s);
 439  #else
 440                          close(s);
 441  #endif
 442  
 443                          if (pai->ai_flags & AI_PASSIVE) {
 444                                  GET_AI(cur->ai_next, afd, afd->a_addrany, port);
 445                                  /* xxx meaningless?
 446                                   * GET_CANONNAME(cur->ai_next, "anyaddr");
 447                                   */
 448                          } else {
 449                                  GET_AI(cur->ai_next, afd, afd->a_loopback,
 450                                          port);
 451                                  /* xxx meaningless?
 452                                   * GET_CANONNAME(cur->ai_next, "localhost");
 453                                   */
 454                          }
 455                          cur = cur->ai_next;
 456                  }
 457                  top = sentinel.ai_next;
 458                  if (top)
 459                          goto good;
 460                  else
 461                          ERR(EAI_FAMILY);
 462          }
 463          
 464          /* hostname as numeric name */
 465          for (i = 0; afdl[i].a_af; i++) {
 466                  if (inet_pton(afdl[i].a_af, hostname, pton)) {
 467                          u_long v4a;
 468  #ifdef INET6
 469                          u_char pfx;
 470  #endif
 471  
 472                          switch (afdl[i].a_af) {
 473                          case AF_INET:
 474                                  v4a = ((struct in_addr *)pton)->s_addr;
 475                                  if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
 476                                          pai->ai_flags &= ~AI_CANONNAME;
 477                                  v4a >>= IN_CLASSA_NSHIFT;
 478                                  if (v4a == 0 || v4a == IN_LOOPBACKNET)
 479                                          pai->ai_flags &= ~AI_CANONNAME;
 480                                  break;
 481  #ifdef INET6
 482                          case AF_INET6:
 483                                  pfx = ((struct in6_addr *)pton)->s6_addr8[0];
 484                                  if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
 485                                          pai->ai_flags &= ~AI_CANONNAME;
 486                                  break;
 487  #endif
 488                          }
 489                          
 490                          if (pai->ai_family == afdl[i].a_af ||
 491                              pai->ai_family == PF_UNSPEC) {
 492                                  if (! (pai->ai_flags & AI_CANONNAME)) {
 493                                          GET_AI(top, &afdl[i], pton, port);
 494                                          goto good;
 495                                  }
 496                                  /*
 497                                   * if AI_CANONNAME and if reverse lookup
 498                                   * fail, return ai anyway to pacify
 499                                   * calling application.
 500                                   *
 501                                   * XXX getaddrinfo() is a name->address
 502                                   * translation function, and it looks strange
 503                                   * that we do addr->name translation here.
 504                                   */
 505                                  get_name(pton, &afdl[i], &top, pton, pai, port);
 506                                  goto good;
 507                          } else 
 508                                  ERR(EAI_FAMILY);        /*xxx*/
 509                  }
 510          }
 511  
 512          if (pai->ai_flags & AI_NUMERICHOST)
 513                  ERR(EAI_NONAME);
 514  
 515          /* hostname as alphabetical name */
 516          error = get_addr(hostname, pai->ai_family, &top, pai, port);
 517          if (error == 0) {
 518                  if (top) {
 519   good:
 520                          *res = top;
 521                          return SUCCESS;
 522                  } else
 523                          error = EAI_FAIL;
 524          }
 525   free:
 526          if (top)
 527                  freeaddrinfo(top);
 528   bad:
 529          *res = NULL;
 530          return error;
 531  }
 532  
 533  static int
 534  get_name(addr, afd, res, numaddr, pai, port0)
 535          const char *addr;
 536          struct afd *afd;
 537          struct addrinfo **res;
 538          char *numaddr;
 539          struct addrinfo *pai;
 540          int port0;
 541  {
 542          u_short port = port0 & 0xffff;
 543          struct hostent *hp;
 544          struct addrinfo *cur;
 545          int error = 0;
 546  #ifdef INET6
 547          int h_error;
 548  #endif
 549  
 550  #ifdef INET6
 551          hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
 552  #else
 553          hp = gethostbyaddr(addr, afd->a_addrlen, AF_INET);
 554  #endif
 555          if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
 556                  GET_AI(cur, afd, hp->h_addr_list[0], port);
 557                  GET_CANONNAME(cur, hp->h_name);
 558          } else
 559                  GET_AI(cur, afd, numaddr, port);
 560          
 561  #ifdef INET6
 562          if (hp)
 563                  freehostent(hp);
 564  #endif
 565          *res = cur;
 566          return SUCCESS;
 567   free:
 568          if (cur)
 569                  freeaddrinfo(cur);
 570  #ifdef INET6
 571          if (hp)
 572                  freehostent(hp);
 573  #endif
 574   /* bad: */
 575          *res = NULL;
 576          return error;
 577  }
 578  
 579  static int
 580  get_addr(hostname, af, res, pai, port0)
 581          const char *hostname;
 582          int af;
 583          struct addrinfo **res;
 584          struct addrinfo *pai;
 585          int port0;
 586  {
 587          u_short port = port0 & 0xffff;
 588          struct addrinfo sentinel;
 589          struct hostent *hp;
 590          struct addrinfo *top, *cur;
 591          struct afd *afd;
 592          int i, error = 0, h_error;
 593          char *ap;
 594  
 595          top = NULL;
 596          sentinel.ai_next = NULL;
 597          cur = &sentinel;
 598  #ifdef INET6
 599          if (af == AF_UNSPEC) {
 600                  hp = getipnodebyname(hostname, AF_INET6,
 601                                  AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
 602          } else
 603                  hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
 604  #else
 605          hp = gethostbyname(hostname);
 606          h_error = h_errno;
 607  #endif
 608          if (hp == NULL) {
 609                  switch (h_error) {
 610                  case HOST_NOT_FOUND:
 611                  case NO_DATA:
 612                          error = EAI_NODATA;
 613                          break;
 614                  case TRY_AGAIN:
 615                          error = EAI_AGAIN;
 616                          break;
 617                  case NO_RECOVERY:
 618                  default:
 619                          error = EAI_FAIL;
 620                          break;
 621                  }
 622                  goto bad;
 623          }
 624  
 625          if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
 626              (hp->h_addr_list[0] == NULL))
 627                  ERR(EAI_FAIL);
 628          
 629          for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
 630                  switch (af) {
 631  #ifdef INET6
 632                  case AF_INET6:
 633                          afd = &afdl[N_INET6];
 634                          break;
 635  #endif
 636  #ifndef INET6
 637                  default:        /* AF_UNSPEC */
 638  #endif
 639                  case AF_INET:
 640                          afd = &afdl[N_INET];
 641                          break;
 642  #ifdef INET6
 643                  default:        /* AF_UNSPEC */
 644                          if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
 645                                  ap += sizeof(struct in6_addr) -
 646                                          sizeof(struct in_addr);
 647                                  afd = &afdl[N_INET];
 648                          } else
 649                                  afd = &afdl[N_INET6];
 650                          break;
 651  #endif
 652                  }
 653  #ifdef FAITH
 654                  if (translate && afd->a_af == AF_INET) {
 655                          struct in6_addr *in6;
 656  
 657                          GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
 658                          in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
 659                          memcpy(&in6->s6_addr32[0], &faith_prefix,
 660                              sizeof(struct in6_addr) - sizeof(struct in_addr));
 661                          memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
 662                  } else
 663  #endif /* FAITH */
 664                  GET_AI(cur->ai_next, afd, ap, port);
 665                  if (cur == &sentinel) {
 666                          top = cur->ai_next;
 667                          GET_CANONNAME(top, hp->h_name);
 668                  }
 669                  cur = cur->ai_next;
 670          }
 671  #ifdef INET6
 672          freehostent(hp);
 673  #endif
 674          *res = top;
 675          return SUCCESS;
 676   free:
 677          if (top)
 678                  freeaddrinfo(top);
 679  #ifdef INET6
 680          if (hp)
 681                  freehostent(hp);
 682  #endif
 683   bad:
 684          *res = NULL;
 685          return error;
 686  }