Skip to content
Snippets Groups Projects
Unverified Commit d9f6731d authored by Adrien Béraud's avatar Adrien Béraud Committed by GitHub
Browse files

Merge pull request #265 from AmarOk1412/fixSplit

proxy_client: fix address split
parents 62e4655d 393a403f
Branches
Tags
No related merge requests found
...@@ -54,6 +54,12 @@ void erase_if(std::map<Key, Item>& map, const Condition& condition) ...@@ -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 { class OPENDHT_PUBLIC DhtException : public std::runtime_error {
public: public:
DhtException(const std::string &str = "") : DhtException(const std::string &str = "") :
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "dhtrunner.h" #include "dhtrunner.h"
#include "op_cache.h" #include "op_cache.h"
#include "utils.h"
#include <restbed> #include <restbed>
#include <json/json.h> #include <json/json.h>
...@@ -452,15 +453,8 @@ DhtProxyClient::getProxyInfos() ...@@ -452,15 +453,8 @@ DhtProxyClient::getProxyInfos()
} }
// A node can have a Ipv4 and a Ipv6. So, we need to retrieve all public ips // A node can have a Ipv4 and a Ipv6. So, we need to retrieve all public ips
std::string host, service; auto hostAndService = splitPort(serverHost_);
auto serviceMarker = serverHost_.find(':'); auto resolved_proxies = SockAddr::resolve(hostAndService.first, hostAndService.second);
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 serverHost = serverHost_; auto serverHost = serverHost_;
// Try to contact the proxy and set the status to connected when done. // Try to contact the proxy and set the status to connected when done.
...@@ -551,10 +545,8 @@ SockAddr ...@@ -551,10 +545,8 @@ SockAddr
DhtProxyClient::parsePublicAddress(const Json::Value& val) DhtProxyClient::parsePublicAddress(const Json::Value& val)
{ {
auto public_ip = val.asString(); auto public_ip = val.asString();
auto endIp = public_ip.find_last_of(':'); auto hostAndService = splitPort(public_ip);
auto marker = (public_ip.size() > 0 && public_ip[0] == '[') ? 1 : 0; auto sa = SockAddr::resolve(hostAndService.first);
std::string address = public_ip.substr(marker, endIp - marker * 2);
auto sa = SockAddr::resolve(address);
if (sa.empty()) return {}; if (sa.empty()) return {};
return sa.front().getMappedIPv4(); return sa.front().getMappedIPv4();
} }
......
...@@ -29,6 +29,26 @@ namespace dht { ...@@ -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}}; 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> std::vector<SockAddr>
SockAddr::resolve(const std::string& host, const std::string& service) SockAddr::resolve(const std::string& host, const std::string& service)
{ {
......
...@@ -45,29 +45,6 @@ ...@@ -45,29 +45,6 @@
#include <sstream> #include <sstream>
#include <fstream> #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: * The mapString shall have the following format:
* *
...@@ -181,7 +158,7 @@ parseArgs(int argc, char **argv) { ...@@ -181,7 +158,7 @@ parseArgs(int argc, char **argv) {
params.network = strtoul(optarg, nullptr, 0); params.network = strtoul(optarg, nullptr, 0);
break; break;
case 'b': 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()) { if (not params.bootstrap.first.empty() and params.bootstrap.second.empty()) {
params.bootstrap.second = std::to_string(DHT_DEFAULT_PORT); 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