aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd')
-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
8 files changed, 957 insertions, 49 deletions
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