Browse Source

send I2NP message instead plain buffer

pull/6/head
orignal 11 years ago
parent
commit
2893ec5a83
  1. 129
      NTCPSession.cpp
  2. 12
      NTCPSession.h

129
NTCPSession.cpp

@ -21,12 +21,13 @@ namespace i2p
namespace ntcp namespace ntcp
{ {
NTCPSession::NTCPSession (boost::asio::ip::tcp::socket& s, const i2p::data::RouterInfo * in_RemoteRouterInfo): NTCPSession::NTCPSession (boost::asio::ip::tcp::socket& s, const i2p::data::RouterInfo * in_RemoteRouterInfo):
m_Socket (s), m_IsEstablished (false), m_ReceiveBufferOffset (0) m_Socket (s), m_IsEstablished (false), m_ReceiveBufferOffset (0),
m_NextMessage (nullptr), m_DelayedMessage (nullptr)
{ {
if (in_RemoteRouterInfo) if (in_RemoteRouterInfo)
m_RemoteRouterInfo = *in_RemoteRouterInfo; m_RemoteRouterInfo = *in_RemoteRouterInfo;
} }
void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey) void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey)
{ {
CryptoPP::DH dh (elgp, elgg); CryptoPP::DH dh (elgp, elgg);
@ -51,15 +52,29 @@ namespace ntcp
{ {
m_IsEstablished = false; m_IsEstablished = false;
m_Socket.close (); m_Socket.close ();
if (m_DelayedMessage)
delete m_DelayedMessage;
// TODO: notify tunnels // TODO: notify tunnels
i2p::transports.RemoveNTCPSession (this); i2p::transports.RemoveNTCPSession (this);
delete this; delete this;
LogPrint ("NTCP session terminated");
} }
void NTCPSession::Connected () void NTCPSession::Connected ()
{ {
LogPrint ("NTCP session connected");
m_IsEstablished = true; m_IsEstablished = true;
i2p::transports.AddNTCPSession (this); i2p::transports.AddNTCPSession (this);
SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
if (m_DelayedMessage)
{
i2p::I2NPMessage * delayedMessage = m_DelayedMessage;
m_DelayedMessage = 0;
SendI2NPMessage (delayedMessage);
}
} }
void NTCPSession::ClientLogin () void NTCPSession::ClientLogin ()
@ -297,7 +312,7 @@ namespace ntcp
LogPrint ("Phase 4 sent: ", bytes_transferred); LogPrint ("Phase 4 sent: ", bytes_transferred);
Connected (); Connected ();
m_ReceiveBufferOffset = 0; m_ReceiveBufferOffset = 0;
m_DecryptedBufferOffset = 0; m_NextMessage = nullptr;
Receive (); Receive ();
} }
} }
@ -332,12 +347,9 @@ namespace ntcp
return; return;
} }
Connected (); Connected ();
SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
m_ReceiveBufferOffset = 0; m_ReceiveBufferOffset = 0;
m_DecryptedBufferOffset = 0; m_NextMessage = nullptr;
Receive (); Receive ();
} }
} }
@ -360,63 +372,58 @@ namespace ntcp
{ {
LogPrint ("Received: ", bytes_transferred); LogPrint ("Received: ", bytes_transferred);
m_ReceiveBufferOffset += bytes_transferred; m_ReceiveBufferOffset += bytes_transferred;
div_t d = div (m_ReceiveBufferOffset, 16);
if (d.quot) if (m_ReceiveBufferOffset >= 16)
{ {
int decryptedLen = d.quot*16; uint8_t * nextBlock = m_ReceiveBuffer;
DecryptReceived (m_ReceiveBuffer, decryptedLen); while (m_ReceiveBufferOffset >= 16)
if (d.rem) {
{ DecryptNextBlock (nextBlock); // 16 bytes
// we guarantee no overlap due if (d.quot) nextBlock += 16;
memcpy (m_ReceiveBuffer, m_ReceiveBuffer + decryptedLen, d.rem); m_ReceiveBufferOffset -= 16;
m_ReceiveBufferOffset = d.rem;
} }
else if (m_ReceiveBufferOffset > 0)
m_ReceiveBufferOffset = 0; memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
} }
Receive (); Receive ();
} }
} }
void NTCPSession::DecryptReceived (uint8_t * encrypted, int len) void NTCPSession::DecryptNextBlock (const uint8_t * encrypted) // 16 bytes
{ {
// here we might want to pass it to another thread if (!m_NextMessage) // new message, header expected
m_Decryption.ProcessData(m_DecryptedBuffer + m_DecryptedBufferOffset, encrypted, len);
m_DecryptedBufferOffset += len;
int size = m_DecryptedBufferOffset;
uint8_t * buf = m_DecryptedBuffer;
while (size > 2)
{ {
uint16_t dataSize = be16toh (*(uint16_t *)buf); m_NextMessage = i2p::NewI2NPMessage ();
int len = dataSize ? dataSize + 6 : 16; // 0 mean timestamp and size = 16 m_NextMessageOffset = 0;
if (dataSize) // regular message
{ uint8_t decrypted[16];
int rem = len % 16; m_Decryption.ProcessData (decrypted, encrypted, 16);
if (rem > 0) len += 16 - rem; uint16_t dataSize = be16toh (*(uint16_t *)decrypted);
if (dataSize)
{
// new message
memcpy (m_NextMessage->buf, decrypted + 2, 14);
m_NextMessageOffset += 14;
m_NextMessage->len = dataSize;
} }
if (len > size) break; else
HandleNextMessage (buf, len, dataSize); // timestamp
buf += len; size -= len; LogPrint ("Timestamp");
} }
else // message continues
if (buf != m_DecryptedBuffer) {
{ m_Decryption.ProcessData (m_NextMessage->buf + m_NextMessageOffset, encrypted, 16);
if (size > 0) m_NextMessageOffset += 16;
memmove (m_DecryptedBuffer, buf, size); }
m_DecryptedBufferOffset = size;
}
}
void NTCPSession::HandleNextMessage (uint8_t * buf, int len, int dataSize) if (m_NextMessageOffset >= m_NextMessage->len + 4) // +checksum
{ {
if (dataSize) // we have a complete I2NP message
i2p::HandleI2NPMessage (buf+2, dataSize); i2p::HandleI2NPMessage (m_NextMessage);
else m_NextMessage = 0;
LogPrint ("Timestamp"); }
} }
void NTCPSession::Send (const uint8_t * buf, int len, bool zeroSize) void NTCPSession::Send (const uint8_t * buf, int len, bool zeroSize)
{ {
@ -459,17 +466,17 @@ namespace ntcp
Send ((uint8_t *)&t, 4, true); Send ((uint8_t *)&t, 4, true);
} }
void NTCPSession::SendMessage (const uint8_t * buf, int len)
{
Send (buf, len);
}
void NTCPSession::SendI2NPMessage (I2NPMessage * msg) void NTCPSession::SendI2NPMessage (I2NPMessage * msg)
{ {
if (msg) if (msg)
{ {
Send (msg->buf, msg->len); if (m_IsEstablished)
DeleteI2NPMessage (msg); {
Send (msg->GetBuffer (), msg->GetLength ());
DeleteI2NPMessage (msg);
}
else
m_DelayedMessage = msg;
} }
} }

12
NTCPSession.h

@ -72,7 +72,6 @@ namespace ntcp
void ClientLogin (); void ClientLogin ();
void ServerLogin (); void ServerLogin ();
void SendMessage (const uint8_t * buf, int len);
void SendI2NPMessage (I2NPMessage * msg); void SendI2NPMessage (I2NPMessage * msg);
protected: protected:
@ -102,9 +101,8 @@ namespace ntcp
// common // common
void Receive (); void Receive ();
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void DecryptReceived (uint8_t * encrypted, int len); // len is multiple of 16 void DecryptNextBlock (const uint8_t * encrypted);
void HandleNextMessage (uint8_t * buf, int len, int dataSize);
void Send (const uint8_t * buf, int len, bool zeroSize = false); 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 HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint8_t * sentBuffer);
@ -129,9 +127,9 @@ namespace ntcp
uint8_t m_ReceiveBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2]; uint8_t m_ReceiveBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2];
int m_ReceiveBufferOffset; int m_ReceiveBufferOffset;
uint8_t m_DecryptedBuffer[i2p::NTCP_MAX_MESSAGE_SIZE*2]; i2p::I2NPMessage * m_NextMessage, * m_DelayedMessage;
int m_DecryptedBufferOffset; size_t m_NextMessageOffset;
std::mutex m_EncryptionMutex; std::mutex m_EncryptionMutex;
}; };

Loading…
Cancel
Save