2019-03-10 17:25:33 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-11 14:43:45 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-16 08:06:49 +03:00
|
|
|
#include <AK/Function.h>
|
2019-03-11 14:43:45 +03:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-03-10 17:25:33 +03:00
|
|
|
#include <AK/Types.h>
|
2019-08-09 05:34:32 +03:00
|
|
|
#include <AK/Weakable.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
2019-08-04 22:22:22 +03:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/ARP.h>
|
|
|
|
#include <Kernel/Net/ICMP.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/Net/IPv4.h>
|
|
|
|
#include <Kernel/Net/MACAddress.h>
|
2019-03-20 19:09:46 +03:00
|
|
|
|
|
|
|
class NetworkAdapter;
|
|
|
|
|
2019-08-09 05:34:32 +03:00
|
|
|
class NetworkAdapter : public Weakable<NetworkAdapter> {
|
2019-03-10 17:25:33 +03:00
|
|
|
public:
|
2019-06-16 08:06:49 +03:00
|
|
|
static void for_each(Function<void(NetworkAdapter&)>);
|
2019-08-09 05:34:32 +03:00
|
|
|
static WeakPtr<NetworkAdapter> from_ipv4_address(const IPv4Address&);
|
2019-03-10 17:25:33 +03:00
|
|
|
virtual ~NetworkAdapter();
|
|
|
|
|
|
|
|
virtual const char* class_name() const = 0;
|
2019-06-16 08:06:49 +03:00
|
|
|
|
|
|
|
const String& name() const { return m_name; }
|
2019-03-10 21:15:22 +03:00
|
|
|
MACAddress mac_address() { return m_mac_address; }
|
2019-03-12 01:21:38 +03:00
|
|
|
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
|
|
|
|
|
|
|
void set_ipv4_address(const IPv4Address&);
|
2019-03-10 17:25:33 +03:00
|
|
|
|
2019-03-11 01:40:09 +03:00
|
|
|
void send(const MACAddress&, const ARPPacket&);
|
2019-08-05 11:37:09 +03:00
|
|
|
void send_ipv4(const MACAddress&, const IPv4Address&, IPv4Protocol, const u8* payload, size_t payload_size);
|
2019-03-10 22:59:23 +03:00
|
|
|
|
2019-08-05 12:06:21 +03:00
|
|
|
Optional<KBuffer> dequeue_packet();
|
2019-03-11 14:43:45 +03:00
|
|
|
|
2019-03-20 19:09:46 +03:00
|
|
|
bool has_queued_packets() const { return !m_packet_queue.is_empty(); }
|
|
|
|
|
2019-08-08 05:32:35 +03:00
|
|
|
u32 packets_in() const { return m_packets_in; }
|
|
|
|
u32 bytes_in() const { return m_bytes_in; }
|
|
|
|
u32 packets_out() const { return m_packets_out; }
|
|
|
|
u32 bytes_out() const { return m_bytes_out; }
|
|
|
|
|
2019-03-10 17:25:33 +03:00
|
|
|
protected:
|
|
|
|
NetworkAdapter();
|
2019-06-16 08:06:49 +03:00
|
|
|
void set_interface_name(const StringView& basename);
|
2019-03-10 21:15:22 +03:00
|
|
|
void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; }
|
2019-07-03 22:17:35 +03:00
|
|
|
virtual void send_raw(const u8*, int) = 0;
|
|
|
|
void did_receive(const u8*, int);
|
2019-03-10 17:25:33 +03:00
|
|
|
|
|
|
|
private:
|
2019-03-10 21:15:22 +03:00
|
|
|
MACAddress m_mac_address;
|
2019-03-12 01:21:38 +03:00
|
|
|
IPv4Address m_ipv4_address;
|
2019-08-05 12:06:21 +03:00
|
|
|
SinglyLinkedList<KBuffer> m_packet_queue;
|
2019-06-16 08:06:49 +03:00
|
|
|
String m_name;
|
2019-08-08 05:32:35 +03:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-03-10 17:25:33 +03:00
|
|
|
};
|