GNU libmicrohttpd  0.9.69
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 
558 #endif /* MHD_WINSOCK_SOCKETS */
559 
560 /* MHD_socket_last_strerr_ is description string of specified socket error code */
561 #if defined(MHD_POSIX_SOCKETS)
562 # define MHD_socket_strerr_(err) strerror ((err))
563 #elif defined(MHD_WINSOCK_SOCKETS)
564 # define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_ ((err))
565 #endif
566 
567 /* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
568  * description string of last socket error (W32) */
569 #define MHD_socket_last_strerr_() MHD_socket_strerr_ (MHD_socket_get_error_ ())
570 
574 #if defined(MHD_POSIX_SOCKETS)
575 # define MHD_socket_fset_error_(err) (errno = (err))
576 #elif defined(MHD_WINSOCK_SOCKETS)
577 # define MHD_socket_fset_error_(err) (WSASetLastError ((err)))
578 #endif
579 
588 #define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
589  (MHD_socket_fset_error_ ((err)), ! 0) : \
590  0)
591 
597 #if defined(MHD_POSIX_SOCKETS)
598 # if defined(ENOSYS)
599 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
600  (errno = ENOSYS) : (errno = (err)) )
601 # elif defined(EOPNOTSUPP)
602 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
603  (errno = EOPNOTSUPP) : (errno = \
604  (err)) )
605 # elif defined (EFAULT)
606 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
607  (errno = EFAULT) : (errno = (err)) )
608 # elif defined (EINVAL)
609 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
610  (errno = EINVAL) : (errno = (err)) )
611 # else /* !EOPNOTSUPP && !EFAULT && !EINVAL */
612 # warning \
613  No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
614 # define MHD_socket_set_error_(err) (errno = (err))
615 # endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
616 #elif defined(MHD_WINSOCK_SOCKETS)
617 # define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
618  (WSASetLastError ((WSAEOPNOTSUPP))) : \
619  (WSASetLastError ((err))) )
620 #endif
621 
631 #define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
632  ((code) == (err)) )
633 
643 #define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_ (MHD_socket_get_error_ (), \
644  (code))
645 
646 /* Specific error code checks */
647 
654 #define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EINTR_)
655 
662 #if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
663 # define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EAGAIN_)
664 #else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
665 # define MHD_SCKT_ERR_IS_EAGAIN_(err) (MHD_SCKT_ERR_IS_ ((err), \
666  MHD_SCKT_EAGAIN_) || \
667  MHD_SCKT_ERR_IS_ ((err), \
668  MHD_SCKT_EWOULDBLOCK_) )
669 #endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
670 
676 #define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) (MHD_SCKT_ERR_IS_ ((err), \
677  MHD_SCKT_EMFILE_) \
678  || \
679  MHD_SCKT_ERR_IS_ ((err), \
680  MHD_SCKT_ENFILE_) \
681  || \
682  MHD_SCKT_ERR_IS_ ((err), \
683  MHD_SCKT_ENOMEM_) \
684  || \
685  MHD_SCKT_ERR_IS_ ((err), \
686  MHD_SCKT_ENOBUFS_) )
687 
694 #if defined(MHD_POSIX_SOCKETS)
695 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
696  MHD_SCKT_ECONNABORTED_)
697 #elif defined(MHD_WINSOCK_SOCKETS)
698 # define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
699  MHD_SCKT_ECONNRESET_)
700 #endif
701 
708 #define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) (MHD_SCKT_ERR_IS_ ((err), \
709  MHD_SCKT_ECONNRESET_) \
710  || \
711  MHD_SCKT_ERR_IS_ ((err), \
712  MHD_SCKT_ECONNABORTED_))
713 
714 /* Specific error code set */
715 
720 #if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
721 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
722  MHD_SCKT_ENOMEM_)
723 #elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
724 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
725  MHD_SCKT_ENOBUFS_)
726 #else
727 # warning \
728  No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
729 # define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
730  MHD_SCKT_ENOMEM_)
731 #endif
732 
733 /* Socket functions */
734 
735 #if defined(AF_LOCAL)
736 # define MHD_SCKT_LOCAL AF_LOCAL
737 #elif defined(AF_UNIX)
738 # define MHD_SCKT_LOCAL AF_UNIX
739 #endif /* AF_UNIX */
740 
741 #if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
742 # define MHD_socket_pair_(fdarr) (! socketpair (MHD_SCKT_LOCAL, SOCK_STREAM, 0, \
743  (fdarr)))
744 # if defined(HAVE_SOCK_NONBLOCK)
745 # define MHD_socket_pair_nblk_(fdarr) (! socketpair (MHD_SCKT_LOCAL, \
746  SOCK_STREAM \
747  | SOCK_NONBLOCK, 0, \
748  (fdarr)))
749 # endif /* HAVE_SOCK_NONBLOCK*/
750 #elif defined(MHD_WINSOCK_SOCKETS)
751 
758 int MHD_W32_socket_pair_ (SOCKET sockets_pair[2], int non_blk);
759 
760 # define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_ ((fdarr), 0)
761 # define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_ ((fdarr), 1)
762 #endif
763 
774 int
776  fd_set *set,
777  MHD_socket *max_fd,
778  unsigned int fd_setsize);
779 
780 
787 int
789 
790 
800 int
802  bool on);
803 
811 int
813 
814 
834 int
836  bool on);
837 
838 
845 int
847 
848 
849 #if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
850 static const int _MHD_socket_int_one = 1;
857 # define MHD_socket_nosignal_(sock) \
858  (! setsockopt ((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one, \
859  sizeof(_MHD_socket_int_one)))
860 #endif /* SOL_SOCKET && SO_NOSIGPIPE */
861 
870 
871 #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:377
additional automatic macros for MHD_config.h
int MHD_socket_nonblocking_(MHD_socket sock)
Definition: mhd_sockets.c:407
int MHD_socket_cork_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:506
int MHD_socket_buffering_reset_(MHD_socket sock)
Definition: mhd_sockets.c:553
int MHD_socket_noninheritable_(MHD_socket sock)
Definition: mhd_sockets.c:442
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:477
int fd
Definition: microhttpd.h:3157
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:474
limits values definitions