2019-03-10 15:25:33 +01:00
|
|
|
#include <Kernel/NetworkAdapter.h>
|
|
|
|
#include <Kernel/StdLib.h>
|
2019-03-10 23:40:09 +01:00
|
|
|
#include <Kernel/EthernetFrameHeader.h>
|
|
|
|
#include <Kernel/kmalloc.h>
|
2019-03-11 23:21:38 +01:00
|
|
|
#include <Kernel/EtherType.h>
|
2019-03-10 15:25:33 +01:00
|
|
|
|
|
|
|
NetworkAdapter::NetworkAdapter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkAdapter::~NetworkAdapter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:40:09 +01:00
|
|
|
void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet)
|
|
|
|
{
|
2019-03-12 01:30:49 +01:00
|
|
|
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket) + sizeof(EthernetFrameCheckSequence);
|
2019-03-10 23:40:09 +01:00
|
|
|
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
|
|
|
eth->set_source(mac_address());
|
|
|
|
eth->set_destination(destination);
|
2019-03-11 23:21:38 +01:00
|
|
|
eth->set_ether_type(EtherType::ARP);
|
2019-03-10 23:40:09 +01:00
|
|
|
memcpy(eth->payload(), &packet, sizeof(ARPPacket));
|
|
|
|
send_raw((byte*)eth, size_in_bytes);
|
|
|
|
kfree(eth);
|
|
|
|
}
|
2019-03-11 12:43:45 +01:00
|
|
|
|
2019-03-12 04:11:20 +01:00
|
|
|
void NetworkAdapter::send_ipv4(const MACAddress& destination, const void* packet, size_t packet_size)
|
|
|
|
{
|
|
|
|
size_t size_in_bytes = sizeof(EthernetFrameHeader) + packet_size + sizeof(EthernetFrameCheckSequence);
|
|
|
|
auto* eth = (EthernetFrameHeader*)kmalloc(size_in_bytes);
|
|
|
|
eth->set_source(mac_address());
|
|
|
|
eth->set_destination(destination);
|
|
|
|
eth->set_ether_type(EtherType::IPv4);
|
|
|
|
memcpy(eth->payload(), packet, packet_size);
|
|
|
|
send_raw((byte*)eth, size_in_bytes);
|
|
|
|
kfree(eth);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-11 12:43:45 +01:00
|
|
|
void NetworkAdapter::did_receive(const byte* data, int length)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_packet_queue.append(ByteBuffer::copy(data, length));
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer NetworkAdapter::dequeue_packet()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (m_packet_queue.is_empty())
|
|
|
|
return { };
|
|
|
|
return m_packet_queue.take_first();
|
|
|
|
}
|
2019-03-11 23:21:38 +01:00
|
|
|
|
|
|
|
void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
|
|
|
|
{
|
|
|
|
m_ipv4_address = address;
|
|
|
|
}
|