[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
inet_ntop, inet_pton on MSVC
From: |
Bruno Haible |
Subject: |
inet_ntop, inet_pton on MSVC |
Date: |
Sat, 17 Sep 2011 15:10:38 +0200 |
User-agent: |
KMail/1.13.6 (Linux/2.6.37.6-0.5-desktop; KDE/4.6.0; x86_64; ; ) |
On MSVC 9, when compiling for Windows Vista or newer (cf. _WIN32_WINNT),
<ws2tcpip.h> declares an inet_ntop function. But the declaration uses
__stdcall calling convention, which is incompatible with the normal C
function call convention:
$ cat foo.c
#include <ws2tcpip.h>
char const * (*ptr2) (int, void const *, char *, socklen_t) = inet_ntop;
PCSTR (*ptr1) (INT, PVOID, PSTR, size_t) = inet_ntop;
$ cl -nologo -W4 -c foo.c
foo.c
foo.c(2) : error C2440: 'Initialisierung': 'PCSTR (__stdcall
*)(INT,PVOID,PSTR,size_t)' kann nicht in 'const char *(__cdecl *)(int,const
void *,char *,socklen_t)' konvertiert werden
foo.c(3) : error C2440: 'Initialisierung': 'PCSTR (__stdcall
*)(INT,PVOID,PSTR,size_t)' kann nicht in 'PCSTR (__cdecl
*)(INT,PVOID,PSTR,size_t)' konvertiert werden
According to POSIX:2008 / System Interfaces / General Information /
Use and Implementation of Interfaces
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html>
section 2.1.1, point 4, POSIX functions must follow the normal C function
call convention.
So gnulib has to override this function. I'm applying this fix, and likewise
for inet_pton.
2011-09-17 Bruno Haible <address@hidden>
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.
--- doc/posix-functions/inet_ntop.texi.orig Sat Sep 17 14:56:03 2011
+++ doc/posix-functions/inet_ntop.texi Sat Sep 17 14:55:34 2011
@@ -15,6 +15,10 @@
This function is declared in @code{<netdb.h>} instead of @code{<arpa/inet.h>}
on some platforms:
NonStop Kernel.
address@hidden
+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:
--- lib/arpa_inet.in.h.orig Sat Sep 17 14:56:03 2011
+++ lib/arpa_inet.in.h Sat Sep 17 12:34:56 2011
@@ -58,7 +58,6 @@
#if @GNULIB_INET_NTOP@
-# if address@hidden@
/* 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
<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 address@hidden@
_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
--- lib/inet_ntop.c.orig Sat Sep 17 14:56:03 2011
+++ lib/inet_ntop.c Sat Sep 17 13:03:04 2011
@@ -38,12 +38,25 @@
/* 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 @@
{
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 @@
return strcpy (dst, tmp);
}
-#if HAVE_IPV6
+# if HAVE_IPV6
/* const char *
* inet_ntop6(src, dst, size)
@@ -231,4 +244,6 @@
return strcpy (dst, tmp);
}
+# endif
+
#endif
--- m4/arpa_inet_h.m4.orig Sat Sep 17 14:56:03 2011
+++ m4/arpa_inet_h.m4 Sat Sep 17 12:36:05 2011
@@ -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 @@
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])
])
--- m4/inet_ntop.m4.orig Sat Sep 17 14:56:03 2011
+++ m4/inet_ntop.m4 Sat Sep 17 14:51:53 2011
@@ -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 @@
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.
--- modules/arpa_inet.orig Sat Sep 17 14:56:03 2011
+++ modules/arpa_inet Sat Sep 17 12:37:07 2011
@@ -36,6 +36,7 @@
-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)' \
--- modules/inet_ntop.orig Sat Sep 17 14:56:03 2011
+++ modules/inet_ntop Sat Sep 17 12:51:49 2011
@@ -4,17 +4,18 @@
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
--
In memoriam Estella Agsteribbe <http://en.wikipedia.org/wiki/Estella_Agsteribbe>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- inet_ntop, inet_pton on MSVC,
Bruno Haible <=