Skip to content

Commit

Permalink
Add IPAddress::get_ip_host and use it whenever appropriate.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 7254ebd036463fe2c8b6262269cbee843b320421
  • Loading branch information
levlam committed May 16, 2020
1 parent 9fe0d4b commit 5b18a56
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 3 additions & 3 deletions td/telegram/net/ConnectionCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,9 @@ Result<mtproto::TransportType> ConnectionCreator::get_transport_type(const Proxy
if (!proxy.user().empty() || !proxy.password().empty()) {
proxy_authorization = "|basic " + base64_encode(PSLICE() << proxy.user() << ':' << proxy.password());
}
return mtproto::TransportType{
mtproto::TransportType::Http, 0,
mtproto::ProxySecret::from_raw(PSTRING() << info.option->get_ip_address().get_ip_str() << proxy_authorization)};
return mtproto::TransportType{mtproto::TransportType::Http, 0,
mtproto::ProxySecret::from_raw(
PSTRING() << info.option->get_ip_address().get_ip_host() << proxy_authorization)};
}

if (info.use_http) {
Expand Down
2 changes: 1 addition & 1 deletion tdnet/td/net/HttpProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Status HttpProxy::wait_connect_response() {
size_t len = min(sizeof(buf), it.size());
it.advance(len, MutableSlice{buf, sizeof(buf)});
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());
}

Expand Down
23 changes: 17 additions & 6 deletions tdutils/td/utils/port/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
if (!is_valid()) {
return 0;
Expand Down Expand Up @@ -624,12 +640,7 @@ StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address) {
if (!address.is_valid()) {
return builder << "[invalid]";
}
if (address.is_ipv4()) {
return builder << "[" << address.get_ip_str() << ":" << address.get_port() << "]";
} else {
CHECK(address.is_ipv6());
return builder << "[[" << address.get_ip_str() << "]:" << address.get_port() << "]";
}
return builder << "[" << address.get_ip_host() << ":" << address.get_port() << "]";
}

} // namespace td
3 changes: 3 additions & 0 deletions tdutils/td/utils/port/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class IPAddress {
// returns result in a static thread-local buffer, which may be overwritten by any subsequent method call
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 ipv6_to_str(Slice ipv6);

Expand Down

0 comments on commit 5b18a56

Please sign in to comment.