1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

Kernel: Tidy up networking code with some named constants.

This commit is contained in:
Andreas Kling 2019-03-12 01:30:49 +01:00
parent 90f60d2f65
commit d5dbb602b8
4 changed files with 21 additions and 15 deletions

View file

@ -4,6 +4,19 @@
#include <Kernel/IPv4Address.h> #include <Kernel/IPv4Address.h>
#include <Kernel/EtherType.h> #include <Kernel/EtherType.h>
struct ARPOperation {
enum : word {
Request = 1,
Response = 2,
};
};
struct ARPHardwareType {
enum : word {
Ethernet = 1,
};
};
class [[gnu::packed]] ARPPacket { class [[gnu::packed]] ARPPacket {
public: public:
word hardware_type() const { return ntohs(m_hardware_type); } word hardware_type() const { return ntohs(m_hardware_type); }

View file

@ -26,5 +26,7 @@ private:
dword m_payload[0]; dword m_payload[0];
}; };
typedef dword EthernetFrameCheckSequence;
static_assert(sizeof(EthernetFrameHeader) == 14); static_assert(sizeof(EthernetFrameHeader) == 14);

View file

@ -14,7 +14,7 @@ NetworkAdapter::~NetworkAdapter()
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet) 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); auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
eth->set_source(mac_address()); eth->set_source(mac_address());
eth->set_destination(destination); eth->set_destination(destination);

View file

@ -21,15 +21,7 @@ void NetworkTask_main()
auto* e1000_ptr = E1000NetworkAdapter::the(); auto* e1000_ptr = E1000NetworkAdapter::the();
ASSERT(e1000_ptr); ASSERT(e1000_ptr);
auto& e1000 = *e1000_ptr; auto& e1000 = *e1000_ptr;
e1000.set_ipv4_address(IPv4Address(192, 168, 5, 2)); 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"); kprintf("NetworkTask: Enter main loop.\n");
for (;;) { for (;;) {
@ -38,7 +30,7 @@ void NetworkTask_main()
sleep(100); sleep(100);
continue; 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()); kprintf("NetworkTask: Packet is too small to be an Ethernet packet! (%d)\n", packet.size());
continue; continue;
} }
@ -63,7 +55,7 @@ void NetworkTask_main()
void handle_arp(const EthernetFrameHeader& eth, int frame_size) 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) { if (frame_size < minimum_arp_frame_size) {
kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size); kprintf("handle_arp: Frame too small (%d, need %d)\n", frame_size, minimum_arp_frame_size);
return; 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. // FIXME: Get the adapter through some kind of lookup by IPv4 address.
auto& e1000 = *E1000NetworkAdapter::the(); auto& e1000 = *E1000NetworkAdapter::the();
if (packet.operation() == 1) { if (packet.operation() == ARPOperation::Request) {
// Who has this IP address? // Who has this IP address?
if (e1000.ipv4_address() == packet.target_protocol_address()) { if (e1000.ipv4_address() == packet.target_protocol_address()) {
// We do! // We do!
kprintf("handle_arp: Responding to ARP request for my IPv4 address (%s)\n", kprintf("handle_arp: Responding to ARP request for my IPv4 address (%s)\n",
e1000.ipv4_address().to_string().characters()); e1000.ipv4_address().to_string().characters());
ARPPacket response; ARPPacket response;
response.set_operation(2); // Response response.set_operation(ARPOperation::Response);
response.set_target_hardware_address(packet.sender_hardware_address()); response.set_target_hardware_address(packet.sender_hardware_address());
response.set_target_protocol_address(packet.sender_protocol_address()); response.set_target_protocol_address(packet.sender_protocol_address());
response.set_sender_hardware_address(e1000.mac_address()); response.set_sender_hardware_address(e1000.mac_address());
@ -116,7 +107,7 @@ void handle_arp(const EthernetFrameHeader& eth, int frame_size)
return; return;
} }
if (packet.operation() == 2) { if (packet.operation() == ARPOperation::Response) {
// Someone has this IPv4 address. I guess we can try to remember that. // Someone has this IPv4 address. I guess we can try to remember that.
// FIXME: Protect against ARP spamming. // FIXME: Protect against ARP spamming.
// FIXME: Support static ARP table entries. // FIXME: Support static ARP table entries.