i2pd/TunnelConfig.h

253 lines
6.2 KiB
C
Raw Normal View History

2013-12-07 04:02:49 +04:00
#ifndef TUNNEL_CONFIG_H__
#define TUNNEL_CONFIG_H__
#include <inttypes.h>
#include <sstream>
2014-01-09 04:30:47 +04:00
#include <vector>
#include <memory>
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
#include "Identity.h"
2013-12-07 04:02:49 +04:00
#include "RouterContext.h"
#include "Timestamp.h"
2013-12-07 04:02:49 +04:00
namespace i2p
{
namespace tunnel
{
struct TunnelHopConfig
{
2015-11-03 17:15:49 +03:00
std::shared_ptr<const i2p::data::IdentityEx> ident;
i2p::data::IdentHash nextIdent;
2013-12-07 04:02:49 +04:00
uint32_t tunnelID, nextTunnelID;
uint8_t layerKey[32];
uint8_t ivKey[32];
uint8_t replyKey[32];
uint8_t replyIV[16];
bool isGateway, isEndpoint;
TunnelHopConfig * next, * prev;
int recordIndex; // record # in tunnel build message
2014-05-10 03:34:12 +04:00
2015-11-03 17:15:49 +03:00
TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r)
2013-12-07 04:02:49 +04:00
{
2015-11-03 17:15:49 +03:00
RAND_bytes (layerKey, 32);
RAND_bytes (ivKey, 32);
2016-01-11 00:40:28 +03:00
RAND_bytes (replyKey, 32);
2015-11-03 17:15:49 +03:00
RAND_bytes (replyIV, 16);
RAND_bytes ((uint8_t *)&tunnelID, 4);
2013-12-07 04:02:49 +04:00
isGateway = true;
isEndpoint = true;
2015-11-03 17:15:49 +03:00
ident = r;
//nextRouter = nullptr;
2013-12-07 04:02:49 +04:00
nextTunnelID = 0;
next = nullptr;
prev = nullptr;
2013-12-07 04:02:49 +04:00
}
2015-11-03 17:15:49 +03:00
void SetNextIdent (const i2p::data::IdentHash& ident)
2013-12-07 04:02:49 +04:00
{
2015-11-03 17:15:49 +03:00
nextIdent = ident;
2013-12-07 04:02:49 +04:00
isEndpoint = false;
2015-11-03 17:15:49 +03:00
RAND_bytes ((uint8_t *)&nextTunnelID, 4);
2013-12-07 04:02:49 +04:00
}
2015-11-03 17:15:49 +03:00
void SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent)
2013-12-07 04:02:49 +04:00
{
2015-11-03 17:15:49 +03:00
nextIdent = replyIdent;
nextTunnelID = replyTunnelID;
2013-12-07 04:02:49 +04:00
isEndpoint = true;
}
void SetNext (TunnelHopConfig * n)
{
next = n;
if (next)
{
next->prev = this;
next->isGateway = false;
isEndpoint = false;
2015-11-03 17:15:49 +03:00
nextIdent = next->ident->GetIdentHash ();
2013-12-07 04:02:49 +04:00
nextTunnelID = next->tunnelID;
}
}
void SetPrev (TunnelHopConfig * p)
{
prev = p;
if (prev)
{
prev->next = this;
prev->isEndpoint = false;
isGateway = false;
}
}
2015-05-06 23:17:48 +03:00
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
{
2015-01-04 17:33:19 +03:00
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
2015-11-03 17:15:49 +03:00
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, ident->GetIdentHash (), 32);
2015-01-04 17:33:19 +03:00
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
2015-11-03 17:15:49 +03:00
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
2015-01-04 17:33:19 +03: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 17:33:19 +03:00
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag;
htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ());
htobe32buf (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
2016-01-11 00:40:28 +03:00
RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET);
2016-12-20 22:10:14 +03:00
i2p::crypto::ElGamalEncrypt (ident->GetEncryptionPublicKey (), clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
2015-11-03 17:15:49 +03:00
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
}
2013-12-07 04:02:49 +04:00
};
2016-03-03 00:12:02 +03:00
class TunnelConfig
2013-12-07 04:02:49 +04:00
{
public:
2015-11-03 17:15:49 +03:00
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers) // inbound
{
CreatePeers (peers);
m_LastHop->SetNextIdent (i2p::context.GetIdentHash ());
}
2013-12-07 04:02:49 +04:00
2015-11-03 17:15:49 +03:00
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers,
uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent) // outbound
2013-12-07 04:02:49 +04:00
{
2015-11-03 17:15:49 +03:00
CreatePeers (peers);
m_FirstHop->isGateway = false;
m_LastHop->SetReplyHop (replyTunnelID, replyIdent);
2014-01-09 04:30:47 +04:00
}
2013-12-07 04:02:49 +04:00
~TunnelConfig ()
{
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
2014-01-22 00:10:49 +04:00
auto tmp = hop;
2013-12-07 04:02:49 +04:00
hop = hop->next;
2014-01-22 00:10:49 +04:00
delete tmp;
2013-12-07 04:02:49 +04:00
}
}
TunnelHopConfig * GetFirstHop () const
{
return m_FirstHop;
}
TunnelHopConfig * GetLastHop () const
{
return m_LastHop;
}
int GetNumHops () const
2013-12-07 04:02:49 +04:00
{
int num = 0;
2013-12-07 04:02:49 +04:00
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
num++;
hop = hop->next;
}
return num;
}
bool IsEmpty () const
{
return !m_FirstHop;
}
2016-03-03 00:12:02 +03:00
virtual bool IsInbound () const { return m_FirstHop->isGateway; }
2015-05-08 21:07:33 +03:00
2016-03-03 00:12:02 +03:00
virtual uint32_t GetTunnelID () const
2015-11-03 17:15:49 +03:00
{
if (!m_FirstHop) return 0;
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
}
2016-03-03 00:12:02 +03:00
virtual uint32_t GetNextTunnelID () const
2015-11-03 17:15:49 +03:00
{
if (!m_FirstHop) return 0;
return m_FirstHop->tunnelID;
}
2016-03-03 00:12:02 +03:00
virtual const i2p::data::IdentHash& GetNextIdentHash () const
2015-11-03 17:15:49 +03:00
{
return m_FirstHop->ident->GetIdentHash ();
}
2016-03-03 00:12:02 +03:00
virtual const i2p::data::IdentHash& GetLastIdentHash () const
2015-11-03 17:15:49 +03:00
{
return m_LastHop->ident->GetIdentHash ();
}
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetPeers () const
2015-05-08 21:07:33 +03:00
{
2015-11-03 17:15:49 +03:00
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
2015-05-08 21:07:33 +03:00
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
2015-11-03 17:15:49 +03:00
peers.push_back (hop->ident);
2015-05-08 21:07:33 +03:00
hop = hop->next;
}
return peers;
}
2013-12-07 04:02:49 +04:00
2016-03-03 00:12:02 +03:00
protected:
2013-12-07 04:02:49 +04:00
// this constructor can't be called from outside
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
{
}
2016-03-03 00:12:02 +03:00
private:
2015-11-03 17:15:49 +03:00
template<class Peers>
void CreatePeers (const Peers& peers)
{
TunnelHopConfig * prev = nullptr;
2016-08-09 01:53:37 +03:00
for (const auto& it: peers)
2015-11-03 17:15:49 +03:00
{
auto hop = new TunnelHopConfig (it);
if (prev)
prev->SetNext (hop);
else
m_FirstHop = hop;
prev = hop;
}
m_LastHop = prev;
}
2013-12-07 04:02:49 +04:00
private:
TunnelHopConfig * m_FirstHop, * m_LastHop;
2016-03-03 00:12:02 +03:00
};
2016-03-03 06:41:53 +03:00
class ZeroHopsTunnelConfig: public TunnelConfig
2016-03-03 00:12:02 +03:00
{
public:
2016-03-03 06:41:53 +03:00
ZeroHopsTunnelConfig () { RAND_bytes ((uint8_t *)&m_TunnelID, 4);};
2016-03-03 00:12:02 +03:00
2016-03-03 06:41:53 +03:00
bool IsInbound () const { return true; }; // TODO:
2016-03-03 00:12:02 +03: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:
uint32_t m_TunnelID;
2013-12-07 04:02:49 +04:00
};
}
}
#endif