2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-18 12:28:48 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-25 16:11:15 +03:00
|
|
|
#include <AK/Endian.h>
|
2022-02-15 20:35:32 +03:00
|
|
|
#include <AK/Format.h>
|
2019-07-08 12:38:58 +03:00
|
|
|
#include <AK/Optional.h>
|
2023-09-21 13:22:05 +03:00
|
|
|
#include <AK/SipHash.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Vector.h>
|
2019-07-08 12:38:58 +03:00
|
|
|
|
2022-02-15 20:35:32 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
# include <AK/Error.h>
|
2023-02-24 21:10:59 +03:00
|
|
|
# include <Kernel/Library/KString.h>
|
2022-02-15 20:35:32 +03:00
|
|
|
#else
|
2022-12-04 21:02:33 +03:00
|
|
|
# include <AK/DeprecatedString.h>
|
2023-04-03 19:50:11 +03:00
|
|
|
# include <AK/String.h>
|
2022-02-15 20:35:32 +03:00
|
|
|
#endif
|
|
|
|
|
2019-06-18 12:28:48 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2020-12-31 00:44:54 +03:00
|
|
|
class [[gnu::packed]] IPv4Address {
|
2020-10-28 19:50:53 +03:00
|
|
|
enum class SubnetClass : int {
|
|
|
|
A = 0,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
D
|
|
|
|
};
|
|
|
|
|
2019-06-18 12:28:48 +03:00
|
|
|
public:
|
2020-10-28 19:50:53 +03:00
|
|
|
using in_addr_t = u32;
|
|
|
|
|
|
|
|
constexpr IPv4Address() = default;
|
|
|
|
|
|
|
|
constexpr IPv4Address(u32 a, u32 b, u32 c, u32 d)
|
2019-06-18 12:28:48 +03:00
|
|
|
{
|
2020-10-28 19:50:53 +03:00
|
|
|
m_data = (d << 24) | (c << 16) | (b << 8) | a;
|
2019-06-18 12:28:48 +03:00
|
|
|
}
|
2020-10-28 19:50:53 +03:00
|
|
|
|
|
|
|
constexpr IPv4Address(const u8 data[4])
|
2019-06-18 12:28:48 +03:00
|
|
|
{
|
2020-10-28 19:50:53 +03:00
|
|
|
m_data = (u32(data[3]) << 24) | (u32(data[2]) << 16) | (u32(data[1]) << 8) | u32(data[0]);
|
2019-06-18 12:28:48 +03:00
|
|
|
}
|
2020-10-28 19:50:53 +03:00
|
|
|
|
|
|
|
constexpr IPv4Address(NetworkOrdered<u32> address)
|
|
|
|
: m_data(address)
|
2019-06-18 12:40:39 +03:00
|
|
|
{
|
|
|
|
}
|
2019-06-18 12:28:48 +03:00
|
|
|
|
2020-10-28 19:50:53 +03:00
|
|
|
constexpr u8 operator[](int i) const
|
2019-06-18 12:28:48 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(i >= 0 && i < 4);
|
2020-10-28 19:50:53 +03:00
|
|
|
return octet(SubnetClass(i));
|
2019-06-18 12:28:48 +03:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:35:32 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
|
|
|
|
{
|
|
|
|
return Kernel::KString::formatted("{}.{}.{}.{}",
|
|
|
|
octet(SubnetClass::A),
|
|
|
|
octet(SubnetClass::B),
|
|
|
|
octet(SubnetClass::C),
|
|
|
|
octet(SubnetClass::D));
|
|
|
|
}
|
|
|
|
#else
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string() const
|
2019-06-18 12:28:48 +03:00
|
|
|
{
|
2022-12-04 21:02:33 +03:00
|
|
|
return DeprecatedString::formatted("{}.{}.{}.{}",
|
2020-10-28 19:50:53 +03:00
|
|
|
octet(SubnetClass::A),
|
|
|
|
octet(SubnetClass::B),
|
|
|
|
octet(SubnetClass::C),
|
|
|
|
octet(SubnetClass::D));
|
2019-06-18 12:28:48 +03:00
|
|
|
}
|
|
|
|
|
2022-12-06 04:12:49 +03:00
|
|
|
DeprecatedString to_deprecated_string_reversed() const
|
2021-06-07 15:15:06 +03:00
|
|
|
{
|
2022-12-04 21:02:33 +03:00
|
|
|
return DeprecatedString::formatted("{}.{}.{}.{}",
|
2021-06-07 15:15:06 +03:00
|
|
|
octet(SubnetClass::D),
|
|
|
|
octet(SubnetClass::C),
|
|
|
|
octet(SubnetClass::B),
|
|
|
|
octet(SubnetClass::A));
|
|
|
|
}
|
2023-04-03 19:50:11 +03:00
|
|
|
|
|
|
|
ErrorOr<String> to_string() const
|
|
|
|
{
|
|
|
|
return String::formatted("{}.{}.{}.{}",
|
|
|
|
octet(SubnetClass::A),
|
|
|
|
octet(SubnetClass::B),
|
|
|
|
octet(SubnetClass::C),
|
|
|
|
octet(SubnetClass::D));
|
|
|
|
}
|
2022-02-15 20:35:32 +03:00
|
|
|
#endif
|
2021-06-07 15:15:06 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static Optional<IPv4Address> from_string(StringView string)
|
2019-07-08 12:38:58 +03:00
|
|
|
{
|
|
|
|
if (string.is_null())
|
|
|
|
return {};
|
2020-08-13 00:57:43 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const parts = string.split_view('.');
|
2020-10-28 19:50:53 +03:00
|
|
|
|
|
|
|
u32 a {};
|
|
|
|
u32 b {};
|
|
|
|
u32 c {};
|
|
|
|
u32 d {};
|
2020-08-13 00:57:43 +03:00
|
|
|
|
|
|
|
if (parts.size() == 1) {
|
2020-08-14 16:05:35 +03:00
|
|
|
d = parts[0].to_uint().value_or(256);
|
2020-08-13 00:57:43 +03:00
|
|
|
} else if (parts.size() == 2) {
|
2020-08-13 22:12:59 +03:00
|
|
|
a = parts[0].to_uint().value_or(256);
|
|
|
|
d = parts[1].to_uint().value_or(256);
|
2020-08-13 00:57:43 +03:00
|
|
|
} else if (parts.size() == 3) {
|
|
|
|
a = parts[0].to_uint().value_or(256);
|
|
|
|
b = parts[1].to_uint().value_or(256);
|
|
|
|
d = parts[2].to_uint().value_or(256);
|
|
|
|
} else if (parts.size() == 4) {
|
|
|
|
a = parts[0].to_uint().value_or(256);
|
|
|
|
b = parts[1].to_uint().value_or(256);
|
|
|
|
c = parts[2].to_uint().value_or(256);
|
|
|
|
d = parts[3].to_uint().value_or(256);
|
|
|
|
} else {
|
2019-07-08 12:38:58 +03:00
|
|
|
return {};
|
2020-08-13 00:57:43 +03:00
|
|
|
}
|
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
if (a > 255 || b > 255 || c > 255 || d > 255)
|
2019-07-08 12:38:58 +03:00
|
|
|
return {};
|
2020-10-28 19:50:53 +03:00
|
|
|
return IPv4Address(a, b, c, d);
|
2019-07-08 12:38:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-26 12:35:52 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:50:53 +03:00
|
|
|
constexpr in_addr_t to_in_addr_t() const { return m_data; }
|
|
|
|
constexpr u32 to_u32() const { return m_data; }
|
2019-07-08 12:38:58 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator==(IPv4Address const& other) const = default;
|
|
|
|
constexpr bool operator!=(IPv4Address const& other) const = default;
|
2019-06-18 12:28:48 +03:00
|
|
|
|
2020-10-28 19:50:53 +03:00
|
|
|
constexpr bool is_zero() const
|
2019-08-28 03:48:35 +03:00
|
|
|
{
|
2020-10-28 19:50:53 +03:00
|
|
|
return m_data == 0u;
|
2019-08-28 03:48:35 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 12:28:48 +03:00
|
|
|
private:
|
2020-10-28 19:50:53 +03:00
|
|
|
constexpr u32 octet(const SubnetClass subnet) const
|
|
|
|
{
|
|
|
|
constexpr auto bits_per_byte = 8;
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const bits_to_shift = bits_per_byte * int(subnet);
|
2020-10-28 19:50:53 +03:00
|
|
|
return (m_data >> bits_to_shift) & 0x0000'00FF;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 m_data {};
|
2019-06-18 12:28:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(IPv4Address) == 4);
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 20:14:03 +03:00
|
|
|
struct Traits<IPv4Address> : public GenericTraits<IPv4Address> {
|
2023-09-21 13:22:05 +03:00
|
|
|
static unsigned hash(IPv4Address const& address) { return secure_sip_hash(static_cast<u64>(address.to_u32())); }
|
2019-06-18 12:28:48 +03:00
|
|
|
};
|
|
|
|
|
2022-02-15 20:35:32 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
template<>
|
2023-01-13 05:21:33 +03:00
|
|
|
struct Formatter<IPv4Address> : Formatter<StringView> {
|
2022-02-15 20:35:32 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
|
|
|
|
{
|
2023-01-13 05:21:33 +03:00
|
|
|
return Formatter<StringView>::format(builder, TRY(value.to_string())->view());
|
2022-02-15 20:35:32 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
2021-01-15 23:46:23 +03:00
|
|
|
template<>
|
2023-01-13 05:21:33 +03:00
|
|
|
struct Formatter<IPv4Address> : Formatter<StringView> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
|
2021-01-15 23:46:23 +03:00
|
|
|
{
|
2023-01-13 05:21:33 +03:00
|
|
|
return Formatter<StringView>::format(builder, value.to_deprecated_string());
|
2021-01-15 23:46:23 +03:00
|
|
|
}
|
|
|
|
};
|
2022-02-15 20:35:32 +03:00
|
|
|
#endif
|
2021-01-15 23:46:23 +03:00
|
|
|
|
2019-06-18 12:28:48 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-18 12:28:48 +03:00
|
|
|
using AK::IPv4Address;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|