2013-10-23 02:45:40 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
#include <cryptopp/dsa.h>
|
2015-03-18 19:06:15 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2013-10-23 02:45:40 +00:00
|
|
|
#include "CryptoConst.h"
|
|
|
|
#include "RouterContext.h"
|
2014-08-31 20:46:39 +00:00
|
|
|
#include "Timestamp.h"
|
2014-10-12 01:27:55 +00:00
|
|
|
#include "I2NPProtocol.h"
|
2015-03-18 19:06:15 +00:00
|
|
|
#include "NetDb.h"
|
2014-01-30 00:56:48 +00:00
|
|
|
#include "util.h"
|
2014-08-17 05:09:15 +00:00
|
|
|
#include "version.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),
|
|
|
|
m_StartupTime (0), m_Status (eRouterStatusOK )
|
2014-09-04 13:31:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::Init ()
|
2013-10-23 02:45:40 +00:00
|
|
|
{
|
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 ()
|
|
|
|
{
|
2013-12-21 01:22:55 +00:00
|
|
|
m_Keys = i2p::data::CreateRandomKeys ();
|
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 ());
|
2014-08-31 20:46:39 +00:00
|
|
|
int port = i2p::util::config::GetArg("-port", 0);
|
|
|
|
if (!port)
|
|
|
|
port = m_Rnd.GenerateWord32 (9111, 30777); // I2P network ports range
|
|
|
|
routerInfo.AddSSUAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), port, routerInfo.GetIdentHash ());
|
|
|
|
routerInfo.AddNTCPAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), 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
|
2014-08-17 05:09:15 +00:00
|
|
|
routerInfo.SetProperty ("coreVersion", I2P_VERSION);
|
2014-02-24 01:48:28 +00:00
|
|
|
routerInfo.SetProperty ("netId", "2");
|
2014-08-17 05:09:15 +00:00
|
|
|
routerInfo.SetProperty ("router.version", I2P_VERSION);
|
2014-09-03 17:00:15 +00:00
|
|
|
routerInfo.SetProperty ("stat_uptime", "90m");
|
2014-08-26 02:47:12 +00:00
|
|
|
routerInfo.CreateBuffer (m_Keys);
|
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);
|
|
|
|
m_RouterInfo.SaveToFile (i2p::util::filesystem::GetFullPath (ROUTER_INFO));
|
|
|
|
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
}
|
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;
|
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2013-12-10 13:10:49 +00:00
|
|
|
{
|
2014-09-11 13:32:34 +00:00
|
|
|
if (address.port != port)
|
|
|
|
{
|
|
|
|
address.port = port;
|
|
|
|
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;
|
2014-02-09 02:06:40 +00:00
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2014-08-31 20:46:39 +00:00
|
|
|
{
|
2014-10-29 17:49:21 +00:00
|
|
|
if (address.host != host && address.IsCompatible (host))
|
2014-08-31 20:46:39 +00:00
|
|
|
{
|
2014-10-29 17:49:21 +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
|
|
|
}
|
|
|
|
|
2014-09-09 12:03:05 +00:00
|
|
|
bool RouterContext::AddIntroducer (const i2p::data::RouterInfo& routerInfo, uint32_t tag)
|
2014-09-01 21:34:20 +00:00
|
|
|
{
|
2014-09-09 12:03:05 +00:00
|
|
|
bool ret = false;
|
2014-09-01 21:34:20 +00:00
|
|
|
auto address = routerInfo.GetSSUAddress ();
|
|
|
|
if (address)
|
|
|
|
{
|
2014-09-09 12:03:05 +00:00
|
|
|
ret = m_RouterInfo.AddIntroducer (address, tag);
|
|
|
|
if (ret)
|
2014-09-01 21:34:20 +00:00
|
|
|
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
|
|
|
|
m_RouterInfo.DeleteProperty (ROUTER_INFO_PROPERTY_LEASESETS);
|
|
|
|
m_RouterInfo.DeleteProperty (ROUTER_INFO_PROPERTY_ROUTERS);
|
|
|
|
}
|
2015-01-28 20:12:15 +00:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:14:21 +00:00
|
|
|
void RouterContext::SetHighBandwidth ()
|
|
|
|
{
|
|
|
|
if (!m_RouterInfo.IsHighBandwidth ())
|
|
|
|
{
|
|
|
|
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eHighBandwidth);
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::SetLowBandwidth ()
|
|
|
|
{
|
|
|
|
if (m_RouterInfo.IsHighBandwidth ())
|
|
|
|
{
|
|
|
|
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eHighBandwidth);
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 01:53:55 +00:00
|
|
|
// delete previous introducers
|
|
|
|
for (auto& addr : addresses)
|
|
|
|
addr.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 ();
|
|
|
|
for (size_t i = 0; i < addresses.size (); i++)
|
|
|
|
{
|
|
|
|
if (addresses[i].transportStyle == i2p::data::RouterInfo::eTransportSSU)
|
|
|
|
{
|
|
|
|
// insert NTCP address with host/port form 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 ();
|
|
|
|
}
|
|
|
|
|
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 ();
|
2014-10-27 01:32:06 +00:00
|
|
|
}
|
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 ();
|
|
|
|
for (auto& addr : addresses)
|
|
|
|
{
|
2014-10-29 22:46:35 +00:00
|
|
|
if (addr.host.is_v6 () && addr.transportStyle == i2p::data::RouterInfo::eTransportNTCP)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
2014-10-29 17:49:21 +00:00
|
|
|
if (addr.host != host)
|
2014-10-27 19:08:50 +00:00
|
|
|
{
|
2014-10-29 17:49:21 +00:00
|
|
|
addr.host = host;
|
2014-10-27 19:08:50 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
port = addr.port;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LogPrint ("Our v6 MTU=", mtu);
|
|
|
|
if (mtu > 1472) mtu = 1472;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
m_RouterInfo.SetProperty (ROUTER_INFO_PROPERTY_LEASESETS, boost::lexical_cast<std::string>(i2p::data::netdb.GetNumLeaseSets ()));
|
|
|
|
m_RouterInfo.SetProperty (ROUTER_INFO_PROPERTY_ROUTERS, boost::lexical_cast<std::string>(i2p::data::netdb.GetNumRouters ()));
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 01:32:06 +00:00
|
|
|
|
2013-10-23 02:45:40 +00:00
|
|
|
bool RouterContext::Load ()
|
|
|
|
{
|
2014-03-14 11:32:11 +00:00
|
|
|
std::ifstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ifstream::binary | std::ofstream::in);
|
2013-10-23 02:45:40 +00:00
|
|
|
if (!fk.is_open ()) return false;
|
2014-08-25 20:25:12 +00:00
|
|
|
|
|
|
|
i2p::data::Keys keys;
|
|
|
|
fk.read ((char *)&keys, sizeof (keys));
|
|
|
|
m_Keys = keys;
|
2013-10-23 02:45:40 +00:00
|
|
|
|
2014-07-24 01:45:45 +00:00
|
|
|
i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
|
|
|
|
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
|
2014-09-02 16:28:57 +00:00
|
|
|
m_RouterInfo.SetProperty ("coreVersion", I2P_VERSION);
|
2014-08-31 20:46:39 +00:00
|
|
|
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
|
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 ()
|
|
|
|
{
|
|
|
|
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
|
|
|
|
i2p::data::Keys keys;
|
|
|
|
memcpy (keys.privateKey, m_Keys.GetPrivateKey (), sizeof (keys.privateKey));
|
|
|
|
memcpy (keys.signingPrivateKey, m_Keys.GetSigningPrivateKey (), sizeof (keys.signingPrivateKey));
|
|
|
|
auto& ident = GetIdentity ().GetStandardIdentity ();
|
|
|
|
memcpy (keys.publicKey, ident.publicKey, sizeof (keys.publicKey));
|
|
|
|
memcpy (keys.signingKey, ident.signingKey, sizeof (keys.signingKey));
|
|
|
|
fk.write ((char *)&keys, sizeof (keys));
|
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
|
|
|
{
|
2014-10-13 21:45:07 +00:00
|
|
|
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
|
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
|
|
|
}
|