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 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@
30 #if defined _GL_ALREADY_INCLUDING_SYS_SOCKET_H
31 /* Special invocation convention:
32 - On Cygwin 1.5.x we have a sequence of nested includes
33 <sys/socket.h> -> <cygwin/socket.h> -> <asm/socket.h> -> <cygwin/if.h>,
34 and the latter includes <sys/socket.h>. In this situation, the functions
35 are not yet declared, therefore we cannot provide the C++ aliases. */
37 #@INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
40 /* Normal invocation convention. */
42 #ifndef _GL_SYS_SOCKET_H
44 #if @HAVE_SYS_SOCKET_H@
46 # define _GL_ALREADY_INCLUDING_SYS_SOCKET_H
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
186 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
188 # define close close_used_without_including_unistd_h
190 _GL_WARN_ON_USE (close
,
191 "close() used without including <unistd.h>");
195 #if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
196 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
198 # define gethostname gethostname_used_without_including_unistd_h
200 _GL_WARN_ON_USE (gethostname
,
201 "gethostname() used without including <unistd.h>");
206 # if @HAVE_WINSOCK2_H@
207 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
209 # define socket rpl_socket
211 _GL_FUNCDECL_RPL (socket
, int, (int domain
, int type
, int protocol
));
212 _GL_CXXALIAS_RPL (socket
, int, (int domain
, int type
, int protocol
));
214 _GL_CXXALIAS_SYS (socket
, int, (int domain
, int type
, int protocol
));
216 _GL_CXXALIASWARN (socket
);
217 #elif @HAVE_WINSOCK2_H@
219 # define socket socket_used_without_requesting_gnulib_module_socket
220 #elif defined GNULIB_POSIXCHECK
222 # if HAVE_RAW_DECL_SOCKET
223 _GL_WARN_ON_USE (socket
, "socket is not always POSIX compliant - "
224 "use gnulib module socket for portability");
229 # if @HAVE_WINSOCK2_H@
230 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
232 # define connect rpl_connect
234 _GL_FUNCDECL_RPL (connect
, int,
235 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
)
236 _GL_ARG_NONNULL ((2)));
237 _GL_CXXALIAS_RPL (connect
, int,
238 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
240 _GL_CXXALIAS_SYS (connect
, int,
241 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
243 _GL_CXXALIASWARN (connect
);
244 #elif @HAVE_WINSOCK2_H@
246 # define connect socket_used_without_requesting_gnulib_module_connect
247 #elif defined GNULIB_POSIXCHECK
249 # if HAVE_RAW_DECL_CONNECT
250 _GL_WARN_ON_USE (connect
, "connect is not always POSIX compliant - "
251 "use gnulib module connect for portability");
256 # if @HAVE_WINSOCK2_H@
257 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
259 # define accept rpl_accept
261 _GL_FUNCDECL_RPL (accept
, int,
262 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
263 _GL_CXXALIAS_RPL (accept
, int,
264 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
266 /* Need to cast, because on Solaris 10 systems, the third parameter is
268 _GL_CXXALIAS_SYS_CAST (accept
, int,
269 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
271 _GL_CXXALIASWARN (accept
);
272 #elif @HAVE_WINSOCK2_H@
274 # define accept accept_used_without_requesting_gnulib_module_accept
275 #elif defined GNULIB_POSIXCHECK
277 # if HAVE_RAW_DECL_ACCEPT
278 _GL_WARN_ON_USE (accept
, "accept is not always POSIX compliant - "
279 "use gnulib module accept for portability");
284 # if @HAVE_WINSOCK2_H@
285 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
287 # define bind rpl_bind
289 _GL_FUNCDECL_RPL (bind
, int,
290 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
)
291 _GL_ARG_NONNULL ((2)));
292 _GL_CXXALIAS_RPL (bind
, int,
293 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
295 _GL_CXXALIAS_SYS (bind
, int,
296 (int fd
, const struct sockaddr
*addr
, socklen_t addrlen
));
298 _GL_CXXALIASWARN (bind
);
299 #elif @HAVE_WINSOCK2_H@
301 # define bind bind_used_without_requesting_gnulib_module_bind
302 #elif defined GNULIB_POSIXCHECK
304 # if HAVE_RAW_DECL_BIND
305 _GL_WARN_ON_USE (bind
, "bind is not always POSIX compliant - "
306 "use gnulib module bind for portability");
310 #if @GNULIB_GETPEERNAME@
311 # if @HAVE_WINSOCK2_H@
312 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
314 # define getpeername rpl_getpeername
316 _GL_FUNCDECL_RPL (getpeername
, int,
317 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
318 _GL_ARG_NONNULL ((2, 3)));
319 _GL_CXXALIAS_RPL (getpeername
, int,
320 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
322 /* Need to cast, because on Solaris 10 systems, the third parameter is
324 _GL_CXXALIAS_SYS_CAST (getpeername
, int,
325 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
327 _GL_CXXALIASWARN (getpeername
);
328 #elif @HAVE_WINSOCK2_H@
330 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
331 #elif defined GNULIB_POSIXCHECK
333 # if HAVE_RAW_DECL_GETPEERNAME
334 _GL_WARN_ON_USE (getpeername
, "getpeername is not always POSIX compliant - "
335 "use gnulib module getpeername for portability");
339 #if @GNULIB_GETSOCKNAME@
340 # if @HAVE_WINSOCK2_H@
341 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
343 # define getsockname rpl_getsockname
345 _GL_FUNCDECL_RPL (getsockname
, int,
346 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
347 _GL_ARG_NONNULL ((2, 3)));
348 _GL_CXXALIAS_RPL (getsockname
, 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 (getsockname
, int,
354 (int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
));
356 _GL_CXXALIASWARN (getsockname
);
357 #elif @HAVE_WINSOCK2_H@
359 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
360 #elif defined GNULIB_POSIXCHECK
362 # if HAVE_RAW_DECL_GETSOCKNAME
363 _GL_WARN_ON_USE (getsockname
, "getsockname is not always POSIX compliant - "
364 "use gnulib module getsockname for portability");
368 #if @GNULIB_GETSOCKOPT@
369 # if @HAVE_WINSOCK2_H@
370 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
372 # define getsockopt rpl_getsockopt
374 _GL_FUNCDECL_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
375 void *optval
, socklen_t
*optlen
)
376 _GL_ARG_NONNULL ((4, 5)));
377 _GL_CXXALIAS_RPL (getsockopt
, int, (int fd
, int level
, int optname
,
378 void *optval
, socklen_t
*optlen
));
380 /* Need to cast, because on Solaris 10 systems, the fifth parameter is
382 _GL_CXXALIAS_SYS_CAST (getsockopt
, int, (int fd
, int level
, int optname
,
383 void *optval
, socklen_t
*optlen
));
385 _GL_CXXALIASWARN (getsockopt
);
386 #elif @HAVE_WINSOCK2_H@
388 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
389 #elif defined GNULIB_POSIXCHECK
391 # if HAVE_RAW_DECL_GETSOCKOPT
392 _GL_WARN_ON_USE (getsockopt
, "getsockopt is not always POSIX compliant - "
393 "use gnulib module getsockopt for portability");
398 # if @HAVE_WINSOCK2_H@
399 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
401 # define listen rpl_listen
403 _GL_FUNCDECL_RPL (listen
, int, (int fd
, int backlog
));
404 _GL_CXXALIAS_RPL (listen
, int, (int fd
, int backlog
));
406 _GL_CXXALIAS_SYS (listen
, int, (int fd
, int backlog
));
408 _GL_CXXALIASWARN (listen
);
409 #elif @HAVE_WINSOCK2_H@
411 # define listen listen_used_without_requesting_gnulib_module_listen
412 #elif defined GNULIB_POSIXCHECK
414 # if HAVE_RAW_DECL_LISTEN
415 _GL_WARN_ON_USE (listen
, "listen is not always POSIX compliant - "
416 "use gnulib module listen for portability");
421 # if @HAVE_WINSOCK2_H@
422 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
424 # define recv rpl_recv
426 _GL_FUNCDECL_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
)
427 _GL_ARG_NONNULL ((2)));
428 _GL_CXXALIAS_RPL (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
430 _GL_CXXALIAS_SYS (recv
, ssize_t
, (int fd
, void *buf
, size_t len
, int flags
));
432 _GL_CXXALIASWARN (recv
);
433 #elif @HAVE_WINSOCK2_H@
435 # define recv recv_used_without_requesting_gnulib_module_recv
436 #elif defined GNULIB_POSIXCHECK
438 # if HAVE_RAW_DECL_RECV
439 _GL_WARN_ON_USE (recv
, "recv is not always POSIX compliant - "
440 "use gnulib module recv for portability");
445 # if @HAVE_WINSOCK2_H@
446 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
448 # define send rpl_send
450 _GL_FUNCDECL_RPL (send
, ssize_t
,
451 (int fd
, const void *buf
, size_t len
, int flags
)
452 _GL_ARG_NONNULL ((2)));
453 _GL_CXXALIAS_RPL (send
, ssize_t
,
454 (int fd
, const void *buf
, size_t len
, int flags
));
456 _GL_CXXALIAS_SYS (send
, ssize_t
,
457 (int fd
, const void *buf
, size_t len
, int flags
));
459 _GL_CXXALIASWARN (send
);
460 #elif @HAVE_WINSOCK2_H@
462 # define send send_used_without_requesting_gnulib_module_send
463 #elif defined GNULIB_POSIXCHECK
465 # if HAVE_RAW_DECL_SEND
466 _GL_WARN_ON_USE (send
, "send is not always POSIX compliant - "
467 "use gnulib module send for portability");
471 #if @GNULIB_RECVFROM@
472 # if @HAVE_WINSOCK2_H@
473 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
475 # define recvfrom rpl_recvfrom
477 _GL_FUNCDECL_RPL (recvfrom
, ssize_t
,
478 (int fd
, void *buf
, size_t len
, int flags
,
479 struct sockaddr
*from
, socklen_t
*fromlen
)
480 _GL_ARG_NONNULL ((2)));
481 _GL_CXXALIAS_RPL (recvfrom
, ssize_t
,
482 (int fd
, void *buf
, size_t len
, int flags
,
483 struct sockaddr
*from
, socklen_t
*fromlen
));
485 /* Need to cast, because on Solaris 10 systems, the sixth parameter is
487 _GL_CXXALIAS_SYS_CAST (recvfrom
, ssize_t
,
488 (int fd
, void *buf
, size_t len
, int flags
,
489 struct sockaddr
*from
, socklen_t
*fromlen
));
491 _GL_CXXALIASWARN (recvfrom
);
492 #elif @HAVE_WINSOCK2_H@
494 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
495 #elif defined GNULIB_POSIXCHECK
497 # if HAVE_RAW_DECL_RECVFROM
498 _GL_WARN_ON_USE (recvfrom
, "recvfrom is not always POSIX compliant - "
499 "use gnulib module recvfrom for portability");
504 # if @HAVE_WINSOCK2_H@
505 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
507 # define sendto rpl_sendto
509 _GL_FUNCDECL_RPL (sendto
, ssize_t
,
510 (int fd
, const void *buf
, size_t len
, int flags
,
511 const struct sockaddr
*to
, socklen_t tolen
)
512 _GL_ARG_NONNULL ((2)));
513 _GL_CXXALIAS_RPL (sendto
, ssize_t
,
514 (int fd
, const void *buf
, size_t len
, int flags
,
515 const struct sockaddr
*to
, socklen_t tolen
));
517 _GL_CXXALIAS_SYS (sendto
, ssize_t
,
518 (int fd
, const void *buf
, size_t len
, int flags
,
519 const struct sockaddr
*to
, socklen_t tolen
));
521 _GL_CXXALIASWARN (sendto
);
522 #elif @HAVE_WINSOCK2_H@
524 # define sendto sendto_used_without_requesting_gnulib_module_sendto
525 #elif defined GNULIB_POSIXCHECK
527 # if HAVE_RAW_DECL_SENDTO
528 _GL_WARN_ON_USE (sendto
, "sendto is not always POSIX compliant - "
529 "use gnulib module sendto for portability");
533 #if @GNULIB_SETSOCKOPT@
534 # if @HAVE_WINSOCK2_H@
535 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
537 # define setsockopt rpl_setsockopt
539 _GL_FUNCDECL_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
540 const void * optval
, socklen_t optlen
)
541 _GL_ARG_NONNULL ((4)));
542 _GL_CXXALIAS_RPL (setsockopt
, int, (int fd
, int level
, int optname
,
543 const void * optval
, socklen_t optlen
));
545 _GL_CXXALIAS_SYS (setsockopt
, int, (int fd
, int level
, int optname
,
546 const void * optval
, socklen_t optlen
));
548 _GL_CXXALIASWARN (setsockopt
);
549 #elif @HAVE_WINSOCK2_H@
551 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
552 #elif defined GNULIB_POSIXCHECK
554 # if HAVE_RAW_DECL_SETSOCKOPT
555 _GL_WARN_ON_USE (setsockopt
, "setsockopt is not always POSIX compliant - "
556 "use gnulib module setsockopt for portability");
560 #if @GNULIB_SHUTDOWN@
561 # if @HAVE_WINSOCK2_H@
562 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
564 # define shutdown rpl_shutdown
566 _GL_FUNCDECL_RPL (shutdown
, int, (int fd
, int how
));
567 _GL_CXXALIAS_RPL (shutdown
, int, (int fd
, int how
));
569 _GL_CXXALIAS_SYS (shutdown
, int, (int fd
, int how
));
571 _GL_CXXALIASWARN (shutdown
);
572 #elif @HAVE_WINSOCK2_H@
574 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
575 #elif defined GNULIB_POSIXCHECK
577 # if HAVE_RAW_DECL_SHUTDOWN
578 _GL_WARN_ON_USE (shutdown
, "shutdown is not always POSIX compliant - "
579 "use gnulib module shutdown for portability");
583 #if @HAVE_WINSOCK2_H@
584 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
586 # define select select_used_without_including_sys_select_h
588 _GL_WARN_ON_USE (select
,
589 "select() used without including <sys/select.h>");
594 /* Accept a connection on a socket, with specific opening flags.
595 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
596 and O_TEXT, O_BINARY (defined in "binary-io.h").
597 See also the Linux man page at
598 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
600 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
601 # define accept4 rpl_accept4
603 _GL_FUNCDECL_RPL (accept4
, int,
604 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
606 _GL_CXXALIAS_RPL (accept4
, int,
607 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
610 _GL_FUNCDECL_SYS (accept4
, int,
611 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
613 _GL_CXXALIAS_SYS (accept4
, int,
614 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
617 _GL_CXXALIASWARN (accept4
);
618 #elif defined GNULIB_POSIXCHECK
620 # if HAVE_RAW_DECL_ACCEPT4
621 _GL_WARN_ON_USE (accept4
, "accept4 is unportable - "
622 "use gnulib module accept4 for portability");
626 #endif /* _GL_SYS_SOCKET_H */
627 #endif /* _GL_SYS_SOCKET_H */