GNU libmicrohttpd  0.9.66
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 SOCK_NOSIGPIPE
166 # define MAYBE_SOCK_NOSIGPIPE SOCK_NOSIGPIPE
167 #else /* ! HAVE_SOCK_NONBLOCK */
168 # define MAYBE_SOCK_NOSIGPIPE 0
169 #endif /* ! HAVE_SOCK_NONBLOCK */
170 
171 #ifdef MSG_NOSIGNAL
172 # define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL
173 #else /* ! MSG_NOSIGNAL */
174 # define MAYBE_MSG_NOSIGNAL 0
175 #endif /* ! MSG_NOSIGNAL */
176 
177 #if !defined(SHUT_WR) && defined(SD_SEND)
178 # define SHUT_WR SD_SEND
179 #endif
180 #if !defined(SHUT_RD) && defined(SD_RECEIVE)
181 # define SHUT_RD SD_RECEIVE
182 #endif
183 #if !defined(SHUT_RDWR) && defined(SD_BOTH)
184 # define SHUT_RDWR SD_BOTH
185 #endif
186 
187 #if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC) || defined(SOCK_NOSIGPIPE))
188 # define USE_ACCEPT4 1
189 #endif
190 
191 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
192 # define USE_EPOLL_CREATE1 1
193 #endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
194 
195 #ifdef TCP_FASTOPEN
196 
199 #define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
200 #endif
201 
202 
203 #if defined(TCP_CORK)
204 
207 #define MHD_TCP_CORK_NOPUSH TCP_CORK
208 #elif defined(TCP_NOPUSH)
209 
212 #define MHD_TCP_CORK_NOPUSH TCP_NOPUSH
213 #endif /* TCP_NOPUSH */
214 
215 
219 #ifdef MHD_POSIX_SOCKETS
220  typedef int MHD_SCKT_OPT_BOOL_;
221 #else /* MHD_WINSOCK_SOCKETS */
222  typedef BOOL MHD_SCKT_OPT_BOOL_;
223 #endif /* MHD_WINSOCK_SOCKETS */
224 
229 #if !defined(MHD_WINSOCK_SOCKETS)
230  typedef size_t MHD_SCKT_SEND_SIZE_;
231 #else
232  typedef int MHD_SCKT_SEND_SIZE_;
233 #endif
234 
238 #if !defined(MHD_WINSOCK_SOCKETS)
239 # define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX
240 #else
241 # define MHD_SCKT_SEND_MAX_SIZE_ INT_MAX
242 #endif
243 
254 #if !defined(MHD_WINSOCK_SOCKETS)
255 # define MHD_socket_close_(fd) ((0 == close((fd))) || (EBADF != errno))
256 #else
257 # define MHD_socket_close_(fd) (0 == closesocket((fd)))
258 #endif
259 
265 #define MHD_socket_close_chk_(fd) do { \
266  if (!MHD_socket_close_(fd)) \
267  MHD_PANIC(_("Close socket failed.\n")); \
268  } while(0)
269 
270 
278 #define MHD_send_(s,b,l) \
279  ((ssize_t)send((s),(const void*)(b),(MHD_SCKT_SEND_SIZE_)(l), MAYBE_MSG_NOSIGNAL))
280 
281 
289 #define MHD_recv_(s,b,l) \
290  ((ssize_t)recv((s),(void*)(b),(MHD_SCKT_SEND_SIZE_)(l), 0))
291 
292 
302 #if defined(MHD_POSIX_SOCKETS)
303 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < ((MHD_socket)setsize))
304 #elif defined(MHD_WINSOCK_SOCKETS)
305 # define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*)(pset)==(void*)0) || \
306  (((fd_set*)(pset))->fd_count < ((unsigned)setsize)) || \
307  (FD_ISSET((fd),(pset))) )
308 #endif
309 
318 #define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_((fd),(pset),FD_SETSIZE)
319 
328 #if defined(MHD_POSIX_SOCKETS)
329 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET((fd),(pset))
330 #elif defined(MHD_WINSOCK_SOCKETS)
331 # define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) \
332  do { \
333  u_int _i_ = 0; \
334  fd_set* const _s_ = (fd_set*)(pset); \
335  while((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array[_i_])) {++_i_;} \
336  if ((_i_ == _s_->fd_count)) {_s_->fd_array[_s_->fd_count++] = (fd);} \
337  } while(0)
338 #endif
339 
340  /* MHD_SYS_select_ is wrapper macro for system select() function */
341 #if !defined(MHD_WINSOCK_SOCKETS)
342 # define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
343 #else
344 # define MHD_SYS_select_(n,r,w,e,t) \
345 ( ( (((void*)(r) == (void*)0) || ((fd_set*)(r))->fd_count == 0) && \
346  (((void*)(w) == (void*)0) || ((fd_set*)(w))->fd_count == 0) && \
347  (((void*)(e) == (void*)0) || ((fd_set*)(e))->fd_count == 0) ) ? \
348  ( ((void*)(t) == (void*)0) ? 0 : \
349  (Sleep(((struct timeval*)(t))->tv_sec * 1000 + \
350  ((struct timeval*)(t))->tv_usec / 1000), 0) ) : \
351  (select((int)0,(r),(w),(e),(t))) )
352 #endif
353 
354 #if defined(HAVE_POLL)
355 /* MHD_sys_poll_ is wrapper macro for system poll() function */
356 # if !defined(MHD_WINSOCK_SOCKETS)
357 # define MHD_sys_poll_ poll
358 # else /* MHD_WINSOCK_SOCKETS */
359 # define MHD_sys_poll_ WSAPoll
360 # endif /* MHD_WINSOCK_SOCKETS */
361 
362 # ifdef POLLPRI
363 # define MHD_POLLPRI_OR_ZERO POLLPRI
364 # else /* ! POLLPRI */
365 # define MHD_POLLPRI_OR_ZERO 0
366 # endif /* ! POLLPRI */
367 # ifdef POLLRDBAND
368 # define MHD_POLLRDBAND_OR_ZERO POLLRDBAND
369 # else /* ! POLLRDBAND */
370 # define MHD_POLLRDBAND_OR_ZERO 0
371 # endif /* ! POLLRDBAND */
372 # ifdef POLLNVAL
373 # define MHD_POLLNVAL_OR_ZERO POLLNVAL
374 # else /* ! POLLNVAL */
375 # define MHD_POLLNVAL_OR_ZERO 0
376 # endif /* ! POLLNVAL */
377 
378 /* MHD_POLL_EVENTS_ERR_DISC is 'events' mask for errors and disconnect.
379  * Note: Out-of-band data is treated as error. */
380 # if defined(_WIN32) && ! defined(__CYGWIN__)
381 # define MHD_POLL_EVENTS_ERR_DISC POLLRDBAND
382 # elif defined(__linux__)
383 # define MHD_POLL_EVENTS_ERR_DISC POLLPRI
384 # else /* ! __linux__ */
385 # define MHD_POLL_EVENTS_ERR_DISC (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO)
386 # endif /* ! __linux__ */
387 /* MHD_POLL_REVENTS_ERR_DISC is 'revents' mask for errors and disconnect.
388  * Note: Out-of-band data is treated as error. */
389 # define MHD_POLL_REVENTS_ERR_DISC \
390  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO | POLLERR | POLLHUP)
391 /* MHD_POLL_REVENTS_ERRROR is 'revents' mask for errors.
392  * Note: Out-of-band data is treated as error. */
393 # define MHD_POLL_REVENTS_ERRROR \
394  (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO | POLLERR)
395 #endif /* HAVE_POLL */
396 
397 #define MHD_SCKT_MISSING_ERR_CODE_ 31450
398 
399 #if defined(MHD_POSIX_SOCKETS)
400 # if defined(EAGAIN)
401 # define MHD_SCKT_EAGAIN_ EAGAIN
402 # elif defined(EWOULDBLOCK)
403 # define MHD_SCKT_EAGAIN_ EWOULDBLOCK
404 # else /* !EAGAIN && !EWOULDBLOCK */
405 # define MHD_SCKT_EAGAIN_ MHD_SCKT_MISSING_ERR_CODE_
406 # endif /* !EAGAIN && !EWOULDBLOCK */
407 # if defined(EWOULDBLOCK)
408 # define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
409 # elif defined(EAGAIN)
410 # define MHD_SCKT_EWOULDBLOCK_ EAGAIN
411 # else /* !EWOULDBLOCK && !EAGAIN */
412 # define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
413 # endif /* !EWOULDBLOCK && !EAGAIN */
414 # ifdef EINTR
415 # define MHD_SCKT_EINTR_ EINTR
416 # else /* ! EINTR */
417 # define MHD_SCKT_EINTR_ MHD_SCKT_MISSING_ERR_CODE_
418 # endif /* ! EINTR */
419 # ifdef ECONNRESET
420 # define MHD_SCKT_ECONNRESET_ ECONNRESET
421 # else /* ! ECONNRESET */
422 # define MHD_SCKT_ECONNRESET_ MHD_SCKT_MISSING_ERR_CODE_
423 # endif /* ! ECONNRESET */
424 # ifdef ECONNABORTED
425 # define MHD_SCKT_ECONNABORTED_ ECONNABORTED
426 # else /* ! ECONNABORTED */
427 # define MHD_SCKT_ECONNABORTED_ MHD_SCKT_MISSING_ERR_CODE_
428 # endif /* ! ECONNABORTED */
429 # ifdef ENOTCONN
430 # define MHD_SCKT_ENOTCONN_ ENOTCONN
431 # else /* ! ENOTCONN */
432 # define MHD_SCKT_ENOTCONN_ MHD_SCKT_MISSING_ERR_CODE_
433 # endif /* ! ENOTCONN */
434 # ifdef EMFILE
435 # define MHD_SCKT_EMFILE_ EMFILE
436 # else /* ! EMFILE */
437 # define MHD_SCKT_EMFILE_ MHD_SCKT_MISSING_ERR_CODE_
438 # endif /* ! EMFILE */
439 # ifdef ENFILE
440 # define MHD_SCKT_ENFILE_ ENFILE
441 # else /* ! ENFILE */
442 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
443 # endif /* ! ENFILE */
444 # ifdef ENOMEM
445 # define MHD_SCKT_ENOMEM_ ENOMEM
446 # else /* ! ENOMEM */
447 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
448 # endif /* ! ENOMEM */
449 # ifdef ENOBUFS
450 # define MHD_SCKT_ENOBUFS_ ENOBUFS
451 # else /* ! ENOBUFS */
452 # define MHD_SCKT_ENOBUFS_ MHD_SCKT_MISSING_ERR_CODE_
453 # endif /* ! ENOBUFS */
454 # ifdef EBADF
455 # define MHD_SCKT_EBADF_ EBADF
456 # else /* ! EBADF */
457 # define MHD_SCKT_EBADF_ MHD_SCKT_MISSING_ERR_CODE_
458 # endif /* ! EBADF */
459 # ifdef ENOTSOCK
460 # define MHD_SCKT_ENOTSOCK_ ENOTSOCK
461 # else /* ! ENOTSOCK */
462 # define MHD_SCKT_ENOTSOCK_ MHD_SCKT_MISSING_ERR_CODE_
463 # endif /* ! ENOTSOCK */
464 # ifdef EINVAL
465 # define MHD_SCKT_EINVAL_ EINVAL
466 # else /* ! EINVAL */
467 # define MHD_SCKT_EINVAL_ MHD_SCKT_MISSING_ERR_CODE_
468 # endif /* ! EINVAL */
469 # ifdef EFAULT
470 # define MHD_SCKT_EFAUL_ EFAULT
471 # else /* ! EFAULT */
472 # define MHD_SCKT_EFAUL_ MHD_SCKT_MISSING_ERR_CODE_
473 # endif /* ! EFAULT */
474 # ifdef ENOSYS
475 # define MHD_SCKT_ENOSYS_ ENOSYS
476 # else /* ! ENOSYS */
477 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
478 # endif /* ! ENOSYS */
479 # ifdef ENOTSUP
480 # define MHD_SCKT_ENOTSUP_ ENOTSUP
481 # else /* ! ENOTSUP */
482 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
483 # endif /* ! ENOTSUP */
484 # ifdef EOPNOTSUPP
485 # define MHD_SCKT_EOPNOTSUPP_ EOPNOTSUPP
486 # else /* ! EOPNOTSUPP */
487 # define MHD_SCKT_EOPNOTSUPP_ MHD_SCKT_MISSING_ERR_CODE_
488 # endif /* ! EOPNOTSUPP */
489 # ifdef EACCES
490 # define MHD_SCKT_EACCESS_ EACCES
491 # else /* ! EACCES */
492 # define MHD_SCKT_EACCESS_ MHD_SCKT_MISSING_ERR_CODE_
493 # endif /* ! EACCES */
494 # ifdef ENETDOWN
495 # define MHD_SCKT_ENETDOWN_ ENETDOWN
496 # else /* ! ENETDOWN */
497 # define MHD_SCKT_ENETDOWN_ MHD_SCKT_MISSING_ERR_CODE_
498 # endif /* ! ENETDOWN */
499 #elif defined(MHD_WINSOCK_SOCKETS)
500 # define MHD_SCKT_EAGAIN_ WSAEWOULDBLOCK
501 # define MHD_SCKT_EWOULDBLOCK_ WSAEWOULDBLOCK
502 # define MHD_SCKT_EINTR_ WSAEINTR
503 # define MHD_SCKT_ECONNRESET_ WSAECONNRESET
504 # define MHD_SCKT_ECONNABORTED_ WSAECONNABORTED
505 # define MHD_SCKT_ENOTCONN_ WSAENOTCONN
506 # define MHD_SCKT_EMFILE_ WSAEMFILE
507 # define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
508 # define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
509 # define MHD_SCKT_ENOBUFS_ WSAENOBUFS
510 # define MHD_SCKT_EBADF_ WSAEBADF
511 # define MHD_SCKT_ENOTSOCK_ WSAENOTSOCK
512 # define MHD_SCKT_EINVAL_ WSAEINVAL
513 # define MHD_SCKT_EFAUL_ WSAEFAULT
514 # define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
515 # define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
516 # define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
517 # define MHD_SCKT_EACCESS_ WSAEACCES
518 # define MHD_SCKT_ENETDOWN_ WSAENETDOWN
519 #endif
520 
525 #if defined(MHD_POSIX_SOCKETS)
526 # define MHD_socket_get_error_() (errno)
527 #elif defined(MHD_WINSOCK_SOCKETS)
528 # define MHD_socket_get_error_() WSAGetLastError()
529 #endif
530 
531 #ifdef MHD_WINSOCK_SOCKETS
532  /* POSIX-W32 sockets compatibility functions */
533 
539  const char* MHD_W32_strerror_winsock_(int err);
540 #endif /* MHD_WINSOCK_SOCKETS */
541 
542 /* MHD_socket_last_strerr_ is description string of specified socket error code */
543 #if defined(MHD_POSIX_SOCKETS)
544 # define MHD_socket_strerr_(err) strerror((err))
545 #elif defined(MHD_WINSOCK_SOCKETS)
546 # define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_((err))
547 #endif
548 
549 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
550  * description string of last socket error (W32) */
551 #define MHD_socket_last_strerr_() MHD_socket_strerr_(MHD_socket_get_error_())
552 
556 #if defined(MHD_POSIX_SOCKETS)
557 # define MHD_socket_fset_error_(err) (errno = (err))
558 #elif defined(MHD_WINSOCK_SOCKETS)
559 # define MHD_socket_fset_error_(err) (WSASetLastError((err)))
560 #endif
561 
570 #define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
571  (MHD_socket_fset_error_((err)), !0) : 0 )
572 
578 #if defined(MHD_POSIX_SOCKETS)
579 # if defined(ENOSYS)
580 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
581  (errno = ENOSYS) : (errno = (err)) )
582 # elif defined(EOPNOTSUPP)
583 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
584  (errno = EOPNOTSUPP) : (errno = (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 No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
593 # define MHD_socket_set_error_(err) (errno = (err))
594 # endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
595 #elif defined(MHD_WINSOCK_SOCKETS)
596 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
597  (WSASetLastError((WSAEOPNOTSUPP))) : \
598  (WSASetLastError((err))) )
599 #endif
600 
610 #define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
611  ((code) == (err)) )
612 
622 #define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_(MHD_socket_get_error_() ,(code))
623 
624 /* Specific error code checks */
625 
632 #define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EINTR_)
633 
640 #if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
641 # define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_)
642 #else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
643 # define MHD_SCKT_ERR_IS_EAGAIN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_) || \
644  MHD_SCKT_ERR_IS_((err),MHD_SCKT_EWOULDBLOCK_) )
645 #endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
646 
652 #define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EMFILE_) || \
653  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENFILE_) || \
654  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOMEM_) || \
655  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOBUFS_) )
656 
663 #if defined(MHD_POSIX_SOCKETS)
664 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_)
665 #elif defined(MHD_WINSOCK_SOCKETS)
666 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_)
667 #endif
668 
675 #define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_) || \
676  MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_))
677 
678 /* Specific error code set */
679 
684 #if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
685 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
686 #elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
687 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOBUFS_)
688 #else
689 # warning No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
690 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
691 #endif
692 
693 /* Socket functions */
694 
695 #if defined(AF_LOCAL)
696 # define MHD_SCKT_LOCAL AF_LOCAL
697 #elif defined(AF_UNIX)
698 # define MHD_SCKT_LOCAL AF_UNIX
699 #endif /* AF_UNIX */
700 
701 #if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
702 # define MHD_socket_pair_(fdarr) (!socketpair(MHD_SCKT_LOCAL, SOCK_STREAM, 0, (fdarr)))
703 # if defined(HAVE_SOCK_NONBLOCK)
704 # define MHD_socket_pair_nblk_(fdarr) (!socketpair(MHD_SCKT_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, (fdarr)))
705 # endif /* HAVE_SOCK_NONBLOCK*/
706 #elif defined(MHD_WINSOCK_SOCKETS)
707 
714  int MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk);
715 
716 # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_((fdarr), 0)
717 # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_((fdarr), 1)
718 #endif
719 
730 int
732  fd_set *set,
733  MHD_socket *max_fd,
734  unsigned int fd_setsize);
735 
736 
743 int
745 
746 
754 int
756 
757 
764 int
766 
767 
768 #if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
769  static const int _MHD_socket_int_one = 1;
776 # define MHD_socket_nosignal_(sock) \
777  (!setsockopt((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one,sizeof(_MHD_socket_int_one)))
778 #endif /* SOL_SOCKET && SO_NOSIGPIPE */
779 
788 
789 #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:187
int MHD_socket
Definition: mhd_sockets.h:143
size_t MHD_SCKT_SEND_SIZE_
Definition: mhd_sockets.h:211
int fd
Definition: microhttpd.h:3087
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