GNU libmicrohttpd  0.9.68
mhd_sockets.h
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 */
20 
33 #ifndef MHD_SOCKETS_H
34 #define MHD_SOCKETS_H 1
35 #include "mhd_options.h"
36 
37 #include <errno.h>
38 #include <stdbool.h>
39 
40 #if ! defined(MHD_POSIX_SOCKETS) && ! defined(MHD_WINSOCK_SOCKETS)
41 # if ! defined(_WIN32) || defined(__CYGWIN__)
42 # define MHD_POSIX_SOCKETS 1
43 # else /* defined(_WIN32) && !defined(__CYGWIN__) */
44 # define MHD_WINSOCK_SOCKETS 1
45 # endif /* defined(_WIN32) && !defined(__CYGWIN__) */
46 #endif /* !MHD_POSIX_SOCKETS && !MHD_WINSOCK_SOCKETS */
47 
48 /*
49  * MHD require headers that define socket type, socket basic functions
50  * (socket(), accept(), listen(), bind(), send(), recv(), select()), socket
51  * parameters like SOCK_CLOEXEC, SOCK_NONBLOCK, additional socket functions
52  * (poll(), epoll(), accept4()), struct timeval and other types, required
53  * for socket function.
54  */
55 #if defined(MHD_POSIX_SOCKETS)
56 # ifdef HAVE_SYS_TYPES_H
57 # include <sys/types.h> /* required on old platforms */
58 # endif
59 # ifdef HAVE_SYS_SOCKET_H
60 # include <sys/socket.h>
61 # endif
62 # if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
63 # ifdef HAVE_SOCKLIB_H
64 # include <sockLib.h>
65 # endif /* HAVE_SOCKLIB_H */
66 # ifdef HAVE_INETLIB_H
67 # include <inetLib.h>
68 # endif /* HAVE_INETLIB_H */
69 # include <strings.h> /* required for FD_SET (bzero() function) */
70 # endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
71 # ifdef HAVE_NETINET_IN_H
72 # include <netinet/in.h>
73 # endif /* HAVE_NETINET_IN_H */
74 # ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
76 # endif
77 # ifdef HAVE_NET_IF_H
78 # include <net/if.h>
79 # endif
80 # ifdef HAVE_SYS_TIME_H
81 # include <sys/time.h>
82 # endif
83 # ifdef HAVE_TIME_H
84 # include <time.h>
85 # endif
86 # ifdef HAVE_NETDB_H
87 # include <netdb.h>
88 # endif
89 # ifdef HAVE_SYS_SELECT_H
90 # include <sys/select.h>
91 # endif
92 # ifdef EPOLL_SUPPORT
93 # include <sys/epoll.h>
94 # endif
95 # ifdef HAVE_NETINET_TCP_H
96 /* for TCP_FASTOPEN and TCP_CORK */
97 # include <netinet/tcp.h>
98 # endif
99 # ifdef HAVE_STRING_H
100 # include <string.h> /* for strerror() */
101 # endif
102 #elif defined(MHD_WINSOCK_SOCKETS)
103 # ifndef WIN32_LEAN_AND_MEAN
104 # define WIN32_LEAN_AND_MEAN 1
105 # endif /* !WIN32_LEAN_AND_MEAN */
106 # include <winsock2.h>
107 # include <ws2tcpip.h>
108 #endif /* MHD_WINSOCK_SOCKETS */
109 
110 #if defined(HAVE_POLL_H) && defined(HAVE_POLL)
111 # include <poll.h>
112 #endif
113 
114 #include <stddef.h>
115 #if defined(_MSC_FULL_VER) && ! defined (_SSIZE_T_DEFINED)
116 # include <stdint.h>
117 # define _SSIZE_T_DEFINED
118 typedef intptr_t ssize_t;
119 #endif /* !_SSIZE_T_DEFINED */
120 
121 #include "mhd_limits.h"
122 
123 #ifdef _MHD_FD_SETSIZE_IS_DEFAULT
124 # define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
125 #else /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
126 # include "sysfdsetsize.h"
127 # define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value ()
128 #endif /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
129 
130 #ifndef MHD_PANIC
131 # include <stdio.h>
132 # include <stdlib.h>
133 /* Simple implementation of MHD_PANIC, to be used outside lib */
134 # define MHD_PANIC(msg) do { fprintf (stderr, \
135  "Abnormal termination at %d line in file %s: %s\n", \
136  (int) __LINE__, __FILE__, msg); abort (); \
137 } while (0)
138 #endif /* ! MHD_PANIC */
139 
140 #ifndef MHD_SOCKET_DEFINED
141 
144 # if defined(MHD_POSIX_SOCKETS)
145 typedef int MHD_socket;
146 # define MHD_INVALID_SOCKET (-1)
147 # elif defined(MHD_WINSOCK_SOCKETS)
148 typedef SOCKET MHD_socket;
149 # define MHD_INVALID_SOCKET (INVALID_SOCKET)
150 # endif /* MHD_WINSOCK_SOCKETS */
151 
152 # define MHD_SOCKET_DEFINED 1
153 #endif /* ! MHD_SOCKET_DEFINED */
154 
155 #ifdef SOCK_CLOEXEC
156 # define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
157 #else /* ! SOCK_CLOEXEC */
158 # define MAYBE_SOCK_CLOEXEC 0
159 #endif /* ! SOCK_CLOEXEC */
160 
161 #ifdef HAVE_SOCK_NONBLOCK
162 # define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
163 #else /* ! HAVE_SOCK_NONBLOCK */
164 # define MAYBE_SOCK_NONBLOCK 0
165 #endif /* ! HAVE_SOCK_NONBLOCK */
166 
167 #ifdef SOCK_NOSIGPIPE
168 # define MAYBE_SOCK_NOSIGPIPE SOCK_NOSIGPIPE
169 #else /* ! HAVE_SOCK_NONBLOCK */
170 # define MAYBE_SOCK_NOSIGPIPE 0
171 #endif /* ! HAVE_SOCK_NONBLOCK */
172 
173 #ifdef MSG_NOSIGNAL
174 # define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL
175 #else /* ! MSG_NOSIGNAL */
176 # define MAYBE_MSG_NOSIGNAL 0
177 #endif /* ! MSG_NOSIGNAL */
178 
179 #if ! defined(SHUT_WR) && defined(SD_SEND)
180 # define SHUT_WR SD_SEND
181 #endif
182 #if ! defined(SHUT_RD) && defined(SD_RECEIVE)
183 # define SHUT_RD SD_RECEIVE
184 #endif
185 #if ! defined(SHUT_RDWR) && defined(SD_BOTH)
186 # define SHUT_RDWR SD_BOTH
187 #endif
188 
189 #if HAVE_ACCEPT4 + 0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || \
190  defined(SOCK_CLOEXEC) || defined(SOCK_NOSIGPIPE))
191 # define USE_ACCEPT4 1
192 #endif
193 
194 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
195 # define USE_EPOLL_CREATE1 1
196 #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
197 
198 #ifdef TCP_FASTOPEN
199 
202 #define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
203 #endif
204 
205 
206 #if defined(TCP_CORK)
207 
210 #define MHD_TCP_CORK_NOPUSH TCP_CORK
211 #elif defined(TCP_NOPUSH)
212 
215 #define MHD_TCP_CORK_NOPUSH TCP_NOPUSH
216 #endif /* TCP_NOPUSH */
217 
218 
222 #ifdef MHD_POSIX_SOCKETS
223 typedef int MHD_SCKT_OPT_BOOL_;
224 #else /* MHD_WINSOCK_SOCKETS */
225 typedef BOOL MHD_SCKT_OPT_BOOL_;
226 #endif /* MHD_WINSOCK_SOCKETS */
227 
232 #if ! defined(MHD_WINSOCK_SOCKETS)
233 typedef size_t MHD_SCKT_SEND_SIZE_;
234 #else
235 typedef int MHD_SCKT_SEND_SIZE_;
236 #endif
237 
241 #if ! defined(MHD_WINSOCK_SOCKETS)
242 # define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX
243 #else
244 # define MHD_SCKT_SEND_MAX_SIZE_ INT_MAX
245 #endif
246 
257 #if ! defined(MHD_WINSOCK_SOCKETS)
258 # define MHD_socket_close_(fd) ((0 == close ((fd))) || (EBADF != errno))
259 #else
260 # define MHD_socket_close_(fd) (0 == closesocket ((fd)))
261 #endif
262 
268 #define MHD_socket_close_chk_(fd) do { \
269  if (! MHD_socket_close_ (fd)) \
270  MHD_PANIC (_ ("Close socket failed.\n")); \
271 } while (0)
272 
273 
281 #define MHD_send_(s,b,l) \
282  ((ssize_t) send ((s),(const void*) (b),(MHD_SCKT_SEND_SIZE_) (l), \
283  MAYBE_MSG_NOSIGNAL))
284 
285 
293 #define MHD_recv_(s,b,l) \
294  ((ssize_t) recv ((s),(void*) (b),(MHD_SCKT_SEND_SIZE_) (l), 0))
295 
296 
306 #if defined(MHD_POSIX_SOCKETS)
307 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < \
308  ((MHD_socket) \
309  setsize))
310 #elif defined(MHD_WINSOCK_SOCKETS)
311 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*) (pset)== \
312  (void*) 0) || \
313  (((fd_set*) (pset)) \
314  ->fd_count < \
315  ((unsigned) \
316  setsize)) || \
317  (FD_ISSET ((fd), \
318  (pset))) )
319 #endif
320 
329 #define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_ ((fd), \
330  (pset), \
331  FD_SETSIZE)
332 
341 #if defined(MHD_POSIX_SOCKETS)
342 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET ((fd), \
343  (pset))
344 #elif defined(MHD_WINSOCK_SOCKETS)
345 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) \
346  do { \
347  u_int _i_ = 0; \
348  fd_set*const _s_ = (fd_set*) (pset); \
349  while ((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array [_i_])) {++_i_;} \
350  if ((_i_ == _s_->fd_count)) {_s_->fd_array [_s_->fd_count ++] = (fd);} \
351  } while (0)
352 #endif
353 
354 /* MHD_SYS_select_ is wrapper macro for system select() function */
355 #if ! defined(MHD_WINSOCK_SOCKETS)
356 # define MHD_SYS_select_(n,r,w,e,t) select ((n),(r),(w),(e),(t))
357 #else
358 # define MHD_SYS_select_(n,r,w,e,t) \
359  ( ( (((void*) (r) == (void*) 0) || ((fd_set*) (r))->fd_count == 0) && \
360  (((void*) (w) == (void*) 0) || ((fd_set*) (w))->fd_count == 0) && \
361  (((void*) (e) == (void*) 0) || ((fd_set*) (e))->fd_count == 0) ) ? \
362  ( ((void*) (t) == (void*) 0) ? 0 : \
363  (Sleep (((struct timeval*) (t))->tv_sec * 1000 \
364  + ((struct timeval*) (t))->tv_usec / 1000), 0) ) : \
365  (select ((int) 0,(r),(w),(e),(t))) )
366 #endif
367 
368 #if defined(HAVE_POLL)
369 /* MHD_sys_poll_ is wrapper macro for system poll() function */
370 # if ! defined(MHD_WINSOCK_SOCKETS)
371 # define MHD_sys_poll_ poll
372 # else /* MHD_WINSOCK_SOCKETS */
373 # define MHD_sys_poll_ WSAPoll
374 # endif /* MHD_WINSOCK_SOCKETS */
375 
376 # ifdef POLLPRI
377 # define MHD_POLLPRI_OR_ZERO POLLPRI
378 # else /* ! POLLPRI */
379 # define MHD_POLLPRI_OR_ZERO 0
380 # endif /* ! POLLPRI */
381 # ifdef POLLRDBAND
382 # define MHD_POLLRDBAND_OR_ZERO POLLRDBAND
383 # else /* ! POLLRDBAND */
384 # define MHD_POLLRDBAND_OR_ZERO 0
385 # endif /* ! POLLRDBAND */
386 # ifdef POLLNVAL
387 # define MHD_POLLNVAL_OR_ZERO POLLNVAL
388 # else /* ! POLLNVAL */
389 # define MHD_POLLNVAL_OR_ZERO 0
390 # endif /* ! POLLNVAL */
391 
392 /* MHD_POLL_EVENTS_ERR_DISC is 'events' mask for errors and disconnect.
393  * Note: Out-of-band data is treated as error. */
394 # if defined(_WIN32) && ! defined(__CYGWIN__)
395 # define MHD_POLL_EVENTS_ERR_DISC POLLRDBAND
396 # elif defined(__linux__)
397 # define MHD_POLL_EVENTS_ERR_DISC POLLPRI
398 # else /* ! __linux__ */
399 # define MHD_POLL_EVENTS_ERR_DISC (MHD_POLLPRI_OR_ZERO \
400  | MHD_POLLRDBAND_OR_ZERO)
401 # endif /* ! __linux__ */
402 /* MHD_POLL_REVENTS_ERR_DISC is 'revents' mask for errors and disconnect.
403  * Note: Out-of-band data is treated as error. */
404 # define MHD_POLL_REVENTS_ERR_DISC \
405  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
406  | POLLERR | POLLHUP)
407 /* MHD_POLL_REVENTS_ERRROR is 'revents' mask for errors.
408  * Note: Out-of-band data is treated as error. */
409 # define MHD_POLL_REVENTS_ERRROR \
410  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
411  | POLLERR)
412 #endif /* HAVE_POLL */
413 
414 #define MHD_SCKT_MISSING_ERR_CODE_ 31450
415 
416 #if defined(MHD_POSIX_SOCKETS)
417 # if defined(EAGAIN)
418 # define MHD_SCKT_EAGAIN_ EAGAIN
419 # elif defined(EWOULDBLOCK)
420 # define MHD_SCKT_EAGAIN_ EWOULDBLOCK
421 # else /* !EAGAIN && !EWOULDBLOCK */
422 # define MHD_SCKT_EAGAIN_ MHD_SCKT_MISSING_ERR_CODE_
423 # endif /* !EAGAIN && !EWOULDBLOCK */
424 # if defined(EWOULDBLOCK)
425 # define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
426 # elif defined(EAGAIN)
427 # define MHD_SCKT_EWOULDBLOCK_ EAGAIN
428 # else /* !EWOULDBLOCK && !EAGAIN */
429 # define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
430 # endif /* !EWOULDBLOCK && !EAGAIN */
431 # ifdef EINTR
432 # define MHD_SCKT_EINTR_ EINTR
433 # else /* ! EINTR */
434 # define MHD_SCKT_EINTR_ MHD_SCKT_MISSING_ERR_CODE_
435 # endif /* ! EINTR */
436 # ifdef ECONNRESET
437 # define MHD_SCKT_ECONNRESET_ ECONNRESET
438 # else /* ! ECONNRESET */
439 # define MHD_SCKT_ECONNRESET_ MHD_SCKT_MISSING_ERR_CODE_
440 # endif /* ! ECONNRESET */
441 # ifdef ECONNABORTED
442 # define MHD_SCKT_ECONNABORTED_ ECONNABORTED
443 # else /* ! ECONNABORTED */
444 # define MHD_SCKT_ECONNABORTED_ MHD_SCKT_MISSING_ERR_CODE_
445 # endif /* ! ECONNABORTED */
446 # ifdef ENOTCONN
447 # define MHD_SCKT_ENOTCONN_ ENOTCONN
448 # else /* ! ENOTCONN */
449 # define MHD_SCKT_ENOTCONN_ MHD_SCKT_MISSING_ERR_CODE_
450 # endif /* ! ENOTCONN */
451 # ifdef EMFILE
452 # define MHD_SCKT_EMFILE_ EMFILE
453 # else /* ! EMFILE */
454 # define MHD_SCKT_EMFILE_ MHD_SCKT_MISSING_ERR_CODE_
455 # endif /* ! EMFILE */
456 # ifdef ENFILE
457 # define MHD_SCKT_ENFILE_ ENFILE
458 # else /* ! ENFILE */
459 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
460 # endif /* ! ENFILE */
461 # ifdef ENOMEM
462 # define MHD_SCKT_ENOMEM_ ENOMEM
463 # else /* ! ENOMEM */
464 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
465 # endif /* ! ENOMEM */
466 # ifdef ENOBUFS
467 # define MHD_SCKT_ENOBUFS_ ENOBUFS
468 # else /* ! ENOBUFS */
469 # define MHD_SCKT_ENOBUFS_ MHD_SCKT_MISSING_ERR_CODE_
470 # endif /* ! ENOBUFS */
471 # ifdef EBADF
472 # define MHD_SCKT_EBADF_ EBADF
473 # else /* ! EBADF */
474 # define MHD_SCKT_EBADF_ MHD_SCKT_MISSING_ERR_CODE_
475 # endif /* ! EBADF */
476 # ifdef ENOTSOCK
477 # define MHD_SCKT_ENOTSOCK_ ENOTSOCK
478 # else /* ! ENOTSOCK */
479 # define MHD_SCKT_ENOTSOCK_ MHD_SCKT_MISSING_ERR_CODE_
480 # endif /* ! ENOTSOCK */
481 # ifdef EINVAL
482 # define MHD_SCKT_EINVAL_ EINVAL
483 # else /* ! EINVAL */
484 # define MHD_SCKT_EINVAL_ MHD_SCKT_MISSING_ERR_CODE_
485 # endif /* ! EINVAL */
486 # ifdef EFAULT
487 # define MHD_SCKT_EFAUL_ EFAULT
488 # else /* ! EFAULT */
489 # define MHD_SCKT_EFAUL_ MHD_SCKT_MISSING_ERR_CODE_
490 # endif /* ! EFAULT */
491 # ifdef ENOSYS
492 # define MHD_SCKT_ENOSYS_ ENOSYS
493 # else /* ! ENOSYS */
494 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
495 # endif /* ! ENOSYS */
496 # ifdef ENOTSUP
497 # define MHD_SCKT_ENOTSUP_ ENOTSUP
498 # else /* ! ENOTSUP */
499 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
500 # endif /* ! ENOTSUP */
501 # ifdef EOPNOTSUPP
502 # define MHD_SCKT_EOPNOTSUPP_ EOPNOTSUPP
503 # else /* ! EOPNOTSUPP */
504 # define MHD_SCKT_EOPNOTSUPP_ MHD_SCKT_MISSING_ERR_CODE_
505 # endif /* ! EOPNOTSUPP */
506 # ifdef EACCES
507 # define MHD_SCKT_EACCESS_ EACCES
508 # else /* ! EACCES */
509 # define MHD_SCKT_EACCESS_ MHD_SCKT_MISSING_ERR_CODE_
510 # endif /* ! EACCES */
511 # ifdef ENETDOWN
512 # define MHD_SCKT_ENETDOWN_ ENETDOWN
513 # else /* ! ENETDOWN */
514 # define MHD_SCKT_ENETDOWN_ MHD_SCKT_MISSING_ERR_CODE_
515 # endif /* ! ENETDOWN */
516 #elif defined(MHD_WINSOCK_SOCKETS)
517 # define MHD_SCKT_EAGAIN_ WSAEWOULDBLOCK
518 # define MHD_SCKT_EWOULDBLOCK_ WSAEWOULDBLOCK
519 # define MHD_SCKT_EINTR_ WSAEINTR
520 # define MHD_SCKT_ECONNRESET_ WSAECONNRESET
521 # define MHD_SCKT_ECONNABORTED_ WSAECONNABORTED
522 # define MHD_SCKT_ENOTCONN_ WSAENOTCONN
523 # define MHD_SCKT_EMFILE_ WSAEMFILE
524 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
525 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
526 # define MHD_SCKT_ENOBUFS_ WSAENOBUFS
527 # define MHD_SCKT_EBADF_ WSAEBADF
528 # define MHD_SCKT_ENOTSOCK_ WSAENOTSOCK
529 # define MHD_SCKT_EINVAL_ WSAEINVAL
530 # define MHD_SCKT_EFAUL_ WSAEFAULT
531 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
532 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
533 # define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
534 # define MHD_SCKT_EACCESS_ WSAEACCES
535 # define MHD_SCKT_ENETDOWN_ WSAENETDOWN
536 #endif
537 
542 #if defined(MHD_POSIX_SOCKETS)
543 # define MHD_socket_get_error_() (errno)
544 #elif defined(MHD_WINSOCK_SOCKETS)
545 # define MHD_socket_get_error_() WSAGetLastError ()
546 #endif
547 
548 #ifdef MHD_WINSOCK_SOCKETS
549 /* POSIX-W32 sockets compatibility functions */
550 
556 const char*MHD_W32_strerror_winsock_ (int err);
557 #endif /* MHD_WINSOCK_SOCKETS */
558 
559 /* MHD_socket_last_strerr_ is description string of specified socket error code */
560 #if defined(MHD_POSIX_SOCKETS)
561 # define MHD_socket_strerr_(err) strerror ((err))
562 #elif defined(MHD_WINSOCK_SOCKETS)
563 # define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_ ((err))
564 #endif
565 
566 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
567  * description string of last socket error (W32) */
568 #define MHD_socket_last_strerr_() MHD_socket_strerr_ (MHD_socket_get_error_ ())
569 
573 #if defined(MHD_POSIX_SOCKETS)
574 # define MHD_socket_fset_error_(err) (errno = (err))
575 #elif defined(MHD_WINSOCK_SOCKETS)
576 # define MHD_socket_fset_error_(err) (WSASetLastError ((err)))
577 #endif
578 
587 #define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
588  (MHD_socket_fset_error_ ((err)), ! 0) : \
589  0)
590 
596 #if defined(MHD_POSIX_SOCKETS)
597 # if defined(ENOSYS)
598 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
599  (errno = ENOSYS) : (errno = (err)) )
600 # elif defined(EOPNOTSUPP)
601 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
602  (errno = EOPNOTSUPP) : (errno = \
603  (err)) )
604 # elif defined (EFAULT)
605 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
606  (errno = EFAULT) : (errno = (err)) )
607 # elif defined (EINVAL)
608 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
609  (errno = EINVAL) : (errno = (err)) )
610 # else /* !EOPNOTSUPP && !EFAULT && !EINVAL */
611 # warning \
612  No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
613 # define MHD_socket_set_error_(err) (errno = (err))
614 # endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
615 #elif defined(MHD_WINSOCK_SOCKETS)
616 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
617  (WSASetLastError ((WSAEOPNOTSUPP))) : \
618  (WSASetLastError ((err))) )
619 #endif
620 
630 #define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
631  ((code) == (err)) )
632 
642 #define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_ (MHD_socket_get_error_ (), \
643  (code))
644 
645 /* Specific error code checks */
646 
653 #define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EINTR_)
654 
661 #if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
662 # define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EAGAIN_)
663 #else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
664 # define MHD_SCKT_ERR_IS_EAGAIN_(err) (MHD_SCKT_ERR_IS_ ((err), \
665  MHD_SCKT_EAGAIN_) || \
666  MHD_SCKT_ERR_IS_ ((err), \
667  MHD_SCKT_EWOULDBLOCK_) )
668 #endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
669 
675 #define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) (MHD_SCKT_ERR_IS_ ((err), \
676  MHD_SCKT_EMFILE_) \
677  || \
678  MHD_SCKT_ERR_IS_ ((err), \
679  MHD_SCKT_ENFILE_) \
680  || \
681  MHD_SCKT_ERR_IS_ ((err), \
682  MHD_SCKT_ENOMEM_) \
683  || \
684  MHD_SCKT_ERR_IS_ ((err), \
685  MHD_SCKT_ENOBUFS_) )
686 
693 #if defined(MHD_POSIX_SOCKETS)
694 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
695  MHD_SCKT_ECONNABORTED_)
696 #elif defined(MHD_WINSOCK_SOCKETS)
697 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
698  MHD_SCKT_ECONNRESET_)
699 #endif
700 
707 #define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) (MHD_SCKT_ERR_IS_ ((err), \
708  MHD_SCKT_ECONNRESET_) \
709  || \
710  MHD_SCKT_ERR_IS_ ((err), \
711  MHD_SCKT_ECONNABORTED_))
712 
713 /* Specific error code set */
714 
719 #if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
720 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
721  MHD_SCKT_ENOMEM_)
722 #elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
723 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
724  MHD_SCKT_ENOBUFS_)
725 #else
726 # warning \
727  No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
728 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
729  MHD_SCKT_ENOMEM_)
730 #endif
731 
732 /* Socket functions */
733 
734 #if defined(AF_LOCAL)
735 # define MHD_SCKT_LOCAL AF_LOCAL
736 #elif defined(AF_UNIX)
737 # define MHD_SCKT_LOCAL AF_UNIX
738 #endif /* AF_UNIX */
739 
740 #if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
741 # define MHD_socket_pair_(fdarr) (! socketpair (MHD_SCKT_LOCAL, SOCK_STREAM, 0, \
742  (fdarr)))
743 # if defined(HAVE_SOCK_NONBLOCK)
744 # define MHD_socket_pair_nblk_(fdarr) (! socketpair (MHD_SCKT_LOCAL, \
745  SOCK_STREAM \
746  | SOCK_NONBLOCK, 0, \
747  (fdarr)))
748 # endif /* HAVE_SOCK_NONBLOCK*/
749 #elif defined(MHD_WINSOCK_SOCKETS)
750 
757 int MHD_W32_socket_pair_ (SOCKET sockets_pair[2], int non_blk);
758 
759 # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_ ((fdarr), 0)
760 # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_ ((fdarr), 1)
761 #endif
762 
773 int
775  fd_set *set,
776  MHD_socket *max_fd,
777  unsigned int fd_setsize);
778 
779 
786 int
788 
789 
799 int
801  bool on);
802 
810 int
812 
813 
833 int
835  bool on);
836 
837 
844 int
846 
847 
848 #if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
849 static const int _MHD_socket_int_one = 1;
856 # define MHD_socket_nosignal_(sock) \
857  (! setsockopt ((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one, \
858  sizeof(_MHD_socket_int_one)))
859 #endif /* SOL_SOCKET && SO_NOSIGPIPE */
860 
869 
870 #endif /* ! MHD_SOCKETS_H */
int MHD_add_to_fd_set_(MHD_socket fd, fd_set *set, MHD_socket *max_fd, unsigned int fd_setsize)
Definition: mhd_sockets.c:375
additional automatic macros for MHD_config.h
int MHD_socket_nonblocking_(MHD_socket sock)
Definition: mhd_sockets.c:405
int MHD_socket_cork_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:504
int MHD_socket_buffering_reset_(MHD_socket sock)
Definition: mhd_sockets.c:550
int MHD_socket_noninheritable_(MHD_socket sock)
Definition: mhd_sockets.c:440
int MHD_socket
Definition: microhttpd.h:187
int MHD_socket
Definition: mhd_sockets.h:144
size_t MHD_SCKT_SEND_SIZE_
Definition: mhd_sockets.h:213
int MHD_socket_set_nodelay_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:475
int fd
Definition: microhttpd.h:3161
int MHD_SCKT_OPT_BOOL_
Definition: mhd_sockets.h:203
Helper for obtaining FD_SETSIZE system default value.
MHD_socket MHD_socket_create_listen_(int pf)
Definition: mhd_sockets.c:472
limits values definitions