Browse Source

backport GetMTU

pull/304/head
orignal 9 years ago
parent
commit
a8f2239495
  1. 201
      util.cpp

201
util.cpp

@ -505,21 +505,25 @@ namespace http @@ -505,21 +505,25 @@ namespace http
}
namespace net
{
int GetMTU (const boost::asio::ip::address& localAddress)
{
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
{
ifaddrs* ifaddr, *ifa = nullptr;
if(getifaddrs(&ifaddr) == -1)
{
LogPrint(eLogError, "Can't excute getifaddrs");
return 0;
return fallback;
}
int family = 0;
// loook for interface matching local address
// look for interface matching local address
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr) continue;
if(!ifa->ifa_addr)
continue;
family = ifa->ifa_addr->sa_family;
if(family == AF_INET && localAddress.is_v4())
{
@ -534,9 +538,9 @@ namespace net @@ -534,9 +538,9 @@ namespace net
break; // address matches
}
}
int mtu = 0;
if (ifa && family) // interface found?
{
int mtu = fallback;
if(ifa && family)
{ // interface found?
int fd = socket(family, SOCK_DGRAM, 0);
if(fd > 0)
{
@ -553,152 +557,165 @@ namespace net @@ -553,152 +557,165 @@ namespace net
}
else
LogPrint(eLogWarning, "Interface for local address", localAddress.to_string(), " not found");
freeifaddrs(ifaddr);
return mtu;
#elif defined(WIN32)
int result = 576; // fallback MTU
return mtu;
}
DWORD dwRetVal = 0;
#elif defined(_WIN32)
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;
#ifdef UNICODE
string localAddress_temporary = localAddress.to_string();
wstring localAddressUniversal (localAddress_temporary.begin(), localAddress_temporary.end());
#else
std::string localAddressUniversal = localAddress.to_string();
#endif
if (localAddress.is_v4())
{
struct sockaddr_in inputAddress;
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW)
{
== ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
dwRetVal = GetAdaptersAddresses (AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen);
if (dwRetVal == NO_ERROR)
{
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
if(dwRetVal != NO_ERROR) {
LogPrint(
eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
);
FREE(pAddresses);
return fallback;
}
pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if (pUnicast != nullptr)
{
for (int i = 0; pUnicast != nullptr; ++i)
{
if(pUnicast == nullptr) {
LogPrint(
eLogError, "GetMTU() has failed: not a unicast ipv4 address, this is not supported"
);
}
for(int i = 0; pUnicast != nullptr; ++i) {
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
struct sockaddr_in *localInterfaceAddress = (struct sockaddr_in*) lpAddr;
if (localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr)
{
result = pAddresses->Mtu;
sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr;
if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr) {
auto result = pAddresses->Mtu;
FREE(pAddresses);
pAddresses = nullptr;
return result;
}
pUnicast = pUnicast->Next;
}
}
else
{
LogPrint (eLogError, "GetMTU() has failed: not a unicast ipv4 address, this is not supported");
}
pCurrAddresses = pCurrAddresses->Next;
}
}
else
{
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed");
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv4 addresses found");
FREE(pAddresses);
return fallback;
}
}
else if (localAddress.is_v6())
int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback)
{
struct sockaddr_in6 inputAddress;
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
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)
{
== ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
dwRetVal = GetAdaptersAddresses (AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen);
if (dwRetVal == NO_ERROR)
{
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
if(dwRetVal != NO_ERROR) {
LogPrint(
eLogError,
"GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
);
FREE(pAddresses);
return fallback;
}
bool found_address = false;
pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if (pUnicast != nullptr)
{
for (int i = 0; pUnicast != nullptr; ++i)
{
if(pUnicast == nullptr) {
LogPrint(
eLogError,
"GetMTU() has failed: not a unicast ipv6 address, this is not supported"
);
}
for(int i = 0; pUnicast != nullptr; ++i) {
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
struct sockaddr_in6 *localInterfaceAddress = (struct sockaddr_in6*) lpAddr;
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])
{
for (int j = 0; j != 8; ++j) {
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j]) {
break;
}
else
{
} else {
found_address = true;
}
}
if (found_address)
{
result = pAddresses->Mtu;
} if (found_address) {
auto result = pAddresses->Mtu;
FREE(pAddresses);
pAddresses = nullptr;
return result;
}
pUnicast = pUnicast->Next;
}
}
else
{
LogPrint (eLogError, "GetMTU() has failed: not a unicast ipv6 address, this is not supported");
}
pCurrAddresses = pCurrAddresses->Next;
}
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv6 addresses found");
FREE(pAddresses);
return fallback;
}
else
int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
{
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed");
#ifdef UNICODE
string localAddress_temporary = localAddress.to_string();
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
#else
std::string localAddressUniversal = localAddress.to_string();
#endif
if(localAddress.is_v4()) {
sockaddr_in inputAddress;
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
return GetMTUWindowsIpv4(inputAddress, fallback);
} else if(localAddress.is_v6()) {
sockaddr_in6 inputAddress;
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
return GetMTUWindowsIpv6(inputAddress, fallback);
} else {
LogPrint(eLogError, "GetMTU() has failed: address family is not supported");
return fallback;
}
}
else
#endif // WIN32
int GetMTU(const boost::asio::ip::address& localAddress)
{
LogPrint (eLogError, "GetMTU() has failed: address family is not supported");
}
const int fallback = 576; // fallback MTU
FREE (pAddresses);
pAddresses = nullptr;
LogPrint(eLogError, "GetMTU() error: control flow should never reach this line");
return result;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
return GetMTUUnix(localAddress, fallback);
#elif defined(WIN32)
return GetMTUWindows(localAddress, fallback);
#endif
return fallback;
}
}

Loading…
Cancel
Save