i2pd/RouterContext.cpp

388 lines
11 KiB
C++
Raw Normal View History

2013-10-23 06:45:40 +04:00
#include <fstream>
#include <boost/lexical_cast.hpp>
2016-01-20 03:00:00 +03:00
#include "Config.h"
2015-11-03 17:15:49 +03:00
#include "Crypto.h"
#include "Timestamp.h"
2014-10-12 05:27:55 +04:00
#include "I2NPProtocol.h"
#include "NetDb.h"
#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 ():
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
m_StartupTime (0), m_Status (eRouterStatusOK )
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 ();
UpdateRouterInfo ();
}
2013-10-23 06:45:40 +04:00
void RouterContext::CreateNewRouter ()
{
2015-11-04 21:48:30 +03:00
#if defined(__x86_64__) || defined(__i386__) || defined(_MSC_VER)
2015-11-03 21:05:37 +03:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
2015-11-04 21:48:30 +03:00
#else
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_DSA_SHA1);
#endif
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)
2015-11-03 17:15:49 +03:00
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
2016-01-20 03:00:00 +03:00
std::string host; i2p::config::GetOption("host", host);
if (i2p::config::IsDefault("host"))
2016-02-03 17:21:22 +03:00
host = "127.0.0.1"; // replace default address with safe value
2016-01-20 03:00:00 +03:00
routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
routerInfo.AddNTCPAddress (host.c_str(), port);
2014-09-05 15:26:36 +04:00
routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |
i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer); // LR, BC
2016-01-16 23:36:30 +03:00
routerInfo.SetProperty ("netId", std::to_string (I2PD_NET_ID));
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 ());
}
void RouterContext::UpdateRouterInfo ()
{
m_RouterInfo.CreateBuffer (m_Keys);
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
}
2014-09-11 17:32:34 +04:00
2015-11-03 17:15:49 +03:00
void RouterContext::SetStatus (RouterStatus status)
{
if (status != m_Status)
{
m_Status = status;
switch (m_Status)
{
case eRouterStatusOK:
SetReachable ();
break;
case eRouterStatusFirewalled:
SetUnreachable ();
break;
default:
;
}
}
}
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;
for (auto& address : m_RouterInfo.GetAddresses ())
2013-12-10 17:10:49 +04:00
{
2014-09-11 17:32:34 +04:00
if (address.port != port)
{
address.port = port;
updated = true;
}
}
if (updated)
UpdateRouterInfo ();
}
2014-02-09 06:06:40 +04:00
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;
2014-02-09 06:06:40 +04:00
for (auto& address : m_RouterInfo.GetAddresses ())
{
2014-10-29 20:49:21 +03:00
if (address.host != host && address.IsCompatible (host))
{
2014-10-29 20:49:21 +03:00
address.host = host;
updated = true;
}
}
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)
UpdateRouterInfo ();
return ret;
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 ();
}
2014-09-09 00:43:20 +04: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 ();
}
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);
}
else
{
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
}
}
2015-03-19 18:14:21 +03:00
void RouterContext::SetHighBandwidth ()
{
2016-01-03 17:54:03 +03:00
if (!m_RouterInfo.IsHighBandwidth () || m_RouterInfo.IsExtraBandwidth ())
2015-03-19 18:14:21 +03:00
{
2016-01-03 06:17:04 +03:00
m_RouterInfo.SetCaps ((m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eHighBandwidth) & ~i2p::data::RouterInfo::eExtraBandwidth);
2015-03-19 18:14:21 +03:00
UpdateRouterInfo ();
}
}
void RouterContext::SetLowBandwidth ()
{
2016-01-03 17:54:03 +03:00
if (m_RouterInfo.IsHighBandwidth () || m_RouterInfo.IsExtraBandwidth ())
2015-03-19 18:14:21 +03:00
{
2016-01-03 06:17:04 +03:00
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eHighBandwidth & ~i2p::data::RouterInfo::eExtraBandwidth);
2015-03-19 18:14:21 +03:00
UpdateRouterInfo ();
}
}
2016-01-03 06:17:04 +03:00
void RouterContext::SetExtraBandwidth ()
{
2016-01-03 17:54:03 +03:00
if (!m_RouterInfo.IsExtraBandwidth () || !m_RouterInfo.IsHighBandwidth ())
2016-01-03 06:17:04 +03:00
{
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eExtraBandwidth | i2p::data::RouterInfo::eHighBandwidth);
UpdateRouterInfo ();
}
}
bool RouterContext::IsUnreachable () const
{
return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
}
2014-09-09 00:43:20 +04:00
void RouterContext::SetUnreachable ()
{
// set caps
m_RouterInfo.SetCaps (i2p::data::RouterInfo::eUnreachable | i2p::data::RouterInfo::eSSUTesting); // LU, B
// remove NTCP address
auto& addresses = m_RouterInfo.GetAddresses ();
for (size_t i = 0; i < addresses.size (); i++)
{
if (addresses[i].transportStyle == i2p::data::RouterInfo::eTransportNTCP)
{
addresses.erase (addresses.begin () + i);
break;
}
}
2014-09-09 05:53:55 +04:00
// delete previous introducers
for (auto& addr : addresses)
addr.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
auto& addresses = m_RouterInfo.GetAddresses ();
for (size_t i = 0; i < addresses.size (); i++)
{
if (addresses[i].transportStyle == i2p::data::RouterInfo::eTransportSSU)
{
2015-11-03 17:15:49 +03:00
// insert NTCP address with host/port from SSU
m_RouterInfo.AddNTCPAddress (addresses[i].host.to_string ().c_str (), addresses[i].port);
break;
}
}
// delete previous introducers
for (auto& addr : addresses)
addr.introducers.clear ();
// update
UpdateRouterInfo ();
}
void RouterContext::SetSupportsV6 (bool supportsV6)
{
if (supportsV6)
m_RouterInfo.EnableV6 ();
else
m_RouterInfo.DisableV6 ();
2014-10-30 01:46:35 +03:00
UpdateRouterInfo ();
}
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
{
bool updated = false, found = false;
int port = 0;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr : addresses)
{
2014-10-30 01:46:35 +03:00
if (addr.host.is_v6 () && addr.transportStyle == i2p::data::RouterInfo::eTransportNTCP)
2014-10-27 22:08:50 +03:00
{
2014-10-29 20:49:21 +03:00
if (addr.host != host)
2014-10-27 22:08:50 +03:00
{
2014-10-29 20:49:21 +03:00
addr.host = host;
2014-10-27 22:08:50 +03:00
updated = true;
}
found = true;
}
else
port = addr.port;
}
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)
{
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");
}
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::UpdateStats ()
{
if (m_IsFloodfill)
{
// update routers and leasesets
2016-02-21 04:20:19 +03:00
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS, boost::lexical_cast<std::string>(i2p::data::netdb.GetNumLeaseSets ()));
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS, boost::lexical_cast<std::string>(i2p::data::netdb.GetNumRouters ()));
2015-03-18 22:36:07 +03:00
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();
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 06:45:40 +04:00
i2p::data::RouterInfo routerInfo(i2p::fs::DataDirPath (ROUTER_INFO)); // TODO
2015-11-03 17:15:49 +03:00
m_RouterInfo.SetRouterIdentity (GetIdentity ());
2014-07-24 05:45:45 +04:00
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
2014-09-02 20:28:57 +04:00
m_RouterInfo.SetProperty ("coreVersion", I2P_VERSION);
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
2016-01-30 06:35:51 +03:00
// Migration to 0.9.24. TODO: remove later
m_RouterInfo.DeleteProperty ("coreVersion");
m_RouterInfo.DeleteProperty ("stat_uptime");
if (IsUnreachable ())
SetReachable (); // we assume reachable until we discover firewall through peer tests
2013-10-23 06:45:40 +04:00
return true;
}
void RouterContext::SaveKeys ()
{
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
{
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
{
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), 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);
}
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);
}
2015-02-23 22:41:56 +03:00
uint32_t RouterContext::GetUptime () const
{
return i2p::util::GetSecondsSinceEpoch () - m_StartupTime;
2014-10-12 05:27:55 +04:00
}
}