From 554c49066f5e1b700751c961875bbcccd90dad36 Mon Sep 17 00:00:00 2001 From: Bensong Liu <bensl@microsoft.com> Date: Wed, 29 Jul 2020 15:46:36 +0800 Subject: [PATCH] fix some error --- src/common.hpp | 2 +- src/forwarder.hpp | 5 +++++ src/lib/rlib/sys/sio.hpp | 10 ++++++++++ src/main.cc | 5 ++++- src/protocols/base.hpp | 2 ++ src/protocols/plain.hpp | 6 +++--- src/utils.hpp | 5 ++++- 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index d3db0c6..91077cc 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -16,7 +16,7 @@ constexpr size_t DGRAM_BUFFER_SIZE = 20480; // to the real openvpn server. constexpr size_t SERVER_ENCRYPT_CONNECTION_TIMEOUT_SECONDS = 60; -// MAGIC PORT NUMBER! Warning! +// MAGIC PORT NUMBER! Warning! Used for Inbound - Outbound IPC talking. Windows wepoll doesn't support PIPE, so I have to use this. constexpr uint16_t TCP_TMP_PORT_NUMBER = 50999; #endif diff --git a/src/forwarder.hpp b/src/forwarder.hpp index 6a0de5b..851d87c 100644 --- a/src/forwarder.hpp +++ b/src/forwarder.hpp @@ -30,6 +30,11 @@ public: if (ptrOutbound) delete ptrOutbound; } + [[noreturn]] void runForever() { + std::thread([this] {ptrInbound->listenForever(ptrOutbound);}).detach(); + ptrOutbound->listenForever(ptrInbound); // Blocks + } + private: Protocols::BaseInbound *ptrInbound; diff --git a/src/lib/rlib/sys/sio.hpp b/src/lib/rlib/sys/sio.hpp index 7919729..6a7da72 100644 --- a/src/lib/rlib/sys/sio.hpp +++ b/src/lib/rlib/sys/sio.hpp @@ -504,6 +504,11 @@ namespace rlib { currvptr = (char *)vptr + current / 2; } } + static void close_ex(sockfd_t fd) { + if (closesocket(fd) == SOCKET_ERROR) { + throw std::runtime_error("closeSocket failed. error code: " + std::to_string(WSAGetLastError())); + } + } #else // POSIX sockIO public: @@ -589,6 +594,11 @@ namespace rlib { currvptr = (char *)vptr + current / 2; } } + static void close_ex(sockfd_t fd) { + if (close(fd) == -1) { + throw std::runtime_error("close failed. error code: " + std::to_string(errno)); + } + } #endif #ifndef MSG_NOSIGNAL diff --git a/src/main.cc b/src/main.cc index f088caa..37da970 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,6 +11,9 @@ using namespace std::chrono_literals; #if RLIB_OS_ID == OS_WINDOWS #define windows_main main + #ifdef ERROR + #undef ERROR + #endif #else #define real_main main #endif @@ -39,7 +42,7 @@ int real_main(int argc, char **argv) { else throw std::runtime_error("Unknown log level: " + log_level); - Forwarder(inboundConfig, outboundConfig).run_forever(); + Forwarder(inboundConfig, outboundConfig).runForever(); return 0; } diff --git a/src/protocols/base.hpp b/src/protocols/base.hpp index ddf3af3..09a9bed 100644 --- a/src/protocols/base.hpp +++ b/src/protocols/base.hpp @@ -16,6 +16,8 @@ User */ namespace Protocols { + struct BaseInbound; + // Outbound holds the senderId=>nextHopFd mapping. // senderId is "$ip@$port", for example, `fe80:8100::1@1080`. // Note: this interface works for both TCP and UDP. diff --git a/src/protocols/plain.hpp b/src/protocols/plain.hpp index 69aea00..2bcc7bc 100644 --- a/src/protocols/plain.hpp +++ b/src/protocols/plain.hpp @@ -20,14 +20,14 @@ namespace Protocols { } virtual void forwardMessageToOutbound(string binaryMessage, string senderId) override { // Outbound calls this function, to alert the inbound listener thread, for the new msg. - - + rlib::sockIO::send_msg(ipcPipe, senderId); + rlib::sockIO::send_msg(ipcPipe, binaryMessage); } virtual void listenForever(BaseOutbound* nextHop) override { std::tie(this->ipcPipe, nextHop->ipcPipe) = mk_tcp_pipe(); auto listenFd = rlib::quick_listen(listenAddr, listenPort, true); - rlib_defer([&] {close(listenFd);}); + rlib_defer([&] {rlib::sockIO::close_ex(listenFd);}); auto epollFd = epoll_create1(0); dynamic_assert((int)epollFd != -1, "epoll_create1 failed"); diff --git a/src/utils.hpp b/src/utils.hpp index 3019e2a..f124673 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -13,6 +13,9 @@ #include <wepoll.h> #endif +#include <string> +using std::string; + struct SockAddr { union { sockaddr_storage addr_storage; @@ -20,7 +23,7 @@ struct SockAddr { sockaddr_in in4; sockaddr_in6 in6; }; - int len; + socklen_t len; }; struct ConnectionMapping { -- GitLab