aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/daemon.c
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2014-02-18 18:37:48 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2014-02-18 18:37:48 +0000
commitc311027f28797ef8dc692baa84a898bda033c410 (patch)
tree4296d1c6e4a083933553482789f1593f281c824e /src/microhttpd/daemon.c
parentcc5f66001e7227b7cba4046807c3b08e246bdfc7 (diff)
downloadlibmicrohttpd-c311027f28797ef8dc692baa84a898bda033c410.tar.gz
libmicrohttpd-c311027f28797ef8dc692baa84a898bda033c410.zip
Implement MHD_socket, MHD_INVALID_SOCKET
Diffstat (limited to 'src/microhttpd/daemon.c')
-rw-r--r--src/microhttpd/daemon.c151
1 files changed, 80 insertions, 71 deletions
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index 6da3fd88..c7ff9040 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -554,13 +554,13 @@ MHD_TLS_init (struct MHD_Daemon *daemon)
554 * @param max_fd maximum value to potentially update 554 * @param max_fd maximum value to potentially update
555 */ 555 */
556static void 556static void
557add_to_fd_set (int fd, 557add_to_fd_set (MHD_socket fd,
558 fd_set *set, 558 fd_set *set,
559 int *max_fd) 559 MHD_socket *max_fd)
560{ 560{
561 FD_SET (fd, set); 561 FD_SET (fd, set);
562 if ( (NULL != max_fd) && 562 if ( (NULL != max_fd) && (MHD_INVALID_SOCKET != fd) &&
563 (fd > *max_fd) ) 563 ((fd > *max_fd) || (MHD_INVALID_SOCKET == *max_fd)) )
564 *max_fd = fd; 564 *max_fd = fd;
565} 565}
566 566
@@ -587,10 +587,10 @@ MHD_get_fdset (struct MHD_Daemon *daemon,
587 fd_set *read_fd_set, 587 fd_set *read_fd_set,
588 fd_set *write_fd_set, 588 fd_set *write_fd_set,
589 fd_set *except_fd_set, 589 fd_set *except_fd_set,
590 int *max_fd) 590 MHD_socket *max_fd)
591{ 591{
592 struct MHD_Connection *pos; 592 struct MHD_Connection *pos;
593 int fd; 593 MHD_socket fd;
594 594
595 if ( (NULL == daemon) 595 if ( (NULL == daemon)
596 || (NULL == read_fd_set) 596 || (NULL == read_fd_set)
@@ -615,11 +615,12 @@ MHD_get_fdset (struct MHD_Daemon *daemon,
615 } 615 }
616#endif 616#endif
617 fd = daemon->socket_fd; 617 fd = daemon->socket_fd;
618 if (-1 != fd) 618 if (MHD_INVALID_SOCKET != fd)
619 { 619 {
620 FD_SET (fd, read_fd_set); 620 FD_SET (fd, read_fd_set);
621 /* update max file descriptor */ 621 /* update max file descriptor */
622 if ( (NULL != max_fd) && ((*max_fd) < fd)) 622 if ( (NULL != max_fd) &&
623 ((*max_fd) < fd || MHD_INVALID_SOCKET == (*max_fd)))
623 *max_fd = fd; 624 *max_fd = fd;
624 } 625 }
625 for (pos = daemon->connections_head; NULL != pos; pos = pos->next) 626 for (pos = daemon->connections_head; NULL != pos; pos = pos->next)
@@ -667,7 +668,7 @@ MHD_handle_connection (void *data)
667 int num_ready; 668 int num_ready;
668 fd_set rs; 669 fd_set rs;
669 fd_set ws; 670 fd_set ws;
670 int max; 671 MHD_socket max;
671 struct timeval tv; 672 struct timeval tv;
672 struct timeval *tvp; 673 struct timeval *tvp;
673 unsigned int timeout; 674 unsigned int timeout;
@@ -843,7 +844,7 @@ recv_param_adapter (struct MHD_Connection *connection,
843{ 844{
844 ssize_t ret; 845 ssize_t ret;
845 846
846 if ( (-1 == connection->socket_fd) || 847 if ( (MHD_INVALID_SOCKET == connection->socket_fd) ||
847 (MHD_CONNECTION_CLOSED == connection->state) ) 848 (MHD_CONNECTION_CLOSED == connection->state) )
848 { 849 {
849 errno = ENOTCONN; 850 errno = ENOTCONN;
@@ -876,12 +877,12 @@ send_param_adapter (struct MHD_Connection *connection,
876{ 877{
877 ssize_t ret; 878 ssize_t ret;
878#if LINUX 879#if LINUX
879 int fd; 880 MHD_socket fd;
880 off_t offset; 881 off_t offset;
881 off_t left; 882 off_t left;
882#endif 883#endif
883 884
884 if ( (-1 == connection->socket_fd) || 885 if ( (MHD_INVALID_SOCKET == connection->socket_fd) ||
885 (MHD_CONNECTION_CLOSED == connection->state) ) 886 (MHD_CONNECTION_CLOSED == connection->state) )
886 { 887 {
887 errno = ENOTCONN; 888 errno = ENOTCONN;
@@ -893,7 +894,7 @@ send_param_adapter (struct MHD_Connection *connection,
893 if ( (connection->write_buffer_append_offset == 894 if ( (connection->write_buffer_append_offset ==
894 connection->write_buffer_send_offset) && 895 connection->write_buffer_send_offset) &&
895 (NULL != connection->response) && 896 (NULL != connection->response) &&
896 (-1 != (fd = connection->response->fd)) ) 897 (MHD_INVALID_SOCKET != (fd = connection->response->fd)) )
897 { 898 {
898 /* can use sendfile */ 899 /* can use sendfile */
899 offset = (off_t) connection->response_write_position + connection->response->fd_off; 900 offset = (off_t) connection->response_write_position + connection->response->fd_off;
@@ -1027,7 +1028,7 @@ create_thread (pthread_t *thread,
1027 */ 1028 */
1028static int 1029static int
1029internal_add_connection (struct MHD_Daemon *daemon, 1030internal_add_connection (struct MHD_Daemon *daemon,
1030 int client_socket, 1031 MHD_socket client_socket,
1031 const struct sockaddr *addr, 1032 const struct sockaddr *addr,
1032 socklen_t addrlen, 1033 socklen_t addrlen,
1033 int external_add) 1034 int external_add)
@@ -1572,7 +1573,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
1572 */ 1573 */
1573static void 1574static void
1574make_nonblocking_noninheritable (struct MHD_Daemon *daemon, 1575make_nonblocking_noninheritable (struct MHD_Daemon *daemon,
1575 int sock) 1576 MHD_socket sock)
1576{ 1577{
1577#ifdef WINDOWS 1578#ifdef WINDOWS
1578 DWORD dwFlags; 1579 DWORD dwFlags;
@@ -1652,7 +1653,7 @@ make_nonblocking_noninheritable (struct MHD_Daemon *daemon,
1652 */ 1653 */
1653int 1654int
1654MHD_add_connection (struct MHD_Daemon *daemon, 1655MHD_add_connection (struct MHD_Daemon *daemon,
1655 int client_socket, 1656 MHD_socket client_socket,
1656 const struct sockaddr *addr, 1657 const struct sockaddr *addr,
1657 socklen_t addrlen) 1658 socklen_t addrlen)
1658{ 1659{
@@ -1687,13 +1688,13 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1687#endif 1688#endif
1688 struct sockaddr *addr = (struct sockaddr *) &addrstorage; 1689 struct sockaddr *addr = (struct sockaddr *) &addrstorage;
1689 socklen_t addrlen; 1690 socklen_t addrlen;
1690 int s; 1691 MHD_socket s;
1691 int fd; 1692 MHD_socket fd;
1692 int nonblock; 1693 int nonblock;
1693 1694
1694 addrlen = sizeof (addrstorage); 1695 addrlen = sizeof (addrstorage);
1695 memset (addr, 0, sizeof (addrstorage)); 1696 memset (addr, 0, sizeof (addrstorage));
1696 if (-1 == (fd = daemon->socket_fd)) 1697 if (MHD_INVALID_SOCKET == (fd = daemon->socket_fd))
1697 return MHD_NO; 1698 return MHD_NO;
1698#ifdef HAVE_SOCK_NONBLOCK 1699#ifdef HAVE_SOCK_NONBLOCK
1699 nonblock = SOCK_NONBLOCK; 1700 nonblock = SOCK_NONBLOCK;
@@ -1709,7 +1710,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1709#else 1710#else
1710 s = ACCEPT (fd, addr, &addrlen); 1711 s = ACCEPT (fd, addr, &addrlen);
1711#endif 1712#endif
1712 if ((-1 == s) || (addrlen <= 0)) 1713 if ((MHD_INVALID_SOCKET == s) || (addrlen <= 0))
1713 { 1714 {
1714#if HAVE_MESSAGES 1715#if HAVE_MESSAGES
1715 /* This could be a common occurance with multiple worker threads */ 1716 /* This could be a common occurance with multiple worker threads */
@@ -1718,7 +1719,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1718 "Error accepting connection: %s\n", 1719 "Error accepting connection: %s\n",
1719 STRERROR (errno)); 1720 STRERROR (errno));
1720#endif 1721#endif
1721 if (-1 != s) 1722 if (MHD_INVALID_SOCKET != s)
1722 { 1723 {
1723 if (0 != CLOSE (s)) 1724 if (0 != CLOSE (s))
1724 MHD_PANIC ("close failed\n"); 1725 MHD_PANIC ("close failed\n");
@@ -1788,7 +1789,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
1788 pos->epoll_state &= ~MHD_EPOLL_STATE_IN_EREADY_EDLL; 1789 pos->epoll_state &= ~MHD_EPOLL_STATE_IN_EREADY_EDLL;
1789 } 1790 }
1790 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && 1791 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) &&
1791 (-1 != daemon->epoll_fd) && 1792 (MHD_INVALID_SOCKET != daemon->epoll_fd) &&
1792 (0 != (pos->epoll_state & MHD_EPOLL_STATE_IN_EPOLL_SET)) ) 1793 (0 != (pos->epoll_state & MHD_EPOLL_STATE_IN_EPOLL_SET)) )
1793 { 1794 {
1794 /* epoll documentation suggests that closing a FD 1795 /* epoll documentation suggests that closing a FD
@@ -1810,7 +1811,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
1810 MHD_destroy_response (pos->response); 1811 MHD_destroy_response (pos->response);
1811 pos->response = NULL; 1812 pos->response = NULL;
1812 } 1813 }
1813 if (-1 != pos->socket_fd) 1814 if (MHD_INVALID_SOCKET != pos->socket_fd)
1814 { 1815 {
1815#ifdef WINDOWS 1816#ifdef WINDOWS
1816 SHUTDOWN (pos->socket_fd, SHUT_WR); 1817 SHUTDOWN (pos->socket_fd, SHUT_WR);
@@ -1939,7 +1940,7 @@ MHD_run_from_select (struct MHD_Daemon *daemon,
1939 const fd_set *write_fd_set, 1940 const fd_set *write_fd_set,
1940 const fd_set *except_fd_set) 1941 const fd_set *except_fd_set)
1941{ 1942{
1942 int ds; 1943 MHD_socket ds;
1943 char tmp; 1944 char tmp;
1944 struct MHD_Connection *pos; 1945 struct MHD_Connection *pos;
1945 struct MHD_Connection *next; 1946 struct MHD_Connection *next;
@@ -1958,7 +1959,7 @@ MHD_run_from_select (struct MHD_Daemon *daemon,
1958#endif 1959#endif
1959 1960
1960 /* select connection thread handling type */ 1961 /* select connection thread handling type */
1961 if ( (-1 != (ds = daemon->socket_fd)) && 1962 if ( (MHD_INVALID_SOCKET != (ds = daemon->socket_fd)) &&
1962 (FD_ISSET (ds, read_fd_set)) ) 1963 (FD_ISSET (ds, read_fd_set)) )
1963 (void) MHD_accept_connection (daemon); 1964 (void) MHD_accept_connection (daemon);
1964 /* drain signaling pipe to avoid spinning select */ 1965 /* drain signaling pipe to avoid spinning select */
@@ -1974,7 +1975,7 @@ MHD_run_from_select (struct MHD_Daemon *daemon,
1974 { 1975 {
1975 next = pos->next; 1976 next = pos->next;
1976 ds = pos->socket_fd; 1977 ds = pos->socket_fd;
1977 if (-1 == ds) 1978 if (MHD_INVALID_SOCKET == ds)
1978 continue; 1979 continue;
1979 switch (pos->event_loop_info) 1980 switch (pos->event_loop_info)
1980 { 1981 {
@@ -2026,7 +2027,7 @@ MHD_select (struct MHD_Daemon *daemon,
2026 fd_set rs; 2027 fd_set rs;
2027 fd_set ws; 2028 fd_set ws;
2028 fd_set es; 2029 fd_set es;
2029 int max; 2030 MHD_socket max;
2030 struct timeval timeout; 2031 struct timeval timeout;
2031 struct timeval *tv; 2032 struct timeval *tv;
2032 MHD_UNSIGNED_LONG_LONG ltimeout; 2033 MHD_UNSIGNED_LONG_LONG ltimeout;
@@ -2038,7 +2039,7 @@ MHD_select (struct MHD_Daemon *daemon,
2038 FD_ZERO (&rs); 2039 FD_ZERO (&rs);
2039 FD_ZERO (&ws); 2040 FD_ZERO (&ws);
2040 FD_ZERO (&es); 2041 FD_ZERO (&es);
2041 max = -1; 2042 max = MHD_INVALID_SOCKET;
2042 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2043 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2043 { 2044 {
2044 if (MHD_USE_SUSPEND_RESUME == (daemon->options & MHD_USE_SUSPEND_RESUME)) 2045 if (MHD_USE_SUSPEND_RESUME == (daemon->options & MHD_USE_SUSPEND_RESUME))
@@ -2051,13 +2052,13 @@ MHD_select (struct MHD_Daemon *daemon,
2051 /* If we're at the connection limit, no need to 2052 /* If we're at the connection limit, no need to
2052 accept new connections. */ 2053 accept new connections. */
2053 if ( (0 == daemon->max_connections) && 2054 if ( (0 == daemon->max_connections) &&
2054 (-1 != daemon->socket_fd) ) 2055 (MHD_INVALID_SOCKET != daemon->socket_fd) )
2055 FD_CLR (daemon->socket_fd, &rs); 2056 FD_CLR (daemon->socket_fd, &rs);
2056 } 2057 }
2057 else 2058 else
2058 { 2059 {
2059 /* accept only, have one thread per connection */ 2060 /* accept only, have one thread per connection */
2060 if (-1 != daemon->socket_fd) 2061 if (MHD_INVALID_SOCKET != daemon->socket_fd)
2061 { 2062 {
2062 max = daemon->socket_fd; 2063 max = daemon->socket_fd;
2063 FD_SET (daemon->socket_fd, &rs); 2064 FD_SET (daemon->socket_fd, &rs);
@@ -2067,7 +2068,7 @@ MHD_select (struct MHD_Daemon *daemon,
2067 { 2068 {
2068 FD_SET (daemon->wpipe[0], &rs); 2069 FD_SET (daemon->wpipe[0], &rs);
2069 /* update max file descriptor */ 2070 /* update max file descriptor */
2070 if (max < daemon->wpipe[0]) 2071 if (max < daemon->wpipe[0] || max == MHD_INVALID_SOCKET)
2071 max = daemon->wpipe[0]; 2072 max = daemon->wpipe[0];
2072 } 2073 }
2073 2074
@@ -2086,7 +2087,7 @@ MHD_select (struct MHD_Daemon *daemon,
2086 timeout.tv_sec = ltimeout / 1000; 2087 timeout.tv_sec = ltimeout / 1000;
2087 tv = &timeout; 2088 tv = &timeout;
2088 } 2089 }
2089 if (-1 == max) 2090 if (MHD_INVALID_SOCKET == max)
2090 return MHD_YES; 2091 return MHD_YES;
2091 num_ready = SELECT (max + 1, &rs, &ws, &es, tv); 2092 num_ready = SELECT (max + 1, &rs, &ws, &es, tv);
2092 if (MHD_YES == daemon->shutdown) 2093 if (MHD_YES == daemon->shutdown)
@@ -2139,7 +2140,7 @@ MHD_poll_all (struct MHD_Daemon *daemon,
2139 memset (p, 0, sizeof (p)); 2140 memset (p, 0, sizeof (p));
2140 poll_server = 0; 2141 poll_server = 0;
2141 poll_listen = -1; 2142 poll_listen = -1;
2142 if ( (-1 != daemon->socket_fd) && 2143 if ( (MHD_INVALID_SOCKET != daemon->socket_fd) &&
2143 (0 != daemon->max_connections) ) 2144 (0 != daemon->max_connections) )
2144 { 2145 {
2145 /* only listen if we are not at the connection limit */ 2146 /* only listen if we are not at the connection limit */
@@ -2275,7 +2276,7 @@ MHD_poll_listen_socket (struct MHD_Daemon *daemon,
2275 memset (&p, 0, sizeof (p)); 2276 memset (&p, 0, sizeof (p));
2276 poll_count = 0; 2277 poll_count = 0;
2277 poll_listen = -1; 2278 poll_listen = -1;
2278 if (-1 != daemon->socket_fd) 2279 if (MHD_INVALID_SOCKET != daemon->socket_fd)
2279 { 2280 {
2280 p[poll_count].fd = daemon->socket_fd; 2281 p[poll_count].fd = daemon->socket_fd;
2281 p[poll_count].events = POLLIN; 2282 p[poll_count].events = POLLIN;
@@ -2380,7 +2381,7 @@ MHD_epoll (struct MHD_Daemon *daemon,
2380 return MHD_NO; /* we're down! */ 2381 return MHD_NO; /* we're down! */
2381 if (MHD_YES == daemon->shutdown) 2382 if (MHD_YES == daemon->shutdown)
2382 return MHD_NO; 2383 return MHD_NO;
2383 if ( (-1 != daemon->socket_fd) && 2384 if ( (MHD_INVALID_SOCKET != daemon->socket_fd) &&
2384 (0 != daemon->max_connections) && 2385 (0 != daemon->max_connections) &&
2385 (MHD_NO == daemon->listen_socket_in_epoll) ) 2386 (MHD_NO == daemon->listen_socket_in_epoll) )
2386 { 2387 {
@@ -2675,22 +2676,22 @@ MHD_start_daemon (unsigned int flags,
2675 * Note that some thread modes require the caller to have passed 2676 * Note that some thread modes require the caller to have passed
2676 * #MHD_USE_PIPE_FOR_SHUTDOWN when using this API. If this daemon is 2677 * #MHD_USE_PIPE_FOR_SHUTDOWN when using this API. If this daemon is
2677 * in one of those modes and this option was not given to 2678 * in one of those modes and this option was not given to
2678 * #MHD_start_daemon, this function will return -1. 2679 * #MHD_start_daemon, this function will return #MHD_INVALID_SOCKET.
2679 * 2680 *
2680 * @param daemon daemon to stop accepting new connections for 2681 * @param daemon daemon to stop accepting new connections for
2681 * @return old listen socket on success, -1 if the daemon was 2682 * @return old listen socket on success, #MHD_INVALID_SOCKET if
2682 * already not listening anymore 2683 * the daemon was already not listening anymore
2683 * @ingroup specialized 2684 * @ingroup specialized
2684 */ 2685 */
2685int 2686MHD_socket
2686MHD_quiesce_daemon (struct MHD_Daemon *daemon) 2687MHD_quiesce_daemon (struct MHD_Daemon *daemon)
2687{ 2688{
2688 unsigned int i; 2689 unsigned int i;
2689 int ret; 2690 MHD_socket ret;
2690 2691
2691 ret = daemon->socket_fd; 2692 ret = daemon->socket_fd;
2692 if (-1 == ret) 2693 if (MHD_INVALID_SOCKET == ret)
2693 return -1; 2694 return MHD_INVALID_SOCKET;
2694 if ( (-1 == daemon->wpipe[1]) && 2695 if ( (-1 == daemon->wpipe[1]) &&
2695 (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) ) 2696 (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) )
2696 { 2697 {
@@ -2698,13 +2699,13 @@ MHD_quiesce_daemon (struct MHD_Daemon *daemon)
2698 MHD_DLOG (daemon, 2699 MHD_DLOG (daemon,
2699 "Using MHD_quiesce_daemon in this mode requires MHD_USE_PIPE_FOR_SHUTDOWN\n"); 2700 "Using MHD_quiesce_daemon in this mode requires MHD_USE_PIPE_FOR_SHUTDOWN\n");
2700#endif 2701#endif
2701 return -1; 2702 return MHD_INVALID_SOCKET;
2702 } 2703 }
2703 2704
2704 if (NULL != daemon->worker_pool) 2705 if (NULL != daemon->worker_pool)
2705 for (i = 0; i < daemon->worker_pool_size; i++) 2706 for (i = 0; i < daemon->worker_pool_size; i++)
2706 { 2707 {
2707 daemon->worker_pool[i].socket_fd = -1; 2708 daemon->worker_pool[i].socket_fd = MHD_INVALID_SOCKET;
2708#if EPOLL_SUPPORT 2709#if EPOLL_SUPPORT
2709 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && 2710 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) &&
2710 (-1 != daemon->worker_pool[i].epoll_fd) && 2711 (-1 != daemon->worker_pool[i].epoll_fd) &&
@@ -2719,7 +2720,7 @@ MHD_quiesce_daemon (struct MHD_Daemon *daemon)
2719 } 2720 }
2720#endif 2721#endif
2721 } 2722 }
2722 daemon->socket_fd = -1; 2723 daemon->socket_fd = MHD_INVALID_SOCKET;
2723#if EPOLL_SUPPORT 2724#if EPOLL_SUPPORT
2724 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && 2725 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) &&
2725 (-1 != daemon->epoll_fd) && 2726 (-1 != daemon->epoll_fd) &&
@@ -2928,7 +2929,7 @@ parse_options_va (struct MHD_Daemon *daemon,
2928 break; 2929 break;
2929#endif 2930#endif
2930 case MHD_OPTION_LISTEN_SOCKET: 2931 case MHD_OPTION_LISTEN_SOCKET:
2931 daemon->socket_fd = va_arg (ap, int); 2932 daemon->socket_fd = va_arg (ap, MHD_socket);
2932 break; 2933 break;
2933 case MHD_OPTION_EXTERNAL_LOGGER: 2934 case MHD_OPTION_EXTERNAL_LOGGER:
2934#if HAVE_MESSAGES 2935#if HAVE_MESSAGES
@@ -2974,9 +2975,8 @@ parse_options_va (struct MHD_Daemon *daemon,
2974 MHD_OPTION_END)) 2975 MHD_OPTION_END))
2975 return MHD_NO; 2976 return MHD_NO;
2976 break; 2977 break;
2977 /* all options taking 'int' or 'enum' */ 2978 /* all options taking 'enum' */
2978 case MHD_OPTION_HTTPS_CRED_TYPE: 2979 case MHD_OPTION_HTTPS_CRED_TYPE:
2979 case MHD_OPTION_LISTEN_SOCKET:
2980 if (MHD_YES != parse_options (daemon, 2980 if (MHD_YES != parse_options (daemon,
2981 servaddr, 2981 servaddr,
2982 opt, 2982 opt,
@@ -2984,6 +2984,15 @@ parse_options_va (struct MHD_Daemon *daemon,
2984 MHD_OPTION_END)) 2984 MHD_OPTION_END))
2985 return MHD_NO; 2985 return MHD_NO;
2986 break; 2986 break;
2987 /* all options taking 'MHD_socket' */
2988 case MHD_OPTION_LISTEN_SOCKET:
2989 if (MHD_YES != parse_options (daemon,
2990 servaddr,
2991 opt,
2992 (MHD_socket) oa[i].value,
2993 MHD_OPTION_END))
2994 return MHD_NO;
2995 break;
2987 /* all options taking one pointer */ 2996 /* all options taking one pointer */
2988 case MHD_OPTION_SOCK_ADDR: 2997 case MHD_OPTION_SOCK_ADDR:
2989 case MHD_OPTION_HTTPS_MEM_KEY: 2998 case MHD_OPTION_HTTPS_MEM_KEY:
@@ -3064,23 +3073,23 @@ parse_options_va (struct MHD_Daemon *daemon,
3064 * @param type socket type (usually SOCK_STREAM) 3073 * @param type socket type (usually SOCK_STREAM)
3065 * @param protocol desired protocol, 0 for default 3074 * @param protocol desired protocol, 0 for default
3066 */ 3075 */
3067static int 3076static MHD_socket
3068create_socket (struct MHD_Daemon *daemon, 3077create_socket (struct MHD_Daemon *daemon,
3069 int domain, int type, int protocol) 3078 int domain, int type, int protocol)
3070{ 3079{
3071 int ctype = type | SOCK_CLOEXEC; 3080 int ctype = type | SOCK_CLOEXEC;
3072 int fd; 3081 MHD_socket fd;
3073 3082
3074 /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo 3083 /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
3075 * implementations do not set ai_socktype, e.g. RHL6.2. */ 3084 * implementations do not set ai_socktype, e.g. RHL6.2. */
3076 fd = SOCKET (domain, ctype, protocol); 3085 fd = SOCKET (domain, ctype, protocol);
3077 if ( (-1 == fd) && (EINVAL == errno) && (0 != SOCK_CLOEXEC) ) 3086 if ( (MHD_INVALID_SOCKET == fd) && (EINVAL == errno) && (0 != SOCK_CLOEXEC) )
3078 { 3087 {
3079 ctype = type; 3088 ctype = type;
3080 fd = SOCKET(domain, type, protocol); 3089 fd = SOCKET(domain, type, protocol);
3081 } 3090 }
3082 if (-1 == fd) 3091 if (MHD_INVALID_SOCKET == fd)
3083 return -1; 3092 return MHD_INVALID_SOCKET;
3084 if (type == ctype) 3093 if (type == ctype)
3085 make_nonblocking_noninheritable (daemon, fd); 3094 make_nonblocking_noninheritable (daemon, fd);
3086 return fd; 3095 return fd;
@@ -3114,7 +3123,7 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
3114 if (0 == EPOLL_CLOEXEC) 3123 if (0 == EPOLL_CLOEXEC)
3115 make_nonblocking_noninheritable (daemon, 3124 make_nonblocking_noninheritable (daemon,
3116 daemon->epoll_fd); 3125 daemon->epoll_fd);
3117 if (-1 == daemon->socket_fd) 3126 if (MHD_INVALID_SOCKET == daemon->socket_fd)
3118 return MHD_YES; /* non-listening daemon */ 3127 return MHD_YES; /* non-listening daemon */
3119 event.events = EPOLLIN; 3128 event.events = EPOLLIN;
3120 event.data.ptr = daemon; 3129 event.data.ptr = daemon;
@@ -3184,7 +3193,7 @@ MHD_start_daemon_va (unsigned int flags,
3184{ 3193{
3185 const int on = 1; 3194 const int on = 1;
3186 struct MHD_Daemon *daemon; 3195 struct MHD_Daemon *daemon;
3187 int socket_fd; 3196 MHD_socket socket_fd;
3188 struct sockaddr_in servaddr4; 3197 struct sockaddr_in servaddr4;
3189#if HAVE_INET6 3198#if HAVE_INET6
3190 struct sockaddr_in6 servaddr6; 3199 struct sockaddr_in6 servaddr6;
@@ -3224,7 +3233,7 @@ MHD_start_daemon_va (unsigned int flags,
3224 NULL); 3233 NULL);
3225 } 3234 }
3226#endif 3235#endif
3227 daemon->socket_fd = -1; 3236 daemon->socket_fd = MHD_INVALID_SOCKET;
3228 daemon->options = (enum MHD_OPTION) flags; 3237 daemon->options = (enum MHD_OPTION) flags;
3229#if WINDOWS 3238#if WINDOWS
3230 /* Winsock is broken with respect to 'shutdown'; 3239 /* Winsock is broken with respect to 'shutdown';
@@ -3417,7 +3426,7 @@ MHD_start_daemon_va (unsigned int flags,
3417 goto free_and_fail; 3426 goto free_and_fail;
3418 } 3427 }
3419#endif 3428#endif
3420 if ( (-1 == daemon->socket_fd) && 3429 if ( (MHD_INVALID_SOCKET == daemon->socket_fd) &&
3421 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) ) 3430 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) )
3422 { 3431 {
3423 /* try to open listen socket */ 3432 /* try to open listen socket */
@@ -3427,7 +3436,7 @@ MHD_start_daemon_va (unsigned int flags,
3427 else 3436 else
3428 socket_fd = create_socket (daemon, 3437 socket_fd = create_socket (daemon,
3429 PF_INET, SOCK_STREAM, 0); 3438 PF_INET, SOCK_STREAM, 0);
3430 if (-1 == socket_fd) 3439 if (MHD_INVALID_SOCKET == socket_fd)
3431 { 3440 {
3432#if HAVE_MESSAGES 3441#if HAVE_MESSAGES
3433 if (0 != (flags & MHD_USE_DEBUG)) 3442 if (0 != (flags & MHD_USE_DEBUG))
@@ -3582,7 +3591,7 @@ MHD_start_daemon_va (unsigned int flags,
3582 MHD_DLOG (daemon, 3591 MHD_DLOG (daemon,
3583 "MHD failed to initialize IP connection limit mutex\n"); 3592 "MHD failed to initialize IP connection limit mutex\n");
3584#endif 3593#endif
3585 if ( (-1 != socket_fd) && 3594 if ( (MHD_INVALID_SOCKET != socket_fd) &&
3586 (0 != CLOSE (socket_fd)) ) 3595 (0 != CLOSE (socket_fd)) )
3587 MHD_PANIC ("close failed\n"); 3596 MHD_PANIC ("close failed\n");
3588 goto free_and_fail; 3597 goto free_and_fail;
@@ -3594,7 +3603,7 @@ MHD_start_daemon_va (unsigned int flags,
3594 "MHD failed to initialize IP connection limit mutex\n"); 3603 "MHD failed to initialize IP connection limit mutex\n");
3595#endif 3604#endif
3596 pthread_mutex_destroy (&daemon->cleanup_connection_mutex); 3605 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
3597 if ( (-1 != socket_fd) && 3606 if ( (MHD_INVALID_SOCKET != socket_fd) &&
3598 (0 != CLOSE (socket_fd)) ) 3607 (0 != CLOSE (socket_fd)) )
3599 MHD_PANIC ("close failed\n"); 3608 MHD_PANIC ("close failed\n");
3600 goto free_and_fail; 3609 goto free_and_fail;
@@ -3608,7 +3617,7 @@ MHD_start_daemon_va (unsigned int flags,
3608 MHD_DLOG (daemon, 3617 MHD_DLOG (daemon,
3609 "Failed to initialize TLS support\n"); 3618 "Failed to initialize TLS support\n");
3610#endif 3619#endif
3611 if ( (-1 != socket_fd) && 3620 if ( (MHD_INVALID_SOCKET != socket_fd) &&
3612 (0 != CLOSE (socket_fd)) ) 3621 (0 != CLOSE (socket_fd)) )
3613 MHD_PANIC ("close failed\n"); 3622 MHD_PANIC ("close failed\n");
3614 pthread_mutex_destroy (&daemon->cleanup_connection_mutex); 3623 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
@@ -3630,7 +3639,7 @@ MHD_start_daemon_va (unsigned int flags,
3630#endif 3639#endif
3631 pthread_mutex_destroy (&daemon->cleanup_connection_mutex); 3640 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
3632 pthread_mutex_destroy (&daemon->per_ip_connection_mutex); 3641 pthread_mutex_destroy (&daemon->per_ip_connection_mutex);
3633 if ( (-1 != socket_fd) && 3642 if ( (MHD_INVALID_SOCKET != socket_fd) &&
3634 (0 != CLOSE (socket_fd)) ) 3643 (0 != CLOSE (socket_fd)) )
3635 MHD_PANIC ("close failed\n"); 3644 MHD_PANIC ("close failed\n");
3636 goto free_and_fail; 3645 goto free_and_fail;
@@ -3773,7 +3782,7 @@ thread_failed:
3773 MHD_USE_SELECT_INTERNALLY mode. */ 3782 MHD_USE_SELECT_INTERNALLY mode. */
3774 if (0 == i) 3783 if (0 == i)
3775 { 3784 {
3776 if ( (-1 != socket_fd) && 3785 if ( (MHD_INVALID_SOCKET != socket_fd) &&
3777 (0 != CLOSE (socket_fd)) ) 3786 (0 != CLOSE (socket_fd)) )
3778 MHD_PANIC ("close failed\n"); 3787 MHD_PANIC ("close failed\n");
3779 pthread_mutex_destroy (&daemon->cleanup_connection_mutex); 3788 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
@@ -3923,7 +3932,7 @@ void
3923MHD_stop_daemon (struct MHD_Daemon *daemon) 3932MHD_stop_daemon (struct MHD_Daemon *daemon)
3924{ 3933{
3925 void *unused; 3934 void *unused;
3926 int fd; 3935 MHD_socket fd;
3927 unsigned int i; 3936 unsigned int i;
3928 int rc; 3937 int rc;
3929 3938
@@ -3931,7 +3940,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
3931 return; 3940 return;
3932 daemon->shutdown = MHD_YES; 3941 daemon->shutdown = MHD_YES;
3933 fd = daemon->socket_fd; 3942 fd = daemon->socket_fd;
3934 daemon->socket_fd = -1; 3943 daemon->socket_fd = MHD_INVALID_SOCKET;
3935 /* Prepare workers for shutdown */ 3944 /* Prepare workers for shutdown */
3936 if (NULL != daemon->worker_pool) 3945 if (NULL != daemon->worker_pool)
3937 { 3946 {
@@ -3939,11 +3948,11 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
3939 for (i = 0; i < daemon->worker_pool_size; ++i) 3948 for (i = 0; i < daemon->worker_pool_size; ++i)
3940 { 3949 {
3941 daemon->worker_pool[i].shutdown = MHD_YES; 3950 daemon->worker_pool[i].shutdown = MHD_YES;
3942 daemon->worker_pool[i].socket_fd = -1; 3951 daemon->worker_pool[i].socket_fd = MHD_INVALID_SOCKET;
3943#if EPOLL_SUPPORT 3952#if EPOLL_SUPPORT
3944 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && 3953 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) &&
3945 (-1 != daemon->worker_pool[i].epoll_fd) && 3954 (-1 != daemon->worker_pool[i].epoll_fd) &&
3946 (-1 == fd) ) 3955 (MHD_INVALID_SOCKET == fd) )
3947 epoll_shutdown (&daemon->worker_pool[i]); 3956 epoll_shutdown (&daemon->worker_pool[i]);
3948#endif 3957#endif
3949 } 3958 }
@@ -3956,15 +3965,15 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
3956#ifdef HAVE_LISTEN_SHUTDOWN 3965#ifdef HAVE_LISTEN_SHUTDOWN
3957 else 3966 else
3958 { 3967 {
3959 /* fd might be -1 here due to 'MHD_quiesce_daemon' */ 3968 /* fd might be MHD_INVALID_SOCKET here due to 'MHD_quiesce_daemon' */
3960 if (-1 != fd) 3969 if (MHD_INVALID_SOCKET != fd)
3961 (void) SHUTDOWN (fd, SHUT_RDWR); 3970 (void) SHUTDOWN (fd, SHUT_RDWR);
3962 } 3971 }
3963#endif 3972#endif
3964#if EPOLL_SUPPORT 3973#if EPOLL_SUPPORT
3965 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) && 3974 if ( (0 != (daemon->options & MHD_USE_EPOLL_LINUX_ONLY)) &&
3966 (-1 != daemon->epoll_fd) && 3975 (-1 != daemon->epoll_fd) &&
3967 (-1 == fd) ) 3976 (MHD_INVALID_SOCKET == fd) )
3968 epoll_shutdown (daemon); 3977 epoll_shutdown (daemon);
3969#endif 3978#endif
3970 3979
@@ -4022,7 +4031,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
4022 } 4031 }
4023 } 4032 }
4024 close_all_connections (daemon); 4033 close_all_connections (daemon);
4025 if ( (-1 != fd) && 4034 if ( (MHD_INVALID_SOCKET != fd) &&
4026 (0 != CLOSE (fd)) ) 4035 (0 != CLOSE (fd)) )
4027 MHD_PANIC ("close failed\n"); 4036 MHD_PANIC ("close failed\n");
4028 4037