Commit a0ef686c authored by Yang Tse's avatar Yang Tse
Browse files

Merged existing IPv4 and IPv6 Curl_ip2addr functions into a single one

which now also takes a protocol address family argument.
parent 2903a5c0
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -6,6 +6,10 @@

                                  Changelog

Yang Tse (6 Nov 2008)
- Merged existing IPv4 and IPv6 Curl_ip2addr functions into a single one
  which now also takes a protocol address family argument.

Version 7.19.1 (5 November 2008)

Daniel Stenberg (4 Nov 2008)
+0 −1
Original line number Diff line number Diff line
@@ -91,7 +91,6 @@
#include "multiif.h"
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
#include "inet_ntop.h"
#include "inet_pton.h"
#include "sslgen.h" /* for Curl_ssl_check_cxn() */

/* The last #include file should be: */
+100 −3
Original line number Diff line number Diff line
@@ -190,7 +190,8 @@ Curl_getaddrinfo_ex(const char *nodename,

  *result = cafirst;

  return error; /* This is not a CURLcode */
  /* This is not a CURLcode */
  return error;
}
#endif /* HAVE_GETADDRINFO */

@@ -254,6 +255,8 @@ Curl_he2ai(const struct hostent *he, int port)
    /* no input == no output! */
    return NULL;

  DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));

  for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {

    int ss_size;
@@ -300,7 +303,7 @@ Curl_he2ai(const struct hostent *he, int port)

    switch (ai->ai_family) {
    case AF_INET:
      addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
      addr = (void *)ai->ai_addr; /* storage area for this info */

      memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
      addr->sin_family = (unsigned short)(he->h_addrtype);
@@ -309,7 +312,7 @@ Curl_he2ai(const struct hostent *he, int port)

#ifdef ENABLE_IPV6
    case AF_INET6:
      addr6 = (struct sockaddr_in6 *)ai->ai_addr; /* storage area for this info */
      addr6 = (void *)ai->ai_addr; /* storage area for this info */

      memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
      addr6->sin6_family = (unsigned short)(he->h_addrtype);
@@ -330,6 +333,100 @@ Curl_he2ai(const struct hostent *he, int port)
}


struct namebuff {
  struct hostent hostentry;
  union {
    struct in_addr  ina4;
#ifdef ENABLE_IPV6
    struct in6_addr ina6;
#endif
  } addrentry;
  char *h_addr_list[2];
};


/*
 * Curl_ip2addr()
 *
 * This function takes an internet address, in binary form, as input parameter
 * along with its address family and the string version of the address, and it
 * returns a Curl_addrinfo chain filled in correctly with information for the
 * given address/host
 */

Curl_addrinfo *
Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
{
  Curl_addrinfo *ai;

#if defined(VMS) && \
    defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
#pragma pointer_size save
#pragma pointer_size short
#pragma message disable PTRMISMATCH
#endif

  struct hostent  *h;
  struct namebuff *buf;
  char  *addrentry;
  char  *hoststr;
  int    addrsize;

  DEBUGASSERT(inaddr && hostname);

  buf = malloc(sizeof(struct namebuff));
  if(!buf)
    return NULL;

  hoststr = strdup(hostname);
  if(!hoststr) {
    free(buf);
    return NULL;
  }

  switch(af) {
  case AF_INET:
    addrsize = sizeof(struct in_addr);
    addrentry = (void *)&buf->addrentry.ina4;
    memcpy(addrentry, inaddr, sizeof(struct in_addr));
    break;
#ifdef ENABLE_IPV6
  case AF_INET6:
    addrsize = sizeof(struct in6_addr);
    addrentry = (void *)&buf->addrentry.ina6;
    memcpy(addrentry, inaddr, sizeof(struct in6_addr));
    break;
#endif
  default:
    free(hoststr);
    free(buf);
    return NULL;
  }

  h = &buf->hostentry;
  h->h_name = hoststr;
  h->h_aliases = NULL;
  h->h_addrtype = (short)af;
  h->h_length = (short)addrsize;
  h->h_addr_list = &buf->h_addr_list[0];
  h->h_addr_list[0] = addrentry;
  h->h_addr_list[1] = NULL; /* terminate list of entries */

#if defined(VMS) && \
    defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
#pragma pointer_size restore
#pragma message enable PTRMISMATCH
#endif

  ai = Curl_he2ai(h, port);

  free(hoststr);
  free(buf);

  return ai;
}


#if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
/*
 * curl_dofreeaddrinfo()
+2 −0
Original line number Diff line number Diff line
@@ -78,6 +78,8 @@ Curl_getaddrinfo_ex(const char *nodename,
Curl_addrinfo *
Curl_he2ai(const struct hostent *he, int port);

Curl_addrinfo *
Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port);

#if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
void
+31 −32
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@
#include "connect.h"
#include "strerror.h"
#include "inet_ntop.h"
#include "inet_pton.h"
#include "select.h"
#include "parsedate.h" /* for the week day and month names */
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
@@ -1105,19 +1106,18 @@ static CURLcode ftp_state_use_port(struct connectdata *conn,

  (void)fcmd; /* not used in the IPv4 code */
  if(ftpportstr) {
    in_addr_t in;
    struct in_addr in;

    /* First check if the given name is an IP address */
    in=inet_addr(ftpportstr);

    if(in != CURL_INADDR_NONE)
    /* First check if the given string is an IP address */
    if(Curl_inet_pton(AF_INET, ftpportstr, &in) > 0) {
      /* this is an IPv4 address */
      addr = Curl_ip2addr(in, ftpportstr, 0);
    else {
      if(Curl_if2ip(AF_INET, ftpportstr, myhost, sizeof(myhost))) {
      addr = Curl_ip2addr(AF_INET, &in, ftpportstr, 0);
    }
    /* otherwise check if the given string is an interface */
    else if(Curl_if2ip(AF_INET, ftpportstr, myhost, sizeof(myhost))) {
      /* The interface to IP conversion provided a dotted address */
        in=inet_addr(myhost);
        addr = Curl_ip2addr(in, myhost, 0);
      if(Curl_inet_pton(AF_INET, myhost, &in) > 0)
        addr = Curl_ip2addr(AF_INET, &in, myhost, 0);
    }
    else if(strlen(ftpportstr)> 1) {
      /* might be a host name! */
@@ -1139,7 +1139,6 @@ static CURLcode ftp_state_use_port(struct connectdata *conn,
        infof(data, "Failed to resolve host name %s\n", ftpportstr);
      }
    } /* strlen */
    } /* CURL_INADDR_NONE */
  } /* ftpportstr */

  if(!addr) {
Loading