mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
common code for padding and sending termination and RouterInfo messages
This commit is contained in:
parent
dc9562e430
commit
b57152cc25
@ -990,6 +990,19 @@ namespace transport
|
|||||||
// msgs get destroyed here
|
// msgs get destroyed here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NTCP2Session::EncryptAndSendNextBuffer (size_t payloadLen)
|
||||||
|
{
|
||||||
|
// encrypt
|
||||||
|
uint8_t nonce[12];
|
||||||
|
CreateNonce (m_SendSequenceNumber, nonce); m_SendSequenceNumber++;
|
||||||
|
i2p::crypto::AEADChaCha20Poly1305Encrypt ({std::make_pair (m_NextSendBuffer + 2, payloadLen)}, m_SendKey, nonce, m_NextSendBuffer + payloadLen + 2);
|
||||||
|
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::HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
void NTCP2Session::HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
m_IsSending = false;
|
m_IsSending = false;
|
||||||
@ -1056,26 +1069,17 @@ namespace transport
|
|||||||
{
|
{
|
||||||
if (!IsEstablished ()) return;
|
if (!IsEstablished ()) return;
|
||||||
auto riLen = i2p::context.GetRouterInfo ().GetBufferLen ();
|
auto riLen = i2p::context.GetRouterInfo ().GetBufferLen ();
|
||||||
int paddingSize = (riLen*NTCP2_MAX_PADDING_RATIO)/100;
|
size_t payloadLen = riLen + 4; // 3 bytes block header + 1 byte RI flag
|
||||||
size_t payloadLen = riLen + paddingSize + 7; // 7 = 2*3 bytes header + 1 byte RI flag
|
m_NextSendBuffer = new uint8_t[payloadLen + 16 + 2 + 64]; // up to 64 bytes padding
|
||||||
m_NextSendBuffer = new uint8_t[payloadLen + 16 + 2];
|
m_NextSendBuffer[2] = eNTCP2BlkRouterInfo;
|
||||||
uint8_t * payload = m_NextSendBuffer + 2;
|
htobe16buf (m_NextSendBuffer + 3, riLen + 1); // size
|
||||||
payload[0] = eNTCP2BlkRouterInfo;
|
m_NextSendBuffer[5] = 0; // flag
|
||||||
htobe16buf (payload + 1, riLen + 1); // size
|
memcpy (m_NextSendBuffer + 6, i2p::context.GetRouterInfo ().GetBuffer (), riLen);
|
||||||
payload[3] = 0; // flag
|
// padding block
|
||||||
memcpy (payload + 4, i2p::context.GetRouterInfo ().GetBuffer (), riLen);
|
auto paddingSize = CreatePaddingBlock (payloadLen, m_NextSendBuffer + 2 + payloadLen, 64);
|
||||||
payload[riLen + 4] = eNTCP2BlkPadding;
|
payloadLen += paddingSize;
|
||||||
htobe16buf (payload + riLen + 5, paddingSize);
|
// encrypt and send
|
||||||
memset (payload + riLen + 7, 0, paddingSize);
|
EncryptAndSendNextBuffer (payloadLen);
|
||||||
// encrypt
|
|
||||||
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)
|
||||||
@ -1089,15 +1093,8 @@ namespace transport
|
|||||||
m_NextSendBuffer[13] = (uint8_t)reason;
|
m_NextSendBuffer[13] = (uint8_t)reason;
|
||||||
// padding block
|
// padding block
|
||||||
auto paddingSize = CreatePaddingBlock (12, m_NextSendBuffer + 14, 19);
|
auto paddingSize = CreatePaddingBlock (12, m_NextSendBuffer + 14, 19);
|
||||||
// encrypt
|
// encrypt and send
|
||||||
uint8_t nonce[12];
|
EncryptAndSendNextBuffer (paddingSize + 12);
|
||||||
CreateNonce (m_SendSequenceNumber, nonce); m_SendSequenceNumber++;
|
|
||||||
i2p::crypto::AEADChaCha20Poly1305Encrypt ({std::make_pair (m_NextSendBuffer + 2, paddingSize + 12)}, m_SendKey, nonce, m_NextSendBuffer + paddingSize + 14);
|
|
||||||
SetNextSentFrameLength (paddingSize + 12 + 16, m_NextSendBuffer);
|
|
||||||
// send
|
|
||||||
m_IsSending = true;
|
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (m_NextSendBuffer, paddingSize + 14 + 16), boost::asio::transfer_all (),
|
|
||||||
std::bind(&NTCP2Session::HandleNextFrameSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NTCP2Session::SendTerminationAndTerminate (NTCP2TerminationReason reason)
|
void NTCP2Session::SendTerminationAndTerminate (NTCP2TerminationReason reason)
|
||||||
|
@ -177,6 +177,7 @@ namespace transport
|
|||||||
void SetNextSentFrameLength (size_t frameLen, uint8_t * lengthBuf);
|
void SetNextSentFrameLength (size_t frameLen, uint8_t * lengthBuf);
|
||||||
void SendI2NPMsgs (std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
void SendI2NPMsgs (std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
||||||
void HandleI2NPMsgsSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
void HandleI2NPMsgsSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs);
|
||||||
|
void EncryptAndSendNextBuffer (size_t payloadLen);
|
||||||
void HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
size_t CreatePaddingBlock (size_t msgLen, uint8_t * buf, size_t len);
|
size_t CreatePaddingBlock (size_t msgLen, uint8_t * buf, size_t len);
|
||||||
void SendQueue ();
|
void SendQueue ();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user