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@
75 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
76 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
77 # define __ss_aligntype unsigned long int
79 # define _SS_PADSIZE \
80 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
81 ? sizeof (sa_family_t) \
82 : alignof (__ss_aligntype)) \
83 + sizeof (__ss_aligntype)))
85 struct sockaddr_storage
87 sa_family_t ss_family; /* Address family, etc. */
88 __ss_aligntype __ss_align; /* Force desired alignment. */
89 char __ss_padding[_SS_PADSIZE];
93 #if @HAVE_SYS_SOCKET_H@
95 /* A platform that has <sys/socket.h>. */
101 # if !defined SHUT_WR
104 # if !defined SHUT_RDWR
111 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
114 /* A platform that lacks <sys/socket.h>.
116 Currently only MinGW is supported. See the gnulib manual regarding
117 Windows sockets. MinGW has the header files winsock2.h and
118 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
119 that you can influence which definitions you get by setting the
120 WINVER symbol before including these two files. For example,
121 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
122 symbol is set indiriectly through WINVER). You can set this by
123 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
124 code may not run on older Windows releases then. My Windows 2000
125 box was not able to run the code, for example. The situation is
126 slightly confusing because
127 <http://msdn.microsoft.com/en-us/library/ms738520>
128 suggests that getaddrinfo should be available on all Windows
132 # if @HAVE_WINSOCK2_H@
133 # include <winsock2.h>
135 # if @HAVE_WS2TCPIP_H@
136 # include <ws2tcpip.h>
139 /* For shutdown(). */
140 # if !defined SHUT_RD && defined SD_RECEIVE
141 # define SHUT_RD SD_RECEIVE
143 # if !defined SHUT_WR && defined SD_SEND
144 # define SHUT_WR SD_SEND
146 # if !defined SHUT_RDWR && defined SD_BOTH
147 # define SHUT_RDWR SD_BOTH
150 # if @HAVE_WINSOCK2_H@
151 /* Include headers needed by the emulation code. */
152 # include <sys/types.h>
155 typedef int socklen_t;
161 #if @HAVE_WINSOCK2_H@
163 /* Re-define FD_ISSET to avoid a WSA call while we are not using
166 rpl_fd_isset (SOCKET fd, fd_set * set)
172 for (i = 0; i < set->fd_count; i++)
173 if (set->fd_array[i] == fd)
180 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
184 /* Wrap everything else to use libc file descriptors for sockets. */
186 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
187 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
189 # define close close_used_without_including_unistd_h
191 _GL_WARN_ON_USE (close,
192 "close() used without including <unistd.h>");
196 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
197 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
199 # define gethostname gethostname_used_without_including_unistd_h
201 _GL_WARN_ON_USE (gethostname,
202 "gethostname() used without including <unistd.h>");
207 # if @HAVE_WINSOCK2_H@
208 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
210 # define socket rpl_socket
212 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
213 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
215 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
217 _GL_CXXALIASWARN (socket);
218 #elif @HAVE_WINSOCK2_H@
220 # define socket socket_used_without_requesting_gnulib_module_socket
221 #elif defined GNULIB_POSIXCHECK
223 # if HAVE_RAW_DECL_SOCKET
224 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
225 "use gnulib module socket for portability");
230 # if @HAVE_WINSOCK2_H@
231 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
233 # define connect rpl_connect
235 _GL_FUNCDECL_RPL (connect, int,
236 (int fd, const struct sockaddr *addr, socklen_t addrlen)
237 _GL_ARG_NONNULL ((2)));
238 _GL_CXXALIAS_RPL (connect, int,
239 (int fd, const struct sockaddr *addr, socklen_t addrlen));
241 _GL_CXXALIAS_SYS (connect, int,
242 (int fd, const struct sockaddr *addr, socklen_t addrlen));
244 _GL_CXXALIASWARN (connect);
245 #elif @HAVE_WINSOCK2_H@
247 # define connect socket_used_without_requesting_gnulib_module_connect
248 #elif defined GNULIB_POSIXCHECK
250 # if HAVE_RAW_DECL_CONNECT
251 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
252 "use gnulib module connect for portability");
257 # if @HAVE_WINSOCK2_H@
258 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
260 # define accept rpl_accept
262 _GL_FUNCDECL_RPL (accept, int,
263 (int fd, struct sockaddr *addr, socklen_t *addrlen));
264 _GL_CXXALIAS_RPL (accept, int,
265 (int fd, struct sockaddr *addr, socklen_t *addrlen));
267 /* Need to cast, because on Solaris 10 systems, the third parameter is
269 _GL_CXXALIAS_SYS_CAST (accept, int,
270 (int fd, struct sockaddr *addr, socklen_t *addrlen));
272 _GL_CXXALIASWARN (accept);
273 #elif @HAVE_WINSOCK2_H@
275 # define accept accept_used_without_requesting_gnulib_module_accept
276 #elif defined GNULIB_POSIXCHECK
278 # if HAVE_RAW_DECL_ACCEPT
279 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
280 "use gnulib module accept for portability");
285 # if @HAVE_WINSOCK2_H@
286 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
288 # define bind rpl_bind
290 _GL_FUNCDECL_RPL (bind, int,
291 (int fd, const struct sockaddr *addr, socklen_t addrlen)
292 _GL_ARG_NONNULL ((2)));
293 _GL_CXXALIAS_RPL (bind, int,
294 (int fd, const struct sockaddr *addr, socklen_t addrlen));
296 _GL_CXXALIAS_SYS (bind, int,
297 (int fd, const struct sockaddr *addr, socklen_t addrlen));
299 _GL_CXXALIASWARN (bind);
300 #elif @HAVE_WINSOCK2_H@
302 # define bind bind_used_without_requesting_gnulib_module_bind
303 #elif defined GNULIB_POSIXCHECK
305 # if HAVE_RAW_DECL_BIND
306 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
307 "use gnulib module bind for portability");
311 #if @GNULIB_GETPEERNAME@
312 # if @HAVE_WINSOCK2_H@
313 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
315 # define getpeername rpl_getpeername
317 _GL_FUNCDECL_RPL (getpeername, int,
318 (int fd, struct sockaddr *addr, socklen_t *addrlen)
319 _GL_ARG_NONNULL ((2, 3)));
320 _GL_CXXALIAS_RPL (getpeername, 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 (getpeername, int,
326 (int fd, struct sockaddr *addr, socklen_t *addrlen));
328 _GL_CXXALIASWARN (getpeername);
329 #elif @HAVE_WINSOCK2_H@
331 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
332 #elif defined GNULIB_POSIXCHECK
334 # if HAVE_RAW_DECL_GETPEERNAME
335 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
336 "use gnulib module getpeername for portability");
340 #if @GNULIB_GETSOCKNAME@
341 # if @HAVE_WINSOCK2_H@
342 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
344 # define getsockname rpl_getsockname
346 _GL_FUNCDECL_RPL (getsockname, int,
347 (int fd, struct sockaddr *addr, socklen_t *addrlen)
348 _GL_ARG_NONNULL ((2, 3)));
349 _GL_CXXALIAS_RPL (getsockname, int,
350 (int fd, struct sockaddr *addr, socklen_t *addrlen));
352 /* Need to cast, because on Solaris 10 systems, the third parameter is
354 _GL_CXXALIAS_SYS_CAST (getsockname, int,
355 (int fd, struct sockaddr *addr, socklen_t *addrlen));
357 _GL_CXXALIASWARN (getsockname);
358 #elif @HAVE_WINSOCK2_H@
360 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
361 #elif defined GNULIB_POSIXCHECK
363 # if HAVE_RAW_DECL_GETSOCKNAME
364 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
365 "use gnulib module getsockname for portability");
369 #if @GNULIB_GETSOCKOPT@
370 # if @HAVE_WINSOCK2_H@
371 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
373 # define getsockopt rpl_getsockopt
375 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
376 void *optval, socklen_t *optlen)
377 _GL_ARG_NONNULL ((4, 5)));
378 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
379 void *optval, socklen_t *optlen));
381 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
383 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
384 void *optval, socklen_t *optlen));
386 _GL_CXXALIASWARN (getsockopt);
387 #elif @HAVE_WINSOCK2_H@
389 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
390 #elif defined GNULIB_POSIXCHECK
392 # if HAVE_RAW_DECL_GETSOCKOPT
393 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
394 "use gnulib module getsockopt for portability");
399 # if @HAVE_WINSOCK2_H@
400 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
402 # define listen rpl_listen
404 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
405 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
407 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
409 _GL_CXXALIASWARN (listen);
410 #elif @HAVE_WINSOCK2_H@
412 # define listen listen_used_without_requesting_gnulib_module_listen
413 #elif defined GNULIB_POSIXCHECK
415 # if HAVE_RAW_DECL_LISTEN
416 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
417 "use gnulib module listen for portability");
422 # if @HAVE_WINSOCK2_H@
423 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
425 # define recv rpl_recv
427 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
428 _GL_ARG_NONNULL ((2)));
429 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
431 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
433 _GL_CXXALIASWARN (recv);
434 #elif @HAVE_WINSOCK2_H@
436 # define recv recv_used_without_requesting_gnulib_module_recv
437 #elif defined GNULIB_POSIXCHECK
439 # if HAVE_RAW_DECL_RECV
440 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
441 "use gnulib module recv for portability");
446 # if @HAVE_WINSOCK2_H@
447 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
449 # define send rpl_send
451 _GL_FUNCDECL_RPL (send, ssize_t,
452 (int fd, const void *buf, size_t len, int flags)
453 _GL_ARG_NONNULL ((2)));
454 _GL_CXXALIAS_RPL (send, ssize_t,
455 (int fd, const void *buf, size_t len, int flags));
457 _GL_CXXALIAS_SYS (send, ssize_t,
458 (int fd, const void *buf, size_t len, int flags));
460 _GL_CXXALIASWARN (send);
461 #elif @HAVE_WINSOCK2_H@
463 # define send send_used_without_requesting_gnulib_module_send
464 #elif defined GNULIB_POSIXCHECK
466 # if HAVE_RAW_DECL_SEND
467 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
468 "use gnulib module send for portability");
472 #if @GNULIB_RECVFROM@
473 # if @HAVE_WINSOCK2_H@
474 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
476 # define recvfrom rpl_recvfrom
478 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
479 (int fd, void *buf, size_t len, int flags,
480 struct sockaddr *from, socklen_t *fromlen)
481 _GL_ARG_NONNULL ((2)));
482 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
483 (int fd, void *buf, size_t len, int flags,
484 struct sockaddr *from, socklen_t *fromlen));
486 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
488 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
489 (int fd, void *buf, size_t len, int flags,
490 struct sockaddr *from, socklen_t *fromlen));
492 _GL_CXXALIASWARN (recvfrom);
493 #elif @HAVE_WINSOCK2_H@
495 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
496 #elif defined GNULIB_POSIXCHECK
498 # if HAVE_RAW_DECL_RECVFROM
499 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
500 "use gnulib module recvfrom for portability");
505 # if @HAVE_WINSOCK2_H@
506 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
508 # define sendto rpl_sendto
510 _GL_FUNCDECL_RPL (sendto, ssize_t,
511 (int fd, const void *buf, size_t len, int flags,
512 const struct sockaddr *to, socklen_t tolen)
513 _GL_ARG_NONNULL ((2)));
514 _GL_CXXALIAS_RPL (sendto, ssize_t,
515 (int fd, const void *buf, size_t len, int flags,
516 const struct sockaddr *to, socklen_t tolen));
518 _GL_CXXALIAS_SYS (sendto, ssize_t,
519 (int fd, const void *buf, size_t len, int flags,
520 const struct sockaddr *to, socklen_t tolen));
522 _GL_CXXALIASWARN (sendto);
523 #elif @HAVE_WINSOCK2_H@
525 # define sendto sendto_used_without_requesting_gnulib_module_sendto
526 #elif defined GNULIB_POSIXCHECK
528 # if HAVE_RAW_DECL_SENDTO
529 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
530 "use gnulib module sendto for portability");
534 #if @GNULIB_SETSOCKOPT@
535 # if @HAVE_WINSOCK2_H@
536 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
538 # define setsockopt rpl_setsockopt
540 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
541 const void * optval, socklen_t optlen)
542 _GL_ARG_NONNULL ((4)));
543 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
544 const void * optval, socklen_t optlen));
546 _GL_CXXALIAS_SYS (setsockopt, int, (int fd, int level, int optname,
547 const void * optval, socklen_t optlen));
549 _GL_CXXALIASWARN (setsockopt);
550 #elif @HAVE_WINSOCK2_H@
552 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
553 #elif defined GNULIB_POSIXCHECK
555 # if HAVE_RAW_DECL_SETSOCKOPT
556 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
557 "use gnulib module setsockopt for portability");
561 #if @GNULIB_SHUTDOWN@
562 # if @HAVE_WINSOCK2_H@
563 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
565 # define shutdown rpl_shutdown
567 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
568 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
570 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
572 _GL_CXXALIASWARN (shutdown);
573 #elif @HAVE_WINSOCK2_H@
575 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
576 #elif defined GNULIB_POSIXCHECK
578 # if HAVE_RAW_DECL_SHUTDOWN
579 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
580 "use gnulib module shutdown for portability");
584 #if @HAVE_WINSOCK2_H@
585 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
587 # define select select_used_without_including_sys_select_h
589 _GL_WARN_ON_USE (select,
590 "select() used without including <sys/select.h>");
595 /* Accept a connection on a socket, with specific opening flags.
596 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
597 and O_TEXT, O_BINARY (defined in "binary-io.h").
598 See also the Linux man page at
599 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
601 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
602 # define accept4 rpl_accept4
604 _GL_FUNCDECL_RPL (accept4, int,
605 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
607 _GL_CXXALIAS_RPL (accept4, int,
608 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
611 _GL_FUNCDECL_SYS (accept4, int,
612 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
614 _GL_CXXALIAS_SYS (accept4, int,
615 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
618 _GL_CXXALIASWARN (accept4);
619 #elif defined GNULIB_POSIXCHECK
621 # if HAVE_RAW_DECL_ACCEPT4
622 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
623 "use gnulib module accept4 for portability");
627 #endif /* _GL_SYS_SOCKET_H */
628 #endif /* _GL_SYS_SOCKET_H */