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>
|
2014-11-21 18:46:11 +03:00
|
|
|
#include <memory>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include "Crypto.h"
|
|
|
|
#include "Identity.h"
|
2013-12-07 04:02:49 +04:00
|
|
|
#include "RouterContext.h"
|
2015-01-04 07:13:07 +03:00
|
|
|
#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;
|
2014-06-19 03:38:21 +04:00
|
|
|
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;
|
2014-11-21 18:46:11 +03:00
|
|
|
//nextRouter = nullptr;
|
2013-12-07 04:02:49 +04:00
|
|
|
nextTunnelID = 0;
|
|
|
|
|
2014-11-21 18:46:11 +03:00
|
|
|
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-01-04 07:13:07 +03:00
|
|
|
|
2015-05-06 23:17:48 +03:00
|
|
|
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
|
2015-01-04 07:13:07 +03:00
|
|
|
{
|
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);
|
2015-01-04 07:13:07 +03:00
|
|
|
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);
|
2015-11-03 17:15:49 +03: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);
|
2015-01-04 07:13:07 +03:00
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
};
|
|
|
|
|
2015-05-08 21:07:33 +03:00
|
|
|
class TunnelConfig: public std::enable_shared_from_this<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;
|
|
|
|
}
|
|
|
|
|
2014-06-26 03:28:33 +04:00
|
|
|
int GetNumHops () const
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
2014-06-26 03:28:33 +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;
|
|
|
|
}
|
|
|
|
|
2015-05-08 21:07:33 +03:00
|
|
|
bool IsInbound () const { return m_FirstHop->isGateway; }
|
|
|
|
|
2015-11-03 17:15:49 +03: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 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
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// this constructor can't be called from outside
|
|
|
|
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
|
|
|
|
{
|
|
|
|
}
|
2015-11-03 17:15:49 +03: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 04:02:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelHopConfig * m_FirstHop, * m_LastHop;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|