fix ipv6 fallback address

This commit is contained in:
R4SAS
2019-05-08 00:45:52 +03:00
parent ece140f18c
commit 73921b1024
2 changed files with 38 additions and 36 deletions

View File

@@ -37,7 +37,7 @@ namespace i2p
void RouterContext::CreateNewRouter () void RouterContext::CreateNewRouter ()
{ {
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519); m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
SaveKeys (); SaveKeys ();
NewRouterInfo (); NewRouterInfo ();
} }
@@ -50,14 +50,14 @@ namespace i2p
{ {
port = rand () % (30777 - 9111) + 9111; // I2P network ports range port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser if (port == 9150) port = 9151; // Tor browser
} }
bool ipv4; i2p::config::GetOption("ipv4", ipv4); bool ipv4; i2p::config::GetOption("ipv4", ipv4);
bool ipv6; i2p::config::GetOption("ipv6", ipv6); bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool ssu; i2p::config::GetOption("ssu", ssu); bool ssu; i2p::config::GetOption("ssu", ssu);
bool ntcp; i2p::config::GetOption("ntcp", ntcp); bool ntcp; i2p::config::GetOption("ntcp", ntcp);
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool nat; i2p::config::GetOption("nat", nat); bool nat; i2p::config::GetOption("nat", nat);
std::string ifname; i2p::config::GetOption("ifname", ifname); std::string ifname; i2p::config::GetOption("ifname", ifname);
std::string ifname4; i2p::config::GetOption("ifname4", ifname4); std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
std::string ifname6; i2p::config::GetOption("ifname6", ifname6); std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
if (ipv4) if (ipv4)
@@ -79,7 +79,7 @@ namespace i2p
} }
if (ipv6) if (ipv6)
{ {
std::string host = "::"; std::string host = "::1";
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
i2p::config::GetOption("host", host); i2p::config::GetOption("host", host);
else if (!ifname.empty()) else if (!ifname.empty())
@@ -103,9 +103,9 @@ namespace i2p
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ()); 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 (ntcp2) // we don't store iv in the address if non published so we must update it from keys
{ {
if (!m_NTCP2Keys) NewNTCP2Keys (); if (!m_NTCP2Keys) NewNTCP2Keys ();
UpdateNTCP2Address (true); UpdateNTCP2Address (true);
if (!ntcp) // NTCP2 should replace NTCP if (!ntcp) // NTCP2 should replace NTCP
{ {
bool published; i2p::config::GetOption("ntcp2.published", published); bool published; i2p::config::GetOption("ntcp2.published", published);
@@ -113,7 +113,6 @@ namespace i2p
PublishNTCP2Address (port, true); PublishNTCP2Address (port, true);
} }
} }
} }
void RouterContext::UpdateRouterInfo () void RouterContext::UpdateRouterInfo ()
@@ -126,14 +125,14 @@ namespace i2p
void RouterContext::NewNTCP2Keys () void RouterContext::NewNTCP2Keys ()
{ {
m_StaticKeys.reset (new i2p::crypto::X25519Keys ()); m_StaticKeys.reset (new i2p::crypto::X25519Keys ());
m_StaticKeys->GenerateKeys (); m_StaticKeys->GenerateKeys ();
m_NTCP2Keys.reset (new NTCP2PrivateKeys ()); m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey); m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32); memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32);
RAND_bytes (m_NTCP2Keys->iv, 16); RAND_bytes (m_NTCP2Keys->iv, 16);
// save // save
std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out); std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys)); fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
} }
void RouterContext::SetStatus (RouterStatus status) void RouterContext::SetStatus (RouterStatus status)
@@ -175,7 +174,7 @@ namespace i2p
{ {
if (!m_NTCP2Keys) return; if (!m_NTCP2Keys) return;
if (!port) if (!port)
{ {
port = rand () % (30777 - 9111) + 9111; // I2P network ports range port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser if (port == 9150) port = 9151; // Tor browser
} }
@@ -192,7 +191,7 @@ namespace i2p
} }
} }
if (updated) if (updated)
UpdateRouterInfo (); UpdateRouterInfo ();
} }
void RouterContext::UpdateNTCP2Address (bool enable) void RouterContext::UpdateNTCP2Address (bool enable)
@@ -205,7 +204,7 @@ namespace i2p
{ {
found = true; found = true;
if (!enable) if (!enable)
{ {
addresses.erase (it); addresses.erase (it);
updated= true; updated= true;
} }
@@ -213,14 +212,14 @@ namespace i2p
} }
} }
if (enable && !found) if (enable && !found)
{ {
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv); m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
updated = true; updated = true;
} }
if (updated) if (updated)
UpdateRouterInfo (); UpdateRouterInfo ();
} }
void RouterContext::UpdateAddress (const boost::asio::ip::address& host) void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
{ {
bool updated = false; bool updated = false;
@@ -371,7 +370,7 @@ namespace i2p
break; break;
} }
} }
} }
} }
else else
{ {
@@ -386,9 +385,9 @@ namespace i2p
else else
++it; ++it;
} }
} }
} }
void RouterContext::SetUnreachable () void RouterContext::SetUnreachable ()
{ {
// set caps // set caps
@@ -421,7 +420,7 @@ namespace i2p
m_RouterInfo.SetCaps (caps); m_RouterInfo.SetCaps (caps);
// insert NTCP back // insert NTCP back
bool ntcp; i2p::config::GetOption("ntcp", ntcp); bool ntcp; i2p::config::GetOption("ntcp", ntcp);
if (ntcp) if (ntcp)
PublishNTCPAddress (true); PublishNTCPAddress (true);
// delete previous introducers // delete previous introducers
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
@@ -516,11 +515,11 @@ namespace i2p
if (!found && port) // we have found NTCP2 v4 but not v6 if (!found && port) // we have found NTCP2 v4 but not v6
{ {
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, host, port); m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, host, port);
updated = true; updated = true;
} }
if (updated) if (updated)
UpdateRouterInfo (); UpdateRouterInfo ();
} }
void RouterContext::UpdateStats () void RouterContext::UpdateStats ()
@@ -563,7 +562,7 @@ namespace i2p
} }
// read NTCP2 keys if available // read NTCP2 keys if available
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary); std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
if (n2k) if (n2k)
{ {
n2k.seekg (0, std::ios::end); n2k.seekg (0, std::ios::end);
len = n2k.tellg(); len = n2k.tellg();
@@ -571,8 +570,8 @@ namespace i2p
if (len == sizeof (NTCP2PrivateKeys)) if (len == sizeof (NTCP2PrivateKeys))
{ {
m_NTCP2Keys.reset (new NTCP2PrivateKeys ()); m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys)); n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
} }
n2k.close (); n2k.close ();
} }
// read RouterInfo // read RouterInfo
@@ -667,7 +666,7 @@ namespace i2p
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys () i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{ {
if (!m_StaticKeys) if (!m_StaticKeys)
{ {
if (!m_NTCP2Keys) NewNTCP2Keys (); if (!m_NTCP2Keys) NewNTCP2Keys ();
auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey); auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
if (!m_StaticKeys) if (!m_StaticKeys)
@@ -675,6 +674,6 @@ namespace i2p
else else
delete x; delete x;
} }
return *m_StaticKeys; return *m_StaticKeys;
} }
} }

View File

@@ -295,7 +295,10 @@ namespace net
{ {
#ifdef WIN32 #ifdef WIN32
LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32"); LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32");
return boost::asio::ip::address::from_string("127.0.0.1"); if(ipv6)
return boost::asio::ip::address::from_string("::1");
else
return boost::asio::ip::address::from_string("127.0.0.1");
#else #else
int af = (ipv6 ? AF_INET6 : AF_INET); int af = (ipv6 ? AF_INET6 : AF_INET);
ifaddrs * addrs = nullptr; ifaddrs * addrs = nullptr;
@@ -327,7 +330,7 @@ namespace net
std::string fallback; std::string fallback;
if(ipv6) if(ipv6)
{ {
fallback = "::"; fallback = "::1";
LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname); LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname);
} else { } else {
fallback = "127.0.0.1"; fallback = "127.0.0.1";