New modules 'accept', 'bind', 'connect', 'getpeername', 'getsockname', 'getsockopt...
[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@
142 #  undef close
143 #  define close                 rpl_close
144 extern int rpl_close(int);
145 # endif
146
147 # if @GNULIB_SOCKET@
148 #  if @HAVE_WINSOCK2_H@
149 #   undef socket
150 #   define socket               rpl_socket
151 extern int rpl_socket (int, int, int protocol);
152 #  endif
153 # elif @HAVE_WINSOCK2_H@
154 #  undef socket
155 #  define socket socket_used_without_requesting_gnulib_module_socket
156 # elif defined GNULIB_POSIXCHECK
157 #  undef socket
158 #  define socket(d,t,p) \
159      (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
160                        "use gnulib module socket for portability"), \
161       socket (d, t, p))
162 # endif
163
164 # if @GNULIB_CONNECT@
165 #  if @HAVE_WINSOCK2_H@
166 #   undef connect
167 #   define connect              rpl_connect
168 extern int rpl_connect (int, struct sockaddr *, int);
169 #  endif
170 # elif @HAVE_WINSOCK2_H@
171 #  undef connect
172 #  define connect socket_used_without_requesting_gnulib_module_connect
173 # elif defined GNULIB_POSIXCHECK
174 #  undef connect
175 #  define connect(s,a,l) \
176      (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
177                        "use gnulib module connect for portability"), \
178       connect (s, a, l))
179 # endif
180
181 # if @GNULIB_ACCEPT@
182 #  if @HAVE_WINSOCK2_H@
183 #   undef accept
184 #   define accept               rpl_accept
185 extern int rpl_accept (int, struct sockaddr *, int *);
186 #  endif
187 # elif @HAVE_WINSOCK2_H@
188 #  undef accept
189 #  define accept accept_used_without_requesting_gnulib_module_accept
190 # elif defined GNULIB_POSIXCHECK
191 #  undef accept
192 #  define accept(s,a,l) \
193      (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
194                        "use gnulib module accept for portability"), \
195       accept (s, a, l))
196 # endif
197
198 # if @GNULIB_BIND@
199 #  if @HAVE_WINSOCK2_H@
200 #   undef bind
201 #   define bind                 rpl_bind
202 extern int rpl_bind (int, struct sockaddr *, int);
203 #  endif
204 # elif @HAVE_WINSOCK2_H@
205 #  undef bind
206 #  define bind bind_used_without_requesting_gnulib_module_bind
207 # elif defined GNULIB_POSIXCHECK
208 #  undef bind
209 #  define bind(s,a,l) \
210      (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
211                        "use gnulib module bind for portability"), \
212       bind (s, a, l))
213 # endif
214
215 # if @GNULIB_GETPEERNAME@
216 #  if @HAVE_WINSOCK2_H@
217 #   undef getpeername
218 #   define getpeername          rpl_getpeername
219 extern int rpl_getpeername (int, struct sockaddr *, int *);
220 #  endif
221 # elif @HAVE_WINSOCK2_H@
222 #  undef getpeername
223 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
224 # elif defined GNULIB_POSIXCHECK
225 #  undef getpeername
226 #  define getpeername(s,a,l) \
227      (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
228                        "use gnulib module getpeername for portability"), \
229       getpeername (s, a, l))
230 # endif
231
232 # if @GNULIB_GETSOCKNAME@
233 #  if @HAVE_WINSOCK2_H@
234 #   undef getsockname
235 #   define getsockname          rpl_getsockname
236 extern int rpl_getsockname (int, struct sockaddr *, int *);
237 #  endif
238 # elif @HAVE_WINSOCK2_H@
239 #  undef getsockname
240 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
241 # elif defined GNULIB_POSIXCHECK
242 #  undef getsockname
243 #  define getsockname(s,a,l) \
244      (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
245                        "use gnulib module getsockname for portability"), \
246       getsockname (s, a, l))
247 # endif
248
249 # if @GNULIB_GETSOCKOPT@
250 #  if @HAVE_WINSOCK2_H@
251 #   undef getsockopt
252 #   define getsockopt           rpl_getsockopt
253 extern int rpl_getsockopt (int, int, int, void *, int *);
254 #  endif
255 # elif @HAVE_WINSOCK2_H@
256 #  undef getsockopt
257 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
258 # elif defined GNULIB_POSIXCHECK
259 #  undef getsockopt
260 #  define getsockopt(s,lvl,o,v,l) \
261      (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
262                        "use gnulib module getsockopt for portability"), \
263       getsockopt (s, lvl, o, v, l))
264 # endif
265
266 # if @GNULIB_LISTEN@
267 #  if @HAVE_WINSOCK2_H@
268 #   undef listen
269 #   define listen               rpl_listen
270 extern int rpl_listen (int, int);
271 #  endif
272 # elif @HAVE_WINSOCK2_H@
273 #  undef listen
274 #  define listen listen_used_without_requesting_gnulib_module_listen
275 # elif defined GNULIB_POSIXCHECK
276 #  undef listen
277 #  define listen(s,b) \
278      (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
279                        "use gnulib module listen for portability"), \
280       listen (s, b))
281 # endif
282
283 # if @HAVE_WINSOCK2_H@
284 #  undef ioctl
285 #  define ioctl                 rpl_ioctl
286 extern int rpl_ioctl (int, unsigned long, char *);
287 # endif
288
289 # if @GNULIB_RECV@
290 #  if @HAVE_WINSOCK2_H@
291 #   undef recv
292 #   define recv                 rpl_recv
293 extern int rpl_recv (int, void *, int, int);
294 #  endif
295 # elif @HAVE_WINSOCK2_H@
296 #  undef recv
297 #  define recv recv_used_without_requesting_gnulib_module_recv
298 # elif defined GNULIB_POSIXCHECK
299 #  undef recv
300 #  define recv(s,b,n,f) \
301      (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
302                        "use gnulib module recv for portability"), \
303       recv (s, b, n, f))
304 # endif
305
306 # if @GNULIB_SEND@
307 #  if @HAVE_WINSOCK2_H@
308 #   undef send
309 #   define send                 rpl_send
310 extern int rpl_send (int, const void *, int, int);
311 #  endif
312 # elif @HAVE_WINSOCK2_H@
313 #  undef send
314 #  define send send_used_without_requesting_gnulib_module_send
315 # elif defined GNULIB_POSIXCHECK
316 #  undef send
317 #  define send(s,b,n,f) \
318      (GL_LINK_WARNING ("send is not always POSIX compliant - " \
319                        "use gnulib module send for portability"), \
320       send (s, b, n, f))
321 # endif
322
323 # if @GNULIB_RECVFROM@
324 #  if @HAVE_WINSOCK2_H@
325 #   undef recvfrom
326 #   define recvfrom             rpl_recvfrom
327 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
328 #  endif
329 # elif @HAVE_WINSOCK2_H@
330 #  undef recvfrom
331 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
332 # elif defined GNULIB_POSIXCHECK
333 #  undef recvfrom
334 #  define recvfrom(s,b,n,f,a,l) \
335      (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
336                        "use gnulib module recvfrom for portability"), \
337       recvfrom (s, b, n, f, a, l))
338 # endif
339
340 # if @GNULIB_SENDTO@
341 #  if @HAVE_WINSOCK2_H@
342 #   undef sendto
343 #   define sendto               rpl_sendto
344 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
345 #  endif
346 # elif @HAVE_WINSOCK2_H@
347 #  undef sendto
348 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
349 # elif defined GNULIB_POSIXCHECK
350 #  undef sendto
351 #  define sendto(s,b,n,f,a,l) \
352      (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
353                        "use gnulib module sendto for portability"), \
354       sendto (s, b, n, f, a, l))
355 # endif
356
357 # if @GNULIB_SETSOCKOPT@
358 #  if @HAVE_WINSOCK2_H@
359 #   undef setsockopt
360 #   define setsockopt           rpl_setsockopt
361 extern int rpl_setsockopt (int, int, int, const void *, int);
362 #  endif
363 # elif @HAVE_WINSOCK2_H@
364 #  undef setsockopt
365 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
366 # elif defined GNULIB_POSIXCHECK
367 #  undef setsockopt
368 #  define setsockopt(s,lvl,o,v,l) \
369      (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
370                        "use gnulib module setsockopt for portability"), \
371       setsockopt (s, lvl, o, v, l))
372 # endif
373
374 # if @HAVE_WINSOCK2_H@
375 #  undef select
376 #  define select                select_used_without_including_sys_select_h
377 # endif
378
379 # ifdef __cplusplus
380 }
381 # endif
382
383 #endif /* HAVE_SYS_SOCKET_H */
384
385 #endif /* _GL_SYS_SOCKET_H */
386 #endif /* _GL_SYS_SOCKET_H */