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)
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_SA_FAMILY_T@
47 typedef unsigned short sa_family_t
;
50 #if !@HAVE_STRUCT_SOCKADDR_STORAGE@
52 /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
53 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
54 # define __ss_aligntype unsigned long int
56 # define _SS_PADSIZE \
57 (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
58 ? sizeof (sa_family_t) \
59 : alignof (__ss_aligntype)) \
60 + sizeof (__ss_aligntype)))
62 struct sockaddr_storage
64 sa_family_t ss_family
; /* Address family, etc. */
65 __ss_aligntype __ss_align
; /* Force desired alignment. */
66 char __ss_padding
[_SS_PADSIZE
];
70 #if @HAVE_SYS_SOCKET_H@
72 /* A platform that has <sys/socket.h>. */
81 # if !defined SHUT_RDWR
88 # error "Cygwin does have a sys/socket.h, doesn't it?!?"
91 /* A platform that lacks <sys/socket.h>.
93 Currently only MinGW is supported. See the gnulib manual regarding
94 Windows sockets. MinGW has the header files winsock2.h and
95 ws2tcpip.h that declare the sys/socket.h definitions we need. Note
96 that you can influence which definitions you get by setting the
97 WINVER symbol before including these two files. For example,
98 getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
99 symbol is set indiriectly through WINVER). You can set this by
100 adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
101 code may not run on older Windows releases then. My Windows 2000
102 box was not able to run the code, for example. The situation is
103 slightly confusing because:
104 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
105 suggests that getaddrinfo should be available on all Windows
109 # if @HAVE_WINSOCK2_H@
110 # include <winsock2.h>
112 # if @HAVE_WS2TCPIP_H@
113 # include <ws2tcpip.h>
116 /* For shutdown(). */
117 # if !defined SHUT_RD && defined SD_RECEIVE
118 # define SHUT_RD SD_RECEIVE
120 # if !defined SHUT_WR && defined SD_SEND
121 # define SHUT_WR SD_SEND
123 # if !defined SHUT_RDWR && defined SD_BOTH
124 # define SHUT_RDWR SD_BOTH
127 /* The definition of GL_LINK_WARNING is copied here. */
129 # if @HAVE_WINSOCK2_H@
130 /* Include headers needed by the emulation code. */
131 # include <sys/types.h>
134 typedef int socklen_t
;
142 # if @HAVE_WINSOCK2_H@
144 /* Re-define FD_ISSET to avoid a WSA call while we are not using
147 rpl_fd_isset (SOCKET fd
, fd_set
* set
)
153 for (i
= 0; i
< set
->fd_count
; i
++)
154 if (set
->fd_array
[i
] == fd
)
161 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
165 /* Wrap everything else to use libc file descriptors for sockets. */
167 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
169 # define close close_used_without_including_unistd_h
172 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
174 # define gethostname gethostname_used_without_including_unistd_h
178 # if @HAVE_WINSOCK2_H@
180 # define socket rpl_socket
181 extern int rpl_socket (int, int, int protocol
);
183 # elif @HAVE_WINSOCK2_H@
185 # define socket socket_used_without_requesting_gnulib_module_socket
186 # elif defined GNULIB_POSIXCHECK
188 # define socket(d,t,p) \
189 (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
190 "use gnulib module socket for portability"), \
194 # if @GNULIB_CONNECT@
195 # if @HAVE_WINSOCK2_H@
197 # define connect rpl_connect
198 extern int rpl_connect (int, struct sockaddr
*, int);
200 # elif @HAVE_WINSOCK2_H@
202 # define connect socket_used_without_requesting_gnulib_module_connect
203 # elif defined GNULIB_POSIXCHECK
205 # define connect(s,a,l) \
206 (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
207 "use gnulib module connect for portability"), \
212 # if @HAVE_WINSOCK2_H@
214 # define accept rpl_accept
215 extern int rpl_accept (int, struct sockaddr
*, int *);
217 # elif @HAVE_WINSOCK2_H@
219 # define accept accept_used_without_requesting_gnulib_module_accept
220 # elif defined GNULIB_POSIXCHECK
222 # define accept(s,a,l) \
223 (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
224 "use gnulib module accept for portability"), \
229 # if @HAVE_WINSOCK2_H@
231 # define bind rpl_bind
232 extern int rpl_bind (int, struct sockaddr
*, int);
234 # elif @HAVE_WINSOCK2_H@
236 # define bind bind_used_without_requesting_gnulib_module_bind
237 # elif defined GNULIB_POSIXCHECK
239 # define bind(s,a,l) \
240 (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
241 "use gnulib module bind for portability"), \
245 # if @GNULIB_GETPEERNAME@
246 # if @HAVE_WINSOCK2_H@
248 # define getpeername rpl_getpeername
249 extern int rpl_getpeername (int, struct sockaddr
*, int *);
251 # elif @HAVE_WINSOCK2_H@
253 # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
254 # elif defined GNULIB_POSIXCHECK
256 # define getpeername(s,a,l) \
257 (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
258 "use gnulib module getpeername for portability"), \
259 getpeername (s, a, l))
262 # if @GNULIB_GETSOCKNAME@
263 # if @HAVE_WINSOCK2_H@
265 # define getsockname rpl_getsockname
266 extern int rpl_getsockname (int, struct sockaddr
*, int *);
268 # elif @HAVE_WINSOCK2_H@
270 # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
271 # elif defined GNULIB_POSIXCHECK
273 # define getsockname(s,a,l) \
274 (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
275 "use gnulib module getsockname for portability"), \
276 getsockname (s, a, l))
279 # if @GNULIB_GETSOCKOPT@
280 # if @HAVE_WINSOCK2_H@
282 # define getsockopt rpl_getsockopt
283 extern int rpl_getsockopt (int, int, int, void *, socklen_t
*);
285 # elif @HAVE_WINSOCK2_H@
287 # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
288 # elif defined GNULIB_POSIXCHECK
290 # define getsockopt(s,lvl,o,v,l) \
291 (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
292 "use gnulib module getsockopt for portability"), \
293 getsockopt (s, lvl, o, v, l))
297 # if @HAVE_WINSOCK2_H@
299 # define listen rpl_listen
300 extern int rpl_listen (int, int);
302 # elif @HAVE_WINSOCK2_H@
304 # define listen listen_used_without_requesting_gnulib_module_listen
305 # elif defined GNULIB_POSIXCHECK
307 # define listen(s,b) \
308 (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
309 "use gnulib module listen for portability"), \
314 # if @HAVE_WINSOCK2_H@
316 # define recv rpl_recv
317 extern int rpl_recv (int, void *, int, int);
319 # elif @HAVE_WINSOCK2_H@
321 # define recv recv_used_without_requesting_gnulib_module_recv
322 # elif defined GNULIB_POSIXCHECK
324 # define recv(s,b,n,f) \
325 (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
326 "use gnulib module recv for portability"), \
331 # if @HAVE_WINSOCK2_H@
333 # define send rpl_send
334 extern int rpl_send (int, const void *, int, int);
336 # elif @HAVE_WINSOCK2_H@
338 # define send send_used_without_requesting_gnulib_module_send
339 # elif defined GNULIB_POSIXCHECK
341 # define send(s,b,n,f) \
342 (GL_LINK_WARNING ("send is not always POSIX compliant - " \
343 "use gnulib module send for portability"), \
347 # if @GNULIB_RECVFROM@
348 # if @HAVE_WINSOCK2_H@
350 # define recvfrom rpl_recvfrom
351 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr
*, int *);
353 # elif @HAVE_WINSOCK2_H@
355 # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
356 # elif defined GNULIB_POSIXCHECK
358 # define recvfrom(s,b,n,f,a,l) \
359 (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
360 "use gnulib module recvfrom for portability"), \
361 recvfrom (s, b, n, f, a, l))
365 # if @HAVE_WINSOCK2_H@
367 # define sendto rpl_sendto
368 extern int rpl_sendto (int, const void *, int, int, struct sockaddr
*, int);
370 # elif @HAVE_WINSOCK2_H@
372 # define sendto sendto_used_without_requesting_gnulib_module_sendto
373 # elif defined GNULIB_POSIXCHECK
375 # define sendto(s,b,n,f,a,l) \
376 (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
377 "use gnulib module sendto for portability"), \
378 sendto (s, b, n, f, a, l))
381 # if @GNULIB_SETSOCKOPT@
382 # if @HAVE_WINSOCK2_H@
384 # define setsockopt rpl_setsockopt
385 extern int rpl_setsockopt (int, int, int, const void *, socklen_t
);
387 # elif @HAVE_WINSOCK2_H@
389 # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
390 # elif defined GNULIB_POSIXCHECK
392 # define setsockopt(s,lvl,o,v,l) \
393 (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
394 "use gnulib module setsockopt for portability"), \
395 setsockopt (s, lvl, o, v, l))
398 # if @GNULIB_SHUTDOWN@
399 # if @HAVE_WINSOCK2_H@
401 # define shutdown rpl_shutdown
402 extern int rpl_shutdown (int, int);
404 # elif @HAVE_WINSOCK2_H@
406 # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
407 # elif defined GNULIB_POSIXCHECK
409 # define shutdown(s,h) \
410 (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
411 "use gnulib module shutdown for portability"), \
415 # if @HAVE_WINSOCK2_H@
417 # define select select_used_without_including_sys_select_h
424 #endif /* HAVE_SYS_SOCKET_H */
431 /* Accept a connection on a socket, with specific opening flags.
432 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
433 and O_TEXT, O_BINARY (defined in "binary-io.h").
434 See also the Linux man page at
435 <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */
437 # define accept4 rpl_accept4
439 extern int accept4 (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
,
441 #elif defined GNULIB_POSIXCHECK
443 # define accept4(s,a,l,f) \
444 (GL_LINK_WARNING ("accept4 is unportable - " \
445 "use gnulib module accept4 for portability"), \
446 accept4 (s, a, l, f))
453 #endif /* _GL_SYS_SOCKET_H */
454 #endif /* _GL_SYS_SOCKET_H */