mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-29 16:21:29 +00:00
AK: Move IPv4Address from Kernel/Net/ to AK/ since it's quite useful.
This commit is contained in:
parent
9f7c11710f
commit
d9a48b5916
Notes:
sideshowbarker
2024-07-19 13:33:04 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d9a48b59163
58
AK/IPv4Address.h
Normal file
58
AK/IPv4Address.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
class [[gnu::packed]] IPv4Address
|
||||
{
|
||||
public:
|
||||
IPv4Address() {}
|
||||
IPv4Address(const byte data[4])
|
||||
{
|
||||
m_data[0] = data[0];
|
||||
m_data[1] = data[1];
|
||||
m_data[2] = data[2];
|
||||
m_data[3] = data[3];
|
||||
}
|
||||
IPv4Address(byte a, byte b, byte c, byte d)
|
||||
{
|
||||
m_data[0] = a;
|
||||
m_data[1] = b;
|
||||
m_data[2] = c;
|
||||
m_data[3] = d;
|
||||
}
|
||||
|
||||
byte operator[](int i) const
|
||||
{
|
||||
ASSERT(i >= 0 && i < 4);
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
String to_string() const
|
||||
{
|
||||
return String::format("%u.%u.%u.%u", m_data[0], m_data[1], m_data[2], m_data[3]);
|
||||
}
|
||||
|
||||
bool operator==(const IPv4Address& other) const { return m_data_as_dword == other.m_data_as_dword; }
|
||||
bool operator!=(const IPv4Address& other) const { return m_data_as_dword != other.m_data_as_dword; }
|
||||
|
||||
private:
|
||||
union {
|
||||
byte m_data[4];
|
||||
dword m_data_as_dword { 0 };
|
||||
};
|
||||
};
|
||||
|
||||
static_assert(sizeof(IPv4Address) == 4);
|
||||
|
||||
template<>
|
||||
struct Traits<IPv4Address> {
|
||||
static unsigned hash(const IPv4Address& address) { return string_hash((const char*)&address, sizeof(address)); }
|
||||
static void dump(const IPv4Address& address) { kprintf("%s", address.to_string().characters()); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::IPv4Address;
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/IPv4Address.h>
|
||||
#include <AK/NetworkOrdered.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
|
@ -13,58 +14,6 @@ enum class IPv4Protocol : word {
|
|||
|
||||
NetworkOrdered<word> internet_checksum(const void*, size_t);
|
||||
|
||||
class [[gnu::packed]] IPv4Address
|
||||
{
|
||||
public:
|
||||
IPv4Address() {}
|
||||
IPv4Address(const byte data[4])
|
||||
{
|
||||
m_data[0] = data[0];
|
||||
m_data[1] = data[1];
|
||||
m_data[2] = data[2];
|
||||
m_data[3] = data[3];
|
||||
}
|
||||
IPv4Address(byte a, byte b, byte c, byte d)
|
||||
{
|
||||
m_data[0] = a;
|
||||
m_data[1] = b;
|
||||
m_data[2] = c;
|
||||
m_data[3] = d;
|
||||
}
|
||||
|
||||
byte operator[](int i) const
|
||||
{
|
||||
ASSERT(i >= 0 && i < 4);
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
String to_string() const
|
||||
{
|
||||
return String::format("%u.%u.%u.%u", m_data[0], m_data[1], m_data[2], m_data[3]);
|
||||
}
|
||||
|
||||
bool operator==(const IPv4Address& other) const { return m_data_as_dword == other.m_data_as_dword; }
|
||||
bool operator!=(const IPv4Address& other) const { return m_data_as_dword != other.m_data_as_dword; }
|
||||
|
||||
private:
|
||||
union {
|
||||
byte m_data[4];
|
||||
dword m_data_as_dword { 0 };
|
||||
};
|
||||
};
|
||||
|
||||
static_assert(sizeof(IPv4Address) == 4);
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Traits<IPv4Address> {
|
||||
static unsigned hash(const IPv4Address& address) { return string_hash((const char*)&address, sizeof(address)); }
|
||||
static void dump(const IPv4Address& address) { kprintf("%s", address.to_string().characters()); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
class [[gnu::packed]] IPv4Packet
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/Net/IPv4.h>
|
||||
#include <AK/IPv4Address.h>
|
||||
|
||||
class CSocketAddress {
|
||||
public:
|
||||
|
|
Loading…
Reference in a new issue