]> Savannah Git Hosting - gnulib.git/commitdiff
htonl: Fix link errors on Windows.
authorCollin Funk <collin.funk1@gmail.com>
Sun, 11 Aug 2024 20:53:48 +0000 (13:53 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sun, 11 Aug 2024 20:53:48 +0000 (13:53 -0700)
Reported by Bruno Haible in:
<https://lists.gnu.org/archive/html/bug-gnulib/2024-08/msg00066.html>.

* lib/arpa_inet.in.h (htons, htonl, ntohs, ntohl): Only define when not
declared by the system.
* m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Set default behavior for
htonl functions.
* m4/htonl.m4 (gl_FUNC_HTONL): Check if -lws2_32 is required. Only check
for declarations on POSIX systems.
* modules/arpa_inet (Makefile.am): Substitute new Makefile variables.
* modules/htonl (Files): Add m4/sys_socket_h.m4.
(Depends-on): Remove endian condition.
(Link): Add $(HOTNL_LIB).
* modules/htonl-tests: Link to @HTONL_LIB@.

ChangeLog
lib/arpa_inet.in.h
m4/arpa_inet_h.m4
m4/htonl.m4
modules/arpa_inet
modules/htonl
modules/htonl-tests

index 3133b317d2780cd79e6a378561d0cdb0afff88c6..4cc404508a89ec4ccd73b2966b0299dcb6c97300 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2024-08-11  Collin Funk  <collin.funk1@gmail.com>
+
+       htonl: Fix link errors on Windows.
+       Reported by Bruno Haible in:
+       <https://lists.gnu.org/archive/html/bug-gnulib/2024-08/msg00066.html>.
+       * lib/arpa_inet.in.h (htons, htonl, ntohs, ntohl): Only define when not
+       declared by the system.
+       * m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Set default behavior for
+       htonl functions.
+       * m4/htonl.m4 (gl_FUNC_HTONL): Check if -lws2_32 is required. Only check
+       for declarations on POSIX systems.
+       * modules/arpa_inet (Makefile.am): Substitute new Makefile variables.
+       * modules/htonl (Files): Add m4/sys_socket_h.m4.
+       (Depends-on): Remove endian condition.
+       (Link): Add $(HOTNL_LIB).
+       * modules/htonl-tests: Link to @HTONL_LIB@.
+
 2024-08-11  Bruno Haible  <bruno@clisp.org>
 
        doc: Update regarding Windows <winsock2.h> functions.
index faaf958443d22375de1c2c45523afcdcec7f0b32..16813d412ae3d7b18c954e412d8187ffb32c1d94 100644 (file)
@@ -60,7 +60,7 @@
 # include <ws2tcpip.h>
 #endif
 
-#if @REPLACE_HTONL@
+#if !(@HAVE_DECL_HTONL@ || @HAVE_DECL_HTONS@ || @HAVE_DECL_NTOHL@ || @HAVE_DECL_NTOHS@)
 # include <endian.h>
 #endif
 
@@ -70,50 +70,43 @@ _GL_INLINE_HEADER_BEGIN
 #endif
 
 
-#if @REPLACE_HTONL@
-
-/* Make sure we don't have any system definitions.  */
-# undef htons
-# undef htonl
-# undef ntohs
-# undef ntohl
-
-/* Define our own.  */
-# define htons rpl_htons
-# define htonl rpl_htonl
-# define ntohs rpl_ntohs
-# define ntohl rpl_ntohl
-
 /* Host to network byte order. */
 
+#if !@HAVE_DECL_HTONS@
 _GL_ARPA_INET_INLINE uint16_t
 htons (uint16_t value)
 {
   return htobe16 (value);
 }
+#endif
 
+#if !@HAVE_DECL_HTONL@
 _GL_ARPA_INET_INLINE uint32_t
 htonl (uint32_t value)
 {
   return htobe32 (value);
 }
+#endif
 
 /* Network to host byte order.  */
 
+#if !@HAVE_DECL_NTOHS@
 _GL_ARPA_INET_INLINE uint16_t
 ntohs (uint16_t value)
 {
   return htobe16 (value);
 }
+#endif
 
+#if !@HAVE_DECL_NTOHL@
 _GL_ARPA_INET_INLINE uint32_t
 ntohl (uint32_t value)
 {
   return htobe32 (value);
 }
-
 #endif
 
+
 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
 
 /* The definition of _GL_ARG_NONNULL is copied here.  */
index 4a2538eaf5914a991c2c9e0b7c1f45304fce3a05..dc0d59196ac904ee37d2dba386236705f1da2b05 100644 (file)
@@ -1,5 +1,5 @@
 # arpa_inet_h.m4
-# serial 17
+# serial 18
 dnl Copyright (C) 2006, 2008-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -68,9 +68,12 @@ AC_DEFUN([gl_ARPA_INET_H_REQUIRE_DEFAULTS],
 AC_DEFUN([gl_ARPA_INET_H_DEFAULTS],
 [
   dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_DECL_HTONL=1;      AC_SUBST([HAVE_DECL_HTONL])
+  HAVE_DECL_HTONS=1;      AC_SUBST([HAVE_DECL_HTONS])
   HAVE_DECL_INET_NTOP=1;  AC_SUBST([HAVE_DECL_INET_NTOP])
   HAVE_DECL_INET_PTON=1;  AC_SUBST([HAVE_DECL_INET_PTON])
-  REPLACE_HTONL=0;        AC_SUBST([REPLACE_HTONL])
+  HAVE_DECL_NTOHL=1;      AC_SUBST([HAVE_DECL_NTOHL])
+  HAVE_DECL_NTOHS=1;      AC_SUBST([HAVE_DECL_NTOHS])
   REPLACE_INET_NTOP=0;    AC_SUBST([REPLACE_INET_NTOP])
   REPLACE_INET_PTON=0;    AC_SUBST([REPLACE_INET_PTON])
 ])
index aee4f272c72b62041e8b443031116d93cbad64f6..9e98523e2ddb6c8494bce5a306189a47bbffbe24 100644 (file)
@@ -1,5 +1,5 @@
 # htonl.m4
-# serial 1
+# serial 2
 dnl Copyright (C) 2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -11,32 +11,15 @@ AC_DEFUN([gl_FUNC_HTONL],
 [
   AC_REQUIRE([gl_ARPA_INET_H_DEFAULTS])
 
-  if test $ac_cv_header_arpa_inet_h = yes; then
-    AC_CACHE_CHECK([if arpa/inet.h defines htonl, htons, ntohl, ntohs],
-      [gl_cv_func_htonl_working],
-      [gl_cv_func_htonl_working=no
-       AC_COMPILE_IFELSE(
-         [AC_LANG_PROGRAM(
-[[
-#include <arpa/inet.h>
-]],
-[[
-/* Host to network.  */
-int network_1 = htons (0.0);
-int network_2 = htonl (0.0);
-
-/* Network to host.  */
-int host_1 = ntohs (0.0);
-int host_2 = ntohl (0.0);
-
-/* Make sure the variables get used.  */
-return !(network_1 + network_2 + host_1 + host_2);
-]])],
-         [gl_cv_func_htonl_working=yes],
-         [gl_cv_func_htonl_working=no])
-      ])
-    if test $gl_cv_func_htonl_working = no; then
-      REPLACE_HTONL=1
-    fi
+  dnl On Windows htonl and friends require -lws2_32 and inclusion of
+  dnl winsock2.h.
+  HTONL_LIB=
+  gl_PREREQ_SYS_H_WINSOCK2
+  if test $HAVE_WINSOCK2_H = 1; then
+    HTONL_LIB="-lws2_32"
+  elif test $ac_cv_header_arpa_inet_h = yes; then
+    AC_CHECK_DECLS([htons, htonl, ntohs, ntohl],,, [[#include <arpa/inet.h>]])
   fi
+
+  AC_SUBST([HTONL_LIB])
 ])
index 3134c706516e206b8ac3c246c9a889895c9d2ad0..b18fe8ff987af2a3f1e416dfa0730536c4a832c1 100644 (file)
@@ -41,9 +41,12 @@ arpa/inet.h: arpa_inet.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON
              -e 's/@''GNULIB_INET_NTOP''@/$(GNULIB_INET_NTOP)/g' \
              -e 's/@''GNULIB_INET_PTON''@/$(GNULIB_INET_PTON)/g' \
              -e 's|@''HAVE_WS2TCPIP_H''@|$(HAVE_WS2TCPIP_H)|g' \
+             -e 's|@''HAVE_DECL_HTONL''@|$(HAVE_DECL_HTONL)|g' \
+             -e 's|@''HAVE_DECL_HTONS''@|$(HAVE_DECL_HTONS)|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_HTONL''@|$(REPLACE_HTONL)|g' \
+             -e 's|@''HAVE_DECL_NTOHL''@|$(HAVE_DECL_NTOHL)|g' \
+             -e 's|@''HAVE_DECL_NTOHS''@|$(HAVE_DECL_NTOHS)|g' \
              -e 's|@''REPLACE_INET_NTOP''@|$(REPLACE_INET_NTOP)|g' \
              -e 's|@''REPLACE_INET_PTON''@|$(REPLACE_INET_PTON)|g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
index cb393e094b63e43dde750c90dd98762dbf72db9d..29f95501c4788a8230c32caf9ac7409c63aaded2 100644 (file)
@@ -4,10 +4,11 @@ byte order
 
 Files:
 m4/htonl.m4
+m4/sys_socket_h.m4
 
 Depends-on:
 arpa_inet
-endian                  [test $REPLACE_HTONL = 1]
+endian
 
 configure.ac:
 gl_FUNC_HTONL
@@ -18,6 +19,7 @@ Include:
 <arpa/inet.h>
 
 Link:
+$(HTONL_LIB)
 
 License:
 LGPLv2+
index c745b4883832401df86924708fca72955259ffd4..36097a9561f84383bb9035edc2a0325d6f653a84 100644 (file)
@@ -11,3 +11,4 @@ configure.ac:
 Makefile.am:
 TESTS += test-htonl
 check_PROGRAMS += test-htonl
+test_htonl_LDADD = $(LDADD) @HTONL_LIB@