Skip to content

Commit

Permalink
fix ipv6 fallback address
Browse files Browse the repository at this point in the history
  • Loading branch information
r4sas committed May 7, 2019
1 parent ece140f commit 73921b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
67 changes: 33 additions & 34 deletions libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace i2p
void RouterContext::CreateNewRouter ()
{
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
SaveKeys ();
SaveKeys ();
NewRouterInfo ();
}

Expand All @@ -50,14 +50,14 @@ namespace i2p
{
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
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);
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool nat; i2p::config::GetOption("nat", nat);
std::string ifname; i2p::config::GetOption("ifname", ifname);
}
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);
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool nat; i2p::config::GetOption("nat", nat);
std::string ifname; i2p::config::GetOption("ifname", ifname);
std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
if (ipv4)
Expand All @@ -79,7 +79,7 @@ namespace i2p
}
if (ipv6)
{
std::string host = "::";
std::string host = "::1";
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
i2p::config::GetOption("host", host);
else if (!ifname.empty())
Expand All @@ -103,17 +103,16 @@ namespace i2p
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);
UpdateNTCP2Address (true);
if (!ntcp) // NTCP2 should replace NTCP
{
bool published; i2p::config::GetOption("ntcp2.published", published);
if (published)
PublishNTCP2Address (port, true);
}
}

}

void RouterContext::UpdateRouterInfo ()
Expand All @@ -126,14 +125,14 @@ namespace i2p
void RouterContext::NewNTCP2Keys ()
{
m_StaticKeys.reset (new i2p::crypto::X25519Keys ());
m_StaticKeys->GenerateKeys ();
m_StaticKeys->GenerateKeys ();
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32);
RAND_bytes (m_NTCP2Keys->iv, 16);
// save
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)
Expand Down Expand Up @@ -175,7 +174,7 @@ namespace i2p
{
if (!m_NTCP2Keys) return;
if (!port)
{
{
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
Expand All @@ -192,7 +191,7 @@ namespace i2p
}
}
if (updated)
UpdateRouterInfo ();
UpdateRouterInfo ();
}

void RouterContext::UpdateNTCP2Address (bool enable)
Expand All @@ -205,22 +204,22 @@ namespace i2p
{
found = true;
if (!enable)
{
{
addresses.erase (it);
updated= true;
}
break;
}
}
if (enable && !found)
{
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
{
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
updated = true;
}
if (updated)
UpdateRouterInfo ();
}

void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
{
bool updated = false;
Expand Down Expand Up @@ -371,7 +370,7 @@ namespace i2p
break;
}
}
}
}
}
else
{
Expand All @@ -386,9 +385,9 @@ namespace i2p
else
++it;
}
}
}
}
}

void RouterContext::SetUnreachable ()
{
// set caps
Expand Down Expand Up @@ -421,7 +420,7 @@ namespace i2p
m_RouterInfo.SetCaps (caps);
// insert NTCP back
bool ntcp; i2p::config::GetOption("ntcp", ntcp);
if (ntcp)
if (ntcp)
PublishNTCPAddress (true);
// delete previous introducers
auto& addresses = m_RouterInfo.GetAddresses ();
Expand Down Expand Up @@ -516,11 +515,11 @@ namespace i2p

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;
}
if (updated)
UpdateRouterInfo ();
UpdateRouterInfo ();
}

void RouterContext::UpdateStats ()
Expand Down Expand Up @@ -563,16 +562,16 @@ namespace i2p
}
// read NTCP2 keys if available
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
if (n2k)
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.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
}
n2k.close ();
}
// read RouterInfo
Expand Down Expand Up @@ -667,14 +666,14 @@ namespace i2p
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{
if (!m_StaticKeys)
{
{
if (!m_NTCP2Keys) NewNTCP2Keys ();
auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
if (!m_StaticKeys)
m_StaticKeys.reset (x);
else
delete x;
}
return *m_StaticKeys;
}
return *m_StaticKeys;
}
}
7 changes: 5 additions & 2 deletions libi2pd/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,10 @@ namespace net
{
#ifdef 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
int af = (ipv6 ? AF_INET6 : AF_INET);
ifaddrs * addrs = nullptr;
Expand Down Expand Up @@ -327,7 +330,7 @@ namespace net
std::string fallback;
if(ipv6)
{
fallback = "::";
fallback = "::1";
LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname);
} else {
fallback = "127.0.0.1";
Expand Down

0 comments on commit 73921b1

Please sign in to comment.