summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-12 01:30:49 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-12 01:30:49 +0100
commitd5dbb602b8cfd2025095f96283bbe8fad993527b (patch)
treeb4aec0169d57c1ca5c8c4f541b8f84569f5c3a69 /Kernel
parent90f60d2f650a417a8ffd002d2cfa22605941069f (diff)
downloadserenity-d5dbb602b8cfd2025095f96283bbe8fad993527b.zip
Kernel: Tidy up networking code with some named constants.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/ARPPacket.h13
-rw-r--r--Kernel/EthernetFrameHeader.h2
-rw-r--r--Kernel/NetworkAdapter.cpp2
-rw-r--r--Kernel/NetworkTask.cpp19
4 files changed, 21 insertions, 15 deletions
diff --git a/Kernel/ARPPacket.h b/Kernel/ARPPacket.h
index 72cb75b625..23f44ccb4c 100644
--- a/Kernel/ARPPacket.h
+++ b/Kernel/ARPPacket.h
@@ -4,6 +4,19 @@
#include <Kernel/IPv4Address.h>
#include <Kernel/EtherType.h>
+struct ARPOperation {
+enum : word {
+ Request = 1,
+ Response = 2,
+};
+};
+
+struct ARPHardwareType {
+enum : word {
+ Ethernet = 1,
+};
+};
+
class [[gnu::packed]] ARPPacket {
public:
word hardware_type() const { return ntohs(m_hardware_type); }
diff --git a/Kernel/EthernetFrameHeader.h b/Kernel/EthernetFrameHeader.h
index f6f74b42bb..5a079231e2 100644
--- a/Kernel/EthernetFrameHeader.h
+++ b/Kernel/EthernetFrameHeader.h
@@ -26,5 +26,7 @@ private:
dword m_payload[0];
};
+typedef dword EthernetFrameCheckSequence;
+
static_assert(sizeof(EthernetFrameHeader) == 14);
diff --git a/Kernel/NetworkAdapter.cpp b/Kernel/NetworkAdapter.cpp
index b7e29967ae..75d32c7763 100644
--- a/Kernel/NetworkAdapter.cpp
+++ b/Kernel/NetworkAdapter.cpp
@@ -14,7 +14,7 @@ NetworkAdapter::~NetworkAdapter()
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
{
- int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + 4;
+ int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + sizeof(EthernetFrameCheckSequence);
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
eth->set_source(mac_address());
eth->set_destination(destination);
diff --git a/Kernel/NetworkTask.cpp b/Kernel/NetworkTask.cpp
index e8a5404bb4..d2e6ad66ab 100644
--- a/Kernel/NetworkTask.cpp
+++ b/Kernel/NetworkTask.cpp
@@ -21,15 +21,7 @@ void NetworkTask_main()
auto* e1000_ptr = E1000NetworkAdapter::the();
ASSERT(e1000_ptr);
auto& e1000 = *e1000_ptr;
-
e1000.set_ipv4_address(IPv4Address(192, 168, 5, 2));
- ARPPacket arp;
- arp.set_hardware_type(1); // Ethernet
- arp.set_hardware_address_length(sizeof(MACAddress));
- arp.set_protocol_type(EtherType::IPv4);
- arp.set_protocol_address_length(sizeof(IPv4Address));
- arp.set_operation(1); // Request
- e1000.send(MACAddress(), arp);
kprintf("NetworkTask: Enter main loop.\n");
for (;;) {
@@ -38,7 +30,7 @@ void NetworkTask_main()
sleep(100);
continue;
}
- if (packet.size() < sizeof(EthernetFrameHeader) + 4) {
+ if (packet.size() < (int)(sizeof(EthernetFrameHeader) + sizeof(EthernetFrameCheckSequence))) {
kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
continue;
}
@@ -63,7 +55,7 @@ void NetworkTask_main()
void handle_arp(const EthernetFrameHeader& eth, int frame_size)
{
- constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + 4;
+ constexpr int minimum_arp_frame_size = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + sizeof(EthernetFrameCheckSequence);
if (frame_size < minimum_arp_frame_size) {
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
return;
@@ -97,15 +89,14 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
// FIXME: Get the adapter through some kind of lookup by IPv4 address.
auto& e1000 = *E1000NetworkAdapter::the();
- if (packet.operation() == 1) {
+ if (packet.operation() == ARPOperation::Request) {
// Who has this IP address?
if (e1000.ipv4_address() == packet.target_protocol_address()) {
// We do!
kprintf("handle_arp: Responding to ARP request for my IPv4 address (%s)\n",
e1000.ipv4_address().to_string().characters());
ARPPacket response;
- response.set_operation(2); // Response
-
+ response.set_operation(ARPOperation::Response);
response.set_target_hardware_address(packet.sender_hardware_address());
response.set_target_protocol_address(packet.sender_protocol_address());
response.set_sender_hardware_address(e1000.mac_address());
@@ -116,7 +107,7 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
return;
}
- if (packet.operation() == 2) {
+ if (packet.operation() == ARPOperation::Response) {
// Someone has this IPv4 address. I guess we can try to remember that.
// FIXME: Protect against ARP spamming.
// FIXME: Support static ARP table entries.