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>
|
2019-07-08 09:38:58 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/String.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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
String to_string() const
|
|
|
|
{
|
2020-10-28 16:50:53 +00:00
|
|
|
return String::formatted("{}.{}.{}.{}",
|
|
|
|
octet(SubnetClass::A),
|
|
|
|
octet(SubnetClass::B),
|
|
|
|
octet(SubnetClass::C),
|
|
|
|
octet(SubnetClass::D));
|
2019-06-18 09:28:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 09:38:58 +00:00
|
|
|
static Optional<IPv4Address> from_string(const StringView& string)
|
|
|
|
{
|
|
|
|
if (string.is_null())
|
|
|
|
return {};
|
2020-08-12 21:57:43 +00:00
|
|
|
|
2020-10-28 16:50:53 +00:00
|
|
|
const auto parts = string.split_view('.');
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-28 16:50:53 +00:00
|
|
|
constexpr bool operator==(const IPv4Address& other) const = default;
|
|
|
|
constexpr bool operator!=(const IPv4Address& 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
|
|
|
|
{
|
|
|
|
NetworkOrdered<u32> address(m_data);
|
|
|
|
constexpr auto bits_per_byte = 8;
|
|
|
|
const auto bits_to_shift = bits_per_byte * int(subnet);
|
|
|
|
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> {
|
2020-10-28 16:50:53 +00:00
|
|
|
static constexpr unsigned hash(const IPv4Address& address) { return int_hash(address.to_u32()); }
|
2019-06-18 09:28:48 +00:00
|
|
|
};
|
|
|
|
|
2021-01-15 20:46:23 +00:00
|
|
|
template<>
|
|
|
|
struct Formatter<IPv4Address> : Formatter<String> {
|
|
|
|
void format(FormatBuilder& builder, IPv4Address value)
|
|
|
|
{
|
|
|
|
return Formatter<String>::format(builder, value.to_string());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 09:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::IPv4Address;
|