From 91d0cb58387e1e52b7023bcd0b01c1337c0fb9c8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 17 Sep 2011 14:56:11 +0200 Subject: [PATCH] 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 , test whether inet_ntop is declared in . 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 | 16 +++++++++ doc/posix-functions/inet_ntop.texi | 4 +++ lib/arpa_inet.in.h | 18 ++++++++-- lib/inet_ntop.c | 39 +++++++++++++++------- m4/arpa_inet_h.m4 | 3 +- m4/inet_ntop.m4 | 68 ++++++++++++++++++++++++-------------- modules/arpa_inet | 1 + modules/inet_ntop | 9 ++--- 8 files changed, 114 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 97633beb1..5627ca9f9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2011-09-17 Bruno Haible + + 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 , test whether inet_ntop is declared in + . 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 test-fsync: yet another enhancement diff --git a/doc/posix-functions/inet_ntop.texi b/doc/posix-functions/inet_ntop.texi index 96a485888..8a32ea5bb 100644 --- a/doc/posix-functions/inet_ntop.texi +++ b/doc/posix-functions/inet_ntop.texi @@ -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{} instead of @code{} on some platforms: NonStop Kernel. +@item +This function is declared in @code{}, with a POSIX incompatible +declaration, on some platforms: +MSVC 9 on Windows >= Vista. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/arpa_inet.in.h b/lib/arpa_inet.in.h index b042d6de8..2ce6585d4 100644 --- a/lib/arpa_inet.in.h +++ b/lib/arpa_inet.in.h @@ -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. @@ -74,16 +73,31 @@ For more details, see the POSIX:2001 specification . */ +# 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 diff --git a/lib/inet_ntop.c b/lib/inet_ntop.c index 5602a29e7..6b801747b 100644 --- a/lib/inet_ntop.c +++ b/lib/inet_ntop.c @@ -38,12 +38,25 @@ /* Specification. */ #include -#include -#include -#include +#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 +# include +# include + +# 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 diff --git a/m4/arpa_inet_h.m4 b/m4/arpa_inet_h.m4 index 6dc0fdb58..b65b72d1a 100644 --- a/m4/arpa_inet_h.m4 +++ b/m4/arpa_inet_h.m4 @@ -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]) ]) diff --git a/m4/inet_ntop.m4 b/m4/inet_ntop.m4 index a9a901e9c..c7b3227d6 100644 --- a/m4/inet_ntop.m4 +++ b/m4/inet_ntop.m4 @@ -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 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 , 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 ]]) + 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 - #if HAVE_NETDB_H - # include - #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 + #if HAVE_NETDB_H + # include + #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. diff --git a/modules/arpa_inet b/modules/arpa_inet index 6653ed420..ac339e6aa 100644 --- a/modules/arpa_inet +++ b/modules/arpa_inet @@ -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)' \ diff --git a/modules/inet_ntop b/modules/inet_ntop index fd4c7f917..275aa3ced 100644 --- a/modules/inet_ntop +++ b/modules/inet_ntop @@ -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 -- 2.11.0