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

SctpAssociation.cpp: Initialize this->socket in the constructor (fixes #348)

上级 22346bdb
...@@ -96,132 +96,130 @@ namespace RTC ...@@ -96,132 +96,130 @@ namespace RTC
// Register ourselves in usrsctp. // Register ourselves in usrsctp.
usrsctp_register_address(static_cast<void*>(this)); usrsctp_register_address(static_cast<void*>(this));
DepUsrSCTP::IncreaseSctpAssociations(); int ret;
}
SctpAssociation::~SctpAssociation() this->socket = usrsctp_socket(
{ AF_CONN, SOCK_STREAM, IPPROTO_SCTP, onRecvSctpData, nullptr, 0, static_cast<void*>(this));
MS_TRACE();
if (this->socket) if (this->socket == nullptr)
{ MS_THROW_ERROR("usrsctp_socket() failed: %s", std::strerror(errno));
usrsctp_set_ulpinfo(this->socket, nullptr);
usrsctp_close(this->socket);
}
// Deregister ourselves from usrsctp. usrsctp_set_ulpinfo(this->socket, static_cast<void*>(this));
usrsctp_deregister_address(static_cast<void*>(this));
DepUsrSCTP::DecreaseSctpAssociations(); // Make the socket non-blocking.
ret = usrsctp_set_non_blocking(this->socket, 1);
delete[] this->messageBuffer; if (ret < 0)
} MS_THROW_ERROR("usrsctp_set_non_blocking() failed: %s", std::strerror(errno));
void SctpAssociation::TransportConnected() // Set SO_LINGER.
{ // This ensures that the usrsctp close call deletes the association. This
MS_TRACE(); // prevents usrsctp from calling the global send callback with references to
// this class as the address.
struct linger lingerOpt; // NOLINT(cppcoreguidelines-pro-type-member-init)
// Just run the SCTP stack if our state is 'new'. lingerOpt.l_onoff = 1;
if (this->state != SctpState::NEW) lingerOpt.l_linger = 0;
return;
try ret = usrsctp_setsockopt(this->socket, SOL_SOCKET, SO_LINGER, &lingerOpt, sizeof(lingerOpt));
{
int ret;
this->socket = usrsctp_socket( if (ret < 0)
AF_CONN, SOCK_STREAM, IPPROTO_SCTP, onRecvSctpData, nullptr, 0, static_cast<void*>(this)); MS_THROW_ERROR("usrsctp_setsockopt(SO_LINGER) failed: %s", std::strerror(errno));
if (this->socket == nullptr) // Set SCTP_ENABLE_STREAM_RESET.
MS_THROW_ERROR("usrsctp_socket() failed: %s", std::strerror(errno)); struct sctp_assoc_value av; // NOLINT(cppcoreguidelines-pro-type-member-init)
usrsctp_set_ulpinfo(this->socket, static_cast<void*>(this)); av.assoc_value =
SCTP_ENABLE_RESET_STREAM_REQ | SCTP_ENABLE_RESET_ASSOC_REQ | SCTP_ENABLE_CHANGE_ASSOC_REQ;
// Make the socket non-blocking. ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &av, sizeof(av));
ret = usrsctp_set_non_blocking(this->socket, 1);
if (ret < 0) if (ret < 0)
MS_THROW_ERROR("usrsctp_set_non_blocking() failed: %s", std::strerror(errno)); {
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_ENABLE_STREAM_RESET) failed: %s", std::strerror(errno));
}
// Set SO_LINGER. // Set SCTP_NODELAY.
// This ensures that the usrsctp close call deletes the association. This uint32_t noDelay = 1;
// prevents usrsctp from calling the global send callback with references to
// this class as the address.
struct linger lingerOpt; // NOLINT(cppcoreguidelines-pro-type-member-init)
lingerOpt.l_onoff = 1; ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_NODELAY, &noDelay, sizeof(noDelay));
lingerOpt.l_linger = 0;
ret = usrsctp_setsockopt(this->socket, SOL_SOCKET, SO_LINGER, &lingerOpt, sizeof(lingerOpt)); if (ret < 0)
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_NODELAY) failed: %s", std::strerror(errno));
if (ret < 0) // Enable events.
MS_THROW_ERROR("usrsctp_setsockopt(SO_LINGER) failed: %s", std::strerror(errno)); struct sctp_event event; // NOLINT(cppcoreguidelines-pro-type-member-init)
// Set SCTP_ENABLE_STREAM_RESET. std::memset(&event, 0, sizeof(event));
struct sctp_assoc_value av; // NOLINT(cppcoreguidelines-pro-type-member-init) event.se_on = 1;
av.assoc_value = for (size_t i{ 0 }; i < sizeof(EventTypes) / sizeof(uint16_t); ++i)
SCTP_ENABLE_RESET_STREAM_REQ | SCTP_ENABLE_RESET_ASSOC_REQ | SCTP_ENABLE_CHANGE_ASSOC_REQ; {
event.se_type = EventTypes[i];
ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &av, sizeof(av)); ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event));
if (ret < 0) if (ret < 0)
MS_THROW_ERROR( MS_THROW_ERROR("usrsctp_setsockopt(SCTP_EVENT) failed: %s", std::strerror(errno));
"usrsctp_setsockopt(SCTP_ENABLE_STREAM_RESET) failed: %s", std::strerror(errno)); }
// Set SCTP_NODELAY. // Init message.
uint32_t noDelay = 1; struct sctp_initmsg initmsg; // NOLINT(cppcoreguidelines-pro-type-member-init)
ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_NODELAY, &noDelay, sizeof(noDelay)); std::memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_num_ostreams = this->os;
initmsg.sinit_max_instreams = this->mis;
if (ret < 0) ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg));
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_NODELAY) failed: %s", std::strerror(errno));
// Enable events. if (ret < 0)
struct sctp_event event; // NOLINT(cppcoreguidelines-pro-type-member-init) MS_THROW_ERROR("usrsctp_setsockopt(SCTP_INITMSG) failed: %s", std::strerror(errno));
std::memset(&event, 0, sizeof(event)); // Server side.
event.se_on = 1; struct sockaddr_conn sconn; // NOLINT(cppcoreguidelines-pro-type-member-init)
for (size_t i{ 0 }; i < sizeof(EventTypes) / sizeof(uint16_t); ++i) std::memset(&sconn, 0, sizeof(sconn));
{ sconn.sconn_family = AF_CONN;
event.se_type = EventTypes[i]; sconn.sconn_port = htons(5000);
sconn.sconn_addr = static_cast<void*>(this);
#ifdef HAVE_SCONN_LEN
rconn.sconn_len = sizeof(sconn);
#endif
ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)); ret = usrsctp_bind(this->socket, reinterpret_cast<struct sockaddr*>(&sconn), sizeof(sconn));
if (ret < 0) if (ret < 0)
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_EVENT) failed: %s", std::strerror(errno)); MS_THROW_ERROR("usrsctp_bind() failed: %s", std::strerror(errno));
}
// Init message. DepUsrSCTP::IncreaseSctpAssociations();
struct sctp_initmsg initmsg; // NOLINT(cppcoreguidelines-pro-type-member-init) }
std::memset(&initmsg, 0, sizeof(initmsg)); SctpAssociation::~SctpAssociation()
initmsg.sinit_num_ostreams = this->os; {
initmsg.sinit_max_instreams = this->mis; MS_TRACE();
ret = usrsctp_setsockopt(this->socket, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg)); usrsctp_set_ulpinfo(this->socket, nullptr);
usrsctp_close(this->socket);
if (ret < 0) // Deregister ourselves from usrsctp.
MS_THROW_ERROR("usrsctp_setsockopt(SCTP_INITMSG) failed: %s", std::strerror(errno)); usrsctp_deregister_address(static_cast<void*>(this));
// Server side. DepUsrSCTP::DecreaseSctpAssociations();
struct sockaddr_conn sconn; // NOLINT(cppcoreguidelines-pro-type-member-init)
std::memset(&sconn, 0, sizeof(sconn)); delete[] this->messageBuffer;
sconn.sconn_family = AF_CONN; }
sconn.sconn_port = htons(5000);
sconn.sconn_addr = static_cast<void*>(this);
#ifdef HAVE_SCONN_LEN
rconn.sconn_len = sizeof(sconn);
#endif
ret = usrsctp_bind(this->socket, reinterpret_cast<struct sockaddr*>(&sconn), sizeof(sconn)); void SctpAssociation::TransportConnected()
{
MS_TRACE();
if (ret < 0) // Just run the SCTP stack if our state is 'new'.
MS_THROW_ERROR("usrsctp_bind() failed: %s", std::strerror(errno)); if (this->state != SctpState::NEW)
return;
// Client side. try
{
int ret;
struct sockaddr_conn rconn; // NOLINT(cppcoreguidelines-pro-type-member-init) struct sockaddr_conn rconn; // NOLINT(cppcoreguidelines-pro-type-member-init)
std::memset(&rconn, 0, sizeof(rconn)); std::memset(&rconn, 0, sizeof(rconn));
......
...@@ -107,6 +107,7 @@ namespace RTC ...@@ -107,6 +107,7 @@ namespace RTC
if (jsonIsDataChannelIt != data.end() && jsonIsDataChannelIt->is_boolean()) if (jsonIsDataChannelIt != data.end() && jsonIsDataChannelIt->is_boolean())
isDataChannel = jsonIsDataChannelIt->get<bool>(); isDataChannel = jsonIsDataChannelIt->get<bool>();
// This may throw.
this->sctpAssociation = this->sctpAssociation =
new RTC::SctpAssociation(this, os, mis, maxSctpMessageSize, isDataChannel); new RTC::SctpAssociation(this, os, mis, maxSctpMessageSize, isDataChannel);
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论