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

Fix TCP leak in mediasoup v2 (#325)

上级 cb564e29
...@@ -17,6 +17,7 @@ namespace RTC ...@@ -17,6 +17,7 @@ namespace RTC
public: public:
TcpConnection(Listener* listener, size_t bufferSize); TcpConnection(Listener* listener, size_t bufferSize);
~TcpConnection() override;
public: public:
void Send(const uint8_t* data, size_t len); void Send(const uint8_t* data, size_t len);
......
...@@ -20,7 +20,7 @@ namespace RTC ...@@ -20,7 +20,7 @@ namespace RTC
public: public:
virtual void OnRtcTcpConnectionClosed( virtual void OnRtcTcpConnectionClosed(
RTC::TcpServer* tcpServer, RTC::TcpConnection* connection, bool isClosedByPeer) = 0; RTC::TcpServer* tcpServer, RTC::TcpConnection* connection) = 0;
}; };
public: public:
...@@ -44,8 +44,8 @@ namespace RTC ...@@ -44,8 +44,8 @@ namespace RTC
/* Pure virtual methods inherited from ::TcpServer. */ /* Pure virtual methods inherited from ::TcpServer. */
public: public:
void UserOnTcpConnectionAlloc(::TcpConnection** connection) override; void UserOnTcpConnectionAlloc(::TcpConnection** connection) override;
void UserOnNewTcpConnection(::TcpConnection* connection) override; bool UserOnNewTcpConnection(::TcpConnection* connection) override;
void UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer) override; void UserOnTcpConnectionClosed(::TcpConnection* connection) override;
private: private:
// Passed by argument. // Passed by argument.
......
...@@ -74,8 +74,7 @@ namespace RTC ...@@ -74,8 +74,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::TcpServer::Listener. */ /* Pure virtual methods inherited from RTC::TcpServer::Listener. */
public: public:
void OnRtcTcpConnectionClosed( void OnRtcTcpConnectionClosed(RTC::TcpServer* tcpServer, RTC::TcpConnection* connection) override;
RTC::TcpServer* tcpServer, RTC::TcpConnection* connection, bool isClosedByPeer) override;
/* Pure virtual methods inherited from RTC::TcpConnection::Listener. */ /* Pure virtual methods inherited from RTC::TcpConnection::Listener. */
public: public:
......
...@@ -18,7 +18,7 @@ public: ...@@ -18,7 +18,7 @@ public:
virtual ~Listener() = default; virtual ~Listener() = default;
public: public:
virtual void OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) = 0; virtual void OnTcpConnectionClosed(TcpConnection* connection) = 0;
}; };
public: public:
...@@ -52,6 +52,7 @@ public: ...@@ -52,6 +52,7 @@ public:
void Write(const uint8_t* data, size_t len); void Write(const uint8_t* data, size_t len);
void Write(const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2); void Write(const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2);
void Write(const std::string& data); void Write(const std::string& data);
void ErrorReceiving();
const struct sockaddr* GetLocalAddress() const; const struct sockaddr* GetLocalAddress() const;
int GetLocalFamily() const; int GetLocalFamily() const;
const std::string& GetLocalIP() const; const std::string& GetLocalIP() const;
......
...@@ -31,9 +31,9 @@ private: ...@@ -31,9 +31,9 @@ private:
/* Pure virtual methods that must be implemented by the subclass. */ /* Pure virtual methods that must be implemented by the subclass. */
protected: protected:
virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0; virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0;
virtual void UserOnNewTcpConnection(TcpConnection* connection) = 0; virtual bool UserOnNewTcpConnection(TcpConnection* connection) = 0;
virtual void UserOnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) = 0; virtual void UserOnTcpConnectionClosed(TcpConnection* connection) = 0;
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
public: public:
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
/* Methods inherited from TcpConnection::Listener. */ /* Methods inherited from TcpConnection::Listener. */
public: public:
void OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) override; void OnTcpConnectionClosed(TcpConnection* connection) override;
private: private:
// Allocated by this (may be passed by argument). // Allocated by this (may be passed by argument).
......
...@@ -16,6 +16,11 @@ namespace RTC ...@@ -16,6 +16,11 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
TcpConnection::~TcpConnection()
{
MS_TRACE();
}
void TcpConnection::UserOnTcpConnectionRead() void TcpConnection::UserOnTcpConnectionRead()
{ {
MS_TRACE(); MS_TRACE();
...@@ -125,7 +130,7 @@ namespace RTC ...@@ -125,7 +130,7 @@ namespace RTC
"connection"); "connection");
// Close the socket. // Close the socket.
Close(); ::TcpConnection::ErrorReceiving();
} }
} }
// The buffer is not full. // The buffer is not full.
...@@ -151,6 +156,6 @@ namespace RTC ...@@ -151,6 +156,6 @@ namespace RTC
uint8_t frameLen[2]; uint8_t frameLen[2];
Utils::Byte::Set2Bytes(frameLen, 0, len); Utils::Byte::Set2Bytes(frameLen, 0, len);
Write(frameLen, 2, data, len); ::TcpConnection::Write(frameLen, 2, data, len);
} }
} // namespace RTC } // namespace RTC
...@@ -248,20 +248,25 @@ namespace RTC ...@@ -248,20 +248,25 @@ namespace RTC
*connection = new RTC::TcpConnection(this->connListener, 65536); *connection = new RTC::TcpConnection(this->connListener, 65536);
} }
void TcpServer::UserOnNewTcpConnection(::TcpConnection* connection) bool TcpServer::UserOnNewTcpConnection(::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
// Allow just MaxTcpConnectionsPerServer. // Allow just MaxTcpConnectionsPerServer.
if (GetNumConnections() > MaxTcpConnectionsPerServer) if (GetNumConnections() >= MaxTcpConnectionsPerServer)
delete connection; {
MS_ERROR("cannot handle more than %zu connections", MaxTcpConnectionsPerServer);
return false;
}
return true;
} }
void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer) void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
this->listener->OnRtcTcpConnectionClosed( this->listener->OnRtcTcpConnectionClosed(this, dynamic_cast<RTC::TcpConnection*>(connection));
this, dynamic_cast<RTC::TcpConnection*>(connection), isClosedByPeer);
} }
} // namespace RTC } // namespace RTC
...@@ -974,14 +974,13 @@ namespace RTC ...@@ -974,14 +974,13 @@ namespace RTC
} }
void WebRtcTransport::OnRtcTcpConnectionClosed( void WebRtcTransport::OnRtcTcpConnectionClosed(
RTC::TcpServer* /*tcpServer*/, RTC::TcpConnection* connection, bool isClosedByPeer) RTC::TcpServer* /*tcpServer*/, RTC::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
if (isClosedByPeer) this->iceServer->RemoveTuple(&tuple);
this->iceServer->RemoveTuple(&tuple);
} }
void WebRtcTransport::OnPacketRecv(RTC::TcpConnection* connection, const uint8_t* data, size_t len) void WebRtcTransport::OnPacketRecv(RTC::TcpConnection* connection, const uint8_t* data, size_t len)
......
...@@ -121,9 +121,6 @@ void TcpConnection::Close() ...@@ -121,9 +121,6 @@ void TcpConnection::Close()
{ {
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose)); uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
} }
// Notify the listener.
this->listener->OnTcpConnectionClosed(this, this->isClosedByPeer);
} }
void TcpConnection::Dump() const void TcpConnection::Dump() const
...@@ -224,6 +221,8 @@ void TcpConnection::Write(const uint8_t* data, size_t len) ...@@ -224,6 +221,8 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
Close(); Close();
this->listener->OnTcpConnectionClosed(this);
return; return;
} }
...@@ -290,6 +289,8 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data ...@@ -290,6 +289,8 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
Close(); Close();
this->listener->OnTcpConnectionClosed(this);
return; return;
} }
...@@ -331,6 +332,15 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data ...@@ -331,6 +332,15 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
MS_ABORT("uv_write() failed: %s", uv_strerror(err)); MS_ABORT("uv_write() failed: %s", uv_strerror(err));
} }
void TcpConnection::ErrorReceiving()
{
MS_ERROR();
Close();
this->listener->OnTcpConnectionClosed(this);
}
bool TcpConnection::SetPeerAddress() bool TcpConnection::SetPeerAddress()
{ {
MS_TRACE(); MS_TRACE();
...@@ -407,6 +417,8 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/) ...@@ -407,6 +417,8 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
// Close server side of the connection. // Close server side of the connection.
Close(); Close();
this->listener->OnTcpConnectionClosed(this);
} }
// Some error. // Some error.
else else
...@@ -417,6 +429,8 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/) ...@@ -417,6 +429,8 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
// Close server side of the connection. // Close server side of the connection.
Close(); Close();
this->listener->OnTcpConnectionClosed(this);
} }
} }
...@@ -433,4 +447,6 @@ inline void TcpConnection::OnUvWriteError(int error) ...@@ -433,4 +447,6 @@ inline void TcpConnection::OnUvWriteError(int error)
MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error)); MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error));
Close(); Close();
this->listener->OnTcpConnectionClosed(this);
} }
...@@ -154,11 +154,8 @@ void TcpServer::Close() ...@@ -154,11 +154,8 @@ void TcpServer::Close()
MS_DEBUG_DEV("closing %zu active connections", this->connections.size()); MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto it = this->connections.begin(); it != this->connections.end();) for (auto* connection : this->connections)
{ {
auto* connection = *it;
it = this->connections.erase(it);
delete connection; delete connection;
} }
...@@ -243,38 +240,38 @@ inline void TcpServer::OnUvConnection(int status) ...@@ -243,38 +240,38 @@ inline void TcpServer::OnUvConnection(int status)
if (err != 0) if (err != 0)
MS_ABORT("uv_accept() failed: %s", uv_strerror(err)); MS_ABORT("uv_accept() failed: %s", uv_strerror(err));
// Insert the TcpConnection in the set.
this->connections.insert(connection);
// Start receiving data. // Start receiving data.
try try
{ {
// NOTE: This may throw.
connection->Start(); connection->Start();
// Notify the subclass.
UserOnNewTcpConnection(connection);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
MS_ERROR("cannot run the TCP connection, closing the connection: %s", error.what());
delete connection; delete connection;
return;
} }
// Notify the subclass and delete the connection if not accepted by the subclass.
if (UserOnNewTcpConnection(connection))
this->connections.insert(connection);
else
delete connection;
} }
void TcpServer::OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) void TcpServer::OnTcpConnectionClosed(TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
MS_DEBUG_DEV("TCP connection closed"); MS_DEBUG_DEV("TCP connection closed");
// Remove the TcpConnection from the set. // Remove the TcpConnection from the set.
size_t numErased = this->connections.erase(connection); this->connections.erase(connection);
// If the closed connection was not present in the set, do nothing else.
if (numErased == 0)
return;
// Notify the subclass. // Notify the subclass.
UserOnTcpConnectionClosed(connection, isClosedByPeer); UserOnTcpConnectionClosed(connection);
// Delete it.
delete connection;
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论