i2pd/NTCPSession.cpp

647 lines
20 KiB
C++
Raw Normal View History

2013-09-10 01:35:46 +00:00
#include <string.h>
#include <stdlib.h>
#include "I2PEndian.h"
2013-09-10 01:35:46 +00:00
#include <boost/bind.hpp>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "base64.h"
#include "Log.h"
2014-01-10 03:26:30 +00:00
#include "Timestamp.h"
2013-09-10 01:35:46 +00:00
#include "CryptoConst.h"
#include "I2NPProtocol.h"
#include "RouterContext.h"
#include "Transports.h"
#include "NTCPSession.h"
using namespace i2p::crypto;
namespace i2p
{
namespace ntcp
{
2013-12-22 16:29:57 +00:00
NTCPSession::NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo):
2014-09-21 00:10:34 +00:00
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
m_DHKeysPair (nullptr), m_RemoteRouterInfo (in_RemoteRouterInfo),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr),
m_NumSentBytes (0), m_NumReceivedBytes (0)
2013-09-10 01:35:46 +00:00
{
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
2014-09-12 02:15:20 +00:00
m_Establisher = new Establisher;
2013-09-10 01:35:46 +00:00
}
2013-10-27 15:23:15 +00:00
2014-04-04 17:30:13 +00:00
NTCPSession::~NTCPSession ()
{
2014-09-12 02:15:20 +00:00
delete m_Establisher;
2014-04-04 17:30:13 +00:00
delete m_DHKeysPair;
2014-08-27 14:02:23 +00:00
if (m_NextMessage)
i2p::DeleteI2NPMessage (m_NextMessage);
2014-09-22 17:28:46 +00:00
for (auto it :m_DelayedMessages)
i2p::DeleteI2NPMessage (it);
m_DelayedMessages.clear ();
2014-04-04 17:30:13 +00:00
}
2013-09-10 01:35:46 +00:00
void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey)
{
CryptoPP::DH dh (elgp, elgg);
2014-04-15 21:44:44 +00:00
uint8_t sharedKey[256];
if (!dh.Agree (sharedKey, m_DHKeysPair->privateKey, pubKey))
2013-09-10 01:35:46 +00:00
{
LogPrint ("Couldn't create shared key");
Terminate ();
return;
};
if (sharedKey[0] & 0x80)
2013-09-10 01:35:46 +00:00
{
aesKey[0] = 0;
memcpy (aesKey + 1, sharedKey, 31);
2013-09-10 01:35:46 +00:00
}
else if (sharedKey[0])
memcpy (aesKey, sharedKey, 32);
else
{
// find first non-zero byte
uint8_t * nonZero = sharedKey + 1;
while (!*nonZero)
{
nonZero++;
if (nonZero - sharedKey > 32)
{
LogPrint ("First 32 bytes of shared key is all zeros. Ignored");
return;
}
}
memcpy (aesKey, nonZero, 32);
}
2013-09-10 01:35:46 +00:00
}
void NTCPSession::Terminate ()
{
2013-10-23 02:43:29 +00:00
m_IsEstablished = false;
2013-09-10 01:35:46 +00:00
m_Socket.close ();
i2p::transports.RemoveNTCPSession (this);
int numDelayed = 0;
2013-11-29 12:52:09 +00:00
for (auto it :m_DelayedMessages)
{
// try to send them again
i2p::transports.SendMessage (m_RemoteRouterInfo.GetIdentHash (), it);
numDelayed++;
}
2013-11-29 12:52:09 +00:00
m_DelayedMessages.clear ();
if (numDelayed > 0)
LogPrint ("NTCP session ", numDelayed, " not sent");
2013-09-10 01:35:46 +00:00
// TODO: notify tunnels
2013-10-23 02:43:29 +00:00
delete this;
2013-10-27 15:23:15 +00:00
LogPrint ("NTCP session terminated");
2013-10-23 02:43:29 +00:00
}
void NTCPSession::Connected ()
{
2013-10-27 15:23:15 +00:00
LogPrint ("NTCP session connected");
2013-10-23 02:43:29 +00:00
m_IsEstablished = true;
2013-10-27 15:23:15 +00:00
2014-09-12 02:15:20 +00:00
delete m_Establisher;
m_Establisher = nullptr;
delete m_DHKeysPair;
m_DHKeysPair = nullptr;
2013-10-27 15:23:15 +00:00
SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
2013-11-29 12:52:09 +00:00
if (!m_DelayedMessages.empty ())
2013-10-27 15:23:15 +00:00
{
2013-11-29 12:52:09 +00:00
for (auto it :m_DelayedMessages)
SendI2NPMessage (it);
m_DelayedMessages.clear ();
2013-10-27 15:23:15 +00:00
}
2013-09-10 01:35:46 +00:00
}
void NTCPSession::ClientLogin ()
{
2014-09-21 00:10:34 +00:00
if (!m_DHKeysPair)
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
2013-09-10 01:35:46 +00:00
// send Phase1
2014-04-04 17:30:13 +00:00
const uint8_t * x = m_DHKeysPair->publicKey;
2014-09-12 02:15:20 +00:00
memcpy (m_Establisher->phase1.pubKey, x, 256);
CryptoPP::SHA256().CalculateDigest(m_Establisher->phase1.HXxorHI, x, 256);
2013-09-10 01:35:46 +00:00
const uint8_t * ident = m_RemoteRouterInfo.GetIdentHash ();
for (int i = 0; i < 32; i++)
2014-09-12 02:15:20 +00:00
m_Establisher->phase1.HXxorHI[i] ^= ident[i];
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase1Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void NTCPSession::ServerLogin ()
{
// receive Phase1
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase1Received, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void NTCPSession::HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("Couldn't send Phase 1 message: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 1 sent: ", bytes_transferred);
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase2Received, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
}
void NTCPSession::HandlePhase1Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("Phase 1 read error: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 1 received: ", bytes_transferred);
// verify ident
uint8_t digest[32];
2014-09-12 02:15:20 +00:00
CryptoPP::SHA256().CalculateDigest(digest, m_Establisher->phase1.pubKey, 256);
2013-09-10 01:35:46 +00:00
const uint8_t * ident = i2p::context.GetRouterInfo ().GetIdentHash ();
for (int i = 0; i < 32; i++)
{
2014-09-12 02:15:20 +00:00
if ((m_Establisher->phase1.HXxorHI[i] ^ ident[i]) != digest[i])
2013-09-10 01:35:46 +00:00
{
LogPrint ("Wrong ident");
Terminate ();
return;
}
}
SendPhase2 ();
}
}
void NTCPSession::SendPhase2 ()
{
2014-09-21 00:10:34 +00:00
if (!m_DHKeysPair)
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
2014-04-04 17:30:13 +00:00
const uint8_t * y = m_DHKeysPair->publicKey;
2014-09-12 02:15:20 +00:00
memcpy (m_Establisher->phase2.pubKey, y, 256);
2013-09-10 01:35:46 +00:00
uint8_t xy[512];
2014-09-12 02:15:20 +00:00
memcpy (xy, m_Establisher->phase1.pubKey, 256);
2013-09-10 01:35:46 +00:00
memcpy (xy + 256, y, 256);
2014-09-12 02:15:20 +00:00
CryptoPP::SHA256().CalculateDigest(m_Establisher->phase2.encrypted.hxy, xy, 512);
2014-01-10 03:26:30 +00:00
uint32_t tsB = htobe32 (i2p::util::GetSecondsSinceEpoch ());
2014-09-12 02:15:20 +00:00
m_Establisher->phase2.encrypted.timestamp = tsB;
2013-09-10 01:35:46 +00:00
// TODO: fill filler
uint8_t aesKey[32];
2014-09-12 02:15:20 +00:00
CreateAESKey (m_Establisher->phase1.pubKey, aesKey);
2014-05-07 02:30:09 +00:00
m_Encryption.SetKey (aesKey);
m_Encryption.SetIV (y + 240);
m_Decryption.SetKey (aesKey);
2014-09-12 02:15:20 +00:00
m_Decryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase2Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsB));
}
void NTCPSession::HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
{
if (ecode)
{
LogPrint ("Couldn't send Phase 2 message: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 2 sent: ", bytes_transferred);
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase3, sizeof (NTCPPhase3)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase3Received, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsB));
}
}
void NTCPSession::HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
2013-12-29 15:48:57 +00:00
LogPrint ("Phase 2 read error: ", ecode.message (), ". Wrong ident assumed");
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
{
GetRemoteRouterInfo ().SetUnreachable (true); // this RouterInfo is not valid
i2p::transports.ReuseDHKeysPair (m_DHKeysPair);
m_DHKeysPair = nullptr;
Terminate ();
}
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 2 received: ", bytes_transferred);
uint8_t aesKey[32];
2014-09-12 02:15:20 +00:00
CreateAESKey (m_Establisher->phase2.pubKey, aesKey);
2014-05-07 02:30:09 +00:00
m_Decryption.SetKey (aesKey);
2014-09-12 02:15:20 +00:00
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240);
2014-05-07 02:30:09 +00:00
m_Encryption.SetKey (aesKey);
2014-09-12 02:15:20 +00:00
m_Encryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
2013-09-10 01:35:46 +00:00
// verify
uint8_t xy[512], hxy[32];
2014-04-04 17:30:13 +00:00
memcpy (xy, m_DHKeysPair->publicKey, 256);
2014-09-12 02:15:20 +00:00
memcpy (xy + 256, m_Establisher->phase2.pubKey, 256);
2013-09-10 01:35:46 +00:00
CryptoPP::SHA256().CalculateDigest(hxy, xy, 512);
2014-09-12 02:15:20 +00:00
if (memcmp (hxy, m_Establisher->phase2.encrypted.hxy, 32))
2013-09-10 01:35:46 +00:00
{
LogPrint ("Incorrect hash");
2014-09-18 15:11:51 +00:00
i2p::transports.ReuseDHKeysPair (m_DHKeysPair);
m_DHKeysPair = nullptr;
2013-09-10 01:35:46 +00:00
Terminate ();
return ;
}
SendPhase3 ();
}
}
void NTCPSession::SendPhase3 ()
{
2014-09-12 02:15:20 +00:00
m_Establisher->phase3.size = htons (i2p::data::DEFAULT_IDENTITY_SIZE);
memcpy (&m_Establisher->phase3.ident, &i2p::context.GetRouterIdentity (), i2p::data::DEFAULT_IDENTITY_SIZE);
2014-01-10 03:26:30 +00:00
uint32_t tsA = htobe32 (i2p::util::GetSecondsSinceEpoch ());
2014-09-12 02:15:20 +00:00
m_Establisher->phase3.timestamp = tsA;
2013-09-10 01:35:46 +00:00
SignedData s;
2014-09-12 02:15:20 +00:00
memcpy (s.x, m_Establisher->phase1.pubKey, 256);
memcpy (s.y, m_Establisher->phase2.pubKey, 256);
2013-09-10 01:35:46 +00:00
memcpy (s.ident, m_RemoteRouterInfo.GetIdentHash (), 32);
s.tsA = tsA;
2014-09-12 02:15:20 +00:00
s.tsB = m_Establisher->phase2.encrypted.timestamp;
i2p::context.Sign ((uint8_t *)&s, sizeof (s), m_Establisher->phase3.signature);
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
m_Encryption.Encrypt((uint8_t *)&m_Establisher->phase3, sizeof(NTCPPhase3), (uint8_t *)&m_Establisher->phase3);
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase3, sizeof (NTCPPhase3)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase3Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsA));
}
void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)
{
if (ecode)
{
LogPrint ("Couldn't send Phase 3 message: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 3 sent: ", bytes_transferred);
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase4, sizeof (NTCPPhase4)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase4Received, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, tsA));
}
}
void NTCPSession::HandlePhase3Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
{
if (ecode)
{
LogPrint ("Phase 3 read error: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 3 received: ", bytes_transferred);
2014-09-12 02:15:20 +00:00
m_Decryption.Decrypt ((uint8_t *)&m_Establisher->phase3, sizeof(NTCPPhase3), (uint8_t *)&m_Establisher->phase3);
m_RemoteRouterInfo.SetRouterIdentity (m_Establisher->phase3.ident);
2013-09-10 01:35:46 +00:00
SignedData s;
2014-09-12 02:15:20 +00:00
memcpy (s.x, m_Establisher->phase1.pubKey, 256);
memcpy (s.y, m_Establisher->phase2.pubKey, 256);
2013-09-10 01:35:46 +00:00
memcpy (s.ident, i2p::context.GetRouterInfo ().GetIdentHash (), 32);
2014-09-12 02:15:20 +00:00
s.tsA = m_Establisher->phase3.timestamp;
2013-09-10 01:35:46 +00:00
s.tsB = tsB;
CryptoPP::DSA::PublicKey pubKey;
pubKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (m_RemoteRouterInfo.GetRouterIdentity ().signingKey, 128));
CryptoPP::DSA::Verifier verifier (pubKey);
2014-09-12 02:15:20 +00:00
if (!verifier.VerifyMessage ((uint8_t *)&s, sizeof(s), m_Establisher->phase3.signature, 40))
2013-09-10 01:35:46 +00:00
{
LogPrint ("signature verification failed");
Terminate ();
return;
}
SendPhase4 (tsB);
}
}
void NTCPSession::SendPhase4 (uint32_t tsB)
{
SignedData s;
2014-09-12 02:15:20 +00:00
memcpy (s.x, m_Establisher->phase1.pubKey, 256);
memcpy (s.y, m_Establisher->phase2.pubKey, 256);
2013-09-10 01:35:46 +00:00
memcpy (s.ident, m_RemoteRouterInfo.GetIdentHash (), 32);
2014-09-12 02:15:20 +00:00
s.tsA = m_Establisher->phase3.timestamp;
2013-09-10 01:35:46 +00:00
s.tsB = tsB;
2014-09-12 02:15:20 +00:00
i2p::context.Sign ((uint8_t *)&s, sizeof (s), m_Establisher->phase4.signature);
m_Encryption.Encrypt ((uint8_t *)&m_Establisher->phase4, sizeof(NTCPPhase4), (uint8_t *)&m_Establisher->phase4);
2013-09-10 01:35:46 +00:00
2014-09-12 02:15:20 +00:00
boost::asio::async_write (m_Socket, boost::asio::buffer (&m_Establisher->phase4, sizeof (NTCPPhase4)), boost::asio::transfer_all (),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandlePhase4Sent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void NTCPSession::HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("Couldn't send Phase 4 message: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 4 sent: ", bytes_transferred);
2013-10-23 02:43:29 +00:00
Connected ();
2013-09-10 01:35:46 +00:00
m_ReceiveBufferOffset = 0;
2013-10-27 15:23:15 +00:00
m_NextMessage = nullptr;
2013-09-10 01:35:46 +00:00
Receive ();
}
}
void NTCPSession::HandlePhase4Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)
{
if (ecode)
{
LogPrint ("Phase 4 read error: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
{
GetRemoteRouterInfo ().SetUnreachable (true); // this router doesn't like us
Terminate ();
}
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Phase 4 received: ", bytes_transferred);
2014-09-12 02:15:20 +00:00
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase4, sizeof(NTCPPhase4), (uint8_t *)&m_Establisher->phase4);
2013-09-10 01:35:46 +00:00
// verify signature
SignedData s;
2014-09-12 02:15:20 +00:00
memcpy (s.x, m_Establisher->phase1.pubKey, 256);
memcpy (s.y, m_Establisher->phase2.pubKey, 256);
2013-09-10 01:35:46 +00:00
memcpy (s.ident, i2p::context.GetRouterInfo ().GetIdentHash (), 32);
s.tsA = tsA;
2014-09-12 02:15:20 +00:00
s.tsB = m_Establisher->phase2.encrypted.timestamp;
2013-09-10 01:35:46 +00:00
CryptoPP::DSA::PublicKey pubKey;
pubKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (m_RemoteRouterInfo.GetRouterIdentity ().signingKey, 128));
CryptoPP::DSA::Verifier verifier (pubKey);
2014-09-12 02:15:20 +00:00
if (!verifier.VerifyMessage ((uint8_t *)&s, sizeof(s), m_Establisher->phase4.signature, 40))
2013-09-10 01:35:46 +00:00
{
LogPrint ("signature verification failed");
Terminate ();
return;
}
2013-10-23 02:43:29 +00:00
Connected ();
2013-10-27 15:23:15 +00:00
2013-09-10 01:35:46 +00:00
m_ReceiveBufferOffset = 0;
2013-10-27 15:23:15 +00:00
m_NextMessage = nullptr;
2013-09-10 01:35:46 +00:00
Receive ();
}
}
void NTCPSession::Receive ()
{
2014-09-12 02:15:20 +00:00
m_Socket.async_read_some (boost::asio::buffer(m_ReceiveBuffer + m_ReceiveBufferOffset, NTCP_BUFFER_SIZE - m_ReceiveBufferOffset),
2013-09-10 01:35:46 +00:00
boost::bind(&NTCPSession::HandleReceived, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("Read error: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
m_NumReceivedBytes += bytes_transferred;
2013-09-10 01:35:46 +00:00
m_ReceiveBufferOffset += bytes_transferred;
2013-10-27 15:23:15 +00:00
if (m_ReceiveBufferOffset >= 16)
2013-09-10 01:35:46 +00:00
{
2013-10-27 15:23:15 +00:00
uint8_t * nextBlock = m_ReceiveBuffer;
while (m_ReceiveBufferOffset >= 16)
{
2014-09-18 15:11:51 +00:00
if (!DecryptNextBlock (nextBlock)) // 16 bytes
{
Terminate ();
return;
}
2013-10-27 15:23:15 +00:00
nextBlock += 16;
m_ReceiveBufferOffset -= 16;
2013-09-10 01:35:46 +00:00
}
2013-10-27 15:23:15 +00:00
if (m_ReceiveBufferOffset > 0)
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
2013-09-10 01:35:46 +00:00
}
ScheduleTermination (); // reset termination timer
2013-09-10 01:35:46 +00:00
Receive ();
}
}
2014-09-18 15:11:51 +00:00
bool NTCPSession::DecryptNextBlock (const uint8_t * encrypted) // 16 bytes
2013-09-10 01:35:46 +00:00
{
2013-10-27 15:23:15 +00:00
if (!m_NextMessage) // new message, header expected
2013-09-10 01:35:46 +00:00
{
2013-10-27 15:23:15 +00:00
m_NextMessage = i2p::NewI2NPMessage ();
m_NextMessageOffset = 0;
2014-05-14 18:54:01 +00:00
m_Decryption.Decrypt (encrypted, m_NextMessage->buf);
uint16_t dataSize = be16toh (*(uint16_t *)m_NextMessage->buf);
2013-10-27 15:23:15 +00:00
if (dataSize)
{
// new message
2014-08-27 14:02:23 +00:00
if (dataSize > NTCP_MAX_MESSAGE_SIZE)
{
LogPrint ("NTCP data size ", dataSize, " exceeds max size");
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 15:11:51 +00:00
return false;
2014-08-27 14:02:23 +00:00
}
m_NextMessageOffset += 16;
m_NextMessage->offset = 2; // size field
m_NextMessage->len = dataSize + 2;
2013-09-10 01:35:46 +00:00
}
2013-10-27 15:23:15 +00:00
else
{
2013-10-27 15:23:15 +00:00
// timestamp
LogPrint ("Timestamp");
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 15:11:51 +00:00
return true;
}
2013-10-27 15:23:15 +00:00
}
else // message continues
{
2014-05-14 18:54:01 +00:00
m_Decryption.Decrypt (encrypted, m_NextMessage->buf + m_NextMessageOffset);
2013-10-27 15:23:15 +00:00
m_NextMessageOffset += 16;
}
2013-09-10 01:35:46 +00:00
2013-10-27 15:23:15 +00:00
if (m_NextMessageOffset >= m_NextMessage->len + 4) // +checksum
{
// we have a complete I2NP message
i2p::HandleI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
2014-09-18 15:11:51 +00:00
}
return true;
2013-10-27 15:23:15 +00:00
}
2013-09-10 01:35:46 +00:00
void NTCPSession::Send (i2p::I2NPMessage * msg)
2013-09-10 01:35:46 +00:00
{
uint8_t * sendBuffer;
int len;
if (msg)
{
// 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) & 0x0F; // %16
2013-09-10 01:35:46 +00:00
int padding = 0;
if (rem > 0) padding = 16 - rem;
// TODO: fill padding
2013-10-23 02:43:29 +00:00
m_Adler.CalculateDigest (sendBuffer + len + 2 + padding, sendBuffer, len + 2+ padding);
2013-09-10 01:35:46 +00:00
int l = len + padding + 6;
2014-05-07 02:30:09 +00:00
m_Encryption.Encrypt(sendBuffer, l, sendBuffer);
2013-09-10 01:35:46 +00:00
2013-10-23 02:43:29 +00:00
boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
2013-11-29 12:52:09 +00:00
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));
2013-09-10 01:35:46 +00:00
}
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
2013-09-10 01:35:46 +00:00
{
if (msg)
i2p::DeleteI2NPMessage (msg);
2013-09-10 01:35:46 +00:00
if (ecode)
{
LogPrint ("Couldn't send msg: ", ecode.message ());
2014-02-03 15:51:01 +00:00
// we shouldn't call Terminate () here, because HandleReceive takes care
// TODO: 'delete this' statement in Terminate () must be eliminated later
// Terminate ();
2013-09-10 01:35:46 +00:00
}
else
{
m_NumSentBytes += bytes_transferred;
ScheduleTermination (); // reset termination timer
2013-09-10 01:35:46 +00:00
}
}
void NTCPSession::SendTimeSyncMessage ()
{
Send (nullptr);
2013-09-10 01:35:46 +00:00
}
2013-10-23 02:43:29 +00:00
void NTCPSession::SendI2NPMessage (I2NPMessage * msg)
{
if (msg)
{
2013-10-27 15:23:15 +00:00
if (m_IsEstablished)
Send (msg);
2013-10-27 15:23:15 +00:00
else
2013-11-29 12:52:09 +00:00
m_DelayedMessages.push_back (msg);
}
}
void NTCPSession::ScheduleTermination ()
{
m_TerminationTimer.cancel ();
2014-04-08 01:40:28 +00:00
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(NTCP_TERMINATION_TIMEOUT));
2013-11-29 12:52:09 +00:00
m_TerminationTimer.async_wait (boost::bind (&NTCPSession::HandleTerminationTimer,
this, boost::asio::placeholders::error));
}
void NTCPSession::HandleTerminationTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
2014-04-08 01:40:28 +00:00
LogPrint ("No activity fo ", NTCP_TERMINATION_TIMEOUT, " seconds");
2014-09-22 21:38:21 +00:00
Terminate ();
2013-10-23 02:43:29 +00:00
}
}
2013-09-10 01:35:46 +00:00
2013-11-29 12:52:09 +00:00
2014-01-21 23:01:11 +00:00
NTCPClient::NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address,
2013-12-22 16:29:57 +00:00
int port, i2p::data::RouterInfo& in_RouterInfo):
NTCPSession (service, in_RouterInfo),
2014-01-21 21:07:16 +00:00
m_Endpoint (address, port)
2013-09-10 01:35:46 +00:00
{
Connect ();
}
void NTCPClient::Connect ()
{
2013-10-23 02:43:29 +00:00
LogPrint ("Connecting to ", m_Endpoint.address ().to_string (),":", m_Endpoint.port ());
2013-11-29 12:52:09 +00:00
GetSocket ().async_connect (m_Endpoint, boost::bind (&NTCPClient::HandleConnect,
2013-09-10 01:35:46 +00:00
this, boost::asio::placeholders::error));
}
void NTCPClient::HandleConnect (const boost::system::error_code& ecode)
{
if (ecode)
{
LogPrint ("Connect error: ", ecode.message ());
2014-09-22 17:28:46 +00:00
if (ecode != boost::asio::error::operation_aborted)
{
GetRemoteRouterInfo ().SetUnreachable (true);
Terminate ();
}
2013-09-10 01:35:46 +00:00
}
else
{
LogPrint ("Connected");
ClientLogin ();
}
}
2013-11-29 12:52:09 +00:00
void NTCPServerConnection::Connected ()
2013-09-10 01:35:46 +00:00
{
2013-11-29 12:52:09 +00:00
LogPrint ("NTCP server session connected");
SetIsEstablished (true);
i2p::transports.AddNTCPSession (this);
SendTimeSyncMessage ();
SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are
2013-09-10 01:35:46 +00:00
}
}
}