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 /* Get struct iovec. */
112 /* But avoid namespace pollution on glibc systems. */
113 #if ! defined __GLIBC__
114 # include <sys/uio.h>
117 #if @HAVE_SYS_SOCKET_H@
119 /* A platform that has <sys/socket.h>. */
121 /* For shutdown(). */
122 # if !defined SHUT_RD
125 # if !defined SHUT_WR
128 # if !defined SHUT_RDWR
135 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
138 /* A platform that lacks <sys/socket.h>.
140 Currently only MinGW is supported. See the gnulib manual regarding
141 Windows sockets. MinGW has the header files winsock2.h and
142 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
143 that you can influence which definitions you get by setting the
144 WINVER symbol before including these two files. For example,
145 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
146 symbol is set indiriectly through WINVER). You can set this by
147 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
148 code may not run on older Windows releases then. My Windows 2000
149 box was not able to run the code, for example. The situation is
150 slightly confusing because
151 <http://msdn.microsoft.com/en-us/library/ms738520>
152 suggests that getaddrinfo should be available on all Windows
155 # if @HAVE_WINSOCK2_H@
156 # include <winsock2.h>
158 # if @HAVE_WS2TCPIP_H@
159 # include <ws2tcpip.h>
162 /* For shutdown(). */
163 # if !defined SHUT_RD && defined SD_RECEIVE
164 # define SHUT_RD SD_RECEIVE
166 # if !defined SHUT_WR && defined SD_SEND
167 # define SHUT_WR SD_SEND
169 # if !defined SHUT_RDWR && defined SD_BOTH
170 # define SHUT_RDWR SD_BOTH
173 # if @HAVE_WINSOCK2_H@
174 /* Include headers needed by the emulation code. */
175 # include <sys/types.h>
178 # if !GNULIB_defined_socklen_t
179 typedef int socklen_t;
180 # define GNULIB_defined_socklen_t 1
185 /* Rudimentary 'struct msghdr'; this works as long as you don't try to
186 access msg_control or msg_controllen. */
189 socklen_t msg_namelen;
190 struct iovec *msg_iov;
197 #if @HAVE_WINSOCK2_H@
199 # if !GNULIB_defined_rpl_fd_isset
201 /* Re-define FD_ISSET to avoid a WSA call while we are not using
204 rpl_fd_isset (SOCKET fd, fd_set * set)
210 for (i = 0; i < set->fd_count; i++)
211 if (set->fd_array[i] == fd)
217 # define GNULIB_defined_rpl_fd_isset 1
221 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
225 /* Wrap everything else to use libc file descriptors for sockets. */
227 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
228 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
230 # define close close_used_without_including_unistd_h
232 _GL_WARN_ON_USE (close,
233 "close() used without including <unistd.h>");
237 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
238 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
240 # define gethostname gethostname_used_without_including_unistd_h
242 _GL_WARN_ON_USE (gethostname,
243 "gethostname() used without including <unistd.h>");
248 # if @HAVE_WINSOCK2_H@
249 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
251 # define socket rpl_socket
253 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
254 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
256 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
258 _GL_CXXALIASWARN (socket);
259 #elif @HAVE_WINSOCK2_H@
261 # define socket socket_used_without_requesting_gnulib_module_socket
262 #elif defined GNULIB_POSIXCHECK
264 # if HAVE_RAW_DECL_SOCKET
265 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
266 "use gnulib module socket for portability");
271 # if @HAVE_WINSOCK2_H@
272 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
274 # define connect rpl_connect
276 _GL_FUNCDECL_RPL (connect, int,
277 (int fd, const struct sockaddr *addr, socklen_t addrlen)
278 _GL_ARG_NONNULL ((2)));
279 _GL_CXXALIAS_RPL (connect, int,
280 (int fd, const struct sockaddr *addr, socklen_t addrlen));
282 /* Need to cast, because on NonStop Kernel, the third parameter is
284 _GL_CXXALIAS_SYS_CAST (connect, int,
286 const struct sockaddr *addr, socklen_t addrlen));
288 _GL_CXXALIASWARN (connect);
289 #elif @HAVE_WINSOCK2_H@
291 # define connect socket_used_without_requesting_gnulib_module_connect
292 #elif defined GNULIB_POSIXCHECK
294 # if HAVE_RAW_DECL_CONNECT
295 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
296 "use gnulib module connect for portability");
301 # if @HAVE_WINSOCK2_H@
302 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 # define accept rpl_accept
306 _GL_FUNCDECL_RPL (accept, int,
307 (int fd, struct sockaddr *addr, socklen_t *addrlen));
308 _GL_CXXALIAS_RPL (accept, int,
309 (int fd, struct sockaddr *addr, socklen_t *addrlen));
311 /* Need to cast, because on Solaris 10 systems, the third parameter is
313 _GL_CXXALIAS_SYS_CAST (accept, int,
314 (int fd, struct sockaddr *addr, socklen_t *addrlen));
316 _GL_CXXALIASWARN (accept);
317 #elif @HAVE_WINSOCK2_H@
319 # define accept accept_used_without_requesting_gnulib_module_accept
320 #elif defined GNULIB_POSIXCHECK
322 # if HAVE_RAW_DECL_ACCEPT
323 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
324 "use gnulib module accept for portability");
329 # if @HAVE_WINSOCK2_H@
330 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
332 # define bind rpl_bind
334 _GL_FUNCDECL_RPL (bind, int,
335 (int fd, const struct sockaddr *addr, socklen_t addrlen)
336 _GL_ARG_NONNULL ((2)));
337 _GL_CXXALIAS_RPL (bind, int,
338 (int fd, const struct sockaddr *addr, socklen_t addrlen));
340 /* Need to cast, because on NonStop Kernel, the third parameter is
342 _GL_CXXALIAS_SYS_CAST (bind, int,
344 const struct sockaddr *addr, socklen_t addrlen));
346 _GL_CXXALIASWARN (bind);
347 #elif @HAVE_WINSOCK2_H@
349 # define bind bind_used_without_requesting_gnulib_module_bind
350 #elif defined GNULIB_POSIXCHECK
352 # if HAVE_RAW_DECL_BIND
353 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
354 "use gnulib module bind for portability");
358 #if @GNULIB_GETPEERNAME@
359 # if @HAVE_WINSOCK2_H@
360 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
362 # define getpeername rpl_getpeername
364 _GL_FUNCDECL_RPL (getpeername, int,
365 (int fd, struct sockaddr *addr, socklen_t *addrlen)
366 _GL_ARG_NONNULL ((2, 3)));
367 _GL_CXXALIAS_RPL (getpeername, int,
368 (int fd, struct sockaddr *addr, socklen_t *addrlen));
370 /* Need to cast, because on Solaris 10 systems, the third parameter is
372 _GL_CXXALIAS_SYS_CAST (getpeername, int,
373 (int fd, struct sockaddr *addr, socklen_t *addrlen));
375 _GL_CXXALIASWARN (getpeername);
376 #elif @HAVE_WINSOCK2_H@
378 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
379 #elif defined GNULIB_POSIXCHECK
381 # if HAVE_RAW_DECL_GETPEERNAME
382 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
383 "use gnulib module getpeername for portability");
387 #if @GNULIB_GETSOCKNAME@
388 # if @HAVE_WINSOCK2_H@
389 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
391 # define getsockname rpl_getsockname
393 _GL_FUNCDECL_RPL (getsockname, int,
394 (int fd, struct sockaddr *addr, socklen_t *addrlen)
395 _GL_ARG_NONNULL ((2, 3)));
396 _GL_CXXALIAS_RPL (getsockname, int,
397 (int fd, struct sockaddr *addr, socklen_t *addrlen));
399 /* Need to cast, because on Solaris 10 systems, the third parameter is
401 _GL_CXXALIAS_SYS_CAST (getsockname, int,
402 (int fd, struct sockaddr *addr, socklen_t *addrlen));
404 _GL_CXXALIASWARN (getsockname);
405 #elif @HAVE_WINSOCK2_H@
407 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
408 #elif defined GNULIB_POSIXCHECK
410 # if HAVE_RAW_DECL_GETSOCKNAME
411 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
412 "use gnulib module getsockname for portability");
416 #if @GNULIB_GETSOCKOPT@
417 # if @HAVE_WINSOCK2_H@
418 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
420 # define getsockopt rpl_getsockopt
422 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
423 void *optval, socklen_t *optlen)
424 _GL_ARG_NONNULL ((4, 5)));
425 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
426 void *optval, socklen_t *optlen));
428 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
430 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
431 void *optval, socklen_t *optlen));
433 _GL_CXXALIASWARN (getsockopt);
434 #elif @HAVE_WINSOCK2_H@
436 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
437 #elif defined GNULIB_POSIXCHECK
439 # if HAVE_RAW_DECL_GETSOCKOPT
440 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
441 "use gnulib module getsockopt for portability");
446 # if @HAVE_WINSOCK2_H@
447 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
449 # define listen rpl_listen
451 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
452 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
454 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
456 _GL_CXXALIASWARN (listen);
457 #elif @HAVE_WINSOCK2_H@
459 # define listen listen_used_without_requesting_gnulib_module_listen
460 #elif defined GNULIB_POSIXCHECK
462 # if HAVE_RAW_DECL_LISTEN
463 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
464 "use gnulib module listen for portability");
469 # if @HAVE_WINSOCK2_H@
470 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
472 # define recv rpl_recv
474 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
475 _GL_ARG_NONNULL ((2)));
476 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
478 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
480 _GL_CXXALIASWARN (recv);
481 #elif @HAVE_WINSOCK2_H@
483 # define recv recv_used_without_requesting_gnulib_module_recv
484 #elif defined GNULIB_POSIXCHECK
486 # if HAVE_RAW_DECL_RECV
487 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
488 "use gnulib module recv for portability");
493 # if @HAVE_WINSOCK2_H@
494 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
496 # define send rpl_send
498 _GL_FUNCDECL_RPL (send, ssize_t,
499 (int fd, const void *buf, size_t len, int flags)
500 _GL_ARG_NONNULL ((2)));
501 _GL_CXXALIAS_RPL (send, ssize_t,
502 (int fd, const void *buf, size_t len, int flags));
504 _GL_CXXALIAS_SYS (send, ssize_t,
505 (int fd, const void *buf, size_t len, int flags));
507 _GL_CXXALIASWARN (send);
508 #elif @HAVE_WINSOCK2_H@
510 # define send send_used_without_requesting_gnulib_module_send
511 #elif defined GNULIB_POSIXCHECK
513 # if HAVE_RAW_DECL_SEND
514 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
515 "use gnulib module send for portability");
519 #if @GNULIB_RECVFROM@
520 # if @HAVE_WINSOCK2_H@
521 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
523 # define recvfrom rpl_recvfrom
525 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
526 (int fd, void *buf, size_t len, int flags,
527 struct sockaddr *from, socklen_t *fromlen)
528 _GL_ARG_NONNULL ((2)));
529 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
530 (int fd, void *buf, size_t len, int flags,
531 struct sockaddr *from, socklen_t *fromlen));
533 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
535 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
536 (int fd, void *buf, size_t len, int flags,
537 struct sockaddr *from, socklen_t *fromlen));
539 _GL_CXXALIASWARN (recvfrom);
540 #elif @HAVE_WINSOCK2_H@
542 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
543 #elif defined GNULIB_POSIXCHECK
545 # if HAVE_RAW_DECL_RECVFROM
546 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
547 "use gnulib module recvfrom for portability");
552 # if @HAVE_WINSOCK2_H@
553 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
555 # define sendto rpl_sendto
557 _GL_FUNCDECL_RPL (sendto, ssize_t,
558 (int fd, const void *buf, size_t len, int flags,
559 const struct sockaddr *to, socklen_t tolen)
560 _GL_ARG_NONNULL ((2)));
561 _GL_CXXALIAS_RPL (sendto, ssize_t,
562 (int fd, const void *buf, size_t len, int flags,
563 const struct sockaddr *to, socklen_t tolen));
565 /* Need to cast, because on NonStop Kernel, the sixth parameter is
567 _GL_CXXALIAS_SYS_CAST (sendto, ssize_t,
568 (int fd, const void *buf, size_t len, int flags,
569 const struct sockaddr *to, socklen_t tolen));
571 _GL_CXXALIASWARN (sendto);
572 #elif @HAVE_WINSOCK2_H@
574 # define sendto sendto_used_without_requesting_gnulib_module_sendto
575 #elif defined GNULIB_POSIXCHECK
577 # if HAVE_RAW_DECL_SENDTO
578 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
579 "use gnulib module sendto for portability");
583 #if @GNULIB_SETSOCKOPT@
584 # if @HAVE_WINSOCK2_H@
585 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
587 # define setsockopt rpl_setsockopt
589 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
590 const void * optval, socklen_t optlen)
591 _GL_ARG_NONNULL ((4)));
592 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
593 const void * optval, socklen_t optlen));
595 /* Need to cast, because on NonStop Kernel, the fifth parameter is
597 _GL_CXXALIAS_SYS_CAST (setsockopt, int,
598 (int fd, int level, int optname,
599 const void * optval, socklen_t optlen));
601 _GL_CXXALIASWARN (setsockopt);
602 #elif @HAVE_WINSOCK2_H@
604 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
605 #elif defined GNULIB_POSIXCHECK
607 # if HAVE_RAW_DECL_SETSOCKOPT
608 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
609 "use gnulib module setsockopt for portability");
613 #if @GNULIB_SHUTDOWN@
614 # if @HAVE_WINSOCK2_H@
615 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
617 # define shutdown rpl_shutdown
619 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
620 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
622 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
624 _GL_CXXALIASWARN (shutdown);
625 #elif @HAVE_WINSOCK2_H@
627 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
628 #elif defined GNULIB_POSIXCHECK
630 # if HAVE_RAW_DECL_SHUTDOWN
631 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
632 "use gnulib module shutdown for portability");
636 #if @HAVE_WINSOCK2_H@
637 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
639 # define select select_used_without_including_sys_select_h
641 _GL_WARN_ON_USE (select,
642 "select() used without including <sys/select.h>");
647 /* Accept a connection on a socket, with specific opening flags.
648 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
649 and O_TEXT, O_BINARY (defined in "binary-io.h").
650 See also the Linux man page at
651 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
653 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
654 # define accept4 rpl_accept4
656 _GL_FUNCDECL_RPL (accept4, int,
657 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
659 _GL_CXXALIAS_RPL (accept4, int,
660 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
663 _GL_FUNCDECL_SYS (accept4, int,
664 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
666 _GL_CXXALIAS_SYS (accept4, int,
667 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
670 _GL_CXXALIASWARN (accept4);
671 #elif defined GNULIB_POSIXCHECK
673 # if HAVE_RAW_DECL_ACCEPT4
674 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
675 "use gnulib module accept4 for portability");
679 #endif /* _GL_SYS_SOCKET_H */
680 #endif /* _GL_SYS_SOCKET_H */