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, struct sockaddr *, int) _GL_ARG_NONNULL ((2));
202 # elif @HAVE_WINSOCK2_H@
204 # define connect socket_used_without_requesting_gnulib_module_connect
205 # elif defined GNULIB_POSIXCHECK
207 # if HAVE_RAW_DECL_CONNECT
208 _GL_WARN_ON_USE (connect, "connect is not always POSIX compliant - "
209 "use gnulib module connect for portability");
214 # if @HAVE_WINSOCK2_H@
216 # define accept rpl_accept
217 extern int rpl_accept (int, struct sockaddr *, int *);
219 # elif @HAVE_WINSOCK2_H@
221 # define accept accept_used_without_requesting_gnulib_module_accept
222 # elif defined GNULIB_POSIXCHECK
224 # if HAVE_RAW_DECL_ACCEPT
225 _GL_WARN_ON_USE (accept, "accept is not always POSIX compliant - "
226 "use gnulib module accept for portability");
231 # if @HAVE_WINSOCK2_H@
233 # define bind rpl_bind
234 extern int rpl_bind (int, struct sockaddr *, int) _GL_ARG_NONNULL ((2));
236 # elif @HAVE_WINSOCK2_H@
238 # define bind bind_used_without_requesting_gnulib_module_bind
239 # elif defined GNULIB_POSIXCHECK
241 # if HAVE_RAW_DECL_BIND
242 _GL_WARN_ON_USE (bind, "bind is not always POSIX compliant - "
243 "use gnulib module bind for portability");
247 # if @GNULIB_GETPEERNAME@
248 # if @HAVE_WINSOCK2_H@
250 # define getpeername rpl_getpeername
251 extern int rpl_getpeername (int, struct sockaddr *, int *)
252 _GL_ARG_NONNULL ((2, 3));
254 # elif @HAVE_WINSOCK2_H@
256 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
257 # elif defined GNULIB_POSIXCHECK
259 # if HAVE_RAW_DECL_GETPEERNAME
260 _GL_WARN_ON_USE (getpeername, "getpeername is not always POSIX compliant - "
261 "use gnulib module getpeername for portability");
265 # if @GNULIB_GETSOCKNAME@
266 # if @HAVE_WINSOCK2_H@
268 # define getsockname rpl_getsockname
269 extern int rpl_getsockname (int, struct sockaddr *, int *)
270 _GL_ARG_NONNULL ((2, 3));
272 # elif @HAVE_WINSOCK2_H@
274 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
275 # elif defined GNULIB_POSIXCHECK
277 # if HAVE_RAW_DECL_GETSOCKNAME
278 _GL_WARN_ON_USE (getsockname, "getsockname is not always POSIX compliant - "
279 "use gnulib module getsockname for portability");
283 # if @GNULIB_GETSOCKOPT@
284 # if @HAVE_WINSOCK2_H@
286 # define getsockopt rpl_getsockopt
287 extern int rpl_getsockopt (int, int, int, void *, socklen_t *)
288 _GL_ARG_NONNULL ((4, 5));
290 # elif @HAVE_WINSOCK2_H@
292 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
293 # elif defined GNULIB_POSIXCHECK
295 # if HAVE_RAW_DECL_GETSOCKOPT
296 _GL_WARN_ON_USE (getsockopt, "getsockopt is not always POSIX compliant - "
297 "use gnulib module getsockopt for portability");
302 # if @HAVE_WINSOCK2_H@
304 # define listen rpl_listen
305 extern int rpl_listen (int, int);
307 # elif @HAVE_WINSOCK2_H@
309 # define listen listen_used_without_requesting_gnulib_module_listen
310 # elif defined GNULIB_POSIXCHECK
312 # if HAVE_RAW_DECL_LISTEN
313 _GL_WARN_ON_USE (listen, "listen is not always POSIX compliant - "
314 "use gnulib module listen for portability");
319 # if @HAVE_WINSOCK2_H@
321 # define recv rpl_recv
322 extern int rpl_recv (int, void *, int, int) _GL_ARG_NONNULL ((2));
324 # elif @HAVE_WINSOCK2_H@
326 # define recv recv_used_without_requesting_gnulib_module_recv
327 # elif defined GNULIB_POSIXCHECK
329 # if HAVE_RAW_DECL_RECV
330 _GL_WARN_ON_USE (recv, "recv is not always POSIX compliant - "
331 "use gnulib module recv for portability");
336 # if @HAVE_WINSOCK2_H@
338 # define send rpl_send
339 extern int rpl_send (int, const void *, int, int) _GL_ARG_NONNULL ((2));
341 # elif @HAVE_WINSOCK2_H@
343 # define send send_used_without_requesting_gnulib_module_send
344 # elif defined GNULIB_POSIXCHECK
346 # if HAVE_RAW_DECL_SEND
347 _GL_WARN_ON_USE (send, "send is not always POSIX compliant - "
348 "use gnulib module send for portability");
352 # if @GNULIB_RECVFROM@
353 # if @HAVE_WINSOCK2_H@
355 # define recvfrom rpl_recvfrom
356 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *)
357 _GL_ARG_NONNULL ((2));
359 # elif @HAVE_WINSOCK2_H@
361 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
362 # elif defined GNULIB_POSIXCHECK
364 # if HAVE_RAW_DECL_RECVFROM
365 _GL_WARN_ON_USE (recvfrom, "recvfrom is not always POSIX compliant - "
366 "use gnulib module recvfrom for portability");
371 # if @HAVE_WINSOCK2_H@
373 # define sendto rpl_sendto
374 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int)
375 _GL_ARG_NONNULL ((2));
377 # elif @HAVE_WINSOCK2_H@
379 # define sendto sendto_used_without_requesting_gnulib_module_sendto
380 # elif defined GNULIB_POSIXCHECK
382 # if HAVE_RAW_DECL_SENDTO
383 _GL_WARN_ON_USE (sendto, "sendto is not always POSIX compliant - "
384 "use gnulib module sendto for portability");
388 # if @GNULIB_SETSOCKOPT@
389 # if @HAVE_WINSOCK2_H@
391 # define setsockopt rpl_setsockopt
392 extern int rpl_setsockopt (int, int, int, const void *, socklen_t)
393 _GL_ARG_NONNULL ((4));
395 # elif @HAVE_WINSOCK2_H@
397 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
398 # elif defined GNULIB_POSIXCHECK
400 # if HAVE_RAW_DECL_SETSOCKOPT
401 _GL_WARN_ON_USE (setsockopt, "setsockopt is not always POSIX compliant - "
402 "use gnulib module setsockopt for portability");
406 # if @GNULIB_SHUTDOWN@
407 # if @HAVE_WINSOCK2_H@
409 # define shutdown rpl_shutdown
410 extern int rpl_shutdown (int, int);
412 # elif @HAVE_WINSOCK2_H@
414 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
415 # elif defined GNULIB_POSIXCHECK
417 # if HAVE_RAW_DECL_SHUTDOWN
418 _GL_WARN_ON_USE (shutdown, "shutdown is not always POSIX compliant - "
419 "use gnulib module shutdown for portability");
423 # if @HAVE_WINSOCK2_H@
425 # define select select_used_without_including_sys_select_h
432 #endif /* HAVE_SYS_SOCKET_H */
439 /* Accept a connection on a socket, with specific opening flags.
440 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
441 and O_TEXT, O_BINARY (defined in "binary-io.h").
442 See also the Linux man page at
443 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
445 # define accept4 rpl_accept4
447 extern int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
449 #elif defined GNULIB_POSIXCHECK
451 # if HAVE_RAW_DECL_ACCEPT4
452 _GL_WARN_ON_USE (accept4, "accept4 is unportable - "
453 "use gnulib module accept4 for portability");
461 #endif /* _GL_SYS_SOCKET_H */
462 #endif /* _GL_SYS_SOCKET_H */