i2pd/util.cpp

491 lines
15 KiB
C++
Raw Normal View History

2015-02-07 17:34:25 +00:00
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
#include <fstream>
#include <set>
#include <boost/asio.hpp>
2014-08-08 11:32:55 +00:00
#include <boost/lexical_cast.hpp>
2016-01-20 00:00:00 +00:00
#include "Config.h"
2014-01-30 03:28:07 +00:00
#include "util.h"
#include "Log.h"
2014-01-30 03:28:07 +00:00
2015-12-28 00:00:00 +00:00
#ifdef WIN32
2015-03-15 15:51:07 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <shlobj.h>
2015-03-15 15:51:07 +00:00
#ifdef _MSC_VER
2015-03-15 15:51:07 +00:00
#pragma comment(lib, "IPHLPAPI.lib")
2015-12-28 00:00:00 +00:00
#endif // _MSC_VER
2015-03-15 15:51:07 +00:00
#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));
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;
}
2015-12-28 00:00:00 +00:00
#else /* !WIN32 => UNIX */
#include <sys/types.h>
#include <ifaddrs.h>
#endif
2014-01-30 03:28:07 +00:00
namespace i2p
{
namespace util
{
namespace http
{
2015-02-20 17:21:33 +00:00
std::string GetHttpContent (std::istream& response)
{
std::string version, statusMessage;
response >> version; // HTTP version
int status;
response >> status; // status
std::getline (response, statusMessage);
if (status == 200) // OK
{
bool isChunked = false;
std::string header;
while (!response.eof () && header != "\r")
{
std::getline(response, header);
auto colon = header.find (':');
if (colon != std::string::npos)
{
std::string field = header.substr (0, colon);
std::transform(field.begin(), field.end(), field.begin(), ::tolower);
2015-02-20 17:21:33 +00:00
if (field == i2p::util::http::TRANSFER_ENCODING)
isChunked = (header.find ("chunked", colon + 1) != std::string::npos);
}
}
std::stringstream ss;
if (isChunked)
MergeChunkedResponse (response, ss);
else
ss << response.rdbuf();
return ss.str();
}
else
{
2015-12-18 12:07:13 +00:00
LogPrint (eLogError, "HTTPClient: error, server responds ", status);
2015-02-20 17:21:33 +00:00
return "";
}
}
2014-12-23 02:20:39 +00:00
void MergeChunkedResponse (std::istream& response, std::ostream& merged)
{
while (!response.eof ())
{
std::string hexLen;
2016-03-24 15:18:11 +00:00
size_t len;
2014-12-23 02:20:39 +00:00
std::getline (response, hexLen);
std::istringstream iss (hexLen);
iss >> std::hex >> len;
2016-03-24 15:18:11 +00:00
if (!len || len > 10000000L) // 10M
{
LogPrint (eLogError, "Unexpected chunk length ", len);
break;
}
2014-12-23 02:20:39 +00:00
char * buf = new char[len];
response.read (buf, len);
merged.write (buf, len);
delete[] buf;
std::getline (response, hexLen); // read \r\n after chunk
}
}
url::url(const std::string& url_s)
{
2014-04-17 20:22:54 +00:00
portstr_ = "80";
port_ = 80;
user_ = "";
pass_ = "";
parse(url_s);
}
2014-04-06 20:18:55 +00:00
// code for parser tests
//{
2014-04-13 21:07:54 +00:00
// i2p::util::http::url u_0("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq");
2014-04-06 20:18:55 +00:00
// i2p::util::http::url u_1("http://user:password@site.com:8080/asdasd?qqqqqqqqqqqqq");
// i2p::util::http::url u_2("http://user:password@site.com/asdasd?qqqqqqqqqqqqqq");
// i2p::util::http::url u_3("http://user:@site.com/asdasd?qqqqqqqqqqqqq");
// i2p::util::http::url u_4("http://user@site.com/asdasd?qqqqqqqqqqqq");
// i2p::util::http::url u_5("http://@site.com:800/asdasd?qqqqqqqqqqqq");
// i2p::util::http::url u_6("http://@site.com:err_port/asdasd?qqqqqqqqqqqq");
2014-04-13 21:07:54 +00:00
// i2p::util::http::url u_7("http://user:password@site.com:err_port/asdasd?qqqqqqqqqqqq");
2014-04-06 20:18:55 +00:00
//}
void url::parse(const std::string& url_s)
{
const std::string prot_end("://");
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));
transform(url_s.begin(), prot_i,
back_inserter(protocol_),
std::ptr_fun<int,int>(tolower)); // protocol is icase
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));
transform(prot_i, path_i,
back_inserter(host_),
std::ptr_fun<int,int>(tolower)); // host is icase
2014-04-06 20:18:55 +00:00
// parse user/password
auto user_pass_i = find(host_.begin(), host_.end(), '@');
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(), ':');
if (pass_i != user_pass.end())
{
user_ = std::string(user_pass.begin(), pass_i);
pass_ = std::string(pass_i + 1, user_pass.end());
}
else
user_ = user_pass;
host_.assign(user_pass_i + 1, host_.end());
}
// parse port
2014-04-06 19:38:21 +00:00
auto port_i = find(host_.begin(), host_.end(), ':');
if (port_i != host_.end())
{
2014-04-06 20:18:55 +00:00
portstr_ = std::string(port_i + 1, host_.end());
host_.assign(host_.begin(), port_i);
2014-04-06 20:18:55 +00:00
try{
2014-08-08 11:32:55 +00:00
port_ = boost::lexical_cast<decltype(port_)>(portstr_);
2014-04-06 20:18:55 +00:00
}
catch (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());
}
2015-02-07 17:34:25 +00:00
std::string urlDecode(const std::string& data)
{
std::string res(data);
for (size_t pos = res.find('%'); pos != std::string::npos; pos = res.find('%',pos+1))
{
char c = strtol(res.substr(pos+1,2).c_str(), NULL, 16);
res.replace(pos,3,1,c);
}
return res;
}
2014-10-31 18:34:08 +00:00
}
2014-10-31 18:17:52 +00:00
namespace net
{
2015-12-28 15:52:02 +00:00
#ifdef WIN32
2015-11-20 15:36:04 +00:00
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);
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
if(dwRetVal != NO_ERROR) {
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
2015-11-20 15:36:04 +00:00
FREE(pAddresses);
return fallback;
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
pCurrAddresses = pAddresses;
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if(pUnicast == nullptr) {
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv4 address, this is not supported");
2015-11-20 15:36:04 +00:00
}
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;
}
2015-03-15 15:51:07 +00:00
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv4 addresses found");
2015-11-20 15:36:04 +00:00
FREE(pAddresses);
return fallback;
}
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);
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
DWORD dwRetVal = GetAdaptersAddresses(
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
);
if(dwRetVal != NO_ERROR) {
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
2015-11-20 15:36:04 +00:00
FREE(pAddresses);
return fallback;
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
bool found_address = false;
pCurrAddresses = pAddresses;
while(pCurrAddresses) {
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
pUnicast = pCurrAddresses->FirstUnicastAddress;
if(pUnicast == nullptr) {
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv6 address, this is not supported");
2015-11-20 15:36:04 +00: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;
}
} if (found_address) {
auto result = pAddresses->Mtu;
FREE(pAddresses);
pAddresses = nullptr;
return result;
}
pUnicast = pUnicast->Next;
}
pCurrAddresses = pCurrAddresses->Next;
}
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv6 addresses found");
2015-11-20 15:36:04 +00:00
FREE(pAddresses);
return fallback;
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
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
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
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 {
2015-12-18 12:07:13 +00:00
LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");
2015-11-20 15:36:04 +00:00
return fallback;
}
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
}
2015-12-28 15:52:02 +00:00
#else // assume unix
int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
{
ifaddrs* ifaddr, *ifa = nullptr;
if(getifaddrs(&ifaddr) == -1)
{
LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
return fallback;
}
int family = 0;
// 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;
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
}
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
}
}
int mtu = fallback;
if(ifa && family)
{ // interface found?
int fd = socket(family, SOCK_DGRAM, 0);
if(fd > 0)
{
ifreq ifr;
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, "NetIface: Failed to run ioctl: ", strerror(errno));
close(fd);
}
else
LogPrint(eLogError, "NetIface: Failed to create datagram socket");
}
else
LogPrint(eLogWarning, "NetIface: interface for local address", localAddress.to_string(), " not found");
freeifaddrs(ifaddr);
return mtu;
}
2015-11-20 15:36:04 +00:00
#endif // WIN32
2015-03-15 15:51:07 +00:00
2015-11-20 15:36:04 +00:00
int GetMTU(const boost::asio::ip::address& localAddress)
{
const int fallback = 576; // fallback MTU
2015-03-15 15:51:07 +00:00
2015-12-28 00:00:00 +00:00
#ifdef WIN32
2015-11-20 15:36:04 +00:00
return GetMTUWindows(localAddress, fallback);
2015-12-28 00:00:00 +00:00
#else
return GetMTUUnix(localAddress, fallback);
#endif
2015-11-20 15:36:04 +00:00
return fallback;
2016-06-29 15:06:51 +00:00
}
const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6)
{
#ifdef WIN32
LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32");
2016-07-01 12:31:27 +00:00
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);
ifaddrs * addrs = nullptr;
if(getifaddrs(&addrs) == 0)
{
// got ifaddrs
ifaddrs * cur = addrs;
while(cur)
{
std::string cur_ifname(cur->ifa_name);
2016-06-29 15:57:44 +00:00
if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af)
2016-06-29 15:06:51 +00:00
{
// match
2016-06-29 20:10:43 +00:00
char * addr = new char[INET6_ADDRSTRLEN];
bzero(addr, 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);
2016-06-29 19:52:32 +00:00
std::string cur_ifaddr(addr);
2016-06-29 15:06:51 +00:00
return boost::asio::ip::address::from_string(cur_ifaddr);
}
cur = cur->ifa_next;
}
}
if(addrs) freeifaddrs(addrs);
std::string fallback;
if(ipv6) {
fallback = "::";
LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname);
} else {
fallback = "127.0.0.1";
LogPrint(eLogWarning, "NetIface: cannot find ipv4 address for interface ", ifname);
}
return boost::asio::ip::address::from_string(fallback);
#endif
}
2014-10-31 18:34:08 +00:00
}
namespace config
{
std::string GetHost(bool ipv4, bool ipv6)
{
std::string host;
if(ipv6)
host = "::";
else if(ipv4)
host = "127.0.0.1";
bool nat; i2p::config::GetOption("nat", nat);
if (nat)
{
if (!i2p::config::IsDefault("host"))
i2p::config::GetOption("host", host);
}
else
{
// we are not behind nat
std::string ifname; i2p::config::GetOption("ifname", ifname);
if (ifname.size())
host = i2p::util::net::GetInterfaceAddress(ifname, ipv6).to_string(); // bind to interface, we have no NAT so set external address too
}
return host;
}
} // config
2014-10-31 18:34:08 +00:00
} // util
} // i2p