Compare commits

..

1 Commits

Author SHA1 Message Date
Benjamin Sergeant
4c15964d43 (ws) add a dnslookup sub-command, to get the ip address of a remote host 2020-01-22 21:11:48 -08:00
16 changed files with 45 additions and 124 deletions

View File

@@ -1,10 +1,6 @@
# Changelog # Changelog
All changes to this project will be documented in this file. All changes to this project will be documented in this file.
## [8.0.0] - 2020-01-26
(SocketServer) add ability to bind on an ipv6 address
## [7.9.6] - 2020-01-22 ## [7.9.6] - 2020-01-22
(ws) add a dnslookup sub-command, to get the ip address of a remote host (ws) add a dnslookup sub-command, to get the ip address of a remote host

View File

@@ -33,16 +33,11 @@ vcpkg install ixwebsocket
### Conan ### Conan
[ ![Download](https://api.bintray.com/packages/conan/conan-center/ixwebsocket%3A_/images/download.svg) ](https://bintray.com/conan/conan-center/ixwebsocket%3A_/_latestVersion) Support for building with conan was contributed by Olivia Zoe (thanks!). The package name to reference is `IXWebSocket/5.0.0@LunarWatcher/stable`, and a list of the uploaded versions is available on [Bintray](https://bintray.com/oliviazoe0/conan-packages/IXWebSocket%3ALunarWatcher). The package is in the process to be published to the official conan package repo, but in the meantime, it can be accessed by adding a new remote
Conan is currently supported through a recipe in [Conan Center](https://github.com/conan-io/conan-center-index/tree/master/recipes/ixwebsocket) ([Bintray entry](https://bintray.com/conan/conan-center/ixwebsocket%3A_)). ```
conan remote add remote_name_here https://api.bintray.com/conan/oliviazoe0/conan-packages
Package reference ```
* Conan 1.21.0 and up: `ixwebsocket/7.9.2`
* Earlier versions: `ixwebsocket/7.9.2@_/_`
Note that the version listed here might not be the latest one. See Bintray or the recipe itself for the latest version. If you're migrating from the previous, custom Bintray remote, note that the package reference _has_ to be lower-case.
### Docker ### Docker

View File

@@ -17,8 +17,8 @@
namespace ix namespace ix
{ {
RedisServer::RedisServer(int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily) RedisServer::RedisServer(int port, const std::string& host, int backlog, size_t maxConnections)
: SocketServer(port, host, backlog, maxConnections, addressFamily) : SocketServer(port, host, backlog, maxConnections)
, _connectedClientsCount(0) , _connectedClientsCount(0)
, _stopHandlingConnections(false) , _stopHandlingConnections(false)
{ {

View File

@@ -25,8 +25,7 @@ namespace ix
RedisServer(int port = SocketServer::kDefaultPort, RedisServer(int port = SocketServer::kDefaultPort,
const std::string& host = SocketServer::kDefaultHost, const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog, int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections, size_t maxConnections = SocketServer::kDefaultMaxConnections);
int addressFamily = SocketServer::kDefaultAddressFamily);
virtual ~RedisServer(); virtual ~RedisServer();
virtual void stop() final; virtual void stop() final;

View File

@@ -42,8 +42,8 @@ namespace
namespace ix namespace ix
{ {
HttpServer::HttpServer(int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily) HttpServer::HttpServer(int port, const std::string& host, int backlog, size_t maxConnections)
: SocketServer(port, host, backlog, maxConnections, addressFamily) : SocketServer(port, host, backlog, maxConnections)
, _connectedClientsCount(0) , _connectedClientsCount(0)
{ {
setDefaultConnectionCallback(); setDefaultConnectionCallback();

View File

@@ -28,8 +28,7 @@ namespace ix
HttpServer(int port = SocketServer::kDefaultPort, HttpServer(int port = SocketServer::kDefaultPort,
const std::string& host = SocketServer::kDefaultHost, const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog, int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections, size_t maxConnections = SocketServer::kDefaultMaxConnections);
int addressFamily = SocketServer::kDefaultAddressFamily);
virtual ~HttpServer(); virtual ~HttpServer();
virtual void stop() final; virtual void stop() final;

View File

@@ -21,18 +21,15 @@ namespace ix
const std::string SocketServer::kDefaultHost("127.0.0.1"); const std::string SocketServer::kDefaultHost("127.0.0.1");
const int SocketServer::kDefaultTcpBacklog(5); const int SocketServer::kDefaultTcpBacklog(5);
const size_t SocketServer::kDefaultMaxConnections(32); const size_t SocketServer::kDefaultMaxConnections(32);
const int SocketServer::kDefaultAddressFamily(AF_INET);
SocketServer::SocketServer(int port, SocketServer::SocketServer(int port,
const std::string& host, const std::string& host,
int backlog, int backlog,
size_t maxConnections, size_t maxConnections)
int addressFamily)
: _port(port) : _port(port)
, _host(host) , _host(host)
, _backlog(backlog) , _backlog(backlog)
, _maxConnections(maxConnections) , _maxConnections(maxConnections)
, _addressFamily(addressFamily)
, _serverFd(-1) , _serverFd(-1)
, _stop(false) , _stop(false)
, _stopGc(false) , _stopGc(false)
@@ -59,15 +56,10 @@ namespace ix
std::pair<bool, std::string> SocketServer::listen() std::pair<bool, std::string> SocketServer::listen()
{ {
if (_addressFamily != AF_INET && _addressFamily != AF_INET6) struct sockaddr_in server; // server address information
{
std::string errMsg("SocketServer::listen() AF_INET and AF_INET6 are currently "
"the only supported address families");
return std::make_pair(false, errMsg);
}
// Get a socket for accepting connections. // Get a socket for accepting connections.
if ((_serverFd = socket(_addressFamily, SOCK_STREAM, 0)) < 0) if ((_serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{ {
std::stringstream ss; std::stringstream ss;
ss << "SocketServer::listen() error creating socket): " << strerror(Socket::getErrno()); ss << "SocketServer::listen() error creating socket): " << strerror(Socket::getErrno());
@@ -87,23 +79,19 @@ namespace ix
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
if (_addressFamily == AF_INET) // Bind the socket to the server address.
{ server.sin_family = AF_INET;
struct sockaddr_in server;
server.sin_family = _addressFamily;
server.sin_port = htons(_port); server.sin_port = htons(_port);
if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0) // Using INADDR_ANY trigger a pop-up box as binding to any address is detected
{ // by the osx firewall. We need to codesign the binary with a self-signed cert
std::stringstream ss; // to allow that, but this is a bit of a pain. (this is what node or python would do).
ss << "SocketServer::listen() error calling inet_pton " //
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno()); // Using INADDR_LOOPBACK also does not work ... while it should.
// We default to 127.0.0.1 (localhost)
//
server.sin_addr.s_addr = inet_addr(_host.c_str());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
// Bind the socket to the server address.
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0) if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
{ {
std::stringstream ss; std::stringstream ss;
@@ -113,34 +101,6 @@ namespace ix
Socket::closeSocket(_serverFd); Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
}
else // AF_INET6
{
struct sockaddr_in6 server;
server.sin6_family = _addressFamily;
server.sin6_port = htons(_port);
if (inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
// Bind the socket to the server address.
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling bind "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
}
// //
// Listen for connections. Specify the tcp backlog. // Listen for connections. Specify the tcp backlog.

View File

@@ -36,8 +36,7 @@ namespace ix
SocketServer(int port = SocketServer::kDefaultPort, SocketServer(int port = SocketServer::kDefaultPort,
const std::string& host = SocketServer::kDefaultHost, const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog, int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections, size_t maxConnections = SocketServer::kDefaultMaxConnections);
int addressFamily = SocketServer::kDefaultAddressFamily);
virtual ~SocketServer(); virtual ~SocketServer();
virtual void stop(); virtual void stop();
@@ -50,7 +49,6 @@ namespace ix
const static std::string kDefaultHost; const static std::string kDefaultHost;
const static int kDefaultTcpBacklog; const static int kDefaultTcpBacklog;
const static size_t kDefaultMaxConnections; const static size_t kDefaultMaxConnections;
const static int kDefaultAddressFamily;
void start(); void start();
std::pair<bool, std::string> listen(); std::pair<bool, std::string> listen();
@@ -71,7 +69,6 @@ namespace ix
std::string _host; std::string _host;
int _backlog; int _backlog;
size_t _maxConnections; size_t _maxConnections;
int _addressFamily;
// socket for accepting connections // socket for accepting connections
int _serverFd; int _serverFd;

View File

@@ -169,7 +169,6 @@ namespace ix
// wait until working thread will exit // wait until working thread will exit
// it will exit after close operation is finished // it will exit after close operation is finished
_stop = true; _stop = true;
_sleepCondition.notify_one();
_thread.join(); _thread.join();
_stop = false; _stop = false;
} }
@@ -283,13 +282,8 @@ namespace ix
// Only sleep if we are retrying // Only sleep if we are retrying
if (duration.count() > 0) if (duration.count() > 0)
{ {
std::unique_lock<std::mutex> lock(_sleepMutex); // to do: make sleeping conditional
_sleepCondition.wait_for(lock, duration); std::this_thread::sleep_for(duration);
}
if (_stop)
{
break;
} }
// Try to connect synchronously // Try to connect synchronously

View File

@@ -22,7 +22,6 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <condition_variable>
namespace ix namespace ix
{ {
@@ -141,10 +140,6 @@ namespace ix
static const uint32_t kDefaultMaxWaitBetweenReconnectionRetries; static const uint32_t kDefaultMaxWaitBetweenReconnectionRetries;
uint32_t _maxWaitBetweenReconnectionRetries; uint32_t _maxWaitBetweenReconnectionRetries;
// Make the sleeping in the automatic reconnection cancellable
std::mutex _sleepMutex;
std::condition_variable _sleepCondition;
std::atomic<int> _handshakeTimeoutSecs; std::atomic<int> _handshakeTimeoutSecs;
static const int kDefaultHandShakeTimeoutSecs; static const int kDefaultHandShakeTimeoutSecs;

View File

@@ -23,9 +23,8 @@ namespace ix
const std::string& host, const std::string& host,
int backlog, int backlog,
size_t maxConnections, size_t maxConnections,
int handshakeTimeoutSecs, int handshakeTimeoutSecs)
int addressFamily) : SocketServer(port, host, backlog, maxConnections)
: SocketServer(port, host, backlog, maxConnections, addressFamily)
, _handshakeTimeoutSecs(handshakeTimeoutSecs) , _handshakeTimeoutSecs(handshakeTimeoutSecs)
, _enablePong(kDefaultEnablePong) , _enablePong(kDefaultEnablePong)
{ {
@@ -107,6 +106,7 @@ namespace ix
} }
} }
logInfo("WebSocketServer::handleConnection() done");
connectionState->setTerminated(); connectionState->setTerminated();
} }

View File

@@ -29,8 +29,7 @@ namespace ix
const std::string& host = SocketServer::kDefaultHost, const std::string& host = SocketServer::kDefaultHost,
int backlog = SocketServer::kDefaultTcpBacklog, int backlog = SocketServer::kDefaultTcpBacklog,
size_t maxConnections = SocketServer::kDefaultMaxConnections, size_t maxConnections = SocketServer::kDefaultMaxConnections,
int handshakeTimeoutSecs = WebSocketServer::kDefaultHandShakeTimeoutSecs, int handshakeTimeoutSecs = WebSocketServer::kDefaultHandShakeTimeoutSecs);
int addressFamily = SocketServer::kDefaultAddressFamily);
virtual ~WebSocketServer(); virtual ~WebSocketServer();
virtual void stop() final; virtual void stop() final;
@@ -42,8 +41,6 @@ namespace ix
// Get all the connected clients // Get all the connected clients
std::set<std::shared_ptr<WebSocket>> getClients(); std::set<std::shared_ptr<WebSocket>> getClients();
const static int kDefaultHandShakeTimeoutSecs;
private: private:
// Member variables // Member variables
int _handshakeTimeoutSecs; int _handshakeTimeoutSecs;
@@ -54,6 +51,7 @@ namespace ix
std::mutex _clientsMutex; std::mutex _clientsMutex;
std::set<std::shared_ptr<WebSocket>> _clients; std::set<std::shared_ptr<WebSocket>> _clients;
const static int kDefaultHandShakeTimeoutSecs;
const static bool kDefaultEnablePong; const static bool kDefaultEnablePong;
// Methods // Methods

View File

@@ -6,4 +6,4 @@
#pragma once #pragma once
#define IX_WEBSOCKET_VERSION "8.0.0" #define IX_WEBSOCKET_VERSION "7.9.6"

View File

@@ -94,7 +94,6 @@ int main(int argc, char** argv)
bool disableAutomaticReconnection = false; bool disableAutomaticReconnection = false;
bool disablePerMessageDeflate = false; bool disablePerMessageDeflate = false;
bool greetings = false; bool greetings = false;
bool ipv6 = false;
bool binaryMode = false; bool binaryMode = false;
bool redirect = false; bool redirect = false;
bool version = false; bool version = false;
@@ -172,7 +171,6 @@ int main(int argc, char** argv)
echoServerApp->add_option("--port", port, "Port"); echoServerApp->add_option("--port", port, "Port");
echoServerApp->add_option("--host", hostname, "Hostname"); echoServerApp->add_option("--host", hostname, "Hostname");
echoServerApp->add_flag("-g", greetings, "Verbose"); echoServerApp->add_flag("-g", greetings, "Verbose");
echoServerApp->add_flag("-6", ipv6, "IpV6");
addTLSOptions(echoServerApp); addTLSOptions(echoServerApp);
CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server"); CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server");
@@ -392,7 +390,7 @@ int main(int argc, char** argv)
} }
else if (app.got_subcommand("echo_server")) else if (app.got_subcommand("echo_server"))
{ {
ret = ix::ws_echo_server_main(port, greetings, hostname, tlsOptions, ipv6); ret = ix::ws_echo_server_main(port, greetings, hostname, tlsOptions);
} }
else if (app.got_subcommand("broadcast_server")) else if (app.got_subcommand("broadcast_server"))
{ {

View File

@@ -29,9 +29,7 @@ namespace ix
int ws_echo_server_main(int port, int ws_echo_server_main(int port,
bool greetings, bool greetings,
const std::string& hostname, const std::string& hostname,
const ix::SocketTLSOptions& tlsOptions, const ix::SocketTLSOptions& tlsOptions);
bool ipv6);
int ws_broadcast_server_main(int port, int ws_broadcast_server_main(int port,
const std::string& hostname, const std::string& hostname,
const ix::SocketTLSOptions& tlsOptions); const ix::SocketTLSOptions& tlsOptions);

View File

@@ -5,7 +5,6 @@
*/ */
#include <ixwebsocket/IXWebSocketServer.h> #include <ixwebsocket/IXWebSocketServer.h>
#include <ixwebsocket/IXNetSystem.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <sstream> #include <sstream>
@@ -14,18 +13,11 @@ namespace ix
int ws_echo_server_main(int port, int ws_echo_server_main(int port,
bool greetings, bool greetings,
const std::string& hostname, const std::string& hostname,
const ix::SocketTLSOptions& tlsOptions, const ix::SocketTLSOptions& tlsOptions)
bool ipv6)
{ {
spdlog::info("Listening on {}:{}", hostname, port); spdlog::info("Listening on {}:{}", hostname, port);
ix::WebSocketServer server(port, ix::WebSocketServer server(port, hostname);
hostname,
SocketServer::kDefaultTcpBacklog,
SocketServer::kDefaultMaxConnections,
WebSocketServer::kDefaultHandShakeTimeoutSecs,
(ipv6) ? AF_INET6 : AF_INET);
server.setTLSOptions(tlsOptions); server.setTLSOptions(tlsOptions);
server.setOnConnectionCallback( server.setOnConnectionCallback(