sys_socket: Define sockaddr_storage.
[gnulib.git] / lib / sys_socket.in.h
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.
5
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)
9    any later version.
10
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.
15
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.  */
19
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.  */
25
26 #ifndef _GL_SYS_SOCKET_H
27
28 #if @HAVE_SYS_SOCKET_H@
29
30 # if __GNUC__ >= 3
31 @PRAGMA_SYSTEM_HEADER@
32 # endif
33
34 /* On many platforms, <sys/socket.h> assumes prior inclusion of
35    <sys/types.h>.  */
36 # include <sys/types.h>
37
38 /* The include_next requires a split double-inclusion guard.  */
39 # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
40
41 #endif
42
43 #ifndef _GL_SYS_SOCKET_H
44 #define _GL_SYS_SOCKET_H
45
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     (_SS_SIZE - (max (sizeof (sa_family_t),         \
53                                           alignof (__ss_aligntype))     \
54                                      + sizeof (__ss_aligntype)))
55
56 struct sockaddr_storage
57 {
58   sa_family_t ss_family;      /* Address family, etc.  */
59   __ss_aligntype __ss_align;  /* Force desired alignment.  */
60   char __ss_padding[_SS_PADSIZE];
61 };
62 #endif
63
64 #if @HAVE_SYS_SOCKET_H@
65
66 /* A platform that has <sys/socket.h>.  */
67
68 /* For shutdown().  */
69 # if !defined SHUT_RD
70 #  define SHUT_RD 0
71 # endif
72 # if !defined SHUT_WR
73 #  define SHUT_WR 1
74 # endif
75 # if !defined SHUT_RDWR
76 #  define SHUT_RDWR 2
77 # endif
78
79 #else
80
81 # ifdef __CYGWIN__
82 #  error "Cygwin does have a sys/socket.h, doesn't it?!?"
83 # endif
84
85 /* A platform that lacks <sys/socket.h>.
86
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
100    releases. */
101
102
103 # if @HAVE_WINSOCK2_H@
104 #  include <winsock2.h>
105 # endif
106 # if @HAVE_WS2TCPIP_H@
107 #  include <ws2tcpip.h>
108 # endif
109
110 /* For shutdown(). */
111 # if !defined SHUT_RD && defined SD_RECEIVE
112 #  define SHUT_RD SD_RECEIVE
113 # endif
114 # if !defined SHUT_WR && defined SD_SEND
115 #  define SHUT_WR SD_SEND
116 # endif
117 # if !defined SHUT_RDWR && defined SD_BOTH
118 #  define SHUT_RDWR SD_BOTH
119 # endif
120
121 /* The definition of GL_LINK_WARNING is copied here.  */
122
123 # if @HAVE_WINSOCK2_H@
124 /* Include headers needed by the emulation code.  */
125 #  include <sys/types.h>
126 #  include <io.h>
127
128 typedef int socklen_t;
129
130 # endif
131
132 # ifdef __cplusplus
133 extern "C" {
134 # endif
135
136 # if @HAVE_WINSOCK2_H@
137
138 /* Re-define FD_ISSET to avoid a WSA call while we are not using
139    network sockets.  */
140 static inline int
141 rpl_fd_isset (SOCKET fd, fd_set * set)
142 {
143   u_int i;
144   if (set == NULL)
145     return 0;
146
147   for (i = 0; i < set->fd_count; i++)
148     if (set->fd_array[i] == fd)
149       return 1;
150
151   return 0;
152 }
153
154 #  undef FD_ISSET
155 #  define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
156
157 # endif
158
159 /* Wrap everything else to use libc file descriptors for sockets.  */
160
161 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
162 #  undef close
163 #  define close close_used_without_including_unistd_h
164 # endif
165
166 # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
167 #  undef gethostname
168 #  define gethostname gethostname_used_without_including_unistd_h
169 # endif
170
171 # if @GNULIB_SOCKET@
172 #  if @HAVE_WINSOCK2_H@
173 #   undef socket
174 #   define socket               rpl_socket
175 extern int rpl_socket (int, int, int protocol);
176 #  endif
177 # elif @HAVE_WINSOCK2_H@
178 #  undef socket
179 #  define socket socket_used_without_requesting_gnulib_module_socket
180 # elif defined GNULIB_POSIXCHECK
181 #  undef socket
182 #  define socket(d,t,p) \
183      (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
184                        "use gnulib module socket for portability"), \
185       socket (d, t, p))
186 # endif
187
188 # if @GNULIB_CONNECT@
189 #  if @HAVE_WINSOCK2_H@
190 #   undef connect
191 #   define connect              rpl_connect
192 extern int rpl_connect (int, struct sockaddr *, int);
193 #  endif
194 # elif @HAVE_WINSOCK2_H@
195 #  undef connect
196 #  define connect socket_used_without_requesting_gnulib_module_connect
197 # elif defined GNULIB_POSIXCHECK
198 #  undef connect
199 #  define connect(s,a,l) \
200      (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
201                        "use gnulib module connect for portability"), \
202       connect (s, a, l))
203 # endif
204
205 # if @GNULIB_ACCEPT@
206 #  if @HAVE_WINSOCK2_H@
207 #   undef accept
208 #   define accept               rpl_accept
209 extern int rpl_accept (int, struct sockaddr *, int *);
210 #  endif
211 # elif @HAVE_WINSOCK2_H@
212 #  undef accept
213 #  define accept accept_used_without_requesting_gnulib_module_accept
214 # elif defined GNULIB_POSIXCHECK
215 #  undef accept
216 #  define accept(s,a,l) \
217      (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
218                        "use gnulib module accept for portability"), \
219       accept (s, a, l))
220 # endif
221
222 # if @GNULIB_BIND@
223 #  if @HAVE_WINSOCK2_H@
224 #   undef bind
225 #   define bind                 rpl_bind
226 extern int rpl_bind (int, struct sockaddr *, int);
227 #  endif
228 # elif @HAVE_WINSOCK2_H@
229 #  undef bind
230 #  define bind bind_used_without_requesting_gnulib_module_bind
231 # elif defined GNULIB_POSIXCHECK
232 #  undef bind
233 #  define bind(s,a,l) \
234      (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
235                        "use gnulib module bind for portability"), \
236       bind (s, a, l))
237 # endif
238
239 # if @GNULIB_GETPEERNAME@
240 #  if @HAVE_WINSOCK2_H@
241 #   undef getpeername
242 #   define getpeername          rpl_getpeername
243 extern int rpl_getpeername (int, struct sockaddr *, int *);
244 #  endif
245 # elif @HAVE_WINSOCK2_H@
246 #  undef getpeername
247 #  define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
248 # elif defined GNULIB_POSIXCHECK
249 #  undef getpeername
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))
254 # endif
255
256 # if @GNULIB_GETSOCKNAME@
257 #  if @HAVE_WINSOCK2_H@
258 #   undef getsockname
259 #   define getsockname          rpl_getsockname
260 extern int rpl_getsockname (int, struct sockaddr *, int *);
261 #  endif
262 # elif @HAVE_WINSOCK2_H@
263 #  undef getsockname
264 #  define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
265 # elif defined GNULIB_POSIXCHECK
266 #  undef getsockname
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))
271 # endif
272
273 # if @GNULIB_GETSOCKOPT@
274 #  if @HAVE_WINSOCK2_H@
275 #   undef getsockopt
276 #   define getsockopt           rpl_getsockopt
277 extern int rpl_getsockopt (int, int, int, void *, socklen_t *);
278 #  endif
279 # elif @HAVE_WINSOCK2_H@
280 #  undef getsockopt
281 #  define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
282 # elif defined GNULIB_POSIXCHECK
283 #  undef getsockopt
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))
288 # endif
289
290 # if @GNULIB_LISTEN@
291 #  if @HAVE_WINSOCK2_H@
292 #   undef listen
293 #   define listen               rpl_listen
294 extern int rpl_listen (int, int);
295 #  endif
296 # elif @HAVE_WINSOCK2_H@
297 #  undef listen
298 #  define listen listen_used_without_requesting_gnulib_module_listen
299 # elif defined GNULIB_POSIXCHECK
300 #  undef listen
301 #  define listen(s,b) \
302      (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
303                        "use gnulib module listen for portability"), \
304       listen (s, b))
305 # endif
306
307 # if @GNULIB_RECV@
308 #  if @HAVE_WINSOCK2_H@
309 #   undef recv
310 #   define recv                 rpl_recv
311 extern int rpl_recv (int, void *, int, int);
312 #  endif
313 # elif @HAVE_WINSOCK2_H@
314 #  undef recv
315 #  define recv recv_used_without_requesting_gnulib_module_recv
316 # elif defined GNULIB_POSIXCHECK
317 #  undef recv
318 #  define recv(s,b,n,f) \
319      (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
320                        "use gnulib module recv for portability"), \
321       recv (s, b, n, f))
322 # endif
323
324 # if @GNULIB_SEND@
325 #  if @HAVE_WINSOCK2_H@
326 #   undef send
327 #   define send                 rpl_send
328 extern int rpl_send (int, const void *, int, int);
329 #  endif
330 # elif @HAVE_WINSOCK2_H@
331 #  undef send
332 #  define send send_used_without_requesting_gnulib_module_send
333 # elif defined GNULIB_POSIXCHECK
334 #  undef send
335 #  define send(s,b,n,f) \
336      (GL_LINK_WARNING ("send is not always POSIX compliant - " \
337                        "use gnulib module send for portability"), \
338       send (s, b, n, f))
339 # endif
340
341 # if @GNULIB_RECVFROM@
342 #  if @HAVE_WINSOCK2_H@
343 #   undef recvfrom
344 #   define recvfrom             rpl_recvfrom
345 extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
346 #  endif
347 # elif @HAVE_WINSOCK2_H@
348 #  undef recvfrom
349 #  define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
350 # elif defined GNULIB_POSIXCHECK
351 #  undef recvfrom
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))
356 # endif
357
358 # if @GNULIB_SENDTO@
359 #  if @HAVE_WINSOCK2_H@
360 #   undef sendto
361 #   define sendto               rpl_sendto
362 extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
363 #  endif
364 # elif @HAVE_WINSOCK2_H@
365 #  undef sendto
366 #  define sendto sendto_used_without_requesting_gnulib_module_sendto
367 # elif defined GNULIB_POSIXCHECK
368 #  undef sendto
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))
373 # endif
374
375 # if @GNULIB_SETSOCKOPT@
376 #  if @HAVE_WINSOCK2_H@
377 #   undef setsockopt
378 #   define setsockopt           rpl_setsockopt
379 extern int rpl_setsockopt (int, int, int, const void *, socklen_t);
380 #  endif
381 # elif @HAVE_WINSOCK2_H@
382 #  undef setsockopt
383 #  define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
384 # elif defined GNULIB_POSIXCHECK
385 #  undef setsockopt
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))
390 # endif
391
392 # if @GNULIB_SHUTDOWN@
393 #  if @HAVE_WINSOCK2_H@
394 #   undef shutdown
395 #   define shutdown             rpl_shutdown
396 extern int rpl_shutdown (int, int);
397 #  endif
398 # elif @HAVE_WINSOCK2_H@
399 #  undef shutdown
400 #  define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
401 # elif defined GNULIB_POSIXCHECK
402 #  undef shutdown
403 #  define shutdown(s,h) \
404      (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
405                        "use gnulib module shutdown for portability"), \
406       shutdown (s, h))
407 # endif
408
409 # if @HAVE_WINSOCK2_H@
410 #  undef select
411 #  define select                select_used_without_including_sys_select_h
412 # endif
413
414 # ifdef __cplusplus
415 }
416 # endif
417
418 #endif /* HAVE_SYS_SOCKET_H */
419
420 #endif /* _GL_SYS_SOCKET_H */
421 #endif /* _GL_SYS_SOCKET_H */