inet_ntop: Support for MSVC on Windows Vista or newer.
authorBruno Haible <bruno@clisp.org>
Sat, 17 Sep 2011 12:56:11 +0000 (14:56 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 17 Sep 2011 12:56:11 +0000 (14:56 +0200)
* lib/arpa_inet.in.h (inet_ntop): Also consider REPLACE_INET_NTOP.
* lib/inet_ntop.c (rpl_inet_ntop): Use a simple wrapper if
HAVE_DECL_INET_NTOP is defined.
* m4/inet_ntop.m4 (gl_FUNC_INET_NTOP): Invoke gl_PREREQ_SYS_H_WINSOCK2.
On platforms with <winsock2.h>, test whether inet_ntop is declared in
<ws2tcpip.h>. If so, arrange to replace it.
* m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Initialize
REPLACE_INET_NTOP.
* modules/arpa_inet (Makefile.am): Substitute REPLACE_INET_NTOP.
* modules/inet_ntop (Files): Add m4/sys_socket_h.m4.
(Depends-on, configure.ac): Update condition.
* doc/posix-functions/inet_ntop.texi: Mention the MSVC problem.

ChangeLog
doc/posix-functions/inet_ntop.texi
lib/arpa_inet.in.h
lib/inet_ntop.c
m4/arpa_inet_h.m4
m4/inet_ntop.m4
modules/arpa_inet
modules/inet_ntop

index 97633be..5627ca9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2011-09-17  Bruno Haible  <bruno@clisp.org>
+
+       inet_ntop: Support for MSVC on Windows Vista or newer.
+       * lib/arpa_inet.in.h (inet_ntop): Also consider REPLACE_INET_NTOP.
+       * lib/inet_ntop.c (rpl_inet_ntop): Use a simple wrapper if
+       HAVE_DECL_INET_NTOP is defined.
+       * m4/inet_ntop.m4 (gl_FUNC_INET_NTOP): Invoke gl_PREREQ_SYS_H_WINSOCK2.
+       On platforms with <winsock2.h>, test whether inet_ntop is declared in
+       <ws2tcpip.h>. If so, arrange to replace it.
+       * m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Initialize
+       REPLACE_INET_NTOP.
+       * modules/arpa_inet (Makefile.am): Substitute REPLACE_INET_NTOP.
+       * modules/inet_ntop (Files): Add m4/sys_socket_h.m4.
+       (Depends-on, configure.ac): Update condition.
+       * doc/posix-functions/inet_ntop.texi: Mention the MSVC problem.
+
 2011-09-16  Eric Blake  <eblake@redhat.com>
 
        test-fsync: yet another enhancement
index 96a4858..8a32ea5 100644 (file)
@@ -15,6 +15,10 @@ HP-UX 11.00, OSF/1 4.0, Solaris 2.5.1, mingw, MSVC 9, Interix 3.5, BeOS.
 This function is declared in @code{<netdb.h>} instead of @code{<arpa/inet.h>}
 on some platforms:
 NonStop Kernel.
+@item
+This function is declared in @code{<ws2tcpip.h>}, with a POSIX incompatible
+declaration, on some platforms:
+MSVC 9 on Windows >= Vista.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index b042d6d..2ce6585 100644 (file)
@@ -58,7 +58,6 @@
 
 
 #if @GNULIB_INET_NTOP@
-# if !@HAVE_DECL_INET_NTOP@
 /* Converts an internet address from internal format to a printable,
    presentable format.
    AF is an internet address family, such as AF_INET or AF_INET6.
 
    For more details, see the POSIX:2001 specification
    <http://www.opengroup.org/susv3xsh/inet_ntop.html>.  */
+# if @REPLACE_INET_NTOP@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef inet_ntop
+#   define inet_ntop rpl_inet_ntop
+#  endif
+_GL_FUNCDECL_RPL (inet_ntop, const char *,
+                  (int af, const void *restrict src,
+                   char *restrict dst, socklen_t cnt)
+                  _GL_ARG_NONNULL ((2, 3)));
+_GL_CXXALIAS_RPL (inet_ntop, const char *,
+                  (int af, const void *restrict src,
+                   char *restrict dst, socklen_t cnt));
+# else
+#  if !@HAVE_DECL_INET_NTOP@
 _GL_FUNCDECL_SYS (inet_ntop, const char *,
                   (int af, const void *restrict src,
                    char *restrict dst, socklen_t cnt)
                   _GL_ARG_NONNULL ((2, 3)));
-# endif
+#  endif
 /* Need to cast, because on NonStop Kernel, the fourth parameter is
                                             size_t cnt.  */
 _GL_CXXALIAS_SYS_CAST (inet_ntop, const char *,
                        (int af, const void *restrict src,
                         char *restrict dst, socklen_t cnt));
+# endif
 _GL_CXXALIASWARN (inet_ntop);
 #elif defined GNULIB_POSIXCHECK
 # undef inet_ntop
index 5602a29..6b80174 100644 (file)
 /* Specification.  */
 #include <arpa/inet.h>
 
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
+#if HAVE_DECL_INET_NTOP
 
-#define NS_IN6ADDRSZ 16
-#define NS_INT16SZ 2
+# undef inet_ntop
+
+const char *
+rpl_inet_ntop (int af, const void *restrict src,
+               char *restrict dst, socklen_t cnt)
+{
+  return inet_ntop (af, src, dst, cnt);
+}
+
+#else
+
+# include <stdio.h>
+# include <string.h>
+# include <errno.h>
+
+# define NS_IN6ADDRSZ 16
+# define NS_INT16SZ 2
 
 /*
  * WARNING: Don't even consider trying to compile this on a system where
@@ -52,9 +65,9 @@
 typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
 
 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
-#if HAVE_IPV6
+# if HAVE_IPV6
 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
-#endif
+# endif
 
 
 /* char *
@@ -71,15 +84,15 @@ inet_ntop (int af, const void *restrict src,
 {
   switch (af)
     {
-#if HAVE_IPV4
+# if HAVE_IPV4
     case AF_INET:
       return (inet_ntop4 (src, dst, cnt));
-#endif
+# endif
 
-#if HAVE_IPV6
+# if HAVE_IPV6
     case AF_INET6:
       return (inet_ntop6 (src, dst, cnt));
-#endif
+# endif
 
     default:
       errno = EAFNOSUPPORT;
@@ -118,7 +131,7 @@ inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
   return strcpy (dst, tmp);
 }
 
-#if HAVE_IPV6
+# if HAVE_IPV6
 
 /* const char *
  * inet_ntop6(src, dst, size)
@@ -231,4 +244,6 @@ inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
   return strcpy (dst, tmp);
 }
 
+# endif
+
 #endif
index 6dc0fdb..b65b72d 100644 (file)
@@ -1,4 +1,4 @@
-# arpa_inet_h.m4 serial 12
+# arpa_inet_h.m4 serial 13
 dnl Copyright (C) 2006, 2008-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -52,4 +52,5 @@ AC_DEFUN([gl_ARPA_INET_H_DEFAULTS],
   dnl Assume proper GNU behavior unless another module says otherwise.
   HAVE_DECL_INET_NTOP=1;  AC_SUBST([HAVE_DECL_INET_NTOP])
   HAVE_DECL_INET_PTON=1;  AC_SUBST([HAVE_DECL_INET_PTON])
+  REPLACE_INET_NTOP=0;    AC_SUBST([REPLACE_INET_NTOP])
 ])
index a9a901e..c7b3227 100644 (file)
@@ -1,4 +1,4 @@
-# inet_ntop.m4 serial 16
+# inet_ntop.m4 serial 17
 dnl Copyright (C) 2005-2006, 2008-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,36 +9,54 @@ AC_DEFUN([gl_FUNC_INET_NTOP],
   dnl Persuade Solaris <arpa/inet.h> to declare inet_ntop.
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
 
+  AC_REQUIRE([AC_C_RESTRICT])
+
   dnl Most platforms that provide inet_ntop define it in libc.
   dnl Solaris 8..10 provide inet_ntop in libnsl instead.
+  dnl Native Windows provides it in -lws2_32 instead, with a declaration in
+  dnl <ws2tcpip.h>, and it uses stdcall calling convention, not cdecl
+  dnl (hence we cannot use AC_CHECK_FUNCS, AC_SEARCH_LIBS to find it).
   HAVE_INET_NTOP=1
-  gl_save_LIBS=$LIBS
-  AC_SEARCH_LIBS([inet_ntop], [nsl], [],
-    [AC_CHECK_FUNCS([inet_ntop])
-     if test $ac_cv_func_inet_ntop = no; then
-       HAVE_INET_NTOP=0
-     fi
-    ])
-  LIBS=$gl_save_LIBS
-
   INET_NTOP_LIB=
-  if test "$ac_cv_search_inet_ntop" != "no" &&
-     test "$ac_cv_search_inet_ntop" != "none required"; then
-    INET_NTOP_LIB="$ac_cv_search_inet_ntop"
-  fi
-  AC_SUBST([INET_NTOP_LIB])
+  gl_PREREQ_SYS_H_WINSOCK2
+  if test $HAVE_WINSOCK2_H = 1; then
+    AC_CHECK_DECLS([inet_ntop],,, [[#include <ws2tcpip.h>]])
+    if test $ac_cv_have_decl_inet_ntop = yes; then
+      dnl It needs to be overridden, because the stdcall calling convention
+      dnl is not compliant with POSIX.
+      REPLACE_INET_NTOP=1
+      INET_NTOP_LIB="-lws2_32"
+    else
+      HAVE_DECL_INET_NTOP=0
+      HAVE_INET_NTOP=0
+    fi
+  else
+    gl_save_LIBS=$LIBS
+    AC_SEARCH_LIBS([inet_ntop], [nsl], [],
+      [AC_CHECK_FUNCS([inet_ntop])
+       if test $ac_cv_func_inet_ntop = no; then
+         HAVE_INET_NTOP=0
+       fi
+      ])
+    LIBS=$gl_save_LIBS
+
+    if test "$ac_cv_search_inet_ntop" != "no" \
+       && test "$ac_cv_search_inet_ntop" != "none required"; then
+      INET_NTOP_LIB="$ac_cv_search_inet_ntop"
+    fi
 
-  AC_CHECK_HEADERS_ONCE([netdb.h])
-  AC_CHECK_DECLS([inet_ntop],,,
-    [[#include <arpa/inet.h>
-      #if HAVE_NETDB_H
-      # include <netdb.h>
-      #endif
-    ]])
-  if test $ac_cv_have_decl_inet_ntop = no; then
-    HAVE_DECL_INET_NTOP=0
-    AC_REQUIRE([AC_C_RESTRICT])
+    AC_CHECK_HEADERS_ONCE([netdb.h])
+    AC_CHECK_DECLS([inet_ntop],,,
+      [[#include <arpa/inet.h>
+        #if HAVE_NETDB_H
+        # include <netdb.h>
+        #endif
+      ]])
+    if test $ac_cv_have_decl_inet_ntop = no; then
+      HAVE_DECL_INET_NTOP=0
+    fi
   fi
+  AC_SUBST([INET_NTOP_LIB])
 ])
 
 # Prerequisites of lib/inet_ntop.c.
index 6653ed4..ac339e6 100644 (file)
@@ -36,6 +36,7 @@ arpa/inet.h: arpa_inet.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON
              -e 's/@''GNULIB_INET_PTON''@/$(GNULIB_INET_PTON)/g' \
              -e 's|@''HAVE_DECL_INET_NTOP''@|$(HAVE_DECL_INET_NTOP)|g' \
              -e 's|@''HAVE_DECL_INET_PTON''@|$(HAVE_DECL_INET_PTON)|g' \
+             -e 's|@''REPLACE_INET_NTOP''@|$(REPLACE_INET_NTOP)|g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
              -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
              -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
index fd4c7f9..275aa3c 100644 (file)
@@ -4,17 +4,18 @@ Convert internet address from internal to printable, presentable format.
 Files:
 lib/inet_ntop.c
 m4/inet_ntop.m4
+m4/sys_socket_h.m4
 
 Depends-on:
 arpa_inet
 extensions
-sys_socket      [test $HAVE_INET_NTOP = 0]
-errno           [test $HAVE_INET_NTOP = 0]
-netinet_in      [test $HAVE_INET_NTOP = 0]
+sys_socket      [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1]
+errno           [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1]
+netinet_in      [test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1]
 
 configure.ac:
 gl_FUNC_INET_NTOP
-if test $HAVE_INET_NTOP = 0; then
+if test $HAVE_INET_NTOP = 0 || test $REPLACE_INET_NTOP = 1; then
   AC_LIBOBJ([inet_ntop])
   gl_PREREQ_INET_NTOP
 fi