Support numeric hosts in GoogleDnsResolver.

GitOrigin-RevId: 6768a90ad7f98d83f507051564fb1646d3834aad
This commit is contained in:
levlam 2019-02-04 06:32:10 +03:00
parent ee082cd58d
commit ca92472b69
5 changed files with 56 additions and 3 deletions

View File

@ -64,6 +64,7 @@
#include "td/utils/PathView.h"
#include "td/utils/tl_helpers.h"
#include <algorithm>
#include <utility>
namespace td {

View File

@ -31,6 +31,17 @@ class GoogleDnsResolver : public Actor {
double begin_time_ = 0;
void start_up() override {
auto r_address = IPAddress::get_ipv4_address(host_);
if (r_address.is_ok()) {
promise_.set_value(r_address.move_as_ok());
return stop();
}
r_address = IPAddress::get_ipv6_address(host_);
if (r_address.is_ok()) {
promise_.set_value(r_address.move_as_ok());
return stop();
}
const int timeout = 10;
const int ttl = 3;
begin_time_ = Time::now();

View File

@ -345,6 +345,32 @@ Status IPAddress::init_ipv4_port(CSlice ipv4, int port) {
return Status::OK();
}
Result<IPAddress> IPAddress::get_ipv4_address(CSlice host) {
// sometimes inet_addr allows much more valid IPv4 hosts than inet_pton,
// like 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001
auto ipv4_numeric_addr = inet_addr(host.c_str());
if (ipv4_numeric_addr == INADDR_NONE) {
return Status::Error("Host is not valid IPv4 address");
}
host = ::td::get_ip_str(AF_INET, &ipv4_numeric_addr);
IPAddress result;
auto status = result.init_ipv4_port(host, 1);
if (status.is_error()) {
return std::move(status);
}
return std::move(result);
}
Result<IPAddress> IPAddress::get_ipv6_address(CSlice host) {
IPAddress result;
auto status = result.init_ipv6_port(host, 1);
if (status.is_error()) {
return std::move(status);
}
return std::move(result);
}
Status IPAddress::init_host_port(CSlice host, int port, bool prefer_ipv6) {
return init_host_port(host, PSLICE() << port, prefer_ipv6);
}

View File

@ -44,6 +44,9 @@ class IPAddress {
IPAddress get_any_addr() const;
static Result<IPAddress> get_ipv4_address(CSlice host);
static Result<IPAddress> get_ipv6_address(CSlice host);
Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT;
Status init_ipv6_as_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT;
Status init_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT;

View File

@ -63,9 +63,21 @@ TEST(Mtproto, GetHostByNameActor) {
send_closure(actor_id, &GetHostByNameActor::run, host, 443, prefer_ipv6, std::move(promise));
};
std::vector<std::string> hosts = {
"127.0.0.2", "1.1.1.1", "localhost", "web.telegram.org", "web.telegram.org.", "москва.рф", "", "%",
" ", "a", "\x80", "127.0.0.1.", "0x12.0x34.0x56.0x78"};
std::vector<std::string> hosts = {"127.0.0.2",
"1.1.1.1",
"localhost",
"web.telegram.org",
"web.telegram.org.",
"москва.рф",
"",
"%",
" ",
"a",
"\x80",
"127.0.0.1.",
"0x12.0x34.0x56.0x78",
"0x7f.001",
"2001:0db8:85a3:0000:0000:8a2e:0370:7334"};
for (auto types : {vector<GetHostByNameActor::ResolverType>{GetHostByNameActor::ResolverType::Native},
vector<GetHostByNameActor::ResolverType>{GetHostByNameActor::ResolverType::Google},
vector<GetHostByNameActor::ResolverType>{GetHostByNameActor::ResolverType::Google,