diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 0955b4ec..133c0d35 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -64,6 +64,7 @@ #include "td/utils/PathView.h" #include "td/utils/tl_helpers.h" +#include #include namespace td { diff --git a/tdnet/td/net/GetHostByNameActor.cpp b/tdnet/td/net/GetHostByNameActor.cpp index 6982f107..a76877c3 100644 --- a/tdnet/td/net/GetHostByNameActor.cpp +++ b/tdnet/td/net/GetHostByNameActor.cpp @@ -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(); diff --git a/tdutils/td/utils/port/IPAddress.cpp b/tdutils/td/utils/port/IPAddress.cpp index 933eef8f..b5d5d1be 100644 --- a/tdutils/td/utils/port/IPAddress.cpp +++ b/tdutils/td/utils/port/IPAddress.cpp @@ -345,6 +345,32 @@ Status IPAddress::init_ipv4_port(CSlice ipv4, int port) { return Status::OK(); } +Result 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::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); } diff --git a/tdutils/td/utils/port/IPAddress.h b/tdutils/td/utils/port/IPAddress.h index 087fb357..2353e46a 100644 --- a/tdutils/td/utils/port/IPAddress.h +++ b/tdutils/td/utils/port/IPAddress.h @@ -44,6 +44,9 @@ class IPAddress { IPAddress get_any_addr() const; + static Result get_ipv4_address(CSlice host); + static Result 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; diff --git a/test/mtproto.cpp b/test/mtproto.cpp index 48850535..f491214b 100644 --- a/test/mtproto.cpp +++ b/test/mtproto.cpp @@ -63,9 +63,21 @@ TEST(Mtproto, GetHostByNameActor) { send_closure(actor_id, &GetHostByNameActor::run, host, 443, prefer_ipv6, std::move(promise)); }; - std::vector 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 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::Native}, vector{GetHostByNameActor::ResolverType::Google}, vector{GetHostByNameActor::ResolverType::Google,