i2pd/libi2pd/util.cpp

388 lines
10 KiB
C++
Raw Normal View History

2015-02-07 20:34:25 +03:00
#include <cstdlib>
#include <string>
#include <boost/asio.hpp>
2014-01-30 07:28:07 +04:00
#include "util.h"
#include "Log.h"
2014-01-30 07:28:07 +04:00
2015-12-28 03:00:00 +03:00
#ifdef WIN32
2015-03-15 18:51:07 +03:00
#include <stdlib.h>
#include <string.h>
2018-01-06 06:48:51 +03:00
#include <stdio.h>
2019-02-05 14:13:23 +03:00
#include <sysinfoapi.h>
2015-03-15 18:51:07 +03:00
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <shlobj.h>
2015-03-15 18:51:07 +03:00
#ifdef _MSC_VER
2015-03-15 18:51:07 +03:00
#pragma comment(lib, "IPHLPAPI.lib")
2015-12-28 03:00:00 +03:00
#endif // _MSC_VER
2015-03-15 18:51:07 +03:00
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
// 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
2019-02-05 14:13:23 +03:00
int inet_pton_xp(int af, const char *src, void *dst)
{
2015-03-15 18:51:07 +03:00
struct sockaddr_storage ss;
int size = sizeof (ss);
char src_copy[INET6_ADDRSTRLEN + 1];
ZeroMemory (&ss, sizeof (ss));
2015-11-20 19:42:38 +03:00
strncpy (src_copy, src, INET6_ADDRSTRLEN + 1);
2015-03-15 18:51:07 +03:00
src_copy[INET6_ADDRSTRLEN] = 0;
if (WSAStringToAddress (src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
{
switch (af)
{
case AF_INET:
*(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
return 1;
case AF_INET6:
*(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
return 1;
}
}
return 0;
2019-02-05 14:13:23 +03:00
}
2015-12-28 03:00:00 +03:00
#else /* !WIN32 => UNIX */
#include <sys/types.h>
#include <ifaddrs.h>
#endif
2014-01-30 07:28:07 +04:00
namespace i2p
{
namespace util
{
2020-02-03 01:05:30 +03:00
void RunnableService::StartService ()
{
if (!m_IsRunning)
{
m_IsRunning = true;
m_Thread.reset (new std::thread (std::bind (& RunnableService::Run, this)));
}
}
void RunnableService::StopService ()
{
if (m_IsRunning)
{
m_IsRunning = false;
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
m_Thread = nullptr;
}
}
}
void RunnableService::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, m_Name, ": runtime exception: ", ex.what ());
}
}
}
2014-10-31 21:17:52 +03:00
namespace net
{
2015-12-28 18:52:02 +03:00
#ifdef WIN32
bool IsWindowsXPorLater()
2019-11-12 22:19:14 +03:00
{
static bool isRequested = false;
static bool isXP = false;
if (!isRequested)
{
// request
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
2019-11-12 22:19:14 +03:00
isXP = osvi.dwMajorVersion <= 5;
isRequested = true;
}
return isXP;
2019-02-05 14:13:23 +03:00
}
int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback)
{
ULONG outBufLen = 0;
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW)
{
2019-02-05 14:13:23 +03:00
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
if(dwRetVal != NO_ERROR)
{
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
FREE(pAddresses);
return fallback;
}
pCurrAddresses = pAddresses;
while(pCurrAddresses)
{
2019-02-05 14:13:23 +03:00
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if(pUnicast == nullptr)
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv4 address, this is not supported");
for(int i = 0; pUnicast != nullptr; ++i)
{
2019-02-05 14:13:23 +03:00
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr;
if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr)
{
2019-02-05 14:13:23 +03:00
auto result = pAddresses->Mtu;
FREE(pAddresses);
return result;
}
pUnicast = pUnicast->Next;
}
pCurrAddresses = pCurrAddresses->Next;
}
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv4 addresses found");
FREE(pAddresses);
return fallback;
}
int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback)
{
ULONG outBufLen = 0;
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW)
{
2019-02-05 14:13:23 +03:00
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
if(dwRetVal != NO_ERROR)
{
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
FREE(pAddresses);
return fallback;
}
bool found_address = false;
pCurrAddresses = pAddresses;
while(pCurrAddresses)
{
2019-02-05 14:13:23 +03:00
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if(pUnicast == nullptr)
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv6 address, this is not supported");
for(int i = 0; pUnicast != nullptr; ++i)
{
2019-02-05 14:13:23 +03:00
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
for (int j = 0; j != 8; ++j)
{
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j])
2019-02-05 14:13:23 +03:00
break;
else
2019-02-05 14:13:23 +03:00
found_address = true;
}
if (found_address)
{
2019-02-05 14:13:23 +03:00
auto result = pAddresses->Mtu;
FREE(pAddresses);
pAddresses = nullptr;
return result;
}
pUnicast = pUnicast->Next;
}
pCurrAddresses = pCurrAddresses->Next;
}
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv6 addresses found");
FREE(pAddresses);
return fallback;
}
int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
{
2015-11-20 18:36:04 +03:00
#ifdef UNICODE
2019-02-05 14:13:23 +03:00
string localAddress_temporary = localAddress.to_string();
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
2015-11-20 18:36:04 +03:00
#else
2019-02-05 14:13:23 +03:00
std::string localAddressUniversal = localAddress.to_string();
2015-11-20 18:36:04 +03:00
#endif
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
2019-02-05 14:13:23 +03:00
if(localAddress.is_v4())
{
2019-02-05 14:13:23 +03:00
sockaddr_in inputAddress;
inetpton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
2019-02-05 14:13:23 +03:00
return GetMTUWindowsIpv4(inputAddress, fallback);
}
else if(localAddress.is_v6())
{
2019-11-12 22:19:14 +03:00
sockaddr_in6 inputAddress;
inetpton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
2019-02-05 14:13:23 +03:00
return GetMTUWindowsIpv6(inputAddress, fallback);
} else {
LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");
return fallback;
}
}
2015-12-28 18:52:02 +03:00
#else // assume unix
int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
2019-02-05 14:13:23 +03:00
{
ifaddrs* ifaddr, *ifa = nullptr;
if(getifaddrs(&ifaddr) == -1)
2015-12-28 18:52:02 +03:00
{
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
return fallback;
}
2015-12-28 18:52:02 +03:00
2019-02-05 14:13:23 +03:00
int family = 0;
// look for interface matching local address
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
2015-12-28 18:52:02 +03:00
{
2019-02-05 14:13:23 +03:00
if(!ifa->ifa_addr)
continue;
2015-12-28 18:52:02 +03:00
2019-02-05 14:13:23 +03:00
family = ifa->ifa_addr->sa_family;
if(family == AF_INET && localAddress.is_v4())
2015-12-28 18:52:02 +03:00
{
2019-02-05 14:13:23 +03:00
sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
break; // address matches
}
2018-01-06 06:48:51 +03:00
else if(family == AF_INET6 && localAddress.is_v6())
2015-12-28 18:52:02 +03:00
{
2019-02-05 14:13:23 +03:00
sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
break; // address matches
}
}
int mtu = fallback;
if(ifa && family)
2015-12-28 18:52:02 +03:00
{ // interface found?
2019-02-05 14:13:23 +03:00
int fd = socket(family, SOCK_DGRAM, 0);
if(fd > 0)
2015-12-28 18:52:02 +03:00
{
2019-02-05 14:13:23 +03:00
ifreq ifr;
strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query
if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
mtu = ifr.ifr_mtu; // MTU
else
LogPrint (eLogError, "NetIface: Failed to run ioctl: ", strerror(errno));
close(fd);
}
2015-12-28 18:52:02 +03:00
else
2019-02-05 14:13:23 +03:00
LogPrint(eLogError, "NetIface: Failed to create datagram socket");
}
2018-01-06 06:48:51 +03:00
else
2019-02-05 14:13:23 +03:00
LogPrint(eLogWarning, "NetIface: interface for local address", localAddress.to_string(), " not found");
freeifaddrs(ifaddr);
2015-12-28 18:52:02 +03:00
return mtu;
2019-02-05 14:13:23 +03:00
}
2015-11-20 18:36:04 +03:00
#endif // WIN32
2015-03-15 18:51:07 +03:00
2019-02-05 14:13:23 +03:00
int GetMTU(const boost::asio::ip::address& localAddress)
{
const int fallback = 576; // fallback MTU
2015-03-15 18:51:07 +03:00
2015-12-28 03:00:00 +03:00
#ifdef WIN32
2019-02-05 14:13:23 +03:00
return GetMTUWindows(localAddress, fallback);
2015-12-28 03:00:00 +03:00
#else
2019-02-05 14:13:23 +03:00
return GetMTUUnix(localAddress, fallback);
#endif
2019-02-05 14:13:23 +03:00
return fallback;
}
2018-01-06 06:48:51 +03:00
2016-06-29 18:06:51 +03:00
const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6)
{
#ifdef WIN32
LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32");
2019-05-08 00:45:52 +03:00
if(ipv6)
return boost::asio::ip::address::from_string("::1");
else
return boost::asio::ip::address::from_string("127.0.0.1");
2016-06-29 18:06:51 +03:00
#else
int af = (ipv6 ? AF_INET6 : AF_INET);
ifaddrs * addrs = nullptr;
if(getifaddrs(&addrs) == 0)
{
// got ifaddrs
ifaddrs * cur = addrs;
while(cur)
{
std::string cur_ifname(cur->ifa_name);
2016-06-29 18:57:44 +03:00
if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af)
2016-06-29 18:06:51 +03:00
{
// match
2019-11-12 15:17:57 +03:00
char addr[INET6_ADDRSTRLEN];
memset (addr, 0, INET6_ADDRSTRLEN);
2016-12-22 23:00:40 +03:00
if(af == AF_INET)
inet_ntop(af, &((sockaddr_in *)cur->ifa_addr)->sin_addr, addr, INET6_ADDRSTRLEN);
else
inet_ntop(af, &((sockaddr_in6 *)cur->ifa_addr)->sin6_addr, addr, INET6_ADDRSTRLEN);
freeifaddrs(addrs);
std::string cur_ifaddr(addr);
2016-06-29 18:06:51 +03:00
return boost::asio::ip::address::from_string(cur_ifaddr);
}
cur = cur->ifa_next;
}
}
if(addrs) freeifaddrs(addrs);
std::string fallback;
if(ipv6)
{
2019-05-08 00:45:52 +03:00
fallback = "::1";
2016-06-29 18:06:51 +03:00
LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname);
} else {
fallback = "127.0.0.1";
LogPrint(eLogWarning, "NetIface: cannot find ipv4 address for interface ", ifname);
}
return boost::asio::ip::address::from_string(fallback);
2018-01-06 06:48:51 +03:00
2016-06-29 18:06:51 +03:00
#endif
}
2018-01-06 06:48:51 +03:00
}
2014-10-31 21:34:08 +03:00
} // util
} // i2p