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-2009 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 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
48 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
49 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
50 #define __ss_aligntype unsigned long int
52 #define _SS_PADSIZE (_SS_SIZE - (max (sizeof (sa_family_t), \
53 alignof (__ss_aligntype)) \
54 + sizeof (__ss_aligntype)))
56 struct sockaddr_storage
58 sa_family_t ss_family; /* Address family, etc. */
59 __ss_aligntype __ss_align; /* Force desired alignment. */
60 char __ss_padding[_SS_PADSIZE];
64 #if @HAVE_SYS_SOCKET_H@
66 /* A platform that has <sys/socket.h>. */
75 # if !defined SHUT_RDWR
82 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
85 /* A platform that lacks <sys/socket.h>.
87 Currently only MinGW is supported. See the gnulib manual regarding
88 Windows sockets. MinGW has the header files winsock2.h and
89 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
90 that you can influence which definitions you get by setting the
91 WINVER symbol before including these two files. For example,
92 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
93 symbol is set indiriectly through WINVER). You can set this by
94 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
95 code may not run on older Windows releases then. My Windows 2000
96 box was not able to run the code, for example. The situation is
97 slightly confusing because:
98 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
99 suggests that getaddrinfo should be available on all Windows
103 # if @HAVE_WINSOCK2_H@
104 # include <winsock2.h>
106 # if @HAVE_WS2TCPIP_H@
107 # include <ws2tcpip.h>
110 /* For shutdown(). */
111 # if !defined SHUT_RD && defined SD_RECEIVE
112 # define SHUT_RD SD_RECEIVE
114 # if !defined SHUT_WR && defined SD_SEND
115 # define SHUT_WR SD_SEND
117 # if !defined SHUT_RDWR && defined SD_BOTH
118 # define SHUT_RDWR SD_BOTH
121 /* The definition of GL_LINK_WARNING is copied here. */
123 # if @HAVE_WINSOCK2_H@
124 /* Include headers needed by the emulation code. */
125 # include <sys/types.h>
128 typedef int socklen_t;
136 # if @HAVE_WINSOCK2_H@
138 /* Re-define FD_ISSET to avoid a WSA call while we are not using
141 rpl_fd_isset (SOCKET fd, fd_set * set)
147 for (i = 0; i < set->fd_count; i++)
148 if (set->fd_array[i] == fd)
155 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
159 /* Wrap everything else to use libc file descriptors for sockets. */
161 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
163 # define close close_used_without_including_unistd_h
166 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
168 # define gethostname gethostname_used_without_including_unistd_h
172 # if @HAVE_WINSOCK2_H@
174 # define socket rpl_socket
175 extern int rpl_socket (int, int, int protocol);
177 # elif @HAVE_WINSOCK2_H@
179 # define socket socket_used_without_requesting_gnulib_module_socket
180 # elif defined GNULIB_POSIXCHECK
182 # define socket(d,t,p) \
183 (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
184 "use gnulib module socket for portability"), \
188 # if @GNULIB_CONNECT@
189 # if @HAVE_WINSOCK2_H@
191 # define connect rpl_connect
192 extern int rpl_connect (int, struct sockaddr *, int);
194 # elif @HAVE_WINSOCK2_H@
196 # define connect socket_used_without_requesting_gnulib_module_connect
197 # elif defined GNULIB_POSIXCHECK
199 # define connect(s,a,l) \
200 (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
201 "use gnulib module connect for portability"), \
206 # if @HAVE_WINSOCK2_H@
208 # define accept rpl_accept
209 extern int rpl_accept (int, struct sockaddr *, int *);
211 # elif @HAVE_WINSOCK2_H@
213 # define accept accept_used_without_requesting_gnulib_module_accept
214 # elif defined GNULIB_POSIXCHECK
216 # define accept(s,a,l) \
217 (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
218 "use gnulib module accept for portability"), \
223 # if @HAVE_WINSOCK2_H@
225 # define bind rpl_bind
226 extern int rpl_bind (int, struct sockaddr *, int);
228 # elif @HAVE_WINSOCK2_H@
230 # define bind bind_used_without_requesting_gnulib_module_bind
231 # elif defined GNULIB_POSIXCHECK
233 # define bind(s,a,l) \
234 (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
235 "use gnulib module bind for portability"), \
239 # if @GNULIB_GETPEERNAME@
240 # if @HAVE_WINSOCK2_H@
242 # define getpeername rpl_getpeername
243 extern int rpl_getpeername (int, struct sockaddr *, int *);
245 # elif @HAVE_WINSOCK2_H@
247 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
248 # elif defined GNULIB_POSIXCHECK
250 # define getpeername(s,a,l) \
251 (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
252 "use gnulib module getpeername for portability"), \
253 getpeername (s, a, l))
256 # if @GNULIB_GETSOCKNAME@
257 # if @HAVE_WINSOCK2_H@
259 # define getsockname rpl_getsockname
260 extern int rpl_getsockname (int, struct sockaddr *, int *);
262 # elif @HAVE_WINSOCK2_H@
264 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
265 # elif defined GNULIB_POSIXCHECK
267 # define getsockname(s,a,l) \
268 (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
269 "use gnulib module getsockname for portability"), \
270 getsockname (s, a, l))
273 # if @GNULIB_GETSOCKOPT@
274 # if @HAVE_WINSOCK2_H@
276 # define getsockopt rpl_getsockopt
277 extern int rpl_getsockopt (int, int, int, void *, socklen_t *);
279 # elif @HAVE_WINSOCK2_H@
281 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
282 # elif defined GNULIB_POSIXCHECK
284 # define getsockopt(s,lvl,o,v,l) \
285 (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
286 "use gnulib module getsockopt for portability"), \
287 getsockopt (s, lvl, o, v, l))
291 # if @HAVE_WINSOCK2_H@
293 # define listen rpl_listen
294 extern int rpl_listen (int, int);
296 # elif @HAVE_WINSOCK2_H@
298 # define listen listen_used_without_requesting_gnulib_module_listen
299 # elif defined GNULIB_POSIXCHECK
301 # define listen(s,b) \
302 (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
303 "use gnulib module listen for portability"), \
308 # if @HAVE_WINSOCK2_H@
310 # define recv rpl_recv
311 extern int rpl_recv (int, void *, int, int);
313 # elif @HAVE_WINSOCK2_H@
315 # define recv recv_used_without_requesting_gnulib_module_recv
316 # elif defined GNULIB_POSIXCHECK
318 # define recv(s,b,n,f) \
319 (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
320 "use gnulib module recv for portability"), \
325 # if @HAVE_WINSOCK2_H@
327 # define send rpl_send
328 extern int rpl_send (int, const void *, int, int);
330 # elif @HAVE_WINSOCK2_H@
332 # define send send_used_without_requesting_gnulib_module_send
333 # elif defined GNULIB_POSIXCHECK
335 # define send(s,b,n,f) \
336 (GL_LINK_WARNING ("send is not always POSIX compliant - " \
337 "use gnulib module send for portability"), \
341 # if @GNULIB_RECVFROM@
342 # if @HAVE_WINSOCK2_H@
344 # define recvfrom rpl_recvfrom
345 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
347 # elif @HAVE_WINSOCK2_H@
349 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
350 # elif defined GNULIB_POSIXCHECK
352 # define recvfrom(s,b,n,f,a,l) \
353 (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
354 "use gnulib module recvfrom for portability"), \
355 recvfrom (s, b, n, f, a, l))
359 # if @HAVE_WINSOCK2_H@
361 # define sendto rpl_sendto
362 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
364 # elif @HAVE_WINSOCK2_H@
366 # define sendto sendto_used_without_requesting_gnulib_module_sendto
367 # elif defined GNULIB_POSIXCHECK
369 # define sendto(s,b,n,f,a,l) \
370 (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
371 "use gnulib module sendto for portability"), \
372 sendto (s, b, n, f, a, l))
375 # if @GNULIB_SETSOCKOPT@
376 # if @HAVE_WINSOCK2_H@
378 # define setsockopt rpl_setsockopt
379 extern int rpl_setsockopt (int, int, int, const void *, socklen_t);
381 # elif @HAVE_WINSOCK2_H@
383 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
384 # elif defined GNULIB_POSIXCHECK
386 # define setsockopt(s,lvl,o,v,l) \
387 (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
388 "use gnulib module setsockopt for portability"), \
389 setsockopt (s, lvl, o, v, l))
392 # if @GNULIB_SHUTDOWN@
393 # if @HAVE_WINSOCK2_H@
395 # define shutdown rpl_shutdown
396 extern int rpl_shutdown (int, int);
398 # elif @HAVE_WINSOCK2_H@
400 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
401 # elif defined GNULIB_POSIXCHECK
403 # define shutdown(s,h) \
404 (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
405 "use gnulib module shutdown for portability"), \
409 # if @HAVE_WINSOCK2_H@
411 # define select select_used_without_including_sys_select_h
418 #endif /* HAVE_SYS_SOCKET_H */
420 #endif /* _GL_SYS_SOCKET_H */
421 #endif /* _GL_SYS_SOCKET_H */