Browse Source

Merge pull request #1292 from PurpleI2P/inet_pton_xp

inet_pton for winxp
pull/1295/head
R4SAS 6 years ago committed by GitHub
parent
commit
841452cb9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 372
      libi2pd/util.cpp

372
libi2pd/util.cpp

@ -9,6 +9,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <sysinfoapi.h>
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <iphlpapi.h> #include <iphlpapi.h>
@ -21,8 +22,8 @@
#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))
/* // No more needed. Exists in MinGW. // inet_pton exists in Windows Vista, but XP haven't that function!
int inet_pton(int af, const char *src, void *dst) int inet_pton_xp(int af, const char *src, void *dst)
{ // 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
struct sockaddr_storage ss; struct sockaddr_storage ss;
int size = sizeof (ss); int size = sizeof (ss);
@ -45,7 +46,7 @@ int inet_pton(int af, const char *src, void *dst)
} }
} }
return 0; return 0;
}*/ }
#else /* !WIN32 => UNIX */ #else /* !WIN32 => UNIX */
#include <sys/types.h> #include <sys/types.h>
#include <ifaddrs.h> #include <ifaddrs.h>
@ -58,204 +59,221 @@ namespace util
namespace net namespace net
{ {
#ifdef WIN32 #ifdef WIN32
int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback) bool IsWindowsXPorLater() {
{ OSVERSIONINFO osvi;
ULONG outBufLen = 0;
PIP_ADAPTER_ADDRESSES pAddresses = nullptr; ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
GetVersionEx(&osvi);
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen) if (osvi.dwMajorVersion <= 5)
== ERROR_BUFFER_OVERFLOW) { return true;
FREE(pAddresses); else
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen); return false;
} }
DWORD dwRetVal = GetAdaptersAddresses( int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback)
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen {
); ULONG outBufLen = 0;
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
if(dwRetVal != NO_ERROR) { PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed"); PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
FREE(pAddresses);
return fallback;
} if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW) {
pCurrAddresses = pAddresses; FREE(pAddresses);
while(pCurrAddresses) { pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress; }
pUnicast = pCurrAddresses->FirstUnicastAddress; DWORD dwRetVal = GetAdaptersAddresses(
if(pUnicast == nullptr) { AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv4 address, this is not supported"); );
}
for(int i = 0; pUnicast != nullptr; ++i) { if(dwRetVal != NO_ERROR) {
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr; LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr; FREE(pAddresses);
if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr) { return fallback;
auto result = pAddresses->Mtu; }
FREE(pAddresses);
return result; pCurrAddresses = pAddresses;
} while(pCurrAddresses) {
pUnicast = pUnicast->Next; PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
}
pCurrAddresses = pCurrAddresses->Next; pUnicast = pCurrAddresses->FirstUnicastAddress;
} if(pUnicast == nullptr) {
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv4 address, this is not supported");
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv4 addresses found"); }
FREE(pAddresses); for(int i = 0; pUnicast != nullptr; ++i) {
return fallback; 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) {
int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback) auto result = pAddresses->Mtu;
{ FREE(pAddresses);
ULONG outBufLen = 0; return result;
PIP_ADAPTER_ADDRESSES pAddresses = nullptr; }
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr; pUnicast = pUnicast->Next;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr; }
pCurrAddresses = pCurrAddresses->Next;
if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen) }
== ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses); LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv4 addresses found");
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen); FREE(pAddresses);
} return fallback;
}
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback)
); {
ULONG outBufLen = 0;
if(dwRetVal != NO_ERROR) { PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed"); PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
FREE(pAddresses); PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
return fallback;
} if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW) {
bool found_address = false; FREE(pAddresses);
pCurrAddresses = pAddresses; pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
while(pCurrAddresses) { }
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress; DWORD dwRetVal = GetAdaptersAddresses(
if(pUnicast == nullptr) { AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv6 address, this is not supported"); );
}
for(int i = 0; pUnicast != nullptr; ++i) { if(dwRetVal != NO_ERROR) {
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr; LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr; FREE(pAddresses);
return fallback;
for (int j = 0; j != 8; ++j) { }
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j]) {
break; bool found_address = false;
} else { pCurrAddresses = pAddresses;
found_address = true; while(pCurrAddresses) {
} PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
} if (found_address) { pUnicast = pCurrAddresses->FirstUnicastAddress;
auto result = pAddresses->Mtu; if(pUnicast == nullptr) {
FREE(pAddresses); LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv6 address, this is not supported");
pAddresses = nullptr; }
return result; for(int i = 0; pUnicast != nullptr; ++i) {
} LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
pUnicast = pUnicast->Next; sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
}
for (int j = 0; j != 8; ++j) {
pCurrAddresses = pCurrAddresses->Next; if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j]) {
} break;
} else {
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv6 addresses found"); found_address = true;
FREE(pAddresses); }
return fallback; } if (found_address) {
} auto result = pAddresses->Mtu;
FREE(pAddresses);
int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback) 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)
{
#ifdef UNICODE #ifdef UNICODE
string localAddress_temporary = localAddress.to_string(); string localAddress_temporary = localAddress.to_string();
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end()); wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
#else #else
std::string localAddressUniversal = localAddress.to_string(); std::string localAddressUniversal = localAddress.to_string();
#endif #endif
if(localAddress.is_v4()) { if (IsWindowsXPorLater())
sockaddr_in inputAddress; #define inet_pton inet_pton_xp
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
return GetMTUWindowsIpv4(inputAddress, fallback); if(localAddress.is_v4()) {
} else if(localAddress.is_v6()) { sockaddr_in inputAddress;
sockaddr_in6 inputAddress; inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr)); return GetMTUWindowsIpv4(inputAddress, fallback);
return GetMTUWindowsIpv6(inputAddress, fallback); } else if(localAddress.is_v6()) {
} else { sockaddr_in6 inputAddress;
LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported"); inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
return fallback; return GetMTUWindowsIpv6(inputAddress, fallback);
} } else {
LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");
} return fallback;
}
}
#else // assume unix #else // assume unix
int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback) int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
{ {
ifaddrs* ifaddr, *ifa = nullptr; ifaddrs* ifaddr, *ifa = nullptr;
if(getifaddrs(&ifaddr) == -1) if(getifaddrs(&ifaddr) == -1)
{ {
LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno)); LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
return fallback; return fallback;
} }
int family = 0; int family = 0;
// look for interface matching local address // look for interface matching local address
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
{ {
if(!ifa->ifa_addr) if(!ifa->ifa_addr)
continue; continue;
family = ifa->ifa_addr->sa_family; family = ifa->ifa_addr->sa_family;
if(family == AF_INET && localAddress.is_v4()) 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 = fallback; int mtu = fallback;
if(ifa && family) if(ifa && family)
{ // interface found? { // 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, "NetIface: Failed to run ioctl: ", strerror(errno)); LogPrint (eLogError, "NetIface: Failed to run ioctl: ", strerror(errno));
close(fd); close(fd);
} }
else else
LogPrint(eLogError, "NetIface: Failed to create datagram socket"); LogPrint(eLogError, "NetIface: Failed to create datagram socket");
} }
else else
LogPrint(eLogWarning, "NetIface: interface for local address", localAddress.to_string(), " not found"); LogPrint(eLogWarning, "NetIface: interface for local address", localAddress.to_string(), " not found");
freeifaddrs(ifaddr); freeifaddrs(ifaddr);
return mtu; return mtu;
} }
#endif // WIN32 #endif // WIN32
int GetMTU(const boost::asio::ip::address& localAddress) int GetMTU(const boost::asio::ip::address& localAddress)
{ {
const int fallback = 576; // fallback MTU const int fallback = 576; // fallback MTU
#ifdef WIN32 #ifdef WIN32
return GetMTUWindows(localAddress, fallback); return GetMTUWindows(localAddress, fallback);
#else #else
return GetMTUUnix(localAddress, fallback); return GetMTUUnix(localAddress, fallback);
#endif #endif
return fallback; return fallback;
} }
const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6) const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6)
{ {

Loading…
Cancel
Save