doc: Remove references to POSIX 202x.
[gnulib.git] / m4 / sys_socket_h.m4
blob3bf3cb47778c5651f9c42760444467c1d006094a
1 # sys_socket_h.m4
2 # serial 29
3 dnl Copyright (C) 2005-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl From Simon Josefsson.
10 AC_DEFUN_ONCE([gl_SYS_SOCKET_H],
12   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
13   AC_REQUIRE([AC_CANONICAL_HOST])
15   dnl On OSF/1, the functions recv(), send(), recvfrom(), sendto() have
16   dnl old-style declarations (with return type 'int' instead of 'ssize_t')
17   dnl unless _POSIX_PII_SOCKET is defined.
18   case "$host_os" in
19     osf*)
20       AC_DEFINE([_POSIX_PII_SOCKET], [1],
21         [Define to 1 in order to get the POSIX compatible declarations
22          of socket functions.])
23       ;;
24   esac
26   GL_GENERATE_SYS_SOCKET_H=false
27   AC_CACHE_CHECK([whether <sys/socket.h> is self-contained],
28     [gl_cv_header_sys_socket_h_selfcontained],
29     [
30       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]], [[]])],
31         [gl_cv_header_sys_socket_h_selfcontained=yes],
32         [gl_cv_header_sys_socket_h_selfcontained=no])
33     ])
34   if test $gl_cv_header_sys_socket_h_selfcontained = yes; then
35     dnl If the shutdown function exists, <sys/socket.h> should define
36     dnl SHUT_RD, SHUT_WR, SHUT_RDWR.
37     AC_CHECK_FUNCS([shutdown])
38     if test $ac_cv_func_shutdown = yes; then
39       AC_CACHE_CHECK([whether <sys/socket.h> defines the SHUT_* macros],
40         [gl_cv_header_sys_socket_h_shut],
41         [
42           AC_COMPILE_IFELSE(
43             [AC_LANG_PROGRAM([[#include <sys/socket.h>]],
44                [[int a[] = { SHUT_RD, SHUT_WR, SHUT_RDWR };]])],
45             [gl_cv_header_sys_socket_h_shut=yes],
46             [gl_cv_header_sys_socket_h_shut=no])
47         ])
48       if test $gl_cv_header_sys_socket_h_shut = no; then
49         GL_GENERATE_SYS_SOCKET_H=true
50       fi
51     fi
52   fi
53   # We need to check for ws2tcpip.h now.
54   gl_PREREQ_SYS_H_SOCKET
55   AC_CHECK_TYPES([struct sockaddr_storage, sa_family_t],,,[
56   /* sys/types.h is not needed according to POSIX, but the
57      sys/socket.h in i386-unknown-freebsd4.10 and
58      powerpc-apple-darwin5.5 required it. */
59 #include <sys/types.h>
60 #ifdef HAVE_SYS_SOCKET_H
61 #include <sys/socket.h>
62 #endif
63 #ifdef HAVE_WS2TCPIP_H
64 #include <ws2tcpip.h>
65 #endif
67   if test $ac_cv_type_struct_sockaddr_storage = no; then
68     HAVE_STRUCT_SOCKADDR_STORAGE=0
69   fi
70   if test $ac_cv_type_sa_family_t = no; then
71     HAVE_SA_FAMILY_T=0
72   fi
73   if test $ac_cv_type_struct_sockaddr_storage != no; then
74     AC_CHECK_MEMBERS([struct sockaddr_storage.ss_family],
75       [],
76       [HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY=0],
77       [#include <sys/types.h>
78        #ifdef HAVE_SYS_SOCKET_H
79        #include <sys/socket.h>
80        #endif
81        #ifdef HAVE_WS2TCPIP_H
82        #include <ws2tcpip.h>
83        #endif
84       ])
85   fi
86   if test $HAVE_STRUCT_SOCKADDR_STORAGE = 0 || test $HAVE_SA_FAMILY_T = 0 \
87      || test $HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY = 0; then
88     GL_GENERATE_SYS_SOCKET_H=true
89   fi
90   gl_PREREQ_SYS_H_WINSOCK2
92   dnl Check for declarations of anything we want to poison if the
93   dnl corresponding gnulib module is not in use.
94   gl_WARN_ON_USE_PREPARE([[
95 /* Some systems require prerequisite headers.  */
96 #include <sys/types.h>
97 #include <sys/socket.h>
98     ]], [socket connect accept bind getpeername getsockname getsockopt
99     listen recv send recvfrom sendto setsockopt shutdown accept4])
101   AC_REQUIRE([AC_C_RESTRICT])
104 AC_DEFUN([gl_PREREQ_SYS_H_SOCKET],
106   dnl Check prerequisites of the <sys/socket.h> replacement.
107   AC_REQUIRE([gl_CHECK_SOCKET_HEADERS])
108   gl_CHECK_NEXT_HEADERS([sys/socket.h])
109   if test $ac_cv_header_sys_socket_h = yes; then
110     HAVE_SYS_SOCKET_H=1
111   else
112     HAVE_SYS_SOCKET_H=0
113   fi
114   AC_SUBST([HAVE_SYS_SOCKET_H])
115   gl_PREREQ_SYS_H_WS2TCPIP
118 # Common prerequisites of the <sys/socket.h> replacement and of the
119 # <sys/select.h> replacement.
120 # Sets and substitutes HAVE_WINSOCK2_H.
121 AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2],
123   m4_ifdef([gl_UNISTD_H_DEFAULTS], [AC_REQUIRE([gl_UNISTD_H_DEFAULTS])])
124   m4_ifdef([gl_SYS_IOCTL_H_DEFAULTS], [AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS])])
125   AC_CHECK_HEADERS_ONCE([sys/socket.h])
126   if test $ac_cv_header_sys_socket_h != yes; then
127     dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
128     dnl the check for those headers unconditional; yet cygwin reports
129     dnl that the headers are present but cannot be compiled (since on
130     dnl cygwin, all socket information should come from sys/socket.h).
131     AC_CHECK_HEADERS([winsock2.h])
132   fi
133   if test "$ac_cv_header_winsock2_h" = yes; then
134     HAVE_WINSOCK2_H=1
135     UNISTD_H_HAVE_WINSOCK2_H=1
136     SYS_IOCTL_H_HAVE_WINSOCK2_H=1
137   else
138     HAVE_WINSOCK2_H=0
139   fi
140   AC_SUBST([HAVE_WINSOCK2_H])
143 # Common prerequisites of the <sys/socket.h> replacement and of the
144 # <arpa/inet.h> replacement.
145 # Sets and substitutes HAVE_WS2TCPIP_H.
146 AC_DEFUN([gl_PREREQ_SYS_H_WS2TCPIP],
148   AC_REQUIRE([gl_CHECK_SOCKET_HEADERS])
149   if test $ac_cv_header_sys_socket_h = yes; then
150     HAVE_WS2TCPIP_H=0
151   else
152     if test $ac_cv_header_ws2tcpip_h = yes; then
153       HAVE_WS2TCPIP_H=1
154     else
155       HAVE_WS2TCPIP_H=0
156     fi
157   fi
158   AC_SUBST([HAVE_WS2TCPIP_H])
161 # gl_SYS_SOCKET_MODULE_INDICATOR([modulename])
162 # sets the shell variable that indicates the presence of the given module
163 # to a C preprocessor expression that will evaluate to 1.
164 # This macro invocation must not occur in macros that are AC_REQUIREd.
165 AC_DEFUN([gl_SYS_SOCKET_MODULE_INDICATOR],
167   dnl Ensure to expand the default settings once only.
168   gl_SYS_SOCKET_H_REQUIRE_DEFAULTS
169   gl_MODULE_INDICATOR_SET_VARIABLE([$1])
170   dnl Define it also as a C macro, for the benefit of the unit tests.
171   gl_MODULE_INDICATOR_FOR_TESTS([$1])
174 # Initializes the default values for AC_SUBSTed shell variables.
175 # This macro must not be AC_REQUIREd.  It must only be invoked, and only
176 # outside of macros or in macros that are not AC_REQUIREd.
177 AC_DEFUN([gl_SYS_SOCKET_H_REQUIRE_DEFAULTS],
179   m4_defun(GL_MODULE_INDICATOR_PREFIX[_SYS_SOCKET_H_MODULE_INDICATOR_DEFAULTS], [
180     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SOCKET])
181     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_CONNECT])
182     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ACCEPT])
183     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_BIND])
184     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETPEERNAME])
185     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETSOCKNAME])
186     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETSOCKOPT])
187     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_LISTEN])
188     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RECV])
189     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SEND])
190     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RECVFROM])
191     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SENDTO])
192     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SETSOCKOPT])
193     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SHUTDOWN])
194     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ACCEPT4])
195   ])
196   m4_require(GL_MODULE_INDICATOR_PREFIX[_SYS_SOCKET_H_MODULE_INDICATOR_DEFAULTS])
197   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
200 AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS],
202   HAVE_STRUCT_SOCKADDR_STORAGE=1; AC_SUBST([HAVE_STRUCT_SOCKADDR_STORAGE])
203   HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY=1;
204                         AC_SUBST([HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY])
205   HAVE_SA_FAMILY_T=1;   AC_SUBST([HAVE_SA_FAMILY_T])
206   HAVE_ACCEPT4=1;       AC_SUBST([HAVE_ACCEPT4])