i2pd/libi2pd_client/I2CP.cpp

889 lines
26 KiB
C++
Raw Normal View History

2016-05-30 16:56:42 +00:00
/*
2019-01-29 16:30:31 +00:00
* Copyright (c) 2013-2019, The PurpleI2P Project
2016-05-30 16:56:42 +00:00
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2016-05-13 19:13:36 +00:00
#include <string.h>
2016-06-01 18:38:13 +00:00
#include <stdlib.h>
2016-05-30 19:19:22 +00:00
#include <openssl/rand.h>
2016-05-13 19:13:36 +00:00
#include "I2PEndian.h"
#include "Log.h"
2016-05-27 20:22:42 +00:00
#include "Timestamp.h"
2016-05-29 20:35:57 +00:00
#include "LeaseSet.h"
2016-06-02 19:49:14 +00:00
#include "ClientContext.h"
2016-06-03 17:48:21 +00:00
#include "Transports.h"
#include "Signature.h"
2016-05-12 19:37:46 +00:00
#include "I2CP.h"
namespace i2p
{
namespace client
{
2016-05-27 17:46:28 +00:00
2018-01-06 03:48:51 +00:00
I2CPDestination::I2CPDestination (std::shared_ptr<I2CPSession> owner, std::shared_ptr<const i2p::data::IdentityEx> identity, bool isPublic, const std::map<std::string, std::string>& params):
RunnableService ("I2CP"), LeaseSetDestination (GetIOService (), isPublic, &params),
m_Owner (owner), m_Identity (identity)
2016-05-27 17:46:28 +00:00
{
}
I2CPDestination::~I2CPDestination ()
{
if (IsRunning ())
Stop ();
}
void I2CPDestination::Start ()
{
if (!IsRunning ())
{
LeaseSetDestination::Start ();
StartIOService ();
}
}
void I2CPDestination::Stop ()
{
if (IsRunning ())
{
LeaseSetDestination::Stop ();
StopIOService ();
}
}
2016-05-30 16:56:42 +00:00
void I2CPDestination::SetEncryptionPrivateKey (const uint8_t * key)
{
memcpy (m_EncryptionPrivateKey, key, 256);
m_Decryptor = i2p::data::PrivateKeys::CreateDecryptor (m_Identity->GetCryptoKeyType (), m_EncryptionPrivateKey);
}
bool I2CPDestination::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const
{
if (m_Decryptor)
return m_Decryptor->Decrypt (encrypted, data, ctx, true);
else
2018-01-06 03:48:51 +00:00
LogPrint (eLogError, "I2CP: decryptor is not set");
return false;
2016-05-30 16:56:42 +00:00
}
2016-06-01 18:38:13 +00:00
void I2CPDestination::HandleDataMessage (const uint8_t * buf, size_t len)
{
uint32_t length = bufbe32toh (buf);
if (length > len - 4) length = len - 4;
m_Owner->SendMessagePayloadMessage (buf + 4, length);
2016-06-01 18:38:13 +00:00
}
2018-01-06 03:48:51 +00:00
void I2CPDestination::CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels)
2016-05-29 20:35:57 +00:00
{
2016-05-30 16:56:42 +00:00
i2p::data::LocalLeaseSet ls (m_Identity, m_EncryptionPrivateKey, tunnels); // we don't care about encryption key
m_LeaseSetExpirationTime = ls.GetExpirationTime ();
2016-05-29 20:35:57 +00:00
uint8_t * leases = ls.GetLeases ();
leases[-1] = tunnels.size ();
htobe16buf (leases - 3, m_Owner->GetSessionID ());
2016-05-29 20:35:57 +00:00
size_t l = 2/*sessionID*/ + 1/*num leases*/ + i2p::data::LEASE_SIZE*tunnels.size ();
2018-01-06 03:48:51 +00:00
m_Owner->SendI2CPMessage (I2CP_REQUEST_VARIABLE_LEASESET_MESSAGE, leases - 3, l);
2016-05-29 20:35:57 +00:00
}
2018-01-06 03:48:51 +00:00
2016-05-30 16:56:42 +00:00
void I2CPDestination::LeaseSetCreated (const uint8_t * buf, size_t len)
{
2019-03-10 13:22:42 +00:00
auto ls = std::make_shared<i2p::data::LocalLeaseSet> (m_Identity, buf, len);
2016-05-30 16:56:42 +00:00
ls->SetExpirationTime (m_LeaseSetExpirationTime);
SetLeaseSet (ls);
}
2018-01-06 03:48:51 +00:00
2019-01-29 16:30:31 +00:00
void I2CPDestination::LeaseSet2Created (uint8_t storeType, const uint8_t * buf, size_t len)
{
auto ls = (storeType == i2p::data::NETDB_STORE_TYPE_ENCRYPTED_LEASESET2) ?
std::make_shared<i2p::data::LocalEncryptedLeaseSet2> (m_Identity, buf, len):
std::make_shared<i2p::data::LocalLeaseSet2> (storeType, m_Identity, buf, len);
2019-01-29 16:30:31 +00:00
ls->SetExpirationTime (m_LeaseSetExpirationTime);
SetLeaseSet (ls);
}
2016-06-01 19:30:57 +00:00
void I2CPDestination::SendMsgTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident, uint32_t nonce)
2016-05-30 18:31:56 +00:00
{
auto msg = NewI2NPMessage ();
uint8_t * buf = msg->GetPayload ();
htobe32buf (buf, len);
memcpy (buf + 4, payload, len);
2018-01-06 03:48:51 +00:00
msg->len += len + 4;
2016-05-30 18:31:56 +00:00
msg->FillI2NPMessageHeader (eI2NPData);
2016-09-29 15:24:52 +00:00
auto s = GetSharedFromThis ();
2016-06-01 18:38:13 +00:00
auto remote = FindLeaseSet (ident);
if (remote)
2016-09-29 15:24:52 +00:00
{
GetService ().post (
[s, msg, remote, nonce]()
{
bool sent = s->SendMsg (msg, remote);
s->m_Owner->SendMessageStatusMessage (nonce, sent ? eI2CPMessageStatusGuaranteedSuccess : eI2CPMessageStatusGuaranteedFailure);
2018-01-06 03:48:51 +00:00
});
2016-09-29 15:24:52 +00:00
}
2016-06-01 18:38:13 +00:00
else
{
RequestDestination (ident,
2016-06-01 19:30:57 +00:00
[s, msg, nonce](std::shared_ptr<i2p::data::LeaseSet> ls)
2016-06-01 18:38:13 +00:00
{
2016-06-01 19:30:57 +00:00
if (ls)
2018-01-06 03:48:51 +00:00
{
2016-06-01 19:30:57 +00:00
bool sent = s->SendMsg (msg, ls);
s->m_Owner->SendMessageStatusMessage (nonce, sent ? eI2CPMessageStatusGuaranteedSuccess : eI2CPMessageStatusGuaranteedFailure);
2016-06-01 19:30:57 +00:00
}
else
s->m_Owner->SendMessageStatusMessage (nonce, eI2CPMessageStatusNoLeaseSet);
2016-06-01 18:38:13 +00:00
});
}
}
2016-06-01 19:30:57 +00:00
bool I2CPDestination::SendMsg (std::shared_ptr<I2NPMessage> msg, std::shared_ptr<const i2p::data::LeaseSet> remote)
2018-01-06 03:48:51 +00:00
{
auto remoteSession = GetRoutingSession (remote, true);
if (!remoteSession)
{
LogPrint (eLogError, "I2CP: Failed to create remote session");
return false;
}
auto path = remoteSession->GetSharedRoutingPath ();
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
2018-01-06 03:48:51 +00:00
std::shared_ptr<const i2p::data::Lease> remoteLease;
if (path)
{
if (!remoteSession->CleanupUnconfirmedTags ()) // no stuck tags
{
outboundTunnel = path->outboundTunnel;
remoteLease = path->remoteLease;
}
else
remoteSession->SetSharedRoutingPath (nullptr);
}
else
{
2016-09-25 21:23:21 +00:00
outboundTunnel = GetTunnelPool ()->GetNextOutboundTunnel ();
auto leases = remote->GetNonExpiredLeases ();
2018-01-06 03:48:51 +00:00
if (!leases.empty ())
remoteLease = leases[rand () % leases.size ()];
if (remoteLease && outboundTunnel)
remoteSession->SetSharedRoutingPath (std::make_shared<i2p::garlic::GarlicRoutingPath> (
i2p::garlic::GarlicRoutingPath{outboundTunnel, remoteLease, 10000, 0, 0})); // 10 secs RTT
else
remoteSession->SetSharedRoutingPath (nullptr);
2018-01-06 03:48:51 +00:00
}
if (remoteLease && outboundTunnel)
2016-06-01 18:38:13 +00:00
{
2018-01-06 03:48:51 +00:00
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
auto garlic = remoteSession->WrapSingleMessage (msg);
2018-01-06 03:48:51 +00:00
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
2016-06-01 18:38:13 +00:00
i2p::tunnel::eDeliveryTypeTunnel,
remoteLease->tunnelGateway, remoteLease->tunnelID,
2016-06-01 18:38:13 +00:00
garlic
});
outboundTunnel->SendTunnelDataMsg (msgs);
2016-06-01 19:30:57 +00:00
return true;
2018-01-06 03:48:51 +00:00
}
2016-06-01 18:38:13 +00:00
else
{
if (outboundTunnel)
LogPrint (eLogWarning, "I2CP: Failed to send message. All leases expired");
else
LogPrint (eLogWarning, "I2CP: Failed to send message. No outbound tunnels");
2016-06-01 19:30:57 +00:00
return false;
2018-01-06 03:48:51 +00:00
}
2016-05-30 18:31:56 +00:00
}
2016-06-23 18:01:41 +00:00
I2CPSession::I2CPSession (I2CPServer& owner, std::shared_ptr<proto::socket> socket):
2016-06-13 17:20:21 +00:00
m_Owner (owner), m_Socket (socket), m_Payload (nullptr),
2016-06-11 02:13:20 +00:00
m_SessionID (0xFFFF), m_MessageID (0), m_IsSendAccepted (true)
2016-05-12 20:17:10 +00:00
{
}
2018-01-06 03:48:51 +00:00
2016-05-13 19:13:36 +00:00
I2CPSession::~I2CPSession ()
{
2016-06-13 17:20:21 +00:00
delete[] m_Payload;
2016-05-13 19:13:36 +00:00
}
2016-06-01 14:05:40 +00:00
void I2CPSession::Start ()
{
ReadProtocolByte ();
}
void I2CPSession::Stop ()
2016-05-31 15:54:45 +00:00
{
Terminate ();
2016-05-31 15:54:45 +00:00
}
2016-05-12 20:17:10 +00:00
void I2CPSession::ReadProtocolByte ()
{
if (m_Socket)
{
2018-01-06 03:48:51 +00:00
auto s = shared_from_this ();
m_Socket->async_read_some (boost::asio::buffer (m_Header, 1),
2016-05-12 20:17:10 +00:00
[s](const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
2016-06-13 17:20:21 +00:00
if (!ecode && bytes_transferred > 0 && s->m_Header[0] == I2CP_PROTOCOL_BYTE)
s->ReceiveHeader ();
2016-05-12 20:17:10 +00:00
else
s->Terminate ();
});
}
}
2016-06-13 17:20:21 +00:00
void I2CPSession::ReceiveHeader ()
2016-05-12 20:17:10 +00:00
{
2016-06-13 17:20:21 +00:00
boost::asio::async_read (*m_Socket, boost::asio::buffer (m_Header, I2CP_HEADER_SIZE),
boost::asio::transfer_all (),
std::bind (&I2CPSession::HandleReceivedHeader, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
2016-05-12 20:17:10 +00:00
}
2016-06-13 17:20:21 +00:00
void I2CPSession::HandleReceivedHeader (const boost::system::error_code& ecode, std::size_t bytes_transferred)
2016-05-12 20:17:10 +00:00
{
if (ecode)
Terminate ();
else
2016-05-13 19:13:36 +00:00
{
2016-06-13 17:20:21 +00:00
m_PayloadLen = bufbe32toh (m_Header + I2CP_HEADER_LENGTH_OFFSET);
if (m_PayloadLen > 0)
2016-05-13 19:13:36 +00:00
{
2016-06-13 17:20:21 +00:00
m_Payload = new uint8_t[m_PayloadLen];
ReceivePayload ();
}
else // no following payload
2016-05-13 19:13:36 +00:00
{
2016-06-13 17:20:21 +00:00
HandleMessage ();
ReceiveHeader (); // next message
}
}
}
void I2CPSession::ReceivePayload ()
{
boost::asio::async_read (*m_Socket, boost::asio::buffer (m_Payload, m_PayloadLen),
boost::asio::transfer_all (),
std::bind (&I2CPSession::HandleReceivedPayload, shared_from_this (), std::placeholders::_1, std::placeholders::_2));
}
void I2CPSession::HandleReceivedPayload (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
Terminate ();
else
{
HandleMessage ();
delete[] m_Payload;
m_Payload = nullptr;
2018-01-06 03:48:51 +00:00
m_PayloadLen = 0;
2016-06-13 17:20:21 +00:00
ReceiveHeader (); // next message
2016-05-13 19:13:36 +00:00
}
}
2016-06-13 17:20:21 +00:00
void I2CPSession::HandleMessage ()
2016-05-13 19:13:36 +00:00
{
2016-06-13 17:20:21 +00:00
auto handler = m_Owner.GetMessagesHandlers ()[m_Header[I2CP_HEADER_TYPE_OFFSET]];
2016-05-13 19:13:36 +00:00
if (handler)
2016-06-13 17:20:21 +00:00
(this->*handler)(m_Payload, m_PayloadLen);
2016-05-13 19:13:36 +00:00
else
2018-07-10 09:39:21 +00:00
LogPrint (eLogError, "I2CP: Unknown I2CP message ", (int)m_Header[I2CP_HEADER_TYPE_OFFSET]);
2016-05-12 20:17:10 +00:00
}
void I2CPSession::Terminate ()
{
2016-05-31 15:54:45 +00:00
if (m_Destination)
{
m_Destination->Stop ();
m_Destination = nullptr;
}
if (m_Socket)
{
m_Socket->close ();
m_Socket = nullptr;
}
2016-05-31 15:54:45 +00:00
m_Owner.RemoveSession (GetSessionID ());
2016-06-13 17:20:21 +00:00
LogPrint (eLogDebug, "I2CP: session ", m_SessionID, " terminated");
2016-05-12 20:17:10 +00:00
}
2016-05-27 20:22:42 +00:00
void I2CPSession::SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len)
{
2016-06-12 12:22:55 +00:00
auto socket = m_Socket;
if (socket)
2018-01-06 03:48:51 +00:00
{
2016-06-12 12:22:55 +00:00
auto l = len + I2CP_HEADER_SIZE;
uint8_t * buf = new uint8_t[l];
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len);
buf[I2CP_HEADER_TYPE_OFFSET] = type;
memcpy (buf + I2CP_HEADER_SIZE, payload, len);
boost::asio::async_write (*socket, boost::asio::buffer (buf, l), boost::asio::transfer_all (),
2018-01-06 04:01:44 +00:00
std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (),
2018-01-06 03:48:51 +00:00
std::placeholders::_1, std::placeholders::_2, buf));
}
2016-06-12 12:22:55 +00:00
else
LogPrint (eLogError, "I2CP: Can't write to the socket");
2016-05-27 20:22:42 +00:00
}
void I2CPSession::HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, const uint8_t * buf)
{
delete[] buf;
if (ecode && ecode != boost::asio::error::operation_aborted)
Terminate ();
}
std::string I2CPSession::ExtractString (const uint8_t * buf, size_t len)
{
uint8_t l = buf[0];
if (l > len) l = len;
2016-06-03 17:48:21 +00:00
return std::string ((const char *)(buf + 1), l);
2016-05-27 20:22:42 +00:00
}
size_t I2CPSession::PutString (uint8_t * buf, size_t len, const std::string& str)
{
auto l = str.length ();
if (l + 1 >= len) l = len - 1;
if (l > 255) l = 255; // 1 byte max
buf[0] = l;
2018-01-06 03:48:51 +00:00
memcpy (buf + 1, str.c_str (), l);
2016-05-27 20:22:42 +00:00
return l + 1;
}
2016-06-02 17:26:41 +00:00
void I2CPSession::ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping)
// TODO: move to Base.cpp
{
size_t offset = 0;
while (offset < len)
{
2016-06-08 18:05:20 +00:00
std::string param = ExtractString (buf + offset, len - offset);
2016-06-10 19:25:30 +00:00
offset += param.length () + 1;
2018-01-06 03:48:51 +00:00
if (buf[offset] != '=')
2016-06-02 17:26:41 +00:00
{
2016-06-08 18:05:20 +00:00
LogPrint (eLogWarning, "I2CP: Unexpected character ", buf[offset], " instead '=' after ", param);
2018-01-06 03:48:51 +00:00
break;
}
2016-06-08 18:05:20 +00:00
offset++;
std::string value = ExtractString (buf + offset, len - offset);
2016-06-10 19:25:30 +00:00
offset += value.length () + 1;
2018-01-06 03:48:51 +00:00
if (buf[offset] != ';')
2016-06-08 18:05:20 +00:00
{
LogPrint (eLogWarning, "I2CP: Unexpected character ", buf[offset], " instead ';' after ", value);
2018-01-06 03:48:51 +00:00
break;
}
2016-06-08 18:05:20 +00:00
offset++;
mapping.insert (std::make_pair (param, value));
2016-06-02 17:26:41 +00:00
}
}
2016-05-13 19:13:36 +00:00
void I2CPSession::GetDateMessageHandler (const uint8_t * buf, size_t len)
{
2016-05-27 20:22:42 +00:00
// get version
auto version = ExtractString (buf, len);
auto l = version.length () + 1 + 8;
uint8_t * payload = new uint8_t[l];
// set date
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
htobe64buf (payload, ts);
// echo vesrion back
PutString (payload + 8, l - 8, version);
2018-01-06 03:48:51 +00:00
SendI2CPMessage (I2CP_SET_DATE_MESSAGE, payload, l);
delete[] payload;
2016-05-27 20:22:42 +00:00
}
void I2CPSession::CreateSessionMessageHandler (const uint8_t * buf, size_t len)
{
2016-06-10 15:47:22 +00:00
RAND_bytes ((uint8_t *)&m_SessionID, 2);
m_Owner.InsertSession (shared_from_this ());
2016-05-30 19:19:22 +00:00
auto identity = std::make_shared<i2p::data::IdentityEx>();
size_t offset = identity->FromBuffer (buf, len);
2016-06-10 15:39:20 +00:00
if (!offset)
{
2019-04-08 19:22:42 +00:00
LogPrint (eLogError, "I2CP: create session malformed identity");
2016-06-10 15:39:20 +00:00
SendSessionStatusMessage (3); // invalid
return;
2018-01-06 03:48:51 +00:00
}
2016-05-30 19:19:22 +00:00
uint16_t optionsSize = bufbe16toh (buf + offset);
2016-06-01 14:05:40 +00:00
offset += 2;
2016-06-10 15:39:20 +00:00
if (optionsSize > len - offset)
{
2018-01-06 03:48:51 +00:00
LogPrint (eLogError, "I2CP: options size ", optionsSize, "exceeds message size");
2016-06-10 15:39:20 +00:00
SendSessionStatusMessage (3); // invalid
return;
}
2016-06-02 17:26:41 +00:00
std::map<std::string, std::string> params;
2018-01-06 03:48:51 +00:00
ExtractMapping (buf + offset, optionsSize, params);
2016-06-10 15:39:20 +00:00
offset += optionsSize; // options
if (params[I2CP_PARAM_MESSAGE_RELIABILITY] == "none") m_IsSendAccepted = false;
2016-05-30 19:19:22 +00:00
offset += 8; // date
if (identity->Verify (buf, offset, buf + offset)) // signature
2018-01-06 03:48:51 +00:00
{
2016-06-02 17:26:41 +00:00
bool isPublic = true;
2016-06-03 15:49:39 +00:00
if (params[I2CP_PARAM_DONT_PUBLISH_LEASESET] == "true") isPublic = false;
2016-06-10 15:39:20 +00:00
if (!m_Destination)
{
m_Destination = std::make_shared<I2CPDestination>(shared_from_this (), identity, isPublic, params);
SendSessionStatusMessage (1); // created
LogPrint (eLogDebug, "I2CP: session ", m_SessionID, " created");
2018-01-06 03:48:51 +00:00
m_Destination->Start ();
2016-06-10 15:39:20 +00:00
}
else
{
2018-01-06 03:48:51 +00:00
LogPrint (eLogError, "I2CP: session already exists");
2016-06-10 15:39:20 +00:00
SendSessionStatusMessage (4); // refused
}
2016-05-30 19:19:22 +00:00
}
else
{
2018-07-10 09:39:21 +00:00
LogPrint (eLogError, "I2CP: create session signature verification failed");
2016-05-30 19:19:22 +00:00
SendSessionStatusMessage (3); // invalid
}
}
2016-06-01 14:05:40 +00:00
void I2CPSession::DestroySessionMessageHandler (const uint8_t * buf, size_t len)
{
SendSessionStatusMessage (0); // destroy
LogPrint (eLogDebug, "I2CP: session ", m_SessionID, " destroyed");
2016-06-10 15:47:22 +00:00
if (m_Destination)
{
m_Destination->Stop ();
m_Destination = 0;
}
2016-06-01 14:05:40 +00:00
}
2016-06-06 19:36:02 +00:00
void I2CPSession::ReconfigureSessionMessageHandler (const uint8_t * buf, size_t len)
{
2018-01-02 16:34:58 +00:00
uint8_t status = 3; // rejected
if(len > sizeof(uint16_t))
{
uint16_t sessionID = bufbe16toh(buf);
if(sessionID == m_SessionID)
{
buf += sizeof(uint16_t);
const uint8_t * body = buf;
i2p::data::IdentityEx ident;
if(ident.FromBuffer(buf, len - sizeof(uint16_t)))
{
if (ident == *m_Destination->GetIdentity())
{
size_t identsz = ident.GetFullLen();
buf += identsz;
uint16_t optssize = bufbe16toh(buf);
if (optssize <= len - sizeof(uint16_t) - sizeof(uint64_t) - identsz - ident.GetSignatureLen() - sizeof(uint16_t))
{
2018-02-26 13:00:07 +00:00
buf += sizeof(uint16_t);
2018-01-02 16:34:58 +00:00
std::map<std::string, std::string> opts;
ExtractMapping(buf, optssize, opts);
buf += optssize;
//uint64_t date = bufbe64toh(buf);
2018-02-26 13:00:07 +00:00
buf += sizeof(uint64_t);
2018-01-02 16:34:58 +00:00
const uint8_t * sig = buf;
2018-02-26 13:00:07 +00:00
if(ident.Verify(body, len - sizeof(uint16_t) - ident.GetSignatureLen(), sig))
2018-01-02 16:34:58 +00:00
{
if(m_Destination->Reconfigure(opts))
{
LogPrint(eLogInfo, "I2CP: reconfigured destination");
status = 2; // updated
}
else
LogPrint(eLogWarning, "I2CP: failed to reconfigure destination");
}
else
LogPrint(eLogError, "I2CP: invalid reconfigure message signature");
}
else
2018-07-10 09:39:21 +00:00
LogPrint(eLogError, "I2CP: mapping size mismatch");
2018-01-02 16:34:58 +00:00
}
else
2018-07-10 09:39:21 +00:00
LogPrint(eLogError, "I2CP: destination mismatch");
2018-01-02 16:34:58 +00:00
}
else
LogPrint(eLogError, "I2CP: malfromed destination");
}
else
2018-07-10 09:39:21 +00:00
LogPrint(eLogError, "I2CP: session mismatch");
2018-01-02 16:34:58 +00:00
}
else
LogPrint(eLogError, "I2CP: short message");
SendSessionStatusMessage (status);
2016-06-06 19:36:02 +00:00
}
2016-05-30 19:19:22 +00:00
void I2CPSession::SendSessionStatusMessage (uint8_t status)
{
uint8_t buf[3];
htobe16buf (buf, m_SessionID);
buf[2] = status;
2018-01-06 03:48:51 +00:00
SendI2CPMessage (I2CP_SESSION_STATUS_MESSAGE, buf, 3);
2016-05-13 19:13:36 +00:00
}
2016-06-01 19:30:57 +00:00
void I2CPSession::SendMessageStatusMessage (uint32_t nonce, I2CPMessageStatus status)
{
2016-06-10 15:39:20 +00:00
if (!nonce) return; // don't send status with zero nonce
2016-06-01 19:30:57 +00:00
uint8_t buf[15];
htobe16buf (buf, m_SessionID);
htobe32buf (buf + 2, m_MessageID++);
buf[6] = (uint8_t)status;
memset (buf + 7, 0, 4); // size
2018-01-06 03:48:51 +00:00
htobe32buf (buf + 11, nonce);
SendI2CPMessage (I2CP_MESSAGE_STATUS_MESSAGE, buf, 15);
2016-06-01 19:30:57 +00:00
}
2016-05-30 16:56:42 +00:00
void I2CPSession::CreateLeaseSetMessageHandler (const uint8_t * buf, size_t len)
{
uint16_t sessionID = bufbe16toh (buf);
if (sessionID == m_SessionID)
{
size_t offset = 2;
if (m_Destination)
{
offset += i2p::crypto::DSA_PRIVATE_KEY_LENGTH; // skip signing private key
// we always assume this field as 20 bytes (DSA) regardless actual size
2018-01-06 03:48:51 +00:00
// instead of
//offset += m_Destination->GetIdentity ()->GetSigningPrivateKeyLen ();
2016-05-30 16:56:42 +00:00
m_Destination->SetEncryptionPrivateKey (buf + offset);
offset += 256;
m_Destination->LeaseSetCreated (buf + offset, len - offset);
}
2018-01-06 03:48:51 +00:00
}
2016-05-30 16:56:42 +00:00
else
LogPrint (eLogError, "I2CP: unexpected sessionID ", sessionID);
}
2019-01-29 16:30:31 +00:00
void I2CPSession::CreateLeaseSet2MessageHandler (const uint8_t * buf, size_t len)
{
uint16_t sessionID = bufbe16toh (buf);
if (sessionID == m_SessionID)
{
size_t offset = 2;
if (m_Destination)
{
uint8_t storeType = buf[offset]; offset++; // store type
i2p::data::LeaseSet2 ls (storeType, buf + offset, len - offset); // outer layer only for encrypted
if (!ls.IsValid ())
2019-01-29 16:30:31 +00:00
{
LogPrint (eLogError, "I2CP: invalid LeaseSet2 of type ", storeType);
return;
}
offset += ls.GetBufferLen ();
// private keys
int numPrivateKeys = buf[offset]; offset++;
uint16_t currentKeyType = 0;
const uint8_t * currentKey = nullptr;
for (int i = 0; i < numPrivateKeys; i++)
{
if (offset + 4 > len) return;
uint16_t keyType = bufbe16toh (buf + offset); offset += 2; // encryption type
uint16_t keyLen = bufbe16toh (buf + offset); offset += 2; // private key length
if (offset + keyLen > len) return;
if (keyType > currentKeyType)
2019-01-29 16:30:31 +00:00
{
currentKeyType = keyType;
currentKey = buf + offset;
2019-01-29 16:30:31 +00:00
}
offset += keyLen;
}
// TODO: support multiple keys
if (currentKey)
m_Destination->SetEncryptionPrivateKey (currentKey);
m_Destination->LeaseSet2Created (storeType, ls.GetBuffer (), ls.GetBufferLen ());
2019-01-29 16:30:31 +00:00
}
}
else
LogPrint (eLogError, "I2CP: unexpected sessionID ", sessionID);
}
2016-05-30 18:31:56 +00:00
void I2CPSession::SendMessageMessageHandler (const uint8_t * buf, size_t len)
{
uint16_t sessionID = bufbe16toh (buf);
if (sessionID == m_SessionID)
{
size_t offset = 2;
if (m_Destination)
{
i2p::data::IdentityEx identity;
2016-06-13 15:34:44 +00:00
size_t identsize = identity.FromBuffer (buf + offset, len - offset);
if (identsize)
{
offset += identsize;
uint32_t payloadLen = bufbe32toh (buf + offset);
if (payloadLen + offset <= len)
2018-01-06 03:48:51 +00:00
{
offset += 4;
uint32_t nonce = bufbe32toh (buf + offset + payloadLen);
2018-01-06 03:48:51 +00:00
if (m_IsSendAccepted)
2018-01-06 04:01:44 +00:00
SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted
m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce);
}
2018-01-06 04:01:44 +00:00
else
LogPrint(eLogError, "I2CP: cannot send message, too big");
}
else
LogPrint(eLogError, "I2CP: invalid identity");
2018-01-06 03:48:51 +00:00
}
}
2016-05-30 18:31:56 +00:00
else
LogPrint (eLogError, "I2CP: unexpected sessionID ", sessionID);
}
2016-06-03 16:03:36 +00:00
void I2CPSession::SendMessageExpiresMessageHandler (const uint8_t * buf, size_t len)
{
2018-01-06 03:48:51 +00:00
SendMessageMessageHandler (buf, len - 8); // ignore flags(2) and expiration(6)
}
2016-06-03 16:03:36 +00:00
2016-06-01 15:11:18 +00:00
void I2CPSession::HostLookupMessageHandler (const uint8_t * buf, size_t len)
{
uint16_t sessionID = bufbe16toh (buf);
2016-06-10 22:43:35 +00:00
if (sessionID == m_SessionID || sessionID == 0xFFFF) // -1 means without session
2016-06-01 15:11:18 +00:00
{
uint32_t requestID = bufbe32toh (buf + 2);
//uint32_t timeout = bufbe32toh (buf + 6);
2016-06-02 19:49:14 +00:00
i2p::data::IdentHash ident;
2018-01-06 03:48:51 +00:00
switch (buf[10])
2016-06-01 15:11:18 +00:00
{
2016-06-02 19:49:14 +00:00
case 0: // hash
ident = i2p::data::IdentHash (buf + 11);
break;
case 1: // address
2016-06-01 15:11:18 +00:00
{
2016-06-02 19:49:14 +00:00
auto name = ExtractString (buf + 11, len - 11);
2019-03-28 16:19:19 +00:00
auto addr = i2p::client::context.GetAddressBook ().GetAddress (name);
if (!addr || !addr->IsIdentHash ())
2016-06-01 15:11:18 +00:00
{
2019-03-28 16:19:19 +00:00
// TODO: handle blinded addresses
2016-06-02 19:49:14 +00:00
LogPrint (eLogError, "I2CP: address ", name, " not found");
SendHostReplyMessage (requestID, nullptr);
return;
}
2019-03-28 16:19:19 +00:00
else
ident = addr->identHash;
2018-01-06 03:48:51 +00:00
break;
2016-06-01 15:11:18 +00:00
}
2016-06-02 19:49:14 +00:00
default:
LogPrint (eLogError, "I2CP: request type ", (int)buf[10], " is not supported");
2016-06-01 15:11:18 +00:00
SendHostReplyMessage (requestID, nullptr);
2016-06-02 19:49:14 +00:00
return;
2016-06-01 15:11:18 +00:00
}
2016-06-02 19:49:14 +00:00
2016-06-10 22:43:35 +00:00
std::shared_ptr<LeaseSetDestination> destination = m_Destination;
2018-01-06 03:48:51 +00:00
if(!destination) destination = i2p::client::context.GetSharedLocalDestination ();
2016-06-10 22:43:35 +00:00
if (destination)
2016-06-01 15:11:18 +00:00
{
2016-06-10 22:43:35 +00:00
auto ls = destination->FindLeaseSet (ident);
2016-06-02 19:49:14 +00:00
if (ls)
SendHostReplyMessage (requestID, ls->GetIdentity ());
else
{
auto s = shared_from_this ();
2016-06-10 22:43:35 +00:00
destination->RequestDestination (ident,
2016-06-02 19:49:14 +00:00
[s, requestID](std::shared_ptr<i2p::data::LeaseSet> leaseSet)
{
s->SendHostReplyMessage (requestID, leaseSet ? leaseSet->GetIdentity () : nullptr);
});
2018-01-06 03:48:51 +00:00
}
2016-06-01 15:11:18 +00:00
}
2016-06-02 19:49:14 +00:00
else
SendHostReplyMessage (requestID, nullptr);
2018-01-06 03:48:51 +00:00
}
2016-06-01 15:11:18 +00:00
else
LogPrint (eLogError, "I2CP: unexpected sessionID ", sessionID);
}
void I2CPSession::SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity)
{
if (identity)
{
size_t l = identity->GetFullLen () + 7;
uint8_t * buf = new uint8_t[l];
htobe16buf (buf, m_SessionID);
htobe32buf (buf + 2, requestID);
buf[6] = 0; // result code
identity->ToBuffer (buf + 7, l - 7);
2018-01-06 03:48:51 +00:00
SendI2CPMessage (I2CP_HOST_REPLY_MESSAGE, buf, l);
2016-06-01 15:11:18 +00:00
delete[] buf;
}
else
{
uint8_t buf[7];
htobe16buf (buf, m_SessionID);
htobe32buf (buf + 2, requestID);
buf[6] = 1; // result code
2018-01-06 03:48:51 +00:00
SendI2CPMessage (I2CP_HOST_REPLY_MESSAGE, buf, 7);
}
2016-06-01 15:11:18 +00:00
}
2016-06-03 15:49:39 +00:00
void I2CPSession::DestLookupMessageHandler (const uint8_t * buf, size_t len)
{
if (m_Destination)
{
auto ls = m_Destination->FindLeaseSet (buf);
if (ls)
2018-01-06 03:48:51 +00:00
{
2016-06-03 15:49:39 +00:00
auto l = ls->GetIdentity ()->GetFullLen ();
uint8_t * identBuf = new uint8_t[l];
ls->GetIdentity ()->ToBuffer (identBuf, l);
SendI2CPMessage (I2CP_DEST_REPLY_MESSAGE, identBuf, l);
delete[] identBuf;
}
else
{
auto s = shared_from_this ();
i2p::data::IdentHash ident (buf);
m_Destination->RequestDestination (ident,
[s, ident](std::shared_ptr<i2p::data::LeaseSet> leaseSet)
{
if (leaseSet) // found
{
auto l = leaseSet->GetIdentity ()->GetFullLen ();
uint8_t * identBuf = new uint8_t[l];
leaseSet->GetIdentity ()->ToBuffer (identBuf, l);
s->SendI2CPMessage (I2CP_DEST_REPLY_MESSAGE, identBuf, l);
delete[] identBuf;
}
else
s->SendI2CPMessage (I2CP_DEST_REPLY_MESSAGE, ident, 32); // not found
});
}
}
else
2018-01-06 03:48:51 +00:00
SendI2CPMessage (I2CP_DEST_REPLY_MESSAGE, buf, 32);
}
2016-06-03 15:49:39 +00:00
2016-06-03 17:48:21 +00:00
void I2CPSession::GetBandwidthLimitsMessageHandler (const uint8_t * buf, size_t len)
{
uint8_t limits[64];
memset (limits, 0, 64);
htobe32buf (limits, i2p::transport::transports.GetInBandwidth ()); // inbound
htobe32buf (limits + 4, i2p::transport::transports.GetOutBandwidth ()); // outbound
SendI2CPMessage (I2CP_BANDWIDTH_LIMITS_MESSAGE, limits, 64);
}
2016-06-01 18:38:13 +00:00
void I2CPSession::SendMessagePayloadMessage (const uint8_t * payload, size_t len)
{
// we don't use SendI2CPMessage to eliminate additional copy
2016-06-01 19:30:57 +00:00
auto l = len + 10 + I2CP_HEADER_SIZE;
2016-06-01 18:38:13 +00:00
uint8_t * buf = new uint8_t[l];
2016-06-01 19:30:57 +00:00
htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10);
2016-06-01 18:38:13 +00:00
buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE;
htobe16buf (buf + I2CP_HEADER_SIZE, m_SessionID);
2016-06-01 19:30:57 +00:00
htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++);
2018-01-06 03:48:51 +00:00
htobe32buf (buf + I2CP_HEADER_SIZE + 6, len);
2016-06-01 19:30:57 +00:00
memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len);
2016-06-01 18:38:13 +00:00
boost::asio::async_write (*m_Socket, boost::asio::buffer (buf, l), boost::asio::transfer_all (),
2018-01-06 04:01:44 +00:00
std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (),
2018-01-06 03:48:51 +00:00
std::placeholders::_1, std::placeholders::_2, buf));
2016-06-01 18:38:13 +00:00
}
2016-05-31 15:54:45 +00:00
I2CPServer::I2CPServer (const std::string& interface, int port):
m_IsRunning (false), m_Thread (nullptr),
2018-01-06 03:48:51 +00:00
m_Acceptor (m_Service,
2016-06-22 19:48:36 +00:00
#ifdef ANDROID
2016-06-23 18:01:41 +00:00
I2CPSession::proto::endpoint(std::string (1, '\0') + interface)) // leading 0 for abstract address
2016-06-22 19:48:36 +00:00
#else
2016-06-23 18:01:41 +00:00
I2CPSession::proto::endpoint(boost::asio::ip::address::from_string(interface), port))
2016-06-22 19:48:36 +00:00
#endif
2016-05-12 19:37:46 +00:00
{
2016-05-13 19:13:36 +00:00
memset (m_MessagesHandlers, 0, sizeof (m_MessagesHandlers));
2016-05-27 20:22:42 +00:00
m_MessagesHandlers[I2CP_GET_DATE_MESSAGE] = &I2CPSession::GetDateMessageHandler;
2016-05-30 16:56:42 +00:00
m_MessagesHandlers[I2CP_CREATE_SESSION_MESSAGE] = &I2CPSession::CreateSessionMessageHandler;
2016-06-01 14:05:40 +00:00
m_MessagesHandlers[I2CP_DESTROY_SESSION_MESSAGE] = &I2CPSession::DestroySessionMessageHandler;
2016-06-06 19:36:02 +00:00
m_MessagesHandlers[I2CP_RECONFIGURE_SESSION_MESSAGE] = &I2CPSession::ReconfigureSessionMessageHandler;
2016-05-30 18:31:56 +00:00
m_MessagesHandlers[I2CP_CREATE_LEASESET_MESSAGE] = &I2CPSession::CreateLeaseSetMessageHandler;
2019-01-29 16:30:31 +00:00
m_MessagesHandlers[I2CP_CREATE_LEASESET2_MESSAGE] = &I2CPSession::CreateLeaseSet2MessageHandler;
2016-06-01 15:11:18 +00:00
m_MessagesHandlers[I2CP_SEND_MESSAGE_MESSAGE] = &I2CPSession::SendMessageMessageHandler;
2018-01-06 03:48:51 +00:00
m_MessagesHandlers[I2CP_SEND_MESSAGE_EXPIRES_MESSAGE] = &I2CPSession::SendMessageExpiresMessageHandler;
2016-06-03 15:49:39 +00:00
m_MessagesHandlers[I2CP_HOST_LOOKUP_MESSAGE] = &I2CPSession::HostLookupMessageHandler;
2018-01-06 03:48:51 +00:00
m_MessagesHandlers[I2CP_DEST_LOOKUP_MESSAGE] = &I2CPSession::DestLookupMessageHandler;
m_MessagesHandlers[I2CP_GET_BANDWIDTH_LIMITS_MESSAGE] = &I2CPSession::GetBandwidthLimitsMessageHandler;
2016-05-12 19:37:46 +00:00
}
2016-05-31 15:54:45 +00:00
I2CPServer::~I2CPServer ()
{
if (m_IsRunning)
Stop ();
}
void I2CPServer::Start ()
{
Accept ();
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&I2CPServer::Run, this));
}
void I2CPServer::Stop ()
{
m_IsRunning = false;
m_Acceptor.cancel ();
2016-08-05 18:23:54 +00:00
for (auto& it: m_Sessions)
2016-06-01 14:05:40 +00:00
it.second->Stop ();
2016-05-31 15:54:45 +00:00
m_Sessions.clear ();
m_Service.stop ();
if (m_Thread)
2018-01-06 03:48:51 +00:00
{
m_Thread->join ();
2016-05-31 15:54:45 +00:00
delete m_Thread;
m_Thread = nullptr;
2018-01-06 03:48:51 +00:00
}
2016-05-31 15:54:45 +00:00
}
2018-01-06 03:48:51 +00:00
void I2CPServer::Run ()
{
2016-05-31 15:54:45 +00:00
while (m_IsRunning)
{
try
2018-01-06 03:48:51 +00:00
{
2016-05-31 15:54:45 +00:00
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "I2CP: runtime exception: ", ex.what ());
2018-01-06 03:48:51 +00:00
}
}
2016-05-31 15:54:45 +00:00
}
void I2CPServer::Accept ()
{
2016-06-23 18:01:41 +00:00
auto newSocket = std::make_shared<I2CPSession::proto::socket> (m_Service);
2016-05-31 15:54:45 +00:00
m_Acceptor.async_accept (*newSocket, std::bind (&I2CPServer::HandleAccept, this,
std::placeholders::_1, newSocket));
}
2016-06-22 19:48:36 +00:00
void I2CPServer::HandleAccept(const boost::system::error_code& ecode,
2016-06-23 18:01:41 +00:00
std::shared_ptr<I2CPSession::proto::socket> socket)
2016-05-31 15:54:45 +00:00
{
if (!ecode && socket)
{
boost::system::error_code ec;
auto ep = socket->remote_endpoint (ec);
if (!ec)
2018-01-06 03:48:51 +00:00
{
2016-05-31 15:54:45 +00:00
LogPrint (eLogDebug, "I2CP: new connection from ", ep);
auto session = std::make_shared<I2CPSession>(*this, socket);
2016-06-01 14:05:40 +00:00
session->Start ();
2016-05-31 15:54:45 +00:00
}
else
LogPrint (eLogError, "I2CP: incoming connection error ", ec.message ());
}
else
LogPrint (eLogError, "I2CP: accept error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Accept ();
}
bool I2CPServer::InsertSession (std::shared_ptr<I2CPSession> session)
{
if (!session) return false;
if (!m_Sessions.insert({session->GetSessionID (), session}).second)
2018-01-06 03:48:51 +00:00
{
LogPrint (eLogError, "I2CP: duplicate session id ", session->GetSessionID ());
return false;
2018-01-06 03:48:51 +00:00
}
return true;
}
2016-05-31 15:54:45 +00:00
void I2CPServer::RemoveSession (uint16_t sessionID)
{
m_Sessions.erase (sessionID);
2018-01-06 03:48:51 +00:00
}
2016-05-12 19:37:46 +00:00
}
}