dadd56ac5845d4cd17c857038fe4768c38c408b5
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 3, or (at your option)
9 any later version.
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
24 application. */
26 #ifndef _GL_SYS_SOCKET_H
28 #if @HAVE_SYS_SOCKET_H@
30 # if __GNUC__ >= 3
31 @PRAGMA_SYSTEM_HEADER@
32 # endif
34 /* On many platforms, <sys/socket.h> assumes prior inclusion of
35 <sys/types.h>. */
36 # include <sys/types.h>
38 /* The include_next requires a split double-inclusion guard. */
39 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
41 #endif
43 #ifndef _GL_SYS_SOCKET_H
44 #define _GL_SYS_SOCKET_H
46 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
47 # include <alignof.h>
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
51 # define _SS_SIZE 256
52 # define _SS_PADSIZE \
53 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
54 ? sizeof (sa_family_t) \
55 : alignof (__ss_aligntype)) \
56 + sizeof (__ss_aligntype)))
58 struct sockaddr_storage
59 {
60 sa_family_t ss_family; /* Address family, etc. */
61 __ss_aligntype __ss_align; /* Force desired alignment. */
62 char __ss_padding[_SS_PADSIZE];
63 };
64 #endif
66 #if @HAVE_SYS_SOCKET_H@
68 /* A platform that has <sys/socket.h>. */
70 /* For shutdown(). */
71 # if !defined SHUT_RD
72 # define SHUT_RD 0
73 # endif
74 # if !defined SHUT_WR
75 # define SHUT_WR 1
76 # endif
77 # if !defined SHUT_RDWR
78 # define SHUT_RDWR 2
79 # endif
81 #else
83 # ifdef __CYGWIN__
84 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
85 # endif
87 /* A platform that lacks <sys/socket.h>.
89 Currently only MinGW is supported. See the gnulib manual regarding
90 Windows sockets. MinGW has the header files winsock2.h and
91 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
92 that you can influence which definitions you get by setting the
93 WINVER symbol before including these two files. For example,
94 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
95 symbol is set indiriectly through WINVER). You can set this by
96 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
97 code may not run on older Windows releases then. My Windows 2000
98 box was not able to run the code, for example. The situation is
99 slightly confusing because:
100 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
101 suggests that getaddrinfo should be available on all Windows
102 releases. */
105 # if @HAVE_WINSOCK2_H@
106 # include <winsock2.h>
107 # endif
108 # if @HAVE_WS2TCPIP_H@
109 # include <ws2tcpip.h>
110 # endif
112 /* For shutdown(). */
113 # if !defined SHUT_RD && defined SD_RECEIVE
114 # define SHUT_RD SD_RECEIVE
115 # endif
116 # if !defined SHUT_WR && defined SD_SEND
117 # define SHUT_WR SD_SEND
118 # endif
119 # if !defined SHUT_RDWR && defined SD_BOTH
120 # define SHUT_RDWR SD_BOTH
121 # endif
123 /* The definition of GL_LINK_WARNING is copied here. */
125 # if @HAVE_WINSOCK2_H@
126 /* Include headers needed by the emulation code. */
127 # include <sys/types.h>
128 # include <io.h>
130 typedef int socklen_t;
132 # endif
134 # ifdef __cplusplus
135 extern "C" {
136 # endif
138 # if @HAVE_WINSOCK2_H@
140 /* Re-define FD_ISSET to avoid a WSA call while we are not using
141 network sockets. */
142 static inline int
143 rpl_fd_isset (SOCKET fd, fd_set * set)
144 {
145 u_int i;
146 if (set == NULL)
147 return 0;
149 for (i = 0; i < set->fd_count; i++)
150 if (set->fd_array[i] == fd)
151 return 1;
153 return 0;
154 }
156 # undef FD_ISSET
157 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
159 # endif
161 /* Wrap everything else to use libc file descriptors for sockets. */
163 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
164 # undef close
165 # define close close_used_without_including_unistd_h
166 # endif
168 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
169 # undef gethostname
170 # define gethostname gethostname_used_without_including_unistd_h
171 # endif
173 # if @GNULIB_SOCKET@
174 # if @HAVE_WINSOCK2_H@
175 # undef socket
176 # define socket rpl_socket
177 extern int rpl_socket (int, int, int protocol);
178 # endif
179 # elif @HAVE_WINSOCK2_H@
180 # undef socket
181 # define socket socket_used_without_requesting_gnulib_module_socket
182 # elif defined GNULIB_POSIXCHECK
183 # undef socket
184 # define socket(d,t,p) \
185 (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
186 "use gnulib module socket for portability"), \
187 socket (d, t, p))
188 # endif
190 # if @GNULIB_CONNECT@
191 # if @HAVE_WINSOCK2_H@
192 # undef connect
193 # define connect rpl_connect
194 extern int rpl_connect (int, struct sockaddr *, int);
195 # endif
196 # elif @HAVE_WINSOCK2_H@
197 # undef connect
198 # define connect socket_used_without_requesting_gnulib_module_connect
199 # elif defined GNULIB_POSIXCHECK
200 # undef connect
201 # define connect(s,a,l) \
202 (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
203 "use gnulib module connect for portability"), \
204 connect (s, a, l))
205 # endif
207 # if @GNULIB_ACCEPT@
208 # if @HAVE_WINSOCK2_H@
209 # undef accept
210 # define accept rpl_accept
211 extern int rpl_accept (int, struct sockaddr *, int *);
212 # endif
213 # elif @HAVE_WINSOCK2_H@
214 # undef accept
215 # define accept accept_used_without_requesting_gnulib_module_accept
216 # elif defined GNULIB_POSIXCHECK
217 # undef accept
218 # define accept(s,a,l) \
219 (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
220 "use gnulib module accept for portability"), \
221 accept (s, a, l))
222 # endif
224 # if @GNULIB_BIND@
225 # if @HAVE_WINSOCK2_H@
226 # undef bind
227 # define bind rpl_bind
228 extern int rpl_bind (int, struct sockaddr *, int);
229 # endif
230 # elif @HAVE_WINSOCK2_H@
231 # undef bind
232 # define bind bind_used_without_requesting_gnulib_module_bind
233 # elif defined GNULIB_POSIXCHECK
234 # undef bind
235 # define bind(s,a,l) \
236 (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
237 "use gnulib module bind for portability"), \
238 bind (s, a, l))
239 # endif
241 # if @GNULIB_GETPEERNAME@
242 # if @HAVE_WINSOCK2_H@
243 # undef getpeername
244 # define getpeername rpl_getpeername
245 extern int rpl_getpeername (int, struct sockaddr *, int *);
246 # endif
247 # elif @HAVE_WINSOCK2_H@
248 # undef getpeername
249 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
250 # elif defined GNULIB_POSIXCHECK
251 # undef getpeername
252 # define getpeername(s,a,l) \
253 (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
254 "use gnulib module getpeername for portability"), \
255 getpeername (s, a, l))
256 # endif
258 # if @GNULIB_GETSOCKNAME@
259 # if @HAVE_WINSOCK2_H@
260 # undef getsockname
261 # define getsockname rpl_getsockname
262 extern int rpl_getsockname (int, struct sockaddr *, int *);
263 # endif
264 # elif @HAVE_WINSOCK2_H@
265 # undef getsockname
266 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
267 # elif defined GNULIB_POSIXCHECK
268 # undef getsockname
269 # define getsockname(s,a,l) \
270 (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
271 "use gnulib module getsockname for portability"), \
272 getsockname (s, a, l))
273 # endif
275 # if @GNULIB_GETSOCKOPT@
276 # if @HAVE_WINSOCK2_H@
277 # undef getsockopt
278 # define getsockopt rpl_getsockopt
279 extern int rpl_getsockopt (int, int, int, void *, socklen_t *);
280 # endif
281 # elif @HAVE_WINSOCK2_H@
282 # undef getsockopt
283 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
284 # elif defined GNULIB_POSIXCHECK
285 # undef getsockopt
286 # define getsockopt(s,lvl,o,v,l) \
287 (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
288 "use gnulib module getsockopt for portability"), \
289 getsockopt (s, lvl, o, v, l))
290 # endif
292 # if @GNULIB_LISTEN@
293 # if @HAVE_WINSOCK2_H@
294 # undef listen
295 # define listen rpl_listen
296 extern int rpl_listen (int, int);
297 # endif
298 # elif @HAVE_WINSOCK2_H@
299 # undef listen
300 # define listen listen_used_without_requesting_gnulib_module_listen
301 # elif defined GNULIB_POSIXCHECK
302 # undef listen
303 # define listen(s,b) \
304 (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
305 "use gnulib module listen for portability"), \
306 listen (s, b))
307 # endif
309 # if @GNULIB_RECV@
310 # if @HAVE_WINSOCK2_H@
311 # undef recv
312 # define recv rpl_recv
313 extern int rpl_recv (int, void *, int, int);
314 # endif
315 # elif @HAVE_WINSOCK2_H@
316 # undef recv
317 # define recv recv_used_without_requesting_gnulib_module_recv
318 # elif defined GNULIB_POSIXCHECK
319 # undef recv
320 # define recv(s,b,n,f) \
321 (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
322 "use gnulib module recv for portability"), \
323 recv (s, b, n, f))
324 # endif
326 # if @GNULIB_SEND@
327 # if @HAVE_WINSOCK2_H@
328 # undef send
329 # define send rpl_send
330 extern int rpl_send (int, const void *, int, int);
331 # endif
332 # elif @HAVE_WINSOCK2_H@
333 # undef send
334 # define send send_used_without_requesting_gnulib_module_send
335 # elif defined GNULIB_POSIXCHECK
336 # undef send
337 # define send(s,b,n,f) \
338 (GL_LINK_WARNING ("send is not always POSIX compliant - " \
339 "use gnulib module send for portability"), \
340 send (s, b, n, f))
341 # endif
343 # if @GNULIB_RECVFROM@
344 # if @HAVE_WINSOCK2_H@
345 # undef recvfrom
346 # define recvfrom rpl_recvfrom
347 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
348 # endif
349 # elif @HAVE_WINSOCK2_H@
350 # undef recvfrom
351 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
352 # elif defined GNULIB_POSIXCHECK
353 # undef recvfrom
354 # define recvfrom(s,b,n,f,a,l) \
355 (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
356 "use gnulib module recvfrom for portability"), \
357 recvfrom (s, b, n, f, a, l))
358 # endif
360 # if @GNULIB_SENDTO@
361 # if @HAVE_WINSOCK2_H@
362 # undef sendto
363 # define sendto rpl_sendto
364 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
365 # endif
366 # elif @HAVE_WINSOCK2_H@
367 # undef sendto
368 # define sendto sendto_used_without_requesting_gnulib_module_sendto
369 # elif defined GNULIB_POSIXCHECK
370 # undef sendto
371 # define sendto(s,b,n,f,a,l) \
372 (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
373 "use gnulib module sendto for portability"), \
374 sendto (s, b, n, f, a, l))
375 # endif
377 # if @GNULIB_SETSOCKOPT@
378 # if @HAVE_WINSOCK2_H@
379 # undef setsockopt
380 # define setsockopt rpl_setsockopt
381 extern int rpl_setsockopt (int, int, int, const void *, socklen_t);
382 # endif
383 # elif @HAVE_WINSOCK2_H@
384 # undef setsockopt
385 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
386 # elif defined GNULIB_POSIXCHECK
387 # undef setsockopt
388 # define setsockopt(s,lvl,o,v,l) \
389 (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
390 "use gnulib module setsockopt for portability"), \
391 setsockopt (s, lvl, o, v, l))
392 # endif
394 # if @GNULIB_SHUTDOWN@
395 # if @HAVE_WINSOCK2_H@
396 # undef shutdown
397 # define shutdown rpl_shutdown
398 extern int rpl_shutdown (int, int);
399 # endif
400 # elif @HAVE_WINSOCK2_H@
401 # undef shutdown
402 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
403 # elif defined GNULIB_POSIXCHECK
404 # undef shutdown
405 # define shutdown(s,h) \
406 (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
407 "use gnulib module shutdown for portability"), \
408 shutdown (s, h))
409 # endif
411 # if @HAVE_WINSOCK2_H@
412 # undef select
413 # define select select_used_without_including_sys_select_h
414 # endif
416 # ifdef __cplusplus
417 }
418 # endif
420 #endif /* HAVE_SYS_SOCKET_H */
422 #endif /* _GL_SYS_SOCKET_H */
423 #endif /* _GL_SYS_SOCKET_H */