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-2011 Free Software Foundation, Inc.
4 Written by Simon Josefsson.
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)
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.
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. */
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
27 @PRAGMA_SYSTEM_HEADER@
31 #if defined _GL_ALREADY_INCLUDING_SYS_SOCKET_H
32 /* Special invocation convention:
33 - On Cygwin 1.5.x we have a sequence of nested includes
34 <sys/socket.h> -> <cygwin/socket.h> -> <asm/socket.h> -> <cygwin/if.h>,
35 and the latter includes <sys/socket.h>. In this situation, the functions
36 are not yet declared, therefore we cannot provide the C++ aliases. */
38 #@INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
41 /* Normal invocation convention. */
43 #ifndef _GL_SYS_SOCKET_H
45 #if @HAVE_SYS_SOCKET_H@
47 # define _GL_ALREADY_INCLUDING_SYS_SOCKET_H
49 /* On many platforms, <sys/socket.h> assumes prior inclusion of
51 # include <sys/types.h>
53 /* On FreeBSD 6.4, <sys/socket.h> defines some macros that assume that NULL
57 /* The include_next requires a split double-inclusion guard. */
58 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
60 # undef _GL_ALREADY_INCLUDING_SYS_SOCKET_H
64 #ifndef _GL_SYS_SOCKET_H
65 #define _GL_SYS_SOCKET_H
67 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
69 /* The definition of _GL_ARG_NONNULL is copied here. */
71 /* The definition of _GL_WARN_ON_USE is copied here. */
73 #if !@HAVE_SA_FAMILY_T@
74 # if !GNULIB_defined_sa_family_t
75 typedef unsigned short sa_family_t;
76 # define GNULIB_defined_sa_family_t 1
80 #if @HAVE_STRUCT_SOCKADDR_STORAGE@
81 /* Make the 'struct sockaddr_storage' field 'ss_family' visible on AIX 7.1. */
82 # if !@HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY@
84 # define ss_family __ss_family
89 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
90 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
91 # define __ss_aligntype unsigned long int
93 # define _SS_PADSIZE \
94 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
95 ? sizeof (sa_family_t) \
96 : alignof (__ss_aligntype)) \
97 + sizeof (__ss_aligntype)))
99 # if !GNULIB_defined_struct_sockaddr_storage
100 struct sockaddr_storage
102 sa_family_t ss_family; /* Address family, etc. */
103 __ss_aligntype __ss_align; /* Force desired alignment. */
104 char __ss_padding[_SS_PADSIZE];
106 # define GNULIB_defined_struct_sockaddr_storage 1
111 #if @HAVE_SYS_SOCKET_H@
113 /* A platform that has <sys/socket.h>. */
115 /* For shutdown(). */
116 # if !defined SHUT_RD
119 # if !defined SHUT_WR
122 # if !defined SHUT_RDWR
129 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
132 /* A platform that lacks <sys/socket.h>.
134 Currently only MinGW is supported. See the gnulib manual regarding
135 Windows sockets. MinGW has the header files winsock2.h and
136 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
137 that you can influence which definitions you get by setting the
138 WINVER symbol before including these two files. For example,
139 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
140 symbol is set indiriectly through WINVER). You can set this by
141 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
142 code may not run on older Windows releases then. My Windows 2000
143 box was not able to run the code, for example. The situation is
144 slightly confusing because
145 <http://msdn.microsoft.com/en-us/library/ms738520>
146 suggests that getaddrinfo should be available on all Windows
150 # if @HAVE_WINSOCK2_H@
151 # include <winsock2.h>
153 # if @HAVE_WS2TCPIP_H@
154 # include <ws2tcpip.h>
157 /* For shutdown(). */
158 # if !defined SHUT_RD && defined SD_RECEIVE
159 # define SHUT_RD SD_RECEIVE
161 # if !defined SHUT_WR && defined SD_SEND
162 # define SHUT_WR SD_SEND
164 # if !defined SHUT_RDWR && defined SD_BOTH
165 # define SHUT_RDWR SD_BOTH
168 # if @HAVE_WINSOCK2_H@
169 /* Include headers needed by the emulation code. */
170 # include <sys/types.h>
173 # if !GNULIB_defined_socklen_t
174 typedef int socklen_t;
175 # define GNULIB_defined_socklen_t 1
182 #if @HAVE_WINSOCK2_H@
184 # if !GNULIB_defined_rpl_fd_isset
186 /* Re-define FD_ISSET to avoid a WSA call while we are not using
189 rpl_fd_isset (SOCKET fd, fd_set * set)
195 for (i = 0; i < set->fd_count; i++)
196 if (set->fd_array[i] == fd)
202 # define GNULIB_defined_rpl_fd_isset 1
206 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
210 /* Wrap everything else to use libc file descriptors for sockets. */
212 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
213 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
215 # define close close_used_without_including_unistd_h
217 _GL_WARN_ON_USE (close,
218 "close() used without including <unistd.h>");
222 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
223 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
225 # define gethostname gethostname_used_without_including_unistd_h
227 _GL_WARN_ON_USE (gethostname,
228 "gethostname() used without including <unistd.h>");
233 # if @HAVE_WINSOCK2_H@
234 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
236 # define socket rpl_socket
238 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
239 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
241 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
243 _GL_CXXALIASWARN (socket);
244 #elif @HAVE_WINSOCK2_H@
246 # define socket socket_used_without_requesting_gnulib_module_socket
247 #elif defined GNULIB_POSIXCHECK
249 # if HAVE_RAW_DECL_SOCKET
250 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
251 "use gnulib module socket for portability");
256 # if @HAVE_WINSOCK2_H@
257 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
259 # define connect rpl_connect
261 _GL_FUNCDECL_RPL (connect, int,
262 (int fd, const struct sockaddr *addr, socklen_t addrlen)
263 _GL_ARG_NONNULL ((2)));
264 _GL_CXXALIAS_RPL (connect, int,
265 (int fd, const struct sockaddr *addr, socklen_t addrlen));
267 /* Need to cast, because on NonStop Kernel, the third parameter is
269 _GL_CXXALIAS_SYS_CAST (connect, int,
271 const struct sockaddr *addr, socklen_t addrlen));
273 _GL_CXXALIASWARN (connect);
274 #elif @HAVE_WINSOCK2_H@
276 # define connect socket_used_without_requesting_gnulib_module_connect
277 #elif defined GNULIB_POSIXCHECK
279 # if HAVE_RAW_DECL_CONNECT
280 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
281 "use gnulib module connect for portability");
286 # if @HAVE_WINSOCK2_H@
287 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
289 # define accept rpl_accept
291 _GL_FUNCDECL_RPL (accept, int,
292 (int fd, struct sockaddr *addr, socklen_t *addrlen));
293 _GL_CXXALIAS_RPL (accept, int,
294 (int fd, struct sockaddr *addr, socklen_t *addrlen));
296 /* Need to cast, because on Solaris 10 systems, the third parameter is
298 _GL_CXXALIAS_SYS_CAST (accept, int,
299 (int fd, struct sockaddr *addr, socklen_t *addrlen));
301 _GL_CXXALIASWARN (accept);
302 #elif @HAVE_WINSOCK2_H@
304 # define accept accept_used_without_requesting_gnulib_module_accept
305 #elif defined GNULIB_POSIXCHECK
307 # if HAVE_RAW_DECL_ACCEPT
308 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
309 "use gnulib module accept for portability");
314 # if @HAVE_WINSOCK2_H@
315 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
317 # define bind rpl_bind
319 _GL_FUNCDECL_RPL (bind, int,
320 (int fd, const struct sockaddr *addr, socklen_t addrlen)
321 _GL_ARG_NONNULL ((2)));
322 _GL_CXXALIAS_RPL (bind, int,
323 (int fd, const struct sockaddr *addr, socklen_t addrlen));
325 /* Need to cast, because on NonStop Kernel, the third parameter is
327 _GL_CXXALIAS_SYS_CAST (bind, int,
329 const struct sockaddr *addr, socklen_t addrlen));
331 _GL_CXXALIASWARN (bind);
332 #elif @HAVE_WINSOCK2_H@
334 # define bind bind_used_without_requesting_gnulib_module_bind
335 #elif defined GNULIB_POSIXCHECK
337 # if HAVE_RAW_DECL_BIND
338 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
339 "use gnulib module bind for portability");
343 #if @GNULIB_GETPEERNAME@
344 # if @HAVE_WINSOCK2_H@
345 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
347 # define getpeername rpl_getpeername
349 _GL_FUNCDECL_RPL (getpeername, int,
350 (int fd, struct sockaddr *addr, socklen_t *addrlen)
351 _GL_ARG_NONNULL ((2, 3)));
352 _GL_CXXALIAS_RPL (getpeername, int,
353 (int fd, struct sockaddr *addr, socklen_t *addrlen));
355 /* Need to cast, because on Solaris 10 systems, the third parameter is
357 _GL_CXXALIAS_SYS_CAST (getpeername, int,
358 (int fd, struct sockaddr *addr, socklen_t *addrlen));
360 _GL_CXXALIASWARN (getpeername);
361 #elif @HAVE_WINSOCK2_H@
363 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
364 #elif defined GNULIB_POSIXCHECK
366 # if HAVE_RAW_DECL_GETPEERNAME
367 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
368 "use gnulib module getpeername for portability");
372 #if @GNULIB_GETSOCKNAME@
373 # if @HAVE_WINSOCK2_H@
374 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
376 # define getsockname rpl_getsockname
378 _GL_FUNCDECL_RPL (getsockname, int,
379 (int fd, struct sockaddr *addr, socklen_t *addrlen)
380 _GL_ARG_NONNULL ((2, 3)));
381 _GL_CXXALIAS_RPL (getsockname, int,
382 (int fd, struct sockaddr *addr, socklen_t *addrlen));
384 /* Need to cast, because on Solaris 10 systems, the third parameter is
386 _GL_CXXALIAS_SYS_CAST (getsockname, int,
387 (int fd, struct sockaddr *addr, socklen_t *addrlen));
389 _GL_CXXALIASWARN (getsockname);
390 #elif @HAVE_WINSOCK2_H@
392 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
393 #elif defined GNULIB_POSIXCHECK
395 # if HAVE_RAW_DECL_GETSOCKNAME
396 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
397 "use gnulib module getsockname for portability");
401 #if @GNULIB_GETSOCKOPT@
402 # if @HAVE_WINSOCK2_H@
403 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
405 # define getsockopt rpl_getsockopt
407 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
408 void *optval, socklen_t *optlen)
409 _GL_ARG_NONNULL ((4, 5)));
410 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
411 void *optval, socklen_t *optlen));
413 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
415 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
416 void *optval, socklen_t *optlen));
418 _GL_CXXALIASWARN (getsockopt);
419 #elif @HAVE_WINSOCK2_H@
421 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
422 #elif defined GNULIB_POSIXCHECK
424 # if HAVE_RAW_DECL_GETSOCKOPT
425 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
426 "use gnulib module getsockopt for portability");
431 # if @HAVE_WINSOCK2_H@
432 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
434 # define listen rpl_listen
436 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
437 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
439 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
441 _GL_CXXALIASWARN (listen);
442 #elif @HAVE_WINSOCK2_H@
444 # define listen listen_used_without_requesting_gnulib_module_listen
445 #elif defined GNULIB_POSIXCHECK
447 # if HAVE_RAW_DECL_LISTEN
448 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
449 "use gnulib module listen for portability");
454 # if @HAVE_WINSOCK2_H@
455 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
457 # define recv rpl_recv
459 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
460 _GL_ARG_NONNULL ((2)));
461 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
463 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
465 _GL_CXXALIASWARN (recv);
466 #elif @HAVE_WINSOCK2_H@
468 # define recv recv_used_without_requesting_gnulib_module_recv
469 #elif defined GNULIB_POSIXCHECK
471 # if HAVE_RAW_DECL_RECV
472 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
473 "use gnulib module recv for portability");
478 # if @HAVE_WINSOCK2_H@
479 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
481 # define send rpl_send
483 _GL_FUNCDECL_RPL (send, ssize_t,
484 (int fd, const void *buf, size_t len, int flags)
485 _GL_ARG_NONNULL ((2)));
486 _GL_CXXALIAS_RPL (send, ssize_t,
487 (int fd, const void *buf, size_t len, int flags));
489 _GL_CXXALIAS_SYS (send, ssize_t,
490 (int fd, const void *buf, size_t len, int flags));
492 _GL_CXXALIASWARN (send);
493 #elif @HAVE_WINSOCK2_H@
495 # define send send_used_without_requesting_gnulib_module_send
496 #elif defined GNULIB_POSIXCHECK
498 # if HAVE_RAW_DECL_SEND
499 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
500 "use gnulib module send for portability");
504 #if @GNULIB_RECVFROM@
505 # if @HAVE_WINSOCK2_H@
506 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
508 # define recvfrom rpl_recvfrom
510 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
511 (int fd, void *buf, size_t len, int flags,
512 struct sockaddr *from, socklen_t *fromlen)
513 _GL_ARG_NONNULL ((2)));
514 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
515 (int fd, void *buf, size_t len, int flags,
516 struct sockaddr *from, socklen_t *fromlen));
518 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
520 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
521 (int fd, void *buf, size_t len, int flags,
522 struct sockaddr *from, socklen_t *fromlen));
524 _GL_CXXALIASWARN (recvfrom);
525 #elif @HAVE_WINSOCK2_H@
527 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
528 #elif defined GNULIB_POSIXCHECK
530 # if HAVE_RAW_DECL_RECVFROM
531 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
532 "use gnulib module recvfrom for portability");
537 # if @HAVE_WINSOCK2_H@
538 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
540 # define sendto rpl_sendto
542 _GL_FUNCDECL_RPL (sendto, ssize_t,
543 (int fd, const void *buf, size_t len, int flags,
544 const struct sockaddr *to, socklen_t tolen)
545 _GL_ARG_NONNULL ((2)));
546 _GL_CXXALIAS_RPL (sendto, ssize_t,
547 (int fd, const void *buf, size_t len, int flags,
548 const struct sockaddr *to, socklen_t tolen));
550 /* Need to cast, because on NonStop Kernel, the sixth parameter is
552 _GL_CXXALIAS_SYS_CAST (sendto, ssize_t,
553 (int fd, const void *buf, size_t len, int flags,
554 const struct sockaddr *to, socklen_t tolen));
556 _GL_CXXALIASWARN (sendto);
557 #elif @HAVE_WINSOCK2_H@
559 # define sendto sendto_used_without_requesting_gnulib_module_sendto
560 #elif defined GNULIB_POSIXCHECK
562 # if HAVE_RAW_DECL_SENDTO
563 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
564 "use gnulib module sendto for portability");
568 #if @GNULIB_SETSOCKOPT@
569 # if @HAVE_WINSOCK2_H@
570 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
572 # define setsockopt rpl_setsockopt
574 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
575 const void * optval, socklen_t optlen)
576 _GL_ARG_NONNULL ((4)));
577 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
578 const void * optval, socklen_t optlen));
580 /* Need to cast, because on NonStop Kernel, the fifth parameter is
582 _GL_CXXALIAS_SYS_CAST (setsockopt, int,
583 (int fd, int level, int optname,
584 const void * optval, socklen_t optlen));
586 _GL_CXXALIASWARN (setsockopt);
587 #elif @HAVE_WINSOCK2_H@
589 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
590 #elif defined GNULIB_POSIXCHECK
592 # if HAVE_RAW_DECL_SETSOCKOPT
593 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
594 "use gnulib module setsockopt for portability");
598 #if @GNULIB_SHUTDOWN@
599 # if @HAVE_WINSOCK2_H@
600 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
602 # define shutdown rpl_shutdown
604 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
605 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
607 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
609 _GL_CXXALIASWARN (shutdown);
610 #elif @HAVE_WINSOCK2_H@
612 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
613 #elif defined GNULIB_POSIXCHECK
615 # if HAVE_RAW_DECL_SHUTDOWN
616 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
617 "use gnulib module shutdown for portability");
621 #if @HAVE_WINSOCK2_H@
622 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
624 # define select select_used_without_including_sys_select_h
626 _GL_WARN_ON_USE (select,
627 "select() used without including <sys/select.h>");
632 /* Accept a connection on a socket, with specific opening flags.
633 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
634 and O_TEXT, O_BINARY (defined in "binary-io.h").
635 See also the Linux man page at
636 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
638 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
639 # define accept4 rpl_accept4
641 _GL_FUNCDECL_RPL (accept4, int,
642 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
644 _GL_CXXALIAS_RPL (accept4, int,
645 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
648 _GL_FUNCDECL_SYS (accept4, int,
649 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
651 _GL_CXXALIAS_SYS (accept4, int,
652 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
655 _GL_CXXALIASWARN (accept4);
656 #elif defined GNULIB_POSIXCHECK
658 # if HAVE_RAW_DECL_ACCEPT4
659 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
660 "use gnulib module accept4 for portability");
664 #endif /* _GL_SYS_SOCKET_H */
665 #endif /* _GL_SYS_SOCKET_H */