i2pd/libi2pd/TunnelConfig.h

264 lines
6.4 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2013-12-07 00:02:49 +00:00
#ifndef TUNNEL_CONFIG_H__
#define TUNNEL_CONFIG_H__
#include <inttypes.h>
#include <sstream>
2014-01-09 00:30:47 +00:00
#include <vector>
#include <memory>
2015-11-03 14:15:49 +00:00
#include "Identity.h"
2013-12-07 00:02:49 +00:00
#include "RouterContext.h"
#include "Timestamp.h"
2013-12-07 00:02:49 +00:00
namespace i2p
{
namespace tunnel
{
struct TunnelHopConfig
{
2015-11-03 14:15:49 +00:00
std::shared_ptr<const i2p::data::IdentityEx> ident;
i2p::data::IdentHash nextIdent;
2013-12-07 00:02:49 +00:00
uint32_t tunnelID, nextTunnelID;
uint8_t layerKey[32];
uint8_t ivKey[32];
uint8_t replyKey[32];
uint8_t replyIV[16];
2018-01-06 03:48:51 +00:00
bool isGateway, isEndpoint;
2013-12-07 00:02:49 +00:00
TunnelHopConfig * next, * prev;
int recordIndex; // record # in tunnel build message
2018-01-06 03:48:51 +00:00
2015-11-03 14:15:49 +00:00
TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r)
2013-12-07 00:02:49 +00:00
{
2015-11-03 14:15:49 +00:00
RAND_bytes (layerKey, 32);
RAND_bytes (ivKey, 32);
2016-01-10 21:40:28 +00:00
RAND_bytes (replyKey, 32);
2015-11-03 14:15:49 +00:00
RAND_bytes (replyIV, 16);
RAND_bytes ((uint8_t *)&tunnelID, 4);
2018-03-07 17:36:38 +00:00
if (!tunnelID) tunnelID = 1; // tunnelID can't be zero
2013-12-07 00:02:49 +00:00
isGateway = true;
isEndpoint = true;
2018-01-06 03:48:51 +00:00
ident = r;
//nextRouter = nullptr;
2013-12-07 00:02:49 +00:00
nextTunnelID = 0;
next = nullptr;
prev = nullptr;
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
2015-11-03 14:15:49 +00:00
void SetNextIdent (const i2p::data::IdentHash& ident)
2013-12-07 00:02:49 +00:00
{
2015-11-03 14:15:49 +00:00
nextIdent = ident;
2013-12-07 00:02:49 +00:00
isEndpoint = false;
2015-11-03 14:15:49 +00:00
RAND_bytes ((uint8_t *)&nextTunnelID, 4);
2018-03-07 17:36:38 +00:00
if (!nextTunnelID) nextTunnelID = 1; // tunnelID can't be zero
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
2015-11-03 14:15:49 +00:00
void SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent)
2013-12-07 00:02:49 +00:00
{
2015-11-03 14:15:49 +00:00
nextIdent = replyIdent;
nextTunnelID = replyTunnelID;
2013-12-07 00:02:49 +00:00
isEndpoint = true;
}
2018-01-06 03:48:51 +00:00
2013-12-07 00:02:49 +00:00
void SetNext (TunnelHopConfig * n)
{
next = n;
if (next)
2018-01-06 03:48:51 +00:00
{
2013-12-07 00:02:49 +00:00
next->prev = this;
next->isGateway = false;
isEndpoint = false;
2015-11-03 14:15:49 +00:00
nextIdent = next->ident->GetIdentHash ();
2013-12-07 00:02:49 +00:00
nextTunnelID = next->tunnelID;
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
}
void SetPrev (TunnelHopConfig * p)
{
prev = p;
2018-01-06 03:48:51 +00:00
if (prev)
{
2013-12-07 00:02:49 +00:00
prev->next = this;
prev->isEndpoint = false;
isGateway = false;
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
}
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx) const
{
2015-01-04 14:33:19 +00:00
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
2018-01-06 03:48:51 +00:00
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
2015-11-03 14:15:49 +00:00
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, ident->GetIdentHash (), 32);
2015-01-04 14:33:19 +00:00
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
2015-11-03 14:15:49 +00:00
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
2015-01-04 14:33:19 +00:00
memcpy (clearText + BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET, layerKey, 32);
memcpy (clearText + BUILD_REQUEST_RECORD_IV_KEY_OFFSET, ivKey, 32);
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET, replyKey, 32);
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, replyIV, 16);
uint8_t flag = 0;
if (isGateway) flag |= 0x80;
if (isEndpoint) flag |= 0x40;
2015-01-04 14:33:19 +00:00
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag;
2018-01-06 03:48:51 +00:00
htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ());
htobe32buf (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
2016-01-10 21:40:28 +00:00
RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET);
auto encryptor = ident->CreateEncryptor (nullptr);
if (encryptor)
encryptor->Encrypt (clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx, false);
2015-11-03 14:15:49 +00:00
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
2018-01-06 03:48:51 +00:00
}
};
2013-12-07 00:02:49 +00:00
2016-03-02 21:12:02 +00:00
class TunnelConfig
2013-12-07 00:02:49 +00:00
{
2018-01-06 03:48:51 +00:00
public:
2015-11-03 14:15:49 +00:00
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers) // inbound
{
CreatePeers (peers);
m_LastHop->SetNextIdent (i2p::context.GetIdentHash ());
}
2013-12-07 00:02:49 +00:00
2018-01-06 03:48:51 +00:00
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers,
2015-11-03 14:15:49 +00:00
uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent) // outbound
2013-12-07 00:02:49 +00:00
{
2015-11-03 14:15:49 +00:00
CreatePeers (peers);
m_FirstHop->isGateway = false;
m_LastHop->SetReplyHop (replyTunnelID, replyIdent);
2014-01-09 00:30:47 +00:00
}
2018-01-06 03:48:51 +00:00
2013-12-07 00:02:49 +00:00
~TunnelConfig ()
{
TunnelHopConfig * hop = m_FirstHop;
2018-01-06 03:48:51 +00:00
2013-12-07 00:02:49 +00:00
while (hop)
{
2014-01-21 20:10:49 +00:00
auto tmp = hop;
2013-12-07 00:02:49 +00:00
hop = hop->next;
2014-01-21 20:10:49 +00:00
delete tmp;
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
}
2018-01-06 03:48:51 +00:00
2013-12-07 00:02:49 +00:00
TunnelHopConfig * GetFirstHop () const
{
return m_FirstHop;
}
TunnelHopConfig * GetLastHop () const
{
return m_LastHop;
}
int GetNumHops () const
2013-12-07 00:02:49 +00:00
{
int num = 0;
2018-01-06 03:48:51 +00:00
TunnelHopConfig * hop = m_FirstHop;
2013-12-07 00:02:49 +00:00
while (hop)
{
num++;
hop = hop->next;
2018-01-06 03:48:51 +00:00
}
2013-12-07 00:02:49 +00:00
return num;
}
bool IsEmpty () const
{
return !m_FirstHop;
2018-01-06 03:48:51 +00:00
}
2016-03-02 21:12:02 +00:00
virtual bool IsInbound () const { return m_FirstHop->isGateway; }
2015-05-08 18:07:33 +00:00
2018-01-06 03:48:51 +00:00
virtual uint32_t GetTunnelID () const
{
2015-11-03 14:15:49 +00:00
if (!m_FirstHop) return 0;
2018-01-06 03:48:51 +00:00
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
2015-11-03 14:15:49 +00:00
}
2018-01-06 03:48:51 +00:00
virtual uint32_t GetNextTunnelID () const
{
2015-11-03 14:15:49 +00:00
if (!m_FirstHop) return 0;
2018-01-06 03:48:51 +00:00
return m_FirstHop->tunnelID;
2015-11-03 14:15:49 +00:00
}
2018-01-06 03:48:51 +00:00
virtual const i2p::data::IdentHash& GetNextIdentHash () const
{
return m_FirstHop->ident->GetIdentHash ();
}
virtual const i2p::data::IdentHash& GetLastIdentHash () const
{
return m_LastHop->ident->GetIdentHash ();
}
2015-11-03 14:15:49 +00:00
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetPeers () const
2015-05-08 18:07:33 +00:00
{
2015-11-03 14:15:49 +00:00
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
2018-01-06 03:48:51 +00:00
TunnelHopConfig * hop = m_FirstHop;
2015-05-08 18:07:33 +00:00
while (hop)
{
2015-11-03 14:15:49 +00:00
peers.push_back (hop->ident);
2015-05-08 18:07:33 +00:00
hop = hop->next;
2018-01-06 03:48:51 +00:00
}
2015-05-08 18:07:33 +00:00
return peers;
}
2018-01-06 03:48:51 +00:00
2016-03-02 21:12:02 +00:00
protected:
2013-12-07 00:02:49 +00:00
// this constructor can't be called from outside
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
{
}
2018-01-06 03:48:51 +00:00
2016-03-02 21:12:02 +00:00
private:
2015-11-03 14:15:49 +00:00
template<class Peers>
void CreatePeers (const Peers& peers)
{
TunnelHopConfig * prev = nullptr;
2016-08-08 22:53:37 +00:00
for (const auto& it: peers)
2015-11-03 14:15:49 +00:00
{
auto hop = new TunnelHopConfig (it);
if (prev)
prev->SetNext (hop);
2018-01-06 03:48:51 +00:00
else
2015-11-03 14:15:49 +00:00
m_FirstHop = hop;
prev = hop;
2018-01-06 03:48:51 +00:00
}
2015-11-03 14:15:49 +00:00
m_LastHop = prev;
}
2018-01-06 03:48:51 +00:00
2013-12-07 00:02:49 +00:00
private:
TunnelHopConfig * m_FirstHop, * m_LastHop;
2016-03-02 21:12:02 +00:00
};
2016-03-03 03:41:53 +00:00
class ZeroHopsTunnelConfig: public TunnelConfig
2016-03-02 21:12:02 +00:00
{
public:
2016-03-03 03:41:53 +00:00
ZeroHopsTunnelConfig () { RAND_bytes ((uint8_t *)&m_TunnelID, 4);};
2016-03-02 21:12:02 +00:00
2018-01-06 03:48:51 +00:00
bool IsInbound () const { return true; }; // TODO:
2016-03-02 21:12:02 +00:00
uint32_t GetTunnelID () const { return m_TunnelID; };
uint32_t GetNextTunnelID () const { return m_TunnelID; };
const i2p::data::IdentHash& GetNextIdentHash () const { return i2p::context.GetIdentHash (); };
const i2p::data::IdentHash& GetLastIdentHash () const { return i2p::context.GetIdentHash (); };
private:
2018-01-06 03:48:51 +00:00
uint32_t m_TunnelID;
};
}
}
2013-12-07 00:02:49 +00:00
#endif