aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_sockets.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/mhd_sockets.h')
-rw-r--r--src/microhttpd/mhd_sockets.h406
1 files changed, 406 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
new file mode 100644
index 00000000..3a3e4b85
--- /dev/null
+++ b/src/microhttpd/mhd_sockets.h
@@ -0,0 +1,406 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2014-2016 Karlson2k (Evgeny Grin)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19*/
20
21/**
22 * @file microhttpd/mhd_sockets.c
23 * @brief Header for platform-independent sockets abstraction
24 * @author Karlson2k (Evgeny Grin)
25 *
26 * Provides basic abstraction for sockets.
27 * Any functions can be implemented as macro on some platforms
28 * unless explicitly marked otherwise.
29 * Any function argument can be skipped in macro, so avoid
30 * variable modification in function parameters.
31 */
32
33#ifndef MHD_SOCKETS_H
34#define MHD_SOCKETS_H 1
35#include "mhd_options.h"
36
37#include <errno.h>
38
39#if !defined(MHD_POSIX_SOCKETS) && !defined(MHD_WINSOCK_SOCKETS)
40# if !defined(_WIN32) || defined(__CYGWIN__)
41# define MHD_POSIX_SOCKETS 1
42# else /* defined(_WIN32) && !defined(__CYGWIN__) */
43# define MHD_WINSOCK_SOCKETS 1
44# endif /* defined(_WIN32) && !defined(__CYGWIN__) */
45#endif /* !MHD_POSIX_SOCKETS && !MHD_WINSOCK_SOCKETS */
46
47/*
48 * MHD require headers that define socket type, socket basic functions
49 * (socket(), accept(), listen(), bind(), send(), recv(), select()), socket
50 * parameters like SOCK_CLOEXEC, SOCK_NONBLOCK, additional socket functions
51 * (poll(), epoll(), accept4()), struct timeval and other types, required
52 * for socket function.
53 */
54#if defined(MHD_POSIX_SOCKETS)
55# if HAVE_SYS_SOCKET_H
56# include <sys/socket.h>
57# endif
58# if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
59# ifdef HAVE_SOCKLIB_H
60# include <sockLib.h>
61# endif /* HAVE_SOCKLIB_H */
62# ifdef HAVE_INETLIB_H
63# include <inetLib.h>
64# endif /* HAVE_INETLIB_H */
65# include <strings.h> /* required for FD_SET (bzero() function) */
66# endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
67# ifdef HAVE_NETINET_IN_H
68# include <netinet/in.h>
69# endif /* HAVE_NETINET_IN_H */
70# if HAVE_ARPA_INET_H
71# include <arpa/inet.h>
72# endif
73# ifdef HAVE_NET_IF_H
74# include <net/if.h>
75# endif
76# if HAVE_SYS_TIME_H
77# include <sys/time.h>
78# endif
79# if HAVE_TIME_H
80# include <time.h>
81# endif
82# if HAVE_NETDB_H
83# include <netdb.h>
84# endif
85# if HAVE_SYS_SELECT_H
86# include <sys/select.h>
87# endif
88# if EPOLL_SUPPORT
89# include <sys/epoll.h>
90# endif
91# if HAVE_NETINET_TCP_H
92 /* for TCP_FASTOPEN and TCP_CORK */
93# include <netinet/tcp.h>
94# endif
95# ifdef HAVE_STRING_H
96# include <string.h> /* for strerror() */
97# endif
98# if defined(HAVE_SYS_TYPES_H)
99# include <sys/types.h> /* required on old platforms */
100# endif /* (!HAVE_SYS_SOCKET_H || !HAVE_SYS_SOCKET_H) && HAVE_SYS_TYPES_H */
101#elif defined(MHD_WINSOCK_SOCKETS)
102# ifndef WIN32_LEAN_AND_MEAN
103# define WIN32_LEAN_AND_MEAN 1
104# endif /* !WIN32_LEAN_AND_MEAN */
105# include <winsock2.h>
106# include <ws2tcpip.h>
107#endif /* MHD_WINSOCK_SOCKETS */
108
109#if defined(HAVE_POLL_H) && defined(HAVE_POLL)
110# include <poll.h>
111#endif
112
113#ifdef _MHD_FD_SETSIZE_IS_DEFAULT
114# define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
115#else /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
116# include "sysfdsetsize.h"
117# define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value()
118#endif /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
119
120#ifndef MHD_SOCKET_DEFINED
121/**
122 * MHD_socket is type for socket FDs
123 */
124# if defined(MHD_POSIX_SOCKETS)
125 typedef int MHD_socket;
126# define MHD_INVALID_SOCKET (-1)
127# elif defined(MHD_WINSOCK_SOCKETS)
128 typedef SOCKET MHD_socket;
129# define MHD_INVALID_SOCKET (INVALID_SOCKET)
130# endif /* MHD_WINSOCK_SOCKETS */
131
132# define MHD_SOCKET_DEFINED 1
133#endif /* ! MHD_SOCKET_DEFINED */
134
135#ifdef SOCK_CLOEXEC
136# define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
137#else /* ! SOCK_CLOEXEC */
138# define MAYBE_SOCK_CLOEXEC 0
139#endif /* ! SOCK_CLOEXEC */
140
141#ifdef HAVE_SOCK_NONBLOCK
142# define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
143#else /* ! HAVE_SOCK_NONBLOCK */
144# define MAYBE_SOCK_NONBLOCK 0
145#endif /* ! HAVE_SOCK_NONBLOCK */
146
147#if !defined(SHUT_WR) && defined(SD_SEND)
148# define SHUT_WR SD_SEND
149#endif
150#if !defined(SHUT_RD) && defined(SD_RECEIVE)
151# define SHUT_RD SD_RECEIVE
152#endif
153#if !defined(SHUT_RDWR) && defined(SD_BOTH)
154# define SHUT_RDWR SD_BOTH
155#endif
156
157#if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC))
158# define USE_ACCEPT4 1
159#endif
160
161#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
162# define USE_EPOLL_CREATE1 1
163#endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
164
165#ifdef TCP_FASTOPEN
166/**
167 * Default TCP fastopen queue size.
168 */
169#define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
170#endif
171
172
173/**
174 * _MHD_SOCKOPT_BOOL_TYPE is type for bool parameters for setsockopt()/getsockopt()
175 */
176#ifdef MHD_POSIX_SOCKETS
177 typedef int _MHD_SOCKOPT_BOOL_TYPE;
178#else /* MHD_WINSOCK_SOCKETS */
179 typedef BOOL _MHD_SOCKOPT_BOOL_TYPE;
180#endif /* MHD_WINSOCK_SOCKETS */
181
182/**
183 * _MHD_socket_funcs_size is type used to specify size for send and recv
184 * functions
185 */
186#if !defined(MHD_WINSOCK_SOCKETS)
187 typedef size_t _MHD_socket_funcs_size;
188#else
189 typedef int _MHD_socket_funcs_size;
190#endif
191
192/**
193 * MHD_socket_close_(fd) close any FDs (non-W32) / close only socket
194 * FDs (W32). Note that on HP-UNIX, this function may leak the FD if
195 * errno is set to EINTR. Do not use HP-UNIX.
196 *
197 * @param fd descriptor to close
198 * @return 0 on success (error codes like EINTR and EIO are counted as success,
199 * only EBADF counts as an error!)
200 */
201#if !defined(MHD_WINSOCK_SOCKETS)
202# define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0)
203#else
204# define MHD_socket_close_(fd) closesocket((fd))
205#endif
206
207/**
208 * MHD_socket_errno_ is errno of last function (non-W32) / errno of
209 * last socket function (W32)
210 */
211#if !defined(MHD_WINSOCK_SOCKETS)
212# define MHD_socket_errno_ errno
213#else
214# define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
215#endif
216
217 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
218 * description string of last socket error (W32) */
219#if !defined(MHD_WINSOCK_SOCKETS)
220# define MHD_socket_last_strerr_() strerror(errno)
221#else
222# define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
223#endif
224
225 /* MHD_strerror_ is strerror (both non-W32/W32) */
226#if !defined(MHD_WINSOCK_SOCKETS)
227# define MHD_strerror_(errnum) strerror((errnum))
228#else
229# define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
230#endif
231
232 /* MHD_set_socket_errno_ set errno to errnum (non-W32) / set socket last error to errnum (W32) */
233#if !defined(MHD_WINSOCK_SOCKETS)
234# define MHD_set_socket_errno_(errnum) errno=(errnum)
235#else
236# define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
237#endif
238
239 /* MHD_SYS_select_ is wrapper macro for system select() function */
240#if !defined(MHD_WINSOCK_SOCKETS)
241# define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
242#else
243# define MHD_SYS_select_(n,r,w,e,t) \
244( (!(r) || ((fd_set*)(r))->fd_count == 0) && \
245 (!(w) || ((fd_set*)(w))->fd_count == 0) && \
246 (!(e) || ((fd_set*)(e))->fd_count == 0) ) ? \
247( (t) ? (Sleep((t)->tv_sec * 1000 + (t)->tv_usec / 1000), 0) : 0 ) : \
248 (select((int)0,(r),(w),(e),(t)))
249#endif
250
251#if defined(HAVE_POLL)
252/* MHD_sys_poll_ is wrapper macro for system poll() function */
253# if !defined(MHD_WINSOCK_SOCKETS)
254# define MHD_sys_poll_ poll
255# else /* MHD_WINSOCK_SOCKETS */
256# define MHD_sys_poll_ WSAPoll
257# endif /* MHD_WINSOCK_SOCKETS */
258#endif /* HAVE_POLL */
259
260
261#ifdef MHD_WINSOCK_SOCKETS
262
263/* POSIX-W32 compatibility functions and macros */
264
265# define MHDW32ERRBASE 3300
266
267# ifndef EWOULDBLOCK
268# define EWOULDBLOCK (MHDW32ERRBASE+1)
269# endif
270# ifndef EINPROGRESS
271# define EINPROGRESS (MHDW32ERRBASE+2)
272# endif
273# ifndef EALREADY
274# define EALREADY (MHDW32ERRBASE+3)
275# endif
276# ifndef ENOTSOCK
277# define ENOTSOCK (MHDW32ERRBASE+4)
278# endif
279# ifndef EDESTADDRREQ
280# define EDESTADDRREQ (MHDW32ERRBASE+5)
281# endif
282# ifndef EMSGSIZE
283# define EMSGSIZE (MHDW32ERRBASE+6)
284# endif
285# ifndef EPROTOTYPE
286# define EPROTOTYPE (MHDW32ERRBASE+7)
287# endif
288# ifndef ENOPROTOOPT
289# define ENOPROTOOPT (MHDW32ERRBASE+8)
290# endif
291# ifndef EPROTONOSUPPORT
292# define EPROTONOSUPPORT (MHDW32ERRBASE+9)
293# endif
294# ifndef EOPNOTSUPP
295# define EOPNOTSUPP (MHDW32ERRBASE+10)
296# endif
297# ifndef EAFNOSUPPORT
298# define EAFNOSUPPORT (MHDW32ERRBASE+11)
299# endif
300# ifndef EADDRINUSE
301# define EADDRINUSE (MHDW32ERRBASE+12)
302# endif
303# ifndef EADDRNOTAVAIL
304# define EADDRNOTAVAIL (MHDW32ERRBASE+13)
305# endif
306# ifndef ENETDOWN
307# define ENETDOWN (MHDW32ERRBASE+14)
308# endif
309# ifndef ENETUNREACH
310# define ENETUNREACH (MHDW32ERRBASE+15)
311# endif
312# ifndef ENETRESET
313# define ENETRESET (MHDW32ERRBASE+16)
314# endif
315# ifndef ECONNABORTED
316# define ECONNABORTED (MHDW32ERRBASE+17)
317# endif
318# ifndef ECONNRESET
319# define ECONNRESET (MHDW32ERRBASE+18)
320# endif
321# ifndef ENOBUFS
322# define ENOBUFS (MHDW32ERRBASE+19)
323# endif
324# ifndef EISCONN
325# define EISCONN (MHDW32ERRBASE+20)
326# endif
327# ifndef ENOTCONN
328# define ENOTCONN (MHDW32ERRBASE+21)
329# endif
330# ifndef ETOOMANYREFS
331# define ETOOMANYREFS (MHDW32ERRBASE+22)
332# endif
333# ifndef ECONNREFUSED
334# define ECONNREFUSED (MHDW32ERRBASE+23)
335# endif
336# ifndef ELOOP
337# define ELOOP (MHDW32ERRBASE+24)
338# endif
339# ifndef EHOSTDOWN
340# define EHOSTDOWN (MHDW32ERRBASE+25)
341# endif
342# ifndef EHOSTUNREACH
343# define EHOSTUNREACH (MHDW32ERRBASE+26)
344# endif
345# ifndef EPROCLIM
346# define EPROCLIM (MHDW32ERRBASE+27)
347# endif
348# ifndef EUSERS
349# define EUSERS (MHDW32ERRBASE+28)
350# endif
351# ifndef EDQUOT
352# define EDQUOT (MHDW32ERRBASE+29)
353# endif
354# ifndef ESTALE
355# define ESTALE (MHDW32ERRBASE+30)
356# endif
357# ifndef EREMOTE
358# define EREMOTE (MHDW32ERRBASE+31)
359# endif
360# ifndef ESOCKTNOSUPPORT
361# define ESOCKTNOSUPPORT (MHDW32ERRBASE+32)
362# endif
363# ifndef EPFNOSUPPORT
364# define EPFNOSUPPORT (MHDW32ERRBASE+33)
365# endif
366# ifndef ESHUTDOWN
367# define ESHUTDOWN (MHDW32ERRBASE+34)
368# endif
369# ifndef ENODATA
370# define ENODATA (MHDW32ERRBASE+35)
371# endif
372# ifndef ETIMEDOUT
373# define ETIMEDOUT (MHDW32ERRBASE+36)
374# endif
375
376/**
377 * Return errno equivalent of last winsock error
378 * @return errno equivalent of last winsock error
379 */
380 int MHD_W32_errno_from_winsock_(void);
381
382/**
383 * Return pointer to string description of errnum error
384 * Works fine with both standard errno errnums
385 * and errnums from MHD_W32_errno_from_winsock_
386 * @param errnum the errno or value from MHD_W32_errno_from_winsock_()
387 * @return pointer to string description of error
388 */
389 const char* MHD_W32_strerror_(int errnum);
390
391/**
392 * Return pointer to string description of last winsock error
393 * @return pointer to string description of last winsock error
394 */
395 const char* MHD_W32_strerror_last_winsock_(void);
396
397/**
398 * Set last winsock error to equivalent of given errno value
399 * @param errnum the errno value to set
400 */
401 void MHD_W32_set_last_winsock_error_(int errnum);
402
403
404#endif /* MHD_WINSOCK_SOCKETS */
405
406#endif /* ! MHD_SOCKETS_H */