1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 17:15:08 +00:00

Kernel: Collect IPv4 stuff in IPv4.h and ARP stuff in ARP.h.

This commit is contained in:
Andreas Kling 2019-03-12 12:49:01 +01:00
parent 87ecf290f4
commit c6a2012fe9
7 changed files with 60 additions and 66 deletions

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <Kernel/MACAddress.h> #include <Kernel/MACAddress.h>
#include <Kernel/IPv4Address.h> #include <Kernel/IPv4.h>
#include <Kernel/EtherType.h> #include <Kernel/EtherType.h>
struct ARPOperation { struct ARPOperation {

View file

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <Kernel/MACAddress.h> #include <Kernel/MACAddress.h>
#include <Kernel/IPv4Packet.h> #include <Kernel/IPv4.h>
#include <Kernel/NetworkOrdered.h>
struct ICMPType { struct ICMPType {
enum { enum {

View file

@ -1,8 +1,10 @@
#pragma once #pragma once
#include <AK/AKString.h>
#include <AK/Assertions.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <Kernel/IPv4Address.h>
#include <Kernel/NetworkOrdered.h> #include <Kernel/NetworkOrdered.h>
#include <Kernel/StdLib.h>
enum class IPv4Protocol : word { enum class IPv4Protocol : word {
ICMP = 1, ICMP = 1,
@ -12,6 +14,55 @@ enum class IPv4Protocol : word {
NetworkOrdered<word> internet_checksum(const void*, size_t); NetworkOrdered<word> internet_checksum(const void*, size_t);
class [[gnu::packed]] IPv4Address {
public:
IPv4Address() { }
IPv4Address(const byte data[4])
{
memcpy(m_data, data, 4);
}
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;
}
~IPv4Address() { }
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;
};
};
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 { class [[gnu::packed]] IPv4Packet {
public: public:
byte version() const { return (m_version_and_ihl >> 4) & 0xf; } byte version() const { return (m_version_and_ihl >> 4) & 0xf; }

View file

@ -1,55 +0,0 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/AKString.h>
#include <AK/Types.h>
#include <Kernel/StdLib.h>
class [[gnu::packed]] IPv4Address {
public:
IPv4Address() { }
IPv4Address(const byte data[4])
{
memcpy(m_data, data, 4);
}
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;
}
~IPv4Address() { }
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;
};
};
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()); }
};
}

View file

@ -4,9 +4,8 @@
#include <AK/SinglyLinkedList.h> #include <AK/SinglyLinkedList.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <Kernel/MACAddress.h> #include <Kernel/MACAddress.h>
#include <Kernel/IPv4Address.h> #include <Kernel/IPv4.h>
#include <Kernel/ARPPacket.h> #include <Kernel/ARP.h>
#include <Kernel/IPv4Packet.h>
#include <Kernel/ICMP.h> #include <Kernel/ICMP.h>
class NetworkAdapter { class NetworkAdapter {

View file

@ -1,8 +1,8 @@
#include <Kernel/E1000NetworkAdapter.h> #include <Kernel/E1000NetworkAdapter.h>
#include <Kernel/EthernetFrameHeader.h> #include <Kernel/EthernetFrameHeader.h>
#include <Kernel/ARPPacket.h> #include <Kernel/ARP.h>
#include <Kernel/ICMP.h> #include <Kernel/ICMP.h>
#include <Kernel/IPv4Packet.h> #include <Kernel/IPv4.h>
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/EtherType.h> #include <Kernel/EtherType.h>
#include <AK/Lock.h> #include <AK/Lock.h>
@ -33,7 +33,7 @@ void NetworkTask_main()
for (;;) { for (;;) {
auto packet = e1000.dequeue_packet(); auto packet = e1000.dequeue_packet();
if (packet.is_null()) { if (packet.is_null()) {
sleep(100); sleep(1);
continue; continue;
} }
if (packet.size() < (int)(sizeof(EthernetFrameHeader))) { if (packet.size() < (int)(sizeof(EthernetFrameHeader))) {

View file

@ -24,7 +24,7 @@
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Kernel/E1000NetworkAdapter.h> #include <Kernel/E1000NetworkAdapter.h>
#include <Kernel/EthernetFrameHeader.h> #include <Kernel/EthernetFrameHeader.h>
#include <Kernel/ARPPacket.h> #include <Kernel/ARP.h>
//#define DEBUG_IO //#define DEBUG_IO
//#define TASK_DEBUG //#define TASK_DEBUG