Skip to content
Snippets Groups Projects
Unverified Commit 393a403f authored by Sébastien Blin's avatar Sébastien Blin
Browse files

proxy_client: fix address split

parent 62e4655d
No related branches found
No related tags found
No related merge requests found
......@@ -54,6 +54,12 @@ void erase_if(std::map<Key, Item>& map, const Condition& condition)
}
}
/**
* Split "[host]:port" or "host:port" to pair<"host", "port">.
*/
OPENDHT_PUBLIC std::pair<std::string, std::string>
splitPort(const std::string& s);
class OPENDHT_PUBLIC DhtException : public std::runtime_error {
public:
DhtException(const std::string &str = "") :
......
......@@ -23,6 +23,7 @@
#include "dhtrunner.h"
#include "op_cache.h"
#include "utils.h"
#include <restbed>
#include <json/json.h>
......@@ -452,15 +453,8 @@ DhtProxyClient::getProxyInfos()
}
// A node can have a Ipv4 and a Ipv6. So, we need to retrieve all public ips
std::string host, service;
auto serviceMarker = serverHost_.find(':');
if (serviceMarker != std::string::npos) {
host = serverHost_.substr(0, serviceMarker - 1);
service = serverHost_.substr(serviceMarker + 1);
} else {
host = serverHost_;
}
auto resolved_proxies = SockAddr::resolve(host, service);
auto hostAndService = splitPort(serverHost_);
auto resolved_proxies = SockAddr::resolve(hostAndService.first, hostAndService.second);
auto serverHost = serverHost_;
// Try to contact the proxy and set the status to connected when done.
......@@ -551,10 +545,8 @@ SockAddr
DhtProxyClient::parsePublicAddress(const Json::Value& val)
{
auto public_ip = val.asString();
auto endIp = public_ip.find_last_of(':');
auto marker = (public_ip.size() > 0 && public_ip[0] == '[') ? 1 : 0;
std::string address = public_ip.substr(marker, endIp - marker * 2);
auto sa = SockAddr::resolve(address);
auto hostAndService = splitPort(public_ip);
auto sa = SockAddr::resolve(hostAndService.first);
if (sa.empty()) return {};
return sa.front().getMappedIPv4();
}
......
......@@ -29,6 +29,26 @@ namespace dht {
static constexpr std::array<uint8_t, 12> MAPPED_IPV4_PREFIX {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}};
std::pair<std::string, std::string>
splitPort(const std::string& s) {
if (s.empty())
return {};
if (s[0] == '[') {
std::size_t closure = s.find_first_of(']');
std::size_t found = s.find_last_of(':');
if (closure == std::string::npos)
return {s, ""};
if (found == std::string::npos or found < closure)
return {s.substr(1,closure-1), ""};
return {s.substr(1,closure-1), s.substr(found+1)};
}
std::size_t found = s.find_last_of(':');
std::size_t first = s.find_first_of(':');
if (found == std::string::npos or found != first)
return {s, ""};
return {s.substr(0,found), s.substr(found+1)};
}
std::vector<SockAddr>
SockAddr::resolve(const std::string& host, const std::string& service)
{
......
......@@ -45,29 +45,6 @@
#include <sstream>
#include <fstream>
/**
* Split "[host]:port" or "host:port" to pair<"host", "port">.
*/
std::pair<std::string, std::string>
splitPort(const std::string& s) {
if (s.empty())
return {};
if (s[0] == '[') {
std::size_t closure = s.find_first_of(']');
std::size_t found = s.find_last_of(':');
if (closure == std::string::npos)
return {s, ""};
if (found == std::string::npos or found < closure)
return {s.substr(1,closure-1), ""};
return {s.substr(1,closure-1), s.substr(found+1)};
}
std::size_t found = s.find_last_of(':');
std::size_t first = s.find_first_of(':');
if (found == std::string::npos or found != first)
return {s, ""};
return {s.substr(0,found), s.substr(found+1)};
}
/*
* The mapString shall have the following format:
*
......@@ -181,7 +158,7 @@ parseArgs(int argc, char **argv) {
params.network = strtoul(optarg, nullptr, 0);
break;
case 'b':
params.bootstrap = splitPort((optarg[0] == '=') ? optarg+1 : optarg);
params.bootstrap = dht::splitPort((optarg[0] == '=') ? optarg+1 : optarg);
if (not params.bootstrap.first.empty() and params.bootstrap.second.empty()) {
params.bootstrap.second = std::to_string(DHT_DEFAULT_PORT);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment