Browse Source

don't create temporary NTCP send buffer

pull/6/head
orignal 11 years ago
parent
commit
18ec2e9ce8
  1. 6
      I2NPProtocol.cpp
  2. 61
      NTCPSession.cpp
  3. 6
      NTCPSession.h

6
I2NPProtocol.cpp

@ -19,8 +19,8 @@ namespace i2p @@ -19,8 +19,8 @@ namespace i2p
I2NPMessage * NewI2NPMessage ()
{
I2NPMessage * msg = new I2NPMessage;
msg->offset = 0;
msg->len = sizeof (I2NPHeader);
msg->offset = 2; // reserve 2 bytes for NTCP header, should reserve more for SSU in future
msg->len = sizeof (I2NPHeader) + 2;
return msg;
}
@ -42,7 +42,7 @@ namespace i2p @@ -42,7 +42,7 @@ namespace i2p
msgID++;
}
header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number
int len = msg->len - sizeof (I2NPHeader);
int len = msg->GetLength () - sizeof (I2NPHeader);
header->size = htobe16 (len);
uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, msg->GetPayload (), len);

61
NTCPSession.cpp

@ -397,19 +397,23 @@ namespace ntcp @@ -397,19 +397,23 @@ namespace ntcp
m_NextMessage = i2p::NewI2NPMessage ();
m_NextMessageOffset = 0;
uint8_t decrypted[16];
m_Decryption.ProcessData (decrypted, encrypted, 16);
uint16_t dataSize = be16toh (*(uint16_t *)decrypted);
m_Decryption.ProcessData (m_NextMessage->buf, encrypted, 16);
uint16_t dataSize = be16toh (*(uint16_t *)m_NextMessage->buf);
if (dataSize)
{
// new message
memcpy (m_NextMessage->buf, decrypted + 2, 14);
m_NextMessageOffset += 14;
m_NextMessage->len = dataSize;
m_NextMessageOffset += 16;
m_NextMessage->offset = 2; // size field
m_NextMessage->len = dataSize + 2;
}
else
{
// timestamp
LogPrint ("Timestamp");
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
return;
}
}
else // message continues
{
@ -421,18 +425,38 @@ namespace ntcp @@ -421,18 +425,38 @@ namespace ntcp
{
// we have a complete I2NP message
i2p::HandleI2NPMessage (m_NextMessage);
m_NextMessage = 0;
m_NextMessage = nullptr;
}
}
void NTCPSession::Send (const uint8_t * buf, int len, bool zeroSize)
void NTCPSession::Send (i2p::I2NPMessage * msg)
{
uint8_t * sendBuffer;
int len;
if (msg)
{
uint8_t * sendBuffer = new uint8_t[NTCP_MAX_MESSAGE_SIZE];
*((uint16_t *)sendBuffer) = zeroSize ? 0 :htobe16 (len);
// regular I2NP
if (msg->offset < 2)
{
LogPrint ("Malformed I2NP message");
i2p::DeleteI2NPMessage (msg);
}
sendBuffer = msg->GetBuffer () - 2;
len = msg->GetLength ();
*((uint16_t *)sendBuffer) = htobe16 (len);
}
else
{
// prepare timestamp
sendBuffer = m_TimeSyncBuffer;
len = 4;
*((uint16_t *)sendBuffer) = 0;
*((uint32_t *)(sendBuffer + 2)) = htobe32 (time (0));
}
int rem = (len + 6) % 16;
int padding = 0;
if (rem > 0) padding = 16 - rem;
memcpy (sendBuffer + 2, buf, len);
// TODO: fill padding
m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding);
@ -443,12 +467,13 @@ namespace ntcp @@ -443,12 +467,13 @@ namespace ntcp
}
boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, sendBuffer));
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));
}
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint8_t * sentBuffer)
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
{
delete sentBuffer;
if (msg)
i2p::DeleteI2NPMessage (msg);
if (ecode)
{
LogPrint ("Couldn't send msg: ", ecode.message ());
@ -462,8 +487,7 @@ namespace ntcp @@ -462,8 +487,7 @@ namespace ntcp
void NTCPSession::SendTimeSyncMessage ()
{
uint32_t t = htobe32 (time (0));
Send ((uint8_t *)&t, 4, true);
Send (nullptr);
}
void NTCPSession::SendI2NPMessage (I2NPMessage * msg)
@ -471,10 +495,7 @@ namespace ntcp @@ -471,10 +495,7 @@ namespace ntcp
if (msg)
{
if (m_IsEstablished)
{
Send (msg->GetBuffer (), msg->GetLength ());
DeleteI2NPMessage (msg);
}
Send (msg);
else
m_DelayedMessage = msg;
}

6
NTCPSession.h

@ -103,8 +103,8 @@ namespace ntcp @@ -103,8 +103,8 @@ namespace ntcp
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void DecryptNextBlock (const uint8_t * encrypted);
void Send (const uint8_t * buf, int len, bool zeroSize = false);
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint8_t * sentBuffer);
void Send (i2p::I2NPMessage * msg);
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg);
void SendTimeSyncMessage ();
@ -124,7 +124,7 @@ namespace ntcp @@ -124,7 +124,7 @@ namespace ntcp
NTCPPhase3 m_Phase3;
NTCPPhase4 m_Phase4;
uint8_t m_ReceiveBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2];
uint8_t m_ReceiveBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2], m_TimeSyncBuffer[16];
int m_ReceiveBufferOffset;
i2p::I2NPMessage * m_NextMessage, * m_DelayedMessage;

Loading…
Cancel
Save