diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:12:56 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-23 20:12:56 +0000 |
commit | 558572ec73c212b87ea999b0c0d2ed65900d82c2 (patch) | |
tree | c914682046b51f504c39c7c6e8e14e9ea163fa73 | |
parent | 50bbf74d4e1a12f5272a732feb26912db2cff63d (diff) |
Moved pipe/socketpair to separate mhd_itc.h/.c files.
-rw-r--r-- | src/include/platform.h | 14 | ||||
-rw-r--r-- | src/include/platform_interface.h | 60 | ||||
-rw-r--r-- | src/include/w32functions.h | 7 | ||||
-rw-r--r-- | src/microhttpd/Makefile.am | 1 | ||||
-rw-r--r-- | src/microhttpd/daemon.c | 1 | ||||
-rw-r--r-- | src/microhttpd/internal.h | 1 | ||||
-rw-r--r-- | src/microhttpd/mhd_itc.c | 108 | ||||
-rw-r--r-- | src/microhttpd/mhd_itc.h | 118 | ||||
-rw-r--r-- | src/platform/w32functions.c | 81 | ||||
-rw-r--r-- | w32/common/libmicrohttpd-files.vcxproj | 2 | ||||
-rw-r--r-- | w32/common/libmicrohttpd-filters.vcxproj | 6 |
11 files changed, 237 insertions, 162 deletions
diff --git a/src/include/platform.h b/src/include/platform.h index e9c2fa12..6d51747d 100644 --- a/src/include/platform.h +++ b/src/include/platform.h @@ -124,20 +124,6 @@ typedef time_t _MHD_TIMEVAL_TV_SEC_TYPE; typedef long _MHD_TIMEVAL_TV_SEC_TYPE; #endif /* _WIN32 */ -/* TODO: remove include when pipes implementation is moved to other file */ -#include "../microhttpd/mhd_sockets.h" -/* Force don't use pipes on W32 */ -#if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) -#define MHD_DONT_USE_PIPES 1 -#endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ - -/* MHD_pipe is type for pipe FDs*/ -#ifndef MHD_DONT_USE_PIPES -typedef int MHD_pipe; -#else /* ! MHD_DONT_USE_PIPES */ -typedef MHD_socket MHD_pipe; -#endif /* ! MHD_DONT_USE_PIPES */ - #if !defined(IPPROTO_IPV6) && defined(_MSC_FULL_VER) && _WIN32_WINNT >= 0x0501 /* VC use IPPROTO_IPV6 as part of enum */ #define IPPROTO_IPV6 IPPROTO_IPV6 diff --git a/src/include/platform_interface.h b/src/include/platform_interface.h index fa84bcd5..b641a002 100644 --- a/src/include/platform_interface.h +++ b/src/include/platform_interface.h @@ -27,7 +27,6 @@ #define MHD_PLATFORM_INTERFACE_H #include "platform.h" -#include "../microhttpd/mhd_sockets.h" #if defined(_WIN32) && !defined(__CYGWIN__) #include "w32functions.h" #endif @@ -47,65 +46,6 @@ #endif /* ! _WIN32*/ #endif /* ! HAVE_SNPRINTF */ -/* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / - * create two connected sockets (MHD_DONT_USE_PIPES) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_(fdarr) pipe((fdarr)) -#else /* MHD_DONT_USE_PIPES */ -#if !defined(_WIN32) || defined(__CYGWIN__) -#define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr)) -#else /* !defined(_WIN32) || defined(__CYGWIN__) */ -#define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr)) -#endif /* !defined(_WIN32) || defined(__CYGWIN__) */ -#endif /* MHD_DONT_USE_PIPES */ - -/* MHD_pipe_errno_ is errno of last function (!MHD_DONT_USE_PIPES) / - * errno of last emulated pipe function (MHD_DONT_USE_PIPES) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_errno_ errno -#else -#define MHD_pipe_errno_ MHD_socket_errno_ -#endif - -/* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) / - * description string of last pipe error (MHD_DONT_USE_PIPES) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_last_strerror_() strerror(errno) -#else -#define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() -#endif - -/* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) / - * write data to emulated pipe (MHD_DONT_USE_PIPES) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz)) -#else -#define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0) -#endif - -/* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) / - * drain data from emulated pipe (MHD_DONT_USE_PIPES) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_drain_(fd) do { long tmp; while (0 < read((fd), (void*)&tmp, sizeof (tmp))) ; } while (0) -#else -#define MHD_pipe_drain_(fd) do { long tmp; while (0 < recv((fd), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) -#endif - -/* MHD_pipe_close_(fd) close any FDs (non-W32) / - * close emulated pipe FDs (W32) */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_pipe_close_(fd) close((fd)) -#else -#define MHD_pipe_close_(fd) MHD_socket_close_((fd)) -#endif - -/* MHD_INVALID_PIPE_ is a value of bad pipe FD */ -#ifndef MHD_DONT_USE_PIPES -#define MHD_INVALID_PIPE_ (-1) -#else -#define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET -#endif - #if !defined(_WIN32) || defined(__CYGWIN__) #define MHD_random_() random() #else diff --git a/src/include/w32functions.h b/src/include/w32functions.h index f8445f30..0694a381 100644 --- a/src/include/w32functions.h +++ b/src/include/w32functions.h @@ -40,13 +40,6 @@ extern "C" #endif /** - * Create pair of mutually connected TCP/IP sockets on loopback address - * @param sockets_pair array to receive resulted sockets - * @return zero on success, -1 otherwise - */ -int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); - -/** * Generate 31-bit pseudo random number. * Function initialize itself at first call to current time. * @return 31-bit pseudo random number. diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am index 371829a5..c72e183f 100644 --- a/src/microhttpd/Makefile.am +++ b/src/microhttpd/Makefile.am @@ -70,6 +70,7 @@ libmicrohttpd_la_SOURCES = \ mhd_threads.c mhd_threads.h \ mhd_locks.h \ mhd_sockets.c mhd_sockets.h \ + mhd_itc.c mhd_itc.h \ response.c response.h libmicrohttpd_la_CPPFLAGS = \ $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \ diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 3d0b5878..d5c00e57 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -35,6 +35,7 @@ #include "mhd_mono_clock.h" #include "mhd_locks.h" #include "mhd_sockets.h" +#include "mhd_itc.h" #if HAVE_SEARCH_H #include <search.h> diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h index 75aa1411..0f70026f 100644 --- a/src/microhttpd/internal.h +++ b/src/microhttpd/internal.h @@ -39,6 +39,7 @@ #include "mhd_threads.h" #include "mhd_locks.h" #include "mhd_sockets.h" +#include "mhd_itc.h" /** diff --git a/src/microhttpd/mhd_itc.c b/src/microhttpd/mhd_itc.c new file mode 100644 index 00000000..cd0e5371 --- /dev/null +++ b/src/microhttpd/mhd_itc.c @@ -0,0 +1,108 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 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 Implementation of inter-thread communication functions + * @author Karlson2k (Evgeny Grin) + */ + +#include "mhd_itc.h" + +#if defined(_WIN32) && !defined(__CYGWIN__) +/** + * Create pair of mutually connected TCP/IP sockets on loopback address + * @param sockets_pair array to receive resulted sockets + * @return zero on success, -1 otherwise + */ +int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]) +{ + int i; + if (!sockets_pair) + { + errno = EINVAL; + return -1; + } + +#define PAIRMAXTRYIES 800 + for (i = 0; i < PAIRMAXTRYIES; i++) + { + struct sockaddr_in listen_addr; + SOCKET listen_s; + static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ + int addr_len = c_addinlen; + int opt = 1; + + listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (INVALID_SOCKET == listen_s) + break; /* can't create even single socket */ + + listen_addr.sin_family = AF_INET; + listen_addr.sin_port = htons(0); + listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (0 == bind(listen_s, (struct sockaddr*) &listen_addr, c_addinlen) + && 0 == listen(listen_s, 1) + && 0 == getsockname(listen_s, (struct sockaddr*) &listen_addr, + &addr_len)) + { + SOCKET client_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (INVALID_SOCKET != client_s) + { + if (0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) + && (0 == connect(client_s, (struct sockaddr*) &listen_addr, c_addinlen) + || WSAGetLastError() == WSAEWOULDBLOCK)) + { + struct sockaddr_in accepted_from_addr; + SOCKET server_s; + addr_len = c_addinlen; + server_s = accept(listen_s, + (struct sockaddr*) &accepted_from_addr, &addr_len); + if (INVALID_SOCKET != server_s) + { + struct sockaddr_in client_addr; + addr_len = c_addinlen; + opt = 0; + if (0 == getsockname(client_s, (struct sockaddr*) &client_addr, &addr_len) + && accepted_from_addr.sin_family == client_addr.sin_family + && accepted_from_addr.sin_port == client_addr.sin_port + && accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr + && 0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) + && 0 == ioctlsocket(server_s, FIONBIO, (u_long*) &opt)) + { + closesocket(listen_s); + sockets_pair[0] = client_s; + sockets_pair[1] = server_s; + return 0; + } + closesocket(server_s); + } + } + closesocket(client_s); + } + } + closesocket(listen_s); + } + + sockets_pair[0] = INVALID_SOCKET; + sockets_pair[1] = INVALID_SOCKET; + return -1; +} + +#endif /* _WIN32 && ! __CYGWIN__ */ diff --git a/src/microhttpd/mhd_itc.h b/src/microhttpd/mhd_itc.h new file mode 100644 index 00000000..169bbb03 --- /dev/null +++ b/src/microhttpd/mhd_itc.h @@ -0,0 +1,118 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 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 inter-thread communication + * @author Karlson2k (Evgeny Grin) + * + * Provides basic abstraction for inter-thread communication. + * 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_ITC_H +#define MHD_ITC_H 1 +#include "mhd_options.h" + +/* Force don't use pipes on W32 */ +#if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) +#define MHD_DONT_USE_PIPES 1 +#endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ + +#ifndef MHD_DONT_USE_PIPES +# ifdef HAVE_STRING_H +# include <string.h> /* for strerror() */ +# endif +#else +# include "mhd_sockets.h" +#endif /* MHD_DONT_USE_PIPES */ + +/* MHD_pipe is type for pipe FDs*/ +#ifndef MHD_DONT_USE_PIPES + typedef int MHD_pipe; +#else /* ! MHD_DONT_USE_PIPES */ + typedef MHD_socket MHD_pipe; +#endif /* ! MHD_DONT_USE_PIPES */ + +/* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / + * create two connected sockets (MHD_DONT_USE_PIPES) */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_pipe_(fdarr) pipe((fdarr)) +#else /* MHD_DONT_USE_PIPES */ +# if !defined(_WIN32) || defined(__CYGWIN__) +# define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr)) +# else /* !defined(_WIN32) || defined(__CYGWIN__) */ +# define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr)) +# endif /* !defined(_WIN32) || defined(__CYGWIN__) */ +#endif /* MHD_DONT_USE_PIPES */ + +/* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) / + * description string of last pipe error (MHD_DONT_USE_PIPES) */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_pipe_last_strerror_() strerror(errno) +#else +# define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() +#endif + +/* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) / + * write data to emulated pipe (MHD_DONT_USE_PIPES) */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz)) +#else +# define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0) +#endif + +/* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) / + * drain data from emulated pipe (MHD_DONT_USE_PIPES) */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_pipe_drain_(fd) do { long tmp; while (0 < read((fd), (void*)&tmp, sizeof (tmp))) ; } while (0) +#else +# define MHD_pipe_drain_(fd) do { long tmp; while (0 < recv((fd), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) +#endif + +/* MHD_pipe_close_(fd) close any FDs (non-W32) / + * close emulated pipe FDs (W32) */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_pipe_close_(fd) close((fd)) +#else +# define MHD_pipe_close_(fd) MHD_socket_close_((fd)) +#endif + +/* MHD_INVALID_PIPE_ is a value of bad pipe FD */ +#ifndef MHD_DONT_USE_PIPES +# define MHD_INVALID_PIPE_ (-1) +#else +# define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET +#endif + +#if defined(_WIN32) && !defined(__CYGWIN__) +/** + * Create pair of mutually connected TCP/IP sockets on loopback address + * @param sockets_pair array to receive resulted sockets + * @return zero on success, -1 otherwise + */ +int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); +#endif /* _WIN32 && ! __CYGWIN__ */ + + +#endif /* MHD_ITC_H */ diff --git a/src/platform/w32functions.c b/src/platform/w32functions.c index 20d8e344..6fec7a3c 100644 --- a/src/platform/w32functions.c +++ b/src/platform/w32functions.c @@ -24,92 +24,11 @@ */ #include "w32functions.h" -#include <errno.h> -#include <winsock2.h> #include <string.h> #include <stdint.h> #include <time.h> #include <stdio.h> #include <stdarg.h> -#include "../microhttpd/mhd_sockets.h" - -/** - * Create pair of mutually connected TCP/IP sockets on loopback address - * @param sockets_pair array to receive resulted sockets - * @return zero on success, -1 otherwise - */ -int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]) -{ - int i; - if (!sockets_pair) - { - errno = EINVAL; - return -1; - } - -#define PAIRMAXTRYIES 800 - for (i = 0; i < PAIRMAXTRYIES; i++) - { - struct sockaddr_in listen_addr; - SOCKET listen_s; - static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ - int addr_len = c_addinlen; - int opt = 1; - - listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (INVALID_SOCKET == listen_s) - break; /* can't create even single socket */ - - listen_addr.sin_family = AF_INET; - listen_addr.sin_port = htons(0); - listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - if (0 == bind(listen_s, (struct sockaddr*) &listen_addr, c_addinlen) - && 0 == listen(listen_s, 1) - && 0 == getsockname(listen_s, (struct sockaddr*) &listen_addr, - &addr_len)) - { - SOCKET client_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (INVALID_SOCKET != client_s) - { - if (0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) - && (0 == connect(client_s, (struct sockaddr*) &listen_addr, c_addinlen) - || WSAGetLastError() == WSAEWOULDBLOCK)) - { - struct sockaddr_in accepted_from_addr; - SOCKET server_s; - addr_len = c_addinlen; - server_s = accept(listen_s, - (struct sockaddr*) &accepted_from_addr, &addr_len); - if (INVALID_SOCKET != server_s) - { - struct sockaddr_in client_addr; - addr_len = c_addinlen; - opt = 0; - if (0 == getsockname(client_s, (struct sockaddr*) &client_addr, &addr_len) - && accepted_from_addr.sin_family == client_addr.sin_family - && accepted_from_addr.sin_port == client_addr.sin_port - && accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr - && 0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) - && 0 == ioctlsocket(server_s, FIONBIO, (u_long*) &opt)) - { - closesocket(listen_s); - sockets_pair[0] = client_s; - sockets_pair[1] = server_s; - return 0; - } - closesocket(server_s); - } - } - closesocket(client_s); - } - } - closesocket(listen_s); - } - - sockets_pair[0] = INVALID_SOCKET; - sockets_pair[1] = INVALID_SOCKET; - return -1; -} /** * Static variable used by pseudo random number generator diff --git a/w32/common/libmicrohttpd-files.vcxproj b/w32/common/libmicrohttpd-files.vcxproj index 64c497b6..fa75b6ad 100644 --- a/w32/common/libmicrohttpd-files.vcxproj +++ b/w32/common/libmicrohttpd-files.vcxproj @@ -18,6 +18,7 @@ <ClCompile Include="$(MhdSrc)microhttpd\mhd_str.c" /> <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c" /> <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c" /> + <ClCompile Include="$(MhdSrc)microhttpd\mhd_itc.c" /> <ClCompile Include="$(MhdSrc)platform\w32functions.c" /> </ItemGroup> <ItemGroup> @@ -42,6 +43,7 @@ <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h" /> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h" /> <ClInclude Include="$(MhdW32Common)MHD_config.h" /> </ItemGroup> <ItemGroup> diff --git a/w32/common/libmicrohttpd-filters.vcxproj b/w32/common/libmicrohttpd-filters.vcxproj index 1ed587eb..11782d00 100644 --- a/w32/common/libmicrohttpd-filters.vcxproj +++ b/w32/common/libmicrohttpd-filters.vcxproj @@ -145,6 +145,12 @@ <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c"> <Filter>Source Files</Filter> </ClCompile> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h"> + <Filter>Source Files</Filter> + </ClInclude> + <ClCompile Include="$(MhdSrc)microhttpd\mhd_itc.c"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> |