5bbaad8df95d34ae1660e04f1c2e40cdee8260c1
[gnulib.git] / m4 / sys_socket_h.m4
1 # sys_socket_h.m4 serial 12
2 dnl Copyright (C) 2005-2009 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([gl_HEADER_SYS_SOCKET],
10 [
11   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
12   AC_REQUIRE([AC_C_INLINE])
13
14   AC_CACHE_CHECK([whether <sys/socket.h> is self-contained],
15     [gl_cv_header_sys_socket_h_selfcontained],
16     [
17       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]], [[]])],
18         [gl_cv_header_sys_socket_h_selfcontained=yes],
19         [gl_cv_header_sys_socket_h_selfcontained=no])
20     ])
21   if test $gl_cv_header_sys_socket_h_selfcontained = yes; then
22     SYS_SOCKET_H=''
23     dnl If the shutdown function exists, <sys/socket.h> should define
24     dnl SHUT_RD, SHUT_WR, SHUT_RDWR.
25     AC_CHECK_FUNCS([shutdown])
26     if test $ac_cv_func_shutdown = yes; then
27       AC_CACHE_CHECK([whether <sys/socket.h> defines the SHUT_* macros],
28         [gl_cv_header_sys_socket_h_shut],
29         [
30           AC_COMPILE_IFELSE(
31             [AC_LANG_PROGRAM([[#include <sys/socket.h>]],
32                [[int a[] = { SHUT_RD, SHUT_WR, SHUT_RDWR };]])],
33             [gl_cv_header_sys_socket_h_shut=yes],
34             [gl_cv_header_sys_socket_h_shut=no])
35         ])
36       if test $gl_cv_header_sys_socket_h_shut = no; then
37         SYS_SOCKET_H='sys/socket.h'
38       fi
39     fi
40   else
41     SYS_SOCKET_H='sys/socket.h'
42   fi
43   AC_CHECK_TYPES([struct sockaddr_storage],,,[
44   /* sys/types.h is not needed according to POSIX, but the
45      sys/socket.h in i386-unknown-freebsd4.10 and
46      powerpc-apple-darwin5.5 required it. */
47 #include <sys/types.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
50 #endif
51 #ifdef HAVE_WS2TCPIP_H
52 #include <ws2tcpip.h>
53 #endif
54 ])
55   if test $ac_cv_type_struct_sockaddr_storage = no; then
56     HAVE_STRUCT_SOCKADDR_STORAGE=0
57     SYS_SOCKET_H='sys/socket.h'
58   fi
59   if test -n "$SYS_SOCKET_H"; then
60     dnl Check prerequisites of the <sys/socket.h> replacement.
61     gl_CHECK_NEXT_HEADERS([sys/socket.h])
62     if test $ac_cv_header_sys_socket_h = yes; then
63       HAVE_SYS_SOCKET_H=1
64       HAVE_WS2TCPIP_H=0
65     else
66       HAVE_SYS_SOCKET_H=0
67       dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
68       dnl the check for those headers unconditional; yet cygwin reports
69       dnl that the headers are present but cannot be compiled (since on
70       dnl cygwin, all socket information should come from sys/socket.h).
71       AC_CHECK_HEADERS([ws2tcpip.h])
72       if test $ac_cv_header_ws2tcpip_h = yes; then
73         HAVE_WS2TCPIP_H=1
74       else
75         HAVE_WS2TCPIP_H=0
76       fi
77     fi
78     gl_PREREQ_SYS_H_WINSOCK2
79     AC_SUBST([HAVE_SYS_SOCKET_H])
80     AC_SUBST([HAVE_WS2TCPIP_H])
81   fi
82   AC_SUBST([SYS_SOCKET_H])
83 ])
84
85 # Common prerequisites of of the <sys/socket.h> replacement and of the
86 # <sys/select.h> replacement.
87 # Sets and substitutes HAVE_WINSOCK2_H.
88 AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2],
89 [
90   m4_ifdef([gl_UNISTD_H_DEFAULTS], [AC_REQUIRE([gl_UNISTD_H_DEFAULTS])])
91   m4_ifdef([gl_SYS_IOCTL_H_DEFAULTS], [AC_REQUIRE([gl_SYS_IOCTL_H_DEFAULTS])])
92   AC_CHECK_HEADERS_ONCE([sys/socket.h])
93   if test $ac_cv_header_sys_socket_h != yes; then
94     dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
95     dnl the check for those headers unconditional; yet cygwin reports
96     dnl that the headers are present but cannot be compiled (since on
97     dnl cygwin, all socket information should come from sys/socket.h).
98     AC_CHECK_HEADERS([winsock2.h])
99   fi
100   if test "$ac_cv_header_winsock2_h" = yes; then
101     HAVE_WINSOCK2_H=1
102     UNISTD_H_HAVE_WINSOCK2_H=1
103     SYS_IOCTL_H_HAVE_WINSOCK2_H=1
104   else
105     HAVE_WINSOCK2_H=0
106   fi
107   AC_SUBST([HAVE_WINSOCK2_H])
108 ])
109
110 AC_DEFUN([gl_SYS_SOCKET_MODULE_INDICATOR],
111 [
112   dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
113   AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
114   GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
115 ])
116
117 AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS],
118 [
119   GNULIB_SOCKET=0;      AC_SUBST([GNULIB_SOCKET])
120   GNULIB_CONNECT=0;     AC_SUBST([GNULIB_CONNECT])
121   GNULIB_ACCEPT=0;      AC_SUBST([GNULIB_ACCEPT])
122   GNULIB_BIND=0;        AC_SUBST([GNULIB_BIND])
123   GNULIB_GETPEERNAME=0; AC_SUBST([GNULIB_GETPEERNAME])
124   GNULIB_GETSOCKNAME=0; AC_SUBST([GNULIB_GETSOCKNAME])
125   GNULIB_GETSOCKOPT=0;  AC_SUBST([GNULIB_GETSOCKOPT])
126   GNULIB_LISTEN=0;      AC_SUBST([GNULIB_LISTEN])
127   GNULIB_RECV=0;        AC_SUBST([GNULIB_RECV])
128   GNULIB_SEND=0;        AC_SUBST([GNULIB_SEND])
129   GNULIB_RECVFROM=0;    AC_SUBST([GNULIB_RECVFROM])
130   GNULIB_SENDTO=0;      AC_SUBST([GNULIB_SENDTO])
131   GNULIB_SETSOCKOPT=0;  AC_SUBST([GNULIB_SETSOCKOPT])
132   GNULIB_SHUTDOWN=0;    AC_SUBST([GNULIB_SHUTDOWN])
133   HAVE_STRUCT_SOCKADDR_STORAGE=1; AC_SUBST([HAVE_STRUCT_SOCKADDR_STORAGE])
134 ])