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 3, 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 # if !GNULIB_defined_sa_family_t
71 typedef unsigned short sa_family_t
;
72 # define GNULIB_defined_sa_family_t 1
76 #if @HAVE_STRUCT_SOCKADDR_STORAGE@
77 /* Make the 'struct sockaddr_storage' field 'ss_family' visible on AIX 7.1. */
78 # if !@HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY@
80 # define ss_family __ss_family
85 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
86 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
87 # define __ss_aligntype unsigned long int
89 # define _SS_PADSIZE \
90 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
91 ? sizeof (sa_family_t) \
92 : alignof (__ss_aligntype)) \
93 + sizeof (__ss_aligntype)))
95 # if !GNULIB_defined_struct_sockaddr_storage
96 struct sockaddr_storage
98 sa_family_t ss_family
; /* Address family, etc. */
99 __ss_aligntype __ss_align
; /* Force desired alignment. */
100 char __ss_padding
[_SS_PADSIZE
];
102 # define GNULIB_defined_struct_sockaddr_storage 1
107 #if @HAVE_SYS_SOCKET_H@
109 /* A platform that has <sys/socket.h>. */
111 /* For shutdown(). */
112 # if !defined SHUT_RD
115 # if !defined SHUT_WR
118 # if !defined SHUT_RDWR
125 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
128 /* A platform that lacks <sys/socket.h>.
130 Currently only MinGW is supported. See the gnulib manual regarding
131 Windows sockets. MinGW has the header files winsock2.h and
132 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
133 that you can influence which definitions you get by setting the
134 WINVER symbol before including these two files. For example,
135 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
136 symbol is set indiriectly through WINVER). You can set this by
137 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
138 code may not run on older Windows releases then. My Windows 2000
139 box was not able to run the code, for example. The situation is
140 slightly confusing because
141 <http://msdn.microsoft.com/en-us/library/ms738520>
142 suggests that getaddrinfo should be available on all Windows
146 # if @HAVE_WINSOCK2_H@
147 # include <winsock2.h>
149 # if @HAVE_WS2TCPIP_H@
150 # include <ws2tcpip.h>
153 /* For shutdown(). */
154 # if !defined SHUT_RD && defined SD_RECEIVE
155 # define SHUT_RD SD_RECEIVE
157 # if !defined SHUT_WR && defined SD_SEND
158 # define SHUT_WR SD_SEND
160 # if !defined SHUT_RDWR && defined SD_BOTH
161 # define SHUT_RDWR SD_BOTH
164 # if @HAVE_WINSOCK2_H@
165 /* Include headers needed by the emulation code. */
166 # include <sys/types.h>
169 # if !GNULIB_defined_socklen_t
170 typedef int socklen_t
;
171 # define GNULIB_defined_socklen_t 1
178 #if @HAVE_WINSOCK2_H@
180 # if !GNULIB_defined_rpl_fd_isset
182 /* Re-define FD_ISSET to avoid a WSA call while we are not using
185 rpl_fd_isset (SOCKET fd
, fd_set
* set
)
191 for (i
= 0; i
< set
->fd_count
; i
++)
192 if (set
->fd_array
[i
] == fd
)
198 # define GNULIB_defined_rpl_fd_isset 1
202 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
206 /* Wrap everything else to use libc file descriptors for sockets. */
208 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
209 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
211 # define close close_used_without_including_unistd_h
213 _GL_WARN_ON_USE (close
,
214 "close() used without including <unistd.h>");
218 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
219 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
221 # define gethostname gethostname_used_without_including_unistd_h
223 _GL_WARN_ON_USE (gethostname
,
224 "gethostname() used without including <unistd.h>");
229 # if @HAVE_WINSOCK2_H@
230 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
232 # define socket rpl_socket
234 _GL_FUNCDECL_RPL (socket
, int, (int domain
, int type
, int protocol
));
235 _GL_CXXALIAS_RPL (socket
, int, (int domain
, int type
, int protocol
));
237 _GL_CXXALIAS_SYS (socket
, int, (int domain
, int type
, int protocol
));
239 _GL_CXXALIASWARN (socket
);
240 #elif @HAVE_WINSOCK2_H@
242 # define socket socket_used_without_requesting_gnulib_module_socket
243 #elif defined GNULIB_POSIXCHECK
245 # if HAVE_RAW_DECL_SOCKET
246 _GL_WARN_ON_USE (socket
, "socket is not always POSIX compliant - "
247 "use gnulib module socket for portability");
252 # if @HAVE_WINSOCK2_H@
253 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
255 # define connect rpl_connect
257 _GL_FUNCDECL_RPL (connect
, int,
258 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
)
259 _GL_ARG_NONNULL ((2)));
260 _GL_CXXALIAS_RPL (connect
, int,
261 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
263 /* Need to cast, because on NonStop Kernel, the third parameter is
265 _GL_CXXALIAS_SYS_CAST (connect
, int,
267 const struct sockaddr
*addr
, socklen_t addrlen
));
269 _GL_CXXALIASWARN (connect
);
270 #elif @HAVE_WINSOCK2_H@
272 # define connect socket_used_without_requesting_gnulib_module_connect
273 #elif defined GNULIB_POSIXCHECK
275 # if HAVE_RAW_DECL_CONNECT
276 _GL_WARN_ON_USE (connect
, "connect is not always POSIX compliant - "
277 "use gnulib module connect for portability");
282 # if @HAVE_WINSOCK2_H@
283 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
285 # define accept rpl_accept
287 _GL_FUNCDECL_RPL (accept
, int,
288 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
289 _GL_CXXALIAS_RPL (accept
, int,
290 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
292 /* Need to cast, because on Solaris 10 systems, the third parameter is
294 _GL_CXXALIAS_SYS_CAST (accept
, int,
295 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
297 _GL_CXXALIASWARN (accept
);
298 #elif @HAVE_WINSOCK2_H@
300 # define accept accept_used_without_requesting_gnulib_module_accept
301 #elif defined GNULIB_POSIXCHECK
303 # if HAVE_RAW_DECL_ACCEPT
304 _GL_WARN_ON_USE (accept
, "accept is not always POSIX compliant - "
305 "use gnulib module accept for portability");
310 # if @HAVE_WINSOCK2_H@
311 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
313 # define bind rpl_bind
315 _GL_FUNCDECL_RPL (bind
, int,
316 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
)
317 _GL_ARG_NONNULL ((2)));
318 _GL_CXXALIAS_RPL (bind
, int,
319 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
321 /* Need to cast, because on NonStop Kernel, the third parameter is
323 _GL_CXXALIAS_SYS_CAST (bind
, int,
325 const struct sockaddr
*addr
, socklen_t addrlen
));
327 _GL_CXXALIASWARN (bind
);
328 #elif @HAVE_WINSOCK2_H@
330 # define bind bind_used_without_requesting_gnulib_module_bind
331 #elif defined GNULIB_POSIXCHECK
333 # if HAVE_RAW_DECL_BIND
334 _GL_WARN_ON_USE (bind
, "bind is not always POSIX compliant - "
335 "use gnulib module bind for portability");
339 #if @GNULIB_GETPEERNAME@
340 # if @HAVE_WINSOCK2_H@
341 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
343 # define getpeername rpl_getpeername
345 _GL_FUNCDECL_RPL (getpeername
, int,
346 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
347 _GL_ARG_NONNULL ((2, 3)));
348 _GL_CXXALIAS_RPL (getpeername
, int,
349 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
351 /* Need to cast, because on Solaris 10 systems, the third parameter is
353 _GL_CXXALIAS_SYS_CAST (getpeername
, int,
354 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
356 _GL_CXXALIASWARN (getpeername
);
357 #elif @HAVE_WINSOCK2_H@
359 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
360 #elif defined GNULIB_POSIXCHECK
362 # if HAVE_RAW_DECL_GETPEERNAME
363 _GL_WARN_ON_USE (getpeername
, "getpeername is not always POSIX compliant - "
364 "use gnulib module getpeername for portability");
368 #if @GNULIB_GETSOCKNAME@
369 # if @HAVE_WINSOCK2_H@
370 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
372 # define getsockname rpl_getsockname
374 _GL_FUNCDECL_RPL (getsockname
, int,
375 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
376 _GL_ARG_NONNULL ((2, 3)));
377 _GL_CXXALIAS_RPL (getsockname
, int,
378 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
380 /* Need to cast, because on Solaris 10 systems, the third parameter is
382 _GL_CXXALIAS_SYS_CAST (getsockname
, int,
383 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
385 _GL_CXXALIASWARN (getsockname
);
386 #elif @HAVE_WINSOCK2_H@
388 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
389 #elif defined GNULIB_POSIXCHECK
391 # if HAVE_RAW_DECL_GETSOCKNAME
392 _GL_WARN_ON_USE (getsockname
, "getsockname is not always POSIX compliant - "
393 "use gnulib module getsockname for portability");
397 #if @GNULIB_GETSOCKOPT@
398 # if @HAVE_WINSOCK2_H@
399 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
401 # define getsockopt rpl_getsockopt
403 _GL_FUNCDECL_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
404 void *optval
, socklen_t
*optlen
)
405 _GL_ARG_NONNULL ((4, 5)));
406 _GL_CXXALIAS_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
407 void *optval
, socklen_t
*optlen
));
409 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
411 _GL_CXXALIAS_SYS_CAST (getsockopt
, int, (int fd
, int level
, int optname
,
412 void *optval
, socklen_t
*optlen
));
414 _GL_CXXALIASWARN (getsockopt
);
415 #elif @HAVE_WINSOCK2_H@
417 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
418 #elif defined GNULIB_POSIXCHECK
420 # if HAVE_RAW_DECL_GETSOCKOPT
421 _GL_WARN_ON_USE (getsockopt
, "getsockopt is not always POSIX compliant - "
422 "use gnulib module getsockopt for portability");
427 # if @HAVE_WINSOCK2_H@
428 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
430 # define listen rpl_listen
432 _GL_FUNCDECL_RPL (listen
, int, (int fd
, int backlog
));
433 _GL_CXXALIAS_RPL (listen
, int, (int fd
, int backlog
));
435 _GL_CXXALIAS_SYS (listen
, int, (int fd
, int backlog
));
437 _GL_CXXALIASWARN (listen
);
438 #elif @HAVE_WINSOCK2_H@
440 # define listen listen_used_without_requesting_gnulib_module_listen
441 #elif defined GNULIB_POSIXCHECK
443 # if HAVE_RAW_DECL_LISTEN
444 _GL_WARN_ON_USE (listen
, "listen is not always POSIX compliant - "
445 "use gnulib module listen for portability");
450 # if @HAVE_WINSOCK2_H@
451 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
453 # define recv rpl_recv
455 _GL_FUNCDECL_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
)
456 _GL_ARG_NONNULL ((2)));
457 _GL_CXXALIAS_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
459 _GL_CXXALIAS_SYS (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
461 _GL_CXXALIASWARN (recv
);
462 #elif @HAVE_WINSOCK2_H@
464 # define recv recv_used_without_requesting_gnulib_module_recv
465 #elif defined GNULIB_POSIXCHECK
467 # if HAVE_RAW_DECL_RECV
468 _GL_WARN_ON_USE (recv
, "recv is not always POSIX compliant - "
469 "use gnulib module recv for portability");
474 # if @HAVE_WINSOCK2_H@
475 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
477 # define send rpl_send
479 _GL_FUNCDECL_RPL (send
, ssize_t
,
480 (int fd
, const void *buf
, size_t len
, int flags
)
481 _GL_ARG_NONNULL ((2)));
482 _GL_CXXALIAS_RPL (send
, ssize_t
,
483 (int fd
, const void *buf
, size_t len
, int flags
));
485 _GL_CXXALIAS_SYS (send
, ssize_t
,
486 (int fd
, const void *buf
, size_t len
, int flags
));
488 _GL_CXXALIASWARN (send
);
489 #elif @HAVE_WINSOCK2_H@
491 # define send send_used_without_requesting_gnulib_module_send
492 #elif defined GNULIB_POSIXCHECK
494 # if HAVE_RAW_DECL_SEND
495 _GL_WARN_ON_USE (send
, "send is not always POSIX compliant - "
496 "use gnulib module send for portability");
500 #if @GNULIB_RECVFROM@
501 # if @HAVE_WINSOCK2_H@
502 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
504 # define recvfrom rpl_recvfrom
506 _GL_FUNCDECL_RPL (recvfrom
, ssize_t
,
507 (int fd
, void *buf
, size_t len
, int flags
,
508 struct sockaddr
*from
, socklen_t
*fromlen
)
509 _GL_ARG_NONNULL ((2)));
510 _GL_CXXALIAS_RPL (recvfrom
, ssize_t
,
511 (int fd
, void *buf
, size_t len
, int flags
,
512 struct sockaddr
*from
, socklen_t
*fromlen
));
514 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
516 _GL_CXXALIAS_SYS_CAST (recvfrom
, ssize_t
,
517 (int fd
, void *buf
, size_t len
, int flags
,
518 struct sockaddr
*from
, socklen_t
*fromlen
));
520 _GL_CXXALIASWARN (recvfrom
);
521 #elif @HAVE_WINSOCK2_H@
523 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
524 #elif defined GNULIB_POSIXCHECK
526 # if HAVE_RAW_DECL_RECVFROM
527 _GL_WARN_ON_USE (recvfrom
, "recvfrom is not always POSIX compliant - "
528 "use gnulib module recvfrom for portability");
533 # if @HAVE_WINSOCK2_H@
534 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
536 # define sendto rpl_sendto
538 _GL_FUNCDECL_RPL (sendto
, ssize_t
,
539 (int fd
, const void *buf
, size_t len
, int flags
,
540 const struct sockaddr
*to
, socklen_t tolen
)
541 _GL_ARG_NONNULL ((2)));
542 _GL_CXXALIAS_RPL (sendto
, ssize_t
,
543 (int fd
, const void *buf
, size_t len
, int flags
,
544 const struct sockaddr
*to
, socklen_t tolen
));
546 /* Need to cast, because on NonStop Kernel, the sixth parameter is
548 _GL_CXXALIAS_SYS_CAST (sendto
, ssize_t
,
549 (int fd
, const void *buf
, size_t len
, int flags
,
550 const struct sockaddr
*to
, socklen_t tolen
));
552 _GL_CXXALIASWARN (sendto
);
553 #elif @HAVE_WINSOCK2_H@
555 # define sendto sendto_used_without_requesting_gnulib_module_sendto
556 #elif defined GNULIB_POSIXCHECK
558 # if HAVE_RAW_DECL_SENDTO
559 _GL_WARN_ON_USE (sendto
, "sendto is not always POSIX compliant - "
560 "use gnulib module sendto for portability");
564 #if @GNULIB_SETSOCKOPT@
565 # if @HAVE_WINSOCK2_H@
566 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
568 # define setsockopt rpl_setsockopt
570 _GL_FUNCDECL_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
571 const void * optval
, socklen_t optlen
)
572 _GL_ARG_NONNULL ((4)));
573 _GL_CXXALIAS_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
574 const void * optval
, socklen_t optlen
));
576 /* Need to cast, because on NonStop Kernel, the fifth parameter is
578 _GL_CXXALIAS_SYS_CAST (setsockopt
, int,
579 (int fd
, int level
, int optname
,
580 const void * optval
, socklen_t optlen
));
582 _GL_CXXALIASWARN (setsockopt
);
583 #elif @HAVE_WINSOCK2_H@
585 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
586 #elif defined GNULIB_POSIXCHECK
588 # if HAVE_RAW_DECL_SETSOCKOPT
589 _GL_WARN_ON_USE (setsockopt
, "setsockopt is not always POSIX compliant - "
590 "use gnulib module setsockopt for portability");
594 #if @GNULIB_SHUTDOWN@
595 # if @HAVE_WINSOCK2_H@
596 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
598 # define shutdown rpl_shutdown
600 _GL_FUNCDECL_RPL (shutdown
, int, (int fd
, int how
));
601 _GL_CXXALIAS_RPL (shutdown
, int, (int fd
, int how
));
603 _GL_CXXALIAS_SYS (shutdown
, int, (int fd
, int how
));
605 _GL_CXXALIASWARN (shutdown
);
606 #elif @HAVE_WINSOCK2_H@
608 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
609 #elif defined GNULIB_POSIXCHECK
611 # if HAVE_RAW_DECL_SHUTDOWN
612 _GL_WARN_ON_USE (shutdown
, "shutdown is not always POSIX compliant - "
613 "use gnulib module shutdown for portability");
617 #if @HAVE_WINSOCK2_H@
618 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
620 # define select select_used_without_including_sys_select_h
622 _GL_WARN_ON_USE (select
,
623 "select() used without including <sys/select.h>");
628 /* Accept a connection on a socket, with specific opening flags.
629 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
630 and O_TEXT, O_BINARY (defined in "binary-io.h").
631 See also the Linux man page at
632 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
634 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
635 # define accept4 rpl_accept4
637 _GL_FUNCDECL_RPL (accept4
, int,
638 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
640 _GL_CXXALIAS_RPL (accept4
, int,
641 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
644 _GL_FUNCDECL_SYS (accept4
, int,
645 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
647 _GL_CXXALIAS_SYS (accept4
, int,
648 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
651 _GL_CXXALIASWARN (accept4
);
652 #elif defined GNULIB_POSIXCHECK
654 # if HAVE_RAW_DECL_ACCEPT4
655 _GL_WARN_ON_USE (accept4
, "accept4 is unportable - "
656 "use gnulib module accept4 for portability");
660 #endif /* _GL_SYS_SOCKET_H */
661 #endif /* _GL_SYS_SOCKET_H */