Make ipv4_to_str/ipv6_to_str safe to use.

GitOrigin-RevId: 622c8f03c8791545922d3ae5f596f0a9699b91a9
This commit is contained in:
levlam 2020-05-16 16:33:32 +03:00
parent bf963ccadf
commit 54f9c77a20
2 changed files with 9 additions and 8 deletions

View File

@ -494,14 +494,14 @@ Status IPAddress::init_peer_address(const SocketFd &socket_fd) {
return Status::OK();
}
CSlice IPAddress::ipv4_to_str(uint32 ipv4) {
string IPAddress::ipv4_to_str(uint32 ipv4) {
ipv4 = ntohl(ipv4);
return ::td::get_ip_str(AF_INET, &ipv4);
return ::td::get_ip_str(AF_INET, &ipv4).str();
}
CSlice IPAddress::ipv6_to_str(Slice ipv6) {
string IPAddress::ipv6_to_str(Slice ipv6) {
CHECK(ipv6.size() == 16);
return ::td::get_ip_str(AF_INET6, ipv6.ubegin());
return ::td::get_ip_str(AF_INET6, ipv6.ubegin()).str();
}
Slice IPAddress::get_ip_str() const {
@ -599,10 +599,10 @@ StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address) {
if (!address.is_valid()) {
return builder << "[invalid]";
}
if (address.get_address_family() == AF_INET) {
if (address.is_ipv4()) {
return builder << "[" << address.get_ip_str() << ":" << address.get_port() << "]";
} else {
CHECK(address.get_address_family() == AF_INET6);
CHECK(address.is_ipv6());
return builder << "[[" << address.get_ip_str() << "]:" << address.get_port() << "]";
}
}

View File

@ -42,6 +42,9 @@ class IPAddress {
string get_ipv6() const;
Slice get_ip_str() const;
static string ipv4_to_str(uint32 ipv4);
static string ipv6_to_str(Slice ipv6);
IPAddress get_any_addr() const;
static Result<IPAddress> get_ipv4_address(CSlice host);
@ -63,8 +66,6 @@ class IPAddress {
const sockaddr *get_sockaddr() const;
size_t get_sockaddr_len() const;
int get_address_family() const;
static CSlice ipv4_to_str(uint32 ipv4);
static CSlice ipv6_to_str(Slice ipv6);
Status init_sockaddr(sockaddr *addr);
Status init_sockaddr(sockaddr *addr, socklen_t len) TD_WARN_UNUSED_RESULT;