mirror of https://github.com/PurpleI2P/i2pd
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
6.2 KiB
C
253 lines
6.2 KiB
C
11 years ago
|
#ifndef TUNNEL_CONFIG_H__
|
||
|
#define TUNNEL_CONFIG_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <sstream>
|
||
11 years ago
|
#include <vector>
|
||
10 years ago
|
#include <memory>
|
||
9 years ago
|
#include "Crypto.h"
|
||
|
#include "Identity.h"
|
||
11 years ago
|
#include "RouterContext.h"
|
||
10 years ago
|
#include "Timestamp.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace tunnel
|
||
|
{
|
||
|
struct TunnelHopConfig
|
||
|
{
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> ident;
|
||
|
i2p::data::IdentHash nextIdent;
|
||
11 years ago
|
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;
|
||
10 years ago
|
int recordIndex; // record # in tunnel build message
|
||
10 years ago
|
|
||
9 years ago
|
TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r)
|
||
11 years ago
|
{
|
||
9 years ago
|
RAND_bytes (layerKey, 32);
|
||
|
RAND_bytes (ivKey, 32);
|
||
9 years ago
|
RAND_bytes (replyKey, 32);
|
||
9 years ago
|
RAND_bytes (replyIV, 16);
|
||
|
RAND_bytes ((uint8_t *)&tunnelID, 4);
|
||
11 years ago
|
isGateway = true;
|
||
|
isEndpoint = true;
|
||
9 years ago
|
ident = r;
|
||
10 years ago
|
//nextRouter = nullptr;
|
||
11 years ago
|
nextTunnelID = 0;
|
||
|
|
||
10 years ago
|
next = nullptr;
|
||
|
prev = nullptr;
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
void SetNextIdent (const i2p::data::IdentHash& ident)
|
||
11 years ago
|
{
|
||
9 years ago
|
nextIdent = ident;
|
||
11 years ago
|
isEndpoint = false;
|
||
9 years ago
|
RAND_bytes ((uint8_t *)&nextTunnelID, 4);
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
void SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent)
|
||
11 years ago
|
{
|
||
9 years ago
|
nextIdent = replyIdent;
|
||
|
nextTunnelID = replyTunnelID;
|
||
11 years ago
|
isEndpoint = true;
|
||
|
}
|
||
|
|
||
|
void SetNext (TunnelHopConfig * n)
|
||
|
{
|
||
|
next = n;
|
||
|
if (next)
|
||
|
{
|
||
|
next->prev = this;
|
||
|
next->isGateway = false;
|
||
|
isEndpoint = false;
|
||
9 years ago
|
nextIdent = next->ident->GetIdentHash ();
|
||
11 years ago
|
nextTunnelID = next->tunnelID;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SetPrev (TunnelHopConfig * p)
|
||
|
{
|
||
|
prev = p;
|
||
|
if (prev)
|
||
|
{
|
||
|
prev->next = this;
|
||
|
prev->isEndpoint = false;
|
||
|
isGateway = false;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx) const
|
||
10 years ago
|
{
|
||
10 years ago
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
||
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
||
9 years ago
|
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, ident->GetIdentHash (), 32);
|
||
10 years ago
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
||
9 years ago
|
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
|
||
10 years ago
|
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);
|
||
10 years ago
|
uint8_t flag = 0;
|
||
|
if (isGateway) flag |= 0x80;
|
||
|
if (isEndpoint) flag |= 0x40;
|
||
10 years ago
|
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);
|
||
9 years ago
|
RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET);
|
||
8 years ago
|
i2p::crypto::ElGamalEncrypt (ident->GetEncryptionPublicKey (), clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx);
|
||
9 years ago
|
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
|
||
10 years ago
|
}
|
||
11 years ago
|
};
|
||
|
|
||
9 years ago
|
class TunnelConfig
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers) // inbound
|
||
|
{
|
||
|
CreatePeers (peers);
|
||
|
m_LastHop->SetNextIdent (i2p::context.GetIdentHash ());
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers,
|
||
|
uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent) // outbound
|
||
11 years ago
|
{
|
||
9 years ago
|
CreatePeers (peers);
|
||
|
m_FirstHop->isGateway = false;
|
||
|
m_LastHop->SetReplyHop (replyTunnelID, replyIdent);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
~TunnelConfig ()
|
||
|
{
|
||
|
TunnelHopConfig * hop = m_FirstHop;
|
||
|
|
||
|
while (hop)
|
||
|
{
|
||
11 years ago
|
auto tmp = hop;
|
||
11 years ago
|
hop = hop->next;
|
||
11 years ago
|
delete tmp;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
TunnelHopConfig * GetFirstHop () const
|
||
|
{
|
||
|
return m_FirstHop;
|
||
|
}
|
||
|
|
||
|
TunnelHopConfig * GetLastHop () const
|
||
|
{
|
||
|
return m_LastHop;
|
||
|
}
|
||
|
|
||
10 years ago
|
int GetNumHops () const
|
||
11 years ago
|
{
|
||
10 years ago
|
int num = 0;
|
||
11 years ago
|
TunnelHopConfig * hop = m_FirstHop;
|
||
|
while (hop)
|
||
|
{
|
||
|
num++;
|
||
|
hop = hop->next;
|
||
|
}
|
||
|
return num;
|
||
|
}
|
||
|
|
||
8 years ago
|
bool IsEmpty () const
|
||
|
{
|
||
|
return !m_FirstHop;
|
||
|
}
|
||
|
|
||
9 years ago
|
virtual bool IsInbound () const { return m_FirstHop->isGateway; }
|
||
9 years ago
|
|
||
9 years ago
|
virtual uint32_t GetTunnelID () const
|
||
9 years ago
|
{
|
||
|
if (!m_FirstHop) return 0;
|
||
|
return IsInbound () ? m_LastHop->nextTunnelID : m_FirstHop->tunnelID;
|
||
|
}
|
||
|
|
||
9 years ago
|
virtual uint32_t GetNextTunnelID () const
|
||
9 years ago
|
{
|
||
|
if (!m_FirstHop) return 0;
|
||
|
return m_FirstHop->tunnelID;
|
||
|
}
|
||
|
|
||
9 years ago
|
virtual const i2p::data::IdentHash& GetNextIdentHash () const
|
||
9 years ago
|
{
|
||
|
return m_FirstHop->ident->GetIdentHash ();
|
||
|
}
|
||
|
|
||
9 years ago
|
virtual const i2p::data::IdentHash& GetLastIdentHash () const
|
||
9 years ago
|
{
|
||
|
return m_LastHop->ident->GetIdentHash ();
|
||
|
}
|
||
|
|
||
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetPeers () const
|
||
9 years ago
|
{
|
||
9 years ago
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
|
||
9 years ago
|
TunnelHopConfig * hop = m_FirstHop;
|
||
|
while (hop)
|
||
|
{
|
||
9 years ago
|
peers.push_back (hop->ident);
|
||
9 years ago
|
hop = hop->next;
|
||
|
}
|
||
|
return peers;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
protected:
|
||
11 years ago
|
|
||
|
// this constructor can't be called from outside
|
||
|
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
|
||
|
{
|
||
|
}
|
||
9 years ago
|
|
||
|
private:
|
||
9 years ago
|
|
||
|
template<class Peers>
|
||
|
void CreatePeers (const Peers& peers)
|
||
|
{
|
||
|
TunnelHopConfig * prev = nullptr;
|
||
8 years ago
|
for (const auto& it: peers)
|
||
9 years ago
|
{
|
||
|
auto hop = new TunnelHopConfig (it);
|
||
|
if (prev)
|
||
|
prev->SetNext (hop);
|
||
|
else
|
||
|
m_FirstHop = hop;
|
||
|
prev = hop;
|
||
|
}
|
||
|
m_LastHop = prev;
|
||
|
}
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
|
TunnelHopConfig * m_FirstHop, * m_LastHop;
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
class ZeroHopsTunnelConfig: public TunnelConfig
|
||
9 years ago
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
ZeroHopsTunnelConfig () { RAND_bytes ((uint8_t *)&m_TunnelID, 4);};
|
||
9 years ago
|
|
||
9 years ago
|
bool IsInbound () const { return true; }; // TODO:
|
||
9 years ago
|
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;
|
||
11 years ago
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|