From eb8fba8b28856d96f58d082b3c6f82a59d497c7b Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 16 May 2020 23:54:40 +0300 Subject: [PATCH] Fix init_host_port. GitOrigin-RevId: 30abb3480d906ebd7ea50a7feecf072be6e36641 --- tdutils/td/utils/port/IPAddress.cpp | 9 +++++---- test/mtproto.cpp | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tdutils/td/utils/port/IPAddress.cpp b/tdutils/td/utils/port/IPAddress.cpp index 275184c6..4f2a323d 100644 --- a/tdutils/td/utils/port/IPAddress.cpp +++ b/tdutils/td/utils/port/IPAddress.cpp @@ -308,7 +308,7 @@ void IPAddress::init_ipv6_any() { Status IPAddress::init_ipv6_port(CSlice ipv6, int port) { is_valid_ = false; if (port <= 0 || port >= (1 << 16)) { - return Status::Error(PSLICE() << "Invalid [port=" << port << "]"); + return Status::Error(PSLICE() << "Invalid [IPv6 address port=" << port << "]"); } string ipv6_plain; if (ipv6.size() > 2 && ipv6[0] == '[' && ipv6.back() == ']') { @@ -335,7 +335,7 @@ Status IPAddress::init_ipv6_as_ipv4_port(CSlice ipv4, int port) { Status IPAddress::init_ipv4_port(CSlice ipv4, int port) { is_valid_ = false; if (port <= 0 || port >= (1 << 16)) { - return Status::Error(PSLICE() << "Invalid [port=" << port << "]"); + return Status::Error(PSLICE() << "Invalid [IPv4 address port=" << port << "]"); } std::memset(&ipv4_addr_, 0, sizeof(ipv4_addr_)); ipv4_addr_.sin_family = AF_INET; @@ -390,7 +390,7 @@ Result IPAddress::get_ipv6_address(CSlice host) { Status IPAddress::init_host_port(CSlice host, int port, bool prefer_ipv6) { if (host.size() > 2 && host[0] == '[' && host.back() == ']') { - return init_ipv6_port(host, port); + return init_ipv6_port(host, port == 0 ? 1 : port); } return init_host_port(host, PSLICE() << port, prefer_ipv6); @@ -410,7 +410,8 @@ Status IPAddress::init_host_port(CSlice host, CSlice port, bool prefer_ipv6) { host = ascii_host; // assign string to CSlice if (host[0] == '[' && host.back() == ']') { - return init_ipv6_port(host, to_integer(port)); + auto port_int = to_integer(port); + return init_ipv6_port(host, port_int == 0 ? 1 : port_int); } // some getaddrinfo implementations use inet_pton instead of inet_aton and support only decimal-dotted IPv4 form, diff --git a/test/mtproto.cpp b/test/mtproto.cpp index 4289165b..0e9648a9 100644 --- a/test/mtproto.cpp +++ b/test/mtproto.cpp @@ -91,7 +91,8 @@ TEST(Mtproto, GetHostByNameActor) { "0x12.0x34.0x56.0x78", "0x7f.001", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", - "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"}; + "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", + "[[2001:0db8:85a3:0000:0000:8a2e:0370:7334]]"}; for (auto types : {vector{GetHostByNameActor::ResolverType::Native}, vector{GetHostByNameActor::ResolverType::Google}, vector{GetHostByNameActor::ResolverType::Google, @@ -107,7 +108,7 @@ TEST(Mtproto, GetHostByNameActor) { for (auto host : hosts) { for (auto prefer_ipv6 : {false, true}) { - bool allow_ok = host.size() > 2; + bool allow_ok = host.size() > 2 && host[1] != '['; bool allow_both = host == "127.0.0.1." || host == "localhost" || (host == "москва.рф" && prefer_ipv6); bool allow_error = !allow_ok || allow_both; run(actor_id, host, prefer_ipv6, allow_ok, allow_error);