i2pd/core/util/util.cpp

677 lines
23 KiB
C++
Raw Normal View History

2015-02-07 20:34:25 +03:00
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
#include <fstream>
#include <set>
#include <boost/asio.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
2014-08-08 15:32:55 +04:00
#include <boost/lexical_cast.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/algorithm/string.hpp>
2014-01-30 07:28:07 +04:00
#include "util.h"
#include "Log.h"
2014-01-30 07:28:07 +04:00
2015-09-05 11:32:54 +03:00
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
2014-10-31 21:17:52 +03:00
#include <sys/types.h>
#include <ifaddrs.h>
2015-03-15 18:51:07 +03:00
#elif defined(WIN32)
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <shlobj.h>
2015-03-15 18:51:07 +03:00
#pragma comment(lib, "IPHLPAPI.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int inet_pton(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 */
struct sockaddr_storage ss;
int size = sizeof (ss);
char src_copy[INET6_ADDRSTRLEN + 1];
ZeroMemory (&ss, sizeof (ss));
strncpy_s (src_copy, src, INET6_ADDRSTRLEN + 1);
src_copy[INET6_ADDRSTRLEN] = 0;
if (WSAStringToAddress (src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
{
switch (af)
{
2015-07-19 16:51:02 +03:00
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;
2015-03-15 18:51:07 +03:00
}
#endif
2015-07-19 18:52:28 +03:00
namespace i2p {
namespace util {
2014-01-30 07:28:07 +04:00
2015-07-19 18:52:28 +03:00
namespace config {
std::map<std::string, std::string> mapArgs;
std::map<std::string, std::vector<std::string> > mapMultiArgs;
2015-07-19 18:52:28 +03:00
void OptionParser(int argc, const char* const argv[])
{
mapArgs.clear();
mapMultiArgs.clear();
2015-07-19 18:52:28 +03:00
for(int i = 1; i < argc; ++i) {
std::string strKey (argv[i]);
std::string strValue;
size_t has_data = strKey.find('=');
2015-07-19 18:52:28 +03:00
if(has_data != std::string::npos) {
strValue = strKey.substr(has_data+1);
strKey = strKey.substr(0, has_data);
}
#ifdef WIN32
boost::to_lower(strKey);
2015-07-19 18:52:28 +03:00
if(boost::algorithm::starts_with(strKey, "/"))
strKey = "-" + strKey.substr(1);
#endif
2015-07-19 18:52:28 +03:00
if(strKey[0] != '-')
break;
mapArgs[strKey] = strValue;
mapMultiArgs[strKey].push_back(strValue);
}
2015-07-19 18:52:28 +03:00
for(auto& entry : mapArgs) {
std::string name = entry.first;
// interpret --foo as -foo (as long as both are not set)
2015-07-19 18:52:28 +03:00
if (name.find("--") == 0) {
std::string singleDash(name.begin()+1, name.end());
if (mapArgs.count(singleDash) == 0)
mapArgs[singleDash] = entry.second;
name = singleDash;
}
}
}
const char* GetCharArg(const std::string& strArg, const std::string& nDefault)
{
2015-07-19 18:52:28 +03:00
if(mapArgs.count(strArg))
return mapArgs[strArg].c_str();
return nDefault.c_str();
}
std::string GetArg(const std::string& strArg, const std::string& strDefault)
{
2015-07-19 18:52:28 +03:00
if(mapArgs.count(strArg))
return mapArgs[strArg];
return strDefault;
}
int GetArg(const std::string& strArg, int nDefault)
{
2015-07-19 18:52:28 +03:00
if(mapArgs.count(strArg))
return stoi(mapArgs[strArg]);
return nDefault;
}
2015-07-19 18:52:28 +03:00
2014-01-30 07:28:07 +04:00
}
namespace filesystem
2014-01-30 07:28:07 +04:00
{
2015-07-19 18:52:28 +03:00
std::string appName("i2pd");
2015-07-19 18:52:28 +03:00
void SetAppName(const std::string& name)
{
appName = name;
}
2015-07-19 18:52:28 +03:00
std::string GetAppName()
{
return appName;
}
2015-07-19 18:52:28 +03:00
const boost::filesystem::path& GetDataDir()
{
static boost::filesystem::path path;
// TODO: datadir parameter is useless because GetDataDir is called before OptionParser
// and mapArgs is not initialized yet
/*if (i2p::util::config::mapArgs.count("-datadir"))
path = boost::filesystem::system_complete(i2p::util::config::mapArgs["-datadir"]);
else */
2015-07-19 18:52:28 +03:00
path = GetDefaultDataDir();
2015-07-19 18:52:28 +03:00
if(!boost::filesystem::exists(path)) {
// Create data directory
2015-07-19 18:52:28 +03:00
if(!boost::filesystem::create_directory(path)) {
LogPrint("Failed to create data directory!");
path = "";
return path;
}
}
2015-07-19 18:52:28 +03:00
if(!boost::filesystem::is_directory(path))
path = GetDefaultDataDir();
return path;
}
2015-07-19 18:52:28 +03:00
std::string GetFullPath(const std::string& filename)
{
2015-07-19 18:52:28 +03:00
std::string fullPath = GetDataDir().string();
2014-03-13 22:22:32 +04:00
#ifndef _WIN32
2015-07-19 18:52:28 +03:00
fullPath.append("/");
2014-03-13 22:22:32 +04:00
#else
2015-07-19 18:52:28 +03:00
fullPath.append("\\");
2014-03-13 22:22:32 +04:00
#endif
2015-07-19 18:52:28 +03:00
fullPath.append(filename);
return fullPath;
}
boost::filesystem::path GetConfigFile()
{
boost::filesystem::path pathConfigFile(i2p::util::config::GetArg("-conf", "i2p.conf"));
2015-07-19 18:52:28 +03:00
if(!pathConfigFile.is_complete())
pathConfigFile = GetDataDir() / pathConfigFile;
return pathConfigFile;
}
2015-08-12 00:19:25 +03:00
boost::filesystem::path GetTunnelsConfigFile()
{
boost::filesystem::path pathTunnelsConfigFile(i2p::util::config::GetArg("-tunnelscfg", "tunnels.cfg"));
if(!pathTunnelsConfigFile.is_complete())
pathTunnelsConfigFile = GetDataDir() / pathTunnelsConfigFile;
return pathTunnelsConfigFile;
}
boost::filesystem::path GetDefaultDataDir()
{
// Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd
// Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd
// Mac: ~/Library/Application Support/i2pd
// Unix: ~/.i2pd or /var/lib/i2pd is system=1
2015-07-19 18:52:28 +03:00
#ifdef WIN32
// Windows
char localAppData[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_APPDATA, 0, NULL, localAppData);
return boost::filesystem::path(std::string(localAppData) + "\\" + appName);
#else
2015-07-19 18:52:28 +03:00
if(i2p::util::config::GetArg("-service", 0)) // use system folder
return boost::filesystem::path(std::string ("/var/lib/") + appName);
boost::filesystem::path pathRet;
char* pszHome = getenv("HOME");
2015-07-19 18:52:28 +03:00
if(pszHome == NULL || strlen(pszHome) == 0)
pathRet = boost::filesystem::path("/");
else
pathRet = boost::filesystem::path(pszHome);
#ifdef MAC_OSX
// Mac
pathRet /= "Library/Application Support";
boost::filesystem::create_directory(pathRet);
return pathRet / appName;
#else
// Unix
return pathRet / (std::string (".") + appName);
#endif
#endif
}
2015-07-19 18:52:28 +03:00
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet)
{
boost::filesystem::ifstream streamConfig(GetConfigFile());
if(!streamConfig.good())
return; // No i2pd.conf file is OK
std::set<std::string> setOptions;
setOptions.insert("*");
for(boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end;
it != end; ++it) {
// Don't overwrite existing settings so command line settings override i2pd.conf
std::string strKey = std::string("-") + it->string_key;
if(mapSettingsRet.count(strKey) == 0) {
mapSettingsRet[strKey] = it->value[0];
}
mapMultiSettingsRet[strKey].push_back(it->value[0]);
}
}
boost::filesystem::path GetCertificatesDir()
{
return GetDataDir () / "certificates";
2015-07-19 18:52:28 +03:00
}
2014-01-30 07:28:07 +04:00
}
namespace http
{
std::string httpRequest(const std::string& address)
{
2015-07-19 18:52:28 +03:00
try {
i2p::util::http::url u(address);
boost::asio::ip::tcp::iostream site;
// please don't uncomment following line because it's not compatible with boost 1.46
// 1.46 is default boost for Ubuntu 12.04 LTS
//site.expires_from_now (boost::posix_time::seconds(30));
2015-07-19 18:52:28 +03:00
if(u.port_ == 80)
site.connect(u.host_, "http");
2015-07-19 18:52:28 +03:00
else {
std::stringstream ss; ss << u.port_;
site.connect(u.host_, ss.str());
}
2015-07-19 18:52:28 +03:00
if(site) {
// User-Agent is needed to get the server list routerInfo files.
site << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_
2015-07-19 18:52:28 +03:00
<< "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n"
<< "Connection: close\r\n\r\n";
// read response and extract content
2015-07-19 18:52:28 +03:00
return GetHttpContent(site);
} else {
LogPrint("Can't connect to ", address);
return "";
}
2015-07-19 18:52:28 +03:00
} catch(const std::exception& ex) {
LogPrint("Failed to download ", address, " : ", ex.what());
return "";
}
}
std::string GetHttpContent (std::istream& response)
{
std::string version, statusMessage;
response >> version; // HTTP version
int status;
response >> status; // status
std::getline (response, statusMessage);
2015-07-19 18:52:28 +03:00
if(status == 200) { // OK
bool isChunked = false;
std::string header;
2015-07-19 18:52:28 +03:00
while(!response.eof() && header != "\r") {
std::getline(response, header);
auto colon = header.find (':');
2015-07-19 18:52:28 +03:00
if(colon != std::string::npos) {
std::string field = header.substr (0, colon);
2015-07-19 18:52:28 +03:00
if(field == i2p::util::http::TRANSFER_ENCODING)
isChunked = (header.find("chunked", colon + 1) != std::string::npos);
}
}
std::stringstream ss;
2015-07-19 18:52:28 +03:00
if(isChunked)
MergeChunkedResponse(response, ss);
else
ss << response.rdbuf();
2015-07-19 18:52:28 +03:00
return ss.str();
2015-07-19 18:52:28 +03:00
} else {
LogPrint("HTTP response ", status);
return "";
}
}
2015-07-19 18:52:28 +03:00
void MergeChunkedResponse(std::istream& response, std::ostream& merged)
{
2015-07-19 18:52:28 +03:00
while(!response.eof()) {
std::string hexLen;
int len;
2015-07-19 18:52:28 +03:00
std::getline(response, hexLen);
std::istringstream iss(hexLen);
iss >> std::hex >> len;
2015-07-19 18:52:28 +03:00
if(!len)
break;
char* buf = new char[len];
response.read(buf, len);
merged.write(buf, len);
delete[] buf;
2015-07-19 18:52:28 +03:00
std::getline(response, hexLen); // read \r\n after chunk
}
}
int httpRequestViaI2pProxy(const std::string& address, std::string &content)
{
content = "";
2015-07-19 18:52:28 +03:00
try {
boost::asio::ip::tcp::iostream site;
// please don't uncomment following line because it's not compatible with boost 1.46
// 1.46 is default boost for Ubuntu 12.04 LTS
//site.expires_from_now (boost::posix_time::seconds(30));
{
std::stringstream ss; ss << i2p::util::config::GetArg("-httpproxyport", 4446);
site.connect("127.0.0.1", ss.str());
}
2015-07-19 18:52:28 +03:00
if(site) {
i2p::util::http::url u(address);
std::stringstream ss;
ss << "GET " << address << " HTTP/1.0" << std::endl;
ss << "Host: " << u.host_ << std::endl;
ss << "Accept: */*" << std::endl;
ss << "User - Agent: Wget / 1.11.4" << std::endl;
ss << "Connection: close" << std::endl;
ss << std::endl;
site << ss.str();
// read response
std::string version, statusMessage;
site >> version; // HTTP version
int status;
site >> status; // status
std::getline(site, statusMessage);
2015-07-19 18:52:28 +03:00
if(status == 200) { // OK
std::string header;
2015-07-19 18:52:28 +03:00
while(std::getline(site, header) && header != "\r"){}
std::stringstream ss;
ss << site.rdbuf();
content = ss.str();
return status;
2015-07-19 18:52:28 +03:00
} else {
LogPrint("HTTP response ", status);
return status;
}
2015-07-19 18:52:28 +03:00
} else {
LogPrint("Can't connect to proxy");
return 408;
}
2015-07-19 18:52:28 +03:00
} catch (std::exception& ex) {
LogPrint("Failed to download ", address, " : ", ex.what());
return 408;
}
}
url::url(const std::string& url_s)
{
portstr_ = "80";
port_ = 80;
user_ = "";
pass_ = "";
parse(url_s);
}
void url::parse(const std::string& url_s)
{
const std::string prot_end("://");
2015-07-19 18:52:28 +03:00
std::string::const_iterator prot_i = search(
url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end()
);
protocol_.reserve(distance(url_s.begin(), prot_i));
2015-07-19 18:52:28 +03:00
// Make portocol lowercase
transform(
url_s.begin(), prot_i, back_inserter(protocol_), std::ptr_fun<int, int>(std::tolower)
);
if(prot_i == url_s.end())
return;
advance(prot_i, prot_end.length());
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/');
host_.reserve(distance(prot_i, path_i));
2015-07-19 18:52:28 +03:00
// Make host lowerase
transform(prot_i, path_i, back_inserter(host_), std::ptr_fun<int, int>(std::tolower));
// parse user/password
auto user_pass_i = find(host_.begin(), host_.end(), '@');
2015-07-19 18:52:28 +03:00
if(user_pass_i != host_.end()) {
std::string user_pass = std::string(host_.begin(), user_pass_i);
auto pass_i = find(user_pass.begin(), user_pass.end(), ':');
2015-07-19 18:52:28 +03:00
if (pass_i != user_pass.end()) {
user_ = std::string(user_pass.begin(), pass_i);
pass_ = std::string(pass_i + 1, user_pass.end());
2015-07-19 18:52:28 +03:00
} else
user_ = user_pass;
host_.assign(user_pass_i + 1, host_.end());
}
// parse port
auto port_i = find(host_.begin(), host_.end(), ':');
2015-07-19 18:52:28 +03:00
if(port_i != host_.end()) {
portstr_ = std::string(port_i + 1, host_.end());
host_.assign(host_.begin(), port_i);
2015-07-19 18:52:28 +03:00
try {
port_ = boost::lexical_cast<decltype(port_)>(portstr_);
2015-07-19 18:52:28 +03:00
} catch(const std::exception& e) {
port_ = 80;
}
}
std::string::const_iterator query_i = find(path_i, url_s.end(), '?');
path_.assign(path_i, query_i);
if( query_i != url_s.end() )
++query_i;
query_.assign(query_i, url_s.end());
}
std::string urlDecode(const std::string& data)
{
std::string res(data);
2015-07-19 18:52:28 +03:00
for(size_t pos = res.find('%'); pos != std::string::npos; pos = res.find('%', pos + 1)) {
const char c = strtol(res.substr(pos + 1, 2).c_str(), NULL, 16);
res.replace(pos, 3, 1, c);
}
return res;
}
2014-10-31 21:34:08 +03:00
}
2015-07-19 18:52:28 +03:00
namespace net {
2015-09-05 11:32:54 +03:00
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
2015-07-19 18:52:28 +03:00
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 fallback;
2014-10-31 21:17:52 +03:00
}
2015-07-19 18:52:28 +03:00
int family = 0;
2015-07-19 18:52:28 +03:00
// look for interface matching local address
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if(!ifa->ifa_addr)
continue;
family = ifa->ifa_addr->sa_family;
2015-07-19 18:52:28 +03:00
if(family == AF_INET && localAddress.is_v4()) {
sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
break; // address matches
2015-07-19 18:52:28 +03:00
} else if(family == AF_INET6 && localAddress.is_v6()) {
sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
break; // address matches
}
}
2015-07-19 18:52:28 +03:00
int mtu = fallback;
if(ifa && family) { // interface found?
int fd = socket(family, SOCK_DGRAM, 0);
if(fd > 0) {
ifreq ifr;
2015-07-19 18:52:28 +03:00
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, "Failed to run ioctl");
2015-07-19 18:52:28 +03:00
close(fd);
} else
LogPrint(eLogError, "Failed to create datagram socket");
} else {
LogPrint(
eLogWarning, "Interface for local address",
localAddress.to_string(), " not found"
);
}
2015-07-19 18:52:28 +03:00
freeifaddrs(ifaddr);
2015-03-15 18:51:07 +03:00
2015-07-19 18:52:28 +03:00
return mtu;
}
2015-03-15 18:51:07 +03:00
2015-07-19 18:52:28 +03:00
#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;
2015-03-15 18:51:07 +03:00
2015-07-19 18:52:28 +03:00
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
== ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
}
2015-07-19 18:52:28 +03:00
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
2015-07-19 18:52:28 +03:00
if(dwRetVal != NO_ERROR) {
LogPrint(
eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
);
FREE(pAddresses);
return fallback;
}
2015-07-19 18:52:28 +03:00
pCurrAddresses = pAddresses;
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
2015-07-19 18:52:28 +03:00
pUnicast = pCurrAddresses->FirstUnicastAddress;
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) {
result = pAddresses->Mtu;
FREE(pAddresses);
return result;
}
pUnicast = pUnicast->Next;
}
pCurrAddresses = pCurrAddresses->Next;
}
2015-07-19 18:52:28 +03:00
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv4 addresses found");
FREE(pAddresses);
return fallback;
}
int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback)
2015-07-19 18:52:28 +03: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);
}
2015-07-19 18:52:28 +03:00
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;
}
2015-07-19 18:52:28 +03:00
bool found_address = false;
pCurrAddresses = pAddresses;
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if(pUnicast == nullptr) {
LogPrint(
eLogError,
"GetMTU() has failed: not a unicast ipv6 address, this is not supported"
);
}
2015-07-19 18:52:28 +03:00
for(int i = 0; pUnicast != nullptr; ++i) {
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]) {
break;
} else {
found_address = true;
}
2015-07-19 18:52:28 +03:00
} if (found_address) {
result = pAddresses->Mtu;
FREE(pAddresses);
pAddresses = nullptr;
return result;
}
2015-07-19 18:52:28 +03:00
pUnicast = pUnicast->Next;
}
2015-07-19 18:52:28 +03:00
pCurrAddresses = pCurrAddresses->Next;
}
2015-07-19 18:52:28 +03:00
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv6 addresses found");
FREE(pAddresses);
return fallback;
}
int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
{
#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;
}
2015-07-19 18:52:28 +03:00
}
#endif // WIN32
int GetMTU(const boost::asio::ip::address& localAddress)
{
const int fallback = 576; // fallback MTU
2015-09-05 11:32:54 +03:00
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__OpenBSD__)
2015-07-19 18:52:28 +03:00
return GetMTUUnix(localAddress, fallback);
#elif defined(WIN32)
return GetMTUWindows(localAddress, fallback);
#endif
return fallback;
}
2014-10-31 21:34:08 +03:00
}
2014-10-31 21:34:08 +03:00
} // util
} // i2p