GNU libmicrohttpd  0.9.72
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 
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 # ifdef HAVE_SYS_TYPES_H
56 # include <sys/types.h> /* required on old platforms */
57 # endif
58 # ifdef HAVE_SYS_SOCKET_H
59 # include <sys/socket.h>
60 # endif
61 # if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
62 # ifdef HAVE_SOCKLIB_H
63 # include <sockLib.h>
64 # endif /* HAVE_SOCKLIB_H */
65 # ifdef HAVE_INETLIB_H
66 # include <inetLib.h>
67 # endif /* HAVE_INETLIB_H */
68 # include <strings.h> /* required for FD_SET (bzero() function) */
69 # endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
70 # ifdef HAVE_NETINET_IN_H
71 # include <netinet/in.h>
72 # endif /* HAVE_NETINET_IN_H */
73 # ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
75 # endif
76 # ifdef HAVE_NET_IF_H
77 # include <net/if.h>
78 # endif
79 # ifdef HAVE_SYS_TIME_H
80 # include <sys/time.h>
81 # endif
82 # ifdef HAVE_TIME_H
83 # include <time.h>
84 # endif
85 # ifdef HAVE_NETDB_H
86 # include <netdb.h>
87 # endif
88 # ifdef HAVE_SYS_SELECT_H
89 # include <sys/select.h>
90 # endif
91 # ifdef EPOLL_SUPPORT
92 # include <sys/epoll.h>
93 # endif
94 # ifdef HAVE_NETINET_TCP_H
95 /* for TCP_FASTOPEN and TCP_CORK */
96 # include <netinet/tcp.h>
97 # endif
98 # ifdef HAVE_STRING_H
99 # include <string.h> /* for strerror() */
100 # endif
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 #include <stddef.h>
114 #if defined(_MSC_FULL_VER) && ! defined (_SSIZE_T_DEFINED)
115 # include <stdint.h>
116 # define _SSIZE_T_DEFINED
117 typedef intptr_t ssize_t;
118 #endif /* !_SSIZE_T_DEFINED */
119 
120 #include "mhd_limits.h"
121 
122 #ifdef _MHD_FD_SETSIZE_IS_DEFAULT
123 # define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
124 #else /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
125 # include "sysfdsetsize.h"
126 # define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value ()
127 #endif /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
128 
129 #ifndef MHD_PANIC
130 # include <stdio.h>
131 # include <stdlib.h>
132 /* Simple implementation of MHD_PANIC, to be used outside lib */
133 # define MHD_PANIC(msg) do { fprintf (stderr, \
134  "Abnormal termination at %d line in file %s: %s\n", \
135  (int) __LINE__, __FILE__, msg); abort (); \
136 } while (0)
137 #endif /* ! MHD_PANIC */
138 
139 #ifndef MHD_SOCKET_DEFINED
143 # if defined(MHD_POSIX_SOCKETS)
144 typedef int MHD_socket;
145 # define MHD_INVALID_SOCKET (-1)
146 # elif defined(MHD_WINSOCK_SOCKETS)
147 typedef SOCKET MHD_socket;
148 # define MHD_INVALID_SOCKET (INVALID_SOCKET)
149 # endif /* MHD_WINSOCK_SOCKETS */
150 
151 # define MHD_SOCKET_DEFINED 1
152 #endif /* ! MHD_SOCKET_DEFINED */
153 
154 #ifdef SOCK_CLOEXEC
155 # define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
156 #else /* ! SOCK_CLOEXEC */
157 # define MAYBE_SOCK_CLOEXEC 0
158 #endif /* ! SOCK_CLOEXEC */
159 
160 #ifdef HAVE_SOCK_NONBLOCK
161 # define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
162 #else /* ! HAVE_SOCK_NONBLOCK */
163 # define MAYBE_SOCK_NONBLOCK 0
164 #endif /* ! HAVE_SOCK_NONBLOCK */
165 
166 #ifdef MSG_NOSIGNAL
167 # define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL
168 #else /* ! MSG_NOSIGNAL */
169 # define MAYBE_MSG_NOSIGNAL 0
170 #endif /* ! MSG_NOSIGNAL */
171 
172 #if ! defined(SHUT_WR) && defined(SD_SEND)
173 # define SHUT_WR SD_SEND
174 #endif
175 #if ! defined(SHUT_RD) && defined(SD_RECEIVE)
176 # define SHUT_RD SD_RECEIVE
177 #endif
178 #if ! defined(SHUT_RDWR) && defined(SD_BOTH)
179 # define SHUT_RDWR SD_BOTH
180 #endif
181 
182 #if HAVE_ACCEPT4 + 0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || \
183  defined(SOCK_CLOEXEC))
184 # define USE_ACCEPT4 1
185 #endif
186 
187 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
188 # define USE_EPOLL_CREATE1 1
189 #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
190 
191 #ifdef TCP_FASTOPEN
195 #define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
196 #endif
197 
198 
202 #ifdef MHD_POSIX_SOCKETS
203 typedef int MHD_SCKT_OPT_BOOL_;
204 #else /* MHD_WINSOCK_SOCKETS */
205 typedef BOOL MHD_SCKT_OPT_BOOL_;
206 #endif /* MHD_WINSOCK_SOCKETS */
207 
212 #if ! defined(MHD_WINSOCK_SOCKETS)
213 typedef size_t MHD_SCKT_SEND_SIZE_;
214 #else
215 typedef int MHD_SCKT_SEND_SIZE_;
216 #endif
217 
221 #if ! defined(MHD_WINSOCK_SOCKETS)
222 # define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX
223 #else
224 # define MHD_SCKT_SEND_MAX_SIZE_ INT_MAX
225 #endif
226 
237 #if ! defined(MHD_WINSOCK_SOCKETS)
238 # define MHD_socket_close_(fd) ((0 == close ((fd))) || (EBADF != errno))
239 #else
240 # define MHD_socket_close_(fd) (0 == closesocket ((fd)))
241 #endif
242 
248 #define MHD_socket_close_chk_(fd) do { \
249  if (! MHD_socket_close_ (fd)) \
250  MHD_PANIC (_ ("Close socket failed.\n")); \
251 } while (0)
252 
253 
261 #define MHD_send_(s,b,l) \
262  ((ssize_t) send ((s),(const void*) (b),((MHD_SCKT_SEND_SIZE_) l), \
263  MAYBE_MSG_NOSIGNAL))
264 
265 
273 #define MHD_recv_(s,b,l) \
274  ((ssize_t) recv ((s),(void*) (b),((MHD_SCKT_SEND_SIZE_) l), 0))
275 
276 
286 #if defined(MHD_POSIX_SOCKETS)
287 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < \
288  ((MHD_socket) \
289  setsize))
290 #elif defined(MHD_WINSOCK_SOCKETS)
291 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*) (pset)== \
292  (void*) 0) || \
293  (((fd_set*) (pset)) \
294  ->fd_count < \
295  ((unsigned) \
296  setsize)) || \
297  (FD_ISSET ((fd), \
298  (pset))) )
299 #endif
300 
309 #define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_ ((fd), \
310  (pset), \
311  FD_SETSIZE)
312 
321 #if defined(MHD_POSIX_SOCKETS)
322 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET ((fd), \
323  (pset))
324 #elif defined(MHD_WINSOCK_SOCKETS)
325 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) \
326  do { \
327  u_int _i_ = 0; \
328  fd_set*const _s_ = (fd_set*) (pset); \
329  while ((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array [_i_])) {++_i_;} \
330  if ((_i_ == _s_->fd_count)) {_s_->fd_array [_s_->fd_count ++] = (fd);} \
331  } while (0)
332 #endif
333 
334 /* MHD_SYS_select_ is wrapper macro for system select() function */
335 #if ! defined(MHD_WINSOCK_SOCKETS)
336 # define MHD_SYS_select_(n,r,w,e,t) select ((n),(r),(w),(e),(t))
337 #else
338 # define MHD_SYS_select_(n,r,w,e,t) \
339  ( ( (((void*) (r) == (void*) 0) || ((fd_set*) (r))->fd_count == 0) && \
340  (((void*) (w) == (void*) 0) || ((fd_set*) (w))->fd_count == 0) && \
341  (((void*) (e) == (void*) 0) || ((fd_set*) (e))->fd_count == 0) ) ? \
342  ( ((void*) (t) == (void*) 0) ? 0 : \
343  (Sleep (((struct timeval*) (t))->tv_sec * 1000 \
344  + ((struct timeval*) (t))->tv_usec / 1000), 0) ) : \
345  (select ((int) 0,(r),(w),(e),(t))) )
346 #endif
347 
348 #if defined(HAVE_POLL)
349 /* MHD_sys_poll_ is wrapper macro for system poll() function */
350 # if ! defined(MHD_WINSOCK_SOCKETS)
351 # define MHD_sys_poll_ poll
352 # else /* MHD_WINSOCK_SOCKETS */
353 # define MHD_sys_poll_ WSAPoll
354 # endif /* MHD_WINSOCK_SOCKETS */
355 
356 # ifdef POLLPRI
357 # define MHD_POLLPRI_OR_ZERO POLLPRI
358 # else /* ! POLLPRI */
359 # define MHD_POLLPRI_OR_ZERO 0
360 # endif /* ! POLLPRI */
361 # ifdef POLLRDBAND
362 # define MHD_POLLRDBAND_OR_ZERO POLLRDBAND
363 # else /* ! POLLRDBAND */
364 # define MHD_POLLRDBAND_OR_ZERO 0
365 # endif /* ! POLLRDBAND */
366 # ifdef POLLNVAL
367 # define MHD_POLLNVAL_OR_ZERO POLLNVAL
368 # else /* ! POLLNVAL */
369 # define MHD_POLLNVAL_OR_ZERO 0
370 # endif /* ! POLLNVAL */
371 
372 /* MHD_POLL_EVENTS_ERR_DISC is 'events' mask for errors and disconnect.
373  * Note: Out-of-band data is treated as error. */
374 # if defined(_WIN32) && ! defined(__CYGWIN__)
375 # define MHD_POLL_EVENTS_ERR_DISC POLLRDBAND
376 # elif defined(__linux__)
377 # define MHD_POLL_EVENTS_ERR_DISC POLLPRI
378 # else /* ! __linux__ */
379 # define MHD_POLL_EVENTS_ERR_DISC (MHD_POLLPRI_OR_ZERO \
380  | MHD_POLLRDBAND_OR_ZERO)
381 # endif /* ! __linux__ */
382 /* MHD_POLL_REVENTS_ERR_DISC is 'revents' mask for errors and disconnect.
383  * Note: Out-of-band data is treated as error. */
384 # define MHD_POLL_REVENTS_ERR_DISC \
385  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
386  | POLLERR | POLLHUP)
387 /* MHD_POLL_REVENTS_ERRROR is 'revents' mask for errors.
388  * Note: Out-of-band data is treated as error. */
389 # define MHD_POLL_REVENTS_ERRROR \
390  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
391  | POLLERR)
392 #endif /* HAVE_POLL */
393 
394 #define MHD_SCKT_MISSING_ERR_CODE_ 31450
395 
396 #if defined(MHD_POSIX_SOCKETS)
397 # if defined(EAGAIN)
398 # define MHD_SCKT_EAGAIN_ EAGAIN
399 # elif defined(EWOULDBLOCK)
400 # define MHD_SCKT_EAGAIN_ EWOULDBLOCK
401 # else /* !EAGAIN && !EWOULDBLOCK */
402 # define MHD_SCKT_EAGAIN_ MHD_SCKT_MISSING_ERR_CODE_
403 # endif /* !EAGAIN && !EWOULDBLOCK */
404 # if defined(EWOULDBLOCK)
405 # define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
406 # elif defined(EAGAIN)
407 # define MHD_SCKT_EWOULDBLOCK_ EAGAIN
408 # else /* !EWOULDBLOCK && !EAGAIN */
409 # define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
410 # endif /* !EWOULDBLOCK && !EAGAIN */
411 # ifdef EINTR
412 # define MHD_SCKT_EINTR_ EINTR
413 # else /* ! EINTR */
414 # define MHD_SCKT_EINTR_ MHD_SCKT_MISSING_ERR_CODE_
415 # endif /* ! EINTR */
416 # ifdef ECONNRESET
417 # define MHD_SCKT_ECONNRESET_ ECONNRESET
418 # else /* ! ECONNRESET */
419 # define MHD_SCKT_ECONNRESET_ MHD_SCKT_MISSING_ERR_CODE_
420 # endif /* ! ECONNRESET */
421 # ifdef ECONNABORTED
422 # define MHD_SCKT_ECONNABORTED_ ECONNABORTED
423 # else /* ! ECONNABORTED */
424 # define MHD_SCKT_ECONNABORTED_ MHD_SCKT_MISSING_ERR_CODE_
425 # endif /* ! ECONNABORTED */
426 # ifdef ENOTCONN
427 # define MHD_SCKT_ENOTCONN_ ENOTCONN
428 # else /* ! ENOTCONN */
429 # define MHD_SCKT_ENOTCONN_ MHD_SCKT_MISSING_ERR_CODE_
430 # endif /* ! ENOTCONN */
431 # ifdef EMFILE
432 # define MHD_SCKT_EMFILE_ EMFILE
433 # else /* ! EMFILE */
434 # define MHD_SCKT_EMFILE_ MHD_SCKT_MISSING_ERR_CODE_
435 # endif /* ! EMFILE */
436 # ifdef ENFILE
437 # define MHD_SCKT_ENFILE_ ENFILE
438 # else /* ! ENFILE */
439 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
440 # endif /* ! ENFILE */
441 # ifdef ENOMEM
442 # define MHD_SCKT_ENOMEM_ ENOMEM
443 # else /* ! ENOMEM */
444 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
445 # endif /* ! ENOMEM */
446 # ifdef ENOBUFS
447 # define MHD_SCKT_ENOBUFS_ ENOBUFS
448 # else /* ! ENOBUFS */
449 # define MHD_SCKT_ENOBUFS_ MHD_SCKT_MISSING_ERR_CODE_
450 # endif /* ! ENOBUFS */
451 # ifdef EBADF
452 # define MHD_SCKT_EBADF_ EBADF
453 # else /* ! EBADF */
454 # define MHD_SCKT_EBADF_ MHD_SCKT_MISSING_ERR_CODE_
455 # endif /* ! EBADF */
456 # ifdef ENOTSOCK
457 # define MHD_SCKT_ENOTSOCK_ ENOTSOCK
458 # else /* ! ENOTSOCK */
459 # define MHD_SCKT_ENOTSOCK_ MHD_SCKT_MISSING_ERR_CODE_
460 # endif /* ! ENOTSOCK */
461 # ifdef EINVAL
462 # define MHD_SCKT_EINVAL_ EINVAL
463 # else /* ! EINVAL */
464 # define MHD_SCKT_EINVAL_ MHD_SCKT_MISSING_ERR_CODE_
465 # endif /* ! EINVAL */
466 # ifdef EFAULT
467 # define MHD_SCKT_EFAUL_ EFAULT
468 # else /* ! EFAULT */
469 # define MHD_SCKT_EFAUL_ MHD_SCKT_MISSING_ERR_CODE_
470 # endif /* ! EFAULT */
471 # ifdef ENOSYS
472 # define MHD_SCKT_ENOSYS_ ENOSYS
473 # else /* ! ENOSYS */
474 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
475 # endif /* ! ENOSYS */
476 # ifdef ENOTSUP
477 # define MHD_SCKT_ENOTSUP_ ENOTSUP
478 # else /* ! ENOTSUP */
479 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
480 # endif /* ! ENOTSUP */
481 # ifdef EOPNOTSUPP
482 # define MHD_SCKT_EOPNOTSUPP_ EOPNOTSUPP
483 # else /* ! EOPNOTSUPP */
484 # define MHD_SCKT_EOPNOTSUPP_ MHD_SCKT_MISSING_ERR_CODE_
485 # endif /* ! EOPNOTSUPP */
486 # ifdef EACCES
487 # define MHD_SCKT_EACCESS_ EACCES
488 # else /* ! EACCES */
489 # define MHD_SCKT_EACCESS_ MHD_SCKT_MISSING_ERR_CODE_
490 # endif /* ! EACCES */
491 # ifdef ENETDOWN
492 # define MHD_SCKT_ENETDOWN_ ENETDOWN
493 # else /* ! ENETDOWN */
494 # define MHD_SCKT_ENETDOWN_ MHD_SCKT_MISSING_ERR_CODE_
495 # endif /* ! ENETDOWN */
496 #elif defined(MHD_WINSOCK_SOCKETS)
497 # define MHD_SCKT_EAGAIN_ WSAEWOULDBLOCK
498 # define MHD_SCKT_EWOULDBLOCK_ WSAEWOULDBLOCK
499 # define MHD_SCKT_EINTR_ WSAEINTR
500 # define MHD_SCKT_ECONNRESET_ WSAECONNRESET
501 # define MHD_SCKT_ECONNABORTED_ WSAECONNABORTED
502 # define MHD_SCKT_ENOTCONN_ WSAENOTCONN
503 # define MHD_SCKT_EMFILE_ WSAEMFILE
504 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
505 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
506 # define MHD_SCKT_ENOBUFS_ WSAENOBUFS
507 # define MHD_SCKT_EBADF_ WSAEBADF
508 # define MHD_SCKT_ENOTSOCK_ WSAENOTSOCK
509 # define MHD_SCKT_EINVAL_ WSAEINVAL
510 # define MHD_SCKT_EFAUL_ WSAEFAULT
511 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
512 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
513 # define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
514 # define MHD_SCKT_EACCESS_ WSAEACCES
515 # define MHD_SCKT_ENETDOWN_ WSAENETDOWN
516 #endif
517 
522 #if defined(MHD_POSIX_SOCKETS)
523 # define MHD_socket_get_error_() (errno)
524 #elif defined(MHD_WINSOCK_SOCKETS)
525 # define MHD_socket_get_error_() WSAGetLastError ()
526 #endif
527 
528 #ifdef MHD_WINSOCK_SOCKETS
529 /* POSIX-W32 sockets compatibility functions */
530 
536 const char*MHD_W32_strerror_winsock_ (int err);
537 
538 #endif /* MHD_WINSOCK_SOCKETS */
539 
540 /* MHD_socket_last_strerr_ is description string of specified socket error code */
541 #if defined(MHD_POSIX_SOCKETS)
542 # define MHD_socket_strerr_(err) strerror ((err))
543 #elif defined(MHD_WINSOCK_SOCKETS)
544 # define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_ ((err))
545 #endif
546 
547 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
548  * description string of last socket error (W32) */
549 #define MHD_socket_last_strerr_() MHD_socket_strerr_ (MHD_socket_get_error_ ())
550 
554 #if defined(MHD_POSIX_SOCKETS)
555 # define MHD_socket_fset_error_(err) (errno = (err))
556 #elif defined(MHD_WINSOCK_SOCKETS)
557 # define MHD_socket_fset_error_(err) (WSASetLastError ((err)))
558 #endif
559 
568 #define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
569  (MHD_socket_fset_error_ ((err)), ! 0) : \
570  0)
571 
577 #if defined(MHD_POSIX_SOCKETS)
578 # if defined(ENOSYS)
579 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
580  (errno = ENOSYS) : (errno = (err)) )
581 # elif defined(EOPNOTSUPP)
582 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
583  (errno = EOPNOTSUPP) : (errno = \
584  (err)) )
585 # elif defined (EFAULT)
586 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
587  (errno = EFAULT) : (errno = (err)) )
588 # elif defined (EINVAL)
589 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
590  (errno = EINVAL) : (errno = (err)) )
591 # else /* !EOPNOTSUPP && !EFAULT && !EINVAL */
592 # warning \
593  No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
594 # define MHD_socket_set_error_(err) (errno = (err))
595 # endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
596 #elif defined(MHD_WINSOCK_SOCKETS)
597 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
598  (WSASetLastError ((WSAEOPNOTSUPP))) : \
599  (WSASetLastError ((err))) )
600 #endif
601 
611 #define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
612  ((code) == (err)) )
613 
623 #define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_ (MHD_socket_get_error_ (), \
624  (code))
625 
626 /* Specific error code checks */
627 
634 #define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EINTR_)
635 
642 #if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
643 # define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EAGAIN_)
644 #else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
645 # define MHD_SCKT_ERR_IS_EAGAIN_(err) (MHD_SCKT_ERR_IS_ ((err), \
646  MHD_SCKT_EAGAIN_) || \
647  MHD_SCKT_ERR_IS_ ((err), \
648  MHD_SCKT_EWOULDBLOCK_) )
649 #endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
650 
656 #define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) (MHD_SCKT_ERR_IS_ ((err), \
657  MHD_SCKT_EMFILE_) \
658  || \
659  MHD_SCKT_ERR_IS_ ((err), \
660  MHD_SCKT_ENFILE_) \
661  || \
662  MHD_SCKT_ERR_IS_ ((err), \
663  MHD_SCKT_ENOMEM_) \
664  || \
665  MHD_SCKT_ERR_IS_ ((err), \
666  MHD_SCKT_ENOBUFS_) )
667 
674 #if defined(MHD_POSIX_SOCKETS)
675 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
676  MHD_SCKT_ECONNABORTED_)
677 #elif defined(MHD_WINSOCK_SOCKETS)
678 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
679  MHD_SCKT_ECONNRESET_)
680 #endif
681 
688 #define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) (MHD_SCKT_ERR_IS_ ((err), \
689  MHD_SCKT_ECONNRESET_) \
690  || \
691  MHD_SCKT_ERR_IS_ ((err), \
692  MHD_SCKT_ECONNABORTED_))
693 
694 /* Specific error code set */
695 
700 #if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
701 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
702  MHD_SCKT_ENOMEM_)
703 #elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
704 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
705  MHD_SCKT_ENOBUFS_)
706 #else
707 # warning \
708  No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
709 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
710  MHD_SCKT_ENOMEM_)
711 #endif
712 
713 /* Socket functions */
714 
715 #if defined(AF_LOCAL)
716 # define MHD_SCKT_LOCAL AF_LOCAL
717 #elif defined(AF_UNIX)
718 # define MHD_SCKT_LOCAL AF_UNIX
719 #endif /* AF_UNIX */
720 
721 #if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
722 # define MHD_socket_pair_(fdarr) (! socketpair (MHD_SCKT_LOCAL, SOCK_STREAM, 0, \
723  (fdarr)))
724 # if defined(HAVE_SOCK_NONBLOCK)
725 # define MHD_socket_pair_nblk_(fdarr) (! socketpair (MHD_SCKT_LOCAL, \
726  SOCK_STREAM \
727  | SOCK_NONBLOCK, 0, \
728  (fdarr)))
729 # endif /* HAVE_SOCK_NONBLOCK*/
730 #elif defined(MHD_WINSOCK_SOCKETS)
738 int MHD_W32_socket_pair_ (SOCKET sockets_pair[2], int non_blk);
739 
740 # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_ ((fdarr), 0)
741 # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_ ((fdarr), 1)
742 #endif
743 
754 int
756  fd_set *set,
757  MHD_socket *max_fd,
758  unsigned int fd_setsize);
759 
760 
767 int
769 
770 
778 int
780 
781 
782 #if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
783 static const int _MHD_socket_int_one = 1;
790 # define MHD_socket_nosignal_(sock) \
791  (! setsockopt ((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one, \
792  sizeof(_MHD_socket_int_one)))
793 #endif /* SOL_SOCKET && SO_NOSIGPIPE */
794 
803 
804 #endif /* ! MHD_SOCKETS_H */
int MHD_socket
Definition: mhd_sockets.h:144
int MHD_SCKT_OPT_BOOL_
Definition: mhd_sockets.h:203
size_t MHD_SCKT_SEND_SIZE_
Definition: mhd_sockets.h:213
int MHD_add_to_fd_set_(MHD_socket fd, fd_set *set, MHD_socket *max_fd, unsigned int fd_setsize)
Definition: mhd_sockets.c:377
int MHD_socket_noninheritable_(MHD_socket sock)
Definition: mhd_sockets.c:442
int MHD_socket_nonblocking_(MHD_socket sock)
Definition: mhd_sockets.c:407
MHD_socket MHD_socket_create_listen_(int pf)
Definition: mhd_sockets.c:474
additional automatic macros for MHD_config.h
limits values definitions
Helper for obtaining FD_SETSIZE system default value.
int MHD_socket
Definition: microhttpd.h:196
int fd
Definition: microhttpd.h:3195