GNU libmicrohttpd 0.9.71
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#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif /* HAVE_UNISTD_H */
42#include <fcntl.h>
43
44#if ! defined(MHD_POSIX_SOCKETS) && ! defined(MHD_WINSOCK_SOCKETS)
45# if ! defined(_WIN32) || defined(__CYGWIN__)
46# define MHD_POSIX_SOCKETS 1
47# else /* defined(_WIN32) && !defined(__CYGWIN__) */
48# define MHD_WINSOCK_SOCKETS 1
49# endif /* defined(_WIN32) && !defined(__CYGWIN__) */
50#endif /* !MHD_POSIX_SOCKETS && !MHD_WINSOCK_SOCKETS */
51
52/*
53 * MHD require headers that define socket type, socket basic functions
54 * (socket(), accept(), listen(), bind(), send(), recv(), select()), socket
55 * parameters like SOCK_CLOEXEC, SOCK_NONBLOCK, additional socket functions
56 * (poll(), epoll(), accept4()), struct timeval and other types, required
57 * for socket function.
58 */
59#if defined(MHD_POSIX_SOCKETS)
60# ifdef HAVE_SYS_TYPES_H
61# include <sys/types.h> /* required on old platforms */
62# endif
63# ifdef HAVE_SYS_SOCKET_H
64# include <sys/socket.h>
65# endif
66# if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
67# ifdef HAVE_SOCKLIB_H
68# include <sockLib.h>
69# endif /* HAVE_SOCKLIB_H */
70# ifdef HAVE_INETLIB_H
71# include <inetLib.h>
72# endif /* HAVE_INETLIB_H */
73# include <strings.h> /* required for FD_SET (bzero() function) */
74# endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
75# ifdef HAVE_NETINET_IN_H
76# include <netinet/in.h>
77# endif /* HAVE_NETINET_IN_H */
78# ifdef HAVE_ARPA_INET_H
79# include <arpa/inet.h>
80# endif
81# ifdef HAVE_NET_IF_H
82# include <net/if.h>
83# endif
84# ifdef HAVE_SYS_TIME_H
85# include <sys/time.h>
86# endif
87# ifdef HAVE_TIME_H
88# include <time.h>
89# endif
90# ifdef HAVE_NETDB_H
91# include <netdb.h>
92# endif
93# ifdef HAVE_SYS_SELECT_H
94# include <sys/select.h>
95# endif
96# ifdef EPOLL_SUPPORT
97# include <sys/epoll.h>
98# endif
99# ifdef HAVE_NETINET_TCP_H
100/* for TCP_FASTOPEN and TCP_CORK */
101# include <netinet/tcp.h>
102# endif
103# ifdef HAVE_STRING_H
104# include <string.h> /* for strerror() */
105# endif
106#elif defined(MHD_WINSOCK_SOCKETS)
107# ifndef WIN32_LEAN_AND_MEAN
108# define WIN32_LEAN_AND_MEAN 1
109# endif /* !WIN32_LEAN_AND_MEAN */
110# include <winsock2.h>
111# include <ws2tcpip.h>
112#endif /* MHD_WINSOCK_SOCKETS */
113
114#if defined(HAVE_POLL_H) && defined(HAVE_POLL)
115# include <poll.h>
116#endif
117
118#include <stddef.h>
119#if defined(_MSC_FULL_VER) && ! defined (_SSIZE_T_DEFINED)
120# include <stdint.h>
121# define _SSIZE_T_DEFINED
122typedef intptr_t ssize_t;
123#endif /* !_SSIZE_T_DEFINED */
124
125#include "mhd_limits.h"
126
127#ifdef _MHD_FD_SETSIZE_IS_DEFAULT
128# define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
129#else /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
130# include "sysfdsetsize.h"
131# define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value ()
132#endif /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
133
134#ifndef MHD_PANIC
135# include <stdio.h>
136# include <stdlib.h>
137/* Simple implementation of MHD_PANIC, to be used outside lib */
138# define MHD_PANIC(msg) do { fprintf (stderr, \
139 "Abnormal termination at %d line in file %s: %s\n", \
140 (int) __LINE__, __FILE__, msg); abort (); \
141} while (0)
142#endif /* ! MHD_PANIC */
143
144#ifndef MHD_SOCKET_DEFINED
148# if defined(MHD_POSIX_SOCKETS)
149typedef int MHD_socket;
150# define MHD_INVALID_SOCKET (-1)
151# elif defined(MHD_WINSOCK_SOCKETS)
152typedef SOCKET MHD_socket;
153# define MHD_INVALID_SOCKET (INVALID_SOCKET)
154# endif /* MHD_WINSOCK_SOCKETS */
155
156# define MHD_SOCKET_DEFINED 1
157#endif /* ! MHD_SOCKET_DEFINED */
158
159#ifdef SOCK_CLOEXEC
160# define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
161#else /* ! SOCK_CLOEXEC */
162# define MAYBE_SOCK_CLOEXEC 0
163#endif /* ! SOCK_CLOEXEC */
164
165#ifdef HAVE_SOCK_NONBLOCK
166# define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
167#else /* ! HAVE_SOCK_NONBLOCK */
168# define MAYBE_SOCK_NONBLOCK 0
169#endif /* ! HAVE_SOCK_NONBLOCK */
170
171#ifdef SOCK_NOSIGPIPE
172# define MAYBE_SOCK_NOSIGPIPE SOCK_NOSIGPIPE
173#else /* ! HAVE_SOCK_NONBLOCK */
174# define MAYBE_SOCK_NOSIGPIPE 0
175#endif /* ! HAVE_SOCK_NONBLOCK */
176
177#ifdef MSG_NOSIGNAL
178# define MAYBE_MSG_NOSIGNAL MSG_NOSIGNAL
179#else /* ! MSG_NOSIGNAL */
180# define MAYBE_MSG_NOSIGNAL 0
181#endif /* ! MSG_NOSIGNAL */
182
183#if ! defined(SHUT_WR) && defined(SD_SEND)
184# define SHUT_WR SD_SEND
185#endif
186#if ! defined(SHUT_RD) && defined(SD_RECEIVE)
187# define SHUT_RD SD_RECEIVE
188#endif
189#if ! defined(SHUT_RDWR) && defined(SD_BOTH)
190# define SHUT_RDWR SD_BOTH
191#endif
192
193#if HAVE_ACCEPT4 + 0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || \
194 defined(SOCK_CLOEXEC) || defined(SOCK_NOSIGPIPE))
195# define USE_ACCEPT4 1
196#endif
197
198#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
199# define USE_EPOLL_CREATE1 1
200#endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */
201
202#ifdef TCP_FASTOPEN
206#define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
207#endif
208
209
210#if defined(TCP_CORK)
214#define MHD_TCP_CORK_NOPUSH TCP_CORK
215#elif defined(TCP_NOPUSH)
219#define MHD_TCP_CORK_NOPUSH TCP_NOPUSH
220#endif /* TCP_NOPUSH */
221
222
226#ifdef MHD_POSIX_SOCKETS
228#else /* MHD_WINSOCK_SOCKETS */
229typedef BOOL MHD_SCKT_OPT_BOOL_;
230#endif /* MHD_WINSOCK_SOCKETS */
231
236#if ! defined(MHD_WINSOCK_SOCKETS)
237typedef size_t MHD_SCKT_SEND_SIZE_;
238#else
239typedef int MHD_SCKT_SEND_SIZE_;
240#endif
241
245#if ! defined(MHD_WINSOCK_SOCKETS)
246# define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX
247#else
248# define MHD_SCKT_SEND_MAX_SIZE_ INT_MAX
249#endif
250
261#if ! defined(MHD_WINSOCK_SOCKETS)
262# define MHD_socket_close_(fd) ((0 == close ((fd))) || (EBADF != errno))
263#else
264# define MHD_socket_close_(fd) (0 == closesocket ((fd)))
265#endif
266
272#define MHD_socket_close_chk_(fd) do { \
273 if (! MHD_socket_close_ (fd)) \
274 MHD_PANIC (_ ("Close socket failed.\n")); \
275} while (0)
276
277
285#define MHD_send_(s,b,l) \
286 ((ssize_t) send ((s),(const void*) (b),(MHD_SCKT_SEND_SIZE_) (l), \
287 MAYBE_MSG_NOSIGNAL))
288
289
297#define MHD_recv_(s,b,l) \
298 ((ssize_t) recv ((s),(void*) (b),(MHD_SCKT_SEND_SIZE_) (l), 0))
299
300
310#if defined(MHD_POSIX_SOCKETS)
311# define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < \
312 ((MHD_socket) \
313 setsize))
314#elif defined(MHD_WINSOCK_SOCKETS)
315# define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*) (pset)== \
316 (void*) 0) || \
317 (((fd_set*) (pset)) \
318 ->fd_count < \
319 ((unsigned) \
320 setsize)) || \
321 (FD_ISSET ((fd), \
322 (pset))) )
323#endif
324
333#define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_ ((fd), \
334 (pset), \
335 FD_SETSIZE)
336
345#if defined(MHD_POSIX_SOCKETS)
346# define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET ((fd), \
347 (pset))
348#elif defined(MHD_WINSOCK_SOCKETS)
349# define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) \
350 do { \
351 u_int _i_ = 0; \
352 fd_set*const _s_ = (fd_set*) (pset); \
353 while ((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array [_i_])) {++_i_;} \
354 if ((_i_ == _s_->fd_count)) {_s_->fd_array [_s_->fd_count ++] = (fd);} \
355 } while (0)
356#endif
357
358/* MHD_SYS_select_ is wrapper macro for system select() function */
359#if ! defined(MHD_WINSOCK_SOCKETS)
360# define MHD_SYS_select_(n,r,w,e,t) select ((n),(r),(w),(e),(t))
361#else
362# define MHD_SYS_select_(n,r,w,e,t) \
363 ( ( (((void*) (r) == (void*) 0) || ((fd_set*) (r))->fd_count == 0) && \
364 (((void*) (w) == (void*) 0) || ((fd_set*) (w))->fd_count == 0) && \
365 (((void*) (e) == (void*) 0) || ((fd_set*) (e))->fd_count == 0) ) ? \
366 ( ((void*) (t) == (void*) 0) ? 0 : \
367 (Sleep (((struct timeval*) (t))->tv_sec * 1000 \
368 + ((struct timeval*) (t))->tv_usec / 1000), 0) ) : \
369 (select ((int) 0,(r),(w),(e),(t))) )
370#endif
371
372#if defined(HAVE_POLL)
373/* MHD_sys_poll_ is wrapper macro for system poll() function */
374# if ! defined(MHD_WINSOCK_SOCKETS)
375# define MHD_sys_poll_ poll
376# else /* MHD_WINSOCK_SOCKETS */
377# define MHD_sys_poll_ WSAPoll
378# endif /* MHD_WINSOCK_SOCKETS */
379
380# ifdef POLLPRI
381# define MHD_POLLPRI_OR_ZERO POLLPRI
382# else /* ! POLLPRI */
383# define MHD_POLLPRI_OR_ZERO 0
384# endif /* ! POLLPRI */
385# ifdef POLLRDBAND
386# define MHD_POLLRDBAND_OR_ZERO POLLRDBAND
387# else /* ! POLLRDBAND */
388# define MHD_POLLRDBAND_OR_ZERO 0
389# endif /* ! POLLRDBAND */
390# ifdef POLLNVAL
391# define MHD_POLLNVAL_OR_ZERO POLLNVAL
392# else /* ! POLLNVAL */
393# define MHD_POLLNVAL_OR_ZERO 0
394# endif /* ! POLLNVAL */
395
396/* MHD_POLL_EVENTS_ERR_DISC is 'events' mask for errors and disconnect.
397 * Note: Out-of-band data is treated as error. */
398# if defined(_WIN32) && ! defined(__CYGWIN__)
399# define MHD_POLL_EVENTS_ERR_DISC POLLRDBAND
400# elif defined(__linux__)
401# define MHD_POLL_EVENTS_ERR_DISC POLLPRI
402# else /* ! __linux__ */
403# define MHD_POLL_EVENTS_ERR_DISC (MHD_POLLPRI_OR_ZERO \
404 | MHD_POLLRDBAND_OR_ZERO)
405# endif /* ! __linux__ */
406/* MHD_POLL_REVENTS_ERR_DISC is 'revents' mask for errors and disconnect.
407 * Note: Out-of-band data is treated as error. */
408# define MHD_POLL_REVENTS_ERR_DISC \
409 (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
410 | POLLERR | POLLHUP)
411/* MHD_POLL_REVENTS_ERRROR is 'revents' mask for errors.
412 * Note: Out-of-band data is treated as error. */
413# define MHD_POLL_REVENTS_ERRROR \
414 (MHD_POLLPRI_OR_ZERO | MHD_POLLRDBAND_OR_ZERO | MHD_POLLNVAL_OR_ZERO \
415 | POLLERR)
416#endif /* HAVE_POLL */
417
418#define MHD_SCKT_MISSING_ERR_CODE_ 31450
419
420#if defined(MHD_POSIX_SOCKETS)
421# if defined(EAGAIN)
422# define MHD_SCKT_EAGAIN_ EAGAIN
423# elif defined(EWOULDBLOCK)
424# define MHD_SCKT_EAGAIN_ EWOULDBLOCK
425# else /* !EAGAIN && !EWOULDBLOCK */
426# define MHD_SCKT_EAGAIN_ MHD_SCKT_MISSING_ERR_CODE_
427# endif /* !EAGAIN && !EWOULDBLOCK */
428# if defined(EWOULDBLOCK)
429# define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
430# elif defined(EAGAIN)
431# define MHD_SCKT_EWOULDBLOCK_ EAGAIN
432# else /* !EWOULDBLOCK && !EAGAIN */
433# define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
434# endif /* !EWOULDBLOCK && !EAGAIN */
435# ifdef EINTR
436# define MHD_SCKT_EINTR_ EINTR
437# else /* ! EINTR */
438# define MHD_SCKT_EINTR_ MHD_SCKT_MISSING_ERR_CODE_
439# endif /* ! EINTR */
440# ifdef ECONNRESET
441# define MHD_SCKT_ECONNRESET_ ECONNRESET
442# else /* ! ECONNRESET */
443# define MHD_SCKT_ECONNRESET_ MHD_SCKT_MISSING_ERR_CODE_
444# endif /* ! ECONNRESET */
445# ifdef ECONNABORTED
446# define MHD_SCKT_ECONNABORTED_ ECONNABORTED
447# else /* ! ECONNABORTED */
448# define MHD_SCKT_ECONNABORTED_ MHD_SCKT_MISSING_ERR_CODE_
449# endif /* ! ECONNABORTED */
450# ifdef ENOTCONN
451# define MHD_SCKT_ENOTCONN_ ENOTCONN
452# else /* ! ENOTCONN */
453# define MHD_SCKT_ENOTCONN_ MHD_SCKT_MISSING_ERR_CODE_
454# endif /* ! ENOTCONN */
455# ifdef EMFILE
456# define MHD_SCKT_EMFILE_ EMFILE
457# else /* ! EMFILE */
458# define MHD_SCKT_EMFILE_ MHD_SCKT_MISSING_ERR_CODE_
459# endif /* ! EMFILE */
460# ifdef ENFILE
461# define MHD_SCKT_ENFILE_ ENFILE
462# else /* ! ENFILE */
463# define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
464# endif /* ! ENFILE */
465# ifdef ENOMEM
466# define MHD_SCKT_ENOMEM_ ENOMEM
467# else /* ! ENOMEM */
468# define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
469# endif /* ! ENOMEM */
470# ifdef ENOBUFS
471# define MHD_SCKT_ENOBUFS_ ENOBUFS
472# else /* ! ENOBUFS */
473# define MHD_SCKT_ENOBUFS_ MHD_SCKT_MISSING_ERR_CODE_
474# endif /* ! ENOBUFS */
475# ifdef EBADF
476# define MHD_SCKT_EBADF_ EBADF
477# else /* ! EBADF */
478# define MHD_SCKT_EBADF_ MHD_SCKT_MISSING_ERR_CODE_
479# endif /* ! EBADF */
480# ifdef ENOTSOCK
481# define MHD_SCKT_ENOTSOCK_ ENOTSOCK
482# else /* ! ENOTSOCK */
483# define MHD_SCKT_ENOTSOCK_ MHD_SCKT_MISSING_ERR_CODE_
484# endif /* ! ENOTSOCK */
485# ifdef EINVAL
486# define MHD_SCKT_EINVAL_ EINVAL
487# else /* ! EINVAL */
488# define MHD_SCKT_EINVAL_ MHD_SCKT_MISSING_ERR_CODE_
489# endif /* ! EINVAL */
490# ifdef EFAULT
491# define MHD_SCKT_EFAUL_ EFAULT
492# else /* ! EFAULT */
493# define MHD_SCKT_EFAUL_ MHD_SCKT_MISSING_ERR_CODE_
494# endif /* ! EFAULT */
495# ifdef ENOSYS
496# define MHD_SCKT_ENOSYS_ ENOSYS
497# else /* ! ENOSYS */
498# define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
499# endif /* ! ENOSYS */
500# ifdef ENOTSUP
501# define MHD_SCKT_ENOTSUP_ ENOTSUP
502# else /* ! ENOTSUP */
503# define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
504# endif /* ! ENOTSUP */
505# ifdef EOPNOTSUPP
506# define MHD_SCKT_EOPNOTSUPP_ EOPNOTSUPP
507# else /* ! EOPNOTSUPP */
508# define MHD_SCKT_EOPNOTSUPP_ MHD_SCKT_MISSING_ERR_CODE_
509# endif /* ! EOPNOTSUPP */
510# ifdef EACCES
511# define MHD_SCKT_EACCESS_ EACCES
512# else /* ! EACCES */
513# define MHD_SCKT_EACCESS_ MHD_SCKT_MISSING_ERR_CODE_
514# endif /* ! EACCES */
515# ifdef ENETDOWN
516# define MHD_SCKT_ENETDOWN_ ENETDOWN
517# else /* ! ENETDOWN */
518# define MHD_SCKT_ENETDOWN_ MHD_SCKT_MISSING_ERR_CODE_
519# endif /* ! ENETDOWN */
520#elif defined(MHD_WINSOCK_SOCKETS)
521# define MHD_SCKT_EAGAIN_ WSAEWOULDBLOCK
522# define MHD_SCKT_EWOULDBLOCK_ WSAEWOULDBLOCK
523# define MHD_SCKT_EINTR_ WSAEINTR
524# define MHD_SCKT_ECONNRESET_ WSAECONNRESET
525# define MHD_SCKT_ECONNABORTED_ WSAECONNABORTED
526# define MHD_SCKT_ENOTCONN_ WSAENOTCONN
527# define MHD_SCKT_EMFILE_ WSAEMFILE
528# define MHD_SCKT_ENFILE_ MHD_SCKT_MISSING_ERR_CODE_
529# define MHD_SCKT_ENOMEM_ MHD_SCKT_MISSING_ERR_CODE_
530# define MHD_SCKT_ENOBUFS_ WSAENOBUFS
531# define MHD_SCKT_EBADF_ WSAEBADF
532# define MHD_SCKT_ENOTSOCK_ WSAENOTSOCK
533# define MHD_SCKT_EINVAL_ WSAEINVAL
534# define MHD_SCKT_EFAUL_ WSAEFAULT
535# define MHD_SCKT_ENOSYS_ MHD_SCKT_MISSING_ERR_CODE_
536# define MHD_SCKT_ENOTSUP_ MHD_SCKT_MISSING_ERR_CODE_
537# define MHD_SCKT_EOPNOTSUPP_ WSAEOPNOTSUPP
538# define MHD_SCKT_EACCESS_ WSAEACCES
539# define MHD_SCKT_ENETDOWN_ WSAENETDOWN
540#endif
541
546#if defined(MHD_POSIX_SOCKETS)
547# define MHD_socket_get_error_() (errno)
548#elif defined(MHD_WINSOCK_SOCKETS)
549# define MHD_socket_get_error_() WSAGetLastError ()
550#endif
551
552#ifdef MHD_WINSOCK_SOCKETS
553/* POSIX-W32 sockets compatibility functions */
554
560const char*MHD_W32_strerror_winsock_ (int err);
561
562#endif /* MHD_WINSOCK_SOCKETS */
563
564/* MHD_socket_last_strerr_ is description string of specified socket error code */
565#if defined(MHD_POSIX_SOCKETS)
566# define MHD_socket_strerr_(err) strerror ((err))
567#elif defined(MHD_WINSOCK_SOCKETS)
568# define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_ ((err))
569#endif
570
571/* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
572 * description string of last socket error (W32) */
573#define MHD_socket_last_strerr_() MHD_socket_strerr_ (MHD_socket_get_error_ ())
574
578#if defined(MHD_POSIX_SOCKETS)
579# define MHD_socket_fset_error_(err) (errno = (err))
580#elif defined(MHD_WINSOCK_SOCKETS)
581# define MHD_socket_fset_error_(err) (WSASetLastError ((err)))
582#endif
583
592#define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
593 (MHD_socket_fset_error_ ((err)), ! 0) : \
594 0)
595
601#if defined(MHD_POSIX_SOCKETS)
602# if defined(ENOSYS)
603# define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
604 (errno = ENOSYS) : (errno = (err)) )
605# elif defined(EOPNOTSUPP)
606# define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
607 (errno = EOPNOTSUPP) : (errno = \
608 (err)) )
609# elif defined (EFAULT)
610# define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
611 (errno = EFAULT) : (errno = (err)) )
612# elif defined (EINVAL)
613# define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
614 (errno = EINVAL) : (errno = (err)) )
615# else /* !EOPNOTSUPP && !EFAULT && !EINVAL */
616# warning \
617 No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
618# define MHD_socket_set_error_(err) (errno = (err))
619# endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
620#elif defined(MHD_WINSOCK_SOCKETS)
621# define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
622 (WSASetLastError ((WSAEOPNOTSUPP))) : \
623 (WSASetLastError ((err))) )
624#endif
625
635#define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
636 ((code) == (err)) )
637
647#define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_ (MHD_socket_get_error_ (), \
648 (code))
649
650/* Specific error code checks */
651
658#define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EINTR_)
659
666#if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
667# define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_ ((err),MHD_SCKT_EAGAIN_)
668#else /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
669# define MHD_SCKT_ERR_IS_EAGAIN_(err) (MHD_SCKT_ERR_IS_ ((err), \
670 MHD_SCKT_EAGAIN_) || \
671 MHD_SCKT_ERR_IS_ ((err), \
672 MHD_SCKT_EWOULDBLOCK_) )
673#endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
674
680#define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) (MHD_SCKT_ERR_IS_ ((err), \
681 MHD_SCKT_EMFILE_) \
682 || \
683 MHD_SCKT_ERR_IS_ ((err), \
684 MHD_SCKT_ENFILE_) \
685 || \
686 MHD_SCKT_ERR_IS_ ((err), \
687 MHD_SCKT_ENOMEM_) \
688 || \
689 MHD_SCKT_ERR_IS_ ((err), \
690 MHD_SCKT_ENOBUFS_) )
691
698#if defined(MHD_POSIX_SOCKETS)
699# define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
700 MHD_SCKT_ECONNABORTED_)
701#elif defined(MHD_WINSOCK_SOCKETS)
702# define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_ ((err), \
703 MHD_SCKT_ECONNRESET_)
704#endif
705
712#define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) (MHD_SCKT_ERR_IS_ ((err), \
713 MHD_SCKT_ECONNRESET_) \
714 || \
715 MHD_SCKT_ERR_IS_ ((err), \
716 MHD_SCKT_ECONNABORTED_))
717
718/* Specific error code set */
719
724#if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
725# define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
726 MHD_SCKT_ENOMEM_)
727#elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
728# define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
729 MHD_SCKT_ENOBUFS_)
730#else
731# warning \
732 No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
733# define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_ ( \
734 MHD_SCKT_ENOMEM_)
735#endif
736
737/* Socket functions */
738
739#if defined(AF_LOCAL)
740# define MHD_SCKT_LOCAL AF_LOCAL
741#elif defined(AF_UNIX)
742# define MHD_SCKT_LOCAL AF_UNIX
743#endif /* AF_UNIX */
744
745#if defined(MHD_POSIX_SOCKETS) && defined(MHD_SCKT_LOCAL)
746# define MHD_socket_pair_(fdarr) (! socketpair (MHD_SCKT_LOCAL, SOCK_STREAM, 0, \
747 (fdarr)))
748# if defined(HAVE_SOCK_NONBLOCK)
749# define MHD_socket_pair_nblk_(fdarr) (! socketpair (MHD_SCKT_LOCAL, \
750 SOCK_STREAM \
751 | SOCK_NONBLOCK, 0, \
752 (fdarr)))
753# endif /* HAVE_SOCK_NONBLOCK*/
754#elif defined(MHD_WINSOCK_SOCKETS)
762int MHD_W32_socket_pair_ (SOCKET sockets_pair[2], int non_blk);
763
764# define MHD_socket_pair_(fdarr) MHD_W32_socket_pair_ ((fdarr), 0)
765# define MHD_socket_pair_nblk_(fdarr) MHD_W32_socket_pair_ ((fdarr), 1)
766#endif
767
778int
780 fd_set *set,
781 MHD_socket *max_fd,
782 unsigned int fd_setsize);
783
784
791int
793
794
804int
806 bool on);
807
815int
817
818
838int
840 bool on);
841
842
849int
851
852
853#if defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
854static const int _MHD_socket_int_one = 1;
861#define MHD_socket_nosignal_(sock) \
862 (! setsockopt ((sock),SOL_SOCKET,SO_NOSIGPIPE,&_MHD_socket_int_one, \
863 sizeof(_MHD_socket_int_one)))
864#elif defined(MHD_POSIX_SOCKETS) && defined(SOCK_NOSIGPIPE) && \
865 defined(SOCK_CLOEXEC)
866#endif
867
876
877#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
int MHD_socket_cork_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:500
int MHD_socket_buffering_reset_(MHD_socket sock)
Definition: mhd_sockets.c:552
int MHD_socket_set_nodelay_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:471
Helper for obtaining FD_SETSIZE system default value.
int MHD_socket
Definition: microhttpd.h:195
int fd
Definition: microhttpd.h:3166