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
26 #if defined _GL_ALREADY_INCLUDING_SYS_SOCKET_H
27 /* Special invocation convention:
28 - On Cygwin 1.5.x we have a sequence of nested includes
29 <sys/socket.h> -> <cygwin/socket.h> -> <asm/socket.h> -> <cygwin/if.h>,
30 and the latter includes <sys/socket.h>. In this situation, the functions
31 are not yet declared, therefore we cannot provide the C++ aliases. */
33 #@INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
36 /* Normal invocation convention. */
38 #ifndef _GL_SYS_SOCKET_H
40 #if @HAVE_SYS_SOCKET_H@
42 # define _GL_ALREADY_INCLUDING_SYS_SOCKET_H
45 @PRAGMA_SYSTEM_HEADER@
48 /* On many platforms, <sys/socket.h> assumes prior inclusion of
50 # include <sys/types.h>
52 /* The include_next requires a split double-inclusion guard. */
53 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
55 # undef _GL_ALREADY_INCLUDING_SYS_SOCKET_H
59 #ifndef _GL_SYS_SOCKET_H
60 #define _GL_SYS_SOCKET_H
62 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
64 /* The definition of _GL_ARG_NONNULL is copied here. */
66 /* The definition of _GL_WARN_ON_USE is copied here. */
68 #if !@HAVE_SA_FAMILY_T@
69 typedef unsigned short sa_family_t;
72 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
74 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
75 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
76 # define __ss_aligntype unsigned long int
78 # define _SS_PADSIZE \
79 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
80 ? sizeof (sa_family_t) \
81 : alignof (__ss_aligntype)) \
82 + sizeof (__ss_aligntype)))
84 struct sockaddr_storage
86 sa_family_t ss_family; /* Address family, etc. */
87 __ss_aligntype __ss_align; /* Force desired alignment. */
88 char __ss_padding[_SS_PADSIZE];
92 #if @HAVE_SYS_SOCKET_H@
94 /* A platform that has <sys/socket.h>. */
100 # if !defined SHUT_WR
103 # if !defined SHUT_RDWR
110 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
113 /* A platform that lacks <sys/socket.h>.
115 Currently only MinGW is supported. See the gnulib manual regarding
116 Windows sockets. MinGW has the header files winsock2.h and
117 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
118 that you can influence which definitions you get by setting the
119 WINVER symbol before including these two files. For example,
120 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
121 symbol is set indiriectly through WINVER). You can set this by
122 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
123 code may not run on older Windows releases then. My Windows 2000
124 box was not able to run the code, for example. The situation is
125 slightly confusing because:
126 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
127 suggests that getaddrinfo should be available on all Windows
131 # if @HAVE_WINSOCK2_H@
132 # include <winsock2.h>
134 # if @HAVE_WS2TCPIP_H@
135 # include <ws2tcpip.h>
138 /* For shutdown(). */
139 # if !defined SHUT_RD && defined SD_RECEIVE
140 # define SHUT_RD SD_RECEIVE
142 # if !defined SHUT_WR && defined SD_SEND
143 # define SHUT_WR SD_SEND
145 # if !defined SHUT_RDWR && defined SD_BOTH
146 # define SHUT_RDWR SD_BOTH
149 # if @HAVE_WINSOCK2_H@
150 /* Include headers needed by the emulation code. */
151 # include <sys/types.h>
154 typedef int socklen_t;
160 #if @HAVE_WINSOCK2_H@
162 /* Re-define FD_ISSET to avoid a WSA call while we are not using
165 rpl_fd_isset (SOCKET fd, fd_set * set)
171 for (i = 0; i < set->fd_count; i++)
172 if (set->fd_array[i] == fd)
179 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
183 /* Wrap everything else to use libc file descriptors for sockets. */
185 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
187 # define close close_used_without_including_unistd_h
190 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
192 # define gethostname gethostname_used_without_including_unistd_h
196 # if @HAVE_WINSOCK2_H@
197 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
199 # define socket rpl_socket
201 _GL_FUNCDECL_RPL (socket, int, (int domain, int type, int protocol));
202 _GL_CXXALIAS_RPL (socket, int, (int domain, int type, int protocol));
204 _GL_CXXALIAS_SYS (socket, int, (int domain, int type, int protocol));
206 _GL_CXXALIASWARN (socket);
207 #elif @HAVE_WINSOCK2_H@
209 # define socket socket_used_without_requesting_gnulib_module_socket
210 #elif defined GNULIB_POSIXCHECK
212 # if HAVE_RAW_DECL_SOCKET
213 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
214 "use gnulib module socket for portability");
219 # if @HAVE_WINSOCK2_H@
220 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
222 # define connect rpl_connect
224 _GL_FUNCDECL_RPL (connect, int,
225 (int fd, const struct sockaddr *addr, socklen_t addrlen)
226 _GL_ARG_NONNULL ((2)));
227 _GL_CXXALIAS_RPL (connect, int,
228 (int fd, const struct sockaddr *addr, socklen_t addrlen));
230 _GL_CXXALIAS_SYS (connect, int,
231 (int fd, const struct sockaddr *addr, socklen_t addrlen));
233 _GL_CXXALIASWARN (connect);
234 #elif @HAVE_WINSOCK2_H@
236 # define connect socket_used_without_requesting_gnulib_module_connect
237 #elif defined GNULIB_POSIXCHECK
239 # if HAVE_RAW_DECL_CONNECT
240 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
241 "use gnulib module connect for portability");
246 # if @HAVE_WINSOCK2_H@
247 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
249 # define accept rpl_accept
251 _GL_FUNCDECL_RPL (accept, int,
252 (int fd, struct sockaddr *addr, socklen_t *addrlen));
253 _GL_CXXALIAS_RPL (accept, int,
254 (int fd, struct sockaddr *addr, socklen_t *addrlen));
256 /* Need to cast, because on Solaris 10 systems, the third parameter is
258 _GL_CXXALIAS_SYS_CAST (accept, int,
259 (int fd, struct sockaddr *addr, socklen_t *addrlen));
261 _GL_CXXALIASWARN (accept);
262 #elif @HAVE_WINSOCK2_H@
264 # define accept accept_used_without_requesting_gnulib_module_accept
265 #elif defined GNULIB_POSIXCHECK
267 # if HAVE_RAW_DECL_ACCEPT
268 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
269 "use gnulib module accept for portability");
274 # if @HAVE_WINSOCK2_H@
275 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
277 # define bind rpl_bind
279 _GL_FUNCDECL_RPL (bind, int,
280 (int fd, const struct sockaddr *addr, socklen_t addrlen)
281 _GL_ARG_NONNULL ((2)));
282 _GL_CXXALIAS_RPL (bind, int,
283 (int fd, const struct sockaddr *addr, socklen_t addrlen));
285 _GL_CXXALIAS_SYS (bind, int,
286 (int fd, const struct sockaddr *addr, socklen_t addrlen));
288 _GL_CXXALIASWARN (bind);
289 #elif @HAVE_WINSOCK2_H@
291 # define bind bind_used_without_requesting_gnulib_module_bind
292 #elif defined GNULIB_POSIXCHECK
294 # if HAVE_RAW_DECL_BIND
295 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
296 "use gnulib module bind for portability");
300 #if @GNULIB_GETPEERNAME@
301 # if @HAVE_WINSOCK2_H@
302 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 # define getpeername rpl_getpeername
306 _GL_FUNCDECL_RPL (getpeername, int,
307 (int fd, struct sockaddr *addr, socklen_t *addrlen)
308 _GL_ARG_NONNULL ((2, 3)));
309 _GL_CXXALIAS_RPL (getpeername, int,
310 (int fd, struct sockaddr *addr, socklen_t *addrlen));
312 /* Need to cast, because on Solaris 10 systems, the third parameter is
314 _GL_CXXALIAS_SYS_CAST (getpeername, int,
315 (int fd, struct sockaddr *addr, socklen_t *addrlen));
317 _GL_CXXALIASWARN (getpeername);
318 #elif @HAVE_WINSOCK2_H@
320 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
321 #elif defined GNULIB_POSIXCHECK
323 # if HAVE_RAW_DECL_GETPEERNAME
324 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
325 "use gnulib module getpeername for portability");
329 #if @GNULIB_GETSOCKNAME@
330 # if @HAVE_WINSOCK2_H@
331 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
333 # define getsockname rpl_getsockname
335 _GL_FUNCDECL_RPL (getsockname, int,
336 (int fd, struct sockaddr *addr, socklen_t *addrlen)
337 _GL_ARG_NONNULL ((2, 3)));
338 _GL_CXXALIAS_RPL (getsockname, int,
339 (int fd, struct sockaddr *addr, socklen_t *addrlen));
341 /* Need to cast, because on Solaris 10 systems, the third parameter is
343 _GL_CXXALIAS_SYS_CAST (getsockname, int,
344 (int fd, struct sockaddr *addr, socklen_t *addrlen));
346 _GL_CXXALIASWARN (getsockname);
347 #elif @HAVE_WINSOCK2_H@
349 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
350 #elif defined GNULIB_POSIXCHECK
352 # if HAVE_RAW_DECL_GETSOCKNAME
353 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
354 "use gnulib module getsockname for portability");
358 #if @GNULIB_GETSOCKOPT@
359 # if @HAVE_WINSOCK2_H@
360 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
362 # define getsockopt rpl_getsockopt
364 _GL_FUNCDECL_RPL (getsockopt, int, (int fd, int level, int optname,
365 void *optval, socklen_t *optlen)
366 _GL_ARG_NONNULL ((4, 5)));
367 _GL_CXXALIAS_RPL (getsockopt, int, (int fd, int level, int optname,
368 void *optval, socklen_t *optlen));
370 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
372 _GL_CXXALIAS_SYS_CAST (getsockopt, int, (int fd, int level, int optname,
373 void *optval, socklen_t *optlen));
375 _GL_CXXALIASWARN (getsockopt);
376 #elif @HAVE_WINSOCK2_H@
378 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
379 #elif defined GNULIB_POSIXCHECK
381 # if HAVE_RAW_DECL_GETSOCKOPT
382 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
383 "use gnulib module getsockopt for portability");
388 # if @HAVE_WINSOCK2_H@
389 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
391 # define listen rpl_listen
393 _GL_FUNCDECL_RPL (listen, int, (int fd, int backlog));
394 _GL_CXXALIAS_RPL (listen, int, (int fd, int backlog));
396 _GL_CXXALIAS_SYS (listen, int, (int fd, int backlog));
398 _GL_CXXALIASWARN (listen);
399 #elif @HAVE_WINSOCK2_H@
401 # define listen listen_used_without_requesting_gnulib_module_listen
402 #elif defined GNULIB_POSIXCHECK
404 # if HAVE_RAW_DECL_LISTEN
405 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
406 "use gnulib module listen for portability");
411 # if @HAVE_WINSOCK2_H@
412 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
414 # define recv rpl_recv
416 _GL_FUNCDECL_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags)
417 _GL_ARG_NONNULL ((2)));
418 _GL_CXXALIAS_RPL (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
420 _GL_CXXALIAS_SYS (recv, ssize_t, (int fd, void *buf, size_t len, int flags));
422 _GL_CXXALIASWARN (recv);
423 #elif @HAVE_WINSOCK2_H@
425 # define recv recv_used_without_requesting_gnulib_module_recv
426 #elif defined GNULIB_POSIXCHECK
428 # if HAVE_RAW_DECL_RECV
429 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
430 "use gnulib module recv for portability");
435 # if @HAVE_WINSOCK2_H@
436 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
438 # define send rpl_send
440 _GL_FUNCDECL_RPL (send, ssize_t,
441 (int fd, const void *buf, size_t len, int flags)
442 _GL_ARG_NONNULL ((2)));
443 _GL_CXXALIAS_RPL (send, ssize_t,
444 (int fd, const void *buf, size_t len, int flags));
446 _GL_CXXALIAS_SYS (send, ssize_t,
447 (int fd, const void *buf, size_t len, int flags));
449 _GL_CXXALIASWARN (send);
450 #elif @HAVE_WINSOCK2_H@
452 # define send send_used_without_requesting_gnulib_module_send
453 #elif defined GNULIB_POSIXCHECK
455 # if HAVE_RAW_DECL_SEND
456 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
457 "use gnulib module send for portability");
461 #if @GNULIB_RECVFROM@
462 # if @HAVE_WINSOCK2_H@
463 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
465 # define recvfrom rpl_recvfrom
467 _GL_FUNCDECL_RPL (recvfrom, ssize_t,
468 (int fd, void *buf, size_t len, int flags,
469 struct sockaddr *from, socklen_t *fromlen)
470 _GL_ARG_NONNULL ((2)));
471 _GL_CXXALIAS_RPL (recvfrom, ssize_t,
472 (int fd, void *buf, size_t len, int flags,
473 struct sockaddr *from, socklen_t *fromlen));
475 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
477 _GL_CXXALIAS_SYS_CAST (recvfrom, ssize_t,
478 (int fd, void *buf, size_t len, int flags,
479 struct sockaddr *from, socklen_t *fromlen));
481 _GL_CXXALIASWARN (recvfrom);
482 #elif @HAVE_WINSOCK2_H@
484 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
485 #elif defined GNULIB_POSIXCHECK
487 # if HAVE_RAW_DECL_RECVFROM
488 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
489 "use gnulib module recvfrom for portability");
494 # if @HAVE_WINSOCK2_H@
495 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
497 # define sendto rpl_sendto
499 _GL_FUNCDECL_RPL (sendto, ssize_t,
500 (int fd, const void *buf, size_t len, int flags,
501 const struct sockaddr *to, socklen_t tolen)
502 _GL_ARG_NONNULL ((2)));
503 _GL_CXXALIAS_RPL (sendto, ssize_t,
504 (int fd, const void *buf, size_t len, int flags,
505 const struct sockaddr *to, socklen_t tolen));
507 _GL_CXXALIAS_SYS (sendto, ssize_t,
508 (int fd, const void *buf, size_t len, int flags,
509 const struct sockaddr *to, socklen_t tolen));
511 _GL_CXXALIASWARN (sendto);
512 #elif @HAVE_WINSOCK2_H@
514 # define sendto sendto_used_without_requesting_gnulib_module_sendto
515 #elif defined GNULIB_POSIXCHECK
517 # if HAVE_RAW_DECL_SENDTO
518 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
519 "use gnulib module sendto for portability");
523 #if @GNULIB_SETSOCKOPT@
524 # if @HAVE_WINSOCK2_H@
525 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
527 # define setsockopt rpl_setsockopt
529 _GL_FUNCDECL_RPL (setsockopt, int, (int fd, int level, int optname,
530 const void * optval, socklen_t optlen)
531 _GL_ARG_NONNULL ((4)));
532 _GL_CXXALIAS_RPL (setsockopt, int, (int fd, int level, int optname,
533 const void * optval, socklen_t optlen));
535 _GL_CXXALIAS_SYS (setsockopt, int, (int fd, int level, int optname,
536 const void * optval, socklen_t optlen));
538 _GL_CXXALIASWARN (setsockopt);
539 #elif @HAVE_WINSOCK2_H@
541 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
542 #elif defined GNULIB_POSIXCHECK
544 # if HAVE_RAW_DECL_SETSOCKOPT
545 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
546 "use gnulib module setsockopt for portability");
550 #if @GNULIB_SHUTDOWN@
551 # if @HAVE_WINSOCK2_H@
552 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
554 # define shutdown rpl_shutdown
556 _GL_FUNCDECL_RPL (shutdown, int, (int fd, int how));
557 _GL_CXXALIAS_RPL (shutdown, int, (int fd, int how));
559 _GL_CXXALIAS_SYS (shutdown, int, (int fd, int how));
561 _GL_CXXALIASWARN (shutdown);
562 #elif @HAVE_WINSOCK2_H@
564 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
565 #elif defined GNULIB_POSIXCHECK
567 # if HAVE_RAW_DECL_SHUTDOWN
568 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
569 "use gnulib module shutdown for portability");
573 #if @HAVE_WINSOCK2_H@
575 # define select select_used_without_including_sys_select_h
579 /* Accept a connection on a socket, with specific opening flags.
580 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
581 and O_TEXT, O_BINARY (defined in "binary-io.h").
582 See also the Linux man page at
583 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
585 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
586 # define accept4 rpl_accept4
588 _GL_FUNCDECL_RPL (accept4, int,
589 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
591 _GL_CXXALIAS_RPL (accept4, int,
592 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
595 _GL_FUNCDECL_SYS (accept4, int,
596 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
598 _GL_CXXALIAS_SYS (accept4, int,
599 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
602 _GL_CXXALIASWARN (accept4);
603 #elif defined GNULIB_POSIXCHECK
605 # if HAVE_RAW_DECL_ACCEPT4
606 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
607 "use gnulib module accept4 for portability");
611 #endif /* _GL_SYS_SOCKET_H */
612 #endif /* _GL_SYS_SOCKET_H */