New module 'accept4'.
[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-2009 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_SA_FAMILY_T@
47 typedef unsigned short  sa_family_t;
48 #endif
49
50 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
51 # include <alignof.h>
52 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
53    2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
54 # define __ss_aligntype unsigned long int
55 # define _SS_SIZE 256
56 # define _SS_PADSIZE \
57     (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype)      \
58                   ? sizeof (sa_family_t)                                \
59                   : alignof (__ss_aligntype))                           \
60                  + sizeof (__ss_aligntype)))
61
62 struct sockaddr_storage
63 {
64   sa_family_t ss_family;      /* Address family, etc.  */
65   __ss_aligntype __ss_align;  /* Force desired alignment.  */
66   char __ss_padding[_SS_PADSIZE];
67 };
68 #endif
69
70 #if @HAVE_SYS_SOCKET_H@
71
72 /* A platform that has <sys/socket.h>.  */
73
74 /* For shutdown().  */
75 # if !defined SHUT_RD
76 #  define SHUT_RD 0
77 # endif
78 # if !defined SHUT_WR
79 #  define SHUT_WR 1
80 # endif
81 # if !defined SHUT_RDWR
82 #  define SHUT_RDWR 2
83 # endif
84
85 #else
86
87 # ifdef __CYGWIN__
88 #  error "Cygwin does have a sys/socket.h, doesn't it?!?"
89 # endif
90
91 /* A platform that lacks <sys/socket.h>.
92
93    Currently only MinGW is supported.  See the gnulib manual regarding
94    Windows sockets.  MinGW has the header files winsock2.h and
95    ws2tcpip.h that declare the sys/socket.h definitions we need.  Note
96    that you can influence which definitions you get by setting the
97    WINVER symbol before including these two files.  For example,
98    getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
99    symbol is set indiriectly through WINVER).  You can set this by
100    adding AC_DEFINE(WINVER, 0x0501) to configure.ac.  Note that your
101    code may not run on older Windows releases then.  My Windows 2000
102    box was not able to run the code, for example.  The situation is
103    slightly confusing because:
104    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
105    suggests that getaddrinfo should be available on all Windows
106    releases. */
107
108
109 # if @HAVE_WINSOCK2_H@
110 #  include <winsock2.h>
111 # endif
112 # if @HAVE_WS2TCPIP_H@
113 #  include <ws2tcpip.h>
114 # endif
115
116 /* For shutdown(). */
117 # if !defined SHUT_RD && defined SD_RECEIVE
118 #  define SHUT_RD SD_RECEIVE
119 # endif
120 # if !defined SHUT_WR && defined SD_SEND
121 #  define SHUT_WR SD_SEND
122 # endif
123 # if !defined SHUT_RDWR && defined SD_BOTH
124 #  define SHUT_RDWR SD_BOTH
125 # endif
126
127 /* The definition of GL_LINK_WARNING is copied here.  */
128
129 # if @HAVE_WINSOCK2_H@
130 /* Include headers needed by the emulation code.  */
131 #  include <sys/types.h>
132 #  include <io.h>
133
134 typedef int socklen_t;
135
136 # endif
137
138 # ifdef __cplusplus
139 extern "C" {
140 # endif
141
142 # if @HAVE_WINSOCK2_H@
143
144 /* Re-define FD_ISSET to avoid a WSA call while we are not using
145    network sockets.  */
146 static inline int
147 rpl_fd_isset (SOCKET fd, fd_set * set)
148 {
149   u_int i;
150   if (set == NULL)
151     return 0;
152
153   for (i = 0; i < set->fd_count; i++)
154     if (set->fd_array[i] == fd)
155       return 1;
156
157   return 0;
158 }
159
160 #  undef FD_ISSET
161 #  define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
162
163 # endif
164
165 /* Wrap everything else to use libc file descriptors for sockets.  */
166
167 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
168 #  undef close
169 #  define close close_used_without_including_unistd_h
170 # endif
171
172 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
173 #  undef gethostname
174 #  define gethostname gethostname_used_without_including_unistd_h
175 # endif
176
177 # if @GNULIB_SOCKET@
178 #  if @HAVE_WINSOCK2_H@
179 #   undef socket
180 #   define socket               rpl_socket
181 extern int rpl_socket (int, int, int protocol);
182 #  endif
183 # elif @HAVE_WINSOCK2_H@
184 #  undef socket
185 #  define socket socket_used_without_requesting_gnulib_module_socket
186 # elif defined GNULIB_POSIXCHECK
187 #  undef socket
188 #  define socket(d,t,p) \
189      (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
190                        "use gnulib module socket for portability"), \
191       socket (d, t, p))
192 # endif
193
194 # if @GNULIB_CONNECT@
195 #  if @HAVE_WINSOCK2_H@
196 #   undef connect
197 #   define connect              rpl_connect
198 extern int rpl_connect (int, struct sockaddr *, int);
199 #  endif
200 # elif @HAVE_WINSOCK2_H@
201 #  undef connect
202 #  define connect socket_used_without_requesting_gnulib_module_connect
203 # elif defined GNULIB_POSIXCHECK
204 #  undef connect
205 #  define connect(s,a,l) \
206      (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
207                        "use gnulib module connect for portability"), \
208       connect (s, a, l))
209 # endif
210
211 # if @GNULIB_ACCEPT@
212 #  if @HAVE_WINSOCK2_H@
213 #   undef accept
214 #   define accept               rpl_accept
215 extern int rpl_accept (int, struct sockaddr *, int *);
216 #  endif
217 # elif @HAVE_WINSOCK2_H@
218 #  undef accept
219 #  define accept accept_used_without_requesting_gnulib_module_accept
220 # elif defined GNULIB_POSIXCHECK
221 #  undef accept
222 #  define accept(s,a,l) \
223      (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
224                        "use gnulib module accept for portability"), \
225       accept (s, a, l))
226 # endif
227
228 # if @GNULIB_BIND@
229 #  if @HAVE_WINSOCK2_H@
230 #   undef bind
231 #   define bind                 rpl_bind
232 extern int rpl_bind (int, struct sockaddr *, int);
233 #  endif
234 # elif @HAVE_WINSOCK2_H@
235 #  undef bind
236 #  define bind bind_used_without_requesting_gnulib_module_bind
237 # elif defined GNULIB_POSIXCHECK
238 #  undef bind
239 #  define bind(s,a,l) \
240      (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
241                        "use gnulib module bind for portability"), \
242       bind (s, a, l))
243 # endif
244
245 # if @GNULIB_GETPEERNAME@
246 #  if @HAVE_WINSOCK2_H@
247 #   undef getpeername
248 #   define getpeername          rpl_getpeername
249 extern int rpl_getpeername (int, struct sockaddr *, int *);
250 #  endif
251 # elif @HAVE_WINSOCK2_H@
252 #  undef getpeername
253 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
254 # elif defined GNULIB_POSIXCHECK
255 #  undef getpeername
256 #  define getpeername(s,a,l) \
257      (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
258                        "use gnulib module getpeername for portability"), \
259       getpeername (s, a, l))
260 # endif
261
262 # if @GNULIB_GETSOCKNAME@
263 #  if @HAVE_WINSOCK2_H@
264 #   undef getsockname
265 #   define getsockname          rpl_getsockname
266 extern int rpl_getsockname (int, struct sockaddr *, int *);
267 #  endif
268 # elif @HAVE_WINSOCK2_H@
269 #  undef getsockname
270 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
271 # elif defined GNULIB_POSIXCHECK
272 #  undef getsockname
273 #  define getsockname(s,a,l) \
274      (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
275                        "use gnulib module getsockname for portability"), \
276       getsockname (s, a, l))
277 # endif
278
279 # if @GNULIB_GETSOCKOPT@
280 #  if @HAVE_WINSOCK2_H@
281 #   undef getsockopt
282 #   define getsockopt           rpl_getsockopt
283 extern int rpl_getsockopt (int, int, int, void *, socklen_t *);
284 #  endif
285 # elif @HAVE_WINSOCK2_H@
286 #  undef getsockopt
287 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
288 # elif defined GNULIB_POSIXCHECK
289 #  undef getsockopt
290 #  define getsockopt(s,lvl,o,v,l) \
291      (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
292                        "use gnulib module getsockopt for portability"), \
293       getsockopt (s, lvl, o, v, l))
294 # endif
295
296 # if @GNULIB_LISTEN@
297 #  if @HAVE_WINSOCK2_H@
298 #   undef listen
299 #   define listen               rpl_listen
300 extern int rpl_listen (int, int);
301 #  endif
302 # elif @HAVE_WINSOCK2_H@
303 #  undef listen
304 #  define listen listen_used_without_requesting_gnulib_module_listen
305 # elif defined GNULIB_POSIXCHECK
306 #  undef listen
307 #  define listen(s,b) \
308      (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
309                        "use gnulib module listen for portability"), \
310       listen (s, b))
311 # endif
312
313 # if @GNULIB_RECV@
314 #  if @HAVE_WINSOCK2_H@
315 #   undef recv
316 #   define recv                 rpl_recv
317 extern int rpl_recv (int, void *, int, int);
318 #  endif
319 # elif @HAVE_WINSOCK2_H@
320 #  undef recv
321 #  define recv recv_used_without_requesting_gnulib_module_recv
322 # elif defined GNULIB_POSIXCHECK
323 #  undef recv
324 #  define recv(s,b,n,f) \
325      (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
326                        "use gnulib module recv for portability"), \
327       recv (s, b, n, f))
328 # endif
329
330 # if @GNULIB_SEND@
331 #  if @HAVE_WINSOCK2_H@
332 #   undef send
333 #   define send                 rpl_send
334 extern int rpl_send (int, const void *, int, int);
335 #  endif
336 # elif @HAVE_WINSOCK2_H@
337 #  undef send
338 #  define send send_used_without_requesting_gnulib_module_send
339 # elif defined GNULIB_POSIXCHECK
340 #  undef send
341 #  define send(s,b,n,f) \
342      (GL_LINK_WARNING ("send is not always POSIX compliant - " \
343                        "use gnulib module send for portability"), \
344       send (s, b, n, f))
345 # endif
346
347 # if @GNULIB_RECVFROM@
348 #  if @HAVE_WINSOCK2_H@
349 #   undef recvfrom
350 #   define recvfrom             rpl_recvfrom
351 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
352 #  endif
353 # elif @HAVE_WINSOCK2_H@
354 #  undef recvfrom
355 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
356 # elif defined GNULIB_POSIXCHECK
357 #  undef recvfrom
358 #  define recvfrom(s,b,n,f,a,l) \
359      (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
360                        "use gnulib module recvfrom for portability"), \
361       recvfrom (s, b, n, f, a, l))
362 # endif
363
364 # if @GNULIB_SENDTO@
365 #  if @HAVE_WINSOCK2_H@
366 #   undef sendto
367 #   define sendto               rpl_sendto
368 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
369 #  endif
370 # elif @HAVE_WINSOCK2_H@
371 #  undef sendto
372 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
373 # elif defined GNULIB_POSIXCHECK
374 #  undef sendto
375 #  define sendto(s,b,n,f,a,l) \
376      (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
377                        "use gnulib module sendto for portability"), \
378       sendto (s, b, n, f, a, l))
379 # endif
380
381 # if @GNULIB_SETSOCKOPT@
382 #  if @HAVE_WINSOCK2_H@
383 #   undef setsockopt
384 #   define setsockopt           rpl_setsockopt
385 extern int rpl_setsockopt (int, int, int, const void *, socklen_t);
386 #  endif
387 # elif @HAVE_WINSOCK2_H@
388 #  undef setsockopt
389 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
390 # elif defined GNULIB_POSIXCHECK
391 #  undef setsockopt
392 #  define setsockopt(s,lvl,o,v,l) \
393      (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
394                        "use gnulib module setsockopt for portability"), \
395       setsockopt (s, lvl, o, v, l))
396 # endif
397
398 # if @GNULIB_SHUTDOWN@
399 #  if @HAVE_WINSOCK2_H@
400 #   undef shutdown
401 #   define shutdown             rpl_shutdown
402 extern int rpl_shutdown (int, int);
403 #  endif
404 # elif @HAVE_WINSOCK2_H@
405 #  undef shutdown
406 #  define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
407 # elif defined GNULIB_POSIXCHECK
408 #  undef shutdown
409 #  define shutdown(s,h) \
410      (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
411                        "use gnulib module shutdown for portability"), \
412       shutdown (s, h))
413 # endif
414
415 # if @HAVE_WINSOCK2_H@
416 #  undef select
417 #  define select                select_used_without_including_sys_select_h
418 # endif
419
420 # ifdef __cplusplus
421 }
422 # endif
423
424 #endif /* HAVE_SYS_SOCKET_H */
425
426 #ifdef __cplusplus
427 extern "C" {
428 #endif
429
430 #if @GNULIB_ACCEPT4@
431 # if !@HAVE_ACCEPT4@
432 /* Accept a connection on a socket, with specific opening flags.
433    The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
434    and O_TEXT, O_BINARY (defined in "binary-io.h").
435    See also the Linux man page at
436    <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>.  */
437 extern int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
438                     int flags);
439 # endif
440 #elif defined GNULIB_POSIXCHECK
441 # undef accept4
442 # define accept4(s,a,l,f) \
443     (GL_LINK_WARNING ("accept4 is unportable - " \
444                       "use gnulib module accept4 for portability"), \
445      accept4 (s, a, l, f))
446 #endif
447
448 #ifdef __cplusplus
449 }
450 #endif
451
452 #endif /* _GL_SYS_SOCKET_H */
453 #endif /* _GL_SYS_SOCKET_H */