remove trailing spaces
[gnulib.git] / lib / sys_socket.in.h
1 /* Provide a sys/socket header file for systems lacking it (read: MinGW)
2    and for systems where it is incomplete.
3    Copyright (C) 2005-2008 Free Software Foundation, Inc.
4    Written by Simon Josefsson.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* This file is supposed to be used on platforms that lack <sys/socket.h>,
21    on platforms where <sys/socket.h> cannot be included standalone, and on
22    platforms where <sys/socket.h> does not provide all necessary definitions.
23    It is intended to provide definitions and prototypes needed by an
24    application.  */
25
26 #ifndef _GL_SYS_SOCKET_H
27
28 #if @HAVE_SYS_SOCKET_H@
29
30 @PRAGMA_SYSTEM_HEADER@
31
32 /* On many platforms, <sys/socket.h> assumes prior inclusion of
33    <sys/types.h>.  */
34 # include <sys/types.h>
35
36 /* The include_next requires a split double-inclusion guard.  */
37 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
38
39 #endif
40
41 #ifndef _GL_SYS_SOCKET_H
42 #define _GL_SYS_SOCKET_H
43
44 #if @HAVE_SYS_SOCKET_H@
45
46 /* A platform that has <sys/socket.h>.  */
47
48 /* For shutdown().  */
49 # if !defined SHUT_RD
50 #  define SHUT_RD 0
51 # endif
52 # if !defined SHUT_WR
53 #  define SHUT_WR 1
54 # endif
55 # if !defined SHUT_RDWR
56 #  define SHUT_RDWR 2
57 # endif
58
59 #else
60
61 # ifdef __CYGWIN__
62 #  error "Cygwin does have a sys/socket.h, doesn't it?!?"
63 # endif
64
65 /* A platform that lacks <sys/socket.h>.
66
67    Currently only MinGW is supported.  See the gnulib manual regarding
68    Windows sockets.  MinGW has the header files winsock2.h and
69    ws2tcpip.h that declare the sys/socket.h definitions we need.  Note
70    that you can influence which definitions you get by setting the
71    WINVER symbol before including these two files.  For example,
72    getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
73    symbol is set indiriectly through WINVER).  You can set this by
74    adding AC_DEFINE(WINVER, 0x0501) to configure.ac.  Note that your
75    code may not run on older Windows releases then.  My Windows 2000
76    box was not able to run the code, for example.  The situation is
77    slightly confusing because:
78    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
79    suggests that getaddrinfo should be available on all Windows
80    releases. */
81
82
83 # if @HAVE_WINSOCK2_H@
84 #  include <winsock2.h>
85 # endif
86 # if @HAVE_WS2TCPIP_H@
87 #  include <ws2tcpip.h>
88 # endif
89
90 /* For shutdown(). */
91 # if !defined SHUT_RD && defined SD_RECEIVE
92 #  define SHUT_RD SD_RECEIVE
93 # endif
94 # if !defined SHUT_WR && defined SD_SEND
95 #  define SHUT_WR SD_SEND
96 # endif
97 # if !defined SHUT_RDWR && defined SD_BOTH
98 #  define SHUT_RDWR SD_BOTH
99 # endif
100
101 # if @HAVE_WINSOCK2_H@
102 /* Include headers needed by the emulation code.  */
103 #  include <sys/types.h>
104 #  include <io.h>
105
106 typedef int socklen_t;
107
108 #  ifdef __cplusplus
109 extern "C" {
110 #  endif
111
112 /* Re-define FD_ISSET to avoid a WSA call while we are not using
113    network sockets.  */
114 static inline int
115 rpl_fd_isset (int fd, fd_set * set)
116 {
117   int i;
118   if (set == NULL)
119     return 0;
120
121   for (i = 0; i < set->fd_count; i++)
122     if (set->fd_array[i] == fd)
123       return 1;
124
125   return 0;
126 }
127
128 #  undef FD_ISSET
129 #  define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
130
131 /* Wrap everything else to use libc file descriptors for sockets.  */
132
133 #  undef close
134 #  define close                 rpl_close
135 #  undef socket
136 #  define socket                rpl_socket
137 #  undef connect
138 #  define connect               rpl_connect
139 #  undef accept
140 #  define accept                rpl_accept
141 #  undef bind
142 #  define bind                  rpl_bind
143 #  undef getpeername
144 #  define getpeername           rpl_getpeername
145 #  undef getsockname
146 #  define getsockname           rpl_getsockname
147 #  undef getsockopt
148 #  define getsockopt            rpl_getsockopt
149 #  undef listen
150 #  define listen                rpl_listen
151 #  undef ioctl
152 #  define ioctl                 rpl_ioctl
153 #  undef recv
154 #  define recv                  rpl_recv
155 #  undef send
156 #  define send                  rpl_send
157 #  undef recvfrom
158 #  define recvfrom              rpl_recvfrom
159 #  undef sendto
160 #  define sendto                rpl_sendto
161 #  undef setsockopt
162 #  define setsockopt            rpl_setsockopt
163 #  undef select
164 #  define select                select_used_without_including_sys_select_h
165
166 extern int rpl_close(int);
167 extern int rpl_socket (int, int, int protocol);
168 extern int rpl_connect (int, struct sockaddr *, int);
169 extern int rpl_accept (int, struct sockaddr *, int *);
170 extern int rpl_bind (int, struct sockaddr *, int);
171 extern int rpl_getpeername (int, struct sockaddr *, int *);
172 extern int rpl_getsockname (int, struct sockaddr *, int *);
173 extern int rpl_getsockopt (int, int, int, void *, int *);
174 extern int rpl_listen (int, int);
175 extern int rpl_ioctl (int, unsigned long, char *);
176 extern int rpl_recv (int, void *, int, int);
177 extern int rpl_send (int, const void *, int, int);
178 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
179 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
180 extern int rpl_setsockopt (int, int, int, const void *, int);
181
182 #  ifdef __cplusplus
183 }
184 #  endif
185
186 # endif /* HAVE_WINSOCK2_H */
187
188 #endif /* HAVE_SYS_SOCKET_H */
189
190 #endif /* _GL_SYS_SOCKET_H */
191 #endif /* _GL_SYS_SOCKET_H */