提交 99bc2578 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Add announced IPv4/IPv6 settings (fixes #53)

上级 1731ac94
......@@ -16,6 +16,8 @@ const VALID_WORKER_PARAMETERS =
'logTags',
'rtcListenIPv4',
'rtcListenIPv6',
'rtcAnnouncedListenIPv4',
'rtcAnnouncedListenIPv6',
'rtcMinPort',
'rtcMaxPort',
'dtlsCertificateFile',
......@@ -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;
......
......@@ -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.
......
......@@ -40,6 +40,7 @@ namespace RTC
// Others.
std::string foundation;
uint32_t priority;
int family = 0;
std::string ip;
Protocol protocol;
uint16_t port;
......
......@@ -26,17 +26,21 @@ public:
// 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:
......@@ -49,6 +53,8 @@ private:
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);
......
......@@ -42,6 +42,7 @@ public:
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();
......@@ -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()
{
......
......@@ -21,6 +21,7 @@ public:
virtual void Dump();
bool IsClosing();
const struct sockaddr* GetLocalAddress();
int GetLocalFamily();
const std::string& GetLocalIP();
uint16_t GetLocalPort();
size_t GetNumConnections();
......@@ -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
......@@ -36,6 +36,7 @@ public:
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();
......@@ -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()
{
......
......@@ -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))
......
......@@ -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()
......
......@@ -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();
......
......@@ -2,6 +2,7 @@
// #define MS_LOG_DEV
#include "RTC/IceCandidate.hpp"
#include "Settings.hpp"
namespace RTC
{
......@@ -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");
......@@ -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;
......
......@@ -57,14 +57,16 @@ void Settings::SetConfiguration(int argc, char* argv[])
struct option options[] =
{
{ "logLevel", optional_argument, nullptr, 'l' },
{ "logTag", optional_argument, nullptr, 't' },
{ "rtcListenIPv4", optional_argument, nullptr, '4' },
{ "rtcListenIPv6", optional_argument, nullptr, '6' },
{ "rtcMinPort", optional_argument, nullptr, 'm' },
{ "rtcMaxPort", optional_argument, nullptr, 'M' },
{ "dtlsCertificateFile", optional_argument, nullptr, 'c' },
{ "dtlsPrivateKeyFile", optional_argument, nullptr, 'p' },
{ "logLevel", optional_argument, nullptr, 'l' },
{ "logTag", optional_argument, nullptr, 't' },
{ "rtcListenIPv4", optional_argument, nullptr, '4' },
{ "rtcListenIPv6", optional_argument, nullptr, '6' },
{ "rtcAnnouncedListenIPv4", optional_argument, nullptr, '5' },
{ "rtcAnnouncedListenIPv6", optional_argument, nullptr, '7' },
{ "rtcMinPort", optional_argument, nullptr, 'm' },
{ "rtcMaxPort", optional_argument, nullptr, 'M' },
{ "dtlsCertificateFile", optional_argument, nullptr, 'c' },
{ "dtlsPrivateKeyFile", optional_argument, nullptr, 'p' },
{ 0, 0, 0, 0 }
};
......@@ -74,7 +76,7 @@ void Settings::SetConfiguration(int argc, char* argv[])
while ((c = getopt_long_only(argc, argv, "", options, &option_index)) != -1)
{
if (!optarg)
MS_THROW_ERROR("parameters without value not allowed");
MS_THROW_ERROR("unknown configuration parameter: %s", optarg);
switch (c)
{
......@@ -98,6 +100,16 @@ void Settings::SetConfiguration(int argc, char* argv[])
SetRtcListenIPv6(value_string);
break;
case '5':
value_string = std::string(optarg);
SetRtcAnnouncedListenIPv4(value_string);
break;
case '7':
value_string = std::string(optarg);
SetRtcAnnouncedListenIPv6(value_string);
break;
case 'm':
Settings::configuration.rtcMinPort = std::stoi(optarg);
break;
......@@ -143,6 +155,18 @@ void Settings::SetConfiguration(int argc, char* argv[])
if (!Settings::configuration.hasIPv4 && !Settings::configuration.hasIPv6)
MS_THROW_ERROR("at least rtcListenIPv4 or rtcListenIPv6 must be enabled");
// Clean RTP announced IPs if not available.
if (!Settings::configuration.hasIPv4)
{
Settings::configuration.rtcAnnouncedListenIPv4 = "";
Settings::configuration.hasAnnouncedIPv4 = false;
}
if (!Settings::configuration.hasIPv6)
{
Settings::configuration.rtcAnnouncedListenIPv6 = "";
Settings::configuration.hasAnnouncedIPv6 = false;
}
// Validate RTC ports.
Settings::SetRtcPorts();
......@@ -171,25 +195,33 @@ void Settings::PrintConfiguration()
MS_DEBUG_TAG(info, "<configuration>");
MS_DEBUG_TAG(info, "logLevel: \"%s\"", Settings::logLevel2String[Settings::configuration.logLevel].c_str());
MS_DEBUG_TAG(info, " logLevel : \"%s\"", Settings::logLevel2String[Settings::configuration.logLevel].c_str());
for (auto& tag : log_tags)
{
MS_DEBUG_TAG(info, "logTag: \"%s\"", tag.c_str());
MS_DEBUG_TAG(info, " logTag : \"%s\"", tag.c_str());
}
if (Settings::configuration.hasIPv4)
MS_DEBUG_TAG(info, "rtcListenIPv4: \"%s\"", Settings::configuration.rtcListenIPv4.c_str());
MS_DEBUG_TAG(info, " rtcListenIPv4 : \"%s\"", Settings::configuration.rtcListenIPv4.c_str());
else
MS_DEBUG_TAG(info, "rtcListenIPv4: (unavailable)");
MS_DEBUG_TAG(info, " rtcListenIPv4 : (unavailable)");
if (Settings::configuration.hasIPv6)
MS_DEBUG_TAG(info, "rtcListenIPv6: \"%s\"", Settings::configuration.rtcListenIPv6.c_str());
MS_DEBUG_TAG(info, " rtcListenIPv6 : \"%s\"", Settings::configuration.rtcListenIPv6.c_str());
else
MS_DEBUG_TAG(info, " rtcListenIPv6 : (unavailable)");
if (Settings::configuration.hasAnnouncedIPv4)
MS_DEBUG_TAG(info, " rtcAnnouncedListenIPv4 : \"%s\"", Settings::configuration.rtcAnnouncedListenIPv4.c_str());
else
MS_DEBUG_TAG(info, " rtcAnnouncedListenIPv4 : (unset)");
if (Settings::configuration.hasAnnouncedIPv6)
MS_DEBUG_TAG(info, " rtcAnnouncedListenIPv6 : \"%s\"", Settings::configuration.rtcAnnouncedListenIPv6.c_str());
else
MS_DEBUG_TAG(info, "rtcListenIPv6: (unavailable)");
MS_DEBUG_TAG(info, "rtcMinPort: %" PRIu16, Settings::configuration.rtcMinPort);
MS_DEBUG_TAG(info, "rtcMaxPort: %" PRIu16, Settings::configuration.rtcMaxPort);
MS_DEBUG_TAG(info, " rtcAnnouncedListenIPv6 : (unset)");
MS_DEBUG_TAG(info, " rtcMinPort : %" PRIu16, Settings::configuration.rtcMinPort);
MS_DEBUG_TAG(info, " rtcMaxPort : %" PRIu16, Settings::configuration.rtcMaxPort);
if (!Settings::configuration.dtlsCertificateFile.empty())
{
MS_DEBUG_TAG(info, "dtlsCertificateFile: \"%s\"", Settings::configuration.dtlsCertificateFile.c_str());
MS_DEBUG_TAG(info, "dtlsPrivateKeyFile: \"%s\"", Settings::configuration.dtlsPrivateKeyFile.c_str());
MS_DEBUG_TAG(info, " dtlsCertificateFile : \"%s\"", Settings::configuration.dtlsCertificateFile.c_str());
MS_DEBUG_TAG(info, " dtlsPrivateKeyFile : \"%s\"", Settings::configuration.dtlsPrivateKeyFile.c_str());
}
MS_DEBUG_TAG(info, "</configuration>");
......@@ -401,6 +433,44 @@ void Settings::SetRtcListenIPv6(const std::string &ip)
MS_THROW_ERROR("cannot bind on '%s' for rtcListenIPv6: %s", ip.c_str(), std::strerror(bind_errno));
}
void Settings::SetRtcAnnouncedListenIPv4(const std::string &ip)
{
MS_TRACE();
switch (Utils::IP::GetFamily(ip))
{
case AF_INET:
if (ip == "0.0.0.0")
MS_THROW_ERROR("rtcAnnouncedListenIPv4 cannot be '0.0.0.0'");
Settings::configuration.rtcAnnouncedListenIPv4 = ip;
Settings::configuration.hasAnnouncedIPv4 = true;
break;
case AF_INET6:
MS_THROW_ERROR("invalid IPv6 '%s' for rtcAnnouncedListenIPv4", ip.c_str());
default:
MS_THROW_ERROR("invalid value '%s' for rtcAnnouncedListenIPv4", ip.c_str());
}
}
void Settings::SetRtcAnnouncedListenIPv6(const std::string &ip)
{
MS_TRACE();
switch (Utils::IP::GetFamily(ip))
{
case AF_INET6:
if (ip == "::")
MS_THROW_ERROR("rtcAnnouncedListenIPv6 cannot be '::'");
Settings::configuration.rtcAnnouncedListenIPv6 = ip;
Settings::configuration.hasAnnouncedIPv6 = true;
break;
case AF_INET:
MS_THROW_ERROR("invalid IPv4 '%s' for rtcAnnouncedListenIPv6", ip.c_str());
default:
MS_THROW_ERROR("invalid value '%s' for rtcAnnouncedListenIPv6", ip.c_str());
}
}
void Settings::SetRtcPorts()
{
MS_TRACE();
......
......@@ -43,7 +43,7 @@ namespace Utils
*/
%%write data;
// Used by Ragel:
// Used by Ragel.
size_t cs;
const unsigned char* p;
const unsigned char* pe;
......
......@@ -162,27 +162,6 @@ void TcpServer::Dump()
MS_DUMP("</TcpServer>");
}
const struct sockaddr* TcpServer::GetLocalAddress()
{
MS_TRACE();
return (const struct sockaddr*)&this->localAddr;
}
const std::string& TcpServer::GetLocalIP()
{
MS_TRACE();
return this->localIP;
}
uint16_t TcpServer::GetLocalPort()
{
MS_TRACE();
return this->localPort;
}
bool TcpServer::SetLocalAddress()
{
MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论