aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:12:50 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:12:50 +0000
commit50bbf74d4e1a12f5272a732feb26912db2cff63d (patch)
tree59f069464128d7df255799d2db82cd9e9765a5d4
parent2b2b1655a6abf33c76f1320940f06d2aac91b41d (diff)
downloadlibmicrohttpd-50bbf74d4e1a12f5272a732feb26912db2cff63d.tar.gz
libmicrohttpd-50bbf74d4e1a12f5272a732feb26912db2cff63d.zip
Moved sockets abstraction to specialized mhd_socket.h/.c files
-rw-r--r--configure.ac5
-rw-r--r--src/include/mhd_options.h9
-rw-r--r--src/include/microhttpd.h21
-rw-r--r--src/include/platform.h70
-rw-r--r--src/include/platform_interface.h80
-rw-r--r--src/include/w32functions.h138
-rw-r--r--src/microhttpd/Makefile.am1
-rw-r--r--src/microhttpd/connection.c6
-rw-r--r--src/microhttpd/daemon.c36
-rw-r--r--src/microhttpd/internal.h8
-rw-r--r--src/microhttpd/mhd_sockets.c545
-rw-r--r--src/microhttpd/mhd_sockets.h406
-rw-r--r--src/microhttpd/sysfdsetsize.c2
-rw-r--r--src/microhttpd/test_shutdown_select.c2
-rw-r--r--src/platform/w32functions.c516
-rw-r--r--src/testcurl/https/test_https_time_out.c1
-rw-r--r--src/testcurl/https/test_tls_extensions.c2
-rw-r--r--src/testcurl/test_get.c1
-rw-r--r--src/testcurl/test_get_sendfile.c3
-rw-r--r--src/testcurl/test_options.c1
-rw-r--r--src/testcurl/test_quiesce.c1
-rw-r--r--w32/common/libmicrohttpd-files.vcxproj2
-rw-r--r--w32/common/libmicrohttpd-filters.vcxproj6
23 files changed, 1010 insertions, 852 deletions
diff --git a/configure.ac b/configure.ac
index f99da256..1ef58edb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -545,7 +545,10 @@ fi
545AC_CHECK_HEADERS([fcntl.h math.h errno.h limits.h stdio.h locale.h sys/stat.h sys/types.h],,AC_MSG_ERROR([Compiling libmicrohttpd requires standard UNIX headers files])) 545AC_CHECK_HEADERS([fcntl.h math.h errno.h limits.h stdio.h locale.h sys/stat.h sys/types.h],,AC_MSG_ERROR([Compiling libmicrohttpd requires standard UNIX headers files]))
546 546
547# Check for optional headers 547# Check for optional headers
548AC_CHECK_HEADERS([sys/types.h sys/time.h sys/msg.h netdb.h netinet/in.h netinet/tcp.h time.h sys/socket.h sys/mman.h arpa/inet.h sys/select.h search.h endian.h machine/endian.h sys/endian.h sys/param.h sys/machine.h sys/byteorder.h machine/param.h sys/isa_defs.h inttypes.h stddef.h]) 548AC_CHECK_HEADERS([sys/types.h sys/time.h sys/msg.h netdb.h netinet/in.h netinet/tcp.h time.h sys/socket.h sys/mman.h arpa/inet.h sys/select.h search.h \
549 endian.h machine/endian.h sys/endian.h sys/param.h sys/machine.h sys/byteorder.h machine/param.h sys/isa_defs.h \
550 inttypes.h stddef.h \
551 sockLib.h inetLib.h net/if.h])
549AM_CONDITIONAL([HAVE_TSEARCH], [test "x$ac_cv_header_search_h" = "xyes"]) 552AM_CONDITIONAL([HAVE_TSEARCH], [test "x$ac_cv_header_search_h" = "xyes"])
550 553
551AC_CHECK_MEMBER([struct sockaddr_in.sin_len], 554AC_CHECK_MEMBER([struct sockaddr_in.sin_len],
diff --git a/src/include/mhd_options.h b/src/include/mhd_options.h
index 2e798ebf..5965001e 100644
--- a/src/include/mhd_options.h
+++ b/src/include/mhd_options.h
@@ -48,16 +48,11 @@
48 headers. */ 48 headers. */
49#ifdef FD_SETSIZE 49#ifdef FD_SETSIZE
50/* FD_SETSIZE defined in command line or in MHD_config.h */ 50/* FD_SETSIZE defined in command line or in MHD_config.h */
51/* Use function to retrieve system default FD_SETSIZE value. */
52#define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value()
53#elif defined(_WIN32) && !defined(__CYGWIN__) 51#elif defined(_WIN32) && !defined(__CYGWIN__)
54/* Platform with WinSock and without overridden FD_SETSIZE */ 52/* Platform with WinSock and without overridden FD_SETSIZE */
55#define FD_SETSIZE 2048 /* Override default small value */ 53#define FD_SETSIZE 2048 /* Override default small value */
56/* Use function to retrieve system default FD_SETSIZE value. */
57#define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value()
58#else /* !FD_SETSIZE && !WinSock*/ 54#else /* !FD_SETSIZE && !WinSock*/
59/* System default value of FD_SETSIZE is used */ 55/* System default value of FD_SETSIZE is used */
60#define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
61#define _MHD_FD_SETSIZE_IS_DEFAULT 1 56#define _MHD_FD_SETSIZE_IS_DEFAULT 1
62#endif /* !FD_SETSIZE && !WinSock*/ 57#endif /* !FD_SETSIZE && !WinSock*/
63 58
@@ -83,6 +78,10 @@
83#endif /* !WIN32_LEAN_AND_MEAN */ 78#endif /* !WIN32_LEAN_AND_MEAN */
84#endif /* _WIN32 */ 79#endif /* _WIN32 */
85 80
81#if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
82#define RESTRICT __restrict__
83#endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
84
86#if LINUX+0 && (defined(HAVE_SENDFILE64) || defined(HAVE_LSEEK64)) && ! defined(_LARGEFILE64_SOURCE) 85#if LINUX+0 && (defined(HAVE_SENDFILE64) || defined(HAVE_LSEEK64)) && ! defined(_LARGEFILE64_SOURCE)
87/* On Linux, special macro is required to enable definitions of some xxx64 functions */ 86/* On Linux, special macro is required to enable definitions of some xxx64 functions */
88#define _LARGEFILE64_SOURCE 1 87#define _LARGEFILE64_SOURCE 1
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index d5e08d82..17828202 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -60,14 +60,6 @@
60 * thread-safe (with the exception of #MHD_set_connection_value, 60 * thread-safe (with the exception of #MHD_set_connection_value,
61 * which must only be used in a particular context). 61 * which must only be used in a particular context).
62 * 62 *
63 * NEW: Before including "microhttpd.h" you should add the necessary
64 * includes to define the `uint64_t`, `size_t`, `fd_set`, `socklen_t`
65 * and `struct sockaddr` data types (which headers are needed may
66 * depend on your platform; for possible suggestions consult
67 * "platform.h" in the MHD distribution). If you have done so, you
68 * should also have a line with "#define MHD_PLATFORM_H" which will
69 * prevent this header from trying (and, depending on your platform,
70 * failing) to include the right headers.
71 * 63 *
72 * @defgroup event event-loop control 64 * @defgroup event event-loop control
73 * MHD API to start and stop the HTTP server and manage the event loop. 65 * MHD API to start and stop the HTTP server and manage the event loop.
@@ -100,11 +92,14 @@ extern "C"
100 hence works on any platform, we use "standard" includes here 92 hence works on any platform, we use "standard" includes here
101 to build out-of-the-box for beginning users on common systems. 93 to build out-of-the-box for beginning users on common systems.
102 94
103 Once you have a proper build system and go for more exotic 95 If generic headers don't work on your platform, include headers
104 platforms, you should define MHD_PLATFORM_H in some header that 96 which define 'va_list', 'size_t', 'ssize_t', 'intptr_t',
105 you always include *before* "microhttpd.h". Then the following 97 'uint16_t', 'uint32_t', 'uint64_t', 'off_t', 'struct sockaddr',
106 "standard" includes won't be used (which might be a good 98 'socklen_t', 'fd_set' and "#define MHD_PLATFORM_H" before
107 idea, especially on platforms where they do not exist). */ 99 including "microhttpd.h". Then the following "standard"
100 includes won't be used (which might be a good idea, especially
101 on platforms where they do not exist).
102 */
108#ifndef MHD_PLATFORM_H 103#ifndef MHD_PLATFORM_H
109#include <stdarg.h> 104#include <stdarg.h>
110#include <stdint.h> 105#include <stdint.h>
diff --git a/src/include/platform.h b/src/include/platform.h
index 5ec2c16f..e9c2fa12 100644
--- a/src/include/platform.h
+++ b/src/include/platform.h
@@ -55,13 +55,17 @@
55 are available) */ 55 are available) */
56 56
57 57
58#ifdef OS_VXWORKS 58#if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
59#include <sockLib.h>
60#include <netinet/in.h>
61#include <stdarg.h> 59#include <stdarg.h>
62#include <sys/mman.h> 60#include <sys/mman.h>
63#define RESTRICT __restrict__ 61#ifdef HAVE_SOCKLIB_H
64#endif 62#include <sockLib.h>
63#endif /* HAVE_SOCKLIB_H */
64#ifdef HAVE_INETLIB_H
65#include <inetLib.h>
66#endif /* HAVE_INETLIB_H */
67#endif /* __VXWORKS__ */
68
65#if HAVE_MEMORY_H 69#if HAVE_MEMORY_H
66#include <memory.h> 70#include <memory.h>
67#endif 71#endif
@@ -84,21 +88,20 @@
84#if HAVE_SYS_MMAN_H 88#if HAVE_SYS_MMAN_H
85#include <sys/mman.h> 89#include <sys/mman.h>
86#endif 90#endif
87#if HAVE_NETDB_H
88#include <netdb.h>
89#endif
90#if HAVE_NETINET_IN_H
91#include <netinet/in.h>
92#endif
93#if HAVE_TIME_H 91#if HAVE_TIME_H
94#include <time.h> 92#include <time.h>
95#endif 93#endif
96#if HAVE_SYS_SOCKET_H 94#if HAVE_SYS_SOCKET_H
97#include <sys/socket.h> 95#include <sys/socket.h>
98#endif 96#endif
99#if HAVE_ARPA_INET_H 97#if defined(_WIN32) && !defined(__CYGWIN__)
100#include <arpa/inet.h> 98#ifndef WIN32_LEAN_AND_MEAN
101#endif 99/* Do not include unneeded parts of W32 headers. */
100#define WIN32_LEAN_AND_MEAN 1
101#endif /* !WIN32_LEAN_AND_MEAN */
102#include <winsock2.h>
103#include <ws2tcpip.h>
104#endif /* _WIN32 && !__CYGWIN__ */
102 105
103#if defined(__CYGWIN__) && !defined(_SYS_TYPES_FD_SET) 106#if defined(__CYGWIN__) && !defined(_SYS_TYPES_FD_SET)
104/* Do not define __USE_W32_SOCKETS under Cygwin! */ 107/* Do not define __USE_W32_SOCKETS under Cygwin! */
@@ -106,58 +109,23 @@
106#endif 109#endif
107 110
108#if defined(_WIN32) && !defined(__CYGWIN__) 111#if defined(_WIN32) && !defined(__CYGWIN__)
109#include <ws2tcpip.h>
110#define sleep(seconds) ((SleepEx((seconds)*1000, 1)==0)?0:(seconds)) 112#define sleep(seconds) ((SleepEx((seconds)*1000, 1)==0)?0:(seconds))
111#define usleep(useconds) ((SleepEx((useconds)/1000, 1)==0)?0:-1) 113#define usleep(useconds) ((SleepEx((useconds)/1000, 1)==0)?0:-1)
112#endif 114#endif
113 115
114#if !defined(SHUT_WR) && defined(SD_SEND)
115#define SHUT_WR SD_SEND
116#endif
117#if !defined(SHUT_RD) && defined(SD_RECEIVE)
118#define SHUT_RD SD_RECEIVE
119#endif
120#if !defined(SHUT_RDWR) && defined(SD_BOTH)
121#define SHUT_RDWR SD_BOTH
122#endif
123
124#if defined(_MSC_FULL_VER) && !defined (_SSIZE_T_DEFINED) 116#if defined(_MSC_FULL_VER) && !defined (_SSIZE_T_DEFINED)
125#define _SSIZE_T_DEFINED 117#define _SSIZE_T_DEFINED
126typedef intptr_t ssize_t; 118typedef intptr_t ssize_t;
127#endif /* !_SSIZE_T_DEFINED */ 119#endif /* !_SSIZE_T_DEFINED */
128 120
129#ifndef MHD_SOCKET_DEFINED
130/**
131 * MHD_socket is type for socket FDs
132 */
133#if !defined(_WIN32) || defined(__CYGWIN__)
134#define MHD_POSIX_SOCKETS 1
135typedef int MHD_socket;
136#define MHD_INVALID_SOCKET (-1)
137#else /* defined(_WIN32) && !defined(__CYGWIN__) */
138#define MHD_WINSOCK_SOCKETS 1
139#include <winsock2.h>
140typedef SOCKET MHD_socket;
141#define MHD_INVALID_SOCKET (INVALID_SOCKET)
142#endif /* defined(_WIN32) && !defined(__CYGWIN__) */
143#define MHD_SOCKET_DEFINED 1
144#endif /* MHD_SOCKET_DEFINED */
145
146/**
147 * _MHD_SOCKOPT_BOOL_TYPE is type for bool parameters for setsockopt()/getsockopt()
148 */
149#ifdef MHD_POSIX_SOCKETS
150typedef int _MHD_SOCKOPT_BOOL_TYPE;
151#else /* MHD_WINSOCK_SOCKETS */
152typedef BOOL _MHD_SOCKOPT_BOOL_TYPE;
153#endif /* MHD_WINSOCK_SOCKETS */
154
155#ifndef _WIN32 121#ifndef _WIN32
156typedef time_t _MHD_TIMEVAL_TV_SEC_TYPE; 122typedef time_t _MHD_TIMEVAL_TV_SEC_TYPE;
157#else /* _WIN32 */ 123#else /* _WIN32 */
158typedef long _MHD_TIMEVAL_TV_SEC_TYPE; 124typedef long _MHD_TIMEVAL_TV_SEC_TYPE;
159#endif /* _WIN32 */ 125#endif /* _WIN32 */
160 126
127/* TODO: remove include when pipes implementation is moved to other file */
128#include "../microhttpd/mhd_sockets.h"
161/* Force don't use pipes on W32 */ 129/* Force don't use pipes on W32 */
162#if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) 130#if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES)
163#define MHD_DONT_USE_PIPES 1 131#define MHD_DONT_USE_PIPES 1
diff --git a/src/include/platform_interface.h b/src/include/platform_interface.h
index c2aee64b..fa84bcd5 100644
--- a/src/include/platform_interface.h
+++ b/src/include/platform_interface.h
@@ -27,6 +27,7 @@
27#define MHD_PLATFORM_INTERFACE_H 27#define MHD_PLATFORM_INTERFACE_H
28 28
29#include "platform.h" 29#include "platform.h"
30#include "../microhttpd/mhd_sockets.h"
30#if defined(_WIN32) && !defined(__CYGWIN__) 31#if defined(_WIN32) && !defined(__CYGWIN__)
31#include "w32functions.h" 32#include "w32functions.h"
32#endif 33#endif
@@ -46,85 +47,6 @@
46#endif /* ! _WIN32*/ 47#endif /* ! _WIN32*/
47#endif /* ! HAVE_SNPRINTF */ 48#endif /* ! HAVE_SNPRINTF */
48 49
49
50/**
51 * _MHD_socket_funcs_size is type used to specify size for send and recv
52 * functions
53 */
54#if !defined(MHD_WINSOCK_SOCKETS)
55typedef size_t _MHD_socket_funcs_size;
56#else
57typedef int _MHD_socket_funcs_size;
58#endif
59
60/**
61 * MHD_socket_close_(fd) close any FDs (non-W32) / close only socket
62 * FDs (W32). Note that on HP-UNIX, this function may leak the FD if
63 * errno is set to EINTR. Do not use HP-UNIX.
64 *
65 * @param fd descriptor to close
66 * @return 0 on success (error codes like EINTR and EIO are counted as success,
67 * only EBADF counts as an error!)
68 */
69#if !defined(MHD_WINSOCK_SOCKETS)
70#define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0)
71#else
72#define MHD_socket_close_(fd) closesocket((fd))
73#endif
74
75/**
76 * MHD_socket_errno_ is errno of last function (non-W32) / errno of
77 * last socket function (W32)
78 */
79#if !defined(MHD_WINSOCK_SOCKETS)
80#define MHD_socket_errno_ errno
81#else
82#define MHD_socket_errno_ MHD_W32_errno_from_winsock_()
83#endif
84
85/* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
86 * description string of last socket error (W32) */
87#if !defined(MHD_WINSOCK_SOCKETS)
88#define MHD_socket_last_strerr_() strerror(errno)
89#else
90#define MHD_socket_last_strerr_() MHD_W32_strerror_last_winsock_()
91#endif
92
93/* MHD_strerror_ is strerror (both non-W32/W32) */
94#if !defined(MHD_WINSOCK_SOCKETS)
95#define MHD_strerror_(errnum) strerror((errnum))
96#else
97#define MHD_strerror_(errnum) MHD_W32_strerror_((errnum))
98#endif
99
100/* MHD_set_socket_errno_ set errno to errnum (non-W32) / set socket last error to errnum (W32) */
101#if !defined(MHD_WINSOCK_SOCKETS)
102#define MHD_set_socket_errno_(errnum) errno=(errnum)
103#else
104#define MHD_set_socket_errno_(errnum) MHD_W32_set_last_winsock_error_((errnum))
105#endif
106
107/* MHD_SYS_select_ is wrapper macro for system select() function */
108#if !defined(MHD_WINSOCK_SOCKETS)
109#define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
110#else
111#define MHD_SYS_select_(n,r,w,e,t) \
112 ( (!(r) || ((fd_set*)(r))->fd_count == 0) && \
113 (!(w) || ((fd_set*)(w))->fd_count == 0) && \
114 (!(e) || ((fd_set*)(e))->fd_count == 0) ) ? \
115 ( (t) ? (Sleep((t)->tv_sec * 1000 + (t)->tv_usec / 1000), 0) : 0 ) : \
116 (select((int)0,(r),(w),(e),(t)))
117#endif
118
119#if defined(HAVE_POLL)
120/* MHD_sys_poll_ is wrapper macro for system poll() function */
121#if !defined(MHD_WINSOCK_SOCKETS)
122#define MHD_sys_poll_ poll
123#else /* MHD_WINSOCK_SOCKETS */
124#define MHD_sys_poll_ WSAPoll
125#endif /* MHD_WINSOCK_SOCKETS */
126#endif /* HAVE_POLL */
127
128/* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / 50/* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) /
129 * create two connected sockets (MHD_DONT_USE_PIPES) */ 51 * create two connected sockets (MHD_DONT_USE_PIPES) */
130#ifndef MHD_DONT_USE_PIPES 52#ifndef MHD_DONT_USE_PIPES
diff --git a/src/include/w32functions.h b/src/include/w32functions.h
index 508778c9..f8445f30 100644
--- a/src/include/w32functions.h
+++ b/src/include/w32functions.h
@@ -39,144 +39,6 @@ extern "C"
39{ 39{
40#endif 40#endif
41 41
42#define MHDW32ERRBASE 3300
43
44#ifndef EWOULDBLOCK
45#define EWOULDBLOCK (MHDW32ERRBASE+1)
46#endif
47#ifndef EINPROGRESS
48#define EINPROGRESS (MHDW32ERRBASE+2)
49#endif
50#ifndef EALREADY
51#define EALREADY (MHDW32ERRBASE+3)
52#endif
53#ifndef ENOTSOCK
54#define ENOTSOCK (MHDW32ERRBASE+4)
55#endif
56#ifndef EDESTADDRREQ
57#define EDESTADDRREQ (MHDW32ERRBASE+5)
58#endif
59#ifndef EMSGSIZE
60#define EMSGSIZE (MHDW32ERRBASE+6)
61#endif
62#ifndef EPROTOTYPE
63#define EPROTOTYPE (MHDW32ERRBASE+7)
64#endif
65#ifndef ENOPROTOOPT
66#define ENOPROTOOPT (MHDW32ERRBASE+8)
67#endif
68#ifndef EPROTONOSUPPORT
69#define EPROTONOSUPPORT (MHDW32ERRBASE+9)
70#endif
71#ifndef EOPNOTSUPP
72#define EOPNOTSUPP (MHDW32ERRBASE+10)
73#endif
74#ifndef EAFNOSUPPORT
75#define EAFNOSUPPORT (MHDW32ERRBASE+11)
76#endif
77#ifndef EADDRINUSE
78#define EADDRINUSE (MHDW32ERRBASE+12)
79#endif
80#ifndef EADDRNOTAVAIL
81#define EADDRNOTAVAIL (MHDW32ERRBASE+13)
82#endif
83#ifndef ENETDOWN
84#define ENETDOWN (MHDW32ERRBASE+14)
85#endif
86#ifndef ENETUNREACH
87#define ENETUNREACH (MHDW32ERRBASE+15)
88#endif
89#ifndef ENETRESET
90#define ENETRESET (MHDW32ERRBASE+16)
91#endif
92#ifndef ECONNABORTED
93#define ECONNABORTED (MHDW32ERRBASE+17)
94#endif
95#ifndef ECONNRESET
96#define ECONNRESET (MHDW32ERRBASE+18)
97#endif
98#ifndef ENOBUFS
99#define ENOBUFS (MHDW32ERRBASE+19)
100#endif
101#ifndef EISCONN
102#define EISCONN (MHDW32ERRBASE+20)
103#endif
104#ifndef ENOTCONN
105#define ENOTCONN (MHDW32ERRBASE+21)
106#endif
107#ifndef ETOOMANYREFS
108#define ETOOMANYREFS (MHDW32ERRBASE+22)
109#endif
110#ifndef ECONNREFUSED
111#define ECONNREFUSED (MHDW32ERRBASE+23)
112#endif
113#ifndef ELOOP
114#define ELOOP (MHDW32ERRBASE+24)
115#endif
116#ifndef EHOSTDOWN
117#define EHOSTDOWN (MHDW32ERRBASE+25)
118#endif
119#ifndef EHOSTUNREACH
120#define EHOSTUNREACH (MHDW32ERRBASE+26)
121#endif
122#ifndef EPROCLIM
123#define EPROCLIM (MHDW32ERRBASE+27)
124#endif
125#ifndef EUSERS
126#define EUSERS (MHDW32ERRBASE+28)
127#endif
128#ifndef EDQUOT
129#define EDQUOT (MHDW32ERRBASE+29)
130#endif
131#ifndef ESTALE
132#define ESTALE (MHDW32ERRBASE+30)
133#endif
134#ifndef EREMOTE
135#define EREMOTE (MHDW32ERRBASE+31)
136#endif
137#ifndef ESOCKTNOSUPPORT
138#define ESOCKTNOSUPPORT (MHDW32ERRBASE+32)
139#endif
140#ifndef EPFNOSUPPORT
141#define EPFNOSUPPORT (MHDW32ERRBASE+33)
142#endif
143#ifndef ESHUTDOWN
144#define ESHUTDOWN (MHDW32ERRBASE+34)
145#endif
146#ifndef ENODATA
147#define ENODATA (MHDW32ERRBASE+35)
148#endif
149#ifndef ETIMEDOUT
150#define ETIMEDOUT (MHDW32ERRBASE+36)
151#endif
152
153/**
154 * Return errno equivalent of last winsock error
155 * @return errno equivalent of last winsock error
156 */
157int MHD_W32_errno_from_winsock_(void);
158
159/**
160 * Return pointer to string description of errnum error
161 * Works fine with both standard errno errnums
162 * and errnums from MHD_W32_errno_from_winsock_
163 * @param errnum the errno or value from MHD_W32_errno_from_winsock_()
164 * @return pointer to string description of error
165 */
166const char* MHD_W32_strerror_(int errnum);
167
168/**
169 * Return pointer to string description of last winsock error
170 * @return pointer to string description of last winsock error
171 */
172const char* MHD_W32_strerror_last_winsock_(void);
173
174/**
175 * Set last winsock error to equivalent of given errno value
176 * @param errnum the errno value to set
177 */
178void MHD_W32_set_last_winsock_error_(int errnum);
179
180/** 42/**
181 * Create pair of mutually connected TCP/IP sockets on loopback address 43 * Create pair of mutually connected TCP/IP sockets on loopback address
182 * @param sockets_pair array to receive resulted sockets 44 * @param sockets_pair array to receive resulted sockets
diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am
index 90324328..371829a5 100644
--- a/src/microhttpd/Makefile.am
+++ b/src/microhttpd/Makefile.am
@@ -69,6 +69,7 @@ libmicrohttpd_la_SOURCES = \
69 mhd_str.c mhd_str.h \ 69 mhd_str.c mhd_str.h \
70 mhd_threads.c mhd_threads.h \ 70 mhd_threads.c mhd_threads.h \
71 mhd_locks.h \ 71 mhd_locks.h \
72 mhd_sockets.c mhd_sockets.h \
72 response.c response.h 73 response.c response.h
73libmicrohttpd_la_CPPFLAGS = \ 74libmicrohttpd_la_CPPFLAGS = \
74 $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \ 75 $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 6223b994..037a33ff 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -32,11 +32,7 @@
32#include "mhd_mono_clock.h" 32#include "mhd_mono_clock.h"
33#include "mhd_str.h" 33#include "mhd_str.h"
34#include "mhd_locks.h" 34#include "mhd_locks.h"
35 35#include "mhd_sockets.h"
36#if HAVE_NETINET_TCP_H
37/* for TCP_CORK */
38#include <netinet/tcp.h>
39#endif
40 36
41 37
42/** 38/**
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index a2f7ec7f..3d0b5878 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -34,6 +34,7 @@
34#include "autoinit_funcs.h" 34#include "autoinit_funcs.h"
35#include "mhd_mono_clock.h" 35#include "mhd_mono_clock.h"
36#include "mhd_locks.h" 36#include "mhd_locks.h"
37#include "mhd_sockets.h"
37 38
38#if HAVE_SEARCH_H 39#if HAVE_SEARCH_H
39#include <search.h> 40#include <search.h>
@@ -46,18 +47,10 @@
46#include <gcrypt.h> 47#include <gcrypt.h>
47#endif 48#endif
48 49
49#if defined(HAVE_POLL_H) && defined(HAVE_POLL)
50#include <poll.h>
51#endif
52
53#ifdef LINUX 50#ifdef LINUX
54#include <sys/sendfile.h> 51#include <sys/sendfile.h>
55#endif 52#endif
56 53
57#ifndef _MHD_FD_SETSIZE_IS_DEFAULT
58#include "sysfdsetsize.h"
59#endif /* !_MHD_FD_SETSIZE_IS_DEFAULT */
60
61#ifdef _WIN32 54#ifdef _WIN32
62#ifndef WIN32_LEAN_AND_MEAN 55#ifndef WIN32_LEAN_AND_MEAN
63#define WIN32_LEAN_AND_MEAN 1 56#define WIN32_LEAN_AND_MEAN 1
@@ -79,13 +72,6 @@
79 */ 72 */
80#define MHD_POOL_SIZE_DEFAULT (32 * 1024) 73#define MHD_POOL_SIZE_DEFAULT (32 * 1024)
81 74
82#ifdef TCP_FASTOPEN
83/**
84 * Default TCP fastopen queue size.
85 */
86#define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
87#endif
88
89/** 75/**
90 * Print extra messages with reasons for closing 76 * Print extra messages with reasons for closing
91 * sockets? (only adds non-error messages). 77 * sockets? (only adds non-error messages).
@@ -104,26 +90,6 @@
104#endif 90#endif
105#endif 91#endif
106 92
107#ifdef SOCK_CLOEXEC
108#define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
109#else /* ! SOCK_CLOEXEC */
110#define MAYBE_SOCK_CLOEXEC 0
111#endif /* ! SOCK_CLOEXEC */
112
113#ifdef HAVE_SOCK_NONBLOCK
114#define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
115#else /* ! HAVE_SOCK_NONBLOCK */
116#define MAYBE_SOCK_NONBLOCK 0
117#endif /* ! HAVE_SOCK_NONBLOCK */
118
119#if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC))
120#define USE_ACCEPT4 1
121#endif
122
123#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
124#define USE_EPOLL_CREATE1 1
125#endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
126
127 93
128/** 94/**
129 * Default implementation of the panic function, 95 * Default implementation of the panic function,
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index 510d56a5..75aa1411 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -36,15 +36,9 @@
36#include <gnutls/abstract.h> 36#include <gnutls/abstract.h>
37#endif 37#endif
38#endif 38#endif
39#if EPOLL_SUPPORT
40#include <sys/epoll.h>
41#endif
42#if HAVE_NETINET_TCP_H
43/* for TCP_FASTOPEN */
44#include <netinet/tcp.h>
45#endif
46#include "mhd_threads.h" 39#include "mhd_threads.h"
47#include "mhd_locks.h" 40#include "mhd_locks.h"
41#include "mhd_sockets.h"
48 42
49 43
50/** 44/**
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
new file mode 100644
index 00000000..4214108e
--- /dev/null
+++ b/src/microhttpd/mhd_sockets.c
@@ -0,0 +1,545 @@
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 Implementation for sockets functions
24 * @author Karlson2k (Evgeny Grin)
25 */
26
27#include "mhd_sockets.h"
28
29#ifdef MHD_WINSOCK_SOCKETS
30
31/**
32 * Return errno equivalent of last winsock error
33 * @return errno equivalent of last winsock error
34 */
35int MHD_W32_errno_from_winsock_(void)
36{
37 switch(WSAGetLastError())
38 {
39 case 0: return 0;
40 case WSA_INVALID_HANDLE: return EBADF;
41 case WSA_NOT_ENOUGH_MEMORY: return ENOMEM;
42 case WSA_INVALID_PARAMETER: return EINVAL;
43 case WSAEINTR: return EINTR;
44 case WSAEWOULDBLOCK: return EWOULDBLOCK;
45 case WSAEINPROGRESS: return EINPROGRESS;
46 case WSAEALREADY: return EALREADY;
47 case WSAENOTSOCK: return ENOTSOCK;
48 case WSAEDESTADDRREQ: return EDESTADDRREQ;
49 case WSAEMSGSIZE: return EMSGSIZE;
50 case WSAEPROTOTYPE: return EPROTOTYPE;
51 case WSAENOPROTOOPT: return ENOPROTOOPT;
52 case WSAEPROTONOSUPPORT: return EPROTONOSUPPORT;
53 case WSAESOCKTNOSUPPORT: return ESOCKTNOSUPPORT;
54 case WSAEOPNOTSUPP: return EOPNOTSUPP;
55 case WSAEPFNOSUPPORT: return EPFNOSUPPORT;
56 case WSAEAFNOSUPPORT: return EAFNOSUPPORT;
57 case WSAEADDRINUSE: return EADDRINUSE;
58 case WSAEADDRNOTAVAIL: return EADDRNOTAVAIL;
59 case WSAENETDOWN: return ENETDOWN;
60 case WSAENETUNREACH: return ENETUNREACH;
61 case WSAENETRESET: return ENETRESET;
62 case WSAECONNABORTED: return ECONNABORTED;
63 case WSAECONNRESET: return ECONNRESET;
64 case WSAENOBUFS: return ENOBUFS;
65 case WSAEISCONN: return EISCONN;
66 case WSAENOTCONN: return ENOTCONN;
67 case WSAESHUTDOWN: return ESHUTDOWN;
68 case WSAETOOMANYREFS: return ETOOMANYREFS;
69 case WSAETIMEDOUT: return ETIMEDOUT;
70 case WSAECONNREFUSED: return ECONNREFUSED;
71 case WSAELOOP: return ELOOP;
72 case WSAENAMETOOLONG: return ENAMETOOLONG;
73 case WSAEHOSTDOWN: return EHOSTDOWN;
74 case WSAEHOSTUNREACH: return EHOSTUNREACH;
75 case WSAENOTEMPTY: return ENOTEMPTY;
76 case WSAEPROCLIM: return EPROCLIM;
77 case WSAEUSERS: return EUSERS;
78 case WSAEDQUOT: return EDQUOT;
79 case WSAESTALE: return ESTALE;
80 case WSAEREMOTE: return EREMOTE;
81 case WSAEINVAL: return EINVAL;
82 case WSAEFAULT: return EFAULT;
83 case WSANO_DATA: return ENODATA;
84 /* Rough equivalents */
85 case WSAEDISCON: return ECONNRESET;
86 case WSAEINVALIDPROCTABLE: return EFAULT;
87 case WSASYSNOTREADY:
88 case WSANOTINITIALISED:
89 case WSASYSCALLFAILURE: return ENOBUFS;
90 case WSAVERNOTSUPPORTED: return EOPNOTSUPP;
91 case WSAEREFUSED: return EIO;
92 }
93 return EINVAL;
94}
95
96/**
97 * Return pointer to string description of errnum error
98 * Works fine with both standard errno errnums
99 * and errnums from MHD_W32_errno_from_winsock_
100 * @param errnum the errno or value from MHD_W32_errno_from_winsock_()
101 * @return pointer to string description of error
102 */
103const char* MHD_W32_strerror_(int errnum)
104{
105 switch(errnum)
106 {
107 case 0:
108 return "No error";
109 case EWOULDBLOCK:
110 return "Operation would block";
111 case EINPROGRESS:
112 return "Connection already in progress";
113 case EALREADY:
114 return "Socket already connected";
115 case ENOTSOCK:
116 return "Socket operation on non-socket";
117 case EDESTADDRREQ:
118 return "Destination address required";
119 case EMSGSIZE:
120 return "Message too long";
121 case EPROTOTYPE:
122 return "Protocol wrong type for socket";
123 case ENOPROTOOPT:
124 return "Protocol not available";
125 case EPROTONOSUPPORT:
126 return "Unknown protocol";
127 case ESOCKTNOSUPPORT:
128 return "Socket type not supported";
129 case EOPNOTSUPP:
130 return "Operation not supported on socket";
131 case EPFNOSUPPORT:
132 return "Protocol family not supported";
133 case EAFNOSUPPORT:
134 return "Address family not supported by protocol family";
135 case EADDRINUSE:
136 return "Address already in use";
137 case EADDRNOTAVAIL:
138 return "Cannot assign requested address";
139 case ENETDOWN:
140 return "Network is down";
141 case ENETUNREACH:
142 return "Network is unreachable";
143 case ENETRESET:
144 return "Network dropped connection on reset";
145 case ECONNABORTED:
146 return "Software caused connection abort";
147 case ECONNRESET:
148 return "Connection reset by peer";
149 case ENOBUFS:
150 return "No system resources available";
151 case EISCONN:
152 return "Socket is already connected";
153 case ENOTCONN:
154 return "Socket is not connected";
155 case ESHUTDOWN:
156 return "Can't send after socket shutdown";
157 case ETOOMANYREFS:
158 return "Too many references: cannot splice";
159 case ETIMEDOUT:
160 return "Connection timed out";
161 case ECONNREFUSED:
162 return "Connection refused";
163 case ELOOP:
164 return "Cannot translate name";
165 case EHOSTDOWN:
166 return "Host is down";
167 case EHOSTUNREACH:
168 return "Host is unreachable";
169 case EPROCLIM:
170 return "Too many processes";
171 case EUSERS:
172 return "Too many users";
173 case EDQUOT:
174 return "Disk quota exceeded";
175 case ESTALE:
176 return "Stale file handle reference";
177 case EREMOTE:
178 return "Resource is remote";
179 case ENODATA:
180 return "No data available";
181 }
182 return strerror(errnum);
183}
184
185/**
186 * Return pointer to string description of last winsock error
187 * @return pointer to string description of last winsock error
188 */
189const char* MHD_W32_strerror_last_winsock_(void)
190{
191 switch (WSAGetLastError())
192 {
193 case 0:
194 return "No error";
195 case WSA_INVALID_HANDLE:
196 return "Specified event object handle is invalid";
197 case WSA_NOT_ENOUGH_MEMORY:
198 return "Insufficient memory available";
199 case WSA_INVALID_PARAMETER:
200 return "One or more parameters are invalid";
201 case WSA_OPERATION_ABORTED:
202 return "Overlapped operation aborted";
203 case WSA_IO_INCOMPLETE:
204 return "Overlapped I/O event object not in signaled state";
205 case WSA_IO_PENDING:
206 return "Overlapped operations will complete later";
207 case WSAEINTR:
208 return "Interrupted function call";
209 case WSAEBADF:
210 return "File handle is not valid";
211 case WSAEACCES:
212 return "Permission denied";
213 case WSAEFAULT:
214 return "Bad address";
215 case WSAEINVAL:
216 return "Invalid argument";
217 case WSAEMFILE:
218 return "Too many open files";
219 case WSAEWOULDBLOCK:
220 return "Resource temporarily unavailable";
221 case WSAEINPROGRESS:
222 return "Operation now in progress";
223 case WSAEALREADY:
224 return "Operation already in progress";
225 case WSAENOTSOCK:
226 return "Socket operation on nonsocket";
227 case WSAEDESTADDRREQ:
228 return "Destination address required";
229 case WSAEMSGSIZE:
230 return "Message too long";
231 case WSAEPROTOTYPE:
232 return "Protocol wrong type for socket";
233 case WSAENOPROTOOPT:
234 return "Bad protocol option";
235 case WSAEPROTONOSUPPORT:
236 return "Protocol not supported";
237 case WSAESOCKTNOSUPPORT:
238 return "Socket type not supported";
239 case WSAEOPNOTSUPP:
240 return "Operation not supported";
241 case WSAEPFNOSUPPORT:
242 return "Protocol family not supported";
243 case WSAEAFNOSUPPORT:
244 return "Address family not supported by protocol family";
245 case WSAEADDRINUSE:
246 return "Address already in use";
247 case WSAEADDRNOTAVAIL:
248 return "Cannot assign requested address";
249 case WSAENETDOWN:
250 return "Network is down";
251 case WSAENETUNREACH:
252 return "Network is unreachable";
253 case WSAENETRESET:
254 return "Network dropped connection on reset";
255 case WSAECONNABORTED:
256 return "Software caused connection abort";
257 case WSAECONNRESET:
258 return "Connection reset by peer";
259 case WSAENOBUFS:
260 return "No buffer space available";
261 case WSAEISCONN:
262 return "Socket is already connected";
263 case WSAENOTCONN:
264 return "Socket is not connected";
265 case WSAESHUTDOWN:
266 return "Cannot send after socket shutdown";
267 case WSAETOOMANYREFS:
268 return "Too many references";
269 case WSAETIMEDOUT:
270 return "Connection timed out";
271 case WSAECONNREFUSED:
272 return "Connection refused";
273 case WSAELOOP:
274 return "Cannot translate name";
275 case WSAENAMETOOLONG:
276 return "Name too long";
277 case WSAEHOSTDOWN:
278 return "Host is down";
279 case WSAEHOSTUNREACH:
280 return "No route to host";
281 case WSAENOTEMPTY:
282 return "Directory not empty";
283 case WSAEPROCLIM:
284 return "Too many processes";
285 case WSAEUSERS:
286 return "User quota exceeded";
287 case WSAEDQUOT:
288 return "Disk quota exceeded";
289 case WSAESTALE:
290 return "Stale file handle reference";
291 case WSAEREMOTE:
292 return "Item is remote";
293 case WSASYSNOTREADY:
294 return "Network subsystem is unavailable";
295 case WSAVERNOTSUPPORTED:
296 return "Winsock.dll version out of range";
297 case WSANOTINITIALISED:
298 return "Successful WSAStartup not yet performed";
299 case WSAEDISCON:
300 return "Graceful shutdown in progress";
301 case WSAENOMORE:
302 return "No more results";
303 case WSAECANCELLED:
304 return "Call has been canceled";
305 case WSAEINVALIDPROCTABLE:
306 return "Procedure call table is invalid";
307 case WSAEINVALIDPROVIDER:
308 return "Service provider is invalid";
309 case WSAEPROVIDERFAILEDINIT:
310 return "Service provider failed to initialize";
311 case WSASYSCALLFAILURE:
312 return "System call failure";
313 case WSASERVICE_NOT_FOUND:
314 return "Service not found";
315 case WSATYPE_NOT_FOUND:
316 return "Class type not found";
317 case WSA_E_NO_MORE:
318 return "No more results";
319 case WSA_E_CANCELLED:
320 return "Call was canceled";
321 case WSAEREFUSED:
322 return "Database query was refused";
323 case WSAHOST_NOT_FOUND:
324 return "Host not found";
325 case WSATRY_AGAIN:
326 return "Nonauthoritative host not found";
327 case WSANO_RECOVERY:
328 return "This is a nonrecoverable error";
329 case WSANO_DATA:
330 return "Valid name, no data record of requested type";
331 case WSA_QOS_RECEIVERS:
332 return "QoS receivers";
333 case WSA_QOS_SENDERS:
334 return "QoS senders";
335 case WSA_QOS_NO_SENDERS:
336 return "No QoS senders";
337 case WSA_QOS_NO_RECEIVERS:
338 return "QoS no receivers";
339 case WSA_QOS_REQUEST_CONFIRMED:
340 return "QoS request confirmed";
341 case WSA_QOS_ADMISSION_FAILURE:
342 return "QoS admission error";
343 case WSA_QOS_POLICY_FAILURE:
344 return "QoS policy failure";
345 case WSA_QOS_BAD_STYLE:
346 return "QoS bad style";
347 case WSA_QOS_BAD_OBJECT:
348 return "QoS bad object";
349 case WSA_QOS_TRAFFIC_CTRL_ERROR:
350 return "QoS traffic control error";
351 case WSA_QOS_GENERIC_ERROR:
352 return "QoS generic error";
353 case WSA_QOS_ESERVICETYPE:
354 return "QoS service type error";
355 case WSA_QOS_EFLOWSPEC:
356 return "QoS flowspec error";
357 case WSA_QOS_EPROVSPECBUF:
358 return "Invalid QoS provider buffer";
359 case WSA_QOS_EFILTERSTYLE:
360 return "Invalid QoS filter style";
361 case WSA_QOS_EFILTERTYPE:
362 return "Invalid QoS filter type";
363 case WSA_QOS_EFILTERCOUNT:
364 return "Incorrect QoS filter count";
365 case WSA_QOS_EOBJLENGTH:
366 return "Invalid QoS object length";
367 case WSA_QOS_EFLOWCOUNT:
368 return "Incorrect QoS flow count";
369 case WSA_QOS_EUNKOWNPSOBJ:
370 return "Unrecognized QoS object";
371 case WSA_QOS_EPOLICYOBJ:
372 return "Invalid QoS policy object";
373 case WSA_QOS_EFLOWDESC:
374 return "Invalid QoS flow descriptor";
375 case WSA_QOS_EPSFLOWSPEC:
376 return "Invalid QoS provider-specific flowspec";
377 case WSA_QOS_EPSFILTERSPEC:
378 return "Invalid QoS provider-specific filterspec";
379 case WSA_QOS_ESDMODEOBJ:
380 return "Invalid QoS shape discard mode object";
381 case WSA_QOS_ESHAPERATEOBJ:
382 return "Invalid QoS shaping rate object";
383 case WSA_QOS_RESERVED_PETYPE:
384 return "Reserved policy QoS element type";
385 }
386 return "Unknown winsock error";
387}
388
389/**
390 * Set last winsock error to equivalent of given errno value
391 * @param errnum the errno value to set
392 */
393void MHD_W32_set_last_winsock_error_(int errnum)
394{
395 switch (errnum)
396 {
397 case 0:
398 WSASetLastError(0);
399 break;
400 case EBADF:
401 WSASetLastError(WSA_INVALID_HANDLE);
402 break;
403 case ENOMEM:
404 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
405 break;
406 case EINVAL:
407 WSASetLastError(WSA_INVALID_PARAMETER);
408 break;
409 case EINTR:
410 WSASetLastError(WSAEINTR);
411 break;
412 case EWOULDBLOCK:
413 WSASetLastError(WSAEWOULDBLOCK);
414 break;
415 case EINPROGRESS:
416 WSASetLastError(WSAEINPROGRESS);
417 break;
418 case EALREADY:
419 WSASetLastError(WSAEALREADY);
420 break;
421 case ENOTSOCK:
422 WSASetLastError(WSAENOTSOCK);
423 break;
424 case EDESTADDRREQ:
425 WSASetLastError(WSAEDESTADDRREQ);
426 break;
427 case EMSGSIZE:
428 WSASetLastError(WSAEMSGSIZE);
429 break;
430 case EPROTOTYPE:
431 WSASetLastError(WSAEPROTOTYPE);
432 break;
433 case ENOPROTOOPT:
434 WSASetLastError(WSAENOPROTOOPT);
435 break;
436 case EPROTONOSUPPORT:
437 WSASetLastError(WSAEPROTONOSUPPORT);
438 break;
439 case ESOCKTNOSUPPORT:
440 WSASetLastError(WSAESOCKTNOSUPPORT);
441 break;
442 case EOPNOTSUPP:
443 WSASetLastError(WSAEOPNOTSUPP);
444 break;
445 case EPFNOSUPPORT:
446 WSASetLastError(WSAEPFNOSUPPORT);
447 break;
448 case EAFNOSUPPORT:
449 WSASetLastError(WSAEAFNOSUPPORT);
450 break;
451 case EADDRINUSE:
452 WSASetLastError(WSAEADDRINUSE);
453 break;
454 case EADDRNOTAVAIL:
455 WSASetLastError(WSAEADDRNOTAVAIL);
456 break;
457 case ENETDOWN:
458 WSASetLastError(WSAENETDOWN);
459 break;
460 case ENETUNREACH:
461 WSASetLastError(WSAENETUNREACH);
462 break;
463 case ENETRESET:
464 WSASetLastError(WSAENETRESET);
465 break;
466 case ECONNABORTED:
467 WSASetLastError(WSAECONNABORTED);
468 break;
469 case ECONNRESET:
470 WSASetLastError(WSAECONNRESET);
471 break;
472 case ENOBUFS:
473 WSASetLastError(WSAENOBUFS);
474 break;
475 case EISCONN:
476 WSASetLastError(WSAEISCONN);
477 break;
478 case ENOTCONN:
479 WSASetLastError(WSAENOTCONN);
480 break;
481 case ESHUTDOWN:
482 WSASetLastError(WSAESHUTDOWN);
483 break;
484 case ETOOMANYREFS:
485 WSASetLastError(WSAETOOMANYREFS);
486 break;
487 case ETIMEDOUT:
488 WSASetLastError(WSAETIMEDOUT);
489 break;
490 case ECONNREFUSED:
491 WSASetLastError(WSAECONNREFUSED);
492 break;
493 case ELOOP:
494 WSASetLastError(WSAELOOP);
495 break;
496 case ENAMETOOLONG:
497 WSASetLastError(WSAENAMETOOLONG);
498 break;
499 case EHOSTDOWN:
500 WSASetLastError(WSAEHOSTDOWN);
501 break;
502 case EHOSTUNREACH:
503 WSASetLastError(WSAEHOSTUNREACH);
504 break;
505 case ENOTEMPTY:
506 WSASetLastError(WSAENOTEMPTY);
507 break;
508 case EPROCLIM:
509 WSASetLastError(WSAEPROCLIM);
510 break;
511 case EUSERS:
512 WSASetLastError(WSAEUSERS);
513 break;
514 case EDQUOT:
515 WSASetLastError(WSAEDQUOT);
516 break;
517 case ESTALE:
518 WSASetLastError(WSAESTALE);
519 break;
520 case EREMOTE:
521 WSASetLastError(WSAEREMOTE);
522 break;
523 case EFAULT:
524 WSASetLastError(WSAEFAULT);
525 break;
526 case ENODATA:
527 WSASetLastError(WSANO_DATA);
528 break;
529#if EAGAIN != EWOULDBLOCK
530 case EAGAIN:
531 WSASetLastError(WSAEWOULDBLOCK);
532 break;
533#endif
534 /* Rough equivalent */
535 case EIO:
536 WSASetLastError(WSAEREFUSED);
537 break;
538
539 default: /* Unmapped errors */
540 WSASetLastError(WSAENOBUFS);
541 break;
542 }
543}
544
545#endif /* MHD_WINSOCK_SOCKETS */
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 */
diff --git a/src/microhttpd/sysfdsetsize.c b/src/microhttpd/sysfdsetsize.c
index d5604088..fe7fba75 100644
--- a/src/microhttpd/sysfdsetsize.c
+++ b/src/microhttpd/sysfdsetsize.c
@@ -34,7 +34,7 @@
34#endif /* FD_SETSIZE */ 34#endif /* FD_SETSIZE */
35 35
36#include <stdlib.h> 36#include <stdlib.h>
37#ifdef OS_VXWORKS 37#if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
38#include <sockLib.h> 38#include <sockLib.h>
39#endif /* OS_VXWORKS */ 39#endif /* OS_VXWORKS */
40#if HAVE_SYS_SELECT_H 40#if HAVE_SYS_SELECT_H
diff --git a/src/microhttpd/test_shutdown_select.c b/src/microhttpd/test_shutdown_select.c
index e3aaa20f..a40ac9cc 100644
--- a/src/microhttpd/test_shutdown_select.c
+++ b/src/microhttpd/test_shutdown_select.c
@@ -30,8 +30,8 @@
30 */ 30 */
31 31
32#include "MHD_config.h" 32#include "MHD_config.h"
33#include "platform_interface.h"
34#include "platform.h" 33#include "platform.h"
34#include "mhd_sockets.h"
35#include <stdlib.h> 35#include <stdlib.h>
36#include <stdio.h> 36#include <stdio.h>
37 37
diff --git a/src/platform/w32functions.c b/src/platform/w32functions.c
index 4b1e2c1c..20d8e344 100644
--- a/src/platform/w32functions.c
+++ b/src/platform/w32functions.c
@@ -31,521 +31,7 @@
31#include <time.h> 31#include <time.h>
32#include <stdio.h> 32#include <stdio.h>
33#include <stdarg.h> 33#include <stdarg.h>
34 34#include "../microhttpd/mhd_sockets.h"
35
36/**
37 * Return errno equivalent of last winsock error
38 * @return errno equivalent of last winsock error
39 */
40int MHD_W32_errno_from_winsock_(void)
41{
42 switch(WSAGetLastError())
43 {
44 case 0: return 0;
45 case WSA_INVALID_HANDLE: return EBADF;
46 case WSA_NOT_ENOUGH_MEMORY: return ENOMEM;
47 case WSA_INVALID_PARAMETER: return EINVAL;
48 case WSAEINTR: return EINTR;
49 case WSAEWOULDBLOCK: return EWOULDBLOCK;
50 case WSAEINPROGRESS: return EINPROGRESS;
51 case WSAEALREADY: return EALREADY;
52 case WSAENOTSOCK: return ENOTSOCK;
53 case WSAEDESTADDRREQ: return EDESTADDRREQ;
54 case WSAEMSGSIZE: return EMSGSIZE;
55 case WSAEPROTOTYPE: return EPROTOTYPE;
56 case WSAENOPROTOOPT: return ENOPROTOOPT;
57 case WSAEPROTONOSUPPORT: return EPROTONOSUPPORT;
58 case WSAESOCKTNOSUPPORT: return ESOCKTNOSUPPORT;
59 case WSAEOPNOTSUPP: return EOPNOTSUPP;
60 case WSAEPFNOSUPPORT: return EPFNOSUPPORT;
61 case WSAEAFNOSUPPORT: return EAFNOSUPPORT;
62 case WSAEADDRINUSE: return EADDRINUSE;
63 case WSAEADDRNOTAVAIL: return EADDRNOTAVAIL;
64 case WSAENETDOWN: return ENETDOWN;
65 case WSAENETUNREACH: return ENETUNREACH;
66 case WSAENETRESET: return ENETRESET;
67 case WSAECONNABORTED: return ECONNABORTED;
68 case WSAECONNRESET: return ECONNRESET;
69 case WSAENOBUFS: return ENOBUFS;
70 case WSAEISCONN: return EISCONN;
71 case WSAENOTCONN: return ENOTCONN;
72 case WSAESHUTDOWN: return ESHUTDOWN;
73 case WSAETOOMANYREFS: return ETOOMANYREFS;
74 case WSAETIMEDOUT: return ETIMEDOUT;
75 case WSAECONNREFUSED: return ECONNREFUSED;
76 case WSAELOOP: return ELOOP;
77 case WSAENAMETOOLONG: return ENAMETOOLONG;
78 case WSAEHOSTDOWN: return EHOSTDOWN;
79 case WSAEHOSTUNREACH: return EHOSTUNREACH;
80 case WSAENOTEMPTY: return ENOTEMPTY;
81 case WSAEPROCLIM: return EPROCLIM;
82 case WSAEUSERS: return EUSERS;
83 case WSAEDQUOT: return EDQUOT;
84 case WSAESTALE: return ESTALE;
85 case WSAEREMOTE: return EREMOTE;
86 case WSAEINVAL: return EINVAL;
87 case WSAEFAULT: return EFAULT;
88 case WSANO_DATA: return ENODATA;
89 /* Rough equivalents */
90 case WSAEDISCON: return ECONNRESET;
91 case WSAEINVALIDPROCTABLE: return EFAULT;
92 case WSASYSNOTREADY:
93 case WSANOTINITIALISED:
94 case WSASYSCALLFAILURE: return ENOBUFS;
95 case WSAVERNOTSUPPORTED: return EOPNOTSUPP;
96 case WSAEREFUSED: return EIO;
97 }
98 return EINVAL;
99}
100
101/**
102 * Return pointer to string description of errnum error
103 * Works fine with both standard errno errnums
104 * and errnums from MHD_W32_errno_from_winsock_
105 * @param errnum the errno or value from MHD_W32_errno_from_winsock_()
106 * @return pointer to string description of error
107 */
108const char* MHD_W32_strerror_(int errnum)
109{
110 switch(errnum)
111 {
112 case 0:
113 return "No error";
114 case EWOULDBLOCK:
115 return "Operation would block";
116 case EINPROGRESS:
117 return "Connection already in progress";
118 case EALREADY:
119 return "Socket already connected";
120 case ENOTSOCK:
121 return "Socket operation on non-socket";
122 case EDESTADDRREQ:
123 return "Destination address required";
124 case EMSGSIZE:
125 return "Message too long";
126 case EPROTOTYPE:
127 return "Protocol wrong type for socket";
128 case ENOPROTOOPT:
129 return "Protocol not available";
130 case EPROTONOSUPPORT:
131 return "Unknown protocol";
132 case ESOCKTNOSUPPORT:
133 return "Socket type not supported";
134 case EOPNOTSUPP:
135 return "Operation not supported on socket";
136 case EPFNOSUPPORT:
137 return "Protocol family not supported";
138 case EAFNOSUPPORT:
139 return "Address family not supported by protocol family";
140 case EADDRINUSE:
141 return "Address already in use";
142 case EADDRNOTAVAIL:
143 return "Cannot assign requested address";
144 case ENETDOWN:
145 return "Network is down";
146 case ENETUNREACH:
147 return "Network is unreachable";
148 case ENETRESET:
149 return "Network dropped connection on reset";
150 case ECONNABORTED:
151 return "Software caused connection abort";
152 case ECONNRESET:
153 return "Connection reset by peer";
154 case ENOBUFS:
155 return "No system resources available";
156 case EISCONN:
157 return "Socket is already connected";
158 case ENOTCONN:
159 return "Socket is not connected";
160 case ESHUTDOWN:
161 return "Can't send after socket shutdown";
162 case ETOOMANYREFS:
163 return "Too many references: cannot splice";
164 case ETIMEDOUT:
165 return "Connection timed out";
166 case ECONNREFUSED:
167 return "Connection refused";
168 case ELOOP:
169 return "Cannot translate name";
170 case EHOSTDOWN:
171 return "Host is down";
172 case EHOSTUNREACH:
173 return "Host is unreachable";
174 case EPROCLIM:
175 return "Too many processes";
176 case EUSERS:
177 return "Too many users";
178 case EDQUOT:
179 return "Disk quota exceeded";
180 case ESTALE:
181 return "Stale file handle reference";
182 case EREMOTE:
183 return "Resource is remote";
184 case ENODATA:
185 return "No data available";
186 }
187 return strerror(errnum);
188}
189
190/**
191 * Return pointer to string description of last winsock error
192 * @return pointer to string description of last winsock error
193 */
194const char* MHD_W32_strerror_last_winsock_(void)
195{
196 switch (WSAGetLastError())
197 {
198 case 0:
199 return "No error";
200 case WSA_INVALID_HANDLE:
201 return "Specified event object handle is invalid";
202 case WSA_NOT_ENOUGH_MEMORY:
203 return "Insufficient memory available";
204 case WSA_INVALID_PARAMETER:
205 return "One or more parameters are invalid";
206 case WSA_OPERATION_ABORTED:
207 return "Overlapped operation aborted";
208 case WSA_IO_INCOMPLETE:
209 return "Overlapped I/O event object not in signaled state";
210 case WSA_IO_PENDING:
211 return "Overlapped operations will complete later";
212 case WSAEINTR:
213 return "Interrupted function call";
214 case WSAEBADF:
215 return "File handle is not valid";
216 case WSAEACCES:
217 return "Permission denied";
218 case WSAEFAULT:
219 return "Bad address";
220 case WSAEINVAL:
221 return "Invalid argument";
222 case WSAEMFILE:
223 return "Too many open files";
224 case WSAEWOULDBLOCK:
225 return "Resource temporarily unavailable";
226 case WSAEINPROGRESS:
227 return "Operation now in progress";
228 case WSAEALREADY:
229 return "Operation already in progress";
230 case WSAENOTSOCK:
231 return "Socket operation on nonsocket";
232 case WSAEDESTADDRREQ:
233 return "Destination address required";
234 case WSAEMSGSIZE:
235 return "Message too long";
236 case WSAEPROTOTYPE:
237 return "Protocol wrong type for socket";
238 case WSAENOPROTOOPT:
239 return "Bad protocol option";
240 case WSAEPROTONOSUPPORT:
241 return "Protocol not supported";
242 case WSAESOCKTNOSUPPORT:
243 return "Socket type not supported";
244 case WSAEOPNOTSUPP:
245 return "Operation not supported";
246 case WSAEPFNOSUPPORT:
247 return "Protocol family not supported";
248 case WSAEAFNOSUPPORT:
249 return "Address family not supported by protocol family";
250 case WSAEADDRINUSE:
251 return "Address already in use";
252 case WSAEADDRNOTAVAIL:
253 return "Cannot assign requested address";
254 case WSAENETDOWN:
255 return "Network is down";
256 case WSAENETUNREACH:
257 return "Network is unreachable";
258 case WSAENETRESET:
259 return "Network dropped connection on reset";
260 case WSAECONNABORTED:
261 return "Software caused connection abort";
262 case WSAECONNRESET:
263 return "Connection reset by peer";
264 case WSAENOBUFS:
265 return "No buffer space available";
266 case WSAEISCONN:
267 return "Socket is already connected";
268 case WSAENOTCONN:
269 return "Socket is not connected";
270 case WSAESHUTDOWN:
271 return "Cannot send after socket shutdown";
272 case WSAETOOMANYREFS:
273 return "Too many references";
274 case WSAETIMEDOUT:
275 return "Connection timed out";
276 case WSAECONNREFUSED:
277 return "Connection refused";
278 case WSAELOOP:
279 return "Cannot translate name";
280 case WSAENAMETOOLONG:
281 return "Name too long";
282 case WSAEHOSTDOWN:
283 return "Host is down";
284 case WSAEHOSTUNREACH:
285 return "No route to host";
286 case WSAENOTEMPTY:
287 return "Directory not empty";
288 case WSAEPROCLIM:
289 return "Too many processes";
290 case WSAEUSERS:
291 return "User quota exceeded";
292 case WSAEDQUOT:
293 return "Disk quota exceeded";
294 case WSAESTALE:
295 return "Stale file handle reference";
296 case WSAEREMOTE:
297 return "Item is remote";
298 case WSASYSNOTREADY:
299 return "Network subsystem is unavailable";
300 case WSAVERNOTSUPPORTED:
301 return "Winsock.dll version out of range";
302 case WSANOTINITIALISED:
303 return "Successful WSAStartup not yet performed";
304 case WSAEDISCON:
305 return "Graceful shutdown in progress";
306 case WSAENOMORE:
307 return "No more results";
308 case WSAECANCELLED:
309 return "Call has been canceled";
310 case WSAEINVALIDPROCTABLE:
311 return "Procedure call table is invalid";
312 case WSAEINVALIDPROVIDER:
313 return "Service provider is invalid";
314 case WSAEPROVIDERFAILEDINIT:
315 return "Service provider failed to initialize";
316 case WSASYSCALLFAILURE:
317 return "System call failure";
318 case WSASERVICE_NOT_FOUND:
319 return "Service not found";
320 case WSATYPE_NOT_FOUND:
321 return "Class type not found";
322 case WSA_E_NO_MORE:
323 return "No more results";
324 case WSA_E_CANCELLED:
325 return "Call was canceled";
326 case WSAEREFUSED:
327 return "Database query was refused";
328 case WSAHOST_NOT_FOUND:
329 return "Host not found";
330 case WSATRY_AGAIN:
331 return "Nonauthoritative host not found";
332 case WSANO_RECOVERY:
333 return "This is a nonrecoverable error";
334 case WSANO_DATA:
335 return "Valid name, no data record of requested type";
336 case WSA_QOS_RECEIVERS:
337 return "QoS receivers";
338 case WSA_QOS_SENDERS:
339 return "QoS senders";
340 case WSA_QOS_NO_SENDERS:
341 return "No QoS senders";
342 case WSA_QOS_NO_RECEIVERS:
343 return "QoS no receivers";
344 case WSA_QOS_REQUEST_CONFIRMED:
345 return "QoS request confirmed";
346 case WSA_QOS_ADMISSION_FAILURE:
347 return "QoS admission error";
348 case WSA_QOS_POLICY_FAILURE:
349 return "QoS policy failure";
350 case WSA_QOS_BAD_STYLE:
351 return "QoS bad style";
352 case WSA_QOS_BAD_OBJECT:
353 return "QoS bad object";
354 case WSA_QOS_TRAFFIC_CTRL_ERROR:
355 return "QoS traffic control error";
356 case WSA_QOS_GENERIC_ERROR:
357 return "QoS generic error";
358 case WSA_QOS_ESERVICETYPE:
359 return "QoS service type error";
360 case WSA_QOS_EFLOWSPEC:
361 return "QoS flowspec error";
362 case WSA_QOS_EPROVSPECBUF:
363 return "Invalid QoS provider buffer";
364 case WSA_QOS_EFILTERSTYLE:
365 return "Invalid QoS filter style";
366 case WSA_QOS_EFILTERTYPE:
367 return "Invalid QoS filter type";
368 case WSA_QOS_EFILTERCOUNT:
369 return "Incorrect QoS filter count";
370 case WSA_QOS_EOBJLENGTH:
371 return "Invalid QoS object length";
372 case WSA_QOS_EFLOWCOUNT:
373 return "Incorrect QoS flow count";
374 case WSA_QOS_EUNKOWNPSOBJ:
375 return "Unrecognized QoS object";
376 case WSA_QOS_EPOLICYOBJ:
377 return "Invalid QoS policy object";
378 case WSA_QOS_EFLOWDESC:
379 return "Invalid QoS flow descriptor";
380 case WSA_QOS_EPSFLOWSPEC:
381 return "Invalid QoS provider-specific flowspec";
382 case WSA_QOS_EPSFILTERSPEC:
383 return "Invalid QoS provider-specific filterspec";
384 case WSA_QOS_ESDMODEOBJ:
385 return "Invalid QoS shape discard mode object";
386 case WSA_QOS_ESHAPERATEOBJ:
387 return "Invalid QoS shaping rate object";
388 case WSA_QOS_RESERVED_PETYPE:
389 return "Reserved policy QoS element type";
390 }
391 return "Unknown winsock error";
392}
393
394/**
395 * Set last winsock error to equivalent of given errno value
396 * @param errnum the errno value to set
397 */
398void MHD_W32_set_last_winsock_error_(int errnum)
399{
400 switch (errnum)
401 {
402 case 0:
403 WSASetLastError(0);
404 break;
405 case EBADF:
406 WSASetLastError(WSA_INVALID_HANDLE);
407 break;
408 case ENOMEM:
409 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
410 break;
411 case EINVAL:
412 WSASetLastError(WSA_INVALID_PARAMETER);
413 break;
414 case EINTR:
415 WSASetLastError(WSAEINTR);
416 break;
417 case EWOULDBLOCK:
418 WSASetLastError(WSAEWOULDBLOCK);
419 break;
420 case EINPROGRESS:
421 WSASetLastError(WSAEINPROGRESS);
422 break;
423 case EALREADY:
424 WSASetLastError(WSAEALREADY);
425 break;
426 case ENOTSOCK:
427 WSASetLastError(WSAENOTSOCK);
428 break;
429 case EDESTADDRREQ:
430 WSASetLastError(WSAEDESTADDRREQ);
431 break;
432 case EMSGSIZE:
433 WSASetLastError(WSAEMSGSIZE);
434 break;
435 case EPROTOTYPE:
436 WSASetLastError(WSAEPROTOTYPE);
437 break;
438 case ENOPROTOOPT:
439 WSASetLastError(WSAENOPROTOOPT);
440 break;
441 case EPROTONOSUPPORT:
442 WSASetLastError(WSAEPROTONOSUPPORT);
443 break;
444 case ESOCKTNOSUPPORT:
445 WSASetLastError(WSAESOCKTNOSUPPORT);
446 break;
447 case EOPNOTSUPP:
448 WSASetLastError(WSAEOPNOTSUPP);
449 break;
450 case EPFNOSUPPORT:
451 WSASetLastError(WSAEPFNOSUPPORT);
452 break;
453 case EAFNOSUPPORT:
454 WSASetLastError(WSAEAFNOSUPPORT);
455 break;
456 case EADDRINUSE:
457 WSASetLastError(WSAEADDRINUSE);
458 break;
459 case EADDRNOTAVAIL:
460 WSASetLastError(WSAEADDRNOTAVAIL);
461 break;
462 case ENETDOWN:
463 WSASetLastError(WSAENETDOWN);
464 break;
465 case ENETUNREACH:
466 WSASetLastError(WSAENETUNREACH);
467 break;
468 case ENETRESET:
469 WSASetLastError(WSAENETRESET);
470 break;
471 case ECONNABORTED:
472 WSASetLastError(WSAECONNABORTED);
473 break;
474 case ECONNRESET:
475 WSASetLastError(WSAECONNRESET);
476 break;
477 case ENOBUFS:
478 WSASetLastError(WSAENOBUFS);
479 break;
480 case EISCONN:
481 WSASetLastError(WSAEISCONN);
482 break;
483 case ENOTCONN:
484 WSASetLastError(WSAENOTCONN);
485 break;
486 case ESHUTDOWN:
487 WSASetLastError(WSAESHUTDOWN);
488 break;
489 case ETOOMANYREFS:
490 WSASetLastError(WSAETOOMANYREFS);
491 break;
492 case ETIMEDOUT:
493 WSASetLastError(WSAETIMEDOUT);
494 break;
495 case ECONNREFUSED:
496 WSASetLastError(WSAECONNREFUSED);
497 break;
498 case ELOOP:
499 WSASetLastError(WSAELOOP);
500 break;
501 case ENAMETOOLONG:
502 WSASetLastError(WSAENAMETOOLONG);
503 break;
504 case EHOSTDOWN:
505 WSASetLastError(WSAEHOSTDOWN);
506 break;
507 case EHOSTUNREACH:
508 WSASetLastError(WSAEHOSTUNREACH);
509 break;
510 case ENOTEMPTY:
511 WSASetLastError(WSAENOTEMPTY);
512 break;
513 case EPROCLIM:
514 WSASetLastError(WSAEPROCLIM);
515 break;
516 case EUSERS:
517 WSASetLastError(WSAEUSERS);
518 break;
519 case EDQUOT:
520 WSASetLastError(WSAEDQUOT);
521 break;
522 case ESTALE:
523 WSASetLastError(WSAESTALE);
524 break;
525 case EREMOTE:
526 WSASetLastError(WSAEREMOTE);
527 break;
528 case EFAULT:
529 WSASetLastError(WSAEFAULT);
530 break;
531 case ENODATA:
532 WSASetLastError(WSANO_DATA);
533 break;
534#if EAGAIN != EWOULDBLOCK
535 case EAGAIN:
536 WSASetLastError(WSAEWOULDBLOCK);
537 break;
538#endif
539 /* Rough equivalent */
540 case EIO:
541 WSASetLastError(WSAEREFUSED);
542 break;
543
544 default: /* Unmapped errors */
545 WSASetLastError(WSAENOBUFS);
546 break;
547 }
548}
549 35
550/** 36/**
551 * Create pair of mutually connected TCP/IP sockets on loopback address 37 * Create pair of mutually connected TCP/IP sockets on loopback address
diff --git a/src/testcurl/https/test_https_time_out.c b/src/testcurl/https/test_https_time_out.c
index 128c140f..914100eb 100644
--- a/src/testcurl/https/test_https_time_out.c
+++ b/src/testcurl/https/test_https_time_out.c
@@ -30,6 +30,7 @@
30#include "internal.h" 30#include "internal.h"
31#include "tls_test_common.h" 31#include "tls_test_common.h"
32#include <gcrypt.h> 32#include <gcrypt.h>
33#include "mhd_sockets.h" /* only macros used */
33 34
34#ifdef _WIN32 35#ifdef _WIN32
35#ifndef WIN32_LEAN_AND_MEAN 36#ifndef WIN32_LEAN_AND_MEAN
diff --git a/src/testcurl/https/test_tls_extensions.c b/src/testcurl/https/test_tls_extensions.c
index b13d8aeb..cc87c2b2 100644
--- a/src/testcurl/https/test_tls_extensions.c
+++ b/src/testcurl/https/test_tls_extensions.c
@@ -28,6 +28,8 @@
28#include "platform.h" 28#include "platform.h"
29#include "microhttpd.h" 29#include "microhttpd.h"
30#include "tls_test_common.h" 30#include "tls_test_common.h"
31#include "mhd_sockets.h" /* only macros used */
32
31#define MAX_EXT_DATA_LENGTH 256 33#define MAX_EXT_DATA_LENGTH 256
32 34
33extern const char srv_key_pem[]; 35extern const char srv_key_pem[];
diff --git a/src/testcurl/test_get.c b/src/testcurl/test_get.c
index 0b8c577b..b9d12816 100644
--- a/src/testcurl/test_get.c
+++ b/src/testcurl/test_get.c
@@ -33,6 +33,7 @@
33#include <stdlib.h> 33#include <stdlib.h>
34#include <string.h> 34#include <string.h>
35#include <time.h> 35#include <time.h>
36#include "mhd_sockets.h" /* only macros used */
36 37
37#ifdef _WIN32 38#ifdef _WIN32
38#ifndef WIN32_LEAN_AND_MEAN 39#ifndef WIN32_LEAN_AND_MEAN
diff --git a/src/testcurl/test_get_sendfile.c b/src/testcurl/test_get_sendfile.c
index 8db2c3aa..19e327fd 100644
--- a/src/testcurl/test_get_sendfile.c
+++ b/src/testcurl/test_get_sendfile.c
@@ -33,6 +33,7 @@
33#include <time.h> 33#include <time.h>
34#include <sys/types.h> 34#include <sys/types.h>
35#include <fcntl.h> 35#include <fcntl.h>
36#include "mhd_sockets.h"
36 37
37#ifndef WINDOWS 38#ifndef WINDOWS
38#include <sys/socket.h> 39#include <sys/socket.h>
@@ -101,7 +102,7 @@ ahc_echo (void *cls,
101 { 102 {
102 fprintf (stderr, "Failed to open `%s': %s\n", 103 fprintf (stderr, "Failed to open `%s': %s\n",
103 sourcefile, 104 sourcefile,
104 MHD_strerror_ (errno)); 105 strerror (errno));
105 exit (1); 106 exit (1);
106 } 107 }
107 response = MHD_create_response_from_fd (strlen (TESTSTR), fd); 108 response = MHD_create_response_from_fd (strlen (TESTSTR), fd);
diff --git a/src/testcurl/test_options.c b/src/testcurl/test_options.c
index d229f9b4..bb52988e 100644
--- a/src/testcurl/test_options.c
+++ b/src/testcurl/test_options.c
@@ -26,6 +26,7 @@
26 26
27#include "platform.h" 27#include "platform.h"
28#include "microhttpd.h" 28#include "microhttpd.h"
29#include "mhd_sockets.h"
29 30
30#define MHD_E_MEM "Error: memory error\n" 31#define MHD_E_MEM "Error: memory error\n"
31#define MHD_E_SERVER_INIT "Error: failed to start server\n" 32#define MHD_E_SERVER_INIT "Error: failed to start server\n"
diff --git a/src/testcurl/test_quiesce.c b/src/testcurl/test_quiesce.c
index bf5e6d44..b0d99494 100644
--- a/src/testcurl/test_quiesce.c
+++ b/src/testcurl/test_quiesce.c
@@ -33,6 +33,7 @@
33#include <time.h> 33#include <time.h>
34#include <sys/types.h> 34#include <sys/types.h>
35#include <pthread.h> 35#include <pthread.h>
36#include "mhd_sockets.h" /* only macros used */
36 37
37#ifndef WINDOWS 38#ifndef WINDOWS
38#include <unistd.h> 39#include <unistd.h>
diff --git a/w32/common/libmicrohttpd-files.vcxproj b/w32/common/libmicrohttpd-files.vcxproj
index 0fdfa2bd..64c497b6 100644
--- a/w32/common/libmicrohttpd-files.vcxproj
+++ b/w32/common/libmicrohttpd-files.vcxproj
@@ -17,6 +17,7 @@
17 <ClCompile Include="$(MhdSrc)microhttpd\sysfdsetsize.c" /> 17 <ClCompile Include="$(MhdSrc)microhttpd\sysfdsetsize.c" />
18 <ClCompile Include="$(MhdSrc)microhttpd\mhd_str.c" /> 18 <ClCompile Include="$(MhdSrc)microhttpd\mhd_str.c" />
19 <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c" /> 19 <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c" />
20 <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c" />
20 <ClCompile Include="$(MhdSrc)platform\w32functions.c" /> 21 <ClCompile Include="$(MhdSrc)platform\w32functions.c" />
21 </ItemGroup> 22 </ItemGroup>
22 <ItemGroup> 23 <ItemGroup>
@@ -40,6 +41,7 @@
40 <ClInclude Include="$(MhdSrc)microhttpd\mhd_str.h" /> 41 <ClInclude Include="$(MhdSrc)microhttpd\mhd_str.h" />
41 <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> 42 <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" />
42 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" /> 43 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" />
44 <ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h" />
43 <ClInclude Include="$(MhdW32Common)MHD_config.h" /> 45 <ClInclude Include="$(MhdW32Common)MHD_config.h" />
44 </ItemGroup> 46 </ItemGroup>
45 <ItemGroup> 47 <ItemGroup>
diff --git a/w32/common/libmicrohttpd-filters.vcxproj b/w32/common/libmicrohttpd-filters.vcxproj
index 6bb3dca7..1ed587eb 100644
--- a/w32/common/libmicrohttpd-filters.vcxproj
+++ b/w32/common/libmicrohttpd-filters.vcxproj
@@ -139,6 +139,12 @@
139 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h"> 139 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h">
140 <Filter>Source Files</Filter> 140 <Filter>Source Files</Filter>
141 </ClInclude> 141 </ClInclude>
142 <ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h">
143 <Filter>Source Files</Filter>
144 </ClInclude>
145 <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c">
146 <Filter>Source Files</Filter>
147 </ClCompile>
142 </ItemGroup> 148 </ItemGroup>
143 <ItemGroup> 149 <ItemGroup>
144 <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> 150 <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc">