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) | |
download | libmicrohttpd-558572ec73c212b87ea999b0c0d2ed65900d82c2.tar.gz libmicrohttpd-558572ec73c212b87ea999b0c0d2ed65900d82c2.zip |
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; | |||
124 | typedef long _MHD_TIMEVAL_TV_SEC_TYPE; | 124 | typedef long _MHD_TIMEVAL_TV_SEC_TYPE; |
125 | #endif /* _WIN32 */ | 125 | #endif /* _WIN32 */ |
126 | 126 | ||
127 | /* TODO: remove include when pipes implementation is moved to other file */ | ||
128 | #include "../microhttpd/mhd_sockets.h" | ||
129 | /* Force don't use pipes on W32 */ | ||
130 | #if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) | ||
131 | #define MHD_DONT_USE_PIPES 1 | ||
132 | #endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ | ||
133 | |||
134 | /* MHD_pipe is type for pipe FDs*/ | ||
135 | #ifndef MHD_DONT_USE_PIPES | ||
136 | typedef int MHD_pipe; | ||
137 | #else /* ! MHD_DONT_USE_PIPES */ | ||
138 | typedef MHD_socket MHD_pipe; | ||
139 | #endif /* ! MHD_DONT_USE_PIPES */ | ||
140 | |||
141 | #if !defined(IPPROTO_IPV6) && defined(_MSC_FULL_VER) && _WIN32_WINNT >= 0x0501 | 127 | #if !defined(IPPROTO_IPV6) && defined(_MSC_FULL_VER) && _WIN32_WINNT >= 0x0501 |
142 | /* VC use IPPROTO_IPV6 as part of enum */ | 128 | /* VC use IPPROTO_IPV6 as part of enum */ |
143 | #define IPPROTO_IPV6 IPPROTO_IPV6 | 129 | #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 @@ | |||
27 | #define MHD_PLATFORM_INTERFACE_H | 27 | #define MHD_PLATFORM_INTERFACE_H |
28 | 28 | ||
29 | #include "platform.h" | 29 | #include "platform.h" |
30 | #include "../microhttpd/mhd_sockets.h" | ||
31 | #if defined(_WIN32) && !defined(__CYGWIN__) | 30 | #if defined(_WIN32) && !defined(__CYGWIN__) |
32 | #include "w32functions.h" | 31 | #include "w32functions.h" |
33 | #endif | 32 | #endif |
@@ -47,65 +46,6 @@ | |||
47 | #endif /* ! _WIN32*/ | 46 | #endif /* ! _WIN32*/ |
48 | #endif /* ! HAVE_SNPRINTF */ | 47 | #endif /* ! HAVE_SNPRINTF */ |
49 | 48 | ||
50 | /* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / | ||
51 | * create two connected sockets (MHD_DONT_USE_PIPES) */ | ||
52 | #ifndef MHD_DONT_USE_PIPES | ||
53 | #define MHD_pipe_(fdarr) pipe((fdarr)) | ||
54 | #else /* MHD_DONT_USE_PIPES */ | ||
55 | #if !defined(_WIN32) || defined(__CYGWIN__) | ||
56 | #define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr)) | ||
57 | #else /* !defined(_WIN32) || defined(__CYGWIN__) */ | ||
58 | #define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr)) | ||
59 | #endif /* !defined(_WIN32) || defined(__CYGWIN__) */ | ||
60 | #endif /* MHD_DONT_USE_PIPES */ | ||
61 | |||
62 | /* MHD_pipe_errno_ is errno of last function (!MHD_DONT_USE_PIPES) / | ||
63 | * errno of last emulated pipe function (MHD_DONT_USE_PIPES) */ | ||
64 | #ifndef MHD_DONT_USE_PIPES | ||
65 | #define MHD_pipe_errno_ errno | ||
66 | #else | ||
67 | #define MHD_pipe_errno_ MHD_socket_errno_ | ||
68 | #endif | ||
69 | |||
70 | /* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) / | ||
71 | * description string of last pipe error (MHD_DONT_USE_PIPES) */ | ||
72 | #ifndef MHD_DONT_USE_PIPES | ||
73 | #define MHD_pipe_last_strerror_() strerror(errno) | ||
74 | #else | ||
75 | #define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() | ||
76 | #endif | ||
77 | |||
78 | /* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) / | ||
79 | * write data to emulated pipe (MHD_DONT_USE_PIPES) */ | ||
80 | #ifndef MHD_DONT_USE_PIPES | ||
81 | #define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz)) | ||
82 | #else | ||
83 | #define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0) | ||
84 | #endif | ||
85 | |||
86 | /* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) / | ||
87 | * drain data from emulated pipe (MHD_DONT_USE_PIPES) */ | ||
88 | #ifndef MHD_DONT_USE_PIPES | ||
89 | #define MHD_pipe_drain_(fd) do { long tmp; while (0 < read((fd), (void*)&tmp, sizeof (tmp))) ; } while (0) | ||
90 | #else | ||
91 | #define MHD_pipe_drain_(fd) do { long tmp; while (0 < recv((fd), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) | ||
92 | #endif | ||
93 | |||
94 | /* MHD_pipe_close_(fd) close any FDs (non-W32) / | ||
95 | * close emulated pipe FDs (W32) */ | ||
96 | #ifndef MHD_DONT_USE_PIPES | ||
97 | #define MHD_pipe_close_(fd) close((fd)) | ||
98 | #else | ||
99 | #define MHD_pipe_close_(fd) MHD_socket_close_((fd)) | ||
100 | #endif | ||
101 | |||
102 | /* MHD_INVALID_PIPE_ is a value of bad pipe FD */ | ||
103 | #ifndef MHD_DONT_USE_PIPES | ||
104 | #define MHD_INVALID_PIPE_ (-1) | ||
105 | #else | ||
106 | #define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET | ||
107 | #endif | ||
108 | |||
109 | #if !defined(_WIN32) || defined(__CYGWIN__) | 49 | #if !defined(_WIN32) || defined(__CYGWIN__) |
110 | #define MHD_random_() random() | 50 | #define MHD_random_() random() |
111 | #else | 51 | #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" | |||
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | /** | 42 | /** |
43 | * Create pair of mutually connected TCP/IP sockets on loopback address | ||
44 | * @param sockets_pair array to receive resulted sockets | ||
45 | * @return zero on success, -1 otherwise | ||
46 | */ | ||
47 | int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); | ||
48 | |||
49 | /** | ||
50 | * Generate 31-bit pseudo random number. | 43 | * Generate 31-bit pseudo random number. |
51 | * Function initialize itself at first call to current time. | 44 | * Function initialize itself at first call to current time. |
52 | * @return 31-bit pseudo random number. | 45 | * @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 = \ | |||
70 | mhd_threads.c mhd_threads.h \ | 70 | mhd_threads.c mhd_threads.h \ |
71 | mhd_locks.h \ | 71 | mhd_locks.h \ |
72 | mhd_sockets.c mhd_sockets.h \ | 72 | mhd_sockets.c mhd_sockets.h \ |
73 | mhd_itc.c mhd_itc.h \ | ||
73 | response.c response.h | 74 | response.c response.h |
74 | libmicrohttpd_la_CPPFLAGS = \ | 75 | libmicrohttpd_la_CPPFLAGS = \ |
75 | $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \ | 76 | $(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 @@ | |||
35 | #include "mhd_mono_clock.h" | 35 | #include "mhd_mono_clock.h" |
36 | #include "mhd_locks.h" | 36 | #include "mhd_locks.h" |
37 | #include "mhd_sockets.h" | 37 | #include "mhd_sockets.h" |
38 | #include "mhd_itc.h" | ||
38 | 39 | ||
39 | #if HAVE_SEARCH_H | 40 | #if HAVE_SEARCH_H |
40 | #include <search.h> | 41 | #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 @@ | |||
39 | #include "mhd_threads.h" | 39 | #include "mhd_threads.h" |
40 | #include "mhd_locks.h" | 40 | #include "mhd_locks.h" |
41 | #include "mhd_sockets.h" | 41 | #include "mhd_sockets.h" |
42 | #include "mhd_itc.h" | ||
42 | 43 | ||
43 | 44 | ||
44 | /** | 45 | /** |
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 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 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 | |||
21 | /** | ||
22 | * @file microhttpd/mhd_sockets.c | ||
23 | * @brief Implementation of inter-thread communication functions | ||
24 | * @author Karlson2k (Evgeny Grin) | ||
25 | */ | ||
26 | |||
27 | #include "mhd_itc.h" | ||
28 | |||
29 | #if defined(_WIN32) && !defined(__CYGWIN__) | ||
30 | /** | ||
31 | * Create pair of mutually connected TCP/IP sockets on loopback address | ||
32 | * @param sockets_pair array to receive resulted sockets | ||
33 | * @return zero on success, -1 otherwise | ||
34 | */ | ||
35 | int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]) | ||
36 | { | ||
37 | int i; | ||
38 | if (!sockets_pair) | ||
39 | { | ||
40 | errno = EINVAL; | ||
41 | return -1; | ||
42 | } | ||
43 | |||
44 | #define PAIRMAXTRYIES 800 | ||
45 | for (i = 0; i < PAIRMAXTRYIES; i++) | ||
46 | { | ||
47 | struct sockaddr_in listen_addr; | ||
48 | SOCKET listen_s; | ||
49 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ | ||
50 | int addr_len = c_addinlen; | ||
51 | int opt = 1; | ||
52 | |||
53 | listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
54 | if (INVALID_SOCKET == listen_s) | ||
55 | break; /* can't create even single socket */ | ||
56 | |||
57 | listen_addr.sin_family = AF_INET; | ||
58 | listen_addr.sin_port = htons(0); | ||
59 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
60 | if (0 == bind(listen_s, (struct sockaddr*) &listen_addr, c_addinlen) | ||
61 | && 0 == listen(listen_s, 1) | ||
62 | && 0 == getsockname(listen_s, (struct sockaddr*) &listen_addr, | ||
63 | &addr_len)) | ||
64 | { | ||
65 | SOCKET client_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
66 | if (INVALID_SOCKET != client_s) | ||
67 | { | ||
68 | if (0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) | ||
69 | && (0 == connect(client_s, (struct sockaddr*) &listen_addr, c_addinlen) | ||
70 | || WSAGetLastError() == WSAEWOULDBLOCK)) | ||
71 | { | ||
72 | struct sockaddr_in accepted_from_addr; | ||
73 | SOCKET server_s; | ||
74 | addr_len = c_addinlen; | ||
75 | server_s = accept(listen_s, | ||
76 | (struct sockaddr*) &accepted_from_addr, &addr_len); | ||
77 | if (INVALID_SOCKET != server_s) | ||
78 | { | ||
79 | struct sockaddr_in client_addr; | ||
80 | addr_len = c_addinlen; | ||
81 | opt = 0; | ||
82 | if (0 == getsockname(client_s, (struct sockaddr*) &client_addr, &addr_len) | ||
83 | && accepted_from_addr.sin_family == client_addr.sin_family | ||
84 | && accepted_from_addr.sin_port == client_addr.sin_port | ||
85 | && accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr | ||
86 | && 0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) | ||
87 | && 0 == ioctlsocket(server_s, FIONBIO, (u_long*) &opt)) | ||
88 | { | ||
89 | closesocket(listen_s); | ||
90 | sockets_pair[0] = client_s; | ||
91 | sockets_pair[1] = server_s; | ||
92 | return 0; | ||
93 | } | ||
94 | closesocket(server_s); | ||
95 | } | ||
96 | } | ||
97 | closesocket(client_s); | ||
98 | } | ||
99 | } | ||
100 | closesocket(listen_s); | ||
101 | } | ||
102 | |||
103 | sockets_pair[0] = INVALID_SOCKET; | ||
104 | sockets_pair[1] = INVALID_SOCKET; | ||
105 | return -1; | ||
106 | } | ||
107 | |||
108 | #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 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 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 | |||
21 | /** | ||
22 | * @file microhttpd/mhd_sockets.c | ||
23 | * @brief Header for platform-independent inter-thread communication | ||
24 | * @author Karlson2k (Evgeny Grin) | ||
25 | * | ||
26 | * Provides basic abstraction for inter-thread communication. | ||
27 | * Any functions can be implemented as macro on some platforms | ||
28 | * unless explicitly marked otherwise. | ||
29 | * Any function argument can be skipped in macro, so avoid | ||
30 | * variable modification in function parameters. | ||
31 | */ | ||
32 | |||
33 | #ifndef MHD_ITC_H | ||
34 | #define MHD_ITC_H 1 | ||
35 | #include "mhd_options.h" | ||
36 | |||
37 | /* Force don't use pipes on W32 */ | ||
38 | #if defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) | ||
39 | #define MHD_DONT_USE_PIPES 1 | ||
40 | #endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ | ||
41 | |||
42 | #ifndef MHD_DONT_USE_PIPES | ||
43 | # ifdef HAVE_STRING_H | ||
44 | # include <string.h> /* for strerror() */ | ||
45 | # endif | ||
46 | #else | ||
47 | # include "mhd_sockets.h" | ||
48 | #endif /* MHD_DONT_USE_PIPES */ | ||
49 | |||
50 | /* MHD_pipe is type for pipe FDs*/ | ||
51 | #ifndef MHD_DONT_USE_PIPES | ||
52 | typedef int MHD_pipe; | ||
53 | #else /* ! MHD_DONT_USE_PIPES */ | ||
54 | typedef MHD_socket MHD_pipe; | ||
55 | #endif /* ! MHD_DONT_USE_PIPES */ | ||
56 | |||
57 | /* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / | ||
58 | * create two connected sockets (MHD_DONT_USE_PIPES) */ | ||
59 | #ifndef MHD_DONT_USE_PIPES | ||
60 | # define MHD_pipe_(fdarr) pipe((fdarr)) | ||
61 | #else /* MHD_DONT_USE_PIPES */ | ||
62 | # if !defined(_WIN32) || defined(__CYGWIN__) | ||
63 | # define MHD_pipe_(fdarr) socketpair(AF_LOCAL, SOCK_STREAM, 0, (fdarr)) | ||
64 | # else /* !defined(_WIN32) || defined(__CYGWIN__) */ | ||
65 | # define MHD_pipe_(fdarr) MHD_W32_pair_of_sockets_((fdarr)) | ||
66 | # endif /* !defined(_WIN32) || defined(__CYGWIN__) */ | ||
67 | #endif /* MHD_DONT_USE_PIPES */ | ||
68 | |||
69 | /* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) / | ||
70 | * description string of last pipe error (MHD_DONT_USE_PIPES) */ | ||
71 | #ifndef MHD_DONT_USE_PIPES | ||
72 | # define MHD_pipe_last_strerror_() strerror(errno) | ||
73 | #else | ||
74 | # define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() | ||
75 | #endif | ||
76 | |||
77 | /* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) / | ||
78 | * write data to emulated pipe (MHD_DONT_USE_PIPES) */ | ||
79 | #ifndef MHD_DONT_USE_PIPES | ||
80 | # define MHD_pipe_write_(fd, ptr, sz) write((fd), (const void*)(ptr), (sz)) | ||
81 | #else | ||
82 | # define MHD_pipe_write_(fd, ptr, sz) send((fd), (const char*)(ptr), (sz), 0) | ||
83 | #endif | ||
84 | |||
85 | /* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) / | ||
86 | * drain data from emulated pipe (MHD_DONT_USE_PIPES) */ | ||
87 | #ifndef MHD_DONT_USE_PIPES | ||
88 | # define MHD_pipe_drain_(fd) do { long tmp; while (0 < read((fd), (void*)&tmp, sizeof (tmp))) ; } while (0) | ||
89 | #else | ||
90 | # define MHD_pipe_drain_(fd) do { long tmp; while (0 < recv((fd), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) | ||
91 | #endif | ||
92 | |||
93 | /* MHD_pipe_close_(fd) close any FDs (non-W32) / | ||
94 | * close emulated pipe FDs (W32) */ | ||
95 | #ifndef MHD_DONT_USE_PIPES | ||
96 | # define MHD_pipe_close_(fd) close((fd)) | ||
97 | #else | ||
98 | # define MHD_pipe_close_(fd) MHD_socket_close_((fd)) | ||
99 | #endif | ||
100 | |||
101 | /* MHD_INVALID_PIPE_ is a value of bad pipe FD */ | ||
102 | #ifndef MHD_DONT_USE_PIPES | ||
103 | # define MHD_INVALID_PIPE_ (-1) | ||
104 | #else | ||
105 | # define MHD_INVALID_PIPE_ MHD_INVALID_SOCKET | ||
106 | #endif | ||
107 | |||
108 | #if defined(_WIN32) && !defined(__CYGWIN__) | ||
109 | /** | ||
110 | * Create pair of mutually connected TCP/IP sockets on loopback address | ||
111 | * @param sockets_pair array to receive resulted sockets | ||
112 | * @return zero on success, -1 otherwise | ||
113 | */ | ||
114 | int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]); | ||
115 | #endif /* _WIN32 && ! __CYGWIN__ */ | ||
116 | |||
117 | |||
118 | #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 @@ | |||
24 | */ | 24 | */ |
25 | 25 | ||
26 | #include "w32functions.h" | 26 | #include "w32functions.h" |
27 | #include <errno.h> | ||
28 | #include <winsock2.h> | ||
29 | #include <string.h> | 27 | #include <string.h> |
30 | #include <stdint.h> | 28 | #include <stdint.h> |
31 | #include <time.h> | 29 | #include <time.h> |
32 | #include <stdio.h> | 30 | #include <stdio.h> |
33 | #include <stdarg.h> | 31 | #include <stdarg.h> |
34 | #include "../microhttpd/mhd_sockets.h" | ||
35 | |||
36 | /** | ||
37 | * Create pair of mutually connected TCP/IP sockets on loopback address | ||
38 | * @param sockets_pair array to receive resulted sockets | ||
39 | * @return zero on success, -1 otherwise | ||
40 | */ | ||
41 | int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]) | ||
42 | { | ||
43 | int i; | ||
44 | if (!sockets_pair) | ||
45 | { | ||
46 | errno = EINVAL; | ||
47 | return -1; | ||
48 | } | ||
49 | |||
50 | #define PAIRMAXTRYIES 800 | ||
51 | for (i = 0; i < PAIRMAXTRYIES; i++) | ||
52 | { | ||
53 | struct sockaddr_in listen_addr; | ||
54 | SOCKET listen_s; | ||
55 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ | ||
56 | int addr_len = c_addinlen; | ||
57 | int opt = 1; | ||
58 | |||
59 | listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
60 | if (INVALID_SOCKET == listen_s) | ||
61 | break; /* can't create even single socket */ | ||
62 | |||
63 | listen_addr.sin_family = AF_INET; | ||
64 | listen_addr.sin_port = htons(0); | ||
65 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
66 | if (0 == bind(listen_s, (struct sockaddr*) &listen_addr, c_addinlen) | ||
67 | && 0 == listen(listen_s, 1) | ||
68 | && 0 == getsockname(listen_s, (struct sockaddr*) &listen_addr, | ||
69 | &addr_len)) | ||
70 | { | ||
71 | SOCKET client_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
72 | if (INVALID_SOCKET != client_s) | ||
73 | { | ||
74 | if (0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) | ||
75 | && (0 == connect(client_s, (struct sockaddr*) &listen_addr, c_addinlen) | ||
76 | || WSAGetLastError() == WSAEWOULDBLOCK)) | ||
77 | { | ||
78 | struct sockaddr_in accepted_from_addr; | ||
79 | SOCKET server_s; | ||
80 | addr_len = c_addinlen; | ||
81 | server_s = accept(listen_s, | ||
82 | (struct sockaddr*) &accepted_from_addr, &addr_len); | ||
83 | if (INVALID_SOCKET != server_s) | ||
84 | { | ||
85 | struct sockaddr_in client_addr; | ||
86 | addr_len = c_addinlen; | ||
87 | opt = 0; | ||
88 | if (0 == getsockname(client_s, (struct sockaddr*) &client_addr, &addr_len) | ||
89 | && accepted_from_addr.sin_family == client_addr.sin_family | ||
90 | && accepted_from_addr.sin_port == client_addr.sin_port | ||
91 | && accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr | ||
92 | && 0 == ioctlsocket(client_s, FIONBIO, (u_long*) &opt) | ||
93 | && 0 == ioctlsocket(server_s, FIONBIO, (u_long*) &opt)) | ||
94 | { | ||
95 | closesocket(listen_s); | ||
96 | sockets_pair[0] = client_s; | ||
97 | sockets_pair[1] = server_s; | ||
98 | return 0; | ||
99 | } | ||
100 | closesocket(server_s); | ||
101 | } | ||
102 | } | ||
103 | closesocket(client_s); | ||
104 | } | ||
105 | } | ||
106 | closesocket(listen_s); | ||
107 | } | ||
108 | |||
109 | sockets_pair[0] = INVALID_SOCKET; | ||
110 | sockets_pair[1] = INVALID_SOCKET; | ||
111 | return -1; | ||
112 | } | ||
113 | 32 | ||
114 | /** | 33 | /** |
115 | * Static variable used by pseudo random number generator | 34 | * 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 @@ | |||
18 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_str.c" /> | 18 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_str.c" /> |
19 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c" /> | 19 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c" /> |
20 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c" /> | 20 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c" /> |
21 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_itc.c" /> | ||
21 | <ClCompile Include="$(MhdSrc)platform\w32functions.c" /> | 22 | <ClCompile Include="$(MhdSrc)platform\w32functions.c" /> |
22 | </ItemGroup> | 23 | </ItemGroup> |
23 | <ItemGroup> | 24 | <ItemGroup> |
@@ -42,6 +43,7 @@ | |||
42 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> | 43 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> |
43 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" /> | 44 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" /> |
44 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h" /> | 45 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h" /> |
46 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h" /> | ||
45 | <ClInclude Include="$(MhdW32Common)MHD_config.h" /> | 47 | <ClInclude Include="$(MhdW32Common)MHD_config.h" /> |
46 | </ItemGroup> | 48 | </ItemGroup> |
47 | <ItemGroup> | 49 | <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 @@ | |||
145 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c"> | 145 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c"> |
146 | <Filter>Source Files</Filter> | 146 | <Filter>Source Files</Filter> |
147 | </ClCompile> | 147 | </ClCompile> |
148 | <ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h"> | ||
149 | <Filter>Source Files</Filter> | ||
150 | </ClInclude> | ||
151 | <ClCompile Include="$(MhdSrc)microhttpd\mhd_itc.c"> | ||
152 | <Filter>Source Files</Filter> | ||
153 | </ClCompile> | ||
148 | </ItemGroup> | 154 | </ItemGroup> |
149 | <ItemGroup> | 155 | <ItemGroup> |
150 | <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> | 156 | <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> |