mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add IPv4Address::netmask_from_cidr
This commit is contained in:
parent
b85666b3d2
commit
36676a1604
Notes:
sideshowbarker
2024-07-17 10:10:18 +09:00
Author: https://github.com/sppmacd Commit: https://github.com/SerenityOS/serenity/commit/36676a1604 Pull-request: https://github.com/SerenityOS/serenity/pull/13259 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/IdanHo
2 changed files with 21 additions and 1 deletions
|
@ -119,6 +119,13 @@ public:
|
|||
return IPv4Address(a, b, c, d);
|
||||
}
|
||||
|
||||
static constexpr IPv4Address netmask_from_cidr(int cidr)
|
||||
{
|
||||
VERIFY(cidr >= 0 && cidr <= 32);
|
||||
u32 value = 0xffffffffull << (32 - cidr);
|
||||
return IPv4Address((value & 0xff000000) >> 24, (value & 0xff0000) >> 16, (value & 0xff00) >> 8, (value & 0xff));
|
||||
}
|
||||
|
||||
constexpr in_addr_t to_in_addr_t() const { return m_data; }
|
||||
constexpr u32 to_u32() const { return m_data; }
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -151,3 +151,16 @@ TEST_CASE(should_compare)
|
|||
EXPECT(addr_a != addr_b);
|
||||
EXPECT(addr_a == addr_a);
|
||||
}
|
||||
|
||||
TEST_CASE(netmask_from_cidr)
|
||||
{
|
||||
EXPECT(IPv4Address::netmask_from_cidr(24) == IPv4Address(255, 255, 255, 0));
|
||||
|
||||
EXPECT(IPv4Address::netmask_from_cidr(0) == IPv4Address(0, 0, 0, 0));
|
||||
EXPECT(IPv4Address::netmask_from_cidr(32) == IPv4Address(255, 255, 255, 255));
|
||||
|
||||
EXPECT(IPv4Address::netmask_from_cidr(28) == IPv4Address(255, 255, 255, 240));
|
||||
EXPECT(IPv4Address::netmask_from_cidr(22) == IPv4Address(255, 255, 252, 0));
|
||||
EXPECT(IPv4Address::netmask_from_cidr(14) == IPv4Address(255, 252, 0, 0));
|
||||
EXPECT(IPv4Address::netmask_from_cidr(6) == IPv4Address(252, 0, 0, 0));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue