mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
check I2CP message size
This commit is contained in:
parent
bc330ff0ea
commit
86e3b977e4
@ -354,40 +354,17 @@ namespace client
|
|||||||
|
|
||||||
void I2CPSession::SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len)
|
void I2CPSession::SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len)
|
||||||
{
|
{
|
||||||
if (len > I2CP_MAX_MESSAGE_LENGTH)
|
auto l = len + I2CP_HEADER_SIZE;
|
||||||
|
if (l > I2CP_MAX_MESSAGE_LENGTH)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "I2CP: Message to send is too long ", len);
|
LogPrint (eLogError, "I2CP: Message to send is too long ", l);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto socket = m_Socket;
|
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
|
||||||
if (socket)
|
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len);
|
||||||
{
|
buf[I2CP_HEADER_TYPE_OFFSET] = type;
|
||||||
auto l = len + I2CP_HEADER_SIZE;
|
memcpy (buf + I2CP_HEADER_SIZE, payload, len);
|
||||||
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
|
SendBuffer (buf, l);
|
||||||
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len);
|
|
||||||
buf[I2CP_HEADER_TYPE_OFFSET] = type;
|
|
||||||
memcpy (buf + I2CP_HEADER_SIZE, payload, len);
|
|
||||||
if (m_IsSending)
|
|
||||||
{
|
|
||||||
if (!m_SendQueue)
|
|
||||||
m_SendQueue = std::make_shared<SendQueue::element_type> ();
|
|
||||||
if (m_SendQueue->size () > I2CP_MAX_SEND_QUEUE_SIZE)
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "I2CP: Queue size exceeds ", I2CP_MAX_SEND_QUEUE_SIZE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_SendQueue->push_back ({buf, l});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_IsSending = true;
|
|
||||||
boost::asio::async_write (*socket, boost::asio::buffer (m_SendBuffer, l), boost::asio::transfer_all (),
|
|
||||||
std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (),
|
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
LogPrint (eLogError, "I2CP: Can't write to the socket");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2CPSession::HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
void I2CPSession::HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
@ -420,6 +397,38 @@ namespace client
|
|||||||
|
|
||||||
HandleI2CPMessageSent (ecode, bytes_transferred);
|
HandleI2CPMessageSent (ecode, bytes_transferred);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void I2CPSession::SendBuffer (uint8_t * buf, size_t len)
|
||||||
|
{
|
||||||
|
auto socket = m_Socket;
|
||||||
|
if (socket)
|
||||||
|
{
|
||||||
|
if (m_IsSending)
|
||||||
|
{
|
||||||
|
auto sendQueue = m_SendQueue;
|
||||||
|
if (!sendQueue)
|
||||||
|
{
|
||||||
|
sendQueue = std::make_shared<SendQueue::element_type> ();
|
||||||
|
m_SendQueue = sendQueue;
|
||||||
|
}
|
||||||
|
else if (sendQueue->size () > I2CP_MAX_SEND_QUEUE_SIZE)
|
||||||
|
{
|
||||||
|
LogPrint (eLogError, "I2CP: Queue size exceeds ", I2CP_MAX_SEND_QUEUE_SIZE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sendQueue->push_back ({buf, len});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_IsSending = true;
|
||||||
|
boost::asio::async_write (*socket, boost::asio::buffer (buf, len), boost::asio::transfer_all (),
|
||||||
|
std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (),
|
||||||
|
std::placeholders::_1, std::placeholders::_2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LogPrint (eLogError, "I2CP: Can't write to the socket");
|
||||||
|
}
|
||||||
|
|
||||||
std::string I2CPSession::ExtractString (const uint8_t * buf, size_t len)
|
std::string I2CPSession::ExtractString (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
@ -850,38 +859,20 @@ namespace client
|
|||||||
void I2CPSession::SendMessagePayloadMessage (const uint8_t * payload, size_t len)
|
void I2CPSession::SendMessagePayloadMessage (const uint8_t * payload, size_t len)
|
||||||
{
|
{
|
||||||
// we don't use SendI2CPMessage to eliminate additional copy
|
// we don't use SendI2CPMessage to eliminate additional copy
|
||||||
auto socket = m_Socket;
|
auto l = len + 10 + I2CP_HEADER_SIZE;
|
||||||
if (socket)
|
if (l > I2CP_MAX_MESSAGE_LENGTH)
|
||||||
{
|
{
|
||||||
auto l = len + 10 + I2CP_HEADER_SIZE;
|
LogPrint (eLogError, "I2CP: Message to send is too long ", l);
|
||||||
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
|
return;
|
||||||
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10);
|
}
|
||||||
buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE;
|
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
|
||||||
htobe16buf (buf + I2CP_HEADER_SIZE, m_SessionID);
|
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10);
|
||||||
htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++);
|
buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE;
|
||||||
htobe32buf (buf + I2CP_HEADER_SIZE + 6, len);
|
htobe16buf (buf + I2CP_HEADER_SIZE, m_SessionID);
|
||||||
memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len);
|
htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++);
|
||||||
if (m_IsSending)
|
htobe32buf (buf + I2CP_HEADER_SIZE + 6, len);
|
||||||
{
|
memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len);
|
||||||
if (!m_SendQueue)
|
SendBuffer (buf, l);
|
||||||
m_SendQueue = std::make_shared<SendQueue::element_type> ();
|
|
||||||
if (m_SendQueue->size () > I2CP_MAX_SEND_QUEUE_SIZE)
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "I2CP: Queue size exceeds ", I2CP_MAX_SEND_QUEUE_SIZE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_SendQueue->push_back ({buf, l});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_IsSending = true;
|
|
||||||
boost::asio::async_write (*socket, boost::asio::buffer (m_SendBuffer, l), boost::asio::transfer_all (),
|
|
||||||
std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (),
|
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
LogPrint (eLogError, "I2CP: Can't write to the socket");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
I2CPServer::I2CPServer (const std::string& interface, int port, bool isSingleThread):
|
I2CPServer::I2CPServer (const std::string& interface, int port, bool isSingleThread):
|
||||||
|
@ -169,7 +169,8 @@ namespace client
|
|||||||
void HandleReceivedPayload (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleReceivedPayload (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleMessage ();
|
void HandleMessage ();
|
||||||
void Terminate ();
|
void Terminate ();
|
||||||
|
void SendBuffer (uint8_t * buf, size_t len);
|
||||||
|
|
||||||
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleI2CPMessageSentQueue (const boost::system::error_code& ecode, std::size_t bytes_transferred, SendQueue queue);
|
void HandleI2CPMessageSentQueue (const boost::system::error_code& ecode, std::size_t bytes_transferred, SendQueue queue);
|
||||||
std::string ExtractString (const uint8_t * buf, size_t len);
|
std::string ExtractString (const uint8_t * buf, size_t len);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user