i2pd/TunnelConfig.h

247 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>
2014-05-10 03:34:12 +04:00
#include "aes.h"
2013-12-07 04:02:49 +04:00
#include "RouterInfo.h"
#include "RouterContext.h"
#include "Timestamp.h"
2013-12-07 04:02:49 +04:00
namespace i2p
{
namespace tunnel
{
struct TunnelHopConfig
{
std::shared_ptr<const i2p::data::RouterInfo> router, nextRouter;
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-05-16 02:58:26 +04:00
i2p::crypto::TunnelDecryption decryption;
int recordIndex; // record # in tunnel build message
2014-05-10 03:34:12 +04:00
TunnelHopConfig (std::shared_ptr<const i2p::data::RouterInfo> r)
2013-12-07 04:02:49 +04:00
{
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (layerKey, 32);
rnd.GenerateBlock (ivKey, 32);
rnd.GenerateBlock (replyIV, 16);
tunnelID = rnd.GenerateWord32 ();
isGateway = true;
isEndpoint = true;
router = 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
}
void SetNextRouter (std::shared_ptr<const i2p::data::RouterInfo> r)
2013-12-07 04:02:49 +04:00
{
nextRouter = r;
isEndpoint = false;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
nextTunnelID = rnd.GenerateWord32 ();
}
2014-08-09 06:44:33 +04:00
void SetReplyHop (const TunnelHopConfig * replyFirstHop)
2013-12-07 04:02:49 +04:00
{
nextRouter = replyFirstHop->router;
nextTunnelID = replyFirstHop->tunnelID;
isEndpoint = true;
}
void SetNext (TunnelHopConfig * n)
{
next = n;
if (next)
{
next->prev = this;
next->isGateway = false;
isEndpoint = false;
nextRouter = next->router;
nextTunnelID = next->tunnelID;
}
}
void SetPrev (TunnelHopConfig * p)
{
prev = p;
if (prev)
{
prev->next = this;
prev->isEndpoint = false;
isGateway = false;
}
}
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID)
{
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);
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, router->GetIdentHash (), 32);
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextRouter->GetIdentHash (), 32);
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);
// TODO: fill padding
2015-01-04 17:33:19 +03:00
router->GetElGamalEncryption ()->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 *)router->GetIdentHash (), 16);
}
2013-12-07 04:02:49 +04:00
};
class TunnelConfig
{
public:
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > peers,
2014-08-09 16:21:55 +04:00
const TunnelConfig * replyTunnelConfig = nullptr) // replyTunnelConfig=nullptr means inbound
2013-12-07 04:02:49 +04:00
{
2014-01-09 04:30:47 +04:00
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
2014-01-09 04:30:47 +04:00
if (replyTunnelConfig) // outbound
2013-12-07 04:02:49 +04:00
{
m_FirstHop->isGateway = false;
m_LastHop->SetReplyHop (replyTunnelConfig->GetFirstHop ());
}
2014-01-09 04:30:47 +04:00
else // inbound
m_LastHop->SetNextRouter (i2p::context.GetSharedRouterInfo ());
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;
}
void Print (std::stringstream& s) const
{
TunnelHopConfig * hop = m_FirstHop;
if (!m_FirstHop->isGateway)
s << "me";
s << "-->" << m_FirstHop->tunnelID;
while (hop)
{
s << ":" << hop->router->GetIdentHashAbbreviation () << "-->";
if (!hop->isEndpoint)
s << hop->nextTunnelID;
else
return;
hop = hop->next;
}
// we didn't reach enpoint that mean we are last hop
s << ":me";
}
TunnelConfig * Invert () const
{
TunnelConfig * newConfig = new TunnelConfig ();
TunnelHopConfig * hop = m_FirstHop, * nextNewHop = nullptr;
while (hop)
{
TunnelHopConfig * newHop = new TunnelHopConfig (hop->router);
if (nextNewHop)
newHop->SetNext (nextNewHop);
nextNewHop = newHop;
newHop->isEndpoint = hop->isGateway;
newHop->isGateway = hop->isEndpoint;
if (!hop->prev) // first hop
{
newConfig->m_LastHop = newHop;
if (hop->isGateway) // inbound tunnel
newHop->SetReplyHop (m_FirstHop); // use it as reply tunnel
else
newHop->SetNextRouter (i2p::context.GetSharedRouterInfo ());
2013-12-07 04:02:49 +04:00
}
if (!hop->next) newConfig->m_FirstHop = newHop; // last hop
hop = hop->next;
}
return newConfig;
}
2014-08-09 06:44:33 +04:00
TunnelConfig * Clone (const TunnelConfig * replyTunnelConfig = nullptr) const
{
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > peers;
2014-08-09 16:21:55 +04:00
TunnelHopConfig * hop = m_FirstHop;
2014-08-09 06:44:33 +04:00
while (hop)
{
2014-08-09 16:21:55 +04:00
peers.push_back (hop->router);
2014-08-09 06:44:33 +04:00
hop = hop->next;
2014-08-09 16:21:55 +04:00
}
return new TunnelConfig (peers, replyTunnelConfig);
2014-08-09 06:44:33 +04:00
}
2013-12-07 04:02:49 +04:00
private:
// this constructor can't be called from outside
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
{
}
private:
TunnelHopConfig * m_FirstHop, * m_LastHop;
};
}
}
#endif