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-2013 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, see <http://www.gnu.org/licenses/>. */
19 /* This file is supposed to be used on platforms that lack <sys/socket.h>,
20 on platforms where <sys/socket.h> cannot be included standalone, and on
21 platforms where <sys/socket.h> does not provide all necessary definitions.
22 It is intended to provide definitions and prototypes needed by an
26 @PRAGMA_SYSTEM_HEADER@
30 #if defined _GL_ALREADY_INCLUDING_SYS_SOCKET_H
31 /* Special invocation convention:
32 - On Cygwin 1.5.x we have a sequence of nested includes
33 <sys/socket.h> -> <cygwin/socket.h> -> <asm/socket.h> -> <cygwin/if.h>,
34 and the latter includes <sys/socket.h>. In this situation, the functions
35 are not yet declared, therefore we cannot provide the C++ aliases. */
37 #@INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
40 /* Normal invocation convention. */
42 #ifndef _@GUARD_PREFIX@_SYS_SOCKET_H
44 #if @HAVE_SYS_SOCKET_H@
46 # define _GL_ALREADY_INCLUDING_SYS_SOCKET_H
48 /* On many platforms, <sys/socket.h> assumes prior inclusion of
50 # include <sys/types.h>
52 /* On FreeBSD 6.4, <sys/socket.h> defines some macros that assume that NULL
56 /* The include_next requires a split double-inclusion guard. */
57 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
59 # undef _GL_ALREADY_INCLUDING_SYS_SOCKET_H
63 #ifndef _@GUARD_PREFIX@_SYS_SOCKET_H
64 #define _@GUARD_PREFIX@_SYS_SOCKET_H
66 _GL_INLINE_HEADER_BEGIN
67 #ifndef _GL_SYS_SOCKET_INLINE
68 # define _GL_SYS_SOCKET_INLINE _GL_INLINE
71 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
73 /* The definition of _GL_ARG_NONNULL is copied here. */
75 /* The definition of _GL_WARN_ON_USE is copied here. */
77 #if !@HAVE_SA_FAMILY_T@
78 # if !GNULIB_defined_sa_family_t
79 typedef unsigned short sa_family_t;
80 # define GNULIB_defined_sa_family_t 1
84 #if @HAVE_STRUCT_SOCKADDR_STORAGE@
85 /* Make the 'struct sockaddr_storage' field 'ss_family' visible on AIX 7.1. */
86 # if !@HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY@
88 # define ss_family __ss_family
92 # include <stdalign.h>
93 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
94 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
95 # define __ss_aligntype unsigned long int
97 # define _SS_PADSIZE \
98 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
99 ? sizeof (sa_family_t) \
100 : alignof (__ss_aligntype)) \
101 + sizeof (__ss_aligntype)))
103 # if !GNULIB_defined_struct_sockaddr_storage
104 struct sockaddr_storage
106 sa_family_t ss_family; /* Address family, etc. */
107 __ss_aligntype __ss_align; /* Force desired alignment. */
108 char __ss_padding[_SS_PADSIZE];
110 # define GNULIB_defined_struct_sockaddr_storage 1
115 /* Get struct iovec. */
116 /* But avoid namespace pollution on glibc systems. */
117 #if ! defined __GLIBC__
118 # include <sys/uio.h>
121 #if @HAVE_SYS_SOCKET_H@
123 /* A platform that has <sys/socket.h>. */
125 /* For shutdown(). */
126 # if !defined SHUT_RD
129 # if !defined SHUT_WR
132 # if !defined SHUT_RDWR
139 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
142 /* A platform that lacks <sys/socket.h>.
144 Currently only MinGW is supported. See the gnulib manual regarding
145 Windows sockets. MinGW has the header files winsock2.h and
146 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
147 that you can influence which definitions you get by setting the
148 WINVER symbol before including these two files. For example,
149 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
150 symbol is set indirectly through WINVER). You can set this by
151 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
152 code may not run on older Windows releases then. My Windows 2000
153 box was not able to run the code, for example. The situation is
154 slightly confusing because
155 <http://msdn.microsoft.com/en-us/library/ms738520>
156 suggests that getaddrinfo should be available on all Windows
159 # if @HAVE_WINSOCK2_H@
160 # include <winsock2.h>
162 # if @HAVE_WS2TCPIP_H@
163 # include <ws2tcpip.h>
166 /* For shutdown(). */
167 # if !defined SHUT_RD && defined SD_RECEIVE
168 # define SHUT_RD SD_RECEIVE
170 # if !defined SHUT_WR && defined SD_SEND
171 # define SHUT_WR SD_SEND
173 # if !defined SHUT_RDWR && defined SD_BOTH
174 # define SHUT_RDWR SD_BOTH
177 # if @HAVE_WINSOCK2_H@
178 /* Include headers needed by the emulation code. */
179 # include <sys/types.h>
182 # if !GNULIB_defined_socklen_t
183 typedef int socklen_t;
184 # define GNULIB_defined_socklen_t 1
189 /* Rudimentary 'struct msghdr'; this works as long as you don't try to
190 access msg_control or msg_controllen. */
193 socklen_t msg_namelen;
194 struct iovec *msg_iov;
201 /* Fix some definitions from <winsock2.h>. */
203 #if @HAVE_WINSOCK2_H@
205 # if !GNULIB_defined_rpl_fd_isset
207 /* Re-define FD_ISSET to avoid a WSA call while we are not using
209 _GL_SYS_SOCKET_INLINE int
210 rpl_fd_isset (SOCKET fd, fd_set * set)
216 for (i = 0; i < set->fd_count; i++)
217 if (set->fd_array[i] == fd)
223 # define GNULIB_defined_rpl_fd_isset 1
227 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
231 /* Hide some function declarations from <winsock2.h>. */
233 #if @HAVE_WINSOCK2_H@
234 # if !defined _@GUARD_PREFIX@_UNISTD_H
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define close close_used_without_including_unistd_h
239 _GL_WARN_ON_USE (close,
240 "close() used without including <unistd.h>");
242 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
244 # define gethostname gethostname_used_without_including_unistd_h
246 _GL_WARN_ON_USE (gethostname,
247 "gethostname() used without including <unistd.h>");
250 # if !defined _@GUARD_PREFIX@_SYS_SELECT_H
251 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
253 # define select select_used_without_including_sys_select_h
255 _GL_WARN_ON_USE (select,
256 "select() used without including <sys/select.h>");
261 /* Wrap everything else to use libc file descriptors for sockets. */
264 # if @HAVE_WINSOCK2_H@
265 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
267 # define socket rpl_socket
269 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
270 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
272 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
274 _GL_CXXALIASWARN (socket);
275 #elif @HAVE_WINSOCK2_H@
277 # define socket socket_used_without_requesting_gnulib_module_socket
278 #elif defined GNULIB_POSIXCHECK
280 # if HAVE_RAW_DECL_SOCKET
281 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
282 "use gnulib module socket for portability");
287 # if @HAVE_WINSOCK2_H@
288 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
290 # define connect rpl_connect
292 _GL_FUNCDECL_RPL (connect, int,
293 (int fd, const struct sockaddr *addr, socklen_t addrlen)
294 _GL_ARG_NONNULL ((2)));
295 _GL_CXXALIAS_RPL (connect, int,
296 (int fd, const struct sockaddr *addr, socklen_t addrlen));
298 /* Need to cast, because on NonStop Kernel, the third parameter is
300 _GL_CXXALIAS_SYS_CAST (connect, int,
302 const struct sockaddr *addr, socklen_t addrlen));
304 _GL_CXXALIASWARN (connect);
305 #elif @HAVE_WINSOCK2_H@
307 # define connect socket_used_without_requesting_gnulib_module_connect
308 #elif defined GNULIB_POSIXCHECK
310 # if HAVE_RAW_DECL_CONNECT
311 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
312 "use gnulib module connect for portability");
317 # if @HAVE_WINSOCK2_H@
318 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
320 # define accept rpl_accept
322 _GL_FUNCDECL_RPL (accept, int,
323 (int fd, struct sockaddr *addr, socklen_t *addrlen));
324 _GL_CXXALIAS_RPL (accept, int,
325 (int fd, struct sockaddr *addr, socklen_t *addrlen));
327 /* Need to cast, because on Solaris 10 systems, the third parameter is
329 _GL_CXXALIAS_SYS_CAST (accept, int,
330 (int fd, struct sockaddr *addr, socklen_t *addrlen));
332 _GL_CXXALIASWARN (accept);
333 #elif @HAVE_WINSOCK2_H@
335 # define accept accept_used_without_requesting_gnulib_module_accept
336 #elif defined GNULIB_POSIXCHECK
338 # if HAVE_RAW_DECL_ACCEPT
339 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
340 "use gnulib module accept for portability");
345 # if @HAVE_WINSOCK2_H@
346 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
348 # define bind rpl_bind
350 _GL_FUNCDECL_RPL (bind, int,
351 (int fd, const struct sockaddr *addr, socklen_t addrlen)
352 _GL_ARG_NONNULL ((2)));
353 _GL_CXXALIAS_RPL (bind, int,
354 (int fd, const struct sockaddr *addr, socklen_t addrlen));
356 /* Need to cast, because on NonStop Kernel, the third parameter is
358 _GL_CXXALIAS_SYS_CAST (bind, int,
360 const struct sockaddr *addr, socklen_t addrlen));
362 _GL_CXXALIASWARN (bind);
363 #elif @HAVE_WINSOCK2_H@
365 # define bind bind_used_without_requesting_gnulib_module_bind
366 #elif defined GNULIB_POSIXCHECK
368 # if HAVE_RAW_DECL_BIND
369 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
370 "use gnulib module bind for portability");
374 #if @GNULIB_GETPEERNAME@
375 # if @HAVE_WINSOCK2_H@
376 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
378 # define getpeername rpl_getpeername
380 _GL_FUNCDECL_RPL (getpeername, int,
381 (int fd, struct sockaddr *addr, socklen_t *addrlen)
382 _GL_ARG_NONNULL ((2, 3)));
383 _GL_CXXALIAS_RPL (getpeername, int,
384 (int fd, struct sockaddr *addr, socklen_t *addrlen));
386 /* Need to cast, because on Solaris 10 systems, the third parameter is
388 _GL_CXXALIAS_SYS_CAST (getpeername, int,
389 (int fd, struct sockaddr *addr, socklen_t *addrlen));
391 _GL_CXXALIASWARN (getpeername);
392 #elif @HAVE_WINSOCK2_H@
394 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
395 #elif defined GNULIB_POSIXCHECK
397 # if HAVE_RAW_DECL_GETPEERNAME
398 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
399 "use gnulib module getpeername for portability");
403 #if @GNULIB_GETSOCKNAME@
404 # if @HAVE_WINSOCK2_H@
405 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
407 # define getsockname rpl_getsockname
409 _GL_FUNCDECL_RPL (getsockname, int,
410 (int fd, struct sockaddr *addr, socklen_t *addrlen)
411 _GL_ARG_NONNULL ((2, 3)));
412 _GL_CXXALIAS_RPL (getsockname, int,
413 (int fd, struct sockaddr *addr, socklen_t *addrlen));
415 /* Need to cast, because on Solaris 10 systems, the third parameter is
417 _GL_CXXALIAS_SYS_CAST (getsockname, int,
418 (int fd, struct sockaddr *addr, socklen_t *addrlen));
420 _GL_CXXALIASWARN (getsockname);
421 #elif @HAVE_WINSOCK2_H@
423 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
424 #elif defined GNULIB_POSIXCHECK
426 # if HAVE_RAW_DECL_GETSOCKNAME
427 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
428 "use gnulib module getsockname for portability");
432 #if @GNULIB_GETSOCKOPT@
433 # if @HAVE_WINSOCK2_H@
434 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
436 # define getsockopt rpl_getsockopt
438 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
439 void *optval, socklen_t *optlen)
440 _GL_ARG_NONNULL ((4, 5)));
441 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
442 void *optval, socklen_t *optlen));
444 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
446 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
447 void *optval, socklen_t *optlen));
449 _GL_CXXALIASWARN (getsockopt);
450 #elif @HAVE_WINSOCK2_H@
452 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
453 #elif defined GNULIB_POSIXCHECK
455 # if HAVE_RAW_DECL_GETSOCKOPT
456 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
457 "use gnulib module getsockopt for portability");
462 # if @HAVE_WINSOCK2_H@
463 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
465 # define listen rpl_listen
467 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
468 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
470 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
472 _GL_CXXALIASWARN (listen);
473 #elif @HAVE_WINSOCK2_H@
475 # define listen listen_used_without_requesting_gnulib_module_listen
476 #elif defined GNULIB_POSIXCHECK
478 # if HAVE_RAW_DECL_LISTEN
479 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
480 "use gnulib module listen for portability");
485 # if @HAVE_WINSOCK2_H@
486 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
488 # define recv rpl_recv
490 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
491 _GL_ARG_NONNULL ((2)));
492 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
494 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
496 _GL_CXXALIASWARN (recv);
497 #elif @HAVE_WINSOCK2_H@
499 # define recv recv_used_without_requesting_gnulib_module_recv
500 #elif defined GNULIB_POSIXCHECK
502 # if HAVE_RAW_DECL_RECV
503 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
504 "use gnulib module recv for portability");
509 # if @HAVE_WINSOCK2_H@
510 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
512 # define send rpl_send
514 _GL_FUNCDECL_RPL (send, ssize_t,
515 (int fd, const void *buf, size_t len, int flags)
516 _GL_ARG_NONNULL ((2)));
517 _GL_CXXALIAS_RPL (send, ssize_t,
518 (int fd, const void *buf, size_t len, int flags));
520 _GL_CXXALIAS_SYS (send, ssize_t,
521 (int fd, const void *buf, size_t len, int flags));
523 _GL_CXXALIASWARN (send);
524 #elif @HAVE_WINSOCK2_H@
526 # define send send_used_without_requesting_gnulib_module_send
527 #elif defined GNULIB_POSIXCHECK
529 # if HAVE_RAW_DECL_SEND
530 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
531 "use gnulib module send for portability");
535 #if @GNULIB_RECVFROM@
536 # if @HAVE_WINSOCK2_H@
537 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
539 # define recvfrom rpl_recvfrom
541 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
542 (int fd, void *buf, size_t len, int flags,
543 struct sockaddr *from, socklen_t *fromlen)
544 _GL_ARG_NONNULL ((2)));
545 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
546 (int fd, void *buf, size_t len, int flags,
547 struct sockaddr *from, socklen_t *fromlen));
549 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
551 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
552 (int fd, void *buf, size_t len, int flags,
553 struct sockaddr *from, socklen_t *fromlen));
555 _GL_CXXALIASWARN (recvfrom);
556 #elif @HAVE_WINSOCK2_H@
558 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
559 #elif defined GNULIB_POSIXCHECK
561 # if HAVE_RAW_DECL_RECVFROM
562 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
563 "use gnulib module recvfrom for portability");
568 # if @HAVE_WINSOCK2_H@
569 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
571 # define sendto rpl_sendto
573 _GL_FUNCDECL_RPL (sendto, ssize_t,
574 (int fd, const void *buf, size_t len, int flags,
575 const struct sockaddr *to, socklen_t tolen)
576 _GL_ARG_NONNULL ((2)));
577 _GL_CXXALIAS_RPL (sendto, ssize_t,
578 (int fd, const void *buf, size_t len, int flags,
579 const struct sockaddr *to, socklen_t tolen));
581 /* Need to cast, because on NonStop Kernel, the sixth parameter is
583 _GL_CXXALIAS_SYS_CAST (sendto, ssize_t,
584 (int fd, const void *buf, size_t len, int flags,
585 const struct sockaddr *to, socklen_t tolen));
587 _GL_CXXALIASWARN (sendto);
588 #elif @HAVE_WINSOCK2_H@
590 # define sendto sendto_used_without_requesting_gnulib_module_sendto
591 #elif defined GNULIB_POSIXCHECK
593 # if HAVE_RAW_DECL_SENDTO
594 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
595 "use gnulib module sendto for portability");
599 #if @GNULIB_SETSOCKOPT@
600 # if @HAVE_WINSOCK2_H@
601 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
603 # define setsockopt rpl_setsockopt
605 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
606 const void * optval, socklen_t optlen)
607 _GL_ARG_NONNULL ((4)));
608 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
609 const void * optval, socklen_t optlen));
611 /* Need to cast, because on NonStop Kernel, the fifth parameter is
613 _GL_CXXALIAS_SYS_CAST (setsockopt, int,
614 (int fd, int level, int optname,
615 const void * optval, socklen_t optlen));
617 _GL_CXXALIASWARN (setsockopt);
618 #elif @HAVE_WINSOCK2_H@
620 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
621 #elif defined GNULIB_POSIXCHECK
623 # if HAVE_RAW_DECL_SETSOCKOPT
624 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
625 "use gnulib module setsockopt for portability");
629 #if @GNULIB_SHUTDOWN@
630 # if @HAVE_WINSOCK2_H@
631 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
633 # define shutdown rpl_shutdown
635 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
636 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
638 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
640 _GL_CXXALIASWARN (shutdown);
641 #elif @HAVE_WINSOCK2_H@
643 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
644 #elif defined GNULIB_POSIXCHECK
646 # if HAVE_RAW_DECL_SHUTDOWN
647 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
648 "use gnulib module shutdown for portability");
653 /* Accept a connection on a socket, with specific opening flags.
654 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
655 and O_TEXT, O_BINARY (defined in "binary-io.h").
656 See also the Linux man page at
657 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
659 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
660 # define accept4 rpl_accept4
662 _GL_FUNCDECL_RPL (accept4, int,
663 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
665 _GL_CXXALIAS_RPL (accept4, int,
666 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
669 _GL_FUNCDECL_SYS (accept4, int,
670 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
672 _GL_CXXALIAS_SYS (accept4, int,
673 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
676 _GL_CXXALIASWARN (accept4);
677 #elif defined GNULIB_POSIXCHECK
679 # if HAVE_RAW_DECL_ACCEPT4
680 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
681 "use gnulib module accept4 for portability");
685 _GL_INLINE_HEADER_END
687 #endif /* _@GUARD_PREFIX@_SYS_SOCKET_H */
688 #endif /* _@GUARD_PREFIX@_SYS_SOCKET_H */