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