Browse Source

eliminate extra copy of RouterInfo

pull/1277/head
orignal 6 years ago
parent
commit
8f6f95211e
  1. 20
      libi2pd/NTCP2.cpp

20
libi2pd/NTCP2.cpp

@ -980,9 +980,12 @@ namespace transport
// if it's long enough we add padding and MAC to it // if it's long enough we add padding and MAC to it
// create padding block // create padding block
auto paddingLen = CreatePaddingBlock (totalLen, buf + len, it->maxLen - it->len - 16); auto paddingLen = CreatePaddingBlock (totalLen, buf + len, it->maxLen - it->len - 16);
if (paddingLen)
{
encryptBufs.push_back (std::make_pair (buf + len, paddingLen)); encryptBufs.push_back (std::make_pair (buf + len, paddingLen));
len += paddingLen; len += paddingLen;
totalLen += paddingLen; totalLen += paddingLen;
}
macBuf = buf + len; macBuf = buf + len;
// allocate 16 bytes for MAC // allocate 16 bytes for MAC
len += 16; len += 16;
@ -998,6 +1001,7 @@ namespace transport
// crate padding block // crate padding block
auto paddingLen = CreatePaddingBlock (totalLen, m_NextSendBuffer, 287 - 16); auto paddingLen = CreatePaddingBlock (totalLen, m_NextSendBuffer, 287 - 16);
// and padding block to encrypt and send // and padding block to encrypt and send
if (paddingLen)
encryptBufs.push_back (std::make_pair (m_NextSendBuffer, paddingLen)); encryptBufs.push_back (std::make_pair (m_NextSendBuffer, paddingLen));
bufs.push_back (boost::asio::buffer (m_NextSendBuffer, paddingLen + 16)); bufs.push_back (boost::asio::buffer (m_NextSendBuffer, paddingLen + 16));
macBuf = m_NextSendBuffer + paddingLen; macBuf = m_NextSendBuffer + paddingLen;
@ -1068,16 +1072,24 @@ namespace transport
auto riLen = i2p::context.GetRouterInfo ().GetBufferLen (); auto riLen = i2p::context.GetRouterInfo ().GetBufferLen ();
int paddingSize = (riLen*NTCP2_MAX_PADDING_RATIO)/100; int paddingSize = (riLen*NTCP2_MAX_PADDING_RATIO)/100;
size_t payloadLen = riLen + paddingSize + 7; // 7 = 2*3 bytes header + 1 byte RI flag size_t payloadLen = riLen + paddingSize + 7; // 7 = 2*3 bytes header + 1 byte RI flag
uint8_t * payload = new uint8_t[payloadLen]; m_NextSendBuffer = new uint8_t[payloadLen + 16 + 2];
uint8_t * payload = m_NextSendBuffer + 2;
payload[0] = eNTCP2BlkRouterInfo; payload[0] = eNTCP2BlkRouterInfo;
htobe16buf (payload + 1, riLen + 1); // size htobe16buf (payload + 1, riLen + 1); // size
payload[3] = 0; // flag payload[3] = 0; // flag
memcpy (payload + 4, i2p::context.GetRouterInfo ().GetBuffer (), riLen); memcpy (payload + 4, i2p::context.GetRouterInfo ().GetBuffer (), riLen);
payload[riLen + 4] = eNTCP2BlkPadding; payload[riLen + 4] = eNTCP2BlkPadding;
htobe16buf (payload + riLen + 5, paddingSize); htobe16buf (payload + riLen + 5, paddingSize);
RAND_bytes (payload + riLen + 7, paddingSize); memset (payload + riLen + 7, 0, paddingSize);
SendNextFrame (payload, payloadLen); // encrypt
delete[] payload; uint8_t nonce[12];
CreateNonce (m_SendSequenceNumber, nonce); m_SendSequenceNumber++;
i2p::crypto::AEADChaCha20Poly1305Encrypt ({std::make_pair (payload, payloadLen)}, m_SendKey, nonce, payload + payloadLen);
SetNextSentFrameLength (payloadLen + 16, m_NextSendBuffer);
// send
m_IsSending = true;
boost::asio::async_write (m_Socket, boost::asio::buffer (m_NextSendBuffer, payloadLen + 16 + 2), boost::asio::transfer_all (),
std::bind(&NTCP2Session::HandleNextFrameSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
} }
void NTCP2Session::SendTermination (NTCP2TerminationReason reason) void NTCP2Session::SendTermination (NTCP2TerminationReason reason)

Loading…
Cancel
Save