(SocketServer) add ability to bind on an ipv6 address

This commit is contained in:
Benjamin Sergeant 2020-01-26 16:17:26 -08:00
parent 9d7807b25c
commit 3c27228585
10 changed files with 37 additions and 22 deletions

View File

@ -1,6 +1,10 @@
# 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

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

View File

@ -25,7 +25,8 @@ 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) HttpServer::HttpServer(int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily)
: SocketServer(port, host, backlog, maxConnections) : SocketServer(port, host, backlog, maxConnections, addressFamily)
, _connectedClientsCount(0) , _connectedClientsCount(0)
{ {
setDefaultConnectionCallback(); setDefaultConnectionCallback();

View File

@ -28,7 +28,8 @@ 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,15 +21,18 @@ 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,7 +62,7 @@ namespace ix
struct sockaddr_in server; // server address information struct sockaddr_in server; // server address information
// Get a socket for accepting connections. // Get a socket for accepting connections.
if ((_serverFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) if ((_serverFd = socket(_addressFamily, 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());
@ -80,17 +83,18 @@ namespace ix
} }
// Bind the socket to the server address. // Bind the socket to the server address.
server.sin_family = AF_INET; server.sin_family = _addressFamily;
server.sin_port = htons(_port); server.sin_port = htons(_port);
// Using INADDR_ANY trigger a pop-up box as binding to any address is detected if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) < 0)
// by the osx firewall. We need to codesign the binary with a self-signed cert {
// to allow that, but this is a bit of a pain. (this is what node or python would do). std::stringstream ss;
// ss << "SocketServer::listen() error calling inet_pton "
// Using INADDR_LOOPBACK also does not work ... while it should. << "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
// We default to 127.0.0.1 (localhost)
// Socket::closeSocket(_serverFd);
server.sin_addr.s_addr = inet_addr(_host.c_str()); 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)
{ {

View File

@ -36,7 +36,8 @@ 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();
@ -49,6 +50,7 @@ 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();
@ -69,6 +71,7 @@ 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

@ -23,8 +23,9 @@ namespace ix
const std::string& host, const std::string& host,
int backlog, int backlog,
size_t maxConnections, size_t maxConnections,
int handshakeTimeoutSecs) int handshakeTimeoutSecs,
: SocketServer(port, host, backlog, maxConnections) int addressFamily)
: SocketServer(port, host, backlog, maxConnections, addressFamily)
, _handshakeTimeoutSecs(handshakeTimeoutSecs) , _handshakeTimeoutSecs(handshakeTimeoutSecs)
, _enablePong(kDefaultEnablePong) , _enablePong(kDefaultEnablePong)
{ {

View File

@ -29,7 +29,8 @@ 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;

View File

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