IPAddress: function to clear interface part of ipv6
GitOrigin-RevId: ff738e215e458bc9e7257e9a89c318fb01cdb345
This commit is contained in:
parent
76056bebf8
commit
9b98451c43
@ -521,6 +521,17 @@ Status IPAddress::init_peer_address(const SocketFd &socket_fd) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void IPAddress::clear_ipv6_interface() {
|
||||
if (!is_valid() || get_address_family() != AF_INET6) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto *begin = ipv6_addr_.sin6_addr.s6_addr;
|
||||
static_assert(sizeof(ipv6_addr_.sin6_addr.s6_addr) == 16, "expected 16 bytes buffer for ipv6");
|
||||
static_assert(sizeof(*begin) == 1, "expected array of bytes");
|
||||
std::fill(begin + 8, begin + 16, 0);
|
||||
}
|
||||
|
||||
string IPAddress::ipv4_to_str(uint32 ipv4) {
|
||||
ipv4 = ntohl(ipv4);
|
||||
return ::td::get_ip_str(AF_INET, &ipv4).str();
|
||||
|
@ -65,6 +65,8 @@ class IPAddress {
|
||||
Status init_socket_address(const SocketFd &socket_fd) TD_WARN_UNUSED_RESULT;
|
||||
Status init_peer_address(const SocketFd &socket_fd) TD_WARN_UNUSED_RESULT;
|
||||
|
||||
void clear_ipv6_interface();
|
||||
|
||||
friend bool operator==(const IPAddress &a, const IPAddress &b);
|
||||
friend bool operator<(const IPAddress &a, const IPAddress &b);
|
||||
|
||||
|
@ -719,6 +719,14 @@ TEST(Misc, IPAddress_is_reserved) {
|
||||
test_is_reserved("255.255.255.255", true);
|
||||
}
|
||||
|
||||
TEST(Misc, ipv6_clear) {
|
||||
IPAddress ip_address;
|
||||
ip_address.init_host_port("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 123).ensure();
|
||||
ASSERT_EQ("2001:db8:85a3::8a2e:370:7334", ip_address.get_ip_str());
|
||||
ip_address.clear_ipv6_interface();
|
||||
ASSERT_EQ("2001:db8:85a3::", ip_address.get_ip_str());
|
||||
}
|
||||
|
||||
static void test_split(Slice str, std::pair<Slice, Slice> expected) {
|
||||
ASSERT_EQ(expected, td::split(str));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user