Add IPAddress::get_ip_host and use it whenever appropriate.

GitOrigin-RevId: 7254ebd036463fe2c8b6262269cbee843b320421
This commit is contained in:
levlam 2020-05-16 23:12:52 +03:00
parent 9fe0d4bbd9
commit 5b18a56e03
4 changed files with 24 additions and 10 deletions

View File

@ -661,9 +661,9 @@ Result<mtproto::TransportType> ConnectionCreator::get_transport_type(const Proxy
if (!proxy.user().empty() || !proxy.password().empty()) { if (!proxy.user().empty() || !proxy.password().empty()) {
proxy_authorization = "|basic " + base64_encode(PSLICE() << proxy.user() << ':' << proxy.password()); proxy_authorization = "|basic " + base64_encode(PSLICE() << proxy.user() << ':' << proxy.password());
} }
return mtproto::TransportType{ return mtproto::TransportType{mtproto::TransportType::Http, 0,
mtproto::TransportType::Http, 0, mtproto::ProxySecret::from_raw(
mtproto::ProxySecret::from_raw(PSTRING() << info.option->get_ip_address().get_ip_str() << proxy_authorization)}; PSTRING() << info.option->get_ip_address().get_ip_host() << proxy_authorization)};
} }
if (info.use_http) { if (info.use_http) {

View File

@ -47,7 +47,7 @@ Status HttpProxy::wait_connect_response() {
size_t len = min(sizeof(buf), it.size()); size_t len = min(sizeof(buf), it.size());
it.advance(len, MutableSlice{buf, sizeof(buf)}); it.advance(len, MutableSlice{buf, sizeof(buf)});
VLOG(proxy) << "Failed to connect: " << format::escaped(Slice(buf, len)); VLOG(proxy) << "Failed to connect: " << format::escaped(Slice(buf, len));
return Status::Error(PSLICE() << "Failed to connect to " << ip_address_.get_ip_str() << ':' return Status::Error(PSLICE() << "Failed to connect to " << ip_address_.get_ip_host() << ':'
<< ip_address_.get_port()); << ip_address_.get_port());
} }

View File

@ -545,6 +545,22 @@ CSlice IPAddress::get_ip_str() const {
} }
} }
string IPAddress::get_ip_host() const {
if (!is_valid()) {
return "0.0.0.0";
}
switch (get_address_family()) {
case AF_INET6:
return PSTRING() << '[' << ::td::get_ip_str(AF_INET6, &ipv6_addr_.sin6_addr) << ']';
case AF_INET:
return ::td::get_ip_str(AF_INET, &ipv4_addr_.sin_addr).str();
default:
UNREACHABLE();
return string();
}
}
int IPAddress::get_port() const { int IPAddress::get_port() const {
if (!is_valid()) { if (!is_valid()) {
return 0; return 0;
@ -624,12 +640,7 @@ StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address) {
if (!address.is_valid()) { if (!address.is_valid()) {
return builder << "[invalid]"; return builder << "[invalid]";
} }
if (address.is_ipv4()) { return builder << "[" << address.get_ip_host() << ":" << address.get_port() << "]";
return builder << "[" << address.get_ip_str() << ":" << address.get_port() << "]";
} else {
CHECK(address.is_ipv6());
return builder << "[[" << address.get_ip_str() << "]:" << address.get_port() << "]";
}
} }
} // namespace td } // namespace td

View File

@ -44,6 +44,9 @@ class IPAddress {
// returns result in a static thread-local buffer, which may be overwritten by any subsequent method call // returns result in a static thread-local buffer, which may be overwritten by any subsequent method call
CSlice get_ip_str() const; CSlice get_ip_str() const;
// returns IP address as a host, i.e. IPv4 or [IPv6]
string get_ip_host() const;
static string ipv4_to_str(uint32 ipv4); static string ipv4_to_str(uint32 ipv4);
static string ipv6_to_str(Slice ipv6); static string ipv6_to_str(Slice ipv6);