2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2022-02-18 06:20:06 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-02-07 17:34:25 +00:00
|
|
|
#include <cstdlib>
|
2014-01-31 06:22:11 +00:00
|
|
|
#include <string>
|
|
|
|
#include <boost/asio.hpp>
|
2016-07-27 02:00:00 +00:00
|
|
|
|
2014-01-30 03:28:07 +00:00
|
|
|
#include "util.h"
|
2014-01-31 06:22:11 +00:00
|
|
|
#include "Log.h"
|
2022-07-14 17:48:28 +00:00
|
|
|
#include "I2PEndian.h"
|
2014-01-30 03:28:07 +00:00
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
#if not defined (__FreeBSD__)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__) || defined(__FreeBSD__)
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-11-22 22:44:21 +00:00
|
|
|
#ifdef _WIN32
|
2015-03-15 15:51:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-01-06 03:48:51 +00:00
|
|
|
#include <stdio.h>
|
2019-02-05 11:13:23 +00:00
|
|
|
#include <sysinfoapi.h>
|
2015-03-15 15:51:07 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <iphlpapi.h>
|
2014-02-03 10:13:40 +00:00
|
|
|
#include <shlobj.h>
|
2015-03-15 15:51:07 +00:00
|
|
|
|
|
|
|
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
|
|
|
|
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
|
|
|
|
|
2022-07-27 12:41:14 +00:00
|
|
|
// inet_pton and inet_ntop have been in Windows since Vista, but XP doesn't have these functions!
|
2019-02-05 21:24:01 +00:00
|
|
|
// This function was written by Petar Korponai?. See http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found
|
2020-12-07 03:22:16 +00:00
|
|
|
int inet_pton_xp (int af, const char *src, void *dst)
|
2019-02-05 21:24:01 +00:00
|
|
|
{
|
2015-03-15 15:51:07 +00:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
int size = sizeof (ss);
|
|
|
|
char src_copy[INET6_ADDRSTRLEN + 1];
|
|
|
|
|
|
|
|
ZeroMemory (&ss, sizeof (ss));
|
2015-11-20 16:42:38 +00:00
|
|
|
strncpy (src_copy, src, INET6_ADDRSTRLEN + 1);
|
2015-03-15 15:51:07 +00: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 11:13:23 +00:00
|
|
|
}
|
2022-07-27 12:41:14 +00:00
|
|
|
|
|
|
|
const char *inet_ntop_xp(int af, const void *src, char *dst, socklen_t size)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
unsigned long s = size;
|
|
|
|
|
|
|
|
ZeroMemory(&ss, sizeof(ss));
|
|
|
|
ss.ss_family = af;
|
|
|
|
|
|
|
|
switch(af) {
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* cannot direclty use &size because of strict aliasing rules */
|
|
|
|
return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0)? dst : NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-22 22:44:21 +00:00
|
|
|
#else /* !_WIN32 => UNIX */
|
2015-12-28 00:00:00 +00:00
|
|
|
#include <sys/types.h>
|
2021-03-05 00:19:30 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "ifaddrs.h"
|
|
|
|
#else
|
2015-12-28 00:00:00 +00:00
|
|
|
#include <ifaddrs.h>
|
2014-02-03 10:13:40 +00:00
|
|
|
#endif
|
2021-03-05 00:19:30 +00:00
|
|
|
#endif
|
2014-02-03 10:13:40 +00:00
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
#define address_pair_v4(a,b) { boost::asio::ip::address_v4::from_string (a).to_ulong (), boost::asio::ip::address_v4::from_string (b).to_ulong () }
|
|
|
|
#define address_pair_v6(a,b) { boost::asio::ip::address_v6::from_string (a).to_bytes (), boost::asio::ip::address_v6::from_string (b).to_bytes () }
|
|
|
|
|
2014-01-30 03:28:07 +00:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2020-02-02 22:05:30 +00:00
|
|
|
|
2020-02-04 16:48:56 +00:00
|
|
|
void RunnableService::StartIOService ()
|
2020-02-02 22:05:30 +00:00
|
|
|
{
|
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
|
|
|
m_Thread.reset (new std::thread (std::bind (& RunnableService::Run, this)));
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-02-02 22:05:30 +00:00
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2020-02-04 16:48:56 +00:00
|
|
|
void RunnableService::StopIOService ()
|
2020-02-02 22:05:30 +00:00
|
|
|
{
|
|
|
|
if (m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = false;
|
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
m_Thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
2020-02-02 22:05:30 +00:00
|
|
|
|
|
|
|
void RunnableService::Run ()
|
|
|
|
{
|
2020-12-07 03:22:16 +00:00
|
|
|
SetThreadName(m_Name.c_str());
|
|
|
|
|
2020-02-02 22:05:30 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Service.run ();
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, m_Name, ": Runtime exception: ", ex.what ());
|
2020-02-02 22:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
void SetThreadName (const char *name) {
|
2022-02-11 19:17:38 +00:00
|
|
|
#if defined(__APPLE__) && !defined(__powerpc__)
|
2022-02-10 21:04:36 +00:00
|
|
|
pthread_setname_np((char*)name);
|
2020-12-07 16:47:50 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
|
2020-12-07 05:36:06 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2021-01-26 15:53:56 +00:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
pthread_setname_np(pthread_self(), "%s", (void *)name);
|
2021-09-04 07:07:09 +00:00
|
|
|
#elif !defined(__gnu_hurd__)
|
2020-12-07 03:22:16 +00:00
|
|
|
pthread_setname_np(pthread_self(), name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-31 18:17:52 +00:00
|
|
|
namespace net
|
|
|
|
{
|
2020-11-22 22:44:21 +00:00
|
|
|
#ifdef _WIN32
|
2020-12-07 03:22:16 +00:00
|
|
|
int GetMTUWindowsIpv4 (sockaddr_in inputAddress, int fallback)
|
2019-02-05 11:13:23 +00:00
|
|
|
{
|
2022-07-27 12:41:14 +00:00
|
|
|
typedef const char *(* IPN)(int af, const void *src, char *dst, socklen_t size);
|
|
|
|
IPN inetntop = (IPN)GetProcAddress (GetModuleHandle ("ws2_32.dll"), "InetNtop");
|
|
|
|
if (!inetntop) inetntop = inet_ntop_xp; // use own implementation if not found
|
|
|
|
|
2019-02-05 11:13:23 +00:00
|
|
|
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)
|
2019-02-05 21:24:01 +00:00
|
|
|
== ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwRetVal = GetAdaptersAddresses(
|
|
|
|
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
|
|
|
|
);
|
|
|
|
|
2019-02-05 21:24:01 +00:00
|
|
|
if(dwRetVal != NO_ERROR)
|
|
|
|
{
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: Enclosed GetAdaptersAddresses() call has failed");
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurrAddresses = pAddresses;
|
2019-02-05 21:24:01 +00:00
|
|
|
while(pCurrAddresses)
|
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
|
|
|
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
2019-02-05 21:24:01 +00:00
|
|
|
if(pUnicast == nullptr)
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: Not a unicast IPv4 address, this is not supported");
|
2019-02-05 21:24:01 +00:00
|
|
|
|
|
|
|
for(int i = 0; pUnicast != nullptr; ++i)
|
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
|
|
|
sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr;
|
2019-02-05 21:24:01 +00:00
|
|
|
if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr)
|
|
|
|
{
|
2022-07-27 10:24:07 +00:00
|
|
|
char addr[INET_ADDRSTRLEN];
|
2022-07-27 12:41:14 +00:00
|
|
|
inetntop(AF_INET, &(((struct sockaddr_in *)localInterfaceAddress)->sin_addr), addr, INET_ADDRSTRLEN);
|
2022-07-27 10:24:07 +00:00
|
|
|
|
|
|
|
auto result = pCurrAddresses->Mtu;
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
2022-07-17 20:34:32 +00:00
|
|
|
pAddresses = nullptr;
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogInfo, "NetIface: GetMTU: Using ", result, " bytes for IPv4 address ", addr);
|
2019-02-05 11:13:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
pUnicast = pUnicast->Next;
|
|
|
|
}
|
|
|
|
pCurrAddresses = pCurrAddresses->Next;
|
|
|
|
}
|
|
|
|
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: No usable unicast IPv4 addresses found");
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
int GetMTUWindowsIpv6 (sockaddr_in6 inputAddress, int fallback)
|
2019-02-05 11:13:23 +00:00
|
|
|
{
|
2022-07-27 12:41:14 +00:00
|
|
|
typedef const char *(* IPN)(int af, const void *src, char *dst, socklen_t size);
|
|
|
|
IPN inetntop = (IPN)GetProcAddress (GetModuleHandle ("ws2_32.dll"), "InetNtop");
|
|
|
|
if (!inetntop) inetntop = inet_ntop_xp; // use own implementation if not found
|
|
|
|
|
2019-02-05 11:13:23 +00:00
|
|
|
ULONG outBufLen = 0;
|
|
|
|
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
|
|
|
|
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
|
|
|
|
|
2022-07-27 10:24:07 +00:00
|
|
|
if (GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
|
2019-02-05 21:24:01 +00:00
|
|
|
== ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwRetVal = GetAdaptersAddresses(
|
|
|
|
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
|
|
|
|
);
|
|
|
|
|
2022-07-27 10:24:07 +00:00
|
|
|
if (dwRetVal != NO_ERROR)
|
2019-02-05 21:24:01 +00:00
|
|
|
{
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: Enclosed GetAdaptersAddresses() call has failed");
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found_address = false;
|
|
|
|
pCurrAddresses = pAddresses;
|
2022-07-27 10:24:07 +00:00
|
|
|
while (pCurrAddresses)
|
2019-02-05 21:24:01 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
2022-07-27 10:24:07 +00:00
|
|
|
if (pUnicast == nullptr)
|
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: Not a unicast IPv6 address, this is not supported");
|
2019-02-05 21:24:01 +00:00
|
|
|
|
2022-07-27 10:24:07 +00:00
|
|
|
for (int i = 0; pUnicast != nullptr; ++i)
|
2019-02-05 21:24:01 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
|
|
|
sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
|
|
|
|
|
2019-02-05 21:24:01 +00:00
|
|
|
for (int j = 0; j != 8; ++j)
|
|
|
|
{
|
|
|
|
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j])
|
2019-02-05 11:13:23 +00:00
|
|
|
break;
|
2019-02-05 21:24:01 +00:00
|
|
|
else
|
2019-02-05 11:13:23 +00:00
|
|
|
found_address = true;
|
2019-02-05 21:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found_address)
|
|
|
|
{
|
2022-07-27 10:24:07 +00:00
|
|
|
char addr[INET6_ADDRSTRLEN];
|
2022-07-27 12:41:14 +00:00
|
|
|
inetntop(AF_INET6, &(((struct sockaddr_in6 *)localInterfaceAddress)->sin6_addr), addr, INET6_ADDRSTRLEN);
|
2022-07-27 10:24:07 +00:00
|
|
|
|
|
|
|
auto result = pCurrAddresses->Mtu;
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
pAddresses = nullptr;
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogInfo, "NetIface: GetMTU: Using ", result, " bytes for IPv6 address ", addr);
|
2019-02-05 11:13:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
pUnicast = pUnicast->Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurrAddresses = pCurrAddresses->Next;
|
|
|
|
}
|
|
|
|
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: No usable unicast IPv6 addresses found");
|
2019-02-05 11:13:23 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
int GetMTUWindows (const boost::asio::ip::address& localAddress, int fallback)
|
2019-02-05 11:13:23 +00:00
|
|
|
{
|
2015-11-20 15:36:04 +00:00
|
|
|
#ifdef UNICODE
|
2019-02-05 11:13:23 +00:00
|
|
|
string localAddress_temporary = localAddress.to_string();
|
|
|
|
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
|
2015-11-20 15:36:04 +00:00
|
|
|
#else
|
2019-02-05 11:13:23 +00:00
|
|
|
std::string localAddressUniversal = localAddress.to_string();
|
2015-11-20 15:36:04 +00:00
|
|
|
#endif
|
2020-02-04 16:48:56 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
typedef int (* IPN)(int af, const char *src, void *dst);
|
2019-11-12 20:06:04 +00:00
|
|
|
IPN inetpton = (IPN)GetProcAddress (GetModuleHandle ("ws2_32.dll"), "InetPton");
|
2020-03-01 10:25:50 +00:00
|
|
|
if (!inetpton) inetpton = inet_pton_xp; // use own implementation if not found
|
2019-02-05 11:13:23 +00:00
|
|
|
|
2019-02-05 21:24:01 +00:00
|
|
|
if(localAddress.is_v4())
|
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
sockaddr_in inputAddress;
|
2020-03-01 10:25:50 +00:00
|
|
|
inetpton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
|
2019-02-05 11:13:23 +00:00
|
|
|
return GetMTUWindowsIpv4(inputAddress, fallback);
|
2019-02-05 21:24:01 +00:00
|
|
|
}
|
|
|
|
else if(localAddress.is_v6())
|
|
|
|
{
|
2019-11-12 19:19:14 +00:00
|
|
|
sockaddr_in6 inputAddress;
|
2020-03-01 10:25:50 +00:00
|
|
|
inetpton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
|
2019-02-05 11:13:23 +00:00
|
|
|
return GetMTUWindowsIpv6(inputAddress, fallback);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-27 10:24:07 +00:00
|
|
|
LogPrint(eLogError, "NetIface: GetMTU: Address family is not supported");
|
2019-02-05 11:13:23 +00:00
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 15:52:02 +00:00
|
|
|
#else // assume unix
|
2020-12-07 03:22:16 +00:00
|
|
|
int GetMTUUnix (const boost::asio::ip::address& localAddress, int fallback)
|
2019-02-05 11:13:23 +00:00
|
|
|
{
|
|
|
|
ifaddrs* ifaddr, *ifa = nullptr;
|
|
|
|
if(getifaddrs(&ifaddr) == -1)
|
2015-12-28 15:52:02 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
|
|
|
|
return fallback;
|
|
|
|
}
|
2015-12-28 15:52:02 +00:00
|
|
|
|
2019-02-05 11:13:23 +00:00
|
|
|
int family = 0;
|
|
|
|
// look for interface matching local address
|
|
|
|
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
|
2015-12-28 15:52:02 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
if(!ifa->ifa_addr)
|
|
|
|
continue;
|
2015-12-28 15:52:02 +00:00
|
|
|
|
2019-02-05 11:13:23 +00:00
|
|
|
family = ifa->ifa_addr->sa_family;
|
|
|
|
if(family == AF_INET && localAddress.is_v4())
|
2015-12-28 15:52:02 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00: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 03:48:51 +00:00
|
|
|
else if(family == AF_INET6 && localAddress.is_v6())
|
2015-12-28 15:52:02 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00: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 15:52:02 +00:00
|
|
|
{ // interface found?
|
2019-02-05 11:13:23 +00:00
|
|
|
int fd = socket(family, SOCK_DGRAM, 0);
|
|
|
|
if(fd > 0)
|
2015-12-28 15:52:02 +00:00
|
|
|
{
|
2019-02-05 11:13:23 +00:00
|
|
|
ifreq ifr;
|
2021-08-23 14:00:46 +00:00
|
|
|
strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ-1); // set interface for query
|
2019-02-05 11:13:23 +00:00
|
|
|
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 15:52:02 +00:00
|
|
|
else
|
2019-02-05 11:13:23 +00:00
|
|
|
LogPrint(eLogError, "NetIface: Failed to create datagram socket");
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
else
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "NetIface: Interface for local address", localAddress.to_string(), " not found");
|
2019-02-05 21:24:01 +00:00
|
|
|
freeifaddrs(ifaddr);
|
2015-12-28 15:52:02 +00:00
|
|
|
|
2019-02-05 21:24:01 +00:00
|
|
|
return mtu;
|
2019-02-05 11:13:23 +00:00
|
|
|
}
|
2020-11-22 22:44:21 +00:00
|
|
|
#endif // _WIN32
|
2015-03-15 15:51:07 +00:00
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
int GetMTU (const boost::asio::ip::address& localAddress)
|
2019-02-05 11:13:23 +00:00
|
|
|
{
|
2020-02-04 18:32:16 +00:00
|
|
|
int fallback = localAddress.is_v6 () ? 1280 : 620; // fallback MTU
|
2015-03-15 15:51:07 +00:00
|
|
|
|
2020-11-22 22:44:21 +00:00
|
|
|
#ifdef _WIN32
|
2019-02-05 11:13:23 +00:00
|
|
|
return GetMTUWindows(localAddress, fallback);
|
2015-12-28 00:00:00 +00:00
|
|
|
#else
|
2019-02-05 11:13:23 +00:00
|
|
|
return GetMTUUnix(localAddress, fallback);
|
2015-03-15 17:07:39 +00:00
|
|
|
#endif
|
2019-02-05 11:13:23 +00:00
|
|
|
return fallback;
|
|
|
|
}
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2020-12-07 03:22:16 +00:00
|
|
|
const boost::asio::ip::address GetInterfaceAddress (const std::string & ifname, bool ipv6)
|
2016-06-29 15:06:51 +00:00
|
|
|
{
|
2020-11-22 22:44:21 +00:00
|
|
|
#ifdef _WIN32
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogError, "NetIface: Cannot get address by interface name, not implemented on WIN32");
|
2019-05-07 21:45:52 +00: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 15:06:51 +00:00
|
|
|
#else
|
|
|
|
int af = (ipv6 ? AF_INET6 : AF_INET);
|
2022-02-10 21:04:36 +00:00
|
|
|
ifaddrs *addrs;
|
|
|
|
try
|
2016-06-29 15:06:51 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
if (!getifaddrs(&addrs))
|
2016-06-29 15:06:51 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
for (auto cur = addrs; cur; cur = cur->ifa_next)
|
2016-06-29 15:06:51 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
std::string cur_ifname(cur->ifa_name);
|
|
|
|
if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af)
|
|
|
|
{
|
|
|
|
// match
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
memset (addr, 0, INET6_ADDRSTRLEN);
|
|
|
|
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);
|
|
|
|
return boost::asio::ip::address::from_string(cur_ifaddr);
|
|
|
|
}
|
2016-06-29 15:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 21:04:36 +00:00
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2022-02-10 21:38:29 +00:00
|
|
|
LogPrint(eLogError, "NetIface: Exception while searching address using ifaddr: ", ex.what());
|
2022-02-10 21:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 15:06:51 +00:00
|
|
|
if(addrs) freeifaddrs(addrs);
|
|
|
|
std::string fallback;
|
2019-02-05 21:24:01 +00:00
|
|
|
if(ipv6)
|
|
|
|
{
|
2019-05-07 21:45:52 +00:00
|
|
|
fallback = "::1";
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "NetIface: Cannot find IPv6 address for interface ", ifname);
|
2016-06-29 15:06:51 +00:00
|
|
|
} else {
|
|
|
|
fallback = "127.0.0.1";
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "NetIface: Cannot find IPv4 address for interface ", ifname);
|
2016-06-29 15:06:51 +00:00
|
|
|
}
|
|
|
|
return boost::asio::ip::address::from_string(fallback);
|
|
|
|
#endif
|
|
|
|
}
|
2020-10-12 14:36:44 +00:00
|
|
|
|
2022-07-14 17:48:28 +00:00
|
|
|
int GetMaxMTU (const boost::asio::ip::address_v6& localAddress)
|
|
|
|
{
|
|
|
|
uint32_t prefix = bufbe32toh (localAddress.to_bytes ().data ());
|
|
|
|
switch (prefix)
|
|
|
|
{
|
|
|
|
case 0x20010470:
|
|
|
|
case 0x260070ff:
|
2022-07-27 10:24:07 +00:00
|
|
|
// Hurricane Electric
|
2022-07-14 17:48:28 +00:00
|
|
|
return 1480;
|
2022-07-24 19:39:46 +00:00
|
|
|
break;
|
2022-07-27 10:24:07 +00:00
|
|
|
case 0x2a06a003:
|
2022-07-14 17:48:28 +00:00
|
|
|
case 0x2a06a004:
|
2022-07-27 10:24:07 +00:00
|
|
|
case 0x2a06a005:
|
|
|
|
// route48
|
2022-07-14 17:48:28 +00:00
|
|
|
return 1420;
|
2022-07-27 10:24:07 +00:00
|
|
|
break;
|
|
|
|
default: ;
|
|
|
|
}
|
2022-07-14 17:48:28 +00:00
|
|
|
return 1500;
|
2022-07-27 10:24:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:27:49 +00:00
|
|
|
static bool IsYggdrasilAddress (const uint8_t addr[16])
|
|
|
|
{
|
|
|
|
return addr[0] == 0x02 || addr[0] == 0x03;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2021-01-29 18:27:49 +00:00
|
|
|
|
|
|
|
bool IsYggdrasilAddress (const boost::asio::ip::address& addr)
|
|
|
|
{
|
|
|
|
if (!addr.is_v6 ()) return false;
|
|
|
|
return IsYggdrasilAddress (addr.to_v6 ().to_bytes ().data ());
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:42:44 +00:00
|
|
|
boost::asio::ip::address_v6 GetYggdrasilAddress ()
|
|
|
|
{
|
2021-03-05 00:19:30 +00:00
|
|
|
#if defined(_WIN32)
|
2021-02-10 14:58:14 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
FREE(pAddresses);
|
|
|
|
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwRetVal = GetAdaptersAddresses(
|
|
|
|
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
|
|
|
|
);
|
|
|
|
|
|
|
|
if(dwRetVal != NO_ERROR)
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "NetIface: GetYggdrasilAddress(): enclosed GetAdaptersAddresses() call has failed");
|
|
|
|
FREE(pAddresses);
|
|
|
|
return boost::asio::ip::address_v6 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurrAddresses = pAddresses;
|
|
|
|
while(pCurrAddresses)
|
|
|
|
{
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
|
|
|
|
for(int i = 0; pUnicast != nullptr; ++i)
|
|
|
|
{
|
|
|
|
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
|
|
|
sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
|
|
|
|
if (IsYggdrasilAddress(localInterfaceAddress->sin6_addr.u.Byte)) {
|
|
|
|
boost::asio::ip::address_v6::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), &localInterfaceAddress->sin6_addr.u.Byte, 16);
|
|
|
|
FREE(pAddresses);
|
|
|
|
return boost::asio::ip::address_v6 (bytes);
|
|
|
|
}
|
|
|
|
pUnicast = pUnicast->Next;
|
|
|
|
}
|
|
|
|
pCurrAddresses = pCurrAddresses->Next;
|
|
|
|
}
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "NetIface: Interface with Yggdrasil network address not found");
|
2021-02-10 14:58:14 +00:00
|
|
|
FREE(pAddresses);
|
|
|
|
return boost::asio::ip::address_v6 ();
|
2021-01-25 00:42:44 +00:00
|
|
|
#else
|
2022-02-10 21:04:36 +00:00
|
|
|
ifaddrs *addrs;
|
|
|
|
try
|
2021-01-25 00:42:44 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
if (!getifaddrs(&addrs))
|
2021-01-25 00:42:44 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
for (auto cur = addrs; cur; cur = cur->ifa_next)
|
2021-01-25 00:42:44 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
if (cur->ifa_addr && cur->ifa_addr->sa_family == AF_INET6)
|
2021-01-25 00:42:44 +00:00
|
|
|
{
|
2022-02-10 21:04:36 +00:00
|
|
|
sockaddr_in6* sa = (sockaddr_in6*)cur->ifa_addr;
|
|
|
|
if (IsYggdrasilAddress(sa->sin6_addr.s6_addr))
|
|
|
|
{
|
|
|
|
boost::asio::ip::address_v6::bytes_type bytes;
|
|
|
|
memcpy (bytes.data (), &sa->sin6_addr, 16);
|
|
|
|
freeifaddrs(addrs);
|
|
|
|
return boost::asio::ip::address_v6 (bytes);
|
|
|
|
}
|
2021-01-26 15:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 00:42:44 +00:00
|
|
|
}
|
2022-02-10 21:04:36 +00:00
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2022-02-10 21:38:29 +00:00
|
|
|
LogPrint(eLogError, "NetIface: Exception while searching Yggdrasill address using ifaddr: ", ex.what());
|
2022-02-10 21:04:36 +00:00
|
|
|
}
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "NetIface: Interface with Yggdrasil network address not found");
|
2021-01-29 18:27:49 +00:00
|
|
|
if(addrs) freeifaddrs(addrs);
|
2021-01-25 00:42:44 +00:00
|
|
|
return boost::asio::ip::address_v6 ();
|
2021-01-26 15:53:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:29:13 +00:00
|
|
|
bool IsLocalAddress (const boost::asio::ip::address& addr)
|
|
|
|
{
|
2022-05-20 16:56:05 +00:00
|
|
|
auto mtu = // TODO: implement better
|
2021-02-03 00:29:13 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
GetMTUWindows(addr, 0);
|
|
|
|
#else
|
|
|
|
GetMTUUnix(addr, 0);
|
2021-11-27 20:30:35 +00:00
|
|
|
#endif
|
2021-02-03 00:29:13 +00:00
|
|
|
return mtu > 0;
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsInReservedRange (const boost::asio::ip::address& host)
|
2021-01-25 02:21:35 +00:00
|
|
|
{
|
2020-10-12 14:36:44 +00:00
|
|
|
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
|
2021-04-03 23:24:07 +00:00
|
|
|
if (host.is_unspecified ()) return false;
|
2020-10-12 14:36:44 +00:00
|
|
|
if(host.is_v4())
|
|
|
|
{
|
|
|
|
static const std::vector< std::pair<uint32_t, uint32_t> > reservedIPv4Ranges {
|
|
|
|
address_pair_v4("0.0.0.0", "0.255.255.255"),
|
|
|
|
address_pair_v4("10.0.0.0", "10.255.255.255"),
|
|
|
|
address_pair_v4("100.64.0.0", "100.127.255.255"),
|
|
|
|
address_pair_v4("127.0.0.0", "127.255.255.255"),
|
|
|
|
address_pair_v4("169.254.0.0", "169.254.255.255"),
|
|
|
|
address_pair_v4("172.16.0.0", "172.31.255.255"),
|
|
|
|
address_pair_v4("192.0.0.0", "192.0.0.255"),
|
|
|
|
address_pair_v4("192.0.2.0", "192.0.2.255"),
|
|
|
|
address_pair_v4("192.88.99.0", "192.88.99.255"),
|
|
|
|
address_pair_v4("192.168.0.0", "192.168.255.255"),
|
|
|
|
address_pair_v4("198.18.0.0", "192.19.255.255"),
|
|
|
|
address_pair_v4("198.51.100.0", "198.51.100.255"),
|
|
|
|
address_pair_v4("203.0.113.0", "203.0.113.255"),
|
|
|
|
address_pair_v4("224.0.0.0", "255.255.255.255")
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t ipv4_address = host.to_v4 ().to_ulong ();
|
|
|
|
for(const auto& it : reservedIPv4Ranges) {
|
|
|
|
if (ipv4_address >= it.first && ipv4_address <= it.second)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(host.is_v6())
|
|
|
|
{
|
|
|
|
static const std::vector< std::pair<boost::asio::ip::address_v6::bytes_type, boost::asio::ip::address_v6::bytes_type> > reservedIPv6Ranges {
|
|
|
|
address_pair_v6("2001:db8::", "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
address_pair_v6("fc00::", "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
|
2021-08-13 06:11:56 +00:00
|
|
|
address_pair_v6("fe80::", "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
address_pair_v6("ff00::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
|
|
|
|
address_pair_v6("::", "::"),
|
|
|
|
address_pair_v6("::1", "::1")
|
2020-10-12 14:36:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::ip::address_v6::bytes_type ipv6_address = host.to_v6 ().to_bytes ();
|
|
|
|
for(const auto& it : reservedIPv6Ranges) {
|
|
|
|
if (ipv6_address >= it.first && ipv6_address <= it.second)
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-01 23:00:03 +00:00
|
|
|
if (IsYggdrasilAddress (ipv6_address.data ())) // yggdrasil?
|
2021-01-25 02:21:35 +00:00
|
|
|
return true;
|
2020-10-12 14:36:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // net
|
2014-10-31 18:34:08 +00:00
|
|
|
} // util
|
|
|
|
} // i2p
|