Avoid gcc warnings because of #pragma GCC system_header on older gcc.
[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 # if __GNUC__ >= 3
31 @PRAGMA_SYSTEM_HEADER@
32 # endif
33
34 /* On many platforms, <sys/socket.h> assumes prior inclusion of
35    <sys/types.h>.  */
36 # include <sys/types.h>
37
38 /* The include_next requires a split double-inclusion guard.  */
39 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
40
41 #endif
42
43 #ifndef _GL_SYS_SOCKET_H
44 #define _GL_SYS_SOCKET_H
45
46 #if @HAVE_SYS_SOCKET_H@
47
48 /* A platform that has <sys/socket.h>.  */
49
50 /* For shutdown().  */
51 # if !defined SHUT_RD
52 #  define SHUT_RD 0
53 # endif
54 # if !defined SHUT_WR
55 #  define SHUT_WR 1
56 # endif
57 # if !defined SHUT_RDWR
58 #  define SHUT_RDWR 2
59 # endif
60
61 #else
62
63 # ifdef __CYGWIN__
64 #  error "Cygwin does have a sys/socket.h, doesn't it?!?"
65 # endif
66
67 /* A platform that lacks <sys/socket.h>.
68
69    Currently only MinGW is supported.  See the gnulib manual regarding
70    Windows sockets.  MinGW has the header files winsock2.h and
71    ws2tcpip.h that declare the sys/socket.h definitions we need.  Note
72    that you can influence which definitions you get by setting the
73    WINVER symbol before including these two files.  For example,
74    getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
75    symbol is set indiriectly through WINVER).  You can set this by
76    adding AC_DEFINE(WINVER, 0x0501) to configure.ac.  Note that your
77    code may not run on older Windows releases then.  My Windows 2000
78    box was not able to run the code, for example.  The situation is
79    slightly confusing because:
80    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
81    suggests that getaddrinfo should be available on all Windows
82    releases. */
83
84
85 # if @HAVE_WINSOCK2_H@
86 #  include <winsock2.h>
87 # endif
88 # if @HAVE_WS2TCPIP_H@
89 #  include <ws2tcpip.h>
90 # endif
91
92 /* For shutdown(). */
93 # if !defined SHUT_RD && defined SD_RECEIVE
94 #  define SHUT_RD SD_RECEIVE
95 # endif
96 # if !defined SHUT_WR && defined SD_SEND
97 #  define SHUT_WR SD_SEND
98 # endif
99 # if !defined SHUT_RDWR && defined SD_BOTH
100 #  define SHUT_RDWR SD_BOTH
101 # endif
102
103 /* The definition of GL_LINK_WARNING is copied here.  */
104
105 # if @HAVE_WINSOCK2_H@
106 /* Include headers needed by the emulation code.  */
107 #  include <sys/types.h>
108 #  include <io.h>
109
110 typedef int socklen_t;
111
112 # endif
113
114 # ifdef __cplusplus
115 extern "C" {
116 # endif
117
118 # if @HAVE_WINSOCK2_H@
119
120 /* Re-define FD_ISSET to avoid a WSA call while we are not using
121    network sockets.  */
122 static inline int
123 rpl_fd_isset (int fd, fd_set * set)
124 {
125   int i;
126   if (set == NULL)
127     return 0;
128
129   for (i = 0; i < set->fd_count; i++)
130     if (set->fd_array[i] == fd)
131       return 1;
132
133   return 0;
134 }
135
136 #  undef FD_ISSET
137 #  define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
138
139 # endif
140
141 /* Wrap everything else to use libc file descriptors for sockets.  */
142
143 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
144 #  undef close
145 #  define close close_used_without_including_unistd_h
146 # endif
147
148 # if @GNULIB_SOCKET@
149 #  if @HAVE_WINSOCK2_H@
150 #   undef socket
151 #   define socket               rpl_socket
152 extern int rpl_socket (int, int, int protocol);
153 #  endif
154 # elif @HAVE_WINSOCK2_H@
155 #  undef socket
156 #  define socket socket_used_without_requesting_gnulib_module_socket
157 # elif defined GNULIB_POSIXCHECK
158 #  undef socket
159 #  define socket(d,t,p) \
160      (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
161                        "use gnulib module socket for portability"), \
162       socket (d, t, p))
163 # endif
164
165 # if @GNULIB_CONNECT@
166 #  if @HAVE_WINSOCK2_H@
167 #   undef connect
168 #   define connect              rpl_connect
169 extern int rpl_connect (int, struct sockaddr *, int);
170 #  endif
171 # elif @HAVE_WINSOCK2_H@
172 #  undef connect
173 #  define connect socket_used_without_requesting_gnulib_module_connect
174 # elif defined GNULIB_POSIXCHECK
175 #  undef connect
176 #  define connect(s,a,l) \
177      (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
178                        "use gnulib module connect for portability"), \
179       connect (s, a, l))
180 # endif
181
182 # if @GNULIB_ACCEPT@
183 #  if @HAVE_WINSOCK2_H@
184 #   undef accept
185 #   define accept               rpl_accept
186 extern int rpl_accept (int, struct sockaddr *, int *);
187 #  endif
188 # elif @HAVE_WINSOCK2_H@
189 #  undef accept
190 #  define accept accept_used_without_requesting_gnulib_module_accept
191 # elif defined GNULIB_POSIXCHECK
192 #  undef accept
193 #  define accept(s,a,l) \
194      (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
195                        "use gnulib module accept for portability"), \
196       accept (s, a, l))
197 # endif
198
199 # if @GNULIB_BIND@
200 #  if @HAVE_WINSOCK2_H@
201 #   undef bind
202 #   define bind                 rpl_bind
203 extern int rpl_bind (int, struct sockaddr *, int);
204 #  endif
205 # elif @HAVE_WINSOCK2_H@
206 #  undef bind
207 #  define bind bind_used_without_requesting_gnulib_module_bind
208 # elif defined GNULIB_POSIXCHECK
209 #  undef bind
210 #  define bind(s,a,l) \
211      (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
212                        "use gnulib module bind for portability"), \
213       bind (s, a, l))
214 # endif
215
216 # if @GNULIB_GETPEERNAME@
217 #  if @HAVE_WINSOCK2_H@
218 #   undef getpeername
219 #   define getpeername          rpl_getpeername
220 extern int rpl_getpeername (int, struct sockaddr *, int *);
221 #  endif
222 # elif @HAVE_WINSOCK2_H@
223 #  undef getpeername
224 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
225 # elif defined GNULIB_POSIXCHECK
226 #  undef getpeername
227 #  define getpeername(s,a,l) \
228      (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
229                        "use gnulib module getpeername for portability"), \
230       getpeername (s, a, l))
231 # endif
232
233 # if @GNULIB_GETSOCKNAME@
234 #  if @HAVE_WINSOCK2_H@
235 #   undef getsockname
236 #   define getsockname          rpl_getsockname
237 extern int rpl_getsockname (int, struct sockaddr *, int *);
238 #  endif
239 # elif @HAVE_WINSOCK2_H@
240 #  undef getsockname
241 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
242 # elif defined GNULIB_POSIXCHECK
243 #  undef getsockname
244 #  define getsockname(s,a,l) \
245      (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
246                        "use gnulib module getsockname for portability"), \
247       getsockname (s, a, l))
248 # endif
249
250 # if @GNULIB_GETSOCKOPT@
251 #  if @HAVE_WINSOCK2_H@
252 #   undef getsockopt
253 #   define getsockopt           rpl_getsockopt
254 extern int rpl_getsockopt (int, int, int, void *, int *);
255 #  endif
256 # elif @HAVE_WINSOCK2_H@
257 #  undef getsockopt
258 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
259 # elif defined GNULIB_POSIXCHECK
260 #  undef getsockopt
261 #  define getsockopt(s,lvl,o,v,l) \
262      (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
263                        "use gnulib module getsockopt for portability"), \
264       getsockopt (s, lvl, o, v, l))
265 # endif
266
267 # if @GNULIB_LISTEN@
268 #  if @HAVE_WINSOCK2_H@
269 #   undef listen
270 #   define listen               rpl_listen
271 extern int rpl_listen (int, int);
272 #  endif
273 # elif @HAVE_WINSOCK2_H@
274 #  undef listen
275 #  define listen listen_used_without_requesting_gnulib_module_listen
276 # elif defined GNULIB_POSIXCHECK
277 #  undef listen
278 #  define listen(s,b) \
279      (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
280                        "use gnulib module listen for portability"), \
281       listen (s, b))
282 # endif
283
284 # if @GNULIB_RECV@
285 #  if @HAVE_WINSOCK2_H@
286 #   undef recv
287 #   define recv                 rpl_recv
288 extern int rpl_recv (int, void *, int, int);
289 #  endif
290 # elif @HAVE_WINSOCK2_H@
291 #  undef recv
292 #  define recv recv_used_without_requesting_gnulib_module_recv
293 # elif defined GNULIB_POSIXCHECK
294 #  undef recv
295 #  define recv(s,b,n,f) \
296      (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
297                        "use gnulib module recv for portability"), \
298       recv (s, b, n, f))
299 # endif
300
301 # if @GNULIB_SEND@
302 #  if @HAVE_WINSOCK2_H@
303 #   undef send
304 #   define send                 rpl_send
305 extern int rpl_send (int, const void *, int, int);
306 #  endif
307 # elif @HAVE_WINSOCK2_H@
308 #  undef send
309 #  define send send_used_without_requesting_gnulib_module_send
310 # elif defined GNULIB_POSIXCHECK
311 #  undef send
312 #  define send(s,b,n,f) \
313      (GL_LINK_WARNING ("send is not always POSIX compliant - " \
314                        "use gnulib module send for portability"), \
315       send (s, b, n, f))
316 # endif
317
318 # if @GNULIB_RECVFROM@
319 #  if @HAVE_WINSOCK2_H@
320 #   undef recvfrom
321 #   define recvfrom             rpl_recvfrom
322 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
323 #  endif
324 # elif @HAVE_WINSOCK2_H@
325 #  undef recvfrom
326 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
327 # elif defined GNULIB_POSIXCHECK
328 #  undef recvfrom
329 #  define recvfrom(s,b,n,f,a,l) \
330      (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
331                        "use gnulib module recvfrom for portability"), \
332       recvfrom (s, b, n, f, a, l))
333 # endif
334
335 # if @GNULIB_SENDTO@
336 #  if @HAVE_WINSOCK2_H@
337 #   undef sendto
338 #   define sendto               rpl_sendto
339 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
340 #  endif
341 # elif @HAVE_WINSOCK2_H@
342 #  undef sendto
343 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
344 # elif defined GNULIB_POSIXCHECK
345 #  undef sendto
346 #  define sendto(s,b,n,f,a,l) \
347      (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
348                        "use gnulib module sendto for portability"), \
349       sendto (s, b, n, f, a, l))
350 # endif
351
352 # if @GNULIB_SETSOCKOPT@
353 #  if @HAVE_WINSOCK2_H@
354 #   undef setsockopt
355 #   define setsockopt           rpl_setsockopt
356 extern int rpl_setsockopt (int, int, int, const void *, int);
357 #  endif
358 # elif @HAVE_WINSOCK2_H@
359 #  undef setsockopt
360 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
361 # elif defined GNULIB_POSIXCHECK
362 #  undef setsockopt
363 #  define setsockopt(s,lvl,o,v,l) \
364      (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
365                        "use gnulib module setsockopt for portability"), \
366       setsockopt (s, lvl, o, v, l))
367 # endif
368
369 # if @GNULIB_SHUTDOWN@
370 #  if @HAVE_WINSOCK2_H@
371 #   undef shutdown
372 #   define shutdown             rpl_shutdown
373 extern int rpl_shutdown (int, int);
374 #  endif
375 # elif @HAVE_WINSOCK2_H@
376 #  undef shutdown
377 #  define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
378 # elif defined GNULIB_POSIXCHECK
379 #  undef shutdown
380 #  define shutdown(s,h) \
381      (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
382                        "use gnulib module shutdown for portability"), \
383       shutdown (s, h))
384 # endif
385
386 # if @HAVE_WINSOCK2_H@
387 #  undef select
388 #  define select                select_used_without_including_sys_select_h
389 # endif
390
391 # if @GNULIB_CLOSE@ && @HAVE_WINSOCK2_H@
392 /* gnulib internal function.  */
393 #  define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1
394 extern int _gl_close_fd_maybe_socket (int fd);
395 # endif
396
397 # ifdef __cplusplus
398 }
399 # endif
400
401 #endif /* HAVE_SYS_SOCKET_H */
402
403 #endif /* _GL_SYS_SOCKET_H */
404 #endif /* _GL_SYS_SOCKET_H */