From 73921b1024c8e967db24c3a7249ce3a0418e3f84 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Wed, 8 May 2019 00:45:52 +0300 Subject: [PATCH] fix ipv6 fallback address --- libi2pd/RouterContext.cpp | 67 +++++++++++++++++++-------------------- libi2pd/util.cpp | 7 ++-- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index dacb66af..4a229546 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -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 (); } @@ -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) @@ -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()) @@ -103,9 +103,9 @@ 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); @@ -113,7 +113,6 @@ namespace i2p PublishNTCP2Address (port, true); } } - } void RouterContext::UpdateRouterInfo () @@ -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) @@ -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 } @@ -192,7 +191,7 @@ namespace i2p } } if (updated) - UpdateRouterInfo (); + UpdateRouterInfo (); } void RouterContext::UpdateNTCP2Address (bool enable) @@ -205,7 +204,7 @@ namespace i2p { found = true; if (!enable) - { + { addresses.erase (it); updated= true; } @@ -213,14 +212,14 @@ namespace i2p } } 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; @@ -371,7 +370,7 @@ namespace i2p break; } } - } + } } else { @@ -386,9 +385,9 @@ namespace i2p else ++it; } - } - } - + } + } + void RouterContext::SetUnreachable () { // set caps @@ -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 (); @@ -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 () @@ -563,7 +562,7 @@ 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(); @@ -571,8 +570,8 @@ namespace i2p 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 @@ -667,7 +666,7 @@ 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) @@ -675,6 +674,6 @@ namespace i2p else delete x; } - return *m_StaticKeys; - } + return *m_StaticKeys; + } } diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index 67366671..96e8ad4c 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -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; @@ -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";