aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:32 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:13:32 +0000
commitf143bdcad91a1141ea6f7f771021724e38691fee (patch)
tree177f880a75eec27c47f0d5937c130639eb791e84
parent4cf082ba053867a7f9749f785627a1a80b2743a0 (diff)
downloadlibmicrohttpd-f143bdcad91a1141ea6f7f771021724e38691fee.tar.gz
libmicrohttpd-f143bdcad91a1141ea6f7f771021724e38691fee.zip
Moved create_listen_socket() to mhd_sockets.c, better error handling and checking on Darwin.
-rw-r--r--src/microhttpd/daemon.c55
-rw-r--r--src/microhttpd/mhd_sockets.c56
-rw-r--r--src/microhttpd/mhd_sockets.h10
3 files changed, 68 insertions, 53 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index cdd6aae6..7a8aa4ac 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -3414,52 +3414,6 @@ parse_options_va (struct MHD_Daemon *daemon,
3414} 3414}
3415 3415
3416 3416
3417/**
3418 * Create a listen socket, if possible with SOCK_CLOEXEC flag set.
3419 *
3420 * @param daemon daemon for which we create the socket
3421 * @param domain socket domain (i.e. PF_INET)
3422 * @param type socket type (usually SOCK_STREAM)
3423 * @param protocol desired protocol, 0 for default
3424 */
3425static MHD_socket
3426create_listen_socket (struct MHD_Daemon *daemon,
3427 int domain, int type, int protocol)
3428{
3429 MHD_socket fd;
3430 int cloexec_set;
3431#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
3432 static const int on_val = 1;
3433#endif
3434
3435 /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
3436 * implementations do not set ai_socktype, e.g. RHL6.2. */
3437#if defined(MHD_POSIX_SOCKETS) && defined(SOCK_CLOEXEC)
3438 fd = socket (domain, type | SOCK_CLOEXEC, protocol);
3439 cloexec_set = MHD_YES;
3440#elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT)
3441 fd = WSASocketW (domain, type, protocol, NULL, 0, WSA_FLAG_NO_HANDLE_INHERIT);
3442 cloexec_set = MHD_YES;
3443#else /* !SOCK_CLOEXEC */
3444 fd = socket (domain, type, protocol);
3445 cloexec_set = MHD_NO;
3446#endif /* !SOCK_CLOEXEC */
3447 if ( (MHD_INVALID_SOCKET == fd) && (MHD_NO != cloexec_set) )
3448 {
3449 fd = socket (domain, type, protocol);
3450 cloexec_set = MHD_NO;
3451 }
3452 if (MHD_INVALID_SOCKET == fd)
3453 return MHD_INVALID_SOCKET;
3454#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
3455 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on_val, sizeof(on_val));
3456#endif
3457 if (MHD_NO == cloexec_set)
3458 MHD_socket_noninheritable_ (fd);
3459 return fd;
3460}
3461
3462
3463#if EPOLL_SUPPORT 3417#if EPOLL_SUPPORT
3464/** 3418/**
3465 * Setup epoll() FD for the daemon and initialize it to listen 3419 * Setup epoll() FD for the daemon and initialize it to listen
@@ -3797,17 +3751,12 @@ MHD_start_daemon_va (unsigned int flags,
3797 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) ) 3751 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) )
3798 { 3752 {
3799 /* try to open listen socket */ 3753 /* try to open listen socket */
3800 if (0 != (flags & MHD_USE_IPv6)) 3754 socket_fd = MHD_socket_create_listen_(flags & MHD_USE_IPv6);
3801 socket_fd = create_listen_socket (daemon,
3802 PF_INET6, SOCK_STREAM, 0);
3803 else
3804 socket_fd = create_listen_socket (daemon,
3805 PF_INET, SOCK_STREAM, 0);
3806 if (MHD_INVALID_SOCKET == socket_fd) 3755 if (MHD_INVALID_SOCKET == socket_fd)
3807 { 3756 {
3808#ifdef HAVE_MESSAGES 3757#ifdef HAVE_MESSAGES
3809 MHD_DLOG (daemon, 3758 MHD_DLOG (daemon,
3810 "Call to socket failed: %s\n", 3759 "Failed to create socket for listening: %s\n",
3811 MHD_socket_last_strerr_ ()); 3760 MHD_socket_last_strerr_ ());
3812#endif 3761#endif
3813 goto free_and_fail; 3762 goto free_and_fail;
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c
index 3fda6138..64b45f9c 100644
--- a/src/microhttpd/mhd_sockets.c
+++ b/src/microhttpd/mhd_sockets.c
@@ -325,3 +325,59 @@ MHD_socket_noninheritable_ (MHD_socket sock)
325#endif /* MHD_WINSOCK_SOCKETS */ 325#endif /* MHD_WINSOCK_SOCKETS */
326 return !0; 326 return !0;
327} 327}
328
329
330/**
331 * Create a listen socket, with noninheritable flag if possible.
332 *
333 * @param use_ipv6 if set to non-zero IPv6 is used
334 * @return created socket or MHD_INVALID_SOCKET in case of errors
335 */
336MHD_socket
337MHD_socket_create_listen_ (int use_ipv6)
338{
339 int domain;
340 MHD_socket fd;
341 int cloexec_set;
342#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
343 static const int on_val = 1;
344#endif
345
346#ifdef HAVE_INET6
347 domain = (use_ipv6) ? PF_INET6 : PF_INET;
348#else /* ! HAVE_INET6 */
349 if (use_ipv6)
350 return MHD_INVALID_SOCKET;
351 domain = PF_INET;
352#endif /* ! HAVE_INET6 */
353
354#if defined(MHD_POSIX_SOCKETS) && defined(SOCK_CLOEXEC)
355 fd = socket (domain, SOCK_STREAM | SOCK_CLOEXEC, 0);
356 cloexec_set = !0;
357#elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT)
358 fd = WSASocketW (domain, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_NO_HANDLE_INHERIT);
359 cloexec_set = !0;
360#else /* !SOCK_CLOEXEC */
361 fd = MHD_INVALID_SOCKET;
362#endif /* !SOCK_CLOEXEC */
363 if (MHD_INVALID_SOCKET == fd)
364 {
365 fd = socket (domain, SOCK_STREAM, 0);
366 cloexec_set = 0;
367 }
368 if (MHD_INVALID_SOCKET == fd)
369 return MHD_INVALID_SOCKET;
370#if defined(OSX) && defined(SOL_SOCKET) && defined(SO_NOSIGPIPE)
371 if(0 != setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on_val, sizeof(on_val)))
372 {
373 int err = MHD_socket_get_error_();
374 MHD_socket_close_(fd);
375 MHD_socket_fset_error_(err);
376 return MHD_INVALID_SOCKET;
377 }
378#endif
379 if (!cloexec_set)
380 (void)MHD_socket_noninheritable_(fd);
381
382 return fd;
383}
diff --git a/src/microhttpd/mhd_sockets.h b/src/microhttpd/mhd_sockets.h
index e22ff7e2..0aa2aaac 100644
--- a/src/microhttpd/mhd_sockets.h
+++ b/src/microhttpd/mhd_sockets.h
@@ -608,4 +608,14 @@ MHD_socket_nonblocking_ (MHD_socket sock);
608int 608int
609MHD_socket_noninheritable_ (MHD_socket sock); 609MHD_socket_noninheritable_ (MHD_socket sock);
610 610
611
612/**
613 * Create a listen socket, with noninheritable flag if possible.
614 *
615 * @param use_ipv6 if set to non-zero IPv6 is used
616 * @return created socket or MHD_INVALID_SOCKET in case of errors
617 */
618MHD_socket
619MHD_socket_create_listen_ (int use_ipv6);
620
611#endif /* ! MHD_SOCKETS_H */ 621#endif /* ! MHD_SOCKETS_H */