From 515c0860990c52b185a3fa36ebcad4bcfcf83a74 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 12 Nov 2019 15:06:04 -0500 Subject: [PATCH] Use GetProcAddress for inet_pton. Fixed build error --- libi2pd/RouterContext.cpp | 30 +++++++++++++----------------- libi2pd/util.cpp | 18 +++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index baa5a3d0..bf3459d8 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -27,12 +27,8 @@ namespace i2p void RouterContext::Init () { srand (i2p::util::GetMillisecondsSinceEpoch () % 1000); -#ifdef WIN32 - // for compatibility with WinXP - m_StartupTime = i2p::util::GetSecondsSinceEpoch (); -#else - m_StartupTime = std::chrono::steady_clock::now(); -#endif + m_StartupTime = std::chrono::steady_clock::now(); + if (!Load ()) CreateNewRouter (); m_Decryptor = m_Keys.CreateDecryptor (nullptr); @@ -194,11 +190,11 @@ namespace i2p if (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish) && (!v4only || address->host.is_v4 ())) { if (!port && !address->port) - { + { // select random port only if address's port is not set port = rand () % (30777 - 9111) + 9111; // I2P network ports range if (port == 9150) port = 9151; // Tor browser - } + } if (port) address->port = port; address->cost = publish ? 3 : 14; address->ntcp2->isPublished = publish; @@ -438,14 +434,14 @@ namespace i2p } // remove NTCP or NTCP2 v4 address bool ntcp; i2p::config::GetOption("ntcp", ntcp); - if (ntcp) + if (ntcp) PublishNTCPAddress (false); else { bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); if (ntcp2) PublishNTCP2Address (port, false, true); - } + } // update UpdateRouterInfo (); } @@ -495,7 +491,7 @@ namespace i2p void RouterContext::SetSupportsV6 (bool supportsV6) { if (supportsV6) - { + { m_RouterInfo.EnableV6 (); // insert v6 addresses if necessary bool foundSSU = false, foundNTCP = false, foundNTCP2 = false; @@ -513,7 +509,7 @@ namespace i2p } else foundNTCP = true; - } + } port = addr->port; } if (!port) i2p::config::GetOption("port", port); @@ -525,7 +521,7 @@ namespace i2p { std::string host = "::1"; // TODO: read host m_RouterInfo.AddSSUAddress (host.c_str (), port, GetIdentHash ()); - } + } } // NTCP2 if (!foundNTCP2) @@ -534,11 +530,11 @@ namespace i2p bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published); if (ntcp2 && ntcp2Published) { - std::string ntcp2Host; + std::string ntcp2Host; if (!i2p::config::IsDefault ("ntcp2.addressv6")) i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host); else - ntcp2Host = "::1"; + ntcp2Host = "::1"; uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port); if (!ntcp2Port) ntcp2Port = port; m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address::from_string (ntcp2Host), ntcp2Port); @@ -550,10 +546,10 @@ namespace i2p bool ntcp; i2p::config::GetOption("ntcp", ntcp); if (ntcp) { - std::string host = "::1"; + std::string host = "::1"; m_RouterInfo.AddNTCPAddress (host.c_str (), port); } - } + } } else m_RouterInfo.DisableV6 (); diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index 8f870ec4..448d1307 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -22,7 +22,7 @@ #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) -// inet_pton exists Windows since Vista, but XP haven't that function! +// inet_pton exists Windows since Vista, but XP doesn't have that function! // This function was written by Petar Korponai?. See http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found int inet_pton_xp(int af, const char *src, void *dst) { @@ -206,25 +206,21 @@ namespace net #else std::string localAddressUniversal = localAddress.to_string(); #endif - - bool isXP = IsWindowsXPorLater(); + + typedef int (* IPN)(int af, const char *src, void *dst); + IPN inetpton = (IPN)GetProcAddress (GetModuleHandle ("ws2_32.dll"), "InetPton"); + if (!inetpton) inetpton = inet_pton_xp; // use own implementation if not found if(localAddress.is_v4()) { sockaddr_in inputAddress; - if (isXP) - inet_pton_xp(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr)); - else - inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr)); + inetpton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr)); return GetMTUWindowsIpv4(inputAddress, fallback); } else if(localAddress.is_v6()) { sockaddr_in6 inputAddress; - if (isXP) - inet_pton_xp(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr)); - else - inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr)); + inetpton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr)); return GetMTUWindowsIpv6(inputAddress, fallback); } else { LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");