Browse Source

Provide ability to bind WebSocketServer to specifc address

Added 'bindAddress' option to structs rtc::WebSocketServer::Configuration  and rtcWsServerConfiguration (c-api)
Tim Schneider 2 years ago
parent
commit
43edfa669b

+ 1 - 0
include/rtc/rtc.h

@@ -398,6 +398,7 @@ typedef struct {
 	const char *certificatePemFile; // NULL for autogenerated certificate
 	const char *keyPemFile;         // NULL for autogenerated certificate
 	const char *keyPemPass;         // NULL if no pass
+	const char *bindAddress;        // NULL for IP_ANY_ADDR
 } rtcWsServerConfiguration;
 
 RTC_C_EXPORT int rtcCreateWebSocketServer(const rtcWsServerConfiguration *config,

+ 1 - 0
include/rtc/websocketserver.hpp

@@ -30,6 +30,7 @@ public:
 		optional<string> certificatePemFile;
 		optional<string> keyPemFile;
 		optional<string> keyPemPass;
+		optional<string> bindAddress;
 	};
 
 	WebSocketServer();

+ 1 - 0
src/capi.cpp

@@ -1451,6 +1451,7 @@ RTC_C_EXPORT int rtcCreateWebSocketServer(const rtcWsServerConfiguration *config
 		                           : nullopt;
 		c.keyPemFile = config->keyPemFile ? make_optional(string(config->keyPemFile)) : nullopt;
 		c.keyPemPass = config->keyPemPass ? make_optional(string(config->keyPemPass)) : nullopt;
+		c.bindAddress = config->bindAddress ? make_optional(string(config->bindAddress)) : nullopt;
 		auto webSocketServer = std::make_shared<WebSocketServer>(std::move(c));
 		int wsserver = emplaceWebSocketServer(webSocketServer);
 

+ 4 - 4
src/impl/tcpserver.cpp

@@ -21,9 +21,9 @@
 
 namespace rtc::impl {
 
-TcpServer::TcpServer(uint16_t port) {
+TcpServer::TcpServer(uint16_t port, const char* bindAddress) {
 	PLOG_DEBUG << "Initializing TCP server";
-	listen(port);
+	listen(port, bindAddress);
 }
 
 TcpServer::~TcpServer() { close(); }
@@ -89,7 +89,7 @@ void TcpServer::close() {
 	}
 }
 
-void TcpServer::listen(uint16_t port) {
+void TcpServer::listen(uint16_t port, const char* bindAddress) {
 	PLOG_DEBUG << "Listening on port " << port;
 
 	struct addrinfo hints = {};
@@ -99,7 +99,7 @@ void TcpServer::listen(uint16_t port) {
 	hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
 
 	struct addrinfo *result = nullptr;
-	if (getaddrinfo(nullptr, std::to_string(port).c_str(), &hints, &result))
+	if (getaddrinfo(bindAddress, std::to_string(port).c_str(), &hints, &result))
 		throw std::runtime_error("Resolution failed for local address");
 
 	try {

+ 2 - 2
src/impl/tcpserver.hpp

@@ -21,7 +21,7 @@ namespace rtc::impl {
 
 class TcpServer final {
 public:
-	TcpServer(uint16_t port);
+	TcpServer(uint16_t port, const char* bindAddress = nullptr);
 	~TcpServer();
 
 	TcpServer(const TcpServer &other) = delete;
@@ -33,7 +33,7 @@ public:
 	uint16_t port() const { return mPort; }
 
 private:
-	void listen(uint16_t port);
+	void listen(uint16_t port, const char* bindAddress);
 
 	uint16_t mPort;
 	socket_t mSock = INVALID_SOCKET;

+ 6 - 2
src/impl/websocketserver.cpp

@@ -40,9 +40,13 @@ WebSocketServer::WebSocketServer(Configuration config_)
 			    "Either none or both certificate and key PEM files must be specified");
 		}
 	}
-
+	
+	const char* bindAddress = nullptr;
+	if(config.bindAddress){
+		bindAddress = config.bindAddress->data();
+	}
 	// Create TCP server
-	tcpServer = std::make_unique<TcpServer>(config.port);
+	tcpServer = std::make_unique<TcpServer>(config.port, bindAddress);
 
 	// Create server thread
 	mThread = std::thread(&WebSocketServer::runLoop, this);