summaryrefslogtreecommitdiff
path: root/Kernel/NetworkAdapter.h
blob: a040aaced26591deb0c63eced738e91f24b18530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once

#include <AK/ByteBuffer.h>
#include <AK/SinglyLinkedList.h>
#include <AK/Types.h>
#include <Kernel/MACAddress.h>
#include <Kernel/IPv4.h>
#include <Kernel/ARP.h>
#include <Kernel/ICMP.h>
#include <Kernel/Alarm.h>

class NetworkAdapter;

class PacketQueueAlarm final : public Alarm {
public:
    PacketQueueAlarm(NetworkAdapter& adapter) : m_adapter(adapter) { }
    virtual ~PacketQueueAlarm() override { }
    virtual bool is_ringing() const override;
private:
    NetworkAdapter& m_adapter;
};

class NetworkAdapter {
public:
    static NetworkAdapter* from_ipv4_address(const IPv4Address&);
    virtual ~NetworkAdapter();

    virtual const char* class_name() const = 0;
    MACAddress mac_address() { return m_mac_address; }
    IPv4Address ipv4_address() const { return m_ipv4_address; }

    void set_ipv4_address(const IPv4Address&);

    void send(const MACAddress&, const ARPPacket&);
    void send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, ByteBuffer&& payload);

    ByteBuffer dequeue_packet();

    Alarm& packet_queue_alarm() { return m_packet_queue_alarm; }

    bool has_queued_packets() const { return !m_packet_queue.is_empty(); }

protected:
    NetworkAdapter();
    void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; }
    virtual void send_raw(const byte*, int) = 0;
    void did_receive(const byte*, int);

private:
    MACAddress m_mac_address;
    IPv4Address m_ipv4_address;
    PacketQueueAlarm m_packet_queue_alarm;
    SinglyLinkedList<ByteBuffer> m_packet_queue;
};