From afed387bcfd9f50aad0e9654af02cd554638b3bc Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 30 Sep 2019 22:06:46 -0700 Subject: [PATCH] Socket Factory has only one function which works for server and client code, and can do tls for both --- docs/usage.md | 2 +- ixcobra/ixcobra/IXCobraConnection.h | 2 +- ixsnake/ixsnake/IXRedisClient.cpp | 4 +++- ixsnake/ixsnake/IXRedisServer.cpp | 4 ++-- ixsnake/ixsnake/IXSnakeProtocol.cpp | 2 +- ixsnake/ixsnake/IXSnakeServer.cpp | 2 +- ixwebsocket/IXHttpClient.cpp | 2 +- ixwebsocket/IXSocketFactory.cpp | 24 ++++++------------------ ixwebsocket/IXSocketFactory.h | 3 +-- ixwebsocket/IXSocketServer.cpp | 3 ++- ixwebsocket/IXWebSocket.cpp | 3 +-- ixwebsocket/IXWebSocket.h | 3 +-- ixwebsocket/IXWebSocketTransport.cpp | 2 +- ixwebsocket/IXWebSocketTransport.h | 10 ++++------ test/IXSocketTest.cpp | 5 +++-- test/IXWebSocketServerTest.cpp | 9 ++++++--- ws/ws_ping_pong.cpp | 3 +-- ws/ws_receive.cpp | 4 +++- 18 files changed, 39 insertions(+), 48 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 9f9ef35c..2aeda01f 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -252,7 +252,7 @@ On a client, this is only necessary for connecting to servers that require a cli On a server, this is necessary for TLS support. -Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates. +Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates. - The special value of `SYSTEM` (the default) indicates that the system-configured trust bundle should be used; this is generally what you want when connecting to any publicly exposed API/server. - The special value of `NONE` can be used to disable peer verification; this is only recommended to rule out certificate verification when testing connectivity. diff --git a/ixcobra/ixcobra/IXCobraConnection.h b/ixcobra/ixcobra/IXCobraConnection.h index 4c8c1a48..076ce769 100644 --- a/ixcobra/ixcobra/IXCobraConnection.h +++ b/ixcobra/ixcobra/IXCobraConnection.h @@ -121,7 +121,7 @@ namespace ix void suspend(); void resume(); - /// Prepare a message for transmission + /// Prepare a message for transmission /// (update the pdu, compute a msgId, serialize json to a string) std::pair prePublish( const Json::Value& channels, diff --git a/ixsnake/ixsnake/IXRedisClient.cpp b/ixsnake/ixsnake/IXRedisClient.cpp index baa15a3d..c2e3ad75 100644 --- a/ixsnake/ixsnake/IXRedisClient.cpp +++ b/ixsnake/ixsnake/IXRedisClient.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,8 @@ namespace ix { bool tls = false; std::string errorMsg; - _socket = createSocket(tls, errorMsg); + SocketTLSOptions tlsOptions; + _socket = createSocket(tls, -1, errorMsg, tlsOptions); if (!_socket) { diff --git a/ixsnake/ixsnake/IXRedisServer.cpp b/ixsnake/ixsnake/IXRedisServer.cpp index 3825d03c..a096d1be 100644 --- a/ixsnake/ixsnake/IXRedisServer.cpp +++ b/ixsnake/ixsnake/IXRedisServer.cpp @@ -40,7 +40,7 @@ namespace ix std::this_thread::sleep_for(std::chrono::milliseconds(10)); } _stopHandlingConnections = false; - + SocketServer::stop(); } @@ -270,7 +270,7 @@ namespace ix // now dispatch the message to subscribers (write custom method) std::lock_guard lock(_mutex); auto it = _subscribers.find(channel); - if (it == _subscribers.end()) + if (it == _subscribers.end()) { // return the number of clients that received the message, 0 in that case socket->writeBytes(":0\r\n", cb); diff --git a/ixsnake/ixsnake/IXSnakeProtocol.cpp b/ixsnake/ixsnake/IXSnakeProtocol.cpp index 877710c1..452b281c 100644 --- a/ixsnake/ixsnake/IXSnakeProtocol.cpp +++ b/ixsnake/ixsnake/IXSnakeProtocol.cpp @@ -211,7 +211,7 @@ namespace snake ss << "Subscribing to " << appChannel << "..."; ix::IXCoreLogger::Log(ss.str().c_str()); } - + if (!redisClient.subscribe(appChannel, responseCallback, callback)) { std::stringstream ss; diff --git a/ixsnake/ixsnake/IXSnakeServer.cpp b/ixsnake/ixsnake/IXSnakeServer.cpp index 0b9db17b..216dd2b1 100644 --- a/ixsnake/ixsnake/IXSnakeServer.cpp +++ b/ixsnake/ixsnake/IXSnakeServer.cpp @@ -102,7 +102,7 @@ namespace snake ss << "Received " << msg->wireSize << " bytes" << std::endl; processCobraMessage(state, webSocket, _appConfig, msg->str); } - + ix::IXCoreLogger::Log(ss.str().c_str()); }); }); diff --git a/ixwebsocket/IXHttpClient.cpp b/ixwebsocket/IXHttpClient.cpp index 2695f673..282e5f80 100644 --- a/ixwebsocket/IXHttpClient.cpp +++ b/ixwebsocket/IXHttpClient.cpp @@ -147,7 +147,7 @@ namespace ix bool tls = protocol == "https"; std::string errorMsg; - _socket = createSocket(tls, errorMsg, _tlsOptions); + _socket = createSocket(tls, -1, errorMsg, _tlsOptions); if (!_socket) { diff --git a/ixwebsocket/IXSocketFactory.cpp b/ixwebsocket/IXSocketFactory.cpp index 26eb5c1c..cc148aaf 100644 --- a/ixwebsocket/IXSocketFactory.cpp +++ b/ixwebsocket/IXSocketFactory.cpp @@ -27,6 +27,7 @@ namespace ix { std::shared_ptr createSocket(bool tls, + int fd, std::string& errorMsg, const SocketTLSOptions& tlsOptions) { @@ -35,19 +36,19 @@ namespace ix if (!tls) { - socket = std::make_shared(); + socket = std::make_shared(fd); } else { #ifdef IXWEBSOCKET_USE_TLS #if defined(IXWEBSOCKET_USE_MBED_TLS) - socket = std::make_shared(tlsOptions); + socket = std::make_shared(tlsOptions, fd); #elif defined(IXWEBSOCKET_USE_OPEN_SSL) - socket = std::make_shared(tlsOptions); + socket = std::make_shared(tlsOptions, fd); #elif defined(_WIN32) - socket = std::make_shared(tlsOptions); + socket = std::make_shared(tlsOptions, fd); #elif defined(__APPLE__) - socket = std::make_shared(tlsOptions); + socket = std::make_shared(tlsOptions, fd); #endif #else errorMsg = "TLS support is not enabled on this platform."; @@ -62,17 +63,4 @@ namespace ix return socket; } - - std::shared_ptr createSocket(int fd, std::string& errorMsg) - { - errorMsg.clear(); - - std::shared_ptr socket = std::make_shared(fd); - if (!socket->init(errorMsg)) - { - socket.reset(); - } - - return socket; - } } // namespace ix diff --git a/ixwebsocket/IXSocketFactory.h b/ixwebsocket/IXSocketFactory.h index 88ec688a..b682fadc 100644 --- a/ixwebsocket/IXSocketFactory.h +++ b/ixwebsocket/IXSocketFactory.h @@ -15,8 +15,7 @@ namespace ix { class Socket; std::shared_ptr createSocket(bool tls, + int fd, std::string& errorMsg, const SocketTLSOptions& tlsOptions); - - std::shared_ptr createSocket(int fd, std::string& errorMsg); } // namespace ix diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 26e51a6e..aaab3ef7 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -270,7 +270,8 @@ namespace ix // create socket std::string errorMsg; - auto socket = createSocket(clientFd, errorMsg); + bool tls = false; + auto socket = createSocket(tls, clientFd, errorMsg, _socketTLSOptions); if (socket == nullptr) { diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 08633b17..cb85395f 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -201,8 +201,7 @@ namespace ix return status; } - WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr socket, - int timeoutSecs) + WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr socket, int timeoutSecs) { { std::lock_guard lock(_configMutex); diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 3d8df157..2c1327a6 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -113,8 +113,7 @@ namespace ix static void invokeTrafficTrackerCallback(size_t size, bool incoming); // Server - WebSocketInitResult connectToSocket(std::shared_ptr, - int timeoutSecs); + WebSocketInitResult connectToSocket(std::shared_ptr, int timeoutSecs); WebSocketTransport _ws; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 3fbe5248..ab54720d 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -148,7 +148,7 @@ namespace ix std::string errorMsg; bool tls = protocol == "wss"; - _socket = createSocket(tls, errorMsg, _socketTLSOptions); + _socket = createSocket(tls, -1, errorMsg, _socketTLSOptions); if (!_socket) { diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index b5d7bcac..189fc3f8 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -78,14 +78,12 @@ namespace ix int pingTimeoutSecs); // Client - WebSocketInitResult connectToUrl( - const std::string& url, - const WebSocketHttpHeaders& headers, - int timeoutSecs); + WebSocketInitResult connectToUrl(const std::string& url, + const WebSocketHttpHeaders& headers, + int timeoutSecs); // Server - WebSocketInitResult connectToSocket(std::shared_ptr socket, - int timeoutSecs); + WebSocketInitResult connectToSocket(std::shared_ptr socket, int timeoutSecs); PollResult poll(); WebSocketSendInfo sendBinary(const std::string& message, diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 99d91e8b..b76dc0b3 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -61,7 +61,8 @@ TEST_CASE("socket", "[socket]") std::string errMsg; bool tls = false; - std::shared_ptr socket = createSocket(tls, errMsg); + SocketTLSOptions tlsOptions; + std::shared_ptr socket = createSocket(tls, -1, errMsg, tlsOptions); std::string host("127.0.0.1"); std::stringstream ss; @@ -84,7 +85,7 @@ TEST_CASE("socket", "[socket]") bool tls = true; SocketTLSOptions tlsOptions; tlsOptions.caFile = "cacert.pem"; - std::shared_ptr socket = createSocket(tls, errMsg, tlsOptions); + std::shared_ptr socket = createSocket(tls, -1, errMsg, tlsOptions); std::string host("www.google.com"); int port = 443; std::string request("GET / HTTP/1.1\r\n\r\n"); diff --git a/test/IXWebSocketServerTest.cpp b/test/IXWebSocketServerTest.cpp index 2ae61eb9..f96a87b6 100644 --- a/test/IXWebSocketServerTest.cpp +++ b/test/IXWebSocketServerTest.cpp @@ -92,7 +92,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") std::string errMsg; bool tls = false; - std::shared_ptr socket = createSocket(tls, errMsg); + SocketTLSOptions tlsOptions; + std::shared_ptr socket = createSocket(tls, -1, errMsg, tlsOptions); std::string host("127.0.0.1"); auto isCancellationRequested = []() -> bool { return false; }; bool success = socket->connect(host, port, errMsg, isCancellationRequested); @@ -125,7 +126,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") std::string errMsg; bool tls = false; - std::shared_ptr socket = createSocket(tls, errMsg); + SocketTLSOptions tlsOptions; + std::shared_ptr socket = createSocket(tls, -1, errMsg, tlsOptions); std::string host("127.0.0.1"); auto isCancellationRequested = []() -> bool { return false; }; bool success = socket->connect(host, port, errMsg, isCancellationRequested); @@ -161,7 +163,8 @@ TEST_CASE("Websocket_server", "[websocket_server]") std::string errMsg; bool tls = false; - std::shared_ptr socket = createSocket(tls, errMsg); + SocketTLSOptions tlsOptions; + std::shared_ptr socket = createSocket(tls, -1, errMsg, tlsOptions); std::string host("127.0.0.1"); auto isCancellationRequested = []() -> bool { return false; }; bool success = socket->connect(host, port, errMsg, isCancellationRequested); diff --git a/ws/ws_ping_pong.cpp b/ws/ws_ping_pong.cpp index 433cd95e..8d81b0e4 100644 --- a/ws/ws_ping_pong.cpp +++ b/ws/ws_ping_pong.cpp @@ -15,8 +15,7 @@ namespace ix class WebSocketPingPong { public: - WebSocketPingPong(const std::string& _url, - const ix::SocketTLSOptions& tlsOptions); + WebSocketPingPong(const std::string& _url, const ix::SocketTLSOptions& tlsOptions); void subscribe(const std::string& channel); void start(); diff --git a/ws/ws_receive.cpp b/ws/ws_receive.cpp index 152ee76c..b966ea97 100644 --- a/ws/ws_receive.cpp +++ b/ws/ws_receive.cpp @@ -26,7 +26,9 @@ namespace ix class WebSocketReceiver { public: - WebSocketReceiver(const std::string& _url, bool enablePerMessageDeflate, int delayMs, + WebSocketReceiver(const std::string& _url, + bool enablePerMessageDeflate, + int delayMs, const ix::SocketTLSOptions& tlsOptions); void subscribe(const std::string& channel);