2013-09-10 01:35:46 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2017-01-20 03:00:02 +00:00
|
|
|
#include <future>
|
2016-06-28 00:00:00 +00:00
|
|
|
|
2014-01-09 19:56:07 +00:00
|
|
|
#include "I2PEndian.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Base.h"
|
2016-05-11 19:12:38 +00:00
|
|
|
#include "Crypto.h"
|
2013-09-10 01:35:46 +00:00
|
|
|
#include "Log.h"
|
2014-01-10 03:26:30 +00:00
|
|
|
#include "Timestamp.h"
|
2013-09-10 01:35:46 +00:00
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Transports.h"
|
2014-10-24 19:39:53 +00:00
|
|
|
#include "NetDb.h"
|
2013-09-10 01:35:46 +00:00
|
|
|
#include "NTCPSession.h"
|
2016-11-01 17:57:25 +00:00
|
|
|
#ifdef WITH_EVENTS
|
2016-10-20 13:12:15 +00:00
|
|
|
#include "Event.h"
|
2016-11-01 17:57:25 +00:00
|
|
|
#endif
|
2013-09-10 01:35:46 +00:00
|
|
|
|
|
|
|
using namespace i2p::crypto;
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-21 16:25:53 +00:00
|
|
|
namespace transport
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
2015-01-11 22:41:56 +00:00
|
|
|
NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter):
|
2016-08-04 14:26:50 +00:00
|
|
|
TransportSession (in_RemoteRouter, NTCP_TERMINATION_TIMEOUT),
|
|
|
|
m_Server (server), m_Socket (m_Server.GetService ()),
|
2016-08-17 14:58:57 +00:00
|
|
|
m_IsEstablished (false), m_IsTerminated (false),
|
2015-03-16 22:57:22 +00:00
|
|
|
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false)
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-01-21 23:59:50 +00:00
|
|
|
void NTCPSession::CreateAESKey (uint8_t * pubKey)
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
2014-04-15 21:44:44 +00:00
|
|
|
uint8_t sharedKey[256];
|
2017-01-20 03:00:02 +00:00
|
|
|
m_DHKeysPair->Agree (pubKey, sharedKey); // time consuming operation
|
2015-11-03 14:15:49 +00:00
|
|
|
|
2017-01-21 23:59:50 +00:00
|
|
|
i2p::crypto::AESKey aesKey;
|
2014-04-15 16:21:18 +00:00
|
|
|
if (sharedKey[0] & 0x80)
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
|
|
|
aesKey[0] = 0;
|
2014-04-15 16:21:18 +00:00
|
|
|
memcpy (aesKey + 1, sharedKey, 31);
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
2014-04-15 16:21:18 +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)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP: First 32 bytes of shared key is all zeros, ignored");
|
2014-04-15 16:21:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy (aesKey, nonZero, 32);
|
|
|
|
}
|
2017-01-21 23:59:50 +00:00
|
|
|
|
|
|
|
m_Decryption.SetKey (aesKey);
|
|
|
|
m_Encryption.SetKey (aesKey);
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 01:53:48 +00:00
|
|
|
void NTCPSession::Done ()
|
|
|
|
{
|
|
|
|
m_Server.GetService ().post (std::bind (&NTCPSession::Terminate, shared_from_this ()));
|
|
|
|
}
|
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
void NTCPSession::Terminate ()
|
|
|
|
{
|
2015-02-07 01:53:48 +00:00
|
|
|
if (!m_IsTerminated)
|
|
|
|
{
|
|
|
|
m_IsTerminated = true;
|
|
|
|
m_IsEstablished = false;
|
|
|
|
m_Socket.close ();
|
|
|
|
transports.PeerDisconnected (shared_from_this ());
|
|
|
|
m_Server.RemoveNTCPSession (shared_from_this ());
|
|
|
|
m_SendQueue.clear ();
|
2015-06-21 19:08:22 +00:00
|
|
|
m_NextMessage = nullptr;
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: session terminated");
|
2015-02-07 01:53:48 +00:00
|
|
|
}
|
2013-10-23 02:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::Connected ()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-09-17 15:13:25 +00:00
|
|
|
m_DHKeysPair = nullptr;
|
|
|
|
|
2016-03-19 02:53:03 +00:00
|
|
|
SendTimeSyncMessage ();
|
2015-01-13 03:53:35 +00:00
|
|
|
transports.PeerConnected (shared_from_this ());
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::ClientLogin ()
|
|
|
|
{
|
2014-09-21 00:10:34 +00:00
|
|
|
if (!m_DHKeysPair)
|
2014-10-21 16:25:53 +00:00
|
|
|
m_DHKeysPair = transports.GetNextDHKeysPair ();
|
2013-09-10 01:35:46 +00:00
|
|
|
// send Phase1
|
2015-11-03 14:15:49 +00:00
|
|
|
const uint8_t * x = m_DHKeysPair->GetPublicKey ();
|
2014-09-12 02:15:20 +00:00
|
|
|
memcpy (m_Establisher->phase1.pubKey, x, 256);
|
2015-11-03 14:15:49 +00:00
|
|
|
SHA256(x, 256, m_Establisher->phase1.HXxorHI);
|
|
|
|
const uint8_t * ident = m_RemoteIdentity->GetIdentHash ();
|
2013-09-10 01:35:46 +00:00
|
|
|
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 (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase1Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::ServerLogin ()
|
|
|
|
{
|
2016-12-31 15:51:42 +00:00
|
|
|
// receive Phase1
|
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase1, sizeof (NTCPPhase1)), boost::asio::transfer_all (),
|
|
|
|
std::bind(&NTCPSession::HandlePhase1Received, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: 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
|
|
|
|
{
|
2014-09-17 13:46:24 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(&m_Establisher->phase2, sizeof (NTCPPhase2)), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase2Received, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase1Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: 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
|
|
|
|
{
|
|
|
|
// verify ident
|
|
|
|
uint8_t digest[32];
|
2015-11-03 14:15:49 +00:00
|
|
|
SHA256(m_Establisher->phase1.pubKey, 256, digest);
|
|
|
|
const uint8_t * ident = i2p::context.GetIdentHash ();
|
2013-09-10 01:35:46 +00:00
|
|
|
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
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: phase 1 error: ident mismatch");
|
2013-09-10 01:35:46 +00:00
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-21 23:59:50 +00:00
|
|
|
|
|
|
|
// TODO: check for number of pending keys
|
|
|
|
auto s = shared_from_this ();
|
|
|
|
auto keyCreated = std::async (std::launch::async, [s] ()
|
|
|
|
{
|
|
|
|
if (!s->m_DHKeysPair)
|
|
|
|
s->m_DHKeysPair = transports.GetNextDHKeysPair ();
|
|
|
|
s->CreateAESKey (s->m_Establisher->phase1.pubKey);
|
|
|
|
}).share ();
|
|
|
|
m_Server.GetService ().post ([s, keyCreated]()
|
|
|
|
{
|
|
|
|
keyCreated.get ();
|
|
|
|
s->SendPhase2 ();
|
|
|
|
});
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::SendPhase2 ()
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
const uint8_t * y = m_DHKeysPair->GetPublicKey ();
|
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);
|
2015-11-03 14:15:49 +00:00
|
|
|
SHA256(xy, 512, m_Establisher->phase2.encrypted.hxy);
|
2014-01-10 03:26:30 +00:00
|
|
|
uint32_t tsB = htobe32 (i2p::util::GetSecondsSinceEpoch ());
|
2016-01-12 00:03:04 +00:00
|
|
|
memcpy (m_Establisher->phase2.encrypted.timestamp, &tsB, 4);
|
|
|
|
RAND_bytes (m_Establisher->phase2.encrypted.filler, 12);
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-05-07 02:30:09 +00:00
|
|
|
m_Encryption.SetIV (y + 240);
|
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 (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase2Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsB));
|
2013-09-10 01:35:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase2Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: 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
|
|
|
|
{
|
2014-11-25 17:33:51 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, NTCP_DEFAULT_PHASE3_SIZE), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase3Received, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2, tsB));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: Phase 2 read error: ", ecode.message (), ". Wrong ident assumed");
|
2014-09-22 17:28:46 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2014-10-24 19:39:53 +00:00
|
|
|
// this RI is not valid
|
2015-11-03 14:15:49 +00:00
|
|
|
i2p::data::netdb.SetUnreachable (GetRemoteIdentity ()->GetIdentHash (), true);
|
2014-10-21 16:25:53 +00:00
|
|
|
transports.ReuseDHKeysPair (m_DHKeysPair);
|
2014-09-22 17:28:46 +00:00
|
|
|
m_DHKeysPair = nullptr;
|
|
|
|
Terminate ();
|
|
|
|
}
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-20 03:00:02 +00:00
|
|
|
auto s = shared_from_this ();
|
|
|
|
// create AES key in separate thread
|
2017-01-21 23:59:50 +00:00
|
|
|
auto keyCreated = std::async (std::launch::async, [s] ()
|
2017-01-20 03:00:02 +00:00
|
|
|
{
|
2017-01-21 23:59:50 +00:00
|
|
|
s->CreateAESKey (s->m_Establisher->phase2.pubKey);
|
2017-01-20 03:00:02 +00:00
|
|
|
}).share (); // TODO: use move capture in C++ 14 instead shared_future
|
|
|
|
// let other operations execute while a key gets created
|
2017-01-21 23:59:50 +00:00
|
|
|
m_Server.GetService ().post ([s, keyCreated]()
|
2017-01-20 03:00:02 +00:00
|
|
|
{
|
2017-01-21 23:59:50 +00:00
|
|
|
keyCreated.get (); // we might wait if no more pending operations
|
|
|
|
s->HandlePhase2 ();
|
2017-01-20 03:00:02 +00:00
|
|
|
});
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 23:59:50 +00:00
|
|
|
void NTCPSession::HandlePhase2 ()
|
2017-01-20 03:00:02 +00:00
|
|
|
{
|
|
|
|
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240);
|
|
|
|
m_Encryption.SetIV (m_Establisher->phase1.HXxorHI + 16);
|
|
|
|
|
|
|
|
m_Decryption.Decrypt((uint8_t *)&m_Establisher->phase2.encrypted, sizeof(m_Establisher->phase2.encrypted), (uint8_t *)&m_Establisher->phase2.encrypted);
|
|
|
|
// verify
|
|
|
|
uint8_t xy[512];
|
|
|
|
memcpy (xy, m_DHKeysPair->GetPublicKey (), 256);
|
|
|
|
memcpy (xy + 256, m_Establisher->phase2.pubKey, 256);
|
|
|
|
uint8_t digest[32];
|
|
|
|
SHA256 (xy, 512, digest);
|
|
|
|
if (memcmp(m_Establisher->phase2.encrypted.hxy, digest, 32))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP: Phase 2 process error: incorrect hash");
|
|
|
|
transports.ReuseDHKeysPair (m_DHKeysPair);
|
|
|
|
m_DHKeysPair = nullptr;
|
|
|
|
Terminate ();
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
SendPhase3 ();
|
|
|
|
}
|
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
void NTCPSession::SendPhase3 ()
|
|
|
|
{
|
2017-01-08 14:07:54 +00:00
|
|
|
auto& keys = i2p::context.GetPrivateKeys ();
|
2014-11-25 15:59:29 +00:00
|
|
|
uint8_t * buf = m_ReceiveBuffer;
|
2015-11-03 14:15:49 +00:00
|
|
|
htobe16buf (buf, keys.GetPublic ()->GetFullLen ());
|
2014-11-25 15:59:29 +00:00
|
|
|
buf += 2;
|
2015-11-03 14:15:49 +00:00
|
|
|
buf += i2p::context.GetIdentity ()->ToBuffer (buf, NTCP_BUFFER_SIZE);
|
2014-01-10 03:26:30 +00:00
|
|
|
uint32_t tsA = htobe32 (i2p::util::GetSecondsSinceEpoch ());
|
2014-12-30 14:37:24 +00:00
|
|
|
htobuf32(buf,tsA);
|
2014-11-25 15:59:29 +00:00
|
|
|
buf += 4;
|
2015-11-03 14:15:49 +00:00
|
|
|
size_t signatureLen = keys.GetPublic ()->GetSignatureLen ();
|
2014-11-25 15:59:29 +00:00
|
|
|
size_t len = (buf - m_ReceiveBuffer) + signatureLen;
|
|
|
|
size_t paddingSize = len & 0x0F; // %16
|
|
|
|
if (paddingSize > 0)
|
|
|
|
{
|
|
|
|
paddingSize = 16 - paddingSize;
|
2016-02-05 15:40:23 +00:00
|
|
|
// fill padding with random data
|
|
|
|
RAND_bytes(buf, paddingSize);
|
2014-11-25 15:59:29 +00:00
|
|
|
buf += paddingSize;
|
|
|
|
len += paddingSize;
|
|
|
|
}
|
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
SignedData s;
|
2014-10-28 15:34:50 +00:00
|
|
|
s.Insert (m_Establisher->phase1.pubKey, 256); // x
|
|
|
|
s.Insert (m_Establisher->phase2.pubKey, 256); // y
|
2015-11-03 14:15:49 +00:00
|
|
|
s.Insert (m_RemoteIdentity->GetIdentHash (), 32); // ident
|
2014-10-28 15:47:28 +00:00
|
|
|
s.Insert (tsA); // tsA
|
2016-01-12 00:03:04 +00:00
|
|
|
s.Insert (m_Establisher->phase2.encrypted.timestamp, 4); // tsB
|
2014-11-25 15:59:29 +00:00
|
|
|
s.Sign (keys, buf);
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-11-25 15:59:29 +00:00
|
|
|
m_Encryption.Encrypt(m_ReceiveBuffer, len, m_ReceiveBuffer);
|
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, len), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase3Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, tsA));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: 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
|
|
|
|
{
|
2014-11-24 20:26:57 +00:00
|
|
|
// wait for phase4
|
2015-11-03 14:15:49 +00:00
|
|
|
auto signatureLen = m_RemoteIdentity->GetSignatureLen ();
|
2014-11-25 15:14:18 +00:00
|
|
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
|
|
|
if (paddingSize > 0) signatureLen += (16 - paddingSize);
|
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase4Received, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2, tsA));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase3Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: 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
|
|
|
|
{
|
2014-11-25 17:33:51 +00:00
|
|
|
m_Decryption.Decrypt (m_ReceiveBuffer, bytes_transferred, m_ReceiveBuffer);
|
|
|
|
uint8_t * buf = m_ReceiveBuffer;
|
2014-12-29 22:04:02 +00:00
|
|
|
uint16_t size = bufbe16toh (buf);
|
2017-02-01 20:20:03 +00:00
|
|
|
auto identity = std::make_shared<i2p::data::IdentityEx> (buf + 2, size);
|
|
|
|
if (m_Server.FindNTCPSession (identity->GetIdentHash ()))
|
2015-02-06 23:37:37 +00:00
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: session already exists");
|
2015-02-06 23:37:37 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
2017-02-01 20:20:03 +00:00
|
|
|
auto existing = i2p::data::netdb.FindRouter (identity->GetIdentHash ()); // check if exists already
|
|
|
|
SetRemoteIdentity (existing ? existing->GetRouterIdentity () : identity);
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
size_t expectedSize = size + 2/*size*/ + 4/*timestamp*/ + m_RemoteIdentity->GetSignatureLen ();
|
2014-11-25 17:33:51 +00:00
|
|
|
size_t paddingLen = expectedSize & 0x0F;
|
|
|
|
if (paddingLen) paddingLen = (16 - paddingLen);
|
|
|
|
if (expectedSize > NTCP_DEFAULT_PHASE3_SIZE)
|
|
|
|
{
|
|
|
|
// we need more bytes for Phase3
|
|
|
|
expectedSize += paddingLen;
|
2015-11-03 14:15:49 +00:00
|
|
|
boost::asio::async_read (m_Socket, boost::asio::buffer(m_ReceiveBuffer + NTCP_DEFAULT_PHASE3_SIZE, expectedSize - NTCP_DEFAULT_PHASE3_SIZE), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase3ExtraReceived, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2, tsB, paddingLen));
|
2014-11-25 17:33:51 +00:00
|
|
|
}
|
2014-11-25 19:29:06 +00:00
|
|
|
else
|
|
|
|
HandlePhase3 (tsB, paddingLen);
|
2014-11-25 17:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase3ExtraReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsB, size_t paddingLen)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
2016-01-28 02:54:42 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: Phase 3 extra read error: ", ecode.message ());
|
2014-11-25 17:33:51 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2013-09-10 01:35:46 +00:00
|
|
|
Terminate ();
|
2014-11-25 17:33:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Decryption.Decrypt (m_ReceiveBuffer + NTCP_DEFAULT_PHASE3_SIZE, bytes_transferred, m_ReceiveBuffer+ NTCP_DEFAULT_PHASE3_SIZE);
|
|
|
|
HandlePhase3 (tsB, paddingLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase3 (uint32_t tsB, size_t paddingLen)
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
uint8_t * buf = m_ReceiveBuffer + m_RemoteIdentity->GetFullLen () + 2 /*size*/;
|
2014-12-30 14:37:24 +00:00
|
|
|
uint32_t tsA = buf32toh(buf);
|
2014-11-25 17:33:51 +00:00
|
|
|
buf += 4;
|
|
|
|
buf += paddingLen;
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2016-01-27 00:02:06 +00:00
|
|
|
// check timestamp
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
uint32_t tsA1 = be32toh (tsA);
|
|
|
|
if (tsA1 < ts - NTCP_CLOCK_SKEW || tsA1 > ts + NTCP_CLOCK_SKEW)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP: Phase3 time difference ", ts - tsA1, " exceeds clock skew");
|
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check signature
|
2014-11-25 17:33:51 +00:00
|
|
|
SignedData s;
|
|
|
|
s.Insert (m_Establisher->phase1.pubKey, 256); // x
|
|
|
|
s.Insert (m_Establisher->phase2.pubKey, 256); // y
|
|
|
|
s.Insert (i2p::context.GetRouterInfo ().GetIdentHash (), 32); // ident
|
|
|
|
s.Insert (tsA); // tsA
|
|
|
|
s.Insert (tsB); // tsB
|
|
|
|
if (!s.Verify (m_RemoteIdentity, buf))
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: signature verification failed");
|
2014-11-25 17:33:51 +00:00
|
|
|
Terminate ();
|
|
|
|
return;
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
2014-11-25 17:33:51 +00:00
|
|
|
|
|
|
|
SendPhase4 (tsA, tsB);
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 15:35:35 +00:00
|
|
|
void NTCPSession::SendPhase4 (uint32_t tsA, uint32_t tsB)
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
|
|
|
SignedData s;
|
2014-10-28 15:34:50 +00:00
|
|
|
s.Insert (m_Establisher->phase1.pubKey, 256); // x
|
|
|
|
s.Insert (m_Establisher->phase2.pubKey, 256); // y
|
2015-11-03 14:15:49 +00:00
|
|
|
s.Insert (m_RemoteIdentity->GetIdentHash (), 32); // ident
|
2014-11-25 15:35:35 +00:00
|
|
|
s.Insert (tsA); // tsA
|
2014-10-28 15:34:50 +00:00
|
|
|
s.Insert (tsB); // tsB
|
2017-01-08 14:07:54 +00:00
|
|
|
auto& keys = i2p::context.GetPrivateKeys ();
|
2015-11-03 14:15:49 +00:00
|
|
|
auto signatureLen = keys.GetPublic ()->GetSignatureLen ();
|
2014-11-25 15:14:18 +00:00
|
|
|
s.Sign (keys, m_ReceiveBuffer);
|
|
|
|
size_t paddingSize = signatureLen & 0x0F; // %16
|
|
|
|
if (paddingSize > 0) signatureLen += (16 - paddingSize);
|
|
|
|
m_Encryption.Encrypt (m_ReceiveBuffer, signatureLen, m_ReceiveBuffer);
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-11-25 15:14:18 +00:00
|
|
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_ReceiveBuffer, signatureLen), boost::asio::transfer_all (),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandlePhase4Sent, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandlePhase4Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) bytes_transferred;
|
2013-09-10 01:35:46 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP: 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
|
|
|
|
{
|
2016-06-27 13:00:00 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: Server session from ", m_Socket.remote_endpoint (), " connected");
|
2015-01-11 22:41:56 +00:00
|
|
|
m_Server.AddNTCPSession (shared_from_this ());
|
2014-11-25 19:29:06 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: Phase 4 read error: ", ecode.message (), ". Check your clock");
|
2014-09-22 17:28:46 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2014-10-24 19:39:53 +00:00
|
|
|
// this router doesn't like us
|
2015-11-03 14:15:49 +00:00
|
|
|
i2p::data::netdb.SetUnreachable (GetRemoteIdentity ()->GetIdentHash (), true);
|
2014-09-22 17:28:46 +00:00
|
|
|
Terminate ();
|
|
|
|
}
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-25 15:14:18 +00:00
|
|
|
m_Decryption.Decrypt(m_ReceiveBuffer, bytes_transferred, m_ReceiveBuffer);
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2016-01-27 00:02:06 +00:00
|
|
|
// check timestamp
|
|
|
|
uint32_t tsB = bufbe32toh (m_Establisher->phase2.encrypted.timestamp);
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (tsB < ts - NTCP_CLOCK_SKEW || tsB > ts + NTCP_CLOCK_SKEW)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NTCP: Phase4 time difference ", ts - tsB, " exceeds clock skew");
|
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
// verify signature
|
|
|
|
SignedData s;
|
2014-10-28 15:34:50 +00:00
|
|
|
s.Insert (m_Establisher->phase1.pubKey, 256); // x
|
|
|
|
s.Insert (m_Establisher->phase2.pubKey, 256); // y
|
2015-11-03 14:15:49 +00:00
|
|
|
s.Insert (i2p::context.GetIdentHash (), 32); // ident
|
2014-10-28 15:34:50 +00:00
|
|
|
s.Insert (tsA); // tsA
|
2016-01-12 00:03:04 +00:00
|
|
|
s.Insert (m_Establisher->phase2.encrypted.timestamp, 4); // tsB
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2014-11-25 15:14:18 +00:00
|
|
|
if (!s.Verify (m_RemoteIdentity, m_ReceiveBuffer))
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: Phase 4 process error: signature verification failed");
|
2013-09-10 01:35:46 +00:00
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: session to ", m_Socket.remote_endpoint (), " connected");
|
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),
|
2014-11-25 21:30:15 +00:00
|
|
|
std::bind(&NTCPSession::HandleReceived, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2017-01-03 18:19:35 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
LogPrint (eLogDebug, "NTCP: Read error: ", ecode.message ());
|
2014-11-01 21:15:59 +00:00
|
|
|
//if (ecode != boost::asio::error::operation_aborted)
|
2016-02-11 00:00:00 +00:00
|
|
|
Terminate ();
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-20 21:12:36 +00:00
|
|
|
m_NumReceivedBytes += bytes_transferred;
|
2015-03-16 23:33:59 +00:00
|
|
|
i2p::transport::transports.UpdateReceivedBytes (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
|
|
|
{
|
2017-01-03 18:19:35 +00:00
|
|
|
// process received data
|
|
|
|
uint8_t * nextBlock = m_ReceiveBuffer;
|
|
|
|
while (m_ReceiveBufferOffset >= 16)
|
|
|
|
{
|
|
|
|
if (!DecryptNextBlock (nextBlock)) // 16 bytes
|
2014-09-18 15:11:51 +00:00
|
|
|
{
|
2017-01-03 18:19:35 +00:00
|
|
|
Terminate ();
|
|
|
|
return;
|
2015-02-08 16:52:03 +00:00
|
|
|
}
|
2017-01-03 18:19:35 +00:00
|
|
|
nextBlock += 16;
|
|
|
|
m_ReceiveBufferOffset -= 16;
|
|
|
|
}
|
|
|
|
if (m_ReceiveBufferOffset > 0)
|
|
|
|
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
|
|
|
|
}
|
2015-02-08 16:52:03 +00:00
|
|
|
|
2017-01-03 18:19:35 +00:00
|
|
|
// read and process more is available
|
|
|
|
boost::system::error_code ec;
|
|
|
|
size_t moreBytes = m_Socket.available(ec);
|
|
|
|
if (moreBytes && !ec)
|
|
|
|
{
|
|
|
|
uint8_t * buf = nullptr, * moreBuf = m_ReceiveBuffer;
|
|
|
|
if (moreBytes + m_ReceiveBufferOffset > NTCP_BUFFER_SIZE)
|
|
|
|
{
|
2017-01-04 22:25:30 +00:00
|
|
|
buf = new uint8_t[moreBytes + m_ReceiveBufferOffset + 16];
|
2017-01-03 18:19:35 +00:00
|
|
|
moreBuf = buf;
|
2017-01-04 22:25:30 +00:00
|
|
|
uint8_t rem = ((size_t)buf) & 0x0f;
|
|
|
|
if (rem) moreBuf += (16 - rem); // align 16
|
|
|
|
if (m_ReceiveBufferOffset)
|
|
|
|
memcpy (moreBuf, m_ReceiveBuffer, m_ReceiveBufferOffset);
|
2017-01-03 18:19:35 +00:00
|
|
|
}
|
|
|
|
moreBytes = m_Socket.read_some (boost::asio::buffer (moreBuf + m_ReceiveBufferOffset, moreBytes), ec);
|
2017-01-03 18:22:42 +00:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "NTCP: Read more bytes error: ", ec.message ());
|
|
|
|
delete[] buf;
|
|
|
|
Terminate ();
|
|
|
|
return;
|
|
|
|
}
|
2017-01-03 18:19:35 +00:00
|
|
|
m_ReceiveBufferOffset += moreBytes;
|
|
|
|
m_NumReceivedBytes += moreBytes;
|
|
|
|
i2p::transport::transports.UpdateReceivedBytes (moreBytes);
|
|
|
|
// process more data
|
|
|
|
uint8_t * nextBlock = moreBuf;
|
|
|
|
while (m_ReceiveBufferOffset >= 16)
|
|
|
|
{
|
|
|
|
if (!DecryptNextBlock (nextBlock)) // 16 bytes
|
|
|
|
{
|
|
|
|
delete[] buf;
|
|
|
|
Terminate ();
|
|
|
|
return;
|
2014-09-18 15:11:51 +00:00
|
|
|
}
|
2017-01-03 18:19:35 +00:00
|
|
|
nextBlock += 16;
|
|
|
|
m_ReceiveBufferOffset -= 16;
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
2017-01-03 18:19:35 +00:00
|
|
|
if (m_ReceiveBufferOffset > 0)
|
|
|
|
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset); // nextBlock points to memory inside buf
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
m_Handler.Flush ();
|
2014-01-04 03:25:22 +00:00
|
|
|
|
2016-08-17 14:58:57 +00:00
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
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
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
// decrypt header and extract length
|
2015-03-13 02:38:22 +00:00
|
|
|
uint8_t buf[16];
|
|
|
|
m_Decryption.Decrypt (encrypted, buf);
|
|
|
|
uint16_t dataSize = bufbe16toh (buf);
|
2013-10-27 15:23:15 +00:00
|
|
|
if (dataSize)
|
|
|
|
{
|
|
|
|
// new message
|
2016-11-22 00:45:29 +00:00
|
|
|
if (dataSize + 16U + 15U > NTCP_MAX_MESSAGE_SIZE - 2) // + 6 + padding
|
2014-08-27 14:02:23 +00:00
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: data size ", dataSize, " exceeds max size");
|
2014-09-18 15:11:51 +00:00
|
|
|
return false;
|
2014-08-27 14:02:23 +00:00
|
|
|
}
|
2016-11-22 00:45:29 +00:00
|
|
|
m_NextMessage = (dataSize + 16U + 15U) <= I2NP_MAX_SHORT_MESSAGE_SIZE - 2 ? NewI2NPShortMessage () : NewI2NPMessage ();
|
|
|
|
m_NextMessage->Align (16);
|
|
|
|
m_NextMessage->offset += 2; // size field
|
|
|
|
m_NextMessage->len = m_NextMessage->offset + dataSize;
|
|
|
|
memcpy (m_NextMessage->GetBuffer () - 2, buf, 16);
|
2015-03-13 02:38:22 +00:00
|
|
|
m_NextMessageOffset = 16;
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
2013-10-27 15:23:15 +00:00
|
|
|
else
|
2013-10-27 20:48:45 +00:00
|
|
|
{
|
2013-10-27 15:23:15 +00:00
|
|
|
// timestamp
|
2016-12-01 15:51:01 +00:00
|
|
|
int diff = (int)bufbe32toh (buf + 2) - (int)i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
LogPrint (eLogInfo, "NTCP: Timestamp. Time difference ", diff, " seconds");
|
2014-09-18 15:11:51 +00:00
|
|
|
return true;
|
2013-10-27 20:48:45 +00:00
|
|
|
}
|
2013-10-27 15:23:15 +00:00
|
|
|
}
|
|
|
|
else // message continues
|
|
|
|
{
|
2016-11-22 00:45:29 +00:00
|
|
|
m_Decryption.Decrypt (encrypted, m_NextMessage->GetBuffer () - 2 + m_NextMessageOffset);
|
2013-10-27 15:23:15 +00:00
|
|
|
m_NextMessageOffset += 16;
|
|
|
|
}
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2016-11-22 00:45:29 +00:00
|
|
|
if (m_NextMessageOffset >= m_NextMessage->GetLength () + 2 + 4) // +checksum
|
2013-10-27 15:23:15 +00:00
|
|
|
{
|
|
|
|
// we have a complete I2NP message
|
2015-11-03 14:15:49 +00:00
|
|
|
uint8_t checksum[4];
|
2016-11-22 00:45:29 +00:00
|
|
|
htobe32buf (checksum, adler32 (adler32 (0, Z_NULL, 0), m_NextMessage->GetBuffer () - 2, m_NextMessageOffset - 4));
|
|
|
|
if (!memcmp (m_NextMessage->GetBuffer () - 2 + m_NextMessageOffset - 4, checksum, 4))
|
2016-01-19 02:13:43 +00:00
|
|
|
{
|
|
|
|
if (!m_NextMessage->IsExpired ())
|
2016-11-01 14:26:40 +00:00
|
|
|
{
|
2016-11-01 17:57:25 +00:00
|
|
|
#ifdef WITH_EVENTS
|
2016-12-07 16:52:20 +00:00
|
|
|
QueueIntEvent("transport.recvmsg", GetIdentHashBase64(), 1);
|
2016-11-01 17:57:25 +00:00
|
|
|
#endif
|
2016-01-19 02:13:43 +00:00
|
|
|
m_Handler.PutNextMessage (m_NextMessage);
|
2016-11-01 14:26:40 +00:00
|
|
|
}
|
2016-01-19 02:13:43 +00:00
|
|
|
else
|
|
|
|
LogPrint (eLogInfo, "NTCP: message expired");
|
|
|
|
}
|
2015-06-14 14:37:15 +00:00
|
|
|
else
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP: Incorrect adler checksum of message, dropped");
|
2013-10-27 20:48:45 +00:00
|
|
|
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
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void NTCPSession::Send (std::shared_ptr<i2p::I2NPMessage> msg)
|
2015-01-21 03:35:27 +00:00
|
|
|
{
|
2015-01-28 00:12:27 +00:00
|
|
|
m_IsSending = true;
|
2015-01-21 03:35:27 +00:00
|
|
|
boost::asio::async_write (m_Socket, CreateMsgBuffer (msg), boost::asio::transfer_all (),
|
2015-06-17 14:47:26 +00:00
|
|
|
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, std::vector<std::shared_ptr<I2NPMessage> >{ msg }));
|
2015-01-21 03:35:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
boost::asio::const_buffers_1 NTCPSession::CreateMsgBuffer (std::shared_ptr<I2NPMessage> msg)
|
2013-09-10 01:35:46 +00:00
|
|
|
{
|
2013-10-27 20:48:45 +00:00
|
|
|
uint8_t * sendBuffer;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
// regular I2NP
|
|
|
|
if (msg->offset < 2)
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: Malformed I2NP message"); // TODO:
|
2013-10-27 20:48:45 +00:00
|
|
|
sendBuffer = msg->GetBuffer () - 2;
|
|
|
|
len = msg->GetLength ();
|
2014-12-30 14:37:24 +00:00
|
|
|
htobe16buf (sendBuffer, len);
|
2013-10-27 20:48:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// prepare timestamp
|
|
|
|
sendBuffer = m_TimeSyncBuffer;
|
|
|
|
len = 4;
|
2014-12-30 14:37:24 +00:00
|
|
|
htobuf16(sendBuffer, 0);
|
2016-12-01 15:51:01 +00:00
|
|
|
htobe32buf (sendBuffer + 2, i2p::util::GetSecondsSinceEpoch ());
|
2013-10-27 20:48:45 +00:00
|
|
|
}
|
2014-04-18 18:38:32 +00:00
|
|
|
int rem = (len + 6) & 0x0F; // %16
|
2013-09-10 01:35:46 +00:00
|
|
|
int padding = 0;
|
2016-02-05 15:40:23 +00:00
|
|
|
if (rem > 0) {
|
|
|
|
padding = 16 - rem;
|
|
|
|
// fill with random padding
|
|
|
|
RAND_bytes(sendBuffer + len + 2, padding);
|
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
htobe32buf (sendBuffer + len + 2 + padding, adler32 (adler32 (0, Z_NULL, 0), 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);
|
2015-01-21 03:35:27 +00:00
|
|
|
return boost::asio::buffer ((const uint8_t *)sendBuffer, l);
|
|
|
|
}
|
2015-01-21 17:08:15 +00:00
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void NTCPSession::Send (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
2015-01-21 03:35:27 +00:00
|
|
|
{
|
2015-01-28 00:12:27 +00:00
|
|
|
m_IsSending = true;
|
2015-01-21 03:35:27 +00:00
|
|
|
std::vector<boost::asio::const_buffer> bufs;
|
2016-08-07 21:52:18 +00:00
|
|
|
for (const auto& it: msgs)
|
2015-01-21 03:35:27 +00:00
|
|
|
bufs.push_back (CreateMsgBuffer (it));
|
|
|
|
boost::asio::async_write (m_Socket, bufs, boost::asio::transfer_all (),
|
2015-01-21 17:08:15 +00:00
|
|
|
std::bind(&NTCPSession::HandleSent, shared_from_this (), std::placeholders::_1, std::placeholders::_2, msgs));
|
2015-01-21 03:35:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
2015-01-21 03:35:27 +00:00
|
|
|
{
|
2016-02-05 15:40:23 +00:00
|
|
|
(void) msgs;
|
2015-01-28 00:12:27 +00:00
|
|
|
m_IsSending = false;
|
2015-01-21 03:35:27 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP: Couldn't send msgs: ", ecode.message ());
|
2015-01-21 03:35:27 +00:00
|
|
|
// we shouldn't call Terminate () here, because HandleReceive takes care
|
|
|
|
// TODO: 'delete this' statement in Terminate () must be eliminated later
|
|
|
|
// Terminate ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-17 14:58:57 +00:00
|
|
|
m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch ();
|
2015-01-21 03:35:27 +00:00
|
|
|
m_NumSentBytes += bytes_transferred;
|
2015-03-16 23:33:59 +00:00
|
|
|
i2p::transport::transports.UpdateSentBytes (bytes_transferred);
|
2015-01-28 00:12:27 +00:00
|
|
|
if (!m_SendQueue.empty())
|
|
|
|
{
|
|
|
|
Send (m_SendQueue);
|
|
|
|
m_SendQueue.clear ();
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 03:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-10 01:35:46 +00:00
|
|
|
void NTCPSession::SendTimeSyncMessage ()
|
|
|
|
{
|
2013-10-27 20:48:45 +00:00
|
|
|
Send (nullptr);
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 02:05:57 +00:00
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void NTCPSession::SendI2NPMessages (const std::vector<std::shared_ptr<I2NPMessage> >& msgs)
|
2015-01-21 02:05:57 +00:00
|
|
|
{
|
|
|
|
m_Server.GetService ().post (std::bind (&NTCPSession::PostI2NPMessages, shared_from_this (), msgs));
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:47:26 +00:00
|
|
|
void NTCPSession::PostI2NPMessages (std::vector<std::shared_ptr<I2NPMessage> > msgs)
|
2015-01-21 02:05:57 +00:00
|
|
|
{
|
2015-06-17 14:47:26 +00:00
|
|
|
if (m_IsTerminated) return;
|
2015-01-28 00:12:27 +00:00
|
|
|
if (m_IsSending)
|
|
|
|
{
|
2016-07-12 20:26:36 +00:00
|
|
|
if (m_SendQueue.size () < NTCP_MAX_OUTGOING_QUEUE_SIZE)
|
|
|
|
{
|
2016-08-07 21:52:18 +00:00
|
|
|
for (const auto& it: msgs)
|
2016-07-12 20:26:36 +00:00
|
|
|
m_SendQueue.push_back (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NTCP: outgoing messages queue size exceeds ", NTCP_MAX_OUTGOING_QUEUE_SIZE);
|
|
|
|
Terminate ();
|
|
|
|
}
|
2015-01-28 00:12:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Send (msgs);
|
2015-01-21 02:05:57 +00:00
|
|
|
}
|
2015-01-11 22:41:56 +00:00
|
|
|
|
|
|
|
//-----------------------------------------
|
2016-01-28 02:54:42 +00:00
|
|
|
NTCPServer::NTCPServer ():
|
2015-01-11 22:41:56 +00:00
|
|
|
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
|
2016-08-17 14:58:57 +00:00
|
|
|
m_TerminationTimer (m_Service), m_NTCPAcceptor (nullptr), m_NTCPV6Acceptor (nullptr)
|
2015-01-11 22:41:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NTCPServer::~NTCPServer ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::Start ()
|
|
|
|
{
|
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
|
|
|
m_Thread = new std::thread (std::bind (&NTCPServer::Run, this));
|
|
|
|
// create acceptors
|
2016-03-21 17:02:51 +00:00
|
|
|
auto& addresses = context.GetRouterInfo ().GetAddresses ();
|
2016-08-07 21:52:18 +00:00
|
|
|
for (const auto& address: addresses)
|
2015-01-11 22:41:56 +00:00
|
|
|
{
|
2016-07-22 13:56:17 +00:00
|
|
|
if (!address) continue;
|
2016-06-13 15:34:44 +00:00
|
|
|
if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
|
|
|
|
{
|
|
|
|
if (address->host.is_v4())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service,
|
|
|
|
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port));
|
|
|
|
} catch ( std::exception & ex ) {
|
|
|
|
/** fail to bind ip4 */
|
|
|
|
LogPrint(eLogError, "NTCP: Failed to bind to ip4 port ",address->port, ex.what());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint (eLogInfo, "NTCP: Start listening TCP port ", address->port);
|
|
|
|
auto conn = std::make_shared<NTCPSession>(*this);
|
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this,
|
|
|
|
conn, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
else if (address->host.is_v6() && context.SupportsV6 ())
|
2015-01-11 22:41:56 +00:00
|
|
|
{
|
2016-06-13 15:34:44 +00:00
|
|
|
m_NTCPV6Acceptor = new boost::asio::ip::tcp::acceptor (m_Service);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_NTCPV6Acceptor->open (boost::asio::ip::tcp::v6());
|
|
|
|
m_NTCPV6Acceptor->set_option (boost::asio::ip::v6_only (true));
|
|
|
|
|
|
|
|
m_NTCPV6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port));
|
|
|
|
m_NTCPV6Acceptor->listen ();
|
|
|
|
|
|
|
|
LogPrint (eLogInfo, "NTCP: Start listening V6 TCP port ", address->port);
|
|
|
|
auto conn = std::make_shared<NTCPSession> (*this);
|
|
|
|
m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6,
|
|
|
|
this, conn, std::placeholders::_1));
|
|
|
|
} catch ( std::exception & ex ) {
|
|
|
|
LogPrint(eLogError, "NTCP: failed to bind to ip6 port ", address->port);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
2016-06-13 15:34:44 +00:00
|
|
|
}
|
2016-08-17 14:58:57 +00:00
|
|
|
}
|
|
|
|
ScheduleTermination ();
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::Stop ()
|
|
|
|
{
|
2016-11-30 14:24:49 +00:00
|
|
|
{
|
|
|
|
// we have to copy it because Terminate changes m_NTCPSessions
|
|
|
|
auto ntcpSessions = m_NTCPSessions;
|
|
|
|
for (auto& it: ntcpSessions)
|
|
|
|
it.second->Terminate ();
|
|
|
|
}
|
2015-01-11 22:41:56 +00:00
|
|
|
m_NTCPSessions.clear ();
|
|
|
|
|
|
|
|
if (m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = false;
|
2016-08-17 14:58:57 +00:00
|
|
|
m_TerminationTimer.cancel ();
|
|
|
|
if (m_NTCPAcceptor)
|
|
|
|
{
|
|
|
|
delete m_NTCPAcceptor;
|
|
|
|
m_NTCPAcceptor = nullptr;
|
|
|
|
}
|
|
|
|
if (m_NTCPV6Acceptor)
|
|
|
|
{
|
|
|
|
delete m_NTCPV6Acceptor;
|
|
|
|
m_NTCPV6Acceptor = nullptr;
|
|
|
|
}
|
2015-01-11 22:41:56 +00:00
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NTCPServer::Run ()
|
|
|
|
{
|
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: runtime exception: ", ex.what ());
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 16:51:35 +00:00
|
|
|
bool NTCPServer::AddNTCPSession (std::shared_ptr<NTCPSession> session)
|
2015-01-11 22:41:56 +00:00
|
|
|
{
|
2015-11-25 16:51:35 +00:00
|
|
|
if (!session || !session->GetRemoteIdentity ()) return false;
|
|
|
|
auto& ident = session->GetRemoteIdentity ()->GetIdentHash ();
|
|
|
|
auto it = m_NTCPSessions.find (ident);
|
|
|
|
if (it != m_NTCPSessions.end ())
|
2015-01-12 17:15:54 +00:00
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogWarning, "NTCP: session to ", ident.ToBase64 (), " already exists");
|
2016-08-03 14:40:30 +00:00
|
|
|
session->Terminate();
|
2015-11-25 16:51:35 +00:00
|
|
|
return false;
|
2015-01-12 17:15:54 +00:00
|
|
|
}
|
2015-11-25 16:51:35 +00:00
|
|
|
m_NTCPSessions.insert (std::pair<i2p::data::IdentHash, std::shared_ptr<NTCPSession> >(ident, session));
|
|
|
|
return true;
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::RemoveNTCPSession (std::shared_ptr<NTCPSession> session)
|
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
if (session && session->GetRemoteIdentity ())
|
|
|
|
m_NTCPSessions.erase (session->GetRemoteIdentity ()->GetIdentHash ());
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<NTCPSession> NTCPServer::FindNTCPSession (const i2p::data::IdentHash& ident)
|
|
|
|
{
|
|
|
|
auto it = m_NTCPSessions.find (ident);
|
|
|
|
if (it != m_NTCPSessions.end ())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::HandleAccept (std::shared_ptr<NTCPSession> conn, const boost::system::error_code& error)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
2015-02-06 18:49:00 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep);
|
2015-02-10 18:05:08 +00:00
|
|
|
if (conn)
|
|
|
|
conn->ServerLogin ();
|
2015-02-06 18:49:00 +00:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ());
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (error != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
conn = std::make_shared<NTCPSession> (*this);
|
|
|
|
m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this,
|
|
|
|
conn, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::HandleAcceptV6 (std::shared_ptr<NTCPSession> conn, const boost::system::error_code& error)
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
2015-02-06 18:49:00 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
auto ep = conn->GetSocket ().remote_endpoint(ec);
|
|
|
|
if (!ec)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: Connected from ", ep);
|
2015-02-10 20:54:07 +00:00
|
|
|
if (conn)
|
|
|
|
conn->ServerLogin ();
|
2015-02-06 18:49:00 +00:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogError, "NTCP: Connected from error ", ec.message ());
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
conn = std::make_shared<NTCPSession> (*this);
|
|
|
|
m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, this,
|
|
|
|
conn, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn)
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: Connecting to ", address ,":", port);
|
2015-11-25 16:51:35 +00:00
|
|
|
m_Service.post([=]()
|
2016-11-29 19:12:44 +00:00
|
|
|
{
|
2015-11-25 16:51:35 +00:00
|
|
|
if (this->AddNTCPSession (conn))
|
2016-11-29 19:12:44 +00:00
|
|
|
{
|
|
|
|
auto timer = std::make_shared<boost::asio::deadline_timer>(m_Service);
|
|
|
|
timer->expires_from_now (boost::posix_time::seconds(NTCP_CONNECT_TIMEOUT));
|
|
|
|
timer->async_wait ([conn](const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2016-12-01 15:51:01 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: Not connected in ", NTCP_CONNECT_TIMEOUT, " seconds");
|
2016-11-29 19:12:44 +00:00
|
|
|
conn->Terminate ();
|
|
|
|
}
|
|
|
|
});
|
2015-11-25 16:51:35 +00:00
|
|
|
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port),
|
2016-11-29 19:12:44 +00:00
|
|
|
std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn, timer));
|
|
|
|
}
|
2015-11-25 16:51:35 +00:00
|
|
|
});
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 19:12:44 +00:00
|
|
|
void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn, std::shared_ptr<boost::asio::deadline_timer> timer)
|
2015-01-11 22:41:56 +00:00
|
|
|
{
|
2016-11-29 19:12:44 +00:00
|
|
|
timer->cancel ();
|
2015-01-11 22:41:56 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
2016-12-01 15:51:01 +00:00
|
|
|
LogPrint (eLogInfo, "NTCP: Connect error ", ecode.message ());
|
2015-01-11 22:41:56 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2015-11-03 14:15:49 +00:00
|
|
|
i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true);
|
2015-02-06 16:14:41 +00:00
|
|
|
conn->Terminate ();
|
2015-01-11 22:41:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-18 04:15:05 +00:00
|
|
|
LogPrint (eLogDebug, "NTCP: Connected to ", conn->GetSocket ().remote_endpoint ());
|
2015-01-11 22:41:56 +00:00
|
|
|
if (conn->GetSocket ().local_endpoint ().protocol () == boost::asio::ip::tcp::v6()) // ipv6
|
|
|
|
context.UpdateNTCPV6Address (conn->GetSocket ().local_endpoint ().address ());
|
|
|
|
conn->ClientLogin ();
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 18:05:08 +00:00
|
|
|
|
2016-08-17 14:58:57 +00:00
|
|
|
void NTCPServer::ScheduleTermination ()
|
|
|
|
{
|
|
|
|
m_TerminationTimer.expires_from_now (boost::posix_time::seconds(NTCP_TERMINATION_CHECK_TIMEOUT));
|
|
|
|
m_TerminationTimer.async_wait (std::bind (&NTCPServer::HandleTerminationTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTCPServer::HandleTerminationTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto& it: m_NTCPSessions)
|
|
|
|
if (it.second->IsTerminationTimeoutExpired (ts))
|
|
|
|
{
|
|
|
|
auto session = it.second;
|
|
|
|
m_Service.post ([session]
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NTCP: No activity for ", session->GetTerminationTimeout (), " seconds");
|
|
|
|
session->Terminate ();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ScheduleTermination ();
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 01:35:46 +00:00
|
|
|
}
|
|
|
|
}
|