GNU libmicrohttpd 0.9.77
Loading...
Searching...
No Matches
mhd_sockets.c
Go to the documentation of this file.
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*/
25#include "mhd_sockets.h"
26
27#ifdef MHD_WINSOCK_SOCKETS
28
34const char *
36{
37 switch (err)
38 {
39 case 0:
40 return "No error";
42 return "Specified event object handle is invalid";
44 return "Insufficient memory available";
46 return "One or more parameters are invalid";
48 return "Overlapped operation aborted";
50 return "Overlapped I/O event object not in signaled state";
51 case WSA_IO_PENDING:
52 return "Overlapped operations will complete later";
53 case WSAEINTR:
54 return "Interrupted function call";
55 case WSAEBADF:
56 return "File handle is not valid";
57 case WSAEACCES:
58 return "Permission denied";
59 case WSAEFAULT:
60 return "Bad address";
61 case WSAEINVAL:
62 return "Invalid argument";
63 case WSAEMFILE:
64 return "Too many open files";
65 case WSAEWOULDBLOCK:
66 return "Resource temporarily unavailable";
67 case WSAEINPROGRESS:
68 return "Operation now in progress";
69 case WSAEALREADY:
70 return "Operation already in progress";
71 case WSAENOTSOCK:
72 return "Socket operation on nonsocket";
73 case WSAEDESTADDRREQ:
74 return "Destination address required";
75 case WSAEMSGSIZE:
76 return "Message too long";
77 case WSAEPROTOTYPE:
78 return "Protocol wrong type for socket";
79 case WSAENOPROTOOPT:
80 return "Bad protocol option";
82 return "Protocol not supported";
84 return "Socket type not supported";
85 case WSAEOPNOTSUPP:
86 return "Operation not supported";
87 case WSAEPFNOSUPPORT:
88 return "Protocol family not supported";
89 case WSAEAFNOSUPPORT:
90 return "Address family not supported by protocol family";
91 case WSAEADDRINUSE:
92 return "Address already in use";
94 return "Cannot assign requested address";
95 case WSAENETDOWN:
96 return "Network is down";
97 case WSAENETUNREACH:
98 return "Network is unreachable";
99 case WSAENETRESET:
100 return "Network dropped connection on reset";
101 case WSAECONNABORTED:
102 return "Software caused connection abort";
103 case WSAECONNRESET:
104 return "Connection reset by peer";
105 case WSAENOBUFS:
106 return "No buffer space available";
107 case WSAEISCONN:
108 return "Socket is already connected";
109 case WSAENOTCONN:
110 return "Socket is not connected";
111 case WSAESHUTDOWN:
112 return "Cannot send after socket shutdown";
113 case WSAETOOMANYREFS:
114 return "Too many references";
115 case WSAETIMEDOUT:
116 return "Connection timed out";
117 case WSAECONNREFUSED:
118 return "Connection refused";
119 case WSAELOOP:
120 return "Cannot translate name";
121 case WSAENAMETOOLONG:
122 return "Name too long";
123 case WSAEHOSTDOWN:
124 return "Host is down";
125 case WSAEHOSTUNREACH:
126 return "No route to host";
127 case WSAENOTEMPTY:
128 return "Directory not empty";
129 case WSAEPROCLIM:
130 return "Too many processes";
131 case WSAEUSERS:
132 return "User quota exceeded";
133 case WSAEDQUOT:
134 return "Disk quota exceeded";
135 case WSAESTALE:
136 return "Stale file handle reference";
137 case WSAEREMOTE:
138 return "Item is remote";
139 case WSASYSNOTREADY:
140 return "Network subsystem is unavailable";
142 return "Winsock.dll version out of range";
144 return "Successful WSAStartup not yet performed";
145 case WSAEDISCON:
146 return "Graceful shutdown in progress";
147 case WSAENOMORE:
148 return "No more results";
149 case WSAECANCELLED:
150 return "Call has been canceled";
152 return "Procedure call table is invalid";
154 return "Service provider is invalid";
156 return "Service provider failed to initialize";
158 return "System call failure";
160 return "Service not found";
162 return "Class type not found";
163 case WSA_E_NO_MORE:
164 return "No more results";
165 case WSA_E_CANCELLED:
166 return "Call was canceled";
167 case WSAEREFUSED:
168 return "Database query was refused";
170 return "Host not found";
171 case WSATRY_AGAIN:
172 return "Nonauthoritative host not found";
173 case WSANO_RECOVERY:
174 return "This is a nonrecoverable error";
175 case WSANO_DATA:
176 return "Valid name, no data record of requested type";
178 return "QoS receivers";
179 case WSA_QOS_SENDERS:
180 return "QoS senders";
182 return "No QoS senders";
184 return "QoS no receivers";
186 return "QoS request confirmed";
188 return "QoS admission error";
190 return "QoS policy failure";
192 return "QoS bad style";
194 return "QoS bad object";
196 return "QoS traffic control error";
198 return "QoS generic error";
200 return "QoS service type error";
202 return "QoS flowspec error";
204 return "Invalid QoS provider buffer";
206 return "Invalid QoS filter style";
208 return "Invalid QoS filter type";
210 return "Incorrect QoS filter count";
212 return "Invalid QoS object length";
214 return "Incorrect QoS flow count";
216 return "Unrecognized QoS object";
218 return "Invalid QoS policy object";
220 return "Invalid QoS flow descriptor";
222 return "Invalid QoS provider-specific flowspec";
224 return "Invalid QoS provider-specific filterspec";
226 return "Invalid QoS shape discard mode object";
228 return "Invalid QoS shaping rate object";
230 return "Reserved policy QoS element type";
231 }
232 return "Unknown winsock error";
233}
234
235
243int
245{
246 int i;
247
248 if (! sockets_pair)
249 {
251 return 0;
252 }
253
254#define PAIRMAXTRYIES 800
255 for (i = 0; i < PAIRMAXTRYIES; i++)
256 {
259 static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */
260 int addr_len = c_addinlen;
261 unsigned long on_val = 1;
262 unsigned long off_val = 0;
263
268 break; /* can't create even single socket */
269
270 listen_addr.sin_family = AF_INET;
271 listen_addr.sin_port = 0; /* same as htons(0) */
272 listen_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
273 if ( ((0 == bind (listen_s,
274 (struct sockaddr *) &listen_addr,
275 c_addinlen)) &&
276 (0 == listen (listen_s,
277 1) ) &&
278 (0 == getsockname (listen_s,
279 (struct sockaddr *) &listen_addr,
280 &addr_len))) )
281 {
286 struct sockaddr_in client_addr;
288
290 {
291 /* try again */
293 continue;
294 }
295
296 if ( (0 != ioctlsocket (client_s,
297 (int) FIONBIO,
298 &on_val)) ||
299 ( (0 != connect (client_s,
300 (struct sockaddr *) &listen_addr,
301 c_addinlen)) &&
303 {
304 /* try again */
307 continue;
308 }
309
310 addr_len = c_addinlen;
312 (struct sockaddr *) &accepted_from_addr,
313 &addr_len);
315 {
316 /* try again */
319 continue;
320 }
321
322 addr_len = c_addinlen;
323 if ( (0 == getsockname (client_s,
324 (struct sockaddr *) &client_addr,
325 &addr_len)) &&
326 (accepted_from_addr.sin_port == client_addr.sin_port) &&
327 (accepted_from_addr.sin_addr.s_addr ==
328 client_addr.sin_addr.s_addr) &&
329 (accepted_from_addr.sin_family == client_addr.sin_family) &&
330 ( (0 != non_blk) ?
331 (0 == ioctlsocket (server_s,
332 (int) FIONBIO,
333 &on_val)) :
334 (0 == ioctlsocket (client_s,
335 (int) FIONBIO,
336 &off_val)) ) &&
337 (0 == setsockopt (server_s,
340 (const void *) (&on_val),
341 sizeof (on_val))) &&
342 (0 == setsockopt (client_s,
345 (const void *) (&on_val),
346 sizeof (on_val))) )
347 {
351 return ! 0;
352 }
355 }
357 }
358
362
363 return 0;
364}
365
366
367#endif /* MHD_WINSOCK_SOCKETS */
368
369
380int
382 fd_set *set,
384 unsigned int fd_setsize)
385{
386 if ( (NULL == set) ||
388 return 0;
390 set,
391 fd_setsize))
392 return 0;
394 set,
395 fd_setsize);
396 if ( (NULL != max_fd) &&
397 ( (fd > *max_fd) ||
398 (MHD_INVALID_SOCKET == *max_fd) ) )
399 *max_fd = fd;
400 return ! 0;
401}
402
403
410int
412{
413#if defined(MHD_POSIX_SOCKETS)
414 int flags;
415
416 flags = fcntl (sock,
417 F_GETFL);
418 if (-1 == flags)
419 return 0;
420
421 if ( ((flags | O_NONBLOCK) != flags) &&
422 (0 != fcntl (sock,
423 F_SETFL,
424 flags | O_NONBLOCK)) )
425 return 0;
426#elif defined(MHD_WINSOCK_SOCKETS)
427 unsigned long flags = 1;
428
429 if (0 != ioctlsocket (sock,
430 (int) FIONBIO,
431 &flags))
432 return 0;
433#endif /* MHD_WINSOCK_SOCKETS */
434 return ! 0;
435}
436
437
445int
447{
448#if defined(MHD_POSIX_SOCKETS)
449 int flags;
450
451 flags = fcntl (sock,
452 F_GETFD);
453 if (-1 == flags)
454 return 0;
455
456 if ( ((flags | FD_CLOEXEC) != flags) &&
457 (0 != fcntl (sock,
458 F_SETFD,
459 flags | FD_CLOEXEC)) )
460 return 0;
461#elif defined(MHD_WINSOCK_SOCKETS)
464 0))
465 return 0;
466#endif /* MHD_WINSOCK_SOCKETS */
467 return ! 0;
468}
469
470
480int
482 bool on)
483{
484#ifdef TCP_NODELAY
485 {
486 const MHD_SCKT_OPT_BOOL_ off_val = 0;
487 const MHD_SCKT_OPT_BOOL_ on_val = 1;
488 /* Disable Nagle's algorithm for normal buffering */
489 return setsockopt (sock,
492 (const void *) ((on) ? &on_val : &off_val),
493 sizeof (on_val));
494 }
495#else
496 (void) sock;
497 return 0;
498#endif /* TCP_NODELAY */
499}
500
501
510{
512 int cloexec_set;
513#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_)
514 int nosigpipe_set;
515#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */
516
517#if defined(MHD_POSIX_SOCKETS) && (defined(SOCK_CLOEXEC) || \
518 defined(SOCK_NOSIGPIPE) )
519 fd = socket (pf,
521 0);
522 if (MHD_INVALID_SOCKET != fd)
523 {
525#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_)
527#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */
528 }
529#elif defined(MHD_WINSOCK_SOCKETS) && defined(WSA_FLAG_NO_HANDLE_INHERIT)
530 fd = WSASocketW (pf,
532 0,
533 NULL,
534 0,
536 cloexec_set = ! 0;
537#else /* !SOCK_CLOEXEC */
539#endif /* !SOCK_CLOEXEC */
540 if (MHD_INVALID_SOCKET == fd)
541 {
542 fd = socket (pf,
544 0);
545 cloexec_set = 0;
546#if defined(SOCK_NOSIGPIPE) || defined(MHD_socket_nosignal_)
547 nosigpipe_set = 0;
548#endif /* SOCK_NOSIGPIPE || MHD_socket_nosignal_ */
549 }
550 if (MHD_INVALID_SOCKET == fd)
551 return MHD_INVALID_SOCKET;
552
553#if defined(MHD_socket_nosignal_)
554 if ( (! nosigpipe_set) &&
555 (0 == MHD_socket_nosignal_ (fd)) &&
556 (0 == MSG_NOSIGNAL_OR_ZERO) )
557 {
558 /* SIGPIPE disable is possible on this platform
559 * (so application expect that it will be disabled),
560 * but failed to be disabled here and it is not
561 * possible to disable SIGPIPE by MSG_NOSIGNAL. */
562 const int err = MHD_socket_get_error_ ();
565 return MHD_INVALID_SOCKET;
566 }
567#endif /* defined(MHD_socket_nosignal_) */
568 if (! cloexec_set)
570
571 return fd;
572}
#define MHD_HTTP_OK
Definition microhttpd.h:356
int MHD_add_to_fd_set_(MHD_socket fd, fd_set *set, MHD_socket *max_fd, unsigned int fd_setsize)
int MHD_socket_noninheritable_(MHD_socket sock)
int MHD_socket_nonblocking_(MHD_socket sock)
MHD_socket MHD_socket_create_listen_(int pf)
#define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd, pset, setsize)
#define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd, pset, setsize)
int MHD_SCKT_OPT_BOOL_
#define MHD_socket_close_(fd)
#define MHD_socket_get_error_()
#define MHD_socket_fset_error_(err)
#define NULL
int MHD_socket_set_nodelay_(MHD_socket sock, bool on)
#define SOCK_NOSIGPIPE_OR_ZERO
#define MSG_NOSIGNAL_OR_ZERO
#define SOCK_CLOEXEC_OR_ZERO
int MHD_socket
Definition microhttpd.h:207
int fd
#define MHD_INVALID_SOCKET
Definition microhttpd.h:208