aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_sockets.h
blob: e22ff7e2cf6e73907add391205bbd9641f1093af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
  This file is part of libmicrohttpd
  Copyright (C) 2014-2016 Karlson2k (Evgeny Grin)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
 * @file microhttpd/mhd_sockets.c
 * @brief  Header for platform-independent sockets abstraction
 * @author Karlson2k (Evgeny Grin)
 *
 * Provides basic abstraction for sockets.
 * Any functions can be implemented as macro on some platforms
 * unless explicitly marked otherwise.
 * Any function argument can be skipped in macro, so avoid
 * variable modification in function parameters.
 */

#ifndef MHD_SOCKETS_H
#define MHD_SOCKETS_H 1
#include "mhd_options.h"

#include <errno.h>

#if !defined(MHD_POSIX_SOCKETS) && !defined(MHD_WINSOCK_SOCKETS)
#  if !defined(_WIN32) || defined(__CYGWIN__)
#    define MHD_POSIX_SOCKETS 1
#  else  /* defined(_WIN32) && !defined(__CYGWIN__) */
#    define MHD_WINSOCK_SOCKETS 1
#  endif /* defined(_WIN32) && !defined(__CYGWIN__) */
#endif /* !MHD_POSIX_SOCKETS && !MHD_WINSOCK_SOCKETS */

/*
 * MHD require headers that define socket type, socket basic functions
 * (socket(), accept(), listen(), bind(), send(), recv(), select()), socket
 * parameters like SOCK_CLOEXEC, SOCK_NONBLOCK, additional socket functions
 * (poll(), epoll(), accept4()), struct timeval and other types, required
 * for socket function.
 */
#if defined(MHD_POSIX_SOCKETS)
#  if HAVE_SYS_SOCKET_H
#    include <sys/socket.h>
#  endif
#  if defined(__VXWORKS__) || defined(__vxworks) || defined(OS_VXWORKS)
#    ifdef HAVE_SOCKLIB_H
#      include <sockLib.h>
#    endif /* HAVE_SOCKLIB_H */
#    ifdef HAVE_INETLIB_H
#      include <inetLib.h>
#    endif /* HAVE_INETLIB_H */
#    include <strings.h>  /* required for FD_SET (bzero() function) */
#  endif /* __VXWORKS__ || __vxworks || OS_VXWORKS */
#  ifdef HAVE_NETINET_IN_H
#    include <netinet/in.h>
#  endif /* HAVE_NETINET_IN_H */
#  if HAVE_ARPA_INET_H
#    include <arpa/inet.h>
#  endif
#  ifdef HAVE_NET_IF_H
#    include <net/if.h>
#  endif
#  if HAVE_SYS_TIME_H
#    include <sys/time.h>
#  endif
#  if HAVE_TIME_H
#    include <time.h>
#  endif
#  if HAVE_NETDB_H
#    include <netdb.h>
#  endif
#  if HAVE_SYS_SELECT_H
#    include <sys/select.h>
#  endif
#  if EPOLL_SUPPORT
#    include <sys/epoll.h>
#  endif
#  if HAVE_NETINET_TCP_H
     /* for TCP_FASTOPEN and TCP_CORK */
#    include <netinet/tcp.h>
#  endif
#  ifdef HAVE_STRING_H
#    include <string.h> /* for strerror() */
#  endif
#  if defined(HAVE_SYS_TYPES_H)
#    include <sys/types.h> /* required on old platforms */
#  endif /* (!HAVE_SYS_SOCKET_H || !HAVE_SYS_SOCKET_H) && HAVE_SYS_TYPES_H */
#elif defined(MHD_WINSOCK_SOCKETS)
#  ifndef WIN32_LEAN_AND_MEAN
#    define WIN32_LEAN_AND_MEAN 1
#  endif /* !WIN32_LEAN_AND_MEAN */
#  include <winsock2.h>
#  include <ws2tcpip.h>
#endif /* MHD_WINSOCK_SOCKETS */

#if defined(HAVE_POLL_H) && defined(HAVE_POLL)
#  include <poll.h>
#endif

#ifdef _MHD_FD_SETSIZE_IS_DEFAULT
#  define _MHD_SYS_DEFAULT_FD_SETSIZE FD_SETSIZE
#else  /* ! _MHD_FD_SETSIZE_IS_DEFAULT */
#  include "sysfdsetsize.h"
#  define _MHD_SYS_DEFAULT_FD_SETSIZE get_system_fdsetsize_value()
#endif /* ! _MHD_FD_SETSIZE_IS_DEFAULT */

#ifndef MHD_SOCKET_DEFINED
/**
 * MHD_socket is type for socket FDs
 */
#  if defined(MHD_POSIX_SOCKETS)
     typedef int MHD_socket;
#    define MHD_INVALID_SOCKET (-1)
#  elif defined(MHD_WINSOCK_SOCKETS)
     typedef SOCKET MHD_socket;
#    define MHD_INVALID_SOCKET (INVALID_SOCKET)
#  endif /* MHD_WINSOCK_SOCKETS */

#  define MHD_SOCKET_DEFINED 1
#endif /* ! MHD_SOCKET_DEFINED */

#ifdef SOCK_CLOEXEC
#  define MAYBE_SOCK_CLOEXEC SOCK_CLOEXEC
#else  /* ! SOCK_CLOEXEC */
#  define MAYBE_SOCK_CLOEXEC 0
#endif /* ! SOCK_CLOEXEC */

#ifdef HAVE_SOCK_NONBLOCK
#  define MAYBE_SOCK_NONBLOCK SOCK_NONBLOCK
#else  /* ! HAVE_SOCK_NONBLOCK */
#  define MAYBE_SOCK_NONBLOCK 0
#endif /* ! HAVE_SOCK_NONBLOCK */

#if !defined(SHUT_WR) && defined(SD_SEND)
#  define SHUT_WR SD_SEND
#endif
#if !defined(SHUT_RD) && defined(SD_RECEIVE)
#  define SHUT_RD SD_RECEIVE
#endif
#if !defined(SHUT_RDWR) && defined(SD_BOTH)
#  define SHUT_RDWR SD_BOTH
#endif

#if HAVE_ACCEPT4+0 != 0 && (defined(HAVE_SOCK_NONBLOCK) || defined(SOCK_CLOEXEC))
#  define USE_ACCEPT4 1
#endif

#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
#  define USE_EPOLL_CREATE1 1
#endif /* HAVE_EPOLL_CREATE1 && EPOLL_CLOEXEC */

#ifdef TCP_FASTOPEN
/**
 * Default TCP fastopen queue size.
 */
#define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
#endif


/**
 * MHD_SCKT_OPT_BOOL_ is type for bool parameters for setsockopt()/getsockopt()
 */
#ifdef MHD_POSIX_SOCKETS
  typedef int MHD_SCKT_OPT_BOOL_;
#else /* MHD_WINSOCK_SOCKETS */
  typedef BOOL MHD_SCKT_OPT_BOOL_;
#endif /* MHD_WINSOCK_SOCKETS */

/**
 * MHD_SCKT_SEND_SIZE_ is type used to specify size for send and recv
 * functions
 */
#if !defined(MHD_WINSOCK_SOCKETS)
  typedef size_t MHD_SCKT_SEND_SIZE_;
#else
  typedef int MHD_SCKT_SEND_SIZE_;
#endif

/**
 * MHD_socket_close_(fd) close any FDs (non-W32) / close only socket
 * FDs (W32).  Note that on HP-UNIX, this function may leak the FD if
 * errno is set to EINTR.  Do not use HP-UNIX.
 *
 * @param fd descriptor to close
 * @return 0 on success (error codes like EINTR and EIO are counted as success,
 *           only EBADF counts as an error!)
 */
#if !defined(MHD_WINSOCK_SOCKETS)
#  define MHD_socket_close_(fd) (((0 != close(fd)) && (EBADF == errno)) ? -1 : 0)
#else
#  define MHD_socket_close_(fd) closesocket((fd))
#endif

/**
 * Check whether FD can be added to fd_set with specified FD_SETSIZE.
 * @param fd   the fd to check
 * @param pset the pointer to fd_set to check or NULL to check
 *             whether FD can be used with fd_sets.
 * @param setsize the value of FD_SETSIZE.
 * @return boolean true if FD can be added to fd_set,
 *         boolean false otherwise.
 */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ((fd) < (setsize))
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_SCKT_FD_FITS_FDSET_SETSIZE_(fd,pset,setsize) ( ((void*)(pset)==(void*)0) || \
                                                             (((fd_set*)(pset))->fd_count < (setsize)) || \
                                                             (FD_ISSET((fd),(pset))) )
#endif

/**
 * Check whether FD can be added to fd_set with current FD_SETSIZE.
 * @param fd   the fd to check
 * @param pset the pointer to fd_set to check or NULL to check
 *             whether FD can be used with fd_sets.
 * @return boolean true if FD can be added to fd_set,
 *         boolean false otherwise.
 */
#define MHD_SCKT_FD_FITS_FDSET_(fd,pset) MHD_SCKT_FD_FITS_FDSET_SETSIZE_((fd),(pset),FD_SETSIZE)

/**
 * Add FD to fd_set with specified FD_SETSIZE.
 * @param fd   the fd to add
 * @param pset the valid pointer to fd_set.
 * @param setsize the value of FD_SETSIZE.
 * @note  To work on W32 with value of FD_SETSIZE different from currently defined value,
 *        system definition of FD_SET() is not used.
 */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize) FD_SET((fd),(pset))
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd,pset,setsize)                     \
        do {                                                                     \
           u_int _i_ = 0;                                                        \
           fd_set* const _s_ = (fd_set*)(pset);                                  \
           while((_i_ < _s_->fd_count) && ((fd) != _s_->fd_array[_i_])) {++_i_;} \
           if ((_i_ == _s_->fd_count)) {_s_->fd_array[_s_->fd_count++] = (fd);}  \
        } while(0)
#endif

 /* MHD_SYS_select_ is wrapper macro for system select() function */
#if !defined(MHD_WINSOCK_SOCKETS)
#  define MHD_SYS_select_(n,r,w,e,t) select((n),(r),(w),(e),(t))
#else
#  define MHD_SYS_select_(n,r,w,e,t) \
( ( (((void*)(r) == (void*)0) || ((fd_set*)(r))->fd_count == 0) &&  \
    (((void*)(w) == (void*)0) || ((fd_set*)(w))->fd_count == 0) &&  \
    (((void*)(e) == (void*)0) || ((fd_set*)(e))->fd_count == 0) ) ? \
  ( ((void*)(t) == (void*)0) ?                                      \
    (Sleep((t)->tv_sec * 1000 + (t)->tv_usec / 1000), 0) : 0 ) :    \
  (select((int)0,(r),(w),(e),(t))) )
#endif

#if defined(HAVE_POLL)
/* MHD_sys_poll_ is wrapper macro for system poll() function */
#  if !defined(MHD_WINSOCK_SOCKETS)
#    define MHD_sys_poll_ poll
#  else  /* MHD_WINSOCK_SOCKETS */
#    define MHD_sys_poll_ WSAPoll
#  endif /* MHD_WINSOCK_SOCKETS */
#endif /* HAVE_POLL */

#define MHD_SCKT_MISSING_ERR_CODE_ 31450

#if defined(MHD_POSIX_SOCKETS)
#  if defined(EAGAIN)
#    define MHD_SCKT_EAGAIN_      EAGAIN
#  elif defined(EWOULDBLOCK)
#    define MHD_SCKT_EAGAIN_      EWOULDBLOCK
#  else  /* !EAGAIN && !EWOULDBLOCK */
#    define MHD_SCKT_EAGAIN_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* !EAGAIN && !EWOULDBLOCK */
#  if defined(EWOULDBLOCK)
#    define MHD_SCKT_EWOULDBLOCK_ EWOULDBLOCK
#  elif defined(EAGAIN)
#    define MHD_SCKT_EWOULDBLOCK_ EAGAIN
#  else  /* !EWOULDBLOCK && !EAGAIN */
#    define MHD_SCKT_EWOULDBLOCK_ MHD_SCKT_MISSING_ERR_CODE_
#  endif /* !EWOULDBLOCK && !EAGAIN */
#  ifdef EINTR
#    define MHD_SCKT_EINTR_       EINTR
#  else  /* ! EINTR */
#    define MHD_SCKT_EINTR_       MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EINTR */
#  ifdef ECONNRESET
#    define MHD_SCKT_ECONNRESET_  ECONNRESET
#  else  /* ! ECONNRESET */
#    define MHD_SCKT_ECONNRESET_  MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ECONNRESET */
#  ifdef ECONNABORTED
#    define MHD_SCKT_ECONNABORTED_  ECONNABORTED
#  else  /* ! ECONNABORTED */
#    define MHD_SCKT_ECONNABORTED_  MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ECONNABORTED */
#  ifdef ENOTCONN
#    define MHD_SCKT_ENOTCONN_    ENOTCONN
#  else  /* ! ENOTCONN */
#    define MHD_SCKT_ENOTCONN_    MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOTCONN */
#  ifdef EMFILE
#    define MHD_SCKT_EMFILE_      EMFILE
#  else  /* ! EMFILE */
#    define MHD_SCKT_EMFILE_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EMFILE */
#  ifdef ENFILE
#    define MHD_SCKT_ENFILE_      ENFILE
#  else  /* ! ENFILE */
#    define MHD_SCKT_ENFILE_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENFILE */
#  ifdef ENOMEM
#    define MHD_SCKT_ENOMEM_      ENOMEM
#  else  /* ! ENOMEM */
#    define MHD_SCKT_ENOMEM_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOMEM */
#  ifdef ENOBUFS
#    define MHD_SCKT_ENOBUFS_     ENOBUFS
#  else  /* ! ENOBUFS */
#    define MHD_SCKT_ENOBUFS_     MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOBUFS */
#  ifdef EBADF
#    define MHD_SCKT_EBADF_       EBADF
#  else  /* ! EBADF */
#    define MHD_SCKT_EBADF_       MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EBADF */
#  ifdef ENOTSOCK
#    define MHD_SCKT_ENOTSOCK_    ENOTSOCK
#  else  /* ! ENOTSOCK */
#    define MHD_SCKT_ENOTSOCK_    MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOTSOCK */
#  ifdef EINVAL
#    define MHD_SCKT_EINVAL_      EINVAL
#  else  /* ! EINVAL */
#    define MHD_SCKT_EINVAL_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EINVAL */
#  ifdef EFAULT
#    define MHD_SCKT_EFAUL_       EFAULT
#  else  /* ! EFAULT */
#    define MHD_SCKT_EFAUL_       MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EFAULT */
#  ifdef ENOSYS
#    define MHD_SCKT_ENOSYS_      ENOSYS
#  else  /* ! ENOSYS */
#    define MHD_SCKT_ENOSYS_      MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOSYS */
#  ifdef ENOTSUP
#    define MHD_SCKT_ENOTSUP_     ENOTSUP
#  else  /* ! ENOTSUP */
#    define MHD_SCKT_ENOTSUP_     MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENOTSUP */
#  ifdef EOPNOTSUPP
#    define MHD_SCKT_EOPNOTSUPP_  EOPNOTSUPP
#  else  /* ! EOPNOTSUPP */
#    define MHD_SCKT_EOPNOTSUPP_  MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EOPNOTSUPP */
#  ifdef EACCES
#    define MHD_SCKT_EACCESS_     EACCES
#  else  /* ! EACCES */
#    define MHD_SCKT_EACCESS_     MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! EACCES */
#  ifdef ENETDOWN
#    define MHD_SCKT_ENETDOWN_    ENETDOWN
#  else  /* ! ENETDOWN */
#    define MHD_SCKT_ENETDOWN_    MHD_SCKT_MISSING_ERR_CODE_
#  endif /* ! ENETDOWN */
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_SCKT_EAGAIN_        WSAEWOULDBLOCK
#  define MHD_SCKT_EWOULDBLOCK_   WSAEWOULDBLOCK
#  define MHD_SCKT_EINTR_         WSAEINTR
#  define MHD_SCKT_ECONNRESET_    WSAECONNRESET
#  define MHD_SCKT_ECONNABORTED_  WSAECONNABORTED
#  define MHD_SCKT_ENOTCONN_      WSAENOTCONN
#  define MHD_SCKT_EMFILE_        WSAEMFILE
#  define MHD_SCKT_ENFILE_        MHD_SCKT_MISSING_ERR_CODE_
#  define MHD_SCKT_ENOMEM_        MHD_SCKT_MISSING_ERR_CODE_
#  define MHD_SCKT_ENOBUFS_       WSAENOBUFS
#  define MHD_SCKT_EBADF_         WSAEBADF
#  define MHD_SCKT_ENOTSOCK_      WSAENOTSOCK
#  define MHD_SCKT_EINVAL_        WSAEINVAL
#  define MHD_SCKT_EFAUL_         WSAEFAULT
#  define MHD_SCKT_ENOSYS_        MHD_SCKT_MISSING_ERR_CODE_
#  define MHD_SCKT_ENOTSUP_       MHD_SCKT_MISSING_ERR_CODE_
#  define MHD_SCKT_EOPNOTSUPP_    WSAEOPNOTSUPP
#  define MHD_SCKT_EACCESS_       WSAEACCES
#  define MHD_SCKT_ENETDOWN_      WSAENETDOWN
#endif

/**
 * MHD_socket_error_ return system native error code for last socket error.
 * @return system error code for last socket error.
 */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_socket_get_error_() (errno)
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_socket_get_error_() WSAGetLastError()
#endif

#ifdef MHD_WINSOCK_SOCKETS
  /* POSIX-W32 sockets compatibility functions */

/**
 * Return pointer to string description of specified WinSock error
 * @param err the WinSock error code.
 * @return pointer to string description of specified WinSock error.
 */
  const char* MHD_W32_strerror_winsock_(int err);
#endif /* MHD_WINSOCK_SOCKETS */

/* MHD_socket_last_strerr_ is description string of specified socket error code */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_socket_strerr_(err) strerror((err))
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_socket_strerr_(err) MHD_W32_strerror_winsock_((err))
#endif

/* MHD_socket_last_strerr_ is description string of last errno (non-W32) /
 *                            description string of last socket error (W32) */
#define MHD_socket_last_strerr_() MHD_socket_strerr_(MHD_socket_get_error_())

/**
 * MHD_socket_fset_error_() set socket system native error code.
 */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_socket_fset_error_(err) (errno = (err))
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_socket_fset_error_(err) (WSASetLastError((err)))
#endif

/**
 * MHD_socket_try_set_error_() set socket system native error code if
 * specified code is defined on system.
 * @return non-zero if specified @a err code is defined on system
 *         and error was set;
 *         zero if specified @a err code is not defined on system
 *         and error was not set.
 */
#define MHD_socket_try_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ != (err)) ? \
                                         (MHD_socket_fset_error_((err)), !0) : 0 )

/**
 * MHD_socket_set_error_() set socket system native error code to
 * specified code or replacement code if specified code is not
 * defined on system.
 */
#if defined(MHD_POSIX_SOCKETS)
#  if defined(ENOSYS)
#    define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
                                         (errno = ENOSYS) : (errno = (err)) )
#  elif defined(EOPNOTSUPP)
#    define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
                                         (errno = EOPNOTSUPP) : (errno = (err)) )
#  elif defined (EFAULT)
#    define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
                                         (errno = EFAULT) : (errno = (err)) )
#  elif defined (EINVAL)
#    define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
                                         (errno = EINVAL) : (errno = (err)) )
#  else  /* !EOPNOTSUPP && !EFAULT && !EINVAL */
#    warning No suitable replacement for missing socket error code is found. Edit this file and add replacement code which is defined on system.
#    define MHD_socket_set_error_(err) (errno = (err))
#  endif /* !EOPNOTSUPP && !EFAULT && !EINVAL*/
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_socket_set_error_(err) ( (MHD_SCKT_MISSING_ERR_CODE_ == (err)) ? \
                                       (WSASetLastError((WSAEOPNOTSUPP))) : \
                                       (WSASetLastError((err))) )
#endif

/**
 * Check whether given socket error is equal to specified system
 * native MHD_SCKT_E*_ code.
 * If platform don't have specific error code, result is
 * always boolean false.
 * @return boolean true if @a code is real error code and
 *         @a err equals to MHD_SCKT_E*_ @a code;
 *         boolean false otherwise
 */
#define MHD_SCKT_ERR_IS_(err,code) ( (MHD_SCKT_MISSING_ERR_CODE_ != (code)) && \
                                     ((code) == (err)) )

/**
 * Check whether last socket error is equal to specified system
 * native MHD_SCKT_E*_ code.
 * If platform don't have specific error code, result is
 * always boolean false.
 * @return boolean true if @a code is real error code and
 *         last socket error equals to MHD_SCKT_E*_ @a code;
 *         boolean false otherwise
 */
#define MHD_SCKT_LAST_ERR_IS_(code) MHD_SCKT_ERR_IS_(MHD_socket_get_error_() ,(code))

/* Specific error code checks */

/**
 * Check whether given socket error is equal to system's
 * socket error codes for EINTR.
 * @return boolean true if @a err is equal to sockets' EINTR code;
 *         boolean false otherwise.
 */
#define MHD_SCKT_ERR_IS_EINTR_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EINTR_)

/**
 * Check whether given socket error is equal to system's
 * socket error codes for EAGAIN or EWOULDBLOCK.
 * @return boolean true if @a err is equal to sockets' EAGAIN or EWOULDBLOCK codes;
 *         boolean false otherwise.
 */
#if MHD_SCKT_EAGAIN_ == MHD_SCKT_EWOULDBLOCK_
#  define MHD_SCKT_ERR_IS_EAGAIN_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_)
#else  /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */
#  define MHD_SCKT_ERR_IS_EAGAIN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EAGAIN_) || \
                                         MHD_SCKT_ERR_IS_((err),MHD_SCKT_EWOULDBLOCK_) )
#endif /* MHD_SCKT_EAGAIN_ != MHD_SCKT_EWOULDBLOCK_ */

/**
 * Check whether given socket error is any kind of "low resource" error.
 * @return boolean true if @a err is any kind of "low resource" error,
 *         boolean false otherwise.
 */
#define MHD_SCKT_ERR_IS_LOW_RESOURCES_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_EMFILE_) || \
                                              MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENFILE_) || \
                                              MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOMEM_) || \
                                              MHD_SCKT_ERR_IS_((err),MHD_SCKT_ENOBUFS_) )

/**
 * Check whether is given socket error is type of "incoming connection
 * was disconnected before 'accept()' is called".
 * @return boolean true is @a err match described socket error code,
 *         boolean false otherwise.
 */
#if defined(MHD_POSIX_SOCKETS)
#  define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_)
#elif defined(MHD_WINSOCK_SOCKETS)
#  define MHD_SCKT_ERR_IS_DISCNN_BEFORE_ACCEPT_(err) MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_)
#endif

/**
 * Check whether is given socket error is type of "connection was terminated
 * by remote side".
 * @return boolean true is @a err match described socket error code,
 *         boolean false otherwise.
 */
#define MHD_SCKT_ERR_IS_REMOTE_DISCNN_(err) ( MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNRESET_) || \
                                              MHD_SCKT_ERR_IS_((err),MHD_SCKT_ECONNABORTED_))

/* Specific error code set */

/**
 * Set socket's error code to ENOMEM or equivalent if ENOMEM is not
 * available on platform.
 */
#if MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOMEM_
#  define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
#elif MHD_SCKT_MISSING_ERR_CODE_ != MHD_SCKT_ENOBUFS_
#  define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOBUFS_)
#else
#  warning No suitable replacement for ENOMEM error codes is found. Edit this file and add replacement code which is defined on system.
#  define MHD_socket_set_error_to_ENOMEM() MHD_socket_set_error_(MHD_SCKT_ENOMEM_)
#endif

/* Socket functions */

/**
 * Add @a fd to the @a set.  If @a fd is
 * greater than @a max_fd, set @a max_fd to @a fd.
 *
 * @param fd file descriptor to add to the @a set
 * @param set set to modify
 * @param max_fd maximum value to potentially update
 * @param fd_setsize value of FD_SETSIZE
 * @return non-zero if succeeded, zero otherwise
 */
int
MHD_add_to_fd_set_ (MHD_socket fd,
                    fd_set *set,
                    MHD_socket *max_fd,
                    unsigned int fd_setsize);


/**
 * Change socket options to be non-blocking.
 *
 * @param sock socket to manipulate
 * @return non-zero if succeeded, zero otherwise
 */
int
MHD_socket_nonblocking_ (MHD_socket sock);


/**
 * Change socket options to be non-inheritable.
 *
 * @param sock socket to manipulate
 * @return non-zero if succeeded, zero otherwise
 * @warning Does not set socket error on W32.
 */
int
MHD_socket_noninheritable_ (MHD_socket sock);

#endif /* ! MHD_SOCKETS_H */