i2pd/TunnelConfig.h

254 lines
6.2 KiB
C
Raw Normal View History

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 "Crypto.h"
#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];
bool isGateway, isEndpoint;
TunnelHopConfig * next, * prev;
int recordIndex; // record # in tunnel build message
2014-05-09 23:34:12 +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);
2013-12-07 00:02:49 +00:00
isGateway = true;
isEndpoint = true;
2015-11-03 14:15:49 +00:00
ident = r;
//nextRouter = nullptr;
2013-12-07 00:02:49 +00:00
nextTunnelID = 0;
next = nullptr;
prev = nullptr;
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);
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;
}
void SetNext (TunnelHopConfig * n)
{
next = n;
if (next)
{
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;
}
}
void SetPrev (TunnelHopConfig * p)
{
prev = p;
if (prev)
{
prev->next = this;
prev->isEndpoint = false;
isGateway = false;
}
}
2015-05-06 20:17:48 +00:00
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
{
2015-01-04 14:33:19 +00:00
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
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;
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);
2015-11-03 14:15:49 +00:00
i2p::crypto::ElGamalEncryption elGamalEncryption (ident->GetEncryptionPublicKey ());
2016-11-24 21:02:14 +00:00
elGamalEncryption.Encrypt (clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
2015-11-03 14:15:49 +00:00
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
}
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
{
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
2015-11-03 14:15:49 +00:00
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers,
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
}
2013-12-07 00:02:49 +00:00
~TunnelConfig ()
{
TunnelHopConfig * hop = m_FirstHop;
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;
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;
2013-12-07 00:02:49 +00:00
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
num++;
hop = hop->next;
}
return num;
}
bool IsEmpty () const
{
return !m_FirstHop;
}
2016-03-02 21:12:02 +00:00
virtual bool IsInbound () const { return m_FirstHop->isGateway; }
2015-05-08 18:07:33 +00:00
2016-03-02 21:12:02 +00:00
virtual uint32_t GetTunnelID () const
2015-11-03 14:15:49 +00:00
{
if (!m_FirstHop) return 0;
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
}
2016-03-02 21:12:02 +00:00
virtual uint32_t GetNextTunnelID () const
2015-11-03 14:15:49 +00:00
{
if (!m_FirstHop) return 0;
return m_FirstHop->tunnelID;
}
2016-03-02 21:12:02 +00:00
virtual const i2p::data::IdentHash& GetNextIdentHash () const
2015-11-03 14:15:49 +00:00
{
return m_FirstHop->ident->GetIdentHash ();
}
2016-03-02 21:12:02 +00:00
virtual const i2p::data::IdentHash& GetLastIdentHash () const
2015-11-03 14:15:49 +00:00
{
return m_LastHop->ident->GetIdentHash ();
}
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;
2015-05-08 18:07:33 +00:00
TunnelHopConfig * hop = m_FirstHop;
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;
}
return peers;
}
2013-12-07 00:02:49 +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)
{
}
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);
else
m_FirstHop = hop;
prev = hop;
}
m_LastHop = prev;
}
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
2016-03-03 03:41:53 +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:
uint32_t m_TunnelID;
2013-12-07 00:02:49 +00:00
};
}
}
#endif