Skip to content

Commit

Permalink
Add announced IPv4/IPv6 settings (fixes #53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Mar 15, 2017
1 parent 1731ac9 commit 99bc257
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 53 deletions.
8 changes: 8 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const VALID_WORKER_PARAMETERS =
'logTags',
'rtcListenIPv4',
'rtcListenIPv6',
'rtcAnnouncedListenIPv4',
'rtcAnnouncedListenIPv6',
'rtcMinPort',
'rtcMaxPort',
'dtlsCertificateFile',
Expand Down Expand Up @@ -62,6 +64,12 @@ class Server extends EventEmitter
if (options.rtcListenIPv6 === null || options.rtcListenIPv6 === undefined)
delete options.rtcListenIPv6;

if (!options.rtcAnnouncedListenIPv4 || options.rtcListenIPv4 === false)
delete options.rtcAnnouncedListenIPv4;

if (!options.rtcAnnouncedListenIPv6 || options.rtcListenIPv6 === false)
delete options.rtcAnnouncedListenIPv6;

if (!check.greaterOrEqual(options.rtcMinPort, 1024))
options.rtcMinPort = 10000;

Expand Down
4 changes: 4 additions & 0 deletions lib/mediasoup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module.exports =
* values are a IPv4, `true` (auto-detect) and `false` (disabled).
* @param {string|boolean} [options.rtcListenIPv6=true] - IPv6 for RTC. Valid
* values are a IPv6, `true` (auto-detect) and `false` (disabled).
* @param {string} [options.rtcAnnouncedListenIPv4] - Announced IPv4 for RTC. Valid
* value is a IPv4.
* @param {string} [options.rtcAnnouncedListenIPv6] - Announced IPv6 for RTC. Valid
* value is a IPv6.
* @param {number} [options.rtcMinPort=10000] - Minimun RTC port.
* @param {number} [options.rtcMaxPort=59999] - Maximum RTC port.
* @param {string} [options.dtlsCertificateFile] - Path to DTLS certificate.
Expand Down
1 change: 1 addition & 0 deletions worker/include/RTC/IceCandidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace RTC
// Others.
std::string foundation;
uint32_t priority;
int family = 0;
std::string ip;
Protocol protocol;
uint16_t port;
Expand Down
16 changes: 11 additions & 5 deletions worker/include/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ class Settings
// Struct holding the configuration.
struct Configuration
{
LogLevel logLevel { LogLevel::LOG_DEBUG };
LogLevel logLevel { LogLevel::LOG_DEBUG };
struct LogTags logTags;
std::string rtcListenIPv4;
std::string rtcListenIPv6;
uint16_t rtcMinPort { 10000 };
uint16_t rtcMaxPort { 59999 };
std::string rtcAnnouncedListenIPv4;
std::string rtcAnnouncedListenIPv6;
uint16_t rtcMinPort { 10000 };
uint16_t rtcMaxPort { 59999 };
std::string dtlsCertificateFile;
std::string dtlsPrivateKeyFile;
// Private fields.
bool hasIPv4 { false };
bool hasIPv6 { false };
bool hasIPv4 { false };
bool hasIPv6 { false };
bool hasAnnouncedIPv4 { false };
bool hasAnnouncedIPv6 { false };
};

public:
Expand All @@ -49,6 +53,8 @@ class Settings
static void SetLogLevel(std::string &level);
static void SetRtcListenIPv4(const std::string &ip);
static void SetRtcListenIPv6(const std::string &ip);
static void SetRtcAnnouncedListenIPv4(const std::string &ip);
static void SetRtcAnnouncedListenIPv6(const std::string &ip);
static void SetRtcPorts();
static void SetDtlsCertificateAndPrivateKeyFiles();
static void SetLogTags(std::vector<std::string>& tags);
Expand Down
7 changes: 7 additions & 0 deletions worker/include/handles/TcpConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TcpConnection
void Write(const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2);
void Write(const std::string &data);
const struct sockaddr* GetLocalAddress();
int GetLocalFamily();
const std::string& GetLocalIP();
uint16_t GetLocalPort();
const struct sockaddr* GetPeerAddress();
Expand Down Expand Up @@ -114,6 +115,12 @@ const struct sockaddr* TcpConnection::GetLocalAddress()
return (const struct sockaddr*)this->localAddr;
}

inline
int TcpConnection::GetLocalFamily()
{
return ((const struct sockaddr*)&this->localAddr)->sa_family;
}

inline
const std::string& TcpConnection::GetLocalIP()
{
Expand Down
26 changes: 26 additions & 0 deletions worker/include/handles/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TcpServer : public TcpConnection::Listener
virtual void Dump();
bool IsClosing();
const struct sockaddr* GetLocalAddress();
int GetLocalFamily();
const std::string& GetLocalIP();
uint16_t GetLocalPort();
size_t GetNumConnections();
Expand Down Expand Up @@ -71,4 +72,29 @@ size_t TcpServer::GetNumConnections()
return this->connections.size();
}

inline
const struct sockaddr* TcpServer::GetLocalAddress()
{
return (const struct sockaddr*)&this->localAddr;
}

inline
int TcpServer::GetLocalFamily()
{
return ((const struct sockaddr*)&this->localAddr)->sa_family;
}

inline
const std::string& TcpServer::GetLocalIP()
{
return this->localIP;
}

inline
uint16_t TcpServer::GetLocalPort()
{
return this->localPort;
}


#endif
7 changes: 7 additions & 0 deletions worker/include/handles/UdpSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class UdpSocket
void Send(const uint8_t* data, size_t len, const std::string &ip, uint16_t port);
void Send(const std::string &data, const std::string &ip, uint16_t port);
const struct sockaddr* GetLocalAddress();
int GetLocalFamily();
const std::string& GetLocalIP();
uint16_t GetLocalPort();

Expand Down Expand Up @@ -86,6 +87,12 @@ const struct sockaddr* UdpSocket::GetLocalAddress()
return (const struct sockaddr*)&this->localAddr;
}

inline
int UdpSocket::GetLocalFamily()
{
return ((const struct sockaddr*)&this->localAddr)->sa_family;
}

inline
const std::string& UdpSocket::GetLocalIP()
{
Expand Down
2 changes: 1 addition & 1 deletion worker/src/DepLibSRTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void DepLibSRTP::ClassInit()

srtp_err_status_t err;

MS_DEBUG_TAG(info, "loaded libsrtp version: %s", srtp_get_version_string());
MS_DEBUG_TAG(info, "loaded libsrtp version: \"%s\"", srtp_get_version_string());

err = srtp_init();
if (DepLibSRTP::IsError(err))
Expand Down
2 changes: 1 addition & 1 deletion worker/src/DepLibUV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void DepLibUV::PrintVersion()
{
MS_TRACE();

MS_DEBUG_TAG(info, "loaded libuv version: %s", uv_version_string());
MS_DEBUG_TAG(info, "loaded libuv version: \"%s\"", uv_version_string());
}

void DepLibUV::RunLoop()
Expand Down
2 changes: 1 addition & 1 deletion worker/src/DepOpenSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void DepOpenSSL::ClassInit()
{
MS_TRACE();

MS_DEBUG_TAG(info, "loaded openssl version: %s", SSLeay_version(SSLEAY_VERSION));
MS_DEBUG_TAG(info, "loaded openssl version: \"%s\"", SSLeay_version(SSLEAY_VERSION));

// Initialize OpenSSL stuff.
SSL_load_error_strings();
Expand Down
70 changes: 66 additions & 4 deletions worker/src/RTC/IceCandidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// #define MS_LOG_DEV

#include "RTC/IceCandidate.hpp"
#include "Settings.hpp"

namespace RTC
{
Expand All @@ -10,26 +11,75 @@ namespace RTC
IceCandidate::IceCandidate(RTC::UdpSocket* udpSocket, uint32_t priority) :
foundation("udpcandidate"),
priority(priority),
ip(udpSocket->GetLocalIP()),
family(udpSocket->GetLocalFamily()),
protocol(Protocol::UDP),
port(udpSocket->GetLocalPort()),
type(CandidateType::HOST)
{}
{
switch (this->family)
{
case AF_INET:
{
if (Settings::configuration.hasAnnouncedIPv4)
this->ip = Settings::configuration.rtcAnnouncedListenIPv4;
else
this->ip = udpSocket->GetLocalIP();

break;
}

case AF_INET6:
{
if (Settings::configuration.hasAnnouncedIPv6)
this->ip = Settings::configuration.rtcAnnouncedListenIPv6;
else
this->ip = udpSocket->GetLocalIP();

break;
}
}
}

IceCandidate::IceCandidate(RTC::TcpServer* tcpServer, uint32_t priority) :
foundation("tcpcandidate"),
priority(priority),
ip(tcpServer->GetLocalIP()),
family(tcpServer->GetLocalFamily()),
protocol(Protocol::TCP),
port(tcpServer->GetLocalPort()),
type(CandidateType::HOST),
tcpType(TcpCandidateType::PASSIVE)
{}
{
switch (this->family)
{
case AF_INET:
{
if (Settings::configuration.hasAnnouncedIPv4)
this->ip = Settings::configuration.rtcAnnouncedListenIPv4;
else
this->ip = tcpServer->GetLocalIP();

break;
}

case AF_INET6:
{
if (Settings::configuration.hasAnnouncedIPv6)
this->ip = Settings::configuration.rtcAnnouncedListenIPv6;
else
this->ip = tcpServer->GetLocalIP();

break;
}
}
}

Json::Value IceCandidate::toJson()
{
static const Json::StaticString k_foundation("foundation");
static const Json::StaticString k_priority("priority");
static const Json::StaticString k_family("family");
static const Json::StaticString v_ipv4("ipv4");
static const Json::StaticString v_ipv6("ipv6");
static const Json::StaticString k_ip("ip");
static const Json::StaticString k_port("port");
static const Json::StaticString k_type("type");
Expand All @@ -43,6 +93,18 @@ namespace RTC
Json::Value json(Json::objectValue);

json[k_foundation] = this->foundation;

switch (this->family)
{
case AF_INET:
json[k_family] = v_ipv4;
break;

case AF_INET6:
json[k_family] = v_ipv6;
break;
}

json[k_priority] = (Json::UInt)this->priority;
json[k_ip] = this->ip;
json[k_port] = (Json::UInt)this->port;
Expand Down
Loading

0 comments on commit 99bc257

Please sign in to comment.