Browse Source

Use GetProcAddress for inet_pton. Fixed build error

pull/1441/head
orignal 5 years ago
parent
commit
515c086099
  1. 30
      libi2pd/RouterContext.cpp
  2. 18
      libi2pd/util.cpp

30
libi2pd/RouterContext.cpp

@ -27,12 +27,8 @@ namespace i2p
void RouterContext::Init () void RouterContext::Init ()
{ {
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000); srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
#ifdef WIN32 m_StartupTime = std::chrono::steady_clock::now();
// for compatibility with WinXP
m_StartupTime = i2p::util::GetSecondsSinceEpoch ();
#else
m_StartupTime = std::chrono::steady_clock::now();
#endif
if (!Load ()) if (!Load ())
CreateNewRouter (); CreateNewRouter ();
m_Decryptor = m_Keys.CreateDecryptor (nullptr); 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 (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish) && (!v4only || address->host.is_v4 ()))
{ {
if (!port && !address->port) if (!port && !address->port)
{ {
// select random port only if address's port is not set // select random port only if address's port is not set
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
} }
if (port) address->port = port; if (port) address->port = port;
address->cost = publish ? 3 : 14; address->cost = publish ? 3 : 14;
address->ntcp2->isPublished = publish; address->ntcp2->isPublished = publish;
@ -438,14 +434,14 @@ namespace i2p
} }
// remove NTCP or NTCP2 v4 address // remove NTCP or NTCP2 v4 address
bool ntcp; i2p::config::GetOption("ntcp", ntcp); bool ntcp; i2p::config::GetOption("ntcp", ntcp);
if (ntcp) if (ntcp)
PublishNTCPAddress (false); PublishNTCPAddress (false);
else else
{ {
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
if (ntcp2) if (ntcp2)
PublishNTCP2Address (port, false, true); PublishNTCP2Address (port, false, true);
} }
// update // update
UpdateRouterInfo (); UpdateRouterInfo ();
} }
@ -495,7 +491,7 @@ namespace i2p
void RouterContext::SetSupportsV6 (bool supportsV6) void RouterContext::SetSupportsV6 (bool supportsV6)
{ {
if (supportsV6) if (supportsV6)
{ {
m_RouterInfo.EnableV6 (); m_RouterInfo.EnableV6 ();
// insert v6 addresses if necessary // insert v6 addresses if necessary
bool foundSSU = false, foundNTCP = false, foundNTCP2 = false; bool foundSSU = false, foundNTCP = false, foundNTCP2 = false;
@ -513,7 +509,7 @@ namespace i2p
} }
else else
foundNTCP = true; foundNTCP = true;
} }
port = addr->port; port = addr->port;
} }
if (!port) i2p::config::GetOption("port", port); if (!port) i2p::config::GetOption("port", port);
@ -525,7 +521,7 @@ namespace i2p
{ {
std::string host = "::1"; // TODO: read host std::string host = "::1"; // TODO: read host
m_RouterInfo.AddSSUAddress (host.c_str (), port, GetIdentHash ()); m_RouterInfo.AddSSUAddress (host.c_str (), port, GetIdentHash ());
} }
} }
// NTCP2 // NTCP2
if (!foundNTCP2) if (!foundNTCP2)
@ -534,11 +530,11 @@ namespace i2p
bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published); bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
if (ntcp2 && ntcp2Published) if (ntcp2 && ntcp2Published)
{ {
std::string ntcp2Host; std::string ntcp2Host;
if (!i2p::config::IsDefault ("ntcp2.addressv6")) if (!i2p::config::IsDefault ("ntcp2.addressv6"))
i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host); i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host);
else else
ntcp2Host = "::1"; ntcp2Host = "::1";
uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port); uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
if (!ntcp2Port) ntcp2Port = port; if (!ntcp2Port) ntcp2Port = port;
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address::from_string (ntcp2Host), ntcp2Port); 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); bool ntcp; i2p::config::GetOption("ntcp", ntcp);
if (ntcp) if (ntcp)
{ {
std::string host = "::1"; std::string host = "::1";
m_RouterInfo.AddNTCPAddress (host.c_str (), port); m_RouterInfo.AddNTCPAddress (host.c_str (), port);
} }
} }
} }
else else
m_RouterInfo.DisableV6 (); m_RouterInfo.DisableV6 ();

18
libi2pd/util.cpp

@ -22,7 +22,7 @@
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(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 // 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) int inet_pton_xp(int af, const char *src, void *dst)
{ {
@ -206,25 +206,21 @@ namespace net
#else #else
std::string localAddressUniversal = localAddress.to_string(); std::string localAddressUniversal = localAddress.to_string();
#endif #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()) if(localAddress.is_v4())
{ {
sockaddr_in inputAddress; sockaddr_in inputAddress;
if (isXP) inetpton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
inet_pton_xp(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
else
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
return GetMTUWindowsIpv4(inputAddress, fallback); return GetMTUWindowsIpv4(inputAddress, fallback);
} }
else if(localAddress.is_v6()) else if(localAddress.is_v6())
{ {
sockaddr_in6 inputAddress; sockaddr_in6 inputAddress;
if (isXP) inetpton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
inet_pton_xp(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
else
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
return GetMTUWindowsIpv6(inputAddress, fallback); return GetMTUWindowsIpv6(inputAddress, fallback);
} else { } else {
LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported"); LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");

Loading…
Cancel
Save