Use POSIX declarations for socket functions.
[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-2010 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_WARN_ON_USE 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 #  if HAVE_RAW_DECL_SOCKET
191 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
192                  "use gnulib module socket for portability");
193 #  endif
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, const struct sockaddr *, socklen_t)
201      _GL_ARG_NONNULL ((2));
202 #  endif
203 # elif @HAVE_WINSOCK2_H@
204 #  undef connect
205 #  define connect socket_used_without_requesting_gnulib_module_connect
206 # elif defined GNULIB_POSIXCHECK
207 #  undef connect
208 #  if HAVE_RAW_DECL_CONNECT
209 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
210                  "use gnulib module connect for portability");
211 #  endif
212 # endif
213
214 # if @GNULIB_ACCEPT@
215 #  if @HAVE_WINSOCK2_H@
216 #   undef accept
217 #   define accept               rpl_accept
218 extern int rpl_accept (int, struct sockaddr *, socklen_t *);
219 #  endif
220 # elif @HAVE_WINSOCK2_H@
221 #  undef accept
222 #  define accept accept_used_without_requesting_gnulib_module_accept
223 # elif defined GNULIB_POSIXCHECK
224 #  undef accept
225 # if HAVE_RAW_DECL_ACCEPT
226 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
227                  "use gnulib module accept for portability");
228 #  endif
229 # endif
230
231 # if @GNULIB_BIND@
232 #  if @HAVE_WINSOCK2_H@
233 #   undef bind
234 #   define bind                 rpl_bind
235 extern int rpl_bind (int, const struct sockaddr *, socklen_t)
236      _GL_ARG_NONNULL ((2));
237 #  endif
238 # elif @HAVE_WINSOCK2_H@
239 #  undef bind
240 #  define bind bind_used_without_requesting_gnulib_module_bind
241 # elif defined GNULIB_POSIXCHECK
242 #  undef bind
243 #  if HAVE_RAW_DECL_BIND
244 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
245                  "use gnulib module bind for portability");
246 #  endif
247 # endif
248
249 # if @GNULIB_GETPEERNAME@
250 #  if @HAVE_WINSOCK2_H@
251 #   undef getpeername
252 #   define getpeername          rpl_getpeername
253 extern int rpl_getpeername (int, struct sockaddr *, socklen_t *)
254      _GL_ARG_NONNULL ((2, 3));
255 #  endif
256 # elif @HAVE_WINSOCK2_H@
257 #  undef getpeername
258 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
259 # elif defined GNULIB_POSIXCHECK
260 #  undef getpeername
261 #  if HAVE_RAW_DECL_GETPEERNAME
262 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
263                  "use gnulib module getpeername for portability");
264 #  endif
265 # endif
266
267 # if @GNULIB_GETSOCKNAME@
268 #  if @HAVE_WINSOCK2_H@
269 #   undef getsockname
270 #   define getsockname          rpl_getsockname
271 extern int rpl_getsockname (int, struct sockaddr *, socklen_t *)
272      _GL_ARG_NONNULL ((2, 3));
273 #  endif
274 # elif @HAVE_WINSOCK2_H@
275 #  undef getsockname
276 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
277 # elif defined GNULIB_POSIXCHECK
278 #  undef getsockname
279 #  if HAVE_RAW_DECL_GETSOCKNAME
280 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
281                  "use gnulib module getsockname for portability");
282 #  endif
283 # endif
284
285 # if @GNULIB_GETSOCKOPT@
286 #  if @HAVE_WINSOCK2_H@
287 #   undef getsockopt
288 #   define getsockopt           rpl_getsockopt
289 extern int rpl_getsockopt (int, int, int, void *, socklen_t *)
290      _GL_ARG_NONNULL ((4, 5));
291 #  endif
292 # elif @HAVE_WINSOCK2_H@
293 #  undef getsockopt
294 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
295 # elif defined GNULIB_POSIXCHECK
296 #  undef getsockopt
297 #  if HAVE_RAW_DECL_GETSOCKOPT
298 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
299                  "use gnulib module getsockopt for portability");
300 #  endif
301 # endif
302
303 # if @GNULIB_LISTEN@
304 #  if @HAVE_WINSOCK2_H@
305 #   undef listen
306 #   define listen               rpl_listen
307 extern int rpl_listen (int, int);
308 #  endif
309 # elif @HAVE_WINSOCK2_H@
310 #  undef listen
311 #  define listen listen_used_without_requesting_gnulib_module_listen
312 # elif defined GNULIB_POSIXCHECK
313 #  undef listen
314 #  if HAVE_RAW_DECL_LISTEN
315 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
316                  "use gnulib module listen for portability");
317 #  endif
318 # endif
319
320 # if @GNULIB_RECV@
321 #  if @HAVE_WINSOCK2_H@
322 #   undef recv
323 #   define recv                 rpl_recv
324 extern ssize_t rpl_recv (int, void *, size_t, int) _GL_ARG_NONNULL ((2));
325 #  endif
326 # elif @HAVE_WINSOCK2_H@
327 #  undef recv
328 #  define recv recv_used_without_requesting_gnulib_module_recv
329 # elif defined GNULIB_POSIXCHECK
330 #  undef recv
331 #  if HAVE_RAW_DECL_RECV
332 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
333                  "use gnulib module recv for portability");
334 #  endif
335 # endif
336
337 # if @GNULIB_SEND@
338 #  if @HAVE_WINSOCK2_H@
339 #   undef send
340 #   define send                 rpl_send
341 extern ssize_t rpl_send (int, const void *, size_t, int) _GL_ARG_NONNULL ((2));
342 #  endif
343 # elif @HAVE_WINSOCK2_H@
344 #  undef send
345 #  define send send_used_without_requesting_gnulib_module_send
346 # elif defined GNULIB_POSIXCHECK
347 #  undef send
348 #  if HAVE_RAW_DECL_SEND
349 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
350                  "use gnulib module send for portability");
351 #  endif
352 # endif
353
354 # if @GNULIB_RECVFROM@
355 #  if @HAVE_WINSOCK2_H@
356 #   undef recvfrom
357 #   define recvfrom             rpl_recvfrom
358 extern ssize_t rpl_recvfrom (int, void *, size_t, int,
359                              struct sockaddr *, socklen_t *)
360      _GL_ARG_NONNULL ((2));
361 #  endif
362 # elif @HAVE_WINSOCK2_H@
363 #  undef recvfrom
364 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
365 # elif defined GNULIB_POSIXCHECK
366 #  undef recvfrom
367 #  if HAVE_RAW_DECL_RECVFROM
368 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
369                  "use gnulib module recvfrom for portability");
370 #  endif
371 # endif
372
373 # if @GNULIB_SENDTO@
374 #  if @HAVE_WINSOCK2_H@
375 #   undef sendto
376 #   define sendto               rpl_sendto
377 extern ssize_t rpl_sendto (int, const void *, size_t, int,
378                            const struct sockaddr *, socklen_t)
379      _GL_ARG_NONNULL ((2));
380 #  endif
381 # elif @HAVE_WINSOCK2_H@
382 #  undef sendto
383 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
384 # elif defined GNULIB_POSIXCHECK
385 #  undef sendto
386 #  if HAVE_RAW_DECL_SENDTO
387 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
388                  "use gnulib module sendto for portability");
389 #  endif
390 # endif
391
392 # if @GNULIB_SETSOCKOPT@
393 #  if @HAVE_WINSOCK2_H@
394 #   undef setsockopt
395 #   define setsockopt           rpl_setsockopt
396 extern int rpl_setsockopt (int, int, int, const void *, socklen_t)
397      _GL_ARG_NONNULL ((4));
398 #  endif
399 # elif @HAVE_WINSOCK2_H@
400 #  undef setsockopt
401 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
402 # elif defined GNULIB_POSIXCHECK
403 #  undef setsockopt
404 #  if HAVE_RAW_DECL_SETSOCKOPT
405 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
406                  "use gnulib module setsockopt for portability");
407 #  endif
408 # endif
409
410 # if @GNULIB_SHUTDOWN@
411 #  if @HAVE_WINSOCK2_H@
412 #   undef shutdown
413 #   define shutdown             rpl_shutdown
414 extern int rpl_shutdown (int, int);
415 #  endif
416 # elif @HAVE_WINSOCK2_H@
417 #  undef shutdown
418 #  define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
419 # elif defined GNULIB_POSIXCHECK
420 #  undef shutdown
421 #  if HAVE_RAW_DECL_SHUTDOWN
422 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
423                  "use gnulib module shutdown for portability");
424 #  endif
425 # endif
426
427 # if @HAVE_WINSOCK2_H@
428 #  undef select
429 #  define select                select_used_without_including_sys_select_h
430 # endif
431
432 # ifdef __cplusplus
433 }
434 # endif
435
436 #endif /* HAVE_SYS_SOCKET_H */
437
438 #ifdef __cplusplus
439 extern "C" {
440 #endif
441
442 #if @GNULIB_ACCEPT4@
443 /* Accept a connection on a socket, with specific opening flags.
444    The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
445    and O_TEXT, O_BINARY (defined in "binary-io.h").
446    See also the Linux man page at
447    <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>.  */
448 # if @HAVE_ACCEPT4@
449 #  define accept4 rpl_accept4
450 # endif
451 extern int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
452                     int flags);
453 #elif defined GNULIB_POSIXCHECK
454 # undef accept4
455 # if HAVE_RAW_DECL_ACCEPT4
456 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
457                  "use gnulib module accept4 for portability");
458 # endif
459 #endif
460
461 #ifdef __cplusplus
462 }
463 #endif
464
465 #endif /* _GL_SYS_SOCKET_H */
466 #endif /* _GL_SYS_SOCKET_H */