aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:12:56 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-23 20:12:56 +0000
commit558572ec73c212b87ea999b0c0d2ed65900d82c2 (patch)
treec914682046b51f504c39c7c6e8e14e9ea163fa73 /src/microhttpd
parent50bbf74d4e1a12f5272a732feb26912db2cff63d (diff)
downloadlibmicrohttpd-558572ec73c212b87ea999b0c0d2ed65900d82c2.tar.gz
libmicrohttpd-558572ec73c212b87ea999b0c0d2ed65900d82c2.zip
Moved pipe/socketpair to separate mhd_itc.h/.c files.
Diffstat (limited to 'src/microhttpd')
-rw-r--r--src/microhttpd/Makefile.am1
-rw-r--r--src/microhttpd/daemon.c1
-rw-r--r--src/microhttpd/internal.h1
-rw-r--r--src/microhttpd/mhd_itc.c108
-rw-r--r--src/microhttpd/mhd_itc.h118
5 files changed, 229 insertions, 0 deletions
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
74libmicrohttpd_la_CPPFLAGS = \ 75libmicrohttpd_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 */
35int 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 */
114int MHD_W32_pair_of_sockets_(SOCKET sockets_pair[2]);
115#endif /* _WIN32 && ! __CYGWIN__ */
116
117
118#endif /* MHD_ITC_H */