Browse Source

backport GetMTU

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

365
util.cpp

@ -506,200 +506,217 @@ namespace http
namespace net namespace net
{ {
int GetMTU (const boost::asio::ip::address& localAddress) #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
{
#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) ifaddrs* ifaddr, *ifa = nullptr;
if(getifaddrs(&ifaddr) == -1)
{ {
LogPrint (eLogError, "Can't excute getifaddrs"); LogPrint(eLogError, "Can't excute getifaddrs");
return 0; return fallback;
} }
int family = 0;
// loook for interface matching local address int family = 0;
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) // look for interface matching local address
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{ {
if (!ifa->ifa_addr) continue; if(!ifa->ifa_addr)
family = ifa->ifa_addr->sa_family; continue;
if (family == AF_INET && localAddress.is_v4 ())
family = ifa->ifa_addr->sa_family;
if(family == AF_INET && localAddress.is_v4())
{ {
sockaddr_in * sa = (sockaddr_in *)ifa->ifa_addr; sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
if (!memcmp (&sa->sin_addr, localAddress.to_v4 ().to_bytes ().data (), 4)) if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
break; // address matches break; // address matches
} }
else if (family == AF_INET6 && localAddress.is_v6 ()) else if(family == AF_INET6 && localAddress.is_v6())
{ {
sockaddr_in6 * sa = (sockaddr_in6 *)ifa->ifa_addr; sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
if (!memcmp (&sa->sin6_addr, localAddress.to_v6 ().to_bytes ().data (), 16)) if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
break; // address matches break; // address matches
} }
} }
int mtu = 0; int mtu = fallback;
if (ifa && family) // interface found? if(ifa && family)
{ { // interface found?
int fd = socket (family, SOCK_DGRAM, 0); int fd = socket(family, SOCK_DGRAM, 0);
if (fd > 0) if(fd > 0)
{ {
ifreq ifr; ifreq ifr;
strncpy (ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query
if (ioctl (fd, SIOCGIFMTU, &ifr) >= 0) if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
mtu = ifr.ifr_mtu; // MTU mtu = ifr.ifr_mtu; // MTU
else else
LogPrint (eLogError, "Failed to run ioctl"); LogPrint (eLogError, "Failed to run ioctl");
close (fd); close(fd);
} }
else else
LogPrint (eLogError, "Failed to create datagram socket"); LogPrint(eLogError, "Failed to create datagram socket");
} }
else else
LogPrint (eLogWarning, "Interface for local address", localAddress.to_string (), " not found"); LogPrint(eLogWarning, "Interface for local address", localAddress.to_string(), " not found");
freeifaddrs(ifaddr);
freeifaddrs (ifaddr);
return mtu; return mtu;
#elif defined(WIN32) }
#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;
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
int result = 576; // fallback MTU DWORD dwRetVal = GetAdaptersAddresses(
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
DWORD dwRetVal = 0; if(dwRetVal != NO_ERROR) {
ULONG outBufLen = 0; LogPrint(
PIP_ADAPTER_ADDRESSES pAddresses = nullptr; eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr; );
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr; FREE(pAddresses);
return fallback;
}
#ifdef UNICODE pCurrAddresses = pAddresses;
string localAddress_temporary = localAddress.to_string(); while(pCurrAddresses) {
wstring localAddressUniversal (localAddress_temporary.begin(), localAddress_temporary.end()); PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
#else
std::string localAddressUniversal = localAddress.to_string(); pUnicast = pCurrAddresses->FirstUnicastAddress;
#endif 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;
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);
return result;
}
pUnicast = pUnicast->Next;
}
pCurrAddresses = pCurrAddresses->Next;
}
if (localAddress.is_v4()) LogPrint(eLogError, "GetMTU() error: no usable unicast ipv4 addresses found");
{ FREE(pAddresses);
struct sockaddr_in inputAddress; return fallback;
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr)); }
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) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
if (GetAdaptersAddresses (AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen) DWORD dwRetVal = GetAdaptersAddresses(
== ERROR_BUFFER_OVERFLOW) AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
{ );
FREE (pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC (outBufLen); if(dwRetVal != NO_ERROR) {
} LogPrint(
eLogError,
"GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
);
FREE(pAddresses);
return fallback;
}
dwRetVal = GetAdaptersAddresses (AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen); bool found_address = false;
if (dwRetVal == NO_ERROR) pCurrAddresses = pAddresses;
{ while(pCurrAddresses) {
pCurrAddresses = pAddresses; PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
while (pCurrAddresses) pUnicast = pCurrAddresses->FirstUnicastAddress;
{ if(pUnicast == nullptr) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress; LogPrint(
eLogError,
pUnicast = pCurrAddresses->FirstUnicastAddress; "GetMTU() has failed: not a unicast ipv6 address, this is not supported"
if (pUnicast != nullptr) );
{ }
for (int i = 0; pUnicast != nullptr; ++i) for(int i = 0; pUnicast != nullptr; ++i) {
{ LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr; sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
struct sockaddr_in *localInterfaceAddress = (struct sockaddr_in*) lpAddr;
if (localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr) for (int j = 0; j != 8; ++j) {
{ if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j]) {
result = pAddresses->Mtu; break;
FREE (pAddresses); } else {
pAddresses = nullptr; found_address = true;
return result; }
} } if (found_address) {
pUnicast = pUnicast->Next; auto result = pAddresses->Mtu;
} FREE(pAddresses);
} pAddresses = nullptr;
else return result;
{ }
LogPrint (eLogError, "GetMTU() has failed: not a unicast ipv4 address, this is not supported"); pUnicast = pUnicast->Next;
} }
pCurrAddresses = pCurrAddresses->Next; pCurrAddresses = pCurrAddresses->Next;
} }
} LogPrint(eLogError, "GetMTU() error: no usable unicast ipv6 addresses found");
else FREE(pAddresses);
{ return fallback;
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"); }
}
} int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
else if (localAddress.is_v6()) {
{ #ifdef UNICODE
struct sockaddr_in6 inputAddress; string localAddress_temporary = localAddress.to_string();
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr)); wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
#else
std::string localAddressUniversal = localAddress.to_string();
#endif
if (GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen) if(localAddress.is_v4()) {
== ERROR_BUFFER_OVERFLOW) sockaddr_in inputAddress;
{ inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
FREE (pAddresses); return GetMTUWindowsIpv4(inputAddress, fallback);
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC (outBufLen); } 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;
}
dwRetVal = GetAdaptersAddresses (AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen); }
if (dwRetVal == NO_ERROR) #endif // WIN32
{
bool found_address = false;
pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if (pUnicast != nullptr)
{
for (int i = 0; pUnicast != nullptr; ++i)
{
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
struct sockaddr_in6 *localInterfaceAddress = (struct sockaddr_in6*) lpAddr;
for (int j = 0; j != 8; ++j)
{
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j])
{
break;
}
else
{
found_address = true;
}
}
if (found_address)
{
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;
}
} int GetMTU(const boost::asio::ip::address& localAddress)
else {
{ const int fallback = 576; // fallback MTU
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed");
}
}
else
{
LogPrint (eLogError, "GetMTU() has failed: address family is not supported");
}
FREE (pAddresses); #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
pAddresses = nullptr; return GetMTUUnix(localAddress, fallback);
LogPrint(eLogError, "GetMTU() error: control flow should never reach this line"); #elif defined(WIN32)
return result; return GetMTUWindows(localAddress, fallback);
#endif #endif
} return fallback;
}
} }
} // util } // util

Loading…
Cancel
Save