2013-10-23 02:45:40 +00:00
|
|
|
#include <fstream>
|
2016-01-20 00:00:00 +00:00
|
|
|
#include "Config.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Crypto.h"
|
2014-08-31 20:46:39 +00:00
|
|
|
#include "Timestamp.h"
|
2014-10-12 01:27:55 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2017-04-22 00:04:16 +00:00
|
|
|
#include "NetDb.hpp"
|
2016-02-11 00:00:00 +00:00
|
|
|
#include "FS.h"
|
2014-01-30 00:56:48 +00:00
|
|
|
#include "util.h"
|
2014-08-17 05:09:15 +00:00
|
|
|
#include "version.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Log.h"
|
2016-02-21 01:20:19 +00:00
|
|
|
#include "Family.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "RouterContext.h"
|
2013-10-23 02:45:40 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
RouterContext context;
|
|
|
|
|
2014-08-31 20:46:39 +00:00
|
|
|
RouterContext::RouterContext ():
|
2015-03-01 12:55:03 +00:00
|
|
|
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
|
2016-10-12 15:26:48 +00:00
|
|
|
m_StartupTime (0), m_Status (eRouterStatusOK), m_Error (eRouterErrorNone),
|
|
|
|
m_NetID (I2PD_NET_ID)
|
2014-09-04 13:31:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::Init ()
|
2013-10-23 02:45:40 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
|
2015-02-23 19:41:56 +00:00
|
|
|
m_StartupTime = i2p::util::GetSecondsSinceEpoch ();
|
2013-10-23 02:45:40 +00:00
|
|
|
if (!Load ())
|
|
|
|
CreateNewRouter ();
|
2014-08-31 20:46:39 +00:00
|
|
|
UpdateRouterInfo ();
|
2014-08-17 05:09:15 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 02:45:40 +00:00
|
|
|
void RouterContext::CreateNewRouter ()
|
|
|
|
{
|
2015-11-04 18:48:30 +00:00
|
|
|
#if defined(__x86_64__) || defined(__i386__) || defined(_MSC_VER)
|
2015-11-03 18:05:37 +00:00
|
|
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
|
2015-11-04 18:48:30 +00:00
|
|
|
#else
|
|
|
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_DSA_SHA1);
|
|
|
|
#endif
|
2014-08-31 20:46:39 +00:00
|
|
|
SaveKeys ();
|
|
|
|
NewRouterInfo ();
|
2014-02-23 16:48:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 20:46:39 +00:00
|
|
|
void RouterContext::NewRouterInfo ()
|
2014-02-23 16:48:09 +00:00
|
|
|
{
|
2014-02-24 01:48:28 +00:00
|
|
|
i2p::data::RouterInfo routerInfo;
|
2014-11-20 17:21:27 +00:00
|
|
|
routerInfo.SetRouterIdentity (GetIdentity ());
|
2016-01-20 00:00:00 +00:00
|
|
|
uint16_t port; i2p::config::GetOption("port", port);
|
2014-08-31 20:46:39 +00:00
|
|
|
if (!port)
|
2015-11-03 14:15:49 +00:00
|
|
|
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
|
2016-07-15 17:52:55 +00:00
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
2016-07-19 00:00:00 +00:00
|
|
|
bool nat; i2p::config::GetOption("nat", nat);
|
|
|
|
std::string ifname; i2p::config::GetOption("ifname", ifname);
|
2016-11-24 18:56:37 +00:00
|
|
|
std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
|
|
|
|
std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
|
2016-07-22 14:16:57 +00:00
|
|
|
if (ipv4)
|
|
|
|
{
|
|
|
|
std::string host = "127.0.0.1";
|
2016-07-19 00:00:00 +00:00
|
|
|
if (!i2p::config::IsDefault("host"))
|
|
|
|
i2p::config::GetOption("host", host);
|
2016-07-22 14:16:57 +00: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 18:56:37 +00:00
|
|
|
|
|
|
|
if(ifname4.size())
|
|
|
|
host = i2p::util::net::GetInterfaceAddress(ifname4, false).to_string();
|
|
|
|
|
2016-07-22 14:16:57 +00:00
|
|
|
routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
|
|
|
|
routerInfo.AddNTCPAddress (host.c_str(), port);
|
|
|
|
}
|
|
|
|
if (ipv6)
|
|
|
|
{
|
|
|
|
std::string host = "::";
|
2016-07-22 14:34:56 +00:00
|
|
|
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
|
|
|
|
i2p::config::GetOption("host", host);
|
|
|
|
else if (!ifname.empty())
|
2016-07-22 14:16:57 +00:00
|
|
|
host = i2p::util::net::GetInterfaceAddress(ifname, true).to_string(); // v6
|
2016-11-24 15:11:46 +00:00
|
|
|
|
2016-11-24 18:56:37 +00:00
|
|
|
if(ifname6.size())
|
|
|
|
host = i2p::util::net::GetInterfaceAddress(ifname6, true).to_string();
|
|
|
|
|
|
|
|
routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
|
2016-11-24 15:11:46 +00:00
|
|
|
routerInfo.AddNTCPAddress (host.c_str(), port);
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:26:36 +00:00
|
|
|
routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |
|
|
|
|
i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer); // LR, BC
|
2016-10-12 15:26:48 +00:00
|
|
|
routerInfo.SetProperty ("netId", std::to_string (m_NetID));
|
2014-08-17 05:09:15 +00:00
|
|
|
routerInfo.SetProperty ("router.version", I2P_VERSION);
|
2014-08-26 02:47:12 +00:00
|
|
|
routerInfo.CreateBuffer (m_Keys);
|
2015-11-03 14:15:49 +00:00
|
|
|
m_RouterInfo.SetRouterIdentity (GetIdentity ());
|
2014-07-22 12:03:02 +00:00
|
|
|
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
|
2014-08-17 05:09:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 20:46:39 +00:00
|
|
|
void RouterContext::UpdateRouterInfo ()
|
|
|
|
{
|
|
|
|
m_RouterInfo.CreateBuffer (m_Keys);
|
2016-02-11 00:00:00 +00:00
|
|
|
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
|
2014-08-31 20:46:39 +00:00
|
|
|
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
}
|
2014-09-11 13:32:34 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
void RouterContext::SetStatus (RouterStatus status)
|
|
|
|
{
|
|
|
|
if (status != m_Status)
|
|
|
|
{
|
|
|
|
m_Status = status;
|
2016-09-20 01:37:04 +00:00
|
|
|
m_Error = eRouterErrorNone;
|
2015-11-03 14:15:49 +00:00
|
|
|
switch (m_Status)
|
|
|
|
{
|
|
|
|
case eRouterStatusOK:
|
|
|
|
SetReachable ();
|
|
|
|
break;
|
|
|
|
case eRouterStatusFirewalled:
|
|
|
|
SetUnreachable ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 13:32:34 +00:00
|
|
|
void RouterContext::UpdatePort (int port)
|
2013-12-10 13:10:49 +00:00
|
|
|
{
|
2014-09-11 13:32:34 +00:00
|
|
|
bool updated = false;
|
2016-08-07 21:52:18 +00:00
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2013-12-10 13:10:49 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
if (address->port != port)
|
2014-09-11 13:32:34 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
address->port = port;
|
2014-09-11 13:32:34 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
2014-08-17 05:09:15 +00:00
|
|
|
}
|
2014-02-09 02:06:40 +00:00
|
|
|
|
2014-10-29 17:49:21 +00:00
|
|
|
void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
|
2014-02-09 02:06:40 +00:00
|
|
|
{
|
2014-08-31 20:46:39 +00:00
|
|
|
bool updated = false;
|
2016-08-07 21:52:18 +00:00
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2014-08-31 20:46:39 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
if (address->host != host && address->IsCompatible (host))
|
2014-08-31 20:46:39 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
address->host = host;
|
2014-08-31 20:46:39 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
|
|
|
|
UpdateRouterInfo ();
|
2013-10-23 02:45:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
bool RouterContext::AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer)
|
2014-09-01 21:34:20 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
bool ret = m_RouterInfo.AddIntroducer (introducer);
|
|
|
|
if (ret)
|
|
|
|
UpdateRouterInfo ();
|
2014-09-15 01:53:00 +00:00
|
|
|
return ret;
|
2014-09-01 21:34:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-07 00:43:20 +00:00
|
|
|
void RouterContext::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
|
2014-09-01 21:34:20 +00:00
|
|
|
{
|
2014-09-07 00:43:20 +00:00
|
|
|
if (m_RouterInfo.RemoveIntroducer (e))
|
2014-09-01 21:34:20 +00:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2014-09-08 20:43:20 +00:00
|
|
|
|
2015-01-28 20:12:15 +00:00
|
|
|
void RouterContext::SetFloodfill (bool floodfill)
|
|
|
|
{
|
|
|
|
m_IsFloodfill = floodfill;
|
|
|
|
if (floodfill)
|
|
|
|
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eFloodfill);
|
|
|
|
else
|
2015-03-18 19:06:15 +00:00
|
|
|
{
|
2015-01-28 20:12:15 +00:00
|
|
|
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eFloodfill);
|
2015-03-18 19:06:15 +00:00
|
|
|
// we don't publish number of routers and leaseset for non-floodfill
|
2016-02-21 01:20:19 +00:00
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS);
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS);
|
2015-03-18 19:06:15 +00:00
|
|
|
}
|
2015-01-28 20:12:15 +00:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:16:11 +00:00
|
|
|
std::string RouterContext::GetFamily () const
|
2016-04-26 22:48:23 +00:00
|
|
|
{
|
2016-04-28 22:16:11 +00:00
|
|
|
return m_RouterInfo.GetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
|
2016-04-26 22:31:33 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 01:20:19 +00: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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 15:14:21 +00:00
|
|
|
|
2016-03-31 00:00:00 +00:00
|
|
|
void RouterContext::SetBandwidth (char L) {
|
|
|
|
uint16_t limit = 0;
|
2017-01-25 21:14:01 +00:00
|
|
|
enum { low, high, extra, unlim } type = high;
|
2016-03-31 00:00:00 +00:00
|
|
|
/* detect parameters */
|
2016-03-31 01:31:17 +00:00
|
|
|
switch (L)
|
|
|
|
{
|
2016-03-31 00:00:00 +00: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-01-25 21:14:01 +00:00
|
|
|
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH2 : limit = 9999; type = unlim; break;
|
2016-03-31 01:31:17 +00:00
|
|
|
default:
|
|
|
|
limit = 48; type = low;
|
2016-03-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
/* update caps & flags in RI */
|
|
|
|
auto caps = m_RouterInfo.GetCaps ();
|
|
|
|
caps &= ~i2p::data::RouterInfo::eHighBandwidth;
|
|
|
|
caps &= ~i2p::data::RouterInfo::eExtraBandwidth;
|
2016-03-31 01:31:17 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2016-03-31 00:00:00 +00:00
|
|
|
case low : /* not set */; break;
|
2017-01-25 21:14:01 +00:00
|
|
|
case extra : caps |= i2p::data::RouterInfo::eExtraBandwidth; break; // 'P'
|
|
|
|
case unlim : caps |= i2p::data::RouterInfo::eExtraBandwidth; // no break here, extra + high means 'X'
|
2016-03-31 00:00:00 +00:00
|
|
|
case high : caps |= i2p::data::RouterInfo::eHighBandwidth; break;
|
|
|
|
}
|
|
|
|
m_RouterInfo.SetCaps (caps);
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
m_BandwidthLimit = limit;
|
2015-03-19 15:14:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:31:17 +00:00
|
|
|
void RouterContext::SetBandwidth (int limit)
|
|
|
|
{
|
2016-03-31 00:00:00 +00: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 03:17:04 +00:00
|
|
|
}
|
2016-03-31 00:00:00 +00:00
|
|
|
|
2015-03-01 12:55:03 +00:00
|
|
|
bool RouterContext::IsUnreachable () const
|
|
|
|
{
|
|
|
|
return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
|
|
|
|
}
|
|
|
|
|
2014-09-08 20:43:20 +00:00
|
|
|
void RouterContext::SetUnreachable ()
|
|
|
|
{
|
|
|
|
// set caps
|
2017-01-25 21:37:21 +00: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
|
|
|
|
m_RouterInfo.SetCaps (caps);
|
2014-09-08 20:43:20 +00:00
|
|
|
// remove NTCP address
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2016-08-07 21:52:18 +00:00
|
|
|
for (auto it = addresses.begin (); it != addresses.end (); ++it)
|
2014-09-08 20:43:20 +00:00
|
|
|
{
|
2016-07-14 18:29:45 +00:00
|
|
|
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP &&
|
|
|
|
(*it)->host.is_v4 ())
|
2014-09-08 20:43:20 +00:00
|
|
|
{
|
2016-07-14 18:10:38 +00:00
|
|
|
addresses.erase (it);
|
2014-09-08 20:43:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 01:53:55 +00:00
|
|
|
// delete previous introducers
|
2017-01-02 21:36:59 +00:00
|
|
|
for (auto& addr : addresses)
|
|
|
|
if (addr->ssu)
|
|
|
|
addr->ssu->introducers.clear ();
|
2015-03-01 12:55:03 +00:00
|
|
|
|
2014-09-08 20:43:20 +00:00
|
|
|
// update
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2014-10-27 01:32:06 +00:00
|
|
|
|
2015-03-01 12:55:03 +00:00
|
|
|
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
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2016-08-07 21:52:18 +00:00
|
|
|
for (const auto& addr : addresses)
|
2015-03-01 12:55:03 +00:00
|
|
|
{
|
2016-07-14 18:29:45 +00:00
|
|
|
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
|
|
|
|
addr->host.is_v4 ())
|
2015-03-01 12:55:03 +00:00
|
|
|
{
|
2015-11-03 14:15:49 +00:00
|
|
|
// insert NTCP address with host/port from SSU
|
2016-07-14 18:10:38 +00:00
|
|
|
m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port);
|
2015-03-01 12:55:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// delete previous introducers
|
2016-08-07 21:52:18 +00:00
|
|
|
for (auto& addr : addresses)
|
2017-01-02 21:36:59 +00:00
|
|
|
if (addr->ssu)
|
|
|
|
addr->ssu->introducers.clear ();
|
2015-03-01 12:55:03 +00:00
|
|
|
|
|
|
|
// update
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2014-10-27 01:32:06 +00:00
|
|
|
void RouterContext::SetSupportsV6 (bool supportsV6)
|
|
|
|
{
|
|
|
|
if (supportsV6)
|
|
|
|
m_RouterInfo.EnableV6 ();
|
|
|
|
else
|
|
|
|
m_RouterInfo.DisableV6 ();
|
2014-10-29 22:46:35 +00:00
|
|
|
UpdateRouterInfo ();
|
2016-03-24 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::SetSupportsV4 (bool supportsV4)
|
|
|
|
{
|
|
|
|
if (supportsV4)
|
|
|
|
m_RouterInfo.EnableV4 ();
|
|
|
|
else
|
|
|
|
m_RouterInfo.DisableV4 ();
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:08:50 +00:00
|
|
|
|
2014-10-29 17:49:21 +00:00
|
|
|
void RouterContext::UpdateNTCPV6Address (const boost::asio::ip::address& host)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
|
|
|
bool updated = false, found = false;
|
|
|
|
int port = 0;
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2016-08-07 21:52:18 +00:00
|
|
|
for (auto& addr: addresses)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
if (addr->host.is_v6 () && addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
if (addr->host != host)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
2016-03-21 17:02:51 +00:00
|
|
|
addr->host = host;
|
2014-10-27 19:08:50 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else
|
2016-03-21 17:02:51 +00:00
|
|
|
port = addr->port;
|
2014-10-27 19:08:50 +00:00
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
// create new address
|
2014-10-29 17:49:21 +00:00
|
|
|
m_RouterInfo.AddNTCPAddress (host.to_string ().c_str (), port);
|
2014-10-31 22:27:51 +00:00
|
|
|
auto mtu = i2p::util::net::GetMTU (host);
|
|
|
|
if (mtu)
|
|
|
|
{
|
2015-12-18 13:49:57 +00: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");
|
|
|
|
}
|
2014-10-31 22:27:51 +00:00
|
|
|
}
|
|
|
|
m_RouterInfo.AddSSUAddress (host.to_string ().c_str (), port, GetIdentHash (), mtu ? mtu : 1472); // TODO
|
2014-10-27 19:08:50 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2015-03-18 19:36:07 +00:00
|
|
|
|
|
|
|
void RouterContext::UpdateStats ()
|
|
|
|
{
|
|
|
|
if (m_IsFloodfill)
|
|
|
|
{
|
|
|
|
// update routers and leasesets
|
2016-11-04 00:00:00 +00:00
|
|
|
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 ()));
|
2015-03-18 19:36:07 +00:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 01:32:06 +00:00
|
|
|
|
2013-10-23 02:45:40 +00:00
|
|
|
bool RouterContext::Load ()
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
std::ifstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ifstream::in | std::ifstream::binary);
|
2013-10-23 02:45:40 +00:00
|
|
|
if (!fk.is_open ()) return false;
|
2015-11-03 18:05:37 +00:00
|
|
|
fk.seekg (0, std::ios::end);
|
|
|
|
size_t len = fk.tellg();
|
|
|
|
fk.seekg (0, std::ios::beg);
|
|
|
|
|
|
|
|
if (len == sizeof (i2p::data::Keys)) // old keys file format
|
|
|
|
{
|
|
|
|
i2p::data::Keys keys;
|
|
|
|
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;
|
|
|
|
}
|
2013-10-23 02:45:40 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
m_RouterInfo.SetRouterIdentity (GetIdentity ());
|
2016-04-05 14:22:32 +00: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 ();
|
|
|
|
}
|
2015-03-01 12:55:03 +00:00
|
|
|
|
|
|
|
if (IsUnreachable ())
|
|
|
|
SetReachable (); // we assume reachable until we discover firewall through peer tests
|
2014-08-23 03:02:48 +00:00
|
|
|
|
2013-10-23 02:45:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-17 05:09:15 +00:00
|
|
|
|
2014-08-31 20:46:39 +00:00
|
|
|
void RouterContext::SaveKeys ()
|
|
|
|
{
|
2015-11-03 18:05:37 +00:00
|
|
|
// save in the same format as .dat files
|
2016-02-11 00:00:00 +00:00
|
|
|
std::ofstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ofstream::binary | std::ofstream::out);
|
2015-11-03 18:05:37 +00: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-08-17 05:09:15 +00:00
|
|
|
}
|
2014-10-12 01:27:55 +00:00
|
|
|
|
2015-04-05 16:54:15 +00:00
|
|
|
std::shared_ptr<i2p::tunnel::TunnelPool> RouterContext::GetTunnelPool () const
|
|
|
|
{
|
|
|
|
return i2p::tunnel::tunnels.GetExploratoryPool ();
|
|
|
|
}
|
|
|
|
|
2015-02-05 23:53:43 +00:00
|
|
|
void RouterContext::HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
2014-10-12 01:27:55 +00:00
|
|
|
{
|
2015-06-30 01:40:43 +00:00
|
|
|
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
|
2015-02-23 19:41:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 14:14:14 +00:00
|
|
|
void RouterContext::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2015-06-10 02:14:31 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
|
|
|
i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg);
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:14:14 +00:00
|
|
|
void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
2015-06-10 02:14:31 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
|
|
|
i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg);
|
|
|
|
}
|
2016-07-28 17:24:25 +00:00
|
|
|
|
|
|
|
void RouterContext::CleanupDestination ()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
|
|
|
i2p::garlic::GarlicDestination::CleanupExpiredTags ();
|
|
|
|
}
|
2015-06-10 02:14:31 +00:00
|
|
|
|
2015-02-23 19:41:56 +00:00
|
|
|
uint32_t RouterContext::GetUptime () const
|
|
|
|
{
|
|
|
|
return i2p::util::GetSecondsSinceEpoch () - m_StartupTime;
|
2014-10-12 01:27:55 +00:00
|
|
|
}
|
2014-08-17 05:09:15 +00:00
|
|
|
}
|