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 /* 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 /* Need to cast, because on NonStop Kernel, the third parameter is
250 _GL_CXXALIAS_SYS_CAST (connect
, int,
252 const struct sockaddr
*addr
, socklen_t addrlen
));
254 _GL_CXXALIASWARN (connect
);
255 #elif @HAVE_WINSOCK2_H@
257 # define connect socket_used_without_requesting_gnulib_module_connect
258 #elif defined GNULIB_POSIXCHECK
260 # if HAVE_RAW_DECL_CONNECT
261 _GL_WARN_ON_USE (connect
, "connect is not always POSIX compliant - "
262 "use gnulib module connect for portability");
267 # if @HAVE_WINSOCK2_H@
268 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
270 # define accept rpl_accept
272 _GL_FUNCDECL_RPL (accept
, int,
273 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
274 _GL_CXXALIAS_RPL (accept
, int,
275 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
277 /* Need to cast, because on Solaris 10 systems, the third parameter is
279 _GL_CXXALIAS_SYS_CAST (accept
, int,
280 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
282 _GL_CXXALIASWARN (accept
);
283 #elif @HAVE_WINSOCK2_H@
285 # define accept accept_used_without_requesting_gnulib_module_accept
286 #elif defined GNULIB_POSIXCHECK
288 # if HAVE_RAW_DECL_ACCEPT
289 _GL_WARN_ON_USE (accept
, "accept is not always POSIX compliant - "
290 "use gnulib module accept for portability");
295 # if @HAVE_WINSOCK2_H@
296 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
298 # define bind rpl_bind
300 _GL_FUNCDECL_RPL (bind
, int,
301 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
)
302 _GL_ARG_NONNULL ((2)));
303 _GL_CXXALIAS_RPL (bind
, int,
304 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
306 /* Need to cast, because on NonStop Kernel, the third parameter is
308 _GL_CXXALIAS_SYS_CAST (bind
, int,
310 const struct sockaddr
*addr
, socklen_t addrlen
));
312 _GL_CXXALIASWARN (bind
);
313 #elif @HAVE_WINSOCK2_H@
315 # define bind bind_used_without_requesting_gnulib_module_bind
316 #elif defined GNULIB_POSIXCHECK
318 # if HAVE_RAW_DECL_BIND
319 _GL_WARN_ON_USE (bind
, "bind is not always POSIX compliant - "
320 "use gnulib module bind for portability");
324 #if @GNULIB_GETPEERNAME@
325 # if @HAVE_WINSOCK2_H@
326 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
328 # define getpeername rpl_getpeername
330 _GL_FUNCDECL_RPL (getpeername
, int,
331 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
332 _GL_ARG_NONNULL ((2, 3)));
333 _GL_CXXALIAS_RPL (getpeername
, int,
334 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
336 /* Need to cast, because on Solaris 10 systems, the third parameter is
338 _GL_CXXALIAS_SYS_CAST (getpeername
, int,
339 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
341 _GL_CXXALIASWARN (getpeername
);
342 #elif @HAVE_WINSOCK2_H@
344 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
345 #elif defined GNULIB_POSIXCHECK
347 # if HAVE_RAW_DECL_GETPEERNAME
348 _GL_WARN_ON_USE (getpeername
, "getpeername is not always POSIX compliant - "
349 "use gnulib module getpeername for portability");
353 #if @GNULIB_GETSOCKNAME@
354 # if @HAVE_WINSOCK2_H@
355 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
357 # define getsockname rpl_getsockname
359 _GL_FUNCDECL_RPL (getsockname
, int,
360 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
361 _GL_ARG_NONNULL ((2, 3)));
362 _GL_CXXALIAS_RPL (getsockname
, int,
363 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
365 /* Need to cast, because on Solaris 10 systems, the third parameter is
367 _GL_CXXALIAS_SYS_CAST (getsockname
, int,
368 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
370 _GL_CXXALIASWARN (getsockname
);
371 #elif @HAVE_WINSOCK2_H@
373 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
374 #elif defined GNULIB_POSIXCHECK
376 # if HAVE_RAW_DECL_GETSOCKNAME
377 _GL_WARN_ON_USE (getsockname
, "getsockname is not always POSIX compliant - "
378 "use gnulib module getsockname for portability");
382 #if @GNULIB_GETSOCKOPT@
383 # if @HAVE_WINSOCK2_H@
384 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
386 # define getsockopt rpl_getsockopt
388 _GL_FUNCDECL_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
389 void *optval
, socklen_t
*optlen
)
390 _GL_ARG_NONNULL ((4, 5)));
391 _GL_CXXALIAS_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
392 void *optval
, socklen_t
*optlen
));
394 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
396 _GL_CXXALIAS_SYS_CAST (getsockopt
, int, (int fd
, int level
, int optname
,
397 void *optval
, socklen_t
*optlen
));
399 _GL_CXXALIASWARN (getsockopt
);
400 #elif @HAVE_WINSOCK2_H@
402 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
403 #elif defined GNULIB_POSIXCHECK
405 # if HAVE_RAW_DECL_GETSOCKOPT
406 _GL_WARN_ON_USE (getsockopt
, "getsockopt is not always POSIX compliant - "
407 "use gnulib module getsockopt for portability");
412 # if @HAVE_WINSOCK2_H@
413 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
415 # define listen rpl_listen
417 _GL_FUNCDECL_RPL (listen
, int, (int fd
, int backlog
));
418 _GL_CXXALIAS_RPL (listen
, int, (int fd
, int backlog
));
420 _GL_CXXALIAS_SYS (listen
, int, (int fd
, int backlog
));
422 _GL_CXXALIASWARN (listen
);
423 #elif @HAVE_WINSOCK2_H@
425 # define listen listen_used_without_requesting_gnulib_module_listen
426 #elif defined GNULIB_POSIXCHECK
428 # if HAVE_RAW_DECL_LISTEN
429 _GL_WARN_ON_USE (listen
, "listen is not always POSIX compliant - "
430 "use gnulib module listen for portability");
435 # if @HAVE_WINSOCK2_H@
436 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
438 # define recv rpl_recv
440 _GL_FUNCDECL_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
)
441 _GL_ARG_NONNULL ((2)));
442 _GL_CXXALIAS_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
444 _GL_CXXALIAS_SYS (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
446 _GL_CXXALIASWARN (recv
);
447 #elif @HAVE_WINSOCK2_H@
449 # define recv recv_used_without_requesting_gnulib_module_recv
450 #elif defined GNULIB_POSIXCHECK
452 # if HAVE_RAW_DECL_RECV
453 _GL_WARN_ON_USE (recv
, "recv is not always POSIX compliant - "
454 "use gnulib module recv for portability");
459 # if @HAVE_WINSOCK2_H@
460 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
462 # define send rpl_send
464 _GL_FUNCDECL_RPL (send
, ssize_t
,
465 (int fd
, const void *buf
, size_t len
, int flags
)
466 _GL_ARG_NONNULL ((2)));
467 _GL_CXXALIAS_RPL (send
, ssize_t
,
468 (int fd
, const void *buf
, size_t len
, int flags
));
470 _GL_CXXALIAS_SYS (send
, ssize_t
,
471 (int fd
, const void *buf
, size_t len
, int flags
));
473 _GL_CXXALIASWARN (send
);
474 #elif @HAVE_WINSOCK2_H@
476 # define send send_used_without_requesting_gnulib_module_send
477 #elif defined GNULIB_POSIXCHECK
479 # if HAVE_RAW_DECL_SEND
480 _GL_WARN_ON_USE (send
, "send is not always POSIX compliant - "
481 "use gnulib module send for portability");
485 #if @GNULIB_RECVFROM@
486 # if @HAVE_WINSOCK2_H@
487 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
489 # define recvfrom rpl_recvfrom
491 _GL_FUNCDECL_RPL (recvfrom
, ssize_t
,
492 (int fd
, void *buf
, size_t len
, int flags
,
493 struct sockaddr
*from
, socklen_t
*fromlen
)
494 _GL_ARG_NONNULL ((2)));
495 _GL_CXXALIAS_RPL (recvfrom
, ssize_t
,
496 (int fd
, void *buf
, size_t len
, int flags
,
497 struct sockaddr
*from
, socklen_t
*fromlen
));
499 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
501 _GL_CXXALIAS_SYS_CAST (recvfrom
, ssize_t
,
502 (int fd
, void *buf
, size_t len
, int flags
,
503 struct sockaddr
*from
, socklen_t
*fromlen
));
505 _GL_CXXALIASWARN (recvfrom
);
506 #elif @HAVE_WINSOCK2_H@
508 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
509 #elif defined GNULIB_POSIXCHECK
511 # if HAVE_RAW_DECL_RECVFROM
512 _GL_WARN_ON_USE (recvfrom
, "recvfrom is not always POSIX compliant - "
513 "use gnulib module recvfrom for portability");
518 # if @HAVE_WINSOCK2_H@
519 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
521 # define sendto rpl_sendto
523 _GL_FUNCDECL_RPL (sendto
, ssize_t
,
524 (int fd
, const void *buf
, size_t len
, int flags
,
525 const struct sockaddr
*to
, socklen_t tolen
)
526 _GL_ARG_NONNULL ((2)));
527 _GL_CXXALIAS_RPL (sendto
, ssize_t
,
528 (int fd
, const void *buf
, size_t len
, int flags
,
529 const struct sockaddr
*to
, socklen_t tolen
));
531 /* Need to cast, because on NonStop Kernel, the sixth parameter is
533 _GL_CXXALIAS_SYS_CAST (sendto
, ssize_t
,
534 (int fd
, const void *buf
, size_t len
, int flags
,
535 const struct sockaddr
*to
, socklen_t tolen
));
537 _GL_CXXALIASWARN (sendto
);
538 #elif @HAVE_WINSOCK2_H@
540 # define sendto sendto_used_without_requesting_gnulib_module_sendto
541 #elif defined GNULIB_POSIXCHECK
543 # if HAVE_RAW_DECL_SENDTO
544 _GL_WARN_ON_USE (sendto
, "sendto is not always POSIX compliant - "
545 "use gnulib module sendto for portability");
549 #if @GNULIB_SETSOCKOPT@
550 # if @HAVE_WINSOCK2_H@
551 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
553 # define setsockopt rpl_setsockopt
555 _GL_FUNCDECL_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
556 const void * optval
, socklen_t optlen
)
557 _GL_ARG_NONNULL ((4)));
558 _GL_CXXALIAS_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
559 const void * optval
, socklen_t optlen
));
561 /* Need to cast, because on NonStop Kernel, the fifth parameter is
563 _GL_CXXALIAS_SYS_CAST (setsockopt
, int,
564 (int fd
, int level
, int optname
,
565 const void * optval
, socklen_t optlen
));
567 _GL_CXXALIASWARN (setsockopt
);
568 #elif @HAVE_WINSOCK2_H@
570 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
571 #elif defined GNULIB_POSIXCHECK
573 # if HAVE_RAW_DECL_SETSOCKOPT
574 _GL_WARN_ON_USE (setsockopt
, "setsockopt is not always POSIX compliant - "
575 "use gnulib module setsockopt for portability");
579 #if @GNULIB_SHUTDOWN@
580 # if @HAVE_WINSOCK2_H@
581 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
583 # define shutdown rpl_shutdown
585 _GL_FUNCDECL_RPL (shutdown
, int, (int fd
, int how
));
586 _GL_CXXALIAS_RPL (shutdown
, int, (int fd
, int how
));
588 _GL_CXXALIAS_SYS (shutdown
, int, (int fd
, int how
));
590 _GL_CXXALIASWARN (shutdown
);
591 #elif @HAVE_WINSOCK2_H@
593 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
594 #elif defined GNULIB_POSIXCHECK
596 # if HAVE_RAW_DECL_SHUTDOWN
597 _GL_WARN_ON_USE (shutdown
, "shutdown is not always POSIX compliant - "
598 "use gnulib module shutdown for portability");
602 #if @HAVE_WINSOCK2_H@
603 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
605 # define select select_used_without_including_sys_select_h
607 _GL_WARN_ON_USE (select
,
608 "select() used without including <sys/select.h>");
613 /* Accept a connection on a socket, with specific opening flags.
614 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
615 and O_TEXT, O_BINARY (defined in "binary-io.h").
616 See also the Linux man page at
617 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
619 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
620 # define accept4 rpl_accept4
622 _GL_FUNCDECL_RPL (accept4
, int,
623 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
625 _GL_CXXALIAS_RPL (accept4
, int,
626 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
629 _GL_FUNCDECL_SYS (accept4
, int,
630 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
632 _GL_CXXALIAS_SYS (accept4
, int,
633 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
636 _GL_CXXALIASWARN (accept4
);
637 #elif defined GNULIB_POSIXCHECK
639 # if HAVE_RAW_DECL_ACCEPT4
640 _GL_WARN_ON_USE (accept4
, "accept4 is unportable - "
641 "use gnulib module accept4 for portability");
645 #endif /* _GL_SYS_SOCKET_H */
646 #endif /* _GL_SYS_SOCKET_H */