Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Iñaki Baz Castillo <[email protected]>
  • Loading branch information
mmasaki and ibc committed Dec 21, 2023
1 parent 7c3c820 commit b460184
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 39 deletions.
5 changes: 3 additions & 2 deletions node/src/PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ export type PlainTransportOptions<PlainTransportAppData extends AppData = AppDat
srtpCryptoSuite?: SrtpCryptoSuite;

/**
* Enable Multicast. Default false.
* Enable multicast for UDP in case listening IP is a multicast valid IP.
* Default false.
*/
enableMulticast?: boolean;

Expand Down Expand Up @@ -139,7 +140,7 @@ export type PlainTransportObserverEvents = TransportObserverEvents &
{
tuple: [TransportTuple];
rtcptuple: [TransportTuple];
sctpstatechange: [SctpState];
sctpstatechange: [SctpState];
};

type PlainTransportConstructorOptions<PlainTransportAppData> =
Expand Down
5 changes: 0 additions & 5 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,6 @@ export class Router<RouterAppData extends AppData = AppData>
};
}

if (enableMulticast)
{
logger.debug('createPlainTransport() | enableMulticast');
}

const transportId = generateUUIDv4();

/* Build Request. */
Expand Down
10 changes: 4 additions & 6 deletions node/src/tests/test-PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,11 @@ test('router.createPlainTransport() with non bindable IP rejects with Error', as
test('router.createPlainTransport() with enableMulticast succeeds', async () =>
{
// Use default cryptoSuite: 'AES_CM_128_HMAC_SHA1_80'.
const transport1 = await router.createPlainTransport(
await expect(router.createPlainTransport(
{
listenIp : '127.0.0.1',
enableMulticast: true
});

expect(typeof transport1.id).toBe('string');
listenIp : '127.0.0.1',
enableMulticast : true
})).not.toThrow();
}, 2000);

test('plainTransport.getStats() succeeds', async () =>
Expand Down
3 changes: 3 additions & 0 deletions rust/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ pub(crate) struct RouterCreatePlainTransportData {
sctp_send_buffer_size: u32,
enable_srtp: bool,
srtp_crypto_suite: SrtpCryptoSuite,
enable_multicast: bool,
is_data_channel: bool,
}

Expand All @@ -931,6 +932,7 @@ impl RouterCreatePlainTransportData {
sctp_send_buffer_size: plain_transport_options.sctp_send_buffer_size,
enable_srtp: plain_transport_options.enable_srtp,
srtp_crypto_suite: plain_transport_options.srtp_crypto_suite,
enable_multicast: plain_transport_options.enable_multicast,
is_data_channel: false,
}
}
Expand All @@ -955,6 +957,7 @@ impl RouterCreatePlainTransportData {
comedia: self.comedia,
enable_srtp: self.enable_srtp,
srtp_crypto_suite: Some(SrtpCryptoSuite::to_fbs(self.srtp_crypto_suite)),
enable_multicast: self.enable_multicast,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion rust/src/router/plain_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct PlainTransportOptions {
/// The SRTP crypto suite to be used if enableSrtp is set.
/// Default 'AesCm128HmacSha180'.
pub srtp_crypto_suite: SrtpCryptoSuite,
/// Enable Multicast.
/// Enable multicast for UDP in case listening IP is a multicast valid IP.
/// Default false.
pub enable_multicast: bool,
/// Custom application data.
Expand All @@ -95,6 +95,7 @@ impl PlainTransportOptions {
sctp_send_buffer_size: 262_144,
enable_srtp: false,
srtp_crypto_suite: SrtpCryptoSuite::default(),
enable_multicast: false,
app_data: AppData::default(),
}
}
Expand Down
26 changes: 26 additions & 0 deletions rust/tests/integration/plain_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,32 @@ fn create_succeeds() {
assert_eq!(transport_dump.sctp_state, transport2.sctp_state());
}
}

{
let transport = router
.create_plain_transport({
let mut plain_transport_options = PlainTransportOptions::new(ListenInfo {
protocol: Protocol::Udp,
ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
announced_ip: Some("4.4.4.4".parse().unwrap()),
port: None,
send_buffer_size: None,
recv_buffer_size: None,
});
plain_transport_options.enable_multicast = true;

plain_transport_options
})
.await
.expect("Failed to create Plain transport");

let router_dump = router.dump().await.expect("Failed to dump router");
assert_eq!(router_dump.transport_ids, {
let mut set = HashedSet::default();
set.insert(transport.id());
set
});
}
});
}

Expand Down
1 change: 0 additions & 1 deletion worker/include/RTC/PlainTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ namespace RTC
size_t srtpMasterLength{ 0 };
std::string srtpKeyBase64;
bool connectCalled{ false }; // Whether connect() was succesfully called.
bool multicast{ false };
};
} // namespace RTC

Expand Down
2 changes: 1 addition & 1 deletion worker/include/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace Utils

static void NormalizeIp(std::string& ip);

static bool IsMulticast(const struct sockaddr_storage* addr, const int& family);
static bool IsMulticast(const struct sockaddr_storage* addr, const int family);
};

class File
Expand Down
10 changes: 4 additions & 6 deletions worker/src/RTC/PlainTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,16 @@ namespace RTC
this->srtpKeyBase64 = Utils::String::Base64Encode(this->srtpKey);
}

this->multicast = options->enableMulticast();

try
{
// This may throw.
if (this->listenInfo.port != 0)
{
this->udpSocket = new RTC::UdpSocket(this, this->listenInfo.ip, this->listenInfo.port, this->multicast);
this->udpSocket = new RTC::UdpSocket(this, this->listenInfo.ip, this->listenInfo.port, options->enableMulticast());
}
else
{
this->udpSocket = new RTC::UdpSocket(this, this->listenInfo.ip, this->multicast);
this->udpSocket = new RTC::UdpSocket(this, this->listenInfo.ip, options->enableMulticast());
}

if (this->listenInfo.sendBufferSize != 0)
Expand All @@ -187,11 +185,11 @@ namespace RTC
if (this->rtcpListenInfo.port != 0)
{
this->rtcpUdpSocket =
new RTC::UdpSocket(this, this->rtcpListenInfo.ip, this->rtcpListenInfo.port, this->multicast);
new RTC::UdpSocket(this, this->rtcpListenInfo.ip, this->rtcpListenInfo.port, options->enableMulticast());
}
else
{
this->rtcpUdpSocket = new RTC::UdpSocket(this, this->rtcpListenInfo.ip, this->multicast);
this->rtcpUdpSocket = new RTC::UdpSocket(this, this->rtcpListenInfo.ip, options->enableMulticast());
}

if (this->rtcpListenInfo.sendBufferSize != 0)
Expand Down
10 changes: 6 additions & 4 deletions worker/src/RTC/PortManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ namespace RTC
case Transport::UDP:
{
#if defined(__linux__)
if (enableMulticast && Utils::IP::IsMulticast(&bindAddr, family))
if (enableMulticast && Utils::IP::IsMulticast(std::addressof(bindAddr), family))
{
flags |= UV_UDP_REUSEADDR;
MS_DEBUG_DEV("enabling UV_UDP_REUSEADDR");

flags |= UV_UDP_REUSEADDR;
}
#endif

Expand Down Expand Up @@ -481,10 +482,11 @@ namespace RTC
case Transport::UDP:
{
#if defined(__linux__)
if (enableMulticast && Utils::IP::IsMulticast(&bindAddr, family))
if (enableMulticast && Utils::IP::IsMulticast(std::addressof(bindAddr), family))
{
flags |= UV_UDP_REUSEADDR;
MS_DEBUG_DEV("enabling UV_UDP_REUSEADDR");

flags |= UV_UDP_REUSEADDR;
}
#endif

Expand Down
18 changes: 5 additions & 13 deletions worker/src/Utils/IP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace Utils
}
}

bool IP::IsMulticast(const struct sockaddr_storage* addr, const int& family)
bool IP::IsMulticast(const struct sockaddr_storage* addr, const int family)
{
MS_TRACE();

Expand All @@ -165,29 +165,21 @@ namespace Utils
case AF_INET:
{
uint32_t s_addr = ntohl((reinterpret_cast<const struct sockaddr_in*>(addr))->sin_addr.s_addr);
if (s_addr >= 0xe0000000 && s_addr <= 0xefffffff)
{
return true;
}
break;

return (s_addr >= 0xe0000000 && s_addr <= 0xefffffff);
}

case AF_INET6:
{
uint8_t _s6_addr = (reinterpret_cast<const struct sockaddr_in6*>(addr))->sin6_addr.s6_addr[0];
if (_s6_addr == 0xFF)
{
return true;
}
break;

return (_s6_addr == 0xFF);
}

default:
{
MS_THROW_TYPE_ERROR("invalid family");
}
}

return false;
}
} // namespace Utils

0 comments on commit b460184

Please sign in to comment.