i2pd/libi2pd/RouterContext.cpp

681 lines
19 KiB
C++
Raw Normal View History

2013-10-23 06:45:40 +04:00
#include <fstream>
2018-06-11 22:33:48 +03:00
#include <openssl/rand.h>
2016-01-20 03:00:00 +03:00
#include "Config.h"
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
2018-06-15 19:52:43 +03:00
#include "Ed25519.h"
#include "Timestamp.h"
2014-10-12 05:27:55 +04:00
#include "I2NPProtocol.h"
#include "NetDb.hpp"
#include "FS.h"
2014-01-30 04:56:48 +04:00
#include "util.h"
#include "version.h"
2015-11-03 17:15:49 +03:00
#include "Log.h"
2016-02-21 04:20:19 +03:00
#include "Family.h"
2015-11-03 17:15:49 +03:00
#include "RouterContext.h"
2013-10-23 06:45:40 +04:00
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ():
2017-10-12 13:52:36 +03:00
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
m_StartupTime (0), m_ShareRatio (100), m_Status (eRouterStatusOK),
m_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID)
2014-09-04 17:31:42 +04:00
{
}
void RouterContext::Init ()
2013-10-23 06:45:40 +04:00
{
2015-11-03 17:15:49 +03:00
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
2015-02-23 22:41:56 +03:00
m_StartupTime = i2p::util::GetSecondsSinceEpoch ();
2013-10-23 06:45:40 +04:00
if (!Load ())
CreateNewRouter ();
2018-01-06 06:48:51 +03:00
m_Decryptor = m_Keys.CreateDecryptor (nullptr);
UpdateRouterInfo ();
}
2013-10-23 06:45:40 +04:00
void RouterContext::CreateNewRouter ()
{
2015-11-03 21:05:37 +03:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
2018-06-15 19:52:43 +03:00
SaveKeys ();
NewRouterInfo ();
2014-02-23 20:48:09 +04:00
}
void RouterContext::NewRouterInfo ()
2014-02-23 20:48:09 +04:00
{
2014-02-24 05:48:28 +04:00
i2p::data::RouterInfo routerInfo;
2014-11-20 20:21:27 +03:00
routerInfo.SetRouterIdentity (GetIdentity ());
2016-01-20 03:00:00 +03:00
uint16_t port; i2p::config::GetOption("port", port);
if (!port)
2019-01-23 17:53:30 +03:00
{
2015-11-03 17:15:49 +03:00
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
2019-01-23 17:53:30 +03:00
if (port == 9150) port = 9151; // Tor browser
}
2016-07-15 20:52:55 +03:00
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool ssu; i2p::config::GetOption("ssu", ssu);
bool ntcp; i2p::config::GetOption("ntcp", ntcp);
2018-07-19 16:45:24 +03:00
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
2018-06-15 19:52:43 +03:00
bool nat; i2p::config::GetOption("nat", nat);
std::string ifname; i2p::config::GetOption("ifname", ifname);
2016-11-24 21:56:37 +03:00
std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
2016-07-22 17:16:57 +03:00
if (ipv4)
{
std::string host = "127.0.0.1";
if (!i2p::config::IsDefault("host"))
i2p::config::GetOption("host", host);
2016-07-22 17:16:57 +03:00
else if (!nat && !ifname.empty())
/* bind to interface, we have no NAT so set external address too */
host = i2p::util::net::GetInterfaceAddress(ifname, false).to_string(); // v4
2016-11-24 21:56:37 +03:00
if(ifname4.size())
host = i2p::util::net::GetInterfaceAddress(ifname4, false).to_string();
if (ssu)
routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
if (ntcp)
routerInfo.AddNTCPAddress (host.c_str(), port);
2016-07-22 17:16:57 +03:00
}
if (ipv6)
{
std::string host = "::";
2016-07-22 17:34:56 +03:00
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
i2p::config::GetOption("host", host);
2017-10-12 13:52:36 +03:00
else if (!ifname.empty())
2016-07-22 17:16:57 +03:00
host = i2p::util::net::GetInterfaceAddress(ifname, true).to_string(); // v6
2016-11-24 18:11:46 +03:00
2016-11-24 21:56:37 +03:00
if(ifname6.size())
host = i2p::util::net::GetInterfaceAddress(ifname6, true).to_string();
if (ssu)
routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
if (ntcp)
routerInfo.AddNTCPAddress (host.c_str(), port);
2016-11-24 18:11:46 +03:00
}
2017-10-12 13:52:36 +03:00
routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |
2014-09-05 15:26:36 +04:00
i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer); // LR, BC
2017-10-12 13:52:36 +03:00
routerInfo.SetProperty ("netId", std::to_string (m_NetID));
routerInfo.SetProperty ("router.version", I2P_VERSION);
2014-08-26 06:47:12 +04:00
routerInfo.CreateBuffer (m_Keys);
2015-11-03 17:15:49 +03:00
m_RouterInfo.SetRouterIdentity (GetIdentity ());
2014-07-22 16:03:02 +04:00
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
if (ntcp2) // we don't store iv in the address if non published so we must update it from keys
{
if (!m_NTCP2Keys) NewNTCP2Keys ();
UpdateNTCP2Address (true);
if (!ntcp) // NTCP2 should replace NTCP
{
bool published; i2p::config::GetOption("ntcp2.published", published);
if (published)
PublishNTCP2Address (port, true);
}
}
}
void RouterContext::UpdateRouterInfo ()
{
m_RouterInfo.CreateBuffer (m_Keys);
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
2017-10-12 13:52:36 +03:00
}
2014-09-11 17:32:34 +04:00
2018-06-11 22:33:48 +03:00
void RouterContext::NewNTCP2Keys ()
{
2018-11-01 17:43:31 +03:00
m_StaticKeys.reset (new i2p::crypto::X25519Keys ());
m_StaticKeys->GenerateKeys ();
2018-06-11 22:33:48 +03:00
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
2018-11-01 17:43:31 +03:00
m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32);
2018-06-11 22:33:48 +03:00
RAND_bytes (m_NTCP2Keys->iv, 16);
// save
std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
2018-06-15 19:52:43 +03:00
fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
2018-06-11 22:33:48 +03:00
}
2017-10-12 13:52:36 +03:00
void RouterContext::SetStatus (RouterStatus status)
{
2015-11-03 17:15:49 +03:00
if (status != m_Status)
2017-10-12 13:52:36 +03:00
{
2015-11-03 17:15:49 +03:00
m_Status = status;
2016-09-20 04:37:04 +03:00
m_Error = eRouterErrorNone;
2015-11-03 17:15:49 +03:00
switch (m_Status)
2017-10-12 13:52:36 +03:00
{
2015-11-03 17:15:49 +03:00
case eRouterStatusOK:
SetReachable ();
break;
case eRouterStatusFirewalled:
SetUnreachable ();
2017-10-12 13:52:36 +03:00
break;
2015-11-03 17:15:49 +03:00
default:
;
}
2017-10-12 13:52:36 +03:00
}
2015-11-03 17:15:49 +03:00
}
2017-10-12 13:52:36 +03:00
2014-09-11 17:32:34 +04:00
void RouterContext::UpdatePort (int port)
2013-12-10 17:10:49 +04:00
{
2014-09-11 17:32:34 +04:00
bool updated = false;
2016-08-08 00:52:18 +03:00
for (auto& address : m_RouterInfo.GetAddresses ())
2013-12-10 17:10:49 +04:00
{
2018-07-23 20:51:29 +03:00
if (!address->IsNTCP2 () && address->port != port)
2017-10-12 13:52:36 +03:00
{
2016-03-21 20:02:51 +03:00
address->port = port;
2014-09-11 17:32:34 +04:00
updated = true;
2017-10-12 13:52:36 +03:00
}
}
2014-09-11 17:32:34 +04:00
if (updated)
UpdateRouterInfo ();
}
2014-02-09 06:06:40 +04:00
2018-08-03 21:49:28 +03:00
void RouterContext::PublishNTCP2Address (int port, bool publish)
2018-07-23 20:51:29 +03:00
{
if (!m_NTCP2Keys) return;
if (!port)
2019-01-23 17:53:30 +03:00
{
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
2019-01-23 17:53:30 +03:00
if (port == 9150) port = 9151; // Tor browser
}
2018-07-23 20:51:29 +03:00
bool updated = false;
for (auto& address : m_RouterInfo.GetAddresses ())
{
2018-08-03 21:49:28 +03:00
if (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish))
2018-07-23 20:51:29 +03:00
{
address->port = port;
address->cost = publish ? 3 : 14;
2018-08-03 21:49:28 +03:00
address->ntcp2->isPublished = publish;
address->ntcp2->iv = m_NTCP2Keys->iv;
2018-07-23 20:51:29 +03:00
updated = true;
}
}
if (updated)
UpdateRouterInfo ();
}
2018-08-03 21:49:28 +03:00
void RouterContext::UpdateNTCP2Address (bool enable)
{
auto& addresses = m_RouterInfo.GetAddresses ();
bool found = false, updated = false;
for (auto it = addresses.begin (); it != addresses.end (); ++it)
{
if ((*it)->IsNTCP2 ())
{
found = true;
if (!enable)
{
addresses.erase (it);
updated= true;
}
break;
}
}
if (enable && !found)
{
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
updated = true;
}
if (updated)
UpdateRouterInfo ();
}
2014-10-29 20:49:21 +03:00
void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
2014-02-09 06:06:40 +04:00
{
bool updated = false;
2016-08-08 00:52:18 +03:00
for (auto& address : m_RouterInfo.GetAddresses ())
{
2016-03-21 20:02:51 +03:00
if (address->host != host && address->IsCompatible (host))
2017-10-12 13:52:36 +03:00
{
2016-03-21 20:02:51 +03:00
address->host = host;
updated = true;
2017-10-12 13:52:36 +03:00
}
}
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
2013-10-23 06:45:40 +04:00
}
2015-11-03 17:15:49 +03:00
bool RouterContext::AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer)
2014-09-02 01:34:20 +04:00
{
2015-11-03 17:15:49 +03:00
bool ret = m_RouterInfo.AddIntroducer (introducer);
if (ret)
2017-10-12 13:52:36 +03:00
UpdateRouterInfo ();
return ret;
2017-10-12 13:52:36 +03:00
}
2014-09-02 01:34:20 +04:00
2014-09-07 04:43:20 +04:00
void RouterContext::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
2014-09-02 01:34:20 +04:00
{
2014-09-07 04:43:20 +04:00
if (m_RouterInfo.RemoveIntroducer (e))
2014-09-02 01:34:20 +04:00
UpdateRouterInfo ();
2017-10-12 13:52:36 +03:00
}
2015-01-28 23:12:15 +03:00
void RouterContext::SetFloodfill (bool floodfill)
{
m_IsFloodfill = floodfill;
if (floodfill)
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eFloodfill);
else
{
2015-01-28 23:12:15 +03:00
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eFloodfill);
// we don't publish number of routers and leaseset for non-floodfill
2016-02-21 04:20:19 +03:00
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS);
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS);
}
2015-01-28 23:12:15 +03:00
UpdateRouterInfo ();
}
std::string RouterContext::GetFamily () const
2016-04-27 01:48:23 +03:00
{
return m_RouterInfo.GetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
}
2016-02-21 04:20:19 +03:00
void RouterContext::SetFamily (const std::string& family)
{
std::string signature;
if (family.length () > 0)
signature = i2p::data::CreateFamilySignature (family, GetIdentHash ());
if (signature.length () > 0)
{
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY, family);
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG, signature);
2017-10-12 13:52:36 +03:00
}
2016-02-21 04:20:19 +03:00
else
{
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
2017-10-12 13:52:36 +03:00
}
}
2015-03-19 18:14:21 +03:00
2018-01-06 06:48:51 +03:00
void RouterContext::SetBandwidth (char L)
2017-10-05 17:37:28 +03:00
{
uint32_t limit = 0;
2017-01-26 00:14:01 +03:00
enum { low, high, extra, unlim } type = high;
/* detect parameters */
2017-10-12 13:52:36 +03:00
switch (L)
2016-03-31 04:31:17 +03:00
{
case i2p::data::CAPS_FLAG_LOW_BANDWIDTH1 : limit = 12; type = low; break;
case i2p::data::CAPS_FLAG_LOW_BANDWIDTH2 : limit = 48; type = low; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH1 : limit = 64; type = high; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH2 : limit = 128; type = high; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH3 : limit = 256; type = high; break;
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH1 : limit = 2048; type = extra; break;
2017-10-05 17:37:28 +03:00
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH2 : limit = 1000000; type = unlim; break; // 1Gbyte/s
2016-03-31 04:31:17 +03:00
default:
limit = 48; type = low;
}
/* update caps & flags in RI */
auto caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eHighBandwidth;
caps &= ~i2p::data::RouterInfo::eExtraBandwidth;
2017-10-12 13:52:36 +03:00
switch (type)
2016-03-31 04:31:17 +03:00
{
case low : /* not set */; break;
2017-01-26 00:14:01 +03:00
case extra : caps |= i2p::data::RouterInfo::eExtraBandwidth; break; // 'P'
case unlim : caps |= i2p::data::RouterInfo::eExtraBandwidth; // no break here, extra + high means 'X'
case high : caps |= i2p::data::RouterInfo::eHighBandwidth; break;
}
m_RouterInfo.SetCaps (caps);
UpdateRouterInfo ();
m_BandwidthLimit = limit;
2015-03-19 18:14:21 +03:00
}
2017-10-12 13:52:36 +03:00
void RouterContext::SetBandwidth (int limit)
2016-03-31 04:31:17 +03:00
{
if (limit > 2000) { SetBandwidth('X'); }
else if (limit > 256) { SetBandwidth('P'); }
else if (limit > 128) { SetBandwidth('O'); }
else if (limit > 64) { SetBandwidth('N'); }
else if (limit > 48) { SetBandwidth('M'); }
else if (limit > 12) { SetBandwidth('L'); }
else { SetBandwidth('K'); }
2016-01-03 06:17:04 +03:00
}
void RouterContext::SetShareRatio (int percents)
{
if (percents < 0) percents = 0;
if (percents > 100) percents = 100;
m_ShareRatio = percents;
}
bool RouterContext::IsUnreachable () const
{
return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
2017-10-12 13:52:36 +03:00
}
2019-04-17 04:04:04 +03:00
void RouterContext::PublishNTCPAddress (bool publish, bool v4only)
{
auto& addresses = m_RouterInfo.GetAddresses ();
if (publish)
{
for (const auto& addr : addresses) // v4
{
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
addr->host.is_v4 ())
{
// insert NTCP address with host/port from SSU
m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port);
break;
}
}
if (!v4only)
{
for (const auto& addr : addresses) // v6
{
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
addr->host.is_v6 ())
{
// insert NTCP address with host/port from SSU
m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port);
break;
}
}
}
}
else
{
for (auto it = addresses.begin (); it != addresses.end ();)
{
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () &&
(!v4only || (*it)->host.is_v4 ()))
{
it = addresses.erase (it);
if (v4only) break; // otherwise might be more than one address
}
else
++it;
}
}
}
2014-09-09 00:43:20 +04:00
void RouterContext::SetUnreachable ()
{
// set caps
2017-01-26 00:37:21 +03:00
uint8_t caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eReachable;
caps |= i2p::data::RouterInfo::eUnreachable;
caps &= ~i2p::data::RouterInfo::eFloodfill; // can't be floodfill
caps &= ~i2p::data::RouterInfo::eSSUIntroducer; // can't be introducer
2017-10-12 13:52:36 +03:00
m_RouterInfo.SetCaps (caps);
2019-04-17 04:04:04 +03:00
// remove NTCP v4 address
PublishNTCPAddress (false);
2014-09-09 05:53:55 +04:00
// delete previous introducers
2019-04-17 04:04:04 +03:00
auto& addresses = m_RouterInfo.GetAddresses ();
2017-10-12 13:52:36 +03:00
for (auto& addr : addresses)
if (addr->ssu)
addr->ssu->introducers.clear ();
2014-09-09 00:43:20 +04:00
// update
UpdateRouterInfo ();
}
void RouterContext::SetReachable ()
{
// update caps
uint8_t caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eUnreachable;
caps |= i2p::data::RouterInfo::eReachable;
caps |= i2p::data::RouterInfo::eSSUIntroducer;
if (m_IsFloodfill)
caps |= i2p::data::RouterInfo::eFloodfill;
m_RouterInfo.SetCaps (caps);
// insert NTCP back
bool ntcp; i2p::config::GetOption("ntcp", ntcp);
2019-04-17 04:04:04 +03:00
if (ntcp)
PublishNTCPAddress (true);
// delete previous introducers
2019-04-17 04:04:04 +03:00
auto& addresses = m_RouterInfo.GetAddresses ();
2016-08-08 00:52:18 +03:00
for (auto& addr : addresses)
if (addr->ssu)
addr->ssu->introducers.clear ();
// update
UpdateRouterInfo ();
2017-10-12 13:52:36 +03:00
}
void RouterContext::SetSupportsV6 (bool supportsV6)
{
if (supportsV6)
m_RouterInfo.EnableV6 ();
else
m_RouterInfo.DisableV6 ();
2014-10-30 01:46:35 +03:00
UpdateRouterInfo ();
2016-03-25 01:44:41 +03:00
}
2017-10-12 13:52:36 +03:00
2016-03-25 01:44:41 +03:00
void RouterContext::SetSupportsV4 (bool supportsV4)
{
if (supportsV4)
m_RouterInfo.EnableV4 ();
else
m_RouterInfo.DisableV4 ();
UpdateRouterInfo ();
}
2017-10-12 13:52:36 +03:00
2014-10-27 22:08:50 +03:00
2014-10-29 20:49:21 +03:00
void RouterContext::UpdateNTCPV6Address (const boost::asio::ip::address& host)
2014-10-27 22:08:50 +03:00
{
2017-10-12 13:52:36 +03:00
bool updated = false, found = false;
2014-10-27 22:08:50 +03:00
int port = 0;
auto& addresses = m_RouterInfo.GetAddresses ();
2016-08-08 00:52:18 +03:00
for (auto& addr: addresses)
2014-10-27 22:08:50 +03:00
{
2016-03-21 20:02:51 +03:00
if (addr->host.is_v6 () && addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
2014-10-27 22:08:50 +03:00
{
2016-03-21 20:02:51 +03:00
if (addr->host != host)
2014-10-27 22:08:50 +03:00
{
2016-03-21 20:02:51 +03:00
addr->host = host;
2014-10-27 22:08:50 +03:00
updated = true;
}
2017-10-12 13:52:36 +03:00
found = true;
}
2014-10-27 22:08:50 +03:00
else
2017-10-12 13:52:36 +03:00
port = addr->port;
}
2014-10-27 22:08:50 +03:00
if (!found)
{
// create new address
2014-10-29 20:49:21 +03:00
m_RouterInfo.AddNTCPAddress (host.to_string ().c_str (), port);
2014-11-01 01:27:51 +03:00
auto mtu = i2p::util::net::GetMTU (host);
if (mtu)
2017-10-12 13:52:36 +03:00
{
2015-12-18 16:49:57 +03:00
LogPrint (eLogDebug, "Router: Our v6 MTU=", mtu);
if (mtu > 1472) { // TODO: magic constant
mtu = 1472;
LogPrint(eLogWarning, "Router: MTU dropped to upper limit of 1472 bytes");
}
2017-10-12 13:52:36 +03:00
}
2014-11-01 01:27:51 +03:00
m_RouterInfo.AddSSUAddress (host.to_string ().c_str (), port, GetIdentHash (), mtu ? mtu : 1472); // TODO
2014-10-27 22:08:50 +03:00
updated = true;
}
if (updated)
UpdateRouterInfo ();
}
2015-03-18 22:36:07 +03:00
void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
{
2018-08-28 01:56:57 +03:00
bool updated = false, found = false;
int port = 0;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr: addresses)
{
if (addr->IsPublishedNTCP2 ())
{
if (addr->host.is_v6 ())
{
if (addr->host != host)
{
addr->host = host;
updated = true;
}
2018-08-28 01:56:57 +03:00
found = true;
break;
}
else
port = addr->port; // NTCP2 v4
}
}
2018-08-28 01:56:57 +03:00
if (!found && port) // we have found NTCP2 v4 but not v6
{
2018-08-27 23:01:47 +03:00
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, host, port);
updated = true;
}
2018-08-27 23:01:47 +03:00
if (updated)
UpdateRouterInfo ();
}
2015-03-18 22:36:07 +03:00
void RouterContext::UpdateStats ()
{
if (m_IsFloodfill)
{
// update routers and leasesets
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS, std::to_string(i2p::data::netdb.GetNumLeaseSets ()));
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS, std::to_string(i2p::data::netdb.GetNumRouters ()));
2017-10-12 13:52:36 +03:00
UpdateRouterInfo ();
2015-03-18 22:36:07 +03:00
}
}
2017-10-12 13:52:36 +03:00
2018-09-21 17:13:18 +03:00
void RouterContext::UpdateTimestamp (uint64_t ts)
{
if (ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
}
2013-10-23 06:45:40 +04:00
bool RouterContext::Load ()
{
std::ifstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ifstream::in | std::ifstream::binary);
2013-10-23 06:45:40 +04:00
if (!fk.is_open ()) return false;
2015-11-03 21:05:37 +03:00
fk.seekg (0, std::ios::end);
size_t len = fk.tellg();
2017-10-12 13:52:36 +03:00
fk.seekg (0, std::ios::beg);
2015-11-03 21:05:37 +03:00
if (len == sizeof (i2p::data::Keys)) // old keys file format
{
2017-10-12 13:52:36 +03:00
i2p::data::Keys keys;
2015-11-03 21:05:37 +03:00
fk.read ((char *)&keys, sizeof (keys));
m_Keys = keys;
}
else // new keys file format
{
uint8_t * buf = new uint8_t[len];
fk.read ((char *)buf, len);
m_Keys.FromBuffer (buf, len);
delete[] buf;
}
2018-08-04 04:16:35 +03:00
// read NTCP2 keys if available
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
if (n2k)
{
n2k.seekg (0, std::ios::end);
len = n2k.tellg();
n2k.seekg (0, std::ios::beg);
if (len == sizeof (NTCP2PrivateKeys))
{
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
}
n2k.close ();
}
// read RouterInfo
2015-11-03 17:15:49 +03:00
m_RouterInfo.SetRouterIdentity (GetIdentity ());
2017-10-12 13:52:36 +03:00
i2p::data::RouterInfo routerInfo(i2p::fs::DataDirPath (ROUTER_INFO));
if (!routerInfo.IsUnreachable ()) // router.info looks good
{
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
m_RouterInfo.SetProperty ("coreVersion", I2P_VERSION);
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
// Migration to 0.9.24. TODO: remove later
m_RouterInfo.DeleteProperty ("coreVersion");
m_RouterInfo.DeleteProperty ("stat_uptime");
}
else
{
LogPrint (eLogError, ROUTER_INFO, " is malformed. Creating new");
NewRouterInfo ();
2017-10-12 13:52:36 +03:00
}
if (IsUnreachable ())
SetReachable (); // we assume reachable until we discover firewall through peer tests
2017-10-12 13:52:36 +03:00
2018-06-11 22:33:48 +03:00
// read NTCP2
2018-07-19 16:45:24 +03:00
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
2018-06-11 22:33:48 +03:00
if (ntcp2)
{
2018-08-04 04:16:35 +03:00
if (!m_NTCP2Keys) NewNTCP2Keys ();
UpdateNTCP2Address (true); // enable NTCP2
2018-06-11 22:33:48 +03:00
}
2018-08-03 21:49:28 +03:00
else
UpdateNTCP2Address (false); // disable NTCP2
2018-06-11 22:33:48 +03:00
2013-10-23 06:45:40 +04:00
return true;
}
void RouterContext::SaveKeys ()
2017-10-12 13:52:36 +03:00
{
2015-11-03 21:05:37 +03:00
// save in the same format as .dat files
std::ofstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ofstream::binary | std::ofstream::out);
2015-11-03 21:05:37 +03:00
size_t len = m_Keys.GetFullLen ();
uint8_t * buf = new uint8_t[len];
m_Keys.ToBuffer (buf, len);
fk.write ((char *)buf, len);
delete[] buf;
}
2014-10-12 05:27:55 +04:00
std::shared_ptr<i2p::tunnel::TunnelPool> RouterContext::GetTunnelPool () const
{
2017-10-12 13:52:36 +03:00
return i2p::tunnel::tunnels.GetExploratoryPool ();
}
2015-02-06 02:53:43 +03:00
void RouterContext::HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
2014-10-12 05:27:55 +04:00
{
2017-12-01 20:57:05 +03:00
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len), from));
2015-02-23 22:41:56 +03:00
}
2015-06-16 17:14:14 +03:00
void RouterContext::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
2015-06-10 05:14:31 +03:00
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg);
2017-10-12 13:52:36 +03:00
}
2015-06-16 17:14:14 +03:00
void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
2015-06-10 05:14:31 +03:00
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg);
2017-10-12 13:52:36 +03:00
}
2016-07-28 20:24:25 +03:00
void RouterContext::CleanupDestination ()
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
i2p::garlic::GarlicDestination::CleanupExpiredTags ();
}
2017-10-12 13:52:36 +03:00
2015-02-23 22:41:56 +03:00
uint32_t RouterContext::GetUptime () const
{
return i2p::util::GetSecondsSinceEpoch () - m_StartupTime;
2017-10-12 13:52:36 +03:00
}
bool RouterContext::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const
{
return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data, ctx, true) : false;
}
bool RouterContext::DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const
{
return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data, ctx, false) : false;
}
2018-09-09 05:08:08 +03:00
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{
if (!m_StaticKeys)
{
if (!m_NTCP2Keys) NewNTCP2Keys ();
2018-09-09 15:38:12 +03:00
auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
if (!m_StaticKeys)
m_StaticKeys.reset (x);
else
delete x;
2018-09-09 05:08:08 +03:00
}
return *m_StaticKeys;
}
}