i2pd/TunnelConfig.h

248 lines
6.0 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 <openssl/rand.h>
#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);
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);
// TODO: fill padding
2015-11-03 14:15:49 +00:00
i2p::crypto::ElGamalEncryption elGamalEncryption (ident->GetEncryptionPublicKey ());
elGamalEncryption.Encrypt (clearText, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
}
2013-12-07 00:02:49 +00:00
};
2015-05-08 18:07:33 +00:00
class TunnelConfig: public std::enable_shared_from_this<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;
}
2015-05-08 18:07:33 +00:00
bool IsInbound () const { return m_FirstHop->isGateway; }
2015-11-03 14:15:49 +00:00
uint32_t GetTunnelID () const
{
if (!m_FirstHop) return 0;
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
}
uint32_t GetNextTunnelID () const
{
if (!m_FirstHop) return 0;
return m_FirstHop->tunnelID;
}
const i2p::data::IdentHash& GetNextIdentHash () const
{
return m_FirstHop->ident->GetIdentHash ();
}
const i2p::data::IdentHash& GetLastIdentHash () const
{
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
void Print (std::stringstream& s) const
{
TunnelHopConfig * hop = m_FirstHop;
2015-05-08 18:07:33 +00:00
if (!IsInbound ()) // outbound
2013-12-07 00:02:49 +00:00
s << "me";
s << "-->" << m_FirstHop->tunnelID;
while (hop)
{
2015-11-03 14:15:49 +00:00
s << ":" << GetIdentHashAbbreviation (hop->ident->GetIdentHash ()) << "-->";
2013-12-07 00:02:49 +00:00
if (!hop->isEndpoint)
s << hop->nextTunnelID;
else
return;
hop = hop->next;
}
// we didn't reach enpoint that mean we are last hop
s << ":me";
}
private:
// this constructor can't be called from outside
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
{
}
2015-11-03 14:15:49 +00:00
template<class Peers>
void CreatePeers (const Peers& peers)
{
TunnelHopConfig * prev = nullptr;
for (auto it: peers)
{
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;
};
}
}
#endif