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