Merge KAME.net IPv6 patch: you can now (in theory) pass IPv6 hostnames
authorMartin Pool <mbp@samba.org>
Fri, 23 Nov 2001 07:35:49 +0000 (07:35 +0000)
committerMartin Pool <mbp@samba.org>
Fri, 23 Nov 2001 07:35:49 +0000 (07:35 +0000)
or literal IP addresses to rsync, and if your platform supports them
they will be used.  Also there are -4 and -6 command-line options to
choose the default address type.  Thankyou!

Makefile.in
acconfig.h
clientserver.c
configure.in
options.c
socket.c

index 71b3fa1..0cbeb15 100644 (file)
@@ -21,6 +21,7 @@ SHELL=/bin/sh
 .SUFFIXES: .c .o
 
 LIBOBJ=lib/fnmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o
+       @LIBOBJS@
 ZLIBOBJ=zlib/deflate.o zlib/infblock.o zlib/infcodes.o zlib/inffast.o \
        zlib/inflate.o zlib/inftrees.o zlib/infutil.o zlib/trees.o \
        zlib/zutil.o zlib/adler32.o 
@@ -86,6 +87,11 @@ clean:
 distclean: clean
        rm -f config.h config.cache config.status Makefile
 
+# missing functions
+getaddrinfo.o: lib/getaddrinfo.c
+       $(CC) -I. -I$(srcdir) -I$(srcdir)/lib $(CFLAGS) -c lib/getaddrinfo.c
+getnameinfo.o: lib/getnameinfo.c
+       $(CC) -I. -I$(srcdir) -I$(srcdir)/lib $(CFLAGS) -c lib/getnameinfo.c
 
 # this target is really just for my use. It only works on a limited
 # range of machines and is used to produce a list of potentially
index 3d89a30..4b9d12a 100644 (file)
@@ -1 +1,10 @@
 #undef ino_t
+#undef HAVE_CONNECT
+#undef HAVE_SHORT_INO_T
+#undef HAVE_GETOPT_LONG
+#undef REPLACE_INET_NTOA
+#undef REPLACE_INET_ATON
+#undef HAVE_GETTIMEOFDAY_TZ
+#undef ENABLE_IPV6
+#undef HAVE_SOCKADDR_LEN
+#undef HAVE_SOCKETPAIR
index 8485886..4a16b6a 100644 (file)
@@ -42,10 +42,10 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
        char *p, *user=NULL;
        extern int remote_version;
        extern int am_sender;
-       extern struct in_addr socket_address;
        extern char *shell_cmd;
        extern int kludge_around_eof;
-       
+       extern char *bind_address;
+       
        if (argc == 0 && !am_sender) {
                extern int list_only;
                list_only = 1;
@@ -78,7 +78,7 @@ int start_socket_client(char *host, char *path, int argc, char *argv[])
        if (!user) user = getenv("USER");
        if (!user) user = getenv("LOGNAME");
 
-       fd = open_socket_out_wrapped (host, rsync_port, &socket_address);
+       fd = open_socket_out_wrapped (host, rsync_port, bind_address);
        if (fd == -1) {
                exit_cleanup(RERR_SOCKETIO);
        }
index 060ebc2..c55b07f 100644 (file)
@@ -1,4 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
+
 AC_INIT(byteorder.h)
 AC_CONFIG_HEADER(config.h)
 AC_PREREQ(2.52)
@@ -39,6 +40,7 @@ AC_DEFINE_UNQUOTED(RSYNC_PATH, "$RSYNC_PATH", [ ])
 
 dnl Checks for programs.
 AC_PROG_CC
+AC_PROG_CPP
 AC_PROG_INSTALL
 AC_SUBST(SHELL)
 
@@ -84,6 +86,285 @@ fi
 
 
 
+AC_DEFINE(ss_family, __ss_family)
+AC_DEFINE(ss_len, __ss_len)
+CFLAGS="$CFLAGS"
+AC_MSG_CHECKING([whether to enable ipv6])
+AC_ARG_ENABLE(ipv6,
+[  --enable-ipv6               Enable ipv6 (with ipv4) support
+  --disable-ipv6               Disable ipv6 support],
+[ case "$enableval" in
+  no)
+       AC_MSG_RESULT(no)
+       ipv6=no
+       ;;
+  *)   AC_MSG_RESULT(yes)
+       AC_DEFINE(ENABLE_IPV6)
+       ipv6=yes
+       ;;
+  esac ],
+
+  AC_TRY_COMPILE([ /* AF_INET6 avalable check */
+#include <sys/types.h>
+#include <sys/socket.h>
+main()
+{
+ if (socket(AF_INET6, SOCK_STREAM, 0) < 0)
+   exit(1);
+ else
+   exit(0);
+}
+],
+  AC_MSG_RESULT(yes)
+  AC_DEFINE(ENABLE_IPV6)
+  ipv6=yes,
+  AC_MSG_RESULT(no)
+  ipv6=no,
+  AC_MSG_RESULT(no)
+  ipv6=no
+))
+
+ipv6type=unknown
+ipv6lib=none
+ipv6trylibc=no
+
+if test "$ipv6" = "yes"; then
+       AC_MSG_CHECKING([ipv6 stack type])
+       for i in inria kame linux-glibc linux-inet6 toshiba v6d zeta; do
+               case $i in
+               inria)
+                       # http://www.kame.net/
+                       AC_EGREP_CPP(yes, [
+#include <netinet/in.h>
+#ifdef IPV6_INRIA_VERSION
+yes
+#endif],
+                               [ipv6type=$i;
+                               CFLAGS="-DINET6 $CFLAGS"])
+                       ;;
+               kame)
+                       # http://www.kame.net/
+                       AC_EGREP_CPP(yes, [
+#include <netinet/in.h>
+#ifdef __KAME__
+yes
+#endif],
+                               [ipv6type=$i;
+                               CFLAGS="-DINET6 $CFLAGS"])
+                       ;;
+               linux-glibc)
+                       # http://www.v6.linux.or.jp/
+                       AC_EGREP_CPP(yes, [
+#include <features.h>
+#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
+yes
+#endif],
+                               [ipv6type=$i;
+                               CFLAGS="-DINET6 $CFLAGS"])
+                       ;;
+               linux-inet6)
+                       # http://www.v6.linux.or.jp/
+                       if test -d /usr/inet6 -o -f /usr/inet6/lib/libinet6.a; then
+                               ipv6type=$i
+                               ipv6lib=inet6
+                               ipv6libdir=/usr/inet6/lib
+                               ipv6trylibc=yes;
+                               CFLAGS="-DINET6 -I/usr/inet6/include $CFLAGS"
+                       fi
+                       ;;
+               toshiba)
+                       AC_EGREP_CPP(yes, [
+#include <sys/param.h>
+#ifdef _TOSHIBA_INET6
+yes
+#endif],
+                               [ipv6type=$i;
+                               ipv6lib=inet6;
+                               ipv6libdir=/usr/local/v6/lib;
+                               CFLAGS="-DINET6 $CFLAGS"])
+                       ;;
+               v6d)
+                       AC_EGREP_CPP(yes, [
+#include </usr/local/v6/include/sys/v6config.h>
+#ifdef __V6D__
+yes
+#endif],
+                               [ipv6type=$i;
+                               ipv6lib=v6;
+                               ipv6libdir=/usr/local/v6/lib;
+                               CFLAGS="-I/usr/local/v6/include $CFLAGS"])
+                       ;;
+               zeta)
+                       AC_EGREP_CPP(yes, [
+#include <sys/param.h>
+#ifdef _ZETA_MINAMI_INET6
+yes
+#endif],
+                               [ipv6type=$i;
+                               ipv6lib=inet6;
+                               ipv6libdir=/usr/local/v6/lib;
+                               CFLAGS="-DINET6 $CFLAGS"])
+                       ;;
+               esac
+               if test "$ipv6type" != "unknown"; then
+                       break
+               fi
+       done
+       AC_MSG_RESULT($ipv6type)
+fi
+
+if test "$ipv6" = "yes" -a -f /usr/local/v6/lib/libinet6.a; then
+       ac_inet6_LDFLAGS="inet6"
+       ipv6libdir=/usr/local/v6/lib
+       LDFLAGS="$LDFLAGS -L/usr/local/v6/lib"
+       AC_CHECK_LIB(inet6, getaddrinfo, , ipv6lib="$ac_inet6_LDFLAGS")
+fi
+
+if test "$ipv6" = "yes" -a -f /usr/lib/libinet6.a; then
+       ac_inet6_LDFLAGS="inet6"
+       AC_CHECK_LIB(inet6, getaddrinfo, , ipv6lib="$ac_inet6_LDFLAGS")
+fi
+
+if test "$ipv6" = "yes" -a "$ipv6lib" != "none"; then
+       if test -d $ipv6libdir -a -f $ipv6libdir/lib$ipv6lib.a; then
+               LIBS="-L$ipv6libdir -l$ipv6lib $LIBS"
+               echo "You have $ipv6lib library, using it"
+       else
+               if test "$ipv6trylibc" = "yes"; then
+                       echo "You do not have $ipv6lib library, using libc"
+               else
+                       echo 'Fatal: no $ipv6lib library found.  cannot continue.'
+                       echo "You need to fetch lib$ipv6lib.a from appropriate"
+                       echo 'ipv6 kit and compile beforehand.'
+                       exit 1
+               fi
+       fi
+fi
+
+
+AC_MSG_CHECKING(getaddrinfo bug)
+AC_TRY_RUN([
+#include <sys/types.h>
+#include <netdb.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+main()
+{
+  int passive, gaierr, inet4 = 0, inet6 = 0;
+  struct addrinfo hints, *ai, *aitop;
+  char straddr[INET6_ADDRSTRLEN], strport[16];
+
+  for (passive = 0; passive <= 1; passive++) {
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_flags = passive ? AI_PASSIVE : 0;
+    hints.ai_socktype = SOCK_STREAM;
+    if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
+      (void)gai_strerror(gaierr);
+      goto bad;
+    }
+    for (ai = aitop; ai; ai = ai->ai_next) {
+      if (ai->ai_addr == NULL ||
+          ai->ai_addrlen == 0 ||
+          getnameinfo(ai->ai_addr, ai->ai_addrlen,
+                      straddr, sizeof(straddr), strport, sizeof(strport),
+                      NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
+        goto bad;
+      }
+      switch (ai->ai_family) {
+      case AF_INET:
+        if (strcmp(strport, "54321") != 0) {
+          goto bad;
+        }
+        if (passive) {
+          if (strcmp(straddr, "0.0.0.0") != 0) {
+            goto bad;
+          }
+        } else {
+          if (strcmp(straddr, "127.0.0.1") != 0) {
+            goto bad;
+          }
+        }
+        inet4++;
+        break;
+      case AF_INET6:
+        if (strcmp(strport, "54321") != 0) {
+          goto bad;
+        }
+        if (passive) {
+          if (strcmp(straddr, "::") != 0) {
+            goto bad;
+          }
+        } else {
+          if (strcmp(straddr, "::1") != 0) {
+            goto bad;
+          }
+        }
+        inet6++;
+        break;
+      case AF_UNSPEC:
+        goto bad;
+        break;
+      default:
+        /* another family support? */
+        break;
+      }
+    }
+  }
+
+  if (!(inet4 == 0 || inet4 == 2))
+    goto bad;
+  if (!(inet6 == 0 || inet6 == 2))
+    goto bad;
+
+  if (aitop)
+    freeaddrinfo(aitop);
+  exit(0);
+
+ bad:
+  if (aitop)
+    freeaddrinfo(aitop);
+  exit(1);
+}
+],
+AC_MSG_RESULT(good)
+buggygetaddrinfo=no,
+AC_MSG_RESULT(buggy)
+buggygetaddrinfo=yes,
+AC_MSG_RESULT(buggy)
+buggygetaddrinfo=yes)
+
+if test "$buggygetaddrinfo" = "yes"; then
+       if test "$ipv6" = "yes" -a "$ipv6type" != "linux"; then
+               echo 'Fatal: You must get working getaddrinfo() function.'
+               echo '       or you can specify "--disable-ipv6"'.
+               exit 1
+       elif test "$ipv6type" = "linux"; then
+               echo 'Warning: getaddrinfo() implementation on your system seems be buggy.'
+               echo '         Better upgreade your system library to newest version'
+               echo '         of GNU C library (aka glibc).'
+       fi
+fi
+AC_REPLACE_FUNCS(getaddrinfo getnameinfo)
+
+AC_MSG_CHECKING([whether struct sockaddr has sa_len])
+AC_TRY_COMPILE([
+#include <sys/types.h>
+#include <sys/socket.h>
+],[
+struct sockaddr s;
+int i = s.sa_len;
+],
+[      AC_MSG_RESULT(yes)
+       AC_DEFINE(HAVE_SOCKADDR_LEN)],
+[      AC_MSG_RESULT(no),
+       ],
+[      AC_MSG_RESULT(unknown),
+       ])
+
+
 AC_C_BIGENDIAN
 AC_HEADER_DIRENT
 AC_HEADER_TIME
index 7c33efa..0316be7 100644 (file)
--- a/options.c
+++ b/options.c
@@ -74,6 +74,9 @@ int modify_window=0;
 #endif
 int blocking_io=0;
 
+/** Network address family. **/
+int af = AF_INET;
+
 int read_batch=0;  /* dw */
 int write_batch=0; /* dw */
 
@@ -97,8 +100,10 @@ char *batch_ext = NULL;
 
 static int modify_window_set;
 
-
-struct in_addr socket_address = {INADDR_ANY};
+/** Local address to bind.  As a character string because it's
+ * interpreted by the IPv6 layer: should be a numeric IP4 or ip6
+ * address, or a hostname. **/
+char *bind_address;
 
 
 static void print_rsync_version(int f)
@@ -215,6 +220,10 @@ void usage(enum logcode F)
   rprintf(F," -f  --read-batch=EXT        read batch file\n");
   rprintf(F," -F  --write-batch           write batch file\n");
   rprintf(F," -h, --help                  show this help screen\n");
+#ifdef INET6
+  rprintf(F," -4                          prefer IPv4\n");
+  rprintf(F," -6                          prefer IPv6\n");
+#endif
 
   rprintf(F,"\n");
 
@@ -295,14 +304,19 @@ static struct poptOption long_options[] = {
   {"port",             0,  POPT_ARG_INT,    &rsync_port},
   {"log-format",       0,  POPT_ARG_STRING, &log_format},
   {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit},
-  {"address",          0,  POPT_ARG_STRING, 0,               OPT_ADDRESS},
+  {"address",          0,  POPT_ARG_STRING, &bind_address, 0},
   {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir},
   {"hard-links",      'H', POPT_ARG_NONE,   &preserve_hard_links},
   {"read-batch",      'f', POPT_ARG_STRING, &batch_ext, 'f'},
   {"write-batch",     'F', POPT_ARG_NONE,   &write_batch, 0},
+#ifdef INET6
+  {0,                '4', POPT_ARG_VAL,    &af,         AF_INET },
+  {0,                '6', POPT_ARG_VAL,    &af,         AF_INET6 },
+#endif
   {0,0,0,0}
 };
 
+
 static char err_buf[100];
 
 
@@ -473,14 +487,6 @@ int parse_arguments(int *argc, const char ***argv, int frommain)
                        keep_partial = 1;
                        break;
 
-               case OPT_ADDRESS:
-                       {
-                               struct in_addr *ia;
-                               if ((ia = ip_address (poptGetOptArg (pc)))) {
-                                       socket_address = *ia;
-                               }
-                       }
-                       break;
 
                case 'f':
                        /* The filename is stored for us by popt */
index 5e03760..4d1d421 100644 (file)
--- a/socket.c
+++ b/socket.c
 
 #include "rsync.h"
 
+#ifndef HAVE_GETADDRINFO
+#include "lib/addrinfo.h"
+#endif
+
+extern int af;
 
 /* Establish a proxy connection on an open socket to a web roxy by
  * using the CONNECT method. */
@@ -91,20 +96,22 @@ static int establish_proxy_connection(int fd, char *host, int port)
 
 
 
-/* open a socket to a tcp remote host with the specified port 
-   based on code from Warren
-   proxy support by Stephen Rothwell */
-static int open_socket_out (char *host,
-                           int port,
-                           struct in_addr *address)
+/** Open a socket to a tcp remote host with the specified port .
+ *
+ * Based on code from Warren.   Proxy support by Stephen Rothwell
+ *
+ *
+ * @param bind_address Local address to use.  Normally NULL to get the stack default.
+ **/
+int open_socket_out(char *host, int port, const char *bind_address)
 {
        int type = SOCK_STREAM;
-       struct sockaddr_in sock_out;
-       struct sockaddr_in sock;
-       int res;
-       struct hostent *hp;
+       int error;
+       int s;
+       int result;
+       struct addrinfo hints, *res0, *res;
+       char portbuf[10];
        char *h;
-       unsigned p;
        int proxied = 0;
        char buffer[1024];
        char *cp;
@@ -124,49 +131,67 @@ static int open_socket_out (char *host,
                        return -1;
                }
                *cp++ = '\0';
-               p = atoi(cp);
+               strcpy(portbuf, cp);
                h = buffer;
        } else {
+               snprintf(portbuf, sizeof(portbuf), "%d", port);
                h = host;
-               p = port;
-       }
-
-       res = socket(PF_INET, type, 0);
-       if (res == -1) {
-               return -1;
        }
 
-       hp = gethostbyname(h);
-       if (!hp) {
-               rprintf(FERROR,"unknown host: \"%s\"\n", h);
-               close(res);
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = af;
+       hints.ai_socktype = type;
+       error = getaddrinfo(h, portbuf, &hints, &res0);
+       if (error) {
+               rprintf(FERROR, "getaddrinfo: %s\n", gai_strerror(error));
                return -1;
        }
 
-       memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
-       sock_out.sin_port = htons(p);
-       sock_out.sin_family = PF_INET;
+       s = -1;
+       for (res = res0; res; res = res->ai_next) {
+               s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+               if (s < 0)
+                       continue;
 
-       if (address) {
-               sock.sin_addr = *address;
-               sock.sin_port = 0;
-               sock.sin_family = hp->h_addrtype;
-               bind(res, (struct sockaddr * ) &sock,sizeof(sock));
-       }
+               if (bind_address) {
+                       struct addrinfo bhints, *bres;
+
+                       memset(&bhints, 0, sizeof(bhints));
+                       bhints.ai_family = res->ai_family;
+                       bhints.ai_socktype = type;
+                       bhints.ai_flags = AI_PASSIVE;
+                       error = getaddrinfo(bind_address, NULL, &bhints, &bres);
+                       if (error) {
+                               rprintf(FERROR, "getaddrinfo: %s\n", gai_strerror(error));
+                               continue;
+                       }
+                       if (bres->ai_next) {
+                               rprintf(FERROR, "getaddrinfo: resolved to multiple hosts\n");
+                               freeaddrinfo(bres);
+                               continue;
+                       }
+                       bind(s, bres->ai_addr, bres->ai_addrlen);
+               }
 
-       if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
-               rprintf (FERROR, RSYNC_NAME ": failed to connect to host %s: %s\n",
-                        h, strerror(errno));
-               close(res);
-               return -1;
+               if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
+                       close(s);
+                       s = -1;
+                       continue;
+               }
+               if (proxied &&
+                   establish_proxy_connection(s, host, port) != 0) {
+                       close(s);
+                       s = -1;
+                       continue;
+               } else
+                       break;
        }
-
-       if (proxied && establish_proxy_connection(res, host, port) != 0) {
-               close(res);
+       freeaddrinfo(res0);
+       if (s < 0) {
+               rprintf(FERROR, "failed to connect to %s - %s\n", h, strerror(errno));
                return -1;
        }
-
-       return res;
+       return s;
 }
 
 
@@ -179,60 +204,71 @@ static int open_socket_out (char *host,
  * cause security problems by really opening remote connections.
  *
  * This is based on the Samba LIBSMB_PROG feature.
+ *
+ * @param bind_address Local address to use.  Normally NULL to get the stack default.
  **/
 int open_socket_out_wrapped (char *host,
                             int port,
-                            struct in_addr *address)
+                            const char *bind_address)
 {
        char *prog;
 
        if ((prog = getenv ("RSYNC_CONNECT_PROG")) != NULL) 
                return sock_exec (prog);
        else 
-               return open_socket_out (host, port, address);
+               return open_socket_out (host, port, bind_address);
 }
 
 
 
-/****************************************************************************
-open a socket of the specified type, port and address for incoming data
-****************************************************************************/
-static int open_socket_in(int type, int port, struct in_addr *address)
+/**
+ * Open a socket of the specified type, port and address for incoming data
+ *
+ * @param bind_address Local address to bind, or NULL to allow it to
+ * default.
+ **/
+static int open_socket_in(int type, int port, const char *bind_address)
 {
-       struct sockaddr_in sock;
-       int res;
        int one=1;
-
-       memset((char *)&sock,0,sizeof(sock));
-       sock.sin_port = htons(port);
-       sock.sin_family = AF_INET;
-       if (address) {
-               sock.sin_addr = *address;
-       } else {
-               sock.sin_addr.s_addr = INADDR_ANY;
+       int s;
+       struct addrinfo hints, *res;
+       char portbuf[10];
+       int error;
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = af;
+       hints.ai_socktype = type;
+       hints.ai_flags = AI_PASSIVE;
+       snprintf(portbuf, sizeof(portbuf), "%d", port);
+       error = getaddrinfo(bind_address, portbuf, &hints, &res);
+       if (error) {
+               rprintf(FERROR, "getaddrinfo: %s\n", gai_strerror(error));
+               return -1;
        }
-       res = socket(AF_INET, type, 0);
-       if (res == -1) { 
-               rprintf(FERROR, RSYNC_NAME ": socket failed: %s\n",
-                       strerror(errno)); 
+       if (res->ai_next) {
+               rprintf(FERROR, "getaddrinfo: resolved to multiple hosts\n");
+               freeaddrinfo(res);
+               return -1;
+       }
+
+       s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+       if (s < 0) {
+               rprintf(FERROR, RSYNC_NAME ": socket failed\n"); 
+               freeaddrinfo(res);
                return -1; 
        }
 
-       setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
+       setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
 
        /* now we've got a socket - we need to bind it */
-       if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) == -1) { 
-               rprintf(FERROR,"bind failed on port %d: %s\n", port,
-                       strerror(errno));
-               if (errno == EACCES && port < 1024) {
-                       rprintf(FERROR, "Note: you must be root to bind "
-                               "to low-numbered ports");
-               }
-               close(res); 
+       if (bind(s, res->ai_addr, res->ai_addrlen) < 0) { 
+               rprintf(FERROR, RSYNC_NAME ": bind failed on port %d\n", port);
+               freeaddrinfo(res);
+               close(s); 
                return -1;
        }
 
-       return res;
+       return s;
 }
 
 
@@ -265,10 +301,10 @@ int is_a_socket(int fd)
 void start_accept_loop(int port, int (*fn)(int ))
 {
        int s;
-       extern struct in_addr socket_address;
+       extern char *bind_address;
 
        /* open an incoming socket */
-       s = open_socket_in(SOCK_STREAM, port, &socket_address);
+       s = open_socket_in(SOCK_STREAM, port, bind_address);
        if (s == -1)
                exit_cleanup(RERR_SOCKETIO);
 
@@ -284,8 +320,8 @@ void start_accept_loop(int port, int (*fn)(int ))
        while (1) {
                fd_set fds;
                int fd;
-               struct sockaddr addr;
-               socklen_t in_addrlen = sizeof(addr);
+               struct sockaddr_storage addr;
+               int in_addrlen = sizeof(addr);
 
                /* close log file before the potentially very long select so
                   file can be trimmed by another process instead of growing
@@ -301,8 +337,7 @@ void start_accept_loop(int port, int (*fn)(int ))
 
                if(!FD_ISSET(s, &fds)) continue;
 
-                /* See note above prototypes. */
-               fd = accept(s,&addr, &in_addrlen);
+               fd = accept(s,(struct sockaddr *)&addr,&in_addrlen);
 
                if (fd == -1) continue;
 
@@ -317,11 +352,9 @@ void start_accept_loop(int port, int (*fn)(int ))
 
                if (fork()==0) {
                        close(s);
-
                        /* open log file in child before possibly giving
                           up privileges  */
                        log_open();
-
                        _exit(fn(fd));
                }
 
@@ -470,9 +503,8 @@ void become_daemon(void)
  ******************************************************************/
 char *client_addr(int fd)
 {
-       struct sockaddr sa;
-       struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
-       socklen_t length = sizeof(sa);
+       struct sockaddr_storage ss;
+       int     length = sizeof(ss);
        static char addr_buf[100];
        static int initialised;
 
@@ -480,11 +512,12 @@ char *client_addr(int fd)
 
        initialised = 1;
 
-       if (getpeername(fd, &sa, &length)) {
+       if (getpeername(fd, (struct sockaddr *)&ss, &length)) {
                exit_cleanup(RERR_SOCKETIO);
        }
-       
-       strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
+
+       getnameinfo((struct sockaddr *)&ss, length,
+               addr_buf, sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
        return addr_buf;
 }
 
@@ -494,14 +527,14 @@ char *client_addr(int fd)
  ******************************************************************/
 char *client_name(int fd)
 {
-       struct sockaddr sa;
-       struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
-       socklen_t length = sizeof(sa);
+       struct sockaddr_storage ss;
+       int     length = sizeof(ss);
        static char name_buf[100];
-       struct hostent *hp;
-       char **p;
+       static char port_buf[100];
        char *def = "UNKNOWN";
        static int initialised;
+       struct addrinfo hints, *res, *res0;
+       int error;
 
        if (initialised) return name_buf;
 
@@ -509,36 +542,69 @@ char *client_name(int fd)
 
        strcpy(name_buf,def);
 
-       if (getpeername(fd, &sa, &length)) {
+       if (getpeername(fd, (struct sockaddr *)&ss, &length)) {
                exit_cleanup(RERR_SOCKETIO);
        }
 
-       /* Look up the remote host name. */
-       if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
-                               sizeof(sockin->sin_addr),
-                               AF_INET))) {
-               strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
+#ifdef INET6
+        if (ss.ss_family == AF_INET6 && 
+           IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
+               struct sockaddr_in6 sin6;
+               struct sockaddr_in *sin;
+
+               memcpy(&sin6, &ss, sizeof(sin6));
+               sin = (struct sockaddr_in *)&ss;
+               memset(sin, 0, sizeof(*sin));
+               sin->sin_family = AF_INET;
+               length = sizeof(struct sockaddr_in);
+#ifdef HAVE_SOCKADDR_LEN
+               sin->sin_len = length;
+#endif
+               sin->sin_port = sin6.sin6_port;
+               memcpy(&sin->sin_addr, &sin6.sin6_addr.s6_addr[12],
+                       sizeof(sin->sin_addr));
+        }
+#endif
+
+       /* reverse lookup */
+       if (getnameinfo((struct sockaddr *)&ss, length,
+                       name_buf, sizeof(name_buf), port_buf, sizeof(port_buf),
+                       NI_NAMEREQD | NI_NUMERICSERV) != 0) {
+               strcpy(name_buf, def);
+               rprintf(FERROR, "reverse name lookup failed\n");
+       }
+
+       /* forward lookup */
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = PF_UNSPEC;
+       hints.ai_flags = AI_CANONNAME;
+       hints.ai_socktype = SOCK_STREAM;
+       error = getaddrinfo(name_buf, port_buf, &hints, &res0);
+       if (error) {
+               strcpy(name_buf, def);
+               rprintf(FERROR, "forward name lookup failed\n");
+               return name_buf;
+       }
+
+       /* XXX sin6_flowinfo and other fields */
+       for (res = res0; res; res = res->ai_next) {
+               if (res->ai_family != ss.ss_family)
+                       continue;
+               if (res->ai_addrlen != length)
+                       continue;
+               if (memcmp(res->ai_addr, &ss, res->ai_addrlen) == 0)
+                       break;
        }
 
+       /* TODO: Do a forward lookup as well to prevent spoofing */
 
-       /* do a forward lookup as well to prevent spoofing */
-       hp = gethostbyname(name_buf);
-       if (!hp) {
-               strcpy (name_buf,def);
-               rprintf (FERROR, "reverse name lookup for \"%s\" failed\n",
-                       name_buf);
-       } else {
-               for (p=hp->h_addr_list;*p;p++) {
-                       if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
-                               break;
-                       }
-               }
-               if (!*p) {
-                       strcpy(name_buf,def);
-                       rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n");
-               } 
+       if (res == NULL) {
+               strcpy(name_buf, def);
+               rprintf(FERROR,
+                       "reverse name lookup mismatch - spoofed address?\n");
        }
 
+       freeaddrinfo(res0);
        return name_buf;
 }