Commit a0ce95e1 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

David LeBlanc's fixes!

parent abc751ae
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ all: all-redirect
.SUFFIXES:
.SUFFIXES: .S .c .o .s
$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) 
	cd $(top_srcdir) && $(AUTOMAKE) --foreign --include-deps lib/Makefile
	cd $(top_srcdir) && $(AUTOMAKE) --foreign lib/Makefile

Makefile: $(srcdir)/Makefile.in  $(top_builddir)/config.status
	cd $(top_builddir) \
+54 −36
Original line number Diff line number Diff line
@@ -56,7 +56,10 @@
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
#include <winsock.h>
#else /* some kind of unix */
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <sys/types.h>
#include <netinet/in.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
@@ -69,6 +72,9 @@
#include <errno.h>
#endif

#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif

#include <curl/curl.h>
#include "urldata.h"
@@ -241,26 +247,20 @@ int GetLastResponse(int sockfd, char *buf,
}

/* -- who are we? -- */
char *getmyhost(void)
char *getmyhost(char *buf, int buf_size)
{
  static char myhost[256];
#ifdef HAVE_UNAME
#if defined(HAVE_GETHOSTNAME)
  gethostname(buf, buf_size);
#elif defined(HAVE_UNAME)
  struct utsname ugnm;

  if (uname(&ugnm) < 0)
    return "localhost";

  (void) strncpy(myhost, ugnm.nodename, 255);
  myhost[255] = '\0';
#endif
#ifdef HAVE_GETHOSTNAME
  gethostname(myhost, 256);
#endif
#if !defined(HAVE_UNAME) && !defined(HAVE_GETHOSTNAME)
  strncpy(buf, uname(&ugnm) < 0 ? "localhost" : ugnm.nodename, buf_size - 1);
  buf[buf_size - 1] = '\0';
#else
  /* We have no means of finding the local host name! */
  strcpy(myhost, "localhost");
  strncpy(buf, "localhost", buf_size);
  buf[buf_size - 1] = '\0';
#endif
  return myhost;
  return buf;
}

#if 0
@@ -473,6 +473,10 @@ CURLcode _ftp(struct connectdata *conn)
  /* for the ftp PORT mode */
  int portsock=-1;
  struct sockaddr_in serv_addr;
  char hostent_buf[512];
#if defined (HAVE_INET_NTOA_R)
  char ntoa_buf[64];
#endif

  struct curl_slist *qitem; /* QUOTE item */
  /* the ftp struct is already inited in ftp_connect() */
@@ -542,24 +546,21 @@ CURLcode _ftp(struct connectdata *conn)
    struct hostent *h=NULL;
    size_t size;
    unsigned short porttouse;

    char *myhost=NULL;
    char myhost[256] = "";

    if(data->ftpport) {
      myhost = if2ip(data->ftpport);
      if(myhost) {
        h = GetHost(data, myhost);
      if(if2ip(data->ftpport, myhost, sizeof(myhost))) {
        h = GetHost(data, myhost, hostent_buf, sizeof(hostent_buf));
      }
      else {
        if(strlen(data->ftpport)>1)
          h = GetHost(data, data->ftpport);
          h = GetHost(data, data->ftpport, hostent_buf, sizeof(hostent_buf));
        if(h)
          myhost=data->ftpport;
          strcpy(myhost,data->ftpport);
      }
    }
    if(!myhost) {
      myhost = getmyhost();
      h=GetHost(data, myhost);
    if(! *myhost) {
      h=GetHost(data, getmyhost(myhost,sizeof(myhost)), hostent_buf, sizeof(hostent_buf));
    }
    infof(data, "We connect from %s\n", myhost);

@@ -609,8 +610,13 @@ CURLcode _ftp(struct connectdata *conn)
      struct in_addr in;
      unsigned short ip[5];
      (void) memcpy(&in.s_addr, *h->h_addr_list, sizeof (in.s_addr));
#if defined (HAVE_INET_NTOA_R)
      sscanf( inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)), "%hu.%hu.%hu.%hu",
              &ip[0], &ip[1], &ip[2], &ip[3]);
#else
      sscanf( inet_ntoa(in), "%hu.%hu.%hu.%hu",
              &ip[0], &ip[1], &ip[2], &ip[3]);
#endif
      sendf(data->firstsocket, data, "PORT %d,%d,%d,%d,%d,%d\n",
            ip[0], ip[1], ip[2], ip[3],
            porttouse >> 8,
@@ -640,7 +646,7 @@ CURLcode _ftp(struct connectdata *conn)
      unsigned short newport;
      char newhost[32];
      struct hostent *he;
      char *str=buf;
      char *str=buf,*ip_addr;

      /*
       * New 227-parser June 3rd 1999.
@@ -665,7 +671,7 @@ CURLcode _ftp(struct connectdata *conn)
	 return CURLE_FTP_WEIRD_227_FORMAT;
      }
      sprintf(newhost, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
      he = GetHost(data, newhost);
      he = GetHost(data, newhost, hostent_buf, sizeof(hostent_buf));
      if(!he) {
        failf(data, "Can't resolve new host %s", newhost);
        return CURLE_FTP_CANT_GET_HOST;
@@ -682,25 +688,36 @@ CURLcode _ftp(struct connectdata *conn)

      if(data->bits.verbose) {
        struct in_addr in;
#if 1
        struct hostent * answer;

        unsigned long address;
#if defined(HAVE_INET_ADDR)
        unsigned long address;
#if defined(HAVE_GETHOSTBYADDR_R)
        int h_errnop;
#endif

        address = inet_addr(newhost);
        answer = gethostbyaddr((char *) &address, sizeof(address), 
                               AF_INET);
#if defined(HAVE_GETHOSTBYADDR_R)
        answer = gethostbyaddr_r((char *) &address, sizeof(address), AF_INET,
                                 (struct hostent *)hostent_buf,
                                 hostent_buf + sizeof(*answer),
                                 sizeof(hostent_buf) - sizeof(*answer),
                                 &h_errnop);
#else
        answer = gethostbyaddr((char *) &address, sizeof(address), AF_INET);
#endif
#else
        answer = NULL;
#endif
        (void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr));
        infof(data, "Connecting to %s (%s) port %u\n",
              answer?answer->h_name:newhost, inet_ntoa(in), newport);
              answer?answer->h_name:newhost,
#if defined(HAVE_INET_NTOA_R)
              ip_addr = inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)),
#else
        (void) memcpy(&in.s_addr, *he->h_addr_list, sizeof (in.s_addr));
        infof(data, "Connecting to %s (%s) port %u\n",
              he->h_name, inet_ntoa(in), newport);
              ip_addr = inet_ntoa(in),
#endif
              newport);
      }
	
      if (connect(data->secondarysocket, (struct sockaddr *) &serv_addr,
@@ -727,6 +744,7 @@ CURLcode _ftp(struct connectdata *conn)

  }
  /* we have the (new) data connection ready */
  infof(data, "Connected!\n");

  /* change directory first */

+50 −23
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@
 ****************************************************************************/

#include <string.h>
#include <malloc.h>

#include "setup.h"

@@ -61,51 +62,77 @@
#include "urldata.h"
#include "sendf.h"

#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif

/* --- resolve name or IP-number --- */

char *MakeIP(unsigned long num)
char *MakeIP(unsigned long num,char *addr, int addr_len)
{
#ifdef HAVE_INET_NTOA
#if defined(HAVE_INET_NTOA) || defined(HAVE_INET_NTOA_R)
  struct in_addr in;

  in.s_addr = htonl(num);
  return (inet_ntoa(in));

#if defined(HAVE_INET_NTOA_R)
  inet_ntoa_r(in,addr,addr_len);
#else
  strncpy(addr,inet_ntoa(in),addr_len);
#endif
#else
  static char addr[128];
  unsigned char *paddr;

  num = htonl(num);  /* htonl() added to avoid endian probs */
  paddr = (unsigned char *)&num;
  sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
  return (addr);
#endif
  return (addr);
}

/* Stolen from Dancer source code, written by
   Bjorn Reese <breese@imada.ou.dk> */
/* The original code to this function was stolen from the Dancer source code,
   written by Bjorn Reese, it has since been patched and modified. */
#ifndef INADDR_NONE
#define INADDR_NONE (unsigned long) ~0
#endif
struct hostent *GetHost(struct UrlData *data, char *hostname)
struct hostent *GetHost(struct UrlData *data,
                        char *hostname,
                        char *buf,
                        int buf_size )
{
  struct hostent *h = NULL;
  unsigned long in;
  static struct hostent he;
  static char name[MAXHOSTNAMELEN];
  static char *addrlist[2];
  static struct in_addr addrentry;

  if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
    addrentry.s_addr = in;
    addrlist[0] = (char *)&addrentry;
    addrlist[1] = NULL;
    he.h_name = strncpy(name, MakeIP(ntohl(in)), MAXHOSTNAMELEN);
    he.h_addrtype = AF_INET;
    he.h_length = sizeof(struct in_addr);
    he.h_addr_list = addrlist;
    h = &he;
  } else if ( (h=gethostbyname(hostname)) == NULL ) {
    struct in_addr *addrentry;

    h = (struct hostent*)buf;
    h->h_addr_list = (char**)(buf + sizeof(*h));
    addrentry = (struct in_addr*)(h->h_addr_list + 2);
    addrentry->s_addr = in;
    h->h_addr_list[0] = (char*)addrentry;
    h->h_addr_list[1] = NULL;
    h->h_addrtype = AF_INET;
    h->h_length = sizeof(*addrentry);
    h->h_name = (char*)(h->h_addr_list + h->h_length);
    MakeIP(ntohl(in),h->h_name,buf_size - (long)(h->h_name) + (long)buf);
#if defined(HAVE_GETHOSTBYNAME_R)
  }
  else {
    int h_errnop;
    memset(buf,0,buf_size);	/* workaround for gethostbyname_r bug in qnx nto */
    if ((h = gethostbyname_r(hostname,
                             (struct hostent *)buf,buf +
                             sizeof(struct hostent),buf_size -
                             sizeof(struct hostent),&h_errnop)) == NULL ) {
      infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
    }
#else
  }
  else {
    if ((h = gethostbyname(hostname)) == NULL ) {
      infof(data, "gethostbyname(2) failed for %s\n", hostname);
    }
#endif
  }
  return (h);
}
+2 −2
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@
 * ------------------------------------------------------------
 ****************************************************************************/

struct hostent *GetHost(struct UrlData *data, char *hostname);
char *MakeIP(unsigned long num);
extern struct hostent *GetHost(struct UrlData *data, char *hostname, char *buf, int buf_size );
extern char *MakeIP(unsigned long num,char *addr, int addr_len);

#endif
+11 −2
Original line number Diff line number Diff line
@@ -74,9 +74,13 @@
#include <sys/sockio.h>
#endif

#ifdef HAVE_INET_NTOA_R
#include "inet_ntoa_r.h"
#endif

#define SYS_ERROR -1

char *if2ip(char *interface)
char *if2ip(char *interface, char *buf, int buf_size)
{
  int dummy;
  char *ip=NULL;
@@ -101,7 +105,12 @@ char *if2ip(char *interface)

      struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
      memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
      ip = (char *)strdup(inet_ntoa(in));
#if defined(HAVE_INET_NTOA_R)
      ip = inet_ntoa_r(in,buf,buf_size);
#else
      ip = strncpy(buf,inet_ntoa(in),buf_size);
      ip[buf_size - 1] = 0;
#endif
    }
    close(dummy);
  }
Loading