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 #ifndef _GL_SYS_SOCKET_H
28 #if @HAVE_SYS_SOCKET_H@
31 @PRAGMA_SYSTEM_HEADER@
34 /* On many platforms, <sys/socket.h> assumes prior inclusion of
36 # include <sys/types.h>
38 /* The include_next requires a split double-inclusion guard. */
39 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
43 #ifndef _GL_SYS_SOCKET_H
44 #define _GL_SYS_SOCKET_H
46 /* The definition of _GL_ARG_NONNULL is copied here. */
48 #if !@HAVE_SA_FAMILY_T@
49 typedef unsigned short sa_family_t;
52 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
54 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
55 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
56 # define __ss_aligntype unsigned long int
58 # define _SS_PADSIZE \
59 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
60 ? sizeof (sa_family_t) \
61 : alignof (__ss_aligntype)) \
62 + sizeof (__ss_aligntype)))
64 struct sockaddr_storage
66 sa_family_t ss_family; /* Address family, etc. */
67 __ss_aligntype __ss_align; /* Force desired alignment. */
68 char __ss_padding[_SS_PADSIZE];
72 #if @HAVE_SYS_SOCKET_H@
74 /* A platform that has <sys/socket.h>. */
83 # if !defined SHUT_RDWR
90 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
93 /* A platform that lacks <sys/socket.h>.
95 Currently only MinGW is supported. See the gnulib manual regarding
96 Windows sockets. MinGW has the header files winsock2.h and
97 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
98 that you can influence which definitions you get by setting the
99 WINVER symbol before including these two files. For example,
100 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
101 symbol is set indiriectly through WINVER). You can set this by
102 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
103 code may not run on older Windows releases then. My Windows 2000
104 box was not able to run the code, for example. The situation is
105 slightly confusing because:
106 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
107 suggests that getaddrinfo should be available on all Windows
111 # if @HAVE_WINSOCK2_H@
112 # include <winsock2.h>
114 # if @HAVE_WS2TCPIP_H@
115 # include <ws2tcpip.h>
118 /* For shutdown(). */
119 # if !defined SHUT_RD && defined SD_RECEIVE
120 # define SHUT_RD SD_RECEIVE
122 # if !defined SHUT_WR && defined SD_SEND
123 # define SHUT_WR SD_SEND
125 # if !defined SHUT_RDWR && defined SD_BOTH
126 # define SHUT_RDWR SD_BOTH
129 /* The definition of _GL_WARN_ON_USE is copied here. */
131 # if @HAVE_WINSOCK2_H@
132 /* Include headers needed by the emulation code. */
133 # include <sys/types.h>
136 typedef int socklen_t;
144 # if @HAVE_WINSOCK2_H@
146 /* Re-define FD_ISSET to avoid a WSA call while we are not using
149 rpl_fd_isset (SOCKET fd, fd_set * set)
155 for (i = 0; i < set->fd_count; i++)
156 if (set->fd_array[i] == fd)
163 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
167 /* Wrap everything else to use libc file descriptors for sockets. */
169 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
171 # define close close_used_without_including_unistd_h
174 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
176 # define gethostname gethostname_used_without_including_unistd_h
180 # if @HAVE_WINSOCK2_H@
182 # define socket rpl_socket
183 extern int rpl_socket (int, int, int protocol);
185 # elif @HAVE_WINSOCK2_H@
187 # define socket socket_used_without_requesting_gnulib_module_socket
188 # elif defined GNULIB_POSIXCHECK
190 # if HAVE_RAW_DECL_SOCKET
191 _GL_WARN_ON_USE (socket, "socket is not always POSIX compliant - "
192 "use gnulib module socket for portability");
196 # if @GNULIB_CONNECT@
197 # if @HAVE_WINSOCK2_H@
199 # define connect rpl_connect
200 extern int rpl_connect (int, const struct sockaddr *, socklen_t)
201 _GL_ARG_NONNULL ((2));
203 # elif @HAVE_WINSOCK2_H@
205 # define connect socket_used_without_requesting_gnulib_module_connect
206 # elif defined GNULIB_POSIXCHECK
208 # if HAVE_RAW_DECL_CONNECT
209 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
210 "use gnulib module connect for portability");
215 # if @HAVE_WINSOCK2_H@
217 # define accept rpl_accept
218 extern int rpl_accept (int, struct sockaddr *, socklen_t *);
220 # elif @HAVE_WINSOCK2_H@
222 # define accept accept_used_without_requesting_gnulib_module_accept
223 # elif defined GNULIB_POSIXCHECK
225 # if HAVE_RAW_DECL_ACCEPT
226 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
227 "use gnulib module accept for portability");
232 # if @HAVE_WINSOCK2_H@
234 # define bind rpl_bind
235 extern int rpl_bind (int, const struct sockaddr *, socklen_t)
236 _GL_ARG_NONNULL ((2));
238 # elif @HAVE_WINSOCK2_H@
240 # define bind bind_used_without_requesting_gnulib_module_bind
241 # elif defined GNULIB_POSIXCHECK
243 # if HAVE_RAW_DECL_BIND
244 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
245 "use gnulib module bind for portability");
249 # if @GNULIB_GETPEERNAME@
250 # if @HAVE_WINSOCK2_H@
252 # define getpeername rpl_getpeername
253 extern int rpl_getpeername (int, struct sockaddr *, socklen_t *)
254 _GL_ARG_NONNULL ((2, 3));
256 # elif @HAVE_WINSOCK2_H@
258 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
259 # elif defined GNULIB_POSIXCHECK
261 # if HAVE_RAW_DECL_GETPEERNAME
262 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
263 "use gnulib module getpeername for portability");
267 # if @GNULIB_GETSOCKNAME@
268 # if @HAVE_WINSOCK2_H@
270 # define getsockname rpl_getsockname
271 extern int rpl_getsockname (int, struct sockaddr *, socklen_t *)
272 _GL_ARG_NONNULL ((2, 3));
274 # elif @HAVE_WINSOCK2_H@
276 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
277 # elif defined GNULIB_POSIXCHECK
279 # if HAVE_RAW_DECL_GETSOCKNAME
280 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
281 "use gnulib module getsockname for portability");
285 # if @GNULIB_GETSOCKOPT@
286 # if @HAVE_WINSOCK2_H@
288 # define getsockopt rpl_getsockopt
289 extern int rpl_getsockopt (int, int, int, void *, socklen_t *)
290 _GL_ARG_NONNULL ((4, 5));
292 # elif @HAVE_WINSOCK2_H@
294 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
295 # elif defined GNULIB_POSIXCHECK
297 # if HAVE_RAW_DECL_GETSOCKOPT
298 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
299 "use gnulib module getsockopt for portability");
304 # if @HAVE_WINSOCK2_H@
306 # define listen rpl_listen
307 extern int rpl_listen (int, int);
309 # elif @HAVE_WINSOCK2_H@
311 # define listen listen_used_without_requesting_gnulib_module_listen
312 # elif defined GNULIB_POSIXCHECK
314 # if HAVE_RAW_DECL_LISTEN
315 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
316 "use gnulib module listen for portability");
321 # if @HAVE_WINSOCK2_H@
323 # define recv rpl_recv
324 extern ssize_t rpl_recv (int, void *, size_t, int) _GL_ARG_NONNULL ((2));
326 # elif @HAVE_WINSOCK2_H@
328 # define recv recv_used_without_requesting_gnulib_module_recv
329 # elif defined GNULIB_POSIXCHECK
331 # if HAVE_RAW_DECL_RECV
332 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
333 "use gnulib module recv for portability");
338 # if @HAVE_WINSOCK2_H@
340 # define send rpl_send
341 extern ssize_t rpl_send (int, const void *, size_t, int) _GL_ARG_NONNULL ((2));
343 # elif @HAVE_WINSOCK2_H@
345 # define send send_used_without_requesting_gnulib_module_send
346 # elif defined GNULIB_POSIXCHECK
348 # if HAVE_RAW_DECL_SEND
349 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
350 "use gnulib module send for portability");
354 # if @GNULIB_RECVFROM@
355 # if @HAVE_WINSOCK2_H@
357 # define recvfrom rpl_recvfrom
358 extern ssize_t rpl_recvfrom (int, void *, size_t, int,
359 struct sockaddr *, socklen_t *)
360 _GL_ARG_NONNULL ((2));
362 # elif @HAVE_WINSOCK2_H@
364 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
365 # elif defined GNULIB_POSIXCHECK
367 # if HAVE_RAW_DECL_RECVFROM
368 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
369 "use gnulib module recvfrom for portability");
374 # if @HAVE_WINSOCK2_H@
376 # define sendto rpl_sendto
377 extern ssize_t rpl_sendto (int, const void *, size_t, int,
378 const struct sockaddr *, socklen_t)
379 _GL_ARG_NONNULL ((2));
381 # elif @HAVE_WINSOCK2_H@
383 # define sendto sendto_used_without_requesting_gnulib_module_sendto
384 # elif defined GNULIB_POSIXCHECK
386 # if HAVE_RAW_DECL_SENDTO
387 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
388 "use gnulib module sendto for portability");
392 # if @GNULIB_SETSOCKOPT@
393 # if @HAVE_WINSOCK2_H@
395 # define setsockopt rpl_setsockopt
396 extern int rpl_setsockopt (int, int, int, const void *, socklen_t)
397 _GL_ARG_NONNULL ((4));
399 # elif @HAVE_WINSOCK2_H@
401 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
402 # elif defined GNULIB_POSIXCHECK
404 # if HAVE_RAW_DECL_SETSOCKOPT
405 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
406 "use gnulib module setsockopt for portability");
410 # if @GNULIB_SHUTDOWN@
411 # if @HAVE_WINSOCK2_H@
413 # define shutdown rpl_shutdown
414 extern int rpl_shutdown (int, int);
416 # elif @HAVE_WINSOCK2_H@
418 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
419 # elif defined GNULIB_POSIXCHECK
421 # if HAVE_RAW_DECL_SHUTDOWN
422 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
423 "use gnulib module shutdown for portability");
427 # if @HAVE_WINSOCK2_H@
429 # define select select_used_without_including_sys_select_h
436 #endif /* HAVE_SYS_SOCKET_H */
443 /* Accept a connection on a socket, with specific opening flags.
444 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
445 and O_TEXT, O_BINARY (defined in "binary-io.h").
446 See also the Linux man page at
447 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
449 # define accept4 rpl_accept4
451 extern int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
453 #elif defined GNULIB_POSIXCHECK
455 # if HAVE_RAW_DECL_ACCEPT4
456 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
457 "use gnulib module accept4 for portability");
465 #endif /* _GL_SYS_SOCKET_H */
466 #endif /* _GL_SYS_SOCKET_H */