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
149 # if @HAVE_WINSOCK2_H@
150 # include <winsock2.h>
152 # if @HAVE_WS2TCPIP_H@
153 # include <ws2tcpip.h>
156 /* For shutdown(). */
157 # if !defined SHUT_RD && defined SD_RECEIVE
158 # define SHUT_RD SD_RECEIVE
160 # if !defined SHUT_WR && defined SD_SEND
161 # define SHUT_WR SD_SEND
163 # if !defined SHUT_RDWR && defined SD_BOTH
164 # define SHUT_RDWR SD_BOTH
167 # if @HAVE_WINSOCK2_H@
168 /* Include headers needed by the emulation code. */
169 # include <sys/types.h>
172 # if !GNULIB_defined_socklen_t
173 typedef int socklen_t;
174 # define GNULIB_defined_socklen_t 1
179 /* For struct iovec */
180 # include <sys/uio.h>
182 /* Rudimentary 'struct msghdr'; this works as long as you don't try to
183 access msg_control or msg_controllen. */
186 socklen_t msg_namelen;
187 struct iovec *msg_iov;
194 #if @HAVE_WINSOCK2_H@
196 # if !GNULIB_defined_rpl_fd_isset
198 /* Re-define FD_ISSET to avoid a WSA call while we are not using
201 rpl_fd_isset (SOCKET fd, fd_set * set)
207 for (i = 0; i < set->fd_count; i++)
208 if (set->fd_array[i] == fd)
214 # define GNULIB_defined_rpl_fd_isset 1
218 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
222 /* Wrap everything else to use libc file descriptors for sockets. */
224 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
225 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
227 # define close close_used_without_including_unistd_h
229 _GL_WARN_ON_USE (close,
230 "close() used without including <unistd.h>");
234 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define gethostname gethostname_used_without_including_unistd_h
239 _GL_WARN_ON_USE (gethostname,
240 "gethostname() used without including <unistd.h>");
245 # if @HAVE_WINSOCK2_H@
246 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
248 # define socket rpl_socket
250 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
251 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
253 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
255 _GL_CXXALIASWARN (socket);
256 #elif @HAVE_WINSOCK2_H@
258 # define socket socket_used_without_requesting_gnulib_module_socket
259 #elif defined GNULIB_POSIXCHECK
261 # if HAVE_RAW_DECL_SOCKET
262 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
263 "use gnulib module socket for portability");
268 # if @HAVE_WINSOCK2_H@
269 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
271 # define connect rpl_connect
273 _GL_FUNCDECL_RPL (connect, int,
274 (int fd, const struct sockaddr *addr, socklen_t addrlen)
275 _GL_ARG_NONNULL ((2)));
276 _GL_CXXALIAS_RPL (connect, int,
277 (int fd, const struct sockaddr *addr, socklen_t addrlen));
279 /* Need to cast, because on NonStop Kernel, the third parameter is
281 _GL_CXXALIAS_SYS_CAST (connect, int,
283 const struct sockaddr *addr, socklen_t addrlen));
285 _GL_CXXALIASWARN (connect);
286 #elif @HAVE_WINSOCK2_H@
288 # define connect socket_used_without_requesting_gnulib_module_connect
289 #elif defined GNULIB_POSIXCHECK
291 # if HAVE_RAW_DECL_CONNECT
292 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
293 "use gnulib module connect for portability");
298 # if @HAVE_WINSOCK2_H@
299 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
301 # define accept rpl_accept
303 _GL_FUNCDECL_RPL (accept, int,
304 (int fd, struct sockaddr *addr, socklen_t *addrlen));
305 _GL_CXXALIAS_RPL (accept, int,
306 (int fd, struct sockaddr *addr, socklen_t *addrlen));
308 /* Need to cast, because on Solaris 10 systems, the third parameter is
310 _GL_CXXALIAS_SYS_CAST (accept, int,
311 (int fd, struct sockaddr *addr, socklen_t *addrlen));
313 _GL_CXXALIASWARN (accept);
314 #elif @HAVE_WINSOCK2_H@
316 # define accept accept_used_without_requesting_gnulib_module_accept
317 #elif defined GNULIB_POSIXCHECK
319 # if HAVE_RAW_DECL_ACCEPT
320 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
321 "use gnulib module accept for portability");
326 # if @HAVE_WINSOCK2_H@
327 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
329 # define bind rpl_bind
331 _GL_FUNCDECL_RPL (bind, int,
332 (int fd, const struct sockaddr *addr, socklen_t addrlen)
333 _GL_ARG_NONNULL ((2)));
334 _GL_CXXALIAS_RPL (bind, int,
335 (int fd, const struct sockaddr *addr, socklen_t addrlen));
337 /* Need to cast, because on NonStop Kernel, the third parameter is
339 _GL_CXXALIAS_SYS_CAST (bind, int,
341 const struct sockaddr *addr, socklen_t addrlen));
343 _GL_CXXALIASWARN (bind);
344 #elif @HAVE_WINSOCK2_H@
346 # define bind bind_used_without_requesting_gnulib_module_bind
347 #elif defined GNULIB_POSIXCHECK
349 # if HAVE_RAW_DECL_BIND
350 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
351 "use gnulib module bind for portability");
355 #if @GNULIB_GETPEERNAME@
356 # if @HAVE_WINSOCK2_H@
357 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
359 # define getpeername rpl_getpeername
361 _GL_FUNCDECL_RPL (getpeername, int,
362 (int fd, struct sockaddr *addr, socklen_t *addrlen)
363 _GL_ARG_NONNULL ((2, 3)));
364 _GL_CXXALIAS_RPL (getpeername, int,
365 (int fd, struct sockaddr *addr, socklen_t *addrlen));
367 /* Need to cast, because on Solaris 10 systems, the third parameter is
369 _GL_CXXALIAS_SYS_CAST (getpeername, int,
370 (int fd, struct sockaddr *addr, socklen_t *addrlen));
372 _GL_CXXALIASWARN (getpeername);
373 #elif @HAVE_WINSOCK2_H@
375 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
376 #elif defined GNULIB_POSIXCHECK
378 # if HAVE_RAW_DECL_GETPEERNAME
379 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
380 "use gnulib module getpeername for portability");
384 #if @GNULIB_GETSOCKNAME@
385 # if @HAVE_WINSOCK2_H@
386 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
388 # define getsockname rpl_getsockname
390 _GL_FUNCDECL_RPL (getsockname, int,
391 (int fd, struct sockaddr *addr, socklen_t *addrlen)
392 _GL_ARG_NONNULL ((2, 3)));
393 _GL_CXXALIAS_RPL (getsockname, int,
394 (int fd, struct sockaddr *addr, socklen_t *addrlen));
396 /* Need to cast, because on Solaris 10 systems, the third parameter is
398 _GL_CXXALIAS_SYS_CAST (getsockname, int,
399 (int fd, struct sockaddr *addr, socklen_t *addrlen));
401 _GL_CXXALIASWARN (getsockname);
402 #elif @HAVE_WINSOCK2_H@
404 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
405 #elif defined GNULIB_POSIXCHECK
407 # if HAVE_RAW_DECL_GETSOCKNAME
408 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
409 "use gnulib module getsockname for portability");
413 #if @GNULIB_GETSOCKOPT@
414 # if @HAVE_WINSOCK2_H@
415 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
417 # define getsockopt rpl_getsockopt
419 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
420 void *optval, socklen_t *optlen)
421 _GL_ARG_NONNULL ((4, 5)));
422 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
423 void *optval, socklen_t *optlen));
425 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
427 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
428 void *optval, socklen_t *optlen));
430 _GL_CXXALIASWARN (getsockopt);
431 #elif @HAVE_WINSOCK2_H@
433 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
434 #elif defined GNULIB_POSIXCHECK
436 # if HAVE_RAW_DECL_GETSOCKOPT
437 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
438 "use gnulib module getsockopt for portability");
443 # if @HAVE_WINSOCK2_H@
444 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
446 # define listen rpl_listen
448 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
449 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
451 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
453 _GL_CXXALIASWARN (listen);
454 #elif @HAVE_WINSOCK2_H@
456 # define listen listen_used_without_requesting_gnulib_module_listen
457 #elif defined GNULIB_POSIXCHECK
459 # if HAVE_RAW_DECL_LISTEN
460 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
461 "use gnulib module listen for portability");
466 # if @HAVE_WINSOCK2_H@
467 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
469 # define recv rpl_recv
471 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
472 _GL_ARG_NONNULL ((2)));
473 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
475 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
477 _GL_CXXALIASWARN (recv);
478 #elif @HAVE_WINSOCK2_H@
480 # define recv recv_used_without_requesting_gnulib_module_recv
481 #elif defined GNULIB_POSIXCHECK
483 # if HAVE_RAW_DECL_RECV
484 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
485 "use gnulib module recv for portability");
490 # if @HAVE_WINSOCK2_H@
491 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
493 # define send rpl_send
495 _GL_FUNCDECL_RPL (send, ssize_t,
496 (int fd, const void *buf, size_t len, int flags)
497 _GL_ARG_NONNULL ((2)));
498 _GL_CXXALIAS_RPL (send, ssize_t,
499 (int fd, const void *buf, size_t len, int flags));
501 _GL_CXXALIAS_SYS (send, ssize_t,
502 (int fd, const void *buf, size_t len, int flags));
504 _GL_CXXALIASWARN (send);
505 #elif @HAVE_WINSOCK2_H@
507 # define send send_used_without_requesting_gnulib_module_send
508 #elif defined GNULIB_POSIXCHECK
510 # if HAVE_RAW_DECL_SEND
511 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
512 "use gnulib module send for portability");
516 #if @GNULIB_RECVFROM@
517 # if @HAVE_WINSOCK2_H@
518 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
520 # define recvfrom rpl_recvfrom
522 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
523 (int fd, void *buf, size_t len, int flags,
524 struct sockaddr *from, socklen_t *fromlen)
525 _GL_ARG_NONNULL ((2)));
526 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
527 (int fd, void *buf, size_t len, int flags,
528 struct sockaddr *from, socklen_t *fromlen));
530 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
532 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
533 (int fd, void *buf, size_t len, int flags,
534 struct sockaddr *from, socklen_t *fromlen));
536 _GL_CXXALIASWARN (recvfrom);
537 #elif @HAVE_WINSOCK2_H@
539 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
540 #elif defined GNULIB_POSIXCHECK
542 # if HAVE_RAW_DECL_RECVFROM
543 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
544 "use gnulib module recvfrom for portability");
549 # if @HAVE_WINSOCK2_H@
550 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
552 # define sendto rpl_sendto
554 _GL_FUNCDECL_RPL (sendto, ssize_t,
555 (int fd, const void *buf, size_t len, int flags,
556 const struct sockaddr *to, socklen_t tolen)
557 _GL_ARG_NONNULL ((2)));
558 _GL_CXXALIAS_RPL (sendto, ssize_t,
559 (int fd, const void *buf, size_t len, int flags,
560 const struct sockaddr *to, socklen_t tolen));
562 /* Need to cast, because on NonStop Kernel, the sixth parameter is
564 _GL_CXXALIAS_SYS_CAST (sendto, ssize_t,
565 (int fd, const void *buf, size_t len, int flags,
566 const struct sockaddr *to, socklen_t tolen));
568 _GL_CXXALIASWARN (sendto);
569 #elif @HAVE_WINSOCK2_H@
571 # define sendto sendto_used_without_requesting_gnulib_module_sendto
572 #elif defined GNULIB_POSIXCHECK
574 # if HAVE_RAW_DECL_SENDTO
575 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
576 "use gnulib module sendto for portability");
580 #if @GNULIB_SETSOCKOPT@
581 # if @HAVE_WINSOCK2_H@
582 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
584 # define setsockopt rpl_setsockopt
586 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
587 const void * optval, socklen_t optlen)
588 _GL_ARG_NONNULL ((4)));
589 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
590 const void * optval, socklen_t optlen));
592 /* Need to cast, because on NonStop Kernel, the fifth parameter is
594 _GL_CXXALIAS_SYS_CAST (setsockopt, int,
595 (int fd, int level, int optname,
596 const void * optval, socklen_t optlen));
598 _GL_CXXALIASWARN (setsockopt);
599 #elif @HAVE_WINSOCK2_H@
601 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
602 #elif defined GNULIB_POSIXCHECK
604 # if HAVE_RAW_DECL_SETSOCKOPT
605 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
606 "use gnulib module setsockopt for portability");
610 #if @GNULIB_SHUTDOWN@
611 # if @HAVE_WINSOCK2_H@
612 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
614 # define shutdown rpl_shutdown
616 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
617 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
619 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
621 _GL_CXXALIASWARN (shutdown);
622 #elif @HAVE_WINSOCK2_H@
624 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
625 #elif defined GNULIB_POSIXCHECK
627 # if HAVE_RAW_DECL_SHUTDOWN
628 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
629 "use gnulib module shutdown for portability");
633 #if @HAVE_WINSOCK2_H@
634 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
636 # define select select_used_without_including_sys_select_h
638 _GL_WARN_ON_USE (select,
639 "select() used without including <sys/select.h>");
644 /* Accept a connection on a socket, with specific opening flags.
645 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
646 and O_TEXT, O_BINARY (defined in "binary-io.h").
647 See also the Linux man page at
648 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
650 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
651 # define accept4 rpl_accept4
653 _GL_FUNCDECL_RPL (accept4, int,
654 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
656 _GL_CXXALIAS_RPL (accept4, int,
657 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
660 _GL_FUNCDECL_SYS (accept4, int,
661 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
663 _GL_CXXALIAS_SYS (accept4, int,
664 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
667 _GL_CXXALIASWARN (accept4);
668 #elif defined GNULIB_POSIXCHECK
670 # if HAVE_RAW_DECL_ACCEPT4
671 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
672 "use gnulib module accept4 for portability");
676 #endif /* _GL_SYS_SOCKET_H */
677 #endif /* _GL_SYS_SOCKET_H */