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-2010 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 /* The include_next requires a split double-inclusion guard. */
54 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
56 # undef _GL_ALREADY_INCLUDING_SYS_SOCKET_H
60 #ifndef _GL_SYS_SOCKET_H
61 #define _GL_SYS_SOCKET_H
63 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
65 /* The definition of _GL_ARG_NONNULL is copied here. */
67 /* The definition of _GL_WARN_ON_USE is copied here. */
69 #if !@HAVE_SA_FAMILY_T@
70 typedef unsigned short sa_family_t;
73 #if @HAVE_STRUCT_SOCKADDR_STORAGE@
74 /* Make the 'struct sockaddr_storage' field 'ss_family' visible on AIX 7.1. */
75 # if !@HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY@
77 # define ss_family __ss_family
82 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
83 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
84 # define __ss_aligntype unsigned long int
86 # define _SS_PADSIZE \
87 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
88 ? sizeof (sa_family_t) \
89 : alignof (__ss_aligntype)) \
90 + sizeof (__ss_aligntype)))
92 struct sockaddr_storage
94 sa_family_t ss_family; /* Address family, etc. */
95 __ss_aligntype __ss_align; /* Force desired alignment. */
96 char __ss_padding[_SS_PADSIZE];
100 #if @HAVE_SYS_SOCKET_H@
102 /* A platform that has <sys/socket.h>. */
104 /* For shutdown(). */
105 # if !defined SHUT_RD
108 # if !defined SHUT_WR
111 # if !defined SHUT_RDWR
118 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
121 /* A platform that lacks <sys/socket.h>.
123 Currently only MinGW is supported. See the gnulib manual regarding
124 Windows sockets. MinGW has the header files winsock2.h and
125 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
126 that you can influence which definitions you get by setting the
127 WINVER symbol before including these two files. For example,
128 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
129 symbol is set indiriectly through WINVER). You can set this by
130 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
131 code may not run on older Windows releases then. My Windows 2000
132 box was not able to run the code, for example. The situation is
133 slightly confusing because
134 <http://msdn.microsoft.com/en-us/library/ms738520>
135 suggests that getaddrinfo should be available on all Windows
139 # if @HAVE_WINSOCK2_H@
140 # include <winsock2.h>
142 # if @HAVE_WS2TCPIP_H@
143 # include <ws2tcpip.h>
146 /* For shutdown(). */
147 # if !defined SHUT_RD && defined SD_RECEIVE
148 # define SHUT_RD SD_RECEIVE
150 # if !defined SHUT_WR && defined SD_SEND
151 # define SHUT_WR SD_SEND
153 # if !defined SHUT_RDWR && defined SD_BOTH
154 # define SHUT_RDWR SD_BOTH
157 # if @HAVE_WINSOCK2_H@
158 /* Include headers needed by the emulation code. */
159 # include <sys/types.h>
162 typedef int socklen_t;
168 #if @HAVE_WINSOCK2_H@
170 /* Re-define FD_ISSET to avoid a WSA call while we are not using
173 rpl_fd_isset (SOCKET fd, fd_set * set)
179 for (i = 0; i < set->fd_count; i++)
180 if (set->fd_array[i] == fd)
187 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
191 /* Wrap everything else to use libc file descriptors for sockets. */
193 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
194 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
196 # define close close_used_without_including_unistd_h
198 _GL_WARN_ON_USE (close,
199 "close() used without including <unistd.h>");
203 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
204 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
206 # define gethostname gethostname_used_without_including_unistd_h
208 _GL_WARN_ON_USE (gethostname,
209 "gethostname() used without including <unistd.h>");
214 # if @HAVE_WINSOCK2_H@
215 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
217 # define socket rpl_socket
219 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
220 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
222 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
224 _GL_CXXALIASWARN (socket);
225 #elif @HAVE_WINSOCK2_H@
227 # define socket socket_used_without_requesting_gnulib_module_socket
228 #elif defined GNULIB_POSIXCHECK
230 # if HAVE_RAW_DECL_SOCKET
231 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
232 "use gnulib module socket for portability");
237 # if @HAVE_WINSOCK2_H@
238 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
240 # define connect rpl_connect
242 _GL_FUNCDECL_RPL (connect, int,
243 (int fd, const struct sockaddr *addr, socklen_t addrlen)
244 _GL_ARG_NONNULL ((2)));
245 _GL_CXXALIAS_RPL (connect, int,
246 (int fd, const struct sockaddr *addr, socklen_t addrlen));
248 _GL_CXXALIAS_SYS (connect, int,
249 (int fd, const struct sockaddr *addr, socklen_t addrlen));
251 _GL_CXXALIASWARN (connect);
252 #elif @HAVE_WINSOCK2_H@
254 # define connect socket_used_without_requesting_gnulib_module_connect
255 #elif defined GNULIB_POSIXCHECK
257 # if HAVE_RAW_DECL_CONNECT
258 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
259 "use gnulib module connect for portability");
264 # if @HAVE_WINSOCK2_H@
265 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
267 # define accept rpl_accept
269 _GL_FUNCDECL_RPL (accept, int,
270 (int fd, struct sockaddr *addr, socklen_t *addrlen));
271 _GL_CXXALIAS_RPL (accept, int,
272 (int fd, struct sockaddr *addr, socklen_t *addrlen));
274 /* Need to cast, because on Solaris 10 systems, the third parameter is
276 _GL_CXXALIAS_SYS_CAST (accept, int,
277 (int fd, struct sockaddr *addr, socklen_t *addrlen));
279 _GL_CXXALIASWARN (accept);
280 #elif @HAVE_WINSOCK2_H@
282 # define accept accept_used_without_requesting_gnulib_module_accept
283 #elif defined GNULIB_POSIXCHECK
285 # if HAVE_RAW_DECL_ACCEPT
286 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
287 "use gnulib module accept for portability");
292 # if @HAVE_WINSOCK2_H@
293 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
295 # define bind rpl_bind
297 _GL_FUNCDECL_RPL (bind, int,
298 (int fd, const struct sockaddr *addr, socklen_t addrlen)
299 _GL_ARG_NONNULL ((2)));
300 _GL_CXXALIAS_RPL (bind, int,
301 (int fd, const struct sockaddr *addr, socklen_t addrlen));
303 _GL_CXXALIAS_SYS (bind, int,
304 (int fd, const struct sockaddr *addr, socklen_t addrlen));
306 _GL_CXXALIASWARN (bind);
307 #elif @HAVE_WINSOCK2_H@
309 # define bind bind_used_without_requesting_gnulib_module_bind
310 #elif defined GNULIB_POSIXCHECK
312 # if HAVE_RAW_DECL_BIND
313 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
314 "use gnulib module bind for portability");
318 #if @GNULIB_GETPEERNAME@
319 # if @HAVE_WINSOCK2_H@
320 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
322 # define getpeername rpl_getpeername
324 _GL_FUNCDECL_RPL (getpeername, int,
325 (int fd, struct sockaddr *addr, socklen_t *addrlen)
326 _GL_ARG_NONNULL ((2, 3)));
327 _GL_CXXALIAS_RPL (getpeername, int,
328 (int fd, struct sockaddr *addr, socklen_t *addrlen));
330 /* Need to cast, because on Solaris 10 systems, the third parameter is
332 _GL_CXXALIAS_SYS_CAST (getpeername, int,
333 (int fd, struct sockaddr *addr, socklen_t *addrlen));
335 _GL_CXXALIASWARN (getpeername);
336 #elif @HAVE_WINSOCK2_H@
338 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
339 #elif defined GNULIB_POSIXCHECK
341 # if HAVE_RAW_DECL_GETPEERNAME
342 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
343 "use gnulib module getpeername for portability");
347 #if @GNULIB_GETSOCKNAME@
348 # if @HAVE_WINSOCK2_H@
349 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
351 # define getsockname rpl_getsockname
353 _GL_FUNCDECL_RPL (getsockname, int,
354 (int fd, struct sockaddr *addr, socklen_t *addrlen)
355 _GL_ARG_NONNULL ((2, 3)));
356 _GL_CXXALIAS_RPL (getsockname, int,
357 (int fd, struct sockaddr *addr, socklen_t *addrlen));
359 /* Need to cast, because on Solaris 10 systems, the third parameter is
361 _GL_CXXALIAS_SYS_CAST (getsockname, int,
362 (int fd, struct sockaddr *addr, socklen_t *addrlen));
364 _GL_CXXALIASWARN (getsockname);
365 #elif @HAVE_WINSOCK2_H@
367 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
368 #elif defined GNULIB_POSIXCHECK
370 # if HAVE_RAW_DECL_GETSOCKNAME
371 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
372 "use gnulib module getsockname for portability");
376 #if @GNULIB_GETSOCKOPT@
377 # if @HAVE_WINSOCK2_H@
378 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
380 # define getsockopt rpl_getsockopt
382 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
383 void *optval, socklen_t *optlen)
384 _GL_ARG_NONNULL ((4, 5)));
385 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
386 void *optval, socklen_t *optlen));
388 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
390 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
391 void *optval, socklen_t *optlen));
393 _GL_CXXALIASWARN (getsockopt);
394 #elif @HAVE_WINSOCK2_H@
396 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
397 #elif defined GNULIB_POSIXCHECK
399 # if HAVE_RAW_DECL_GETSOCKOPT
400 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
401 "use gnulib module getsockopt for portability");
406 # if @HAVE_WINSOCK2_H@
407 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
409 # define listen rpl_listen
411 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
412 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
414 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
416 _GL_CXXALIASWARN (listen);
417 #elif @HAVE_WINSOCK2_H@
419 # define listen listen_used_without_requesting_gnulib_module_listen
420 #elif defined GNULIB_POSIXCHECK
422 # if HAVE_RAW_DECL_LISTEN
423 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
424 "use gnulib module listen for portability");
429 # if @HAVE_WINSOCK2_H@
430 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
432 # define recv rpl_recv
434 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
435 _GL_ARG_NONNULL ((2)));
436 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
438 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
440 _GL_CXXALIASWARN (recv);
441 #elif @HAVE_WINSOCK2_H@
443 # define recv recv_used_without_requesting_gnulib_module_recv
444 #elif defined GNULIB_POSIXCHECK
446 # if HAVE_RAW_DECL_RECV
447 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
448 "use gnulib module recv for portability");
453 # if @HAVE_WINSOCK2_H@
454 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
456 # define send rpl_send
458 _GL_FUNCDECL_RPL (send, ssize_t,
459 (int fd, const void *buf, size_t len, int flags)
460 _GL_ARG_NONNULL ((2)));
461 _GL_CXXALIAS_RPL (send, ssize_t,
462 (int fd, const void *buf, size_t len, int flags));
464 _GL_CXXALIAS_SYS (send, ssize_t,
465 (int fd, const void *buf, size_t len, int flags));
467 _GL_CXXALIASWARN (send);
468 #elif @HAVE_WINSOCK2_H@
470 # define send send_used_without_requesting_gnulib_module_send
471 #elif defined GNULIB_POSIXCHECK
473 # if HAVE_RAW_DECL_SEND
474 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
475 "use gnulib module send for portability");
479 #if @GNULIB_RECVFROM@
480 # if @HAVE_WINSOCK2_H@
481 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
483 # define recvfrom rpl_recvfrom
485 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
486 (int fd, void *buf, size_t len, int flags,
487 struct sockaddr *from, socklen_t *fromlen)
488 _GL_ARG_NONNULL ((2)));
489 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
490 (int fd, void *buf, size_t len, int flags,
491 struct sockaddr *from, socklen_t *fromlen));
493 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
495 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
496 (int fd, void *buf, size_t len, int flags,
497 struct sockaddr *from, socklen_t *fromlen));
499 _GL_CXXALIASWARN (recvfrom);
500 #elif @HAVE_WINSOCK2_H@
502 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
503 #elif defined GNULIB_POSIXCHECK
505 # if HAVE_RAW_DECL_RECVFROM
506 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
507 "use gnulib module recvfrom for portability");
512 # if @HAVE_WINSOCK2_H@
513 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
515 # define sendto rpl_sendto
517 _GL_FUNCDECL_RPL (sendto, ssize_t,
518 (int fd, const void *buf, size_t len, int flags,
519 const struct sockaddr *to, socklen_t tolen)
520 _GL_ARG_NONNULL ((2)));
521 _GL_CXXALIAS_RPL (sendto, ssize_t,
522 (int fd, const void *buf, size_t len, int flags,
523 const struct sockaddr *to, socklen_t tolen));
525 _GL_CXXALIAS_SYS (sendto, ssize_t,
526 (int fd, const void *buf, size_t len, int flags,
527 const struct sockaddr *to, socklen_t tolen));
529 _GL_CXXALIASWARN (sendto);
530 #elif @HAVE_WINSOCK2_H@
532 # define sendto sendto_used_without_requesting_gnulib_module_sendto
533 #elif defined GNULIB_POSIXCHECK
535 # if HAVE_RAW_DECL_SENDTO
536 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
537 "use gnulib module sendto for portability");
541 #if @GNULIB_SETSOCKOPT@
542 # if @HAVE_WINSOCK2_H@
543 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
545 # define setsockopt rpl_setsockopt
547 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
548 const void * optval, socklen_t optlen)
549 _GL_ARG_NONNULL ((4)));
550 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
551 const void * optval, socklen_t optlen));
553 _GL_CXXALIAS_SYS (setsockopt, int, (int fd, int level, int optname,
554 const void * optval, socklen_t optlen));
556 _GL_CXXALIASWARN (setsockopt);
557 #elif @HAVE_WINSOCK2_H@
559 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
560 #elif defined GNULIB_POSIXCHECK
562 # if HAVE_RAW_DECL_SETSOCKOPT
563 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
564 "use gnulib module setsockopt for portability");
568 #if @GNULIB_SHUTDOWN@
569 # if @HAVE_WINSOCK2_H@
570 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
572 # define shutdown rpl_shutdown
574 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
575 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
577 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
579 _GL_CXXALIASWARN (shutdown);
580 #elif @HAVE_WINSOCK2_H@
582 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
583 #elif defined GNULIB_POSIXCHECK
585 # if HAVE_RAW_DECL_SHUTDOWN
586 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
587 "use gnulib module shutdown for portability");
591 #if @HAVE_WINSOCK2_H@
592 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
594 # define select select_used_without_including_sys_select_h
596 _GL_WARN_ON_USE (select,
597 "select() used without including <sys/select.h>");
602 /* Accept a connection on a socket, with specific opening flags.
603 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
604 and O_TEXT, O_BINARY (defined in "binary-io.h").
605 See also the Linux man page at
606 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
608 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
609 # define accept4 rpl_accept4
611 _GL_FUNCDECL_RPL (accept4, int,
612 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
614 _GL_CXXALIAS_RPL (accept4, int,
615 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
618 _GL_FUNCDECL_SYS (accept4, int,
619 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
621 _GL_CXXALIAS_SYS (accept4, int,
622 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
625 _GL_CXXALIASWARN (accept4);
626 #elif defined GNULIB_POSIXCHECK
628 # if HAVE_RAW_DECL_ACCEPT4
629 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
630 "use gnulib module accept4 for portability");
634 #endif /* _GL_SYS_SOCKET_H */
635 #endif /* _GL_SYS_SOCKET_H */