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 _@GUARD_PREFIX@_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 _@GUARD_PREFIX@_SYS_SOCKET_H
65 #define _@GUARD_PREFIX@_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 /* Fix some definitions from <winsock2.h>. */
199 #if @HAVE_WINSOCK2_H@
201 # if !GNULIB_defined_rpl_fd_isset
203 /* Re-define FD_ISSET to avoid a WSA call while we are not using
206 rpl_fd_isset (SOCKET fd, fd_set * set)
212 for (i = 0; i < set->fd_count; i++)
213 if (set->fd_array[i] == fd)
219 # define GNULIB_defined_rpl_fd_isset 1
223 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
227 /* Hide some function declarations from <winsock2.h>. */
229 #if @HAVE_WINSOCK2_H@
230 # if !defined _@GUARD_PREFIX@_UNISTD_H
231 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
233 # define close close_used_without_including_unistd_h
235 _GL_WARN_ON_USE (close,
236 "close() used without including <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>");
246 # if !defined _@GUARD_PREFIX@_SYS_SELECT_H
247 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
249 # define select select_used_without_including_sys_select_h
251 _GL_WARN_ON_USE (select,
252 "select() used without including <sys/select.h>");
257 /* Wrap everything else to use libc file descriptors for sockets. */
260 # if @HAVE_WINSOCK2_H@
261 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
263 # define socket rpl_socket
265 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
266 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
268 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
270 _GL_CXXALIASWARN (socket);
271 #elif @HAVE_WINSOCK2_H@
273 # define socket socket_used_without_requesting_gnulib_module_socket
274 #elif defined GNULIB_POSIXCHECK
276 # if HAVE_RAW_DECL_SOCKET
277 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
278 "use gnulib module socket for portability");
283 # if @HAVE_WINSOCK2_H@
284 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
286 # define connect rpl_connect
288 _GL_FUNCDECL_RPL (connect, int,
289 (int fd, const struct sockaddr *addr, socklen_t addrlen)
290 _GL_ARG_NONNULL ((2)));
291 _GL_CXXALIAS_RPL (connect, int,
292 (int fd, const struct sockaddr *addr, socklen_t addrlen));
294 /* Need to cast, because on NonStop Kernel, the third parameter is
296 _GL_CXXALIAS_SYS_CAST (connect, int,
298 const struct sockaddr *addr, socklen_t addrlen));
300 _GL_CXXALIASWARN (connect);
301 #elif @HAVE_WINSOCK2_H@
303 # define connect socket_used_without_requesting_gnulib_module_connect
304 #elif defined GNULIB_POSIXCHECK
306 # if HAVE_RAW_DECL_CONNECT
307 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
308 "use gnulib module connect for portability");
313 # if @HAVE_WINSOCK2_H@
314 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
316 # define accept rpl_accept
318 _GL_FUNCDECL_RPL (accept, int,
319 (int fd, struct sockaddr *addr, socklen_t *addrlen));
320 _GL_CXXALIAS_RPL (accept, int,
321 (int fd, struct sockaddr *addr, socklen_t *addrlen));
323 /* Need to cast, because on Solaris 10 systems, the third parameter is
325 _GL_CXXALIAS_SYS_CAST (accept, int,
326 (int fd, struct sockaddr *addr, socklen_t *addrlen));
328 _GL_CXXALIASWARN (accept);
329 #elif @HAVE_WINSOCK2_H@
331 # define accept accept_used_without_requesting_gnulib_module_accept
332 #elif defined GNULIB_POSIXCHECK
334 # if HAVE_RAW_DECL_ACCEPT
335 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
336 "use gnulib module accept for portability");
341 # if @HAVE_WINSOCK2_H@
342 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
344 # define bind rpl_bind
346 _GL_FUNCDECL_RPL (bind, int,
347 (int fd, const struct sockaddr *addr, socklen_t addrlen)
348 _GL_ARG_NONNULL ((2)));
349 _GL_CXXALIAS_RPL (bind, int,
350 (int fd, const struct sockaddr *addr, socklen_t addrlen));
352 /* Need to cast, because on NonStop Kernel, the third parameter is
354 _GL_CXXALIAS_SYS_CAST (bind, int,
356 const struct sockaddr *addr, socklen_t addrlen));
358 _GL_CXXALIASWARN (bind);
359 #elif @HAVE_WINSOCK2_H@
361 # define bind bind_used_without_requesting_gnulib_module_bind
362 #elif defined GNULIB_POSIXCHECK
364 # if HAVE_RAW_DECL_BIND
365 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
366 "use gnulib module bind for portability");
370 #if @GNULIB_GETPEERNAME@
371 # if @HAVE_WINSOCK2_H@
372 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
374 # define getpeername rpl_getpeername
376 _GL_FUNCDECL_RPL (getpeername, int,
377 (int fd, struct sockaddr *addr, socklen_t *addrlen)
378 _GL_ARG_NONNULL ((2, 3)));
379 _GL_CXXALIAS_RPL (getpeername, int,
380 (int fd, struct sockaddr *addr, socklen_t *addrlen));
382 /* Need to cast, because on Solaris 10 systems, the third parameter is
384 _GL_CXXALIAS_SYS_CAST (getpeername, int,
385 (int fd, struct sockaddr *addr, socklen_t *addrlen));
387 _GL_CXXALIASWARN (getpeername);
388 #elif @HAVE_WINSOCK2_H@
390 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
391 #elif defined GNULIB_POSIXCHECK
393 # if HAVE_RAW_DECL_GETPEERNAME
394 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
395 "use gnulib module getpeername for portability");
399 #if @GNULIB_GETSOCKNAME@
400 # if @HAVE_WINSOCK2_H@
401 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
403 # define getsockname rpl_getsockname
405 _GL_FUNCDECL_RPL (getsockname, int,
406 (int fd, struct sockaddr *addr, socklen_t *addrlen)
407 _GL_ARG_NONNULL ((2, 3)));
408 _GL_CXXALIAS_RPL (getsockname, int,
409 (int fd, struct sockaddr *addr, socklen_t *addrlen));
411 /* Need to cast, because on Solaris 10 systems, the third parameter is
413 _GL_CXXALIAS_SYS_CAST (getsockname, int,
414 (int fd, struct sockaddr *addr, socklen_t *addrlen));
416 _GL_CXXALIASWARN (getsockname);
417 #elif @HAVE_WINSOCK2_H@
419 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
420 #elif defined GNULIB_POSIXCHECK
422 # if HAVE_RAW_DECL_GETSOCKNAME
423 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
424 "use gnulib module getsockname for portability");
428 #if @GNULIB_GETSOCKOPT@
429 # if @HAVE_WINSOCK2_H@
430 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
432 # define getsockopt rpl_getsockopt
434 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
435 void *optval, socklen_t *optlen)
436 _GL_ARG_NONNULL ((4, 5)));
437 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
438 void *optval, socklen_t *optlen));
440 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
442 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
443 void *optval, socklen_t *optlen));
445 _GL_CXXALIASWARN (getsockopt);
446 #elif @HAVE_WINSOCK2_H@
448 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
449 #elif defined GNULIB_POSIXCHECK
451 # if HAVE_RAW_DECL_GETSOCKOPT
452 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
453 "use gnulib module getsockopt for portability");
458 # if @HAVE_WINSOCK2_H@
459 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
461 # define listen rpl_listen
463 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
464 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
466 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
468 _GL_CXXALIASWARN (listen);
469 #elif @HAVE_WINSOCK2_H@
471 # define listen listen_used_without_requesting_gnulib_module_listen
472 #elif defined GNULIB_POSIXCHECK
474 # if HAVE_RAW_DECL_LISTEN
475 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
476 "use gnulib module listen for portability");
481 # if @HAVE_WINSOCK2_H@
482 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
484 # define recv rpl_recv
486 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
487 _GL_ARG_NONNULL ((2)));
488 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
490 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
492 _GL_CXXALIASWARN (recv);
493 #elif @HAVE_WINSOCK2_H@
495 # define recv recv_used_without_requesting_gnulib_module_recv
496 #elif defined GNULIB_POSIXCHECK
498 # if HAVE_RAW_DECL_RECV
499 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
500 "use gnulib module recv for portability");
505 # if @HAVE_WINSOCK2_H@
506 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
508 # define send rpl_send
510 _GL_FUNCDECL_RPL (send, ssize_t,
511 (int fd, const void *buf, size_t len, int flags)
512 _GL_ARG_NONNULL ((2)));
513 _GL_CXXALIAS_RPL (send, ssize_t,
514 (int fd, const void *buf, size_t len, int flags));
516 _GL_CXXALIAS_SYS (send, ssize_t,
517 (int fd, const void *buf, size_t len, int flags));
519 _GL_CXXALIASWARN (send);
520 #elif @HAVE_WINSOCK2_H@
522 # define send send_used_without_requesting_gnulib_module_send
523 #elif defined GNULIB_POSIXCHECK
525 # if HAVE_RAW_DECL_SEND
526 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
527 "use gnulib module send for portability");
531 #if @GNULIB_RECVFROM@
532 # if @HAVE_WINSOCK2_H@
533 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
535 # define recvfrom rpl_recvfrom
537 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
538 (int fd, void *buf, size_t len, int flags,
539 struct sockaddr *from, socklen_t *fromlen)
540 _GL_ARG_NONNULL ((2)));
541 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
542 (int fd, void *buf, size_t len, int flags,
543 struct sockaddr *from, socklen_t *fromlen));
545 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
547 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
548 (int fd, void *buf, size_t len, int flags,
549 struct sockaddr *from, socklen_t *fromlen));
551 _GL_CXXALIASWARN (recvfrom);
552 #elif @HAVE_WINSOCK2_H@
554 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
555 #elif defined GNULIB_POSIXCHECK
557 # if HAVE_RAW_DECL_RECVFROM
558 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
559 "use gnulib module recvfrom for portability");
564 # if @HAVE_WINSOCK2_H@
565 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
567 # define sendto rpl_sendto
569 _GL_FUNCDECL_RPL (sendto, ssize_t,
570 (int fd, const void *buf, size_t len, int flags,
571 const struct sockaddr *to, socklen_t tolen)
572 _GL_ARG_NONNULL ((2)));
573 _GL_CXXALIAS_RPL (sendto, ssize_t,
574 (int fd, const void *buf, size_t len, int flags,
575 const struct sockaddr *to, socklen_t tolen));
577 /* Need to cast, because on NonStop Kernel, the sixth parameter is
579 _GL_CXXALIAS_SYS_CAST (sendto, ssize_t,
580 (int fd, const void *buf, size_t len, int flags,
581 const struct sockaddr *to, socklen_t tolen));
583 _GL_CXXALIASWARN (sendto);
584 #elif @HAVE_WINSOCK2_H@
586 # define sendto sendto_used_without_requesting_gnulib_module_sendto
587 #elif defined GNULIB_POSIXCHECK
589 # if HAVE_RAW_DECL_SENDTO
590 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
591 "use gnulib module sendto for portability");
595 #if @GNULIB_SETSOCKOPT@
596 # if @HAVE_WINSOCK2_H@
597 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
599 # define setsockopt rpl_setsockopt
601 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
602 const void * optval, socklen_t optlen)
603 _GL_ARG_NONNULL ((4)));
604 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
605 const void * optval, socklen_t optlen));
607 /* Need to cast, because on NonStop Kernel, the fifth parameter is
609 _GL_CXXALIAS_SYS_CAST (setsockopt, int,
610 (int fd, int level, int optname,
611 const void * optval, socklen_t optlen));
613 _GL_CXXALIASWARN (setsockopt);
614 #elif @HAVE_WINSOCK2_H@
616 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
617 #elif defined GNULIB_POSIXCHECK
619 # if HAVE_RAW_DECL_SETSOCKOPT
620 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
621 "use gnulib module setsockopt for portability");
625 #if @GNULIB_SHUTDOWN@
626 # if @HAVE_WINSOCK2_H@
627 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
629 # define shutdown rpl_shutdown
631 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
632 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
634 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
636 _GL_CXXALIASWARN (shutdown);
637 #elif @HAVE_WINSOCK2_H@
639 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
640 #elif defined GNULIB_POSIXCHECK
642 # if HAVE_RAW_DECL_SHUTDOWN
643 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
644 "use gnulib module shutdown for portability");
649 /* Accept a connection on a socket, with specific opening flags.
650 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
651 and O_TEXT, O_BINARY (defined in "binary-io.h").
652 See also the Linux man page at
653 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
655 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
656 # define accept4 rpl_accept4
658 _GL_FUNCDECL_RPL (accept4, int,
659 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
661 _GL_CXXALIAS_RPL (accept4, int,
662 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
665 _GL_FUNCDECL_SYS (accept4, int,
666 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
668 _GL_CXXALIAS_SYS (accept4, int,
669 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
672 _GL_CXXALIASWARN (accept4);
673 #elif defined GNULIB_POSIXCHECK
675 # if HAVE_RAW_DECL_ACCEPT4
676 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
677 "use gnulib module accept4 for portability");
681 #endif /* _@GUARD_PREFIX@_SYS_SOCKET_H */
682 #endif /* _@GUARD_PREFIX@_SYS_SOCKET_H */