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-2008 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_SYS_SOCKET_H@
48 /* A platform that has <sys/socket.h>. */
57 # if !defined SHUT_RDWR
64 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
67 /* A platform that lacks <sys/socket.h>.
69 Currently only MinGW is supported. See the gnulib manual regarding
70 Windows sockets. MinGW has the header files winsock2.h and
71 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
72 that you can influence which definitions you get by setting the
73 WINVER symbol before including these two files. For example,
74 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
75 symbol is set indiriectly through WINVER). You can set this by
76 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
77 code may not run on older Windows releases then. My Windows 2000
78 box was not able to run the code, for example. The situation is
79 slightly confusing because:
80 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
81 suggests that getaddrinfo should be available on all Windows
85 # if @HAVE_WINSOCK2_H@
86 # include <winsock2.h>
88 # if @HAVE_WS2TCPIP_H@
89 # include <ws2tcpip.h>
93 # if !defined SHUT_RD && defined SD_RECEIVE
94 # define SHUT_RD SD_RECEIVE
96 # if !defined SHUT_WR && defined SD_SEND
97 # define SHUT_WR SD_SEND
99 # if !defined SHUT_RDWR && defined SD_BOTH
100 # define SHUT_RDWR SD_BOTH
103 /* The definition of GL_LINK_WARNING is copied here. */
105 # if @HAVE_WINSOCK2_H@
106 /* Include headers needed by the emulation code. */
107 # include <sys/types.h>
110 typedef int socklen_t;
118 # if @HAVE_WINSOCK2_H@
120 /* Re-define FD_ISSET to avoid a WSA call while we are not using
123 rpl_fd_isset (SOCKET fd, fd_set * set)
129 for (i = 0; i < set->fd_count; i++)
130 if (set->fd_array[i] == fd)
137 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
141 /* Wrap everything else to use libc file descriptors for sockets. */
143 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
145 # define close close_used_without_including_unistd_h
148 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
150 # define gethostname gethostname_used_without_including_unistd_h
154 # if @HAVE_WINSOCK2_H@
156 # define socket rpl_socket
157 extern int rpl_socket (int, int, int protocol);
159 # elif @HAVE_WINSOCK2_H@
161 # define socket socket_used_without_requesting_gnulib_module_socket
162 # elif defined GNULIB_POSIXCHECK
164 # define socket(d,t,p) \
165 (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
166 "use gnulib module socket for portability"), \
170 # if @GNULIB_CONNECT@
171 # if @HAVE_WINSOCK2_H@
173 # define connect rpl_connect
174 extern int rpl_connect (int, struct sockaddr *, int);
176 # elif @HAVE_WINSOCK2_H@
178 # define connect socket_used_without_requesting_gnulib_module_connect
179 # elif defined GNULIB_POSIXCHECK
181 # define connect(s,a,l) \
182 (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
183 "use gnulib module connect for portability"), \
188 # if @HAVE_WINSOCK2_H@
190 # define accept rpl_accept
191 extern int rpl_accept (int, struct sockaddr *, int *);
193 # elif @HAVE_WINSOCK2_H@
195 # define accept accept_used_without_requesting_gnulib_module_accept
196 # elif defined GNULIB_POSIXCHECK
198 # define accept(s,a,l) \
199 (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
200 "use gnulib module accept for portability"), \
205 # if @HAVE_WINSOCK2_H@
207 # define bind rpl_bind
208 extern int rpl_bind (int, struct sockaddr *, int);
210 # elif @HAVE_WINSOCK2_H@
212 # define bind bind_used_without_requesting_gnulib_module_bind
213 # elif defined GNULIB_POSIXCHECK
215 # define bind(s,a,l) \
216 (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
217 "use gnulib module bind for portability"), \
221 # if @GNULIB_GETPEERNAME@
222 # if @HAVE_WINSOCK2_H@
224 # define getpeername rpl_getpeername
225 extern int rpl_getpeername (int, struct sockaddr *, int *);
227 # elif @HAVE_WINSOCK2_H@
229 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
230 # elif defined GNULIB_POSIXCHECK
232 # define getpeername(s,a,l) \
233 (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
234 "use gnulib module getpeername for portability"), \
235 getpeername (s, a, l))
238 # if @GNULIB_GETSOCKNAME@
239 # if @HAVE_WINSOCK2_H@
241 # define getsockname rpl_getsockname
242 extern int rpl_getsockname (int, struct sockaddr *, int *);
244 # elif @HAVE_WINSOCK2_H@
246 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
247 # elif defined GNULIB_POSIXCHECK
249 # define getsockname(s,a,l) \
250 (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
251 "use gnulib module getsockname for portability"), \
252 getsockname (s, a, l))
255 # if @GNULIB_GETSOCKOPT@
256 # if @HAVE_WINSOCK2_H@
258 # define getsockopt rpl_getsockopt
259 extern int rpl_getsockopt (int, int, int, void *, int *);
261 # elif @HAVE_WINSOCK2_H@
263 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
264 # elif defined GNULIB_POSIXCHECK
266 # define getsockopt(s,lvl,o,v,l) \
267 (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
268 "use gnulib module getsockopt for portability"), \
269 getsockopt (s, lvl, o, v, l))
273 # if @HAVE_WINSOCK2_H@
275 # define listen rpl_listen
276 extern int rpl_listen (int, int);
278 # elif @HAVE_WINSOCK2_H@
280 # define listen listen_used_without_requesting_gnulib_module_listen
281 # elif defined GNULIB_POSIXCHECK
283 # define listen(s,b) \
284 (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
285 "use gnulib module listen for portability"), \
290 # if @HAVE_WINSOCK2_H@
292 # define recv rpl_recv
293 extern int rpl_recv (int, void *, int, int);
295 # elif @HAVE_WINSOCK2_H@
297 # define recv recv_used_without_requesting_gnulib_module_recv
298 # elif defined GNULIB_POSIXCHECK
300 # define recv(s,b,n,f) \
301 (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
302 "use gnulib module recv for portability"), \
307 # if @HAVE_WINSOCK2_H@
309 # define send rpl_send
310 extern int rpl_send (int, const void *, int, int);
312 # elif @HAVE_WINSOCK2_H@
314 # define send send_used_without_requesting_gnulib_module_send
315 # elif defined GNULIB_POSIXCHECK
317 # define send(s,b,n,f) \
318 (GL_LINK_WARNING ("send is not always POSIX compliant - " \
319 "use gnulib module send for portability"), \
323 # if @GNULIB_RECVFROM@
324 # if @HAVE_WINSOCK2_H@
326 # define recvfrom rpl_recvfrom
327 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
329 # elif @HAVE_WINSOCK2_H@
331 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
332 # elif defined GNULIB_POSIXCHECK
334 # define recvfrom(s,b,n,f,a,l) \
335 (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
336 "use gnulib module recvfrom for portability"), \
337 recvfrom (s, b, n, f, a, l))
341 # if @HAVE_WINSOCK2_H@
343 # define sendto rpl_sendto
344 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
346 # elif @HAVE_WINSOCK2_H@
348 # define sendto sendto_used_without_requesting_gnulib_module_sendto
349 # elif defined GNULIB_POSIXCHECK
351 # define sendto(s,b,n,f,a,l) \
352 (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
353 "use gnulib module sendto for portability"), \
354 sendto (s, b, n, f, a, l))
357 # if @GNULIB_SETSOCKOPT@
358 # if @HAVE_WINSOCK2_H@
360 # define setsockopt rpl_setsockopt
361 extern int rpl_setsockopt (int, int, int, const void *, int);
363 # elif @HAVE_WINSOCK2_H@
365 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
366 # elif defined GNULIB_POSIXCHECK
368 # define setsockopt(s,lvl,o,v,l) \
369 (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
370 "use gnulib module setsockopt for portability"), \
371 setsockopt (s, lvl, o, v, l))
374 # if @GNULIB_SHUTDOWN@
375 # if @HAVE_WINSOCK2_H@
377 # define shutdown rpl_shutdown
378 extern int rpl_shutdown (int, int);
380 # elif @HAVE_WINSOCK2_H@
382 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
383 # elif defined GNULIB_POSIXCHECK
385 # define shutdown(s,h) \
386 (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
387 "use gnulib module shutdown for portability"), \
391 # if @HAVE_WINSOCK2_H@
393 # define select select_used_without_including_sys_select_h
400 #endif /* HAVE_SYS_SOCKET_H */
402 #endif /* _GL_SYS_SOCKET_H */
403 #endif /* _GL_SYS_SOCKET_H */