Browse Source

check I2CP message size

pull/1583/head
orignal 4 years ago
parent
commit
86e3b977e4
  1. 93
      libi2pd_client/I2CP.cpp
  2. 1
      libi2pd_client/I2CP.h

93
libi2pd_client/I2CP.cpp

@ -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;
if (socket)
{
auto l = len + I2CP_HEADER_SIZE;
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer; uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len); htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len);
buf[I2CP_HEADER_TYPE_OFFSET] = type; buf[I2CP_HEADER_TYPE_OFFSET] = type;
memcpy (buf + I2CP_HEADER_SIZE, payload, len); memcpy (buf + I2CP_HEADER_SIZE, payload, len);
if (m_IsSending) SendBuffer (buf, l);
{
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)
@ -421,6 +398,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)
{ {
uint8_t l = buf[0]; uint8_t l = buf[0];
@ -850,10 +859,12 @@ 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;
if (socket)
{
auto l = len + 10 + I2CP_HEADER_SIZE; auto l = len + 10 + I2CP_HEADER_SIZE;
if (l > I2CP_MAX_MESSAGE_LENGTH)
{
LogPrint (eLogError, "I2CP: Message to send is too long ", l);
return;
}
uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer; uint8_t * buf = m_IsSending ? new uint8_t[l] : m_SendBuffer;
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10); htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10);
buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE; buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE;
@ -861,27 +872,7 @@ namespace client
htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++); htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++);
htobe32buf (buf + I2CP_HEADER_SIZE + 6, len); htobe32buf (buf + I2CP_HEADER_SIZE + 6, len);
memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len); memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len);
if (m_IsSending) SendBuffer (buf, l);
{
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");
} }
I2CPServer::I2CPServer (const std::string& interface, int port, bool isSingleThread): I2CPServer::I2CPServer (const std::string& interface, int port, bool isSingleThread):

1
libi2pd_client/I2CP.h

@ -169,6 +169,7 @@ 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);

Loading…
Cancel
Save