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