GNU libmicrohttpd  0.9.63
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();} while(0)
136 #endif /* ! MHD_PANIC */
137 
138 #ifndef MHD_SOCKET_DEFINED
139 
142 # if defined(MHD_POSIX_SOCKETS)
143  typedef int MHD_socket;
144 # define MHD_INVALID_SOCKET (-1)
145 # elif defined(MHD_WINSOCK_SOCKETS)
146  typedef SOCKET MHD_socket;
147 # define MHD_INVALID_SOCKET (INVALID_SOCKET)
148 # endif /* MHD_WINSOCK_SOCKETS */
149 
150 # define MHD_SOCKET_DEFINED 1
151 #endif /* ! MHD_SOCKET_DEFINED */
152 
153 #ifdef SOCK_CLOEXEC
154 # define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
155 #else /* ! SOCK_CLOEXEC */
156 # define MAYBE_SOCK_CLOEXEC 0
157 #endif /* ! SOCK_CLOEXEC */
158 
159 #ifdef HAVE_SOCK_NONBLOCK
160 # define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
161 #else /* ! HAVE_SOCK_NONBLOCK */
162 # define MAYBE_SOCK_NONBLOCK 0
163 #endif /* ! HAVE_SOCK_NONBLOCK */
164 
165 #ifdef MSG_NOSIGNAL
166 # define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL
167 #else /* ! MSG_NOSIGNAL */
168 # define MAYBE_MSG_NOSIGNAL 0
169 #endif /* ! MSG_NOSIGNAL */
170 
171 #if !defined(SHUT_WR) && defined(SD_SEND)
172 # define SHUT_WR SD_SEND
173 #endif
174 #if !defined(SHUT_RD) && defined(SD_RECEIVE)
175 # define SHUT_RD SD_RECEIVE
176 #endif
177 #if !defined(SHUT_RDWR) && defined(SD_BOTH)
178 # define SHUT_RDWR SD_BOTH
179 #endif
180 
181 #if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC))
182 # define USE_ACCEPT4 1
183 #endif
184 
185 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
186 # define USE_EPOLL_CREATE1 1
187 #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
188 
189 #ifdef TCP_FASTOPEN
190 
193 #define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
194 #endif
195 
196 
197 #if defined(TCP_CORK)
198 
201 #define MHD_TCP_CORK_NOPUSH TCP_CORK
202 #elif defined(TCP_NOPUSH)
203 
206 #define MHD_TCP_CORK_NOPUSH TCP_NOPUSH
207 #endif /* TCP_NOPUSH */
208 
209 
213 #ifdef MHD_POSIX_SOCKETS
214  typedef int MHD_SCKT_OPT_BOOL_;
215 #else /* MHD_WINSOCK_SOCKETS */
216  typedef BOOL MHD_SCKT_OPT_BOOL_;
217 #endif /* MHD_WINSOCK_SOCKETS */
218 
223 #if !defined(MHD_WINSOCK_SOCKETS)
224  typedef size_t MHD_SCKT_SEND_SIZE_;
225 #else
226  typedef int MHD_SCKT_SEND_SIZE_;
227 #endif
228 
232 #if !defined(MHD_WINSOCK_SOCKETS)
233 # define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX
234 #else
235 # define MHD_SCKT_SEND_MAX_SIZE_ INT_MAX
236 #endif
237 
248 #if !defined(MHD_WINSOCK_SOCKETS)
249 # define MHD_socket_close_(fd) ((0 == close((fd))) || (EBADF != errno))
250 #else
251 # define MHD_socket_close_(fd) (0 == closesocket((fd)))
252 #endif
253 
259 #define MHD_socket_close_chk_(fd) do { \
260  if (!MHD_socket_close_(fd)) \
261  MHD_PANIC(_("Close socket failed.\n")); \
262  } while(0)
263 
264 
272 #define MHD_send_(s,b,l) \
273  ((ssize_t)send((s),(const void*)(b),((MHD_SCKT_SEND_SIZE_)l), MAYBE_MSG_NOSIGNAL))
274 
275 
283 #define MHD_recv_(s,b,l) \
284  ((ssize_t)recv((s),(void*)(b),((MHD_SCKT_SEND_SIZE_)l), 0))
285 
286 
296 #if defined(MHD_POSIX_SOCKETS)
297 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < ((MHD_socket)setsize))
298 #elif defined(MHD_WINSOCK_SOCKETS)
299 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*)(pset)==(void*)0) || \
300  (((fd_set*)(pset))->fd_count < ((unsigned)setsize)) || \
301  (FD_ISSET((fd),(pset))) )
302 #endif
303 
312 #define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_((fd),(pset),FD_SETSIZE)
313 
322 #if defined(MHD_POSIX_SOCKETS)
323 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET((fd),(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 | MHD_POLLRDBAND_OR_ZERO)
380 # endif /* ! __linux__ */
381 /* MHD_POLL_REVENTS_ERR_DISC is 'revents' mask for errors and disconnect.
382  * Note: Out-of-band data is treated as error. */
383 # define MHD_POLL_REVENTS_ERR_DISC \
384  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO | POLLERR | POLLHUP)
385 /* MHD_POLL_REVENTS_ERRROR is 'revents' mask for errors.
386  * Note: Out-of-band data is treated as error. */
387 # define MHD_POLL_REVENTS_ERRROR \
388  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO | POLLERR)
389 #endif /* HAVE_POLL */
390 
391 #define MHD_SCKT_MISSING_ERR_CODE_ 31450
392 
393 #if defined(MHD_POSIX_SOCKETS)
394 # if defined(EAGAIN)
395 # define MHD_SCKT_EAGAIN_ EAGAIN
396 # elif defined(EWOULDBLOCK)
397 # define MHD_SCKT_EAGAIN_ EWOULDBLOCK
398 # else /* !EAGAIN && !EWOULDBLOCK */
399 # define MHD_SCKT_EAGAIN_ MHD_SCKT_MISSING_ERR_CODE_
400 # endif /* !EAGAIN && !EWOULDBLOCK */
401 # if defined(EWOULDBLOCK)
402 # define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
403 # elif defined(EAGAIN)
404 # define MHD_SCKT_EWOULDBLOCK_ EAGAIN
405 # else /* !EWOULDBLOCK && !EAGAIN */
406 # define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
407 # endif /* !EWOULDBLOCK && !EAGAIN */
408 # ifdef EINTR
409 # define MHD_SCKT_EINTR_ EINTR
410 # else /* ! EINTR */
411 # define MHD_SCKT_EINTR_ MHD_SCKT_MISSING_ERR_CODE_
412 # endif /* ! EINTR */
413 # ifdef ECONNRESET
414 # define MHD_SCKT_ECONNRESET_ ECONNRESET
415 # else /* ! ECONNRESET */
416 # define MHD_SCKT_ECONNRESET_ MHD_SCKT_MISSING_ERR_CODE_
417 # endif /* ! ECONNRESET */
418 # ifdef ECONNABORTED
419 # define MHD_SCKT_ECONNABORTED_ ECONNABORTED
420 # else /* ! ECONNABORTED */
421 # define MHD_SCKT_ECONNABORTED_ MHD_SCKT_MISSING_ERR_CODE_
422 # endif /* ! ECONNABORTED */
423 # ifdef ENOTCONN
424 # define MHD_SCKT_ENOTCONN_ ENOTCONN
425 # else /* ! ENOTCONN */
426 # define MHD_SCKT_ENOTCONN_ MHD_SCKT_MISSING_ERR_CODE_
427 # endif /* ! ENOTCONN */
428 # ifdef EMFILE
429 # define MHD_SCKT_EMFILE_ EMFILE
430 # else /* ! EMFILE */
431 # define MHD_SCKT_EMFILE_ MHD_SCKT_MISSING_ERR_CODE_
432 # endif /* ! EMFILE */
433 # ifdef ENFILE
434 # define MHD_SCKT_ENFILE_ ENFILE
435 # else /* ! ENFILE */
436 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
437 # endif /* ! ENFILE */
438 # ifdef ENOMEM
439 # define MHD_SCKT_ENOMEM_ ENOMEM
440 # else /* ! ENOMEM */
441 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
442 # endif /* ! ENOMEM */
443 # ifdef ENOBUFS
444 # define MHD_SCKT_ENOBUFS_ ENOBUFS
445 # else /* ! ENOBUFS */
446 # define MHD_SCKT_ENOBUFS_ MHD_SCKT_MISSING_ERR_CODE_
447 # endif /* ! ENOBUFS */
448 # ifdef EBADF
449 # define MHD_SCKT_EBADF_ EBADF
450 # else /* ! EBADF */
451 # define MHD_SCKT_EBADF_ MHD_SCKT_MISSING_ERR_CODE_
452 # endif /* ! EBADF */
453 # ifdef ENOTSOCK
454 # define MHD_SCKT_ENOTSOCK_ ENOTSOCK
455 # else /* ! ENOTSOCK */
456 # define MHD_SCKT_ENOTSOCK_ MHD_SCKT_MISSING_ERR_CODE_
457 # endif /* ! ENOTSOCK */
458 # ifdef EINVAL
459 # define MHD_SCKT_EINVAL_ EINVAL
460 # else /* ! EINVAL */
461 # define MHD_SCKT_EINVAL_ MHD_SCKT_MISSING_ERR_CODE_
462 # endif /* ! EINVAL */
463 # ifdef EFAULT
464 # define MHD_SCKT_EFAUL_ EFAULT
465 # else /* ! EFAULT */
466 # define MHD_SCKT_EFAUL_ MHD_SCKT_MISSING_ERR_CODE_
467 # endif /* ! EFAULT */
468 # ifdef ENOSYS
469 # define MHD_SCKT_ENOSYS_ ENOSYS
470 # else /* ! ENOSYS */
471 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
472 # endif /* ! ENOSYS */
473 # ifdef ENOTSUP
474 # define MHD_SCKT_ENOTSUP_ ENOTSUP
475 # else /* ! ENOTSUP */
476 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
477 # endif /* ! ENOTSUP */
478 # ifdef EOPNOTSUPP
479 # define MHD_SCKT_EOPNOTSUPP_ EOPNOTSUPP
480 # else /* ! EOPNOTSUPP */
481 # define MHD_SCKT_EOPNOTSUPP_ MHD_SCKT_MISSING_ERR_CODE_
482 # endif /* ! EOPNOTSUPP */
483 # ifdef EACCES
484 # define MHD_SCKT_EACCESS_ EACCES
485 # else /* ! EACCES */
486 # define MHD_SCKT_EACCESS_ MHD_SCKT_MISSING_ERR_CODE_
487 # endif /* ! EACCES */
488 # ifdef ENETDOWN
489 # define MHD_SCKT_ENETDOWN_ ENETDOWN
490 # else /* ! ENETDOWN */
491 # define MHD_SCKT_ENETDOWN_ MHD_SCKT_MISSING_ERR_CODE_
492 # endif /* ! ENETDOWN */
493 #elif defined(MHD_WINSOCK_SOCKETS)
494 # define MHD_SCKT_EAGAIN_ WSAEWOULDBLOCK
495 # define MHD_SCKT_EWOULDBLOCK_ WSAEWOULDBLOCK
496 # define MHD_SCKT_EINTR_ WSAEINTR
497 # define MHD_SCKT_ECONNRESET_ WSAECONNRESET
498 # define MHD_SCKT_ECONNABORTED_ WSAECONNABORTED
499 # define MHD_SCKT_ENOTCONN_ WSAENOTCONN
500 # define MHD_SCKT_EMFILE_ WSAEMFILE
501 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
502 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
503 # define MHD_SCKT_ENOBUFS_ WSAENOBUFS
504 # define MHD_SCKT_EBADF_ WSAEBADF
505 # define MHD_SCKT_ENOTSOCK_ WSAENOTSOCK
506 # define MHD_SCKT_EINVAL_ WSAEINVAL
507 # define MHD_SCKT_EFAUL_ WSAEFAULT
508 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
509 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
510 # define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
511 # define MHD_SCKT_EACCESS_ WSAEACCES
512 # define MHD_SCKT_ENETDOWN_ WSAENETDOWN
513 #endif
514 
519 #if defined(MHD_POSIX_SOCKETS)
520 # define MHD_socket_get_error_() (errno)
521 #elif defined(MHD_WINSOCK_SOCKETS)
522 # define MHD_socket_get_error_() WSAGetLastError()
523 #endif
524 
525 #ifdef MHD_WINSOCK_SOCKETS
526  /* POSIX-W32 sockets compatibility functions */
527 
533  const char* MHD_W32_strerror_winsock_(int err);
534 #endif /* MHD_WINSOCK_SOCKETS */
535 
536 /* MHD_socket_last_strerr_ is description string of specified socket error code */
537 #if defined(MHD_POSIX_SOCKETS)
538 # define MHD_socket_strerr_(err) strerror((err))
539 #elif defined(MHD_WINSOCK_SOCKETS)
540 # define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_((err))
541 #endif
542 
543 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
544  * description string of last socket error (W32) */
545 #define MHD_socket_last_strerr_() MHD_socket_strerr_(MHD_socket_get_error_())
546 
550 #if defined(MHD_POSIX_SOCKETS)
551 # define MHD_socket_fset_error_(err) (errno = (err))
552 #elif defined(MHD_WINSOCK_SOCKETS)
553 # define MHD_socket_fset_error_(err) (WSASetLastError((err)))
554 #endif
555 
564 #define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
565  (MHD_socket_fset_error_((err)), !0) : 0 )
566 
572 #if defined(MHD_POSIX_SOCKETS)
573 # if defined(ENOSYS)
574 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
575  (errno = ENOSYS) : (errno = (err)) )
576 # elif defined(EOPNOTSUPP)
577 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
578  (errno = EOPNOTSUPP) : (errno = (err)) )
579 # elif defined (EFAULT)
580 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
581  (errno = EFAULT) : (errno = (err)) )
582 # elif defined (EINVAL)
583 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
584  (errno = EINVAL) : (errno = (err)) )
585 # else /* !EOPNOTSUPP && !EFAULT && !EINVAL */
586 # warning No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
587 # define MHD_socket_set_error_(err) (errno = (err))
588 # endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
589 #elif defined(MHD_WINSOCK_SOCKETS)
590 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
591  (WSASetLastError((WSAEOPNOTSUPP))) : \
592  (WSASetLastError((err))) )
593 #endif
594 
604 #define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
605  ((code) == (err)) )
606 
616 #define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_(MHD_socket_get_error_() ,(code))
617 
618 /* Specific error code checks */
619 
626 #define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EINTR_)
627 
634 #if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
635 # define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_)
636 #else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
637 # define MHD_SCKT_ERR_IS_EAGAIN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_) || \
638  MHD_SCKT_ERR_IS_((err),MHD_SCKT_EWOULDBLOCK_) )
639 #endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
640 
646 #define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EMFILE_) || \
647  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENFILE_) || \
648  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOMEM_) || \
649  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOBUFS_) )
650 
657 #if defined(MHD_POSIX_SOCKETS)
658 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_)
659 #elif defined(MHD_WINSOCK_SOCKETS)
660 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_)
661 #endif
662 
669 #define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_) || \
670  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_))
671 
672 /* Specific error code set */
673 
678 #if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
679 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
680 #elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
681 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOBUFS_)
682 #else
683 # warning No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
684 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
685 #endif
686 
687 /* Socket functions */
688 
689 #if defined(AF_LOCAL)
690 # define MHD_SCKT_LOCAL AF_LOCAL
691 #elif defined(AF_UNIX)
692 # define MHD_SCKT_LOCAL AF_UNIX
693 #endif /* AF_UNIX */
694 
695 #if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
696 # define MHD_socket_pair_(fdarr) (!socketpair(MHD_SCKT_LOCAL, SOCK_STREAM, 0, (fdarr)))
697 # if defined(HAVE_SOCK_NONBLOCK)
698 # define MHD_socket_pair_nblk_(fdarr) (!socketpair(MHD_SCKT_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr)))
699 # endif /* HAVE_SOCK_NONBLOCK*/
700 #elif defined(MHD_WINSOCK_SOCKETS)
701 
708  int MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk);
709 
710 # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr), 0)
711 # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_((fdarr), 1)
712 #endif
713 
724 int
726  fd_set *set,
727  MHD_socket *max_fd,
728  unsigned int fd_setsize);
729 
730 
737 int
739 
740 
748 int
750 
751 
758 int
760 
761 
762 #if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
763  static const int _MHD_socket_int_one = 1;
770 # define MHD_socket_nosignal_(sock) \
771  (!setsockopt((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one,sizeof(_MHD_socket_int_one)))
772 #endif /* SOL_SOCKET && SO_NOSIGPIPE */
773 
782 
783 #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:374
additional automatic macros for MHD_config.h
int MHD_socket_nonblocking_(MHD_socket sock)
Definition: mhd_sockets.c:404
int MHD_socket_buffering_reset_(MHD_socket sock)
Definition: mhd_sockets.c:471
int MHD_socket_noninheritable_(MHD_socket sock)
Definition: mhd_sockets.c:439
int MHD_socket
Definition: microhttpd.h:181
int MHD_socket
Definition: mhd_sockets.h:143
size_t MHD_SCKT_SEND_SIZE_
Definition: mhd_sockets.h:211
int fd
Definition: microhttpd.h:2837
int MHD_SCKT_OPT_BOOL_
Definition: mhd_sockets.h:201
Helper for obtaining FD_SETSIZE system default value.
MHD_socket MHD_socket_create_listen_(int pf)
Definition: mhd_sockets.c:471
limits values definitions