From f3d562ff0258e8adfb0a9e4d119d3d1cf240627f Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sun, 26 Jan 2020 16:44:44 -0800 Subject: [PATCH] add -6 option to ws echo_server / cf #148 --- ixwebsocket/IXSocketServer.cpp | 4 ++-- ixwebsocket/IXWebSocketServer.h | 3 ++- ws/ws.cpp | 4 +++- ws/ws.h | 4 +++- ws/ws_echo_server.cpp | 12 ++++++++++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 76b0b4eb..29b1fbad 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -86,7 +86,7 @@ namespace ix server.sin_family = _addressFamily; server.sin_port = htons(_port); - if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) < 0) + if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0) { std::stringstream ss; ss << "SocketServer::listen() error calling inet_pton " @@ -96,7 +96,7 @@ namespace ix return std::make_pair(false, ss.str()); } - if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0) + if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) <= 0) { std::stringstream ss; ss << "SocketServer::listen() error calling bind " diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index fb2aa4be..c7f96469 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -42,6 +42,8 @@ namespace ix // Get all the connected clients std::set> getClients(); + const static int kDefaultHandShakeTimeoutSecs; + private: // Member variables int _handshakeTimeoutSecs; @@ -52,7 +54,6 @@ namespace ix std::mutex _clientsMutex; std::set> _clients; - const static int kDefaultHandShakeTimeoutSecs; const static bool kDefaultEnablePong; // Methods diff --git a/ws/ws.cpp b/ws/ws.cpp index 03a4495a..311ffe99 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -94,6 +94,7 @@ int main(int argc, char** argv) bool disableAutomaticReconnection = false; bool disablePerMessageDeflate = false; bool greetings = false; + bool ipv6 = false; bool binaryMode = false; bool redirect = false; bool version = false; @@ -171,6 +172,7 @@ int main(int argc, char** argv) echoServerApp->add_option("--port", port, "Port"); echoServerApp->add_option("--host", hostname, "Hostname"); echoServerApp->add_flag("-g", greetings, "Verbose"); + echoServerApp->add_flag("-6", ipv6, "IpV6"); addTLSOptions(echoServerApp); CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server"); @@ -390,7 +392,7 @@ int main(int argc, char** argv) } else if (app.got_subcommand("echo_server")) { - ret = ix::ws_echo_server_main(port, greetings, hostname, tlsOptions); + ret = ix::ws_echo_server_main(port, greetings, hostname, tlsOptions, ipv6); } else if (app.got_subcommand("broadcast_server")) { diff --git a/ws/ws.h b/ws/ws.h index 354cb7f1..70220322 100644 --- a/ws/ws.h +++ b/ws/ws.h @@ -29,7 +29,9 @@ namespace ix int ws_echo_server_main(int port, bool greetings, const std::string& hostname, - const ix::SocketTLSOptions& tlsOptions); + const ix::SocketTLSOptions& tlsOptions, + bool ipv6); + int ws_broadcast_server_main(int port, const std::string& hostname, const ix::SocketTLSOptions& tlsOptions); diff --git a/ws/ws_echo_server.cpp b/ws/ws_echo_server.cpp index e2fd46c7..8abb0d4c 100644 --- a/ws/ws_echo_server.cpp +++ b/ws/ws_echo_server.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -13,11 +14,18 @@ namespace ix int ws_echo_server_main(int port, bool greetings, const std::string& hostname, - const ix::SocketTLSOptions& tlsOptions) + const ix::SocketTLSOptions& tlsOptions, + bool ipv6) { spdlog::info("Listening on {}:{}", hostname, port); - ix::WebSocketServer server(port, hostname); + ix::WebSocketServer server(port, + hostname, + SocketServer::kDefaultTcpBacklog, + SocketServer::kDefaultMaxConnections, + WebSocketServer::kDefaultHandShakeTimeoutSecs, + (ipv6) ? AF_INET6 : AF_INET); + server.setTLSOptions(tlsOptions); server.setOnConnectionCallback(