Combine the two replacements of 'close'.
[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 /* The definition of GL_LINK_WARNING is copied here.  */
102
103 # if @HAVE_WINSOCK2_H@
104 /* Include headers needed by the emulation code.  */
105 #  include <sys/types.h>
106 #  include <io.h>
107
108 typedef int socklen_t;
109
110 # endif
111
112 # ifdef __cplusplus
113 extern "C" {
114 # endif
115
116 # if @HAVE_WINSOCK2_H@
117
118 /* Re-define FD_ISSET to avoid a WSA call while we are not using
119    network sockets.  */
120 static inline int
121 rpl_fd_isset (int fd, fd_set * set)
122 {
123   int i;
124   if (set == NULL)
125     return 0;
126
127   for (i = 0; i < set->fd_count; i++)
128     if (set->fd_array[i] == fd)
129       return 1;
130
131   return 0;
132 }
133
134 #  undef FD_ISSET
135 #  define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
136
137 # endif
138
139 /* Wrap everything else to use libc file descriptors for sockets.  */
140
141 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
142 #  undef close
143 #  define close close_used_without_including_unistd_h
144 # endif
145
146 # if @GNULIB_SOCKET@
147 #  if @HAVE_WINSOCK2_H@
148 #   undef socket
149 #   define socket               rpl_socket
150 extern int rpl_socket (int, int, int protocol);
151 #  endif
152 # elif @HAVE_WINSOCK2_H@
153 #  undef socket
154 #  define socket socket_used_without_requesting_gnulib_module_socket
155 # elif defined GNULIB_POSIXCHECK
156 #  undef socket
157 #  define socket(d,t,p) \
158      (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
159                        "use gnulib module socket for portability"), \
160       socket (d, t, p))
161 # endif
162
163 # if @GNULIB_CONNECT@
164 #  if @HAVE_WINSOCK2_H@
165 #   undef connect
166 #   define connect              rpl_connect
167 extern int rpl_connect (int, struct sockaddr *, int);
168 #  endif
169 # elif @HAVE_WINSOCK2_H@
170 #  undef connect
171 #  define connect socket_used_without_requesting_gnulib_module_connect
172 # elif defined GNULIB_POSIXCHECK
173 #  undef connect
174 #  define connect(s,a,l) \
175      (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
176                        "use gnulib module connect for portability"), \
177       connect (s, a, l))
178 # endif
179
180 # if @GNULIB_ACCEPT@
181 #  if @HAVE_WINSOCK2_H@
182 #   undef accept
183 #   define accept               rpl_accept
184 extern int rpl_accept (int, struct sockaddr *, int *);
185 #  endif
186 # elif @HAVE_WINSOCK2_H@
187 #  undef accept
188 #  define accept accept_used_without_requesting_gnulib_module_accept
189 # elif defined GNULIB_POSIXCHECK
190 #  undef accept
191 #  define accept(s,a,l) \
192      (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
193                        "use gnulib module accept for portability"), \
194       accept (s, a, l))
195 # endif
196
197 # if @GNULIB_BIND@
198 #  if @HAVE_WINSOCK2_H@
199 #   undef bind
200 #   define bind                 rpl_bind
201 extern int rpl_bind (int, struct sockaddr *, int);
202 #  endif
203 # elif @HAVE_WINSOCK2_H@
204 #  undef bind
205 #  define bind bind_used_without_requesting_gnulib_module_bind
206 # elif defined GNULIB_POSIXCHECK
207 #  undef bind
208 #  define bind(s,a,l) \
209      (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
210                        "use gnulib module bind for portability"), \
211       bind (s, a, l))
212 # endif
213
214 # if @GNULIB_GETPEERNAME@
215 #  if @HAVE_WINSOCK2_H@
216 #   undef getpeername
217 #   define getpeername          rpl_getpeername
218 extern int rpl_getpeername (int, struct sockaddr *, int *);
219 #  endif
220 # elif @HAVE_WINSOCK2_H@
221 #  undef getpeername
222 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
223 # elif defined GNULIB_POSIXCHECK
224 #  undef getpeername
225 #  define getpeername(s,a,l) \
226      (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
227                        "use gnulib module getpeername for portability"), \
228       getpeername (s, a, l))
229 # endif
230
231 # if @GNULIB_GETSOCKNAME@
232 #  if @HAVE_WINSOCK2_H@
233 #   undef getsockname
234 #   define getsockname          rpl_getsockname
235 extern int rpl_getsockname (int, struct sockaddr *, int *);
236 #  endif
237 # elif @HAVE_WINSOCK2_H@
238 #  undef getsockname
239 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
240 # elif defined GNULIB_POSIXCHECK
241 #  undef getsockname
242 #  define getsockname(s,a,l) \
243      (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
244                        "use gnulib module getsockname for portability"), \
245       getsockname (s, a, l))
246 # endif
247
248 # if @GNULIB_GETSOCKOPT@
249 #  if @HAVE_WINSOCK2_H@
250 #   undef getsockopt
251 #   define getsockopt           rpl_getsockopt
252 extern int rpl_getsockopt (int, int, int, void *, int *);
253 #  endif
254 # elif @HAVE_WINSOCK2_H@
255 #  undef getsockopt
256 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
257 # elif defined GNULIB_POSIXCHECK
258 #  undef getsockopt
259 #  define getsockopt(s,lvl,o,v,l) \
260      (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
261                        "use gnulib module getsockopt for portability"), \
262       getsockopt (s, lvl, o, v, l))
263 # endif
264
265 # if @GNULIB_LISTEN@
266 #  if @HAVE_WINSOCK2_H@
267 #   undef listen
268 #   define listen               rpl_listen
269 extern int rpl_listen (int, int);
270 #  endif
271 # elif @HAVE_WINSOCK2_H@
272 #  undef listen
273 #  define listen listen_used_without_requesting_gnulib_module_listen
274 # elif defined GNULIB_POSIXCHECK
275 #  undef listen
276 #  define listen(s,b) \
277      (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
278                        "use gnulib module listen for portability"), \
279       listen (s, b))
280 # endif
281
282 # if @HAVE_WINSOCK2_H@
283 #  undef ioctl
284 #  define ioctl                 rpl_ioctl
285 extern int rpl_ioctl (int, unsigned long, char *);
286 # endif
287
288 # if @GNULIB_RECV@
289 #  if @HAVE_WINSOCK2_H@
290 #   undef recv
291 #   define recv                 rpl_recv
292 extern int rpl_recv (int, void *, int, int);
293 #  endif
294 # elif @HAVE_WINSOCK2_H@
295 #  undef recv
296 #  define recv recv_used_without_requesting_gnulib_module_recv
297 # elif defined GNULIB_POSIXCHECK
298 #  undef recv
299 #  define recv(s,b,n,f) \
300      (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
301                        "use gnulib module recv for portability"), \
302       recv (s, b, n, f))
303 # endif
304
305 # if @GNULIB_SEND@
306 #  if @HAVE_WINSOCK2_H@
307 #   undef send
308 #   define send                 rpl_send
309 extern int rpl_send (int, const void *, int, int);
310 #  endif
311 # elif @HAVE_WINSOCK2_H@
312 #  undef send
313 #  define send send_used_without_requesting_gnulib_module_send
314 # elif defined GNULIB_POSIXCHECK
315 #  undef send
316 #  define send(s,b,n,f) \
317      (GL_LINK_WARNING ("send is not always POSIX compliant - " \
318                        "use gnulib module send for portability"), \
319       send (s, b, n, f))
320 # endif
321
322 # if @GNULIB_RECVFROM@
323 #  if @HAVE_WINSOCK2_H@
324 #   undef recvfrom
325 #   define recvfrom             rpl_recvfrom
326 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
327 #  endif
328 # elif @HAVE_WINSOCK2_H@
329 #  undef recvfrom
330 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
331 # elif defined GNULIB_POSIXCHECK
332 #  undef recvfrom
333 #  define recvfrom(s,b,n,f,a,l) \
334      (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
335                        "use gnulib module recvfrom for portability"), \
336       recvfrom (s, b, n, f, a, l))
337 # endif
338
339 # if @GNULIB_SENDTO@
340 #  if @HAVE_WINSOCK2_H@
341 #   undef sendto
342 #   define sendto               rpl_sendto
343 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
344 #  endif
345 # elif @HAVE_WINSOCK2_H@
346 #  undef sendto
347 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
348 # elif defined GNULIB_POSIXCHECK
349 #  undef sendto
350 #  define sendto(s,b,n,f,a,l) \
351      (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
352                        "use gnulib module sendto for portability"), \
353       sendto (s, b, n, f, a, l))
354 # endif
355
356 # if @GNULIB_SETSOCKOPT@
357 #  if @HAVE_WINSOCK2_H@
358 #   undef setsockopt
359 #   define setsockopt           rpl_setsockopt
360 extern int rpl_setsockopt (int, int, int, const void *, int);
361 #  endif
362 # elif @HAVE_WINSOCK2_H@
363 #  undef setsockopt
364 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
365 # elif defined GNULIB_POSIXCHECK
366 #  undef setsockopt
367 #  define setsockopt(s,lvl,o,v,l) \
368      (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
369                        "use gnulib module setsockopt for portability"), \
370       setsockopt (s, lvl, o, v, l))
371 # endif
372
373 # if @HAVE_WINSOCK2_H@
374 #  undef select
375 #  define select                select_used_without_including_sys_select_h
376 # endif
377
378 # if @GNULIB_CLOSE@ && @HAVE_WINSOCK2_H@
379 /* gnulib internal function.  */
380 #  define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1
381 extern int _gl_close_fd_maybe_socket (int fd);
382 # endif
383
384 # ifdef __cplusplus
385 }
386 # endif
387
388 #endif /* HAVE_SYS_SOCKET_H */
389
390 #endif /* _GL_SYS_SOCKET_H */
391 #endif /* _GL_SYS_SOCKET_H */