Move ethernet to their own namespace and create util namespace to avoid cyclic dependencies
This commit is contained in:
parent
8bde798c88
commit
118f6b62c2
16 changed files with 410 additions and 345 deletions
2
Makefile
2
Makefile
|
@ -4,7 +4,7 @@ OBJS = armc-start.o armc-cstartup.o armc-cstubs.o armc-cppstubs.o \
|
||||||
Drive.o Pi1541.o DiskImage.o iec_bus.o iec_commands.o m6502.o m6522.o \
|
Drive.o Pi1541.o DiskImage.o iec_bus.o iec_commands.o m6502.o m6522.o \
|
||||||
gcr.o prot.o lz.o emmc.o diskio.o options.o Screen.o SSD1306.o ScreenLCD.o \
|
gcr.o prot.o lz.o emmc.o diskio.o options.o Screen.o SSD1306.o ScreenLCD.o \
|
||||||
Timer.o FileBrowser.o DiskCaddy.o ROMs.o InputMappings.o xga_font_data.o m8520.o wd177x.o Pi1581.o SpinLock.o \
|
Timer.o FileBrowser.o DiskCaddy.o ROMs.o InputMappings.o xga_font_data.o m8520.o wd177x.o Pi1581.o SpinLock.o \
|
||||||
net.o net-tftp.o net-arp.o net-ethernet.o net-icmp.o net-ipv4.o net-udp.o net-dhcp.o
|
net.o net-tftp.o net-arp.o net-ethernet.o net-icmp.o net-ipv4.o net-udp.o net-dhcp.o net-utils.o
|
||||||
|
|
||||||
SRCDIR = src
|
SRCDIR = src
|
||||||
OBJS := $(addprefix $(SRCDIR)/, $(OBJS))
|
OBJS := $(addprefix $(SRCDIR)/, $(OBJS))
|
||||||
|
|
|
@ -376,22 +376,23 @@ void updateNetwork()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ethernetHeader = EthernetFrameHeader::Deserialize(ipBuffer);
|
auto ethernetHeader = Net::Ethernet::EthernetFrameHeader::Deserialize(ipBuffer);
|
||||||
const auto offset = ethernetHeader.SerializedLength();
|
const auto offset = ethernetHeader.SerializedLength();
|
||||||
|
|
||||||
static bool announcementSent = false;
|
static bool announcementSent = false;
|
||||||
if (!announcementSent)
|
if (!announcementSent)
|
||||||
{
|
{
|
||||||
Net::Arp::SendAnnouncement(GetMacAddress(), Ipv4Address);
|
Net::Arp::SendAnnouncement(
|
||||||
|
Net::Utils::GetMacAddress(), Net::Utils::Ipv4Address);
|
||||||
announcementSent = true;
|
announcementSent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ethernetHeader.type)
|
switch (ethernetHeader.type)
|
||||||
{
|
{
|
||||||
case ETHERTYPE_ARP:
|
case Net::Ethernet::ETHERTYPE_ARP:
|
||||||
Net::Arp::HandlePacket(ethernetHeader, ipBuffer + offset);
|
Net::Arp::HandlePacket(ethernetHeader, ipBuffer + offset);
|
||||||
break;
|
break;
|
||||||
case ETHERTYPE_IPV4:
|
case Net::Ethernet::ETHERTYPE_IPV4:
|
||||||
HandleIpv4Packet(ethernetHeader, ipBuffer + offset, sizeof(ipBuffer) - offset);
|
HandleIpv4Packet(ethernetHeader, ipBuffer + offset, sizeof(ipBuffer) - offset);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "net-arp.h"
|
#include "net-arp.h"
|
||||||
#include "net-ethernet.h"
|
#include "net-ethernet.h"
|
||||||
|
|
||||||
|
@ -9,15 +11,15 @@ namespace Net::Arp
|
||||||
Ipv4ArpPacket::Ipv4ArpPacket()
|
Ipv4ArpPacket::Ipv4ArpPacket()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Ipv4ArpPacket::Ipv4ArpPacket(std::uint16_t operation) :
|
Ipv4ArpPacket::Ipv4ArpPacket(uint16_t operation) :
|
||||||
hardwareType(1), // Ethernet
|
hardwareType(1), // Ethernet
|
||||||
protocolType(ETHERTYPE_IPV4), // IPv4
|
protocolType(Net::Ethernet::ETHERTYPE_IPV4),
|
||||||
hardwareAddressLength(6),
|
hardwareAddressLength(6),
|
||||||
protocolAddressLength(4),
|
protocolAddressLength(4),
|
||||||
operation(operation)
|
operation(operation)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::size_t Ipv4ArpPacket::Serialize(std::uint8_t* buffer)
|
size_t Ipv4ArpPacket::Serialize(uint8_t* buffer)
|
||||||
{
|
{
|
||||||
buffer[0] = hardwareType >> 8;
|
buffer[0] = hardwareType >> 8;
|
||||||
buffer[1] = hardwareType;
|
buffer[1] = hardwareType;
|
||||||
|
@ -79,7 +81,8 @@ namespace Net::Arp
|
||||||
arpPacket.targetIp = targetIp;
|
arpPacket.targetIp = targetIp;
|
||||||
arpPacket.senderIp = senderIp;
|
arpPacket.senderIp = senderIp;
|
||||||
|
|
||||||
EthernetFrameHeader ethernetHeader(senderMac, targetMac, ETHERTYPE_ARP);
|
Net::Ethernet::EthernetFrameHeader ethernetHeader(
|
||||||
|
senderMac, targetMac, Net::Ethernet::ETHERTYPE_ARP);
|
||||||
|
|
||||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
@ -89,8 +92,11 @@ namespace Net::Arp
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendRequest(
|
void SendRequest(
|
||||||
MacAddress targetMac, MacAddress senderMac, uint32_t targetIp, uint32_t senderIp)
|
MacAddress targetMac,
|
||||||
{
|
MacAddress senderMac,
|
||||||
|
uint32_t targetIp,
|
||||||
|
uint32_t senderIp
|
||||||
|
) {
|
||||||
SendPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp);
|
SendPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,33 +108,39 @@ namespace Net::Arp
|
||||||
|
|
||||||
void SendAnnouncement(MacAddress mac, uint32_t ip)
|
void SendAnnouncement(MacAddress mac, uint32_t ip)
|
||||||
{
|
{
|
||||||
SendReply(MacBroadcast, mac, ip, ip);
|
SendReply(Net::Utils::MacBroadcast, mac, ip, ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandlePacket(const EthernetFrameHeader ethernetHeader, uint8_t* buffer)
|
void HandlePacket(
|
||||||
{
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader, uint8_t* buffer
|
||||||
const auto macAddress = GetMacAddress();
|
) {
|
||||||
|
const auto macAddress = Net::Utils::GetMacAddress();
|
||||||
const auto arpPacket = Ipv4ArpPacket::Deserialize(buffer);
|
const auto arpPacket = Ipv4ArpPacket::Deserialize(buffer);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
arpPacket.hardwareType == 1 &&
|
arpPacket.hardwareType == 1 &&
|
||||||
arpPacket.protocolType == ETHERTYPE_IPV4 &&
|
arpPacket.protocolType == Net::Ethernet::ETHERTYPE_IPV4 &&
|
||||||
arpPacket.operation == ARP_OPERATION_REQUEST &&
|
arpPacket.operation == ARP_OPERATION_REQUEST &&
|
||||||
arpPacket.targetIp == Ipv4Address)
|
arpPacket.targetIp == Net::Utils::Ipv4Address)
|
||||||
{
|
{
|
||||||
SendReply(arpPacket.senderMac, macAddress, arpPacket.senderIp, Ipv4Address);
|
SendReply(
|
||||||
|
arpPacket.senderMac,
|
||||||
|
macAddress,
|
||||||
|
arpPacket.senderIp,
|
||||||
|
Net::Utils::Ipv4Address
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (
|
else if (
|
||||||
arpPacket.hardwareType == 1 &&
|
arpPacket.hardwareType == 1 &&
|
||||||
arpPacket.protocolType == ETHERTYPE_IPV4 &&
|
arpPacket.protocolType == Net::Ethernet::ETHERTYPE_IPV4 &&
|
||||||
arpPacket.operation == ARP_OPERATION_REPLY &&
|
arpPacket.operation == ARP_OPERATION_REPLY &&
|
||||||
arpPacket.targetIp == Ipv4Address &&
|
arpPacket.targetIp == Net::Utils::Ipv4Address &&
|
||||||
arpPacket.targetMac == macAddress)
|
arpPacket.targetMac == macAddress)
|
||||||
{
|
{
|
||||||
ArpTable.insert(std::make_pair(arpPacket.senderIp, arpPacket.senderMac));
|
ArpTable.insert(std::make_pair(arpPacket.senderIp, arpPacket.senderMac));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<std::uint32_t, MacAddress> ArpTable;
|
std::unordered_map<uint32_t, MacAddress> ArpTable;
|
||||||
}; // namespace Net::Arp
|
}; // namespace Net::Arp
|
||||||
|
|
|
@ -1,25 +1,34 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "net.h"
|
#include <unordered_map>
|
||||||
|
#include "net-ethernet.h"
|
||||||
|
#include "net-utils.h"
|
||||||
|
|
||||||
namespace Net::Arp
|
namespace Net::Arp
|
||||||
{
|
{
|
||||||
|
using Net::Utils::MacAddress;
|
||||||
|
|
||||||
|
enum ArpOperation {
|
||||||
|
ARP_OPERATION_REQUEST = 1,
|
||||||
|
ARP_OPERATION_REPLY = 2,
|
||||||
|
};
|
||||||
|
|
||||||
struct Ipv4ArpPacket
|
struct Ipv4ArpPacket
|
||||||
{
|
{
|
||||||
std::uint16_t hardwareType;
|
uint16_t hardwareType;
|
||||||
std::uint16_t protocolType;
|
uint16_t protocolType;
|
||||||
std::uint8_t hardwareAddressLength;
|
uint8_t hardwareAddressLength;
|
||||||
std::uint8_t protocolAddressLength;
|
uint8_t protocolAddressLength;
|
||||||
std::uint16_t operation;
|
uint16_t operation;
|
||||||
|
|
||||||
MacAddress senderMac;
|
MacAddress senderMac;
|
||||||
std::uint32_t senderIp;
|
uint32_t senderIp;
|
||||||
MacAddress targetMac;
|
MacAddress targetMac;
|
||||||
std::uint32_t targetIp;
|
uint32_t targetIp;
|
||||||
|
|
||||||
Ipv4ArpPacket();
|
Ipv4ArpPacket();
|
||||||
Ipv4ArpPacket(std::uint16_t operation);
|
Ipv4ArpPacket(uint16_t operation);
|
||||||
|
|
||||||
constexpr std::size_t SerializedLength() const
|
constexpr size_t SerializedLength() const
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
sizeof(hardwareType) +
|
sizeof(hardwareType) +
|
||||||
|
@ -33,12 +42,12 @@ namespace Net::Arp
|
||||||
sizeof(targetIp);
|
sizeof(targetIp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Serialize(std::uint8_t* buffer);
|
size_t Serialize(uint8_t* buffer);
|
||||||
|
|
||||||
static Ipv4ArpPacket Deserialize(const uint8_t* buffer);
|
static Ipv4ArpPacket Deserialize(const uint8_t* buffer);
|
||||||
};
|
};
|
||||||
|
|
||||||
void HandlePacket(EthernetFrameHeader header, uint8_t* buffer);
|
void HandlePacket(Net::Ethernet::EthernetFrameHeader header, uint8_t* buffer);
|
||||||
|
|
||||||
void SendPacket(
|
void SendPacket(
|
||||||
ArpOperation operation,
|
ArpOperation operation,
|
||||||
|
@ -64,5 +73,5 @@ namespace Net::Arp
|
||||||
|
|
||||||
void SendAnnouncement(MacAddress mac, uint32_t ip);
|
void SendAnnouncement(MacAddress mac, uint32_t ip);
|
||||||
|
|
||||||
extern std::unordered_map<std::uint32_t, MacAddress> ArpTable;
|
extern std::unordered_map<uint32_t, MacAddress> ArpTable;
|
||||||
}; // namespace Net::Arp
|
}; // namespace Net::Arp
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace Net::Dhcp
|
||||||
bootFile{0},
|
bootFile{0},
|
||||||
magicValue{99, 130, 83, 99}
|
magicValue{99, 130, 83, 99}
|
||||||
{
|
{
|
||||||
const auto mac = GetMacAddress();
|
const auto mac = Net::Utils::GetMacAddress();
|
||||||
hardwareAddressLength = mac.size();
|
hardwareAddressLength = mac.size();
|
||||||
std::memcpy(clientHardwareAddress.data(), mac.data(), mac.size());
|
std::memcpy(clientHardwareAddress.data(), mac.data(), mac.size());
|
||||||
}
|
}
|
||||||
|
@ -136,8 +136,11 @@ namespace Net::Dhcp
|
||||||
size_t ipv4Length = udpLength + Ipv4Header::SerializedLength();
|
size_t ipv4Length = udpLength + Ipv4Header::SerializedLength();
|
||||||
const Ipv4Header ipv4Header(
|
const Ipv4Header ipv4Header(
|
||||||
IP_PROTO_UDP, clientIpAddress, serverIpAddress, ipv4Length);
|
IP_PROTO_UDP, clientIpAddress, serverIpAddress, ipv4Length);
|
||||||
const EthernetFrameHeader ethernetHeader(
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader(
|
||||||
serverMacAddress, GetMacAddress(), ETHERTYPE_IPV4);
|
serverMacAddress,
|
||||||
|
Net::Utils::GetMacAddress(),
|
||||||
|
Net::Ethernet::ETHERTYPE_IPV4
|
||||||
|
);
|
||||||
|
|
||||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
@ -190,7 +193,8 @@ namespace Net::Dhcp
|
||||||
|
|
||||||
size_t ipv4Length = udpLength + Ipv4Header::SerializedLength();
|
size_t ipv4Length = udpLength + Ipv4Header::SerializedLength();
|
||||||
const Ipv4Header ipv4Header(IP_PROTO_UDP, 0, 0xFFFFFFFF, ipv4Length);
|
const Ipv4Header ipv4Header(IP_PROTO_UDP, 0, 0xFFFFFFFF, ipv4Length);
|
||||||
const EthernetFrameHeader ethernetHeader(GetMacAddress(), ETHERTYPE_IPV4);
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader(
|
||||||
|
Net::Utils::GetMacAddress(), Net::Ethernet::ETHERTYPE_IPV4);
|
||||||
|
|
||||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
@ -217,7 +221,8 @@ namespace Net::Dhcp
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleOfferPacket(
|
static void handleOfferPacket(
|
||||||
const EthernetFrameHeader ethernetHeader, const DhcpHeader dhcpHeader
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
|
const DhcpHeader dhcpHeader
|
||||||
) {
|
) {
|
||||||
offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
|
offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
|
||||||
serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
|
serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
|
||||||
|
@ -225,9 +230,10 @@ namespace Net::Dhcp
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleAckPacket(
|
static void handleAckPacket(
|
||||||
const EthernetFrameHeader ethernetHeader, const DhcpHeader dhcpHeader
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
|
const DhcpHeader dhcpHeader
|
||||||
) {
|
) {
|
||||||
Ipv4Address = dhcpHeader.yourIpAddress;
|
Net::Utils::Ipv4Address = dhcpHeader.yourIpAddress;
|
||||||
|
|
||||||
// TODO Schedule handler for end of lease.
|
// TODO Schedule handler for end of lease.
|
||||||
|
|
||||||
|
@ -239,7 +245,9 @@ namespace Net::Dhcp
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandlePacket(
|
void HandlePacket(
|
||||||
const EthernetFrameHeader& ethernetHeader, const uint8_t* buffer, size_t size
|
const Net::Ethernet::EthernetFrameHeader& ethernetHeader,
|
||||||
|
const uint8_t* buffer,
|
||||||
|
size_t size
|
||||||
) {
|
) {
|
||||||
auto dhcpHeader = DhcpHeader();
|
auto dhcpHeader = DhcpHeader();
|
||||||
const auto dhcpSize = DhcpHeader::Deserialize(dhcpHeader, buffer, size);
|
const auto dhcpSize = DhcpHeader::Deserialize(dhcpHeader, buffer, size);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include "net-ethernet.h"
|
||||||
|
|
||||||
namespace Net::Dhcp
|
namespace Net::Dhcp
|
||||||
{
|
{
|
||||||
|
@ -89,5 +90,8 @@ namespace Net::Dhcp
|
||||||
|
|
||||||
void SendDiscover();
|
void SendDiscover();
|
||||||
void HandlePacket(
|
void HandlePacket(
|
||||||
const EthernetFrameHeader& ethernetHeader, const uint8_t* buffer, size_t size);
|
const Net::Ethernet::EthernetFrameHeader& ethernetHeader,
|
||||||
|
const uint8_t* buffer,
|
||||||
|
size_t size
|
||||||
|
);
|
||||||
} // namespace Net::Dhcp
|
} // namespace Net::Dhcp
|
||||||
|
|
|
@ -1,32 +1,34 @@
|
||||||
|
#include <cstring>
|
||||||
#include "net-ethernet.h"
|
#include "net-ethernet.h"
|
||||||
|
|
||||||
EthernetFrameHeader::EthernetFrameHeader()
|
namespace Net::Ethernet {
|
||||||
{}
|
EthernetFrameHeader::EthernetFrameHeader()
|
||||||
|
{}
|
||||||
|
|
||||||
EthernetFrameHeader::EthernetFrameHeader(std::uint16_t type) :
|
EthernetFrameHeader::EthernetFrameHeader(std::uint16_t type) :
|
||||||
macDestination{255, 255, 255, 255, 255, 255},
|
macDestination(Net::Utils::MacBroadcast),
|
||||||
macSource{0, 0, 0, 0, 0, 0},
|
macSource{0, 0, 0, 0, 0, 0},
|
||||||
type(type)
|
type(type)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
EthernetFrameHeader::EthernetFrameHeader(
|
EthernetFrameHeader::EthernetFrameHeader(
|
||||||
MacAddress macSource, uint16_t type
|
MacAddress macSource, uint16_t type
|
||||||
) :
|
) :
|
||||||
macDestination{255, 255, 255, 255, 255, 255},
|
macDestination(Net::Utils::MacBroadcast),
|
||||||
macSource(macSource),
|
macSource(macSource),
|
||||||
type(type)
|
type(type)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
EthernetFrameHeader::EthernetFrameHeader(
|
EthernetFrameHeader::EthernetFrameHeader(
|
||||||
MacAddress macDestination, MacAddress macSource, uint16_t type
|
MacAddress macDestination, MacAddress macSource, uint16_t type
|
||||||
) :
|
) :
|
||||||
macDestination(macDestination),
|
macDestination(macDestination),
|
||||||
macSource(macSource),
|
macSource(macSource),
|
||||||
type(type)
|
type(type)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::size_t EthernetFrameHeader::Serialize(uint8_t* buffer) const
|
std::size_t EthernetFrameHeader::Serialize(uint8_t* buffer) const
|
||||||
{
|
{
|
||||||
std::size_t i = 0;
|
std::size_t i = 0;
|
||||||
|
|
||||||
std::memcpy(buffer + i, macDestination.data(), macDestination.size());
|
std::memcpy(buffer + i, macDestination.data(), macDestination.size());
|
||||||
|
@ -39,13 +41,14 @@ std::size_t EthernetFrameHeader::Serialize(uint8_t* buffer) const
|
||||||
buffer[i++] = type;
|
buffer[i++] = type;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
EthernetFrameHeader EthernetFrameHeader::Deserialize(const uint8_t* buffer)
|
EthernetFrameHeader EthernetFrameHeader::Deserialize(const uint8_t* buffer)
|
||||||
{
|
{
|
||||||
EthernetFrameHeader self;
|
EthernetFrameHeader self;
|
||||||
std::memcpy(self.macDestination.data(), buffer + 0, self.macDestination.size());
|
std::memcpy(self.macDestination.data(), buffer + 0, self.macDestination.size());
|
||||||
std::memcpy(self.macSource.data(), buffer + 6, self.macSource.size());
|
std::memcpy(self.macSource.data(), buffer + 6, self.macSource.size());
|
||||||
self.type = buffer[12] << 8 | buffer[13];
|
self.type = buffer[12] << 8 | buffer[13];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
}; // namespace Net::Ethernet
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "net.h"
|
#include <array>
|
||||||
|
#include "net-utils.h"
|
||||||
|
|
||||||
struct EthernetFrameHeader
|
using Net::Utils::MacAddress;
|
||||||
|
|
||||||
|
namespace Net::Ethernet
|
||||||
{
|
{
|
||||||
|
enum EtherType
|
||||||
|
{
|
||||||
|
ETHERTYPE_IPV4 = 0x0800,
|
||||||
|
ETHERTYPE_ARP = 0x0806,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct EthernetFrameHeader
|
||||||
|
{
|
||||||
MacAddress macDestination;
|
MacAddress macDestination;
|
||||||
MacAddress macSource;
|
MacAddress macSource;
|
||||||
std::uint16_t type;
|
std::uint16_t type;
|
||||||
|
@ -19,4 +30,5 @@ struct EthernetFrameHeader
|
||||||
|
|
||||||
std::size_t Serialize(uint8_t* buffer) const;
|
std::size_t Serialize(uint8_t* buffer) const;
|
||||||
static EthernetFrameHeader Deserialize(const uint8_t* buffer);
|
static EthernetFrameHeader Deserialize(const uint8_t* buffer);
|
||||||
};
|
};
|
||||||
|
}; // namespace Net::Ethernet
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "net-ipv4.h"
|
#include "net-ipv4.h"
|
||||||
|
#include "net-utils.h"
|
||||||
|
|
||||||
Ipv4Header::Ipv4Header() {}
|
Ipv4Header::Ipv4Header() {}
|
||||||
|
|
||||||
|
@ -48,7 +49,7 @@ size_t Ipv4Header::Serialize(uint8_t* buffer) const
|
||||||
buffer[i++] = destinationIp >> 8;
|
buffer[i++] = destinationIp >> 8;
|
||||||
buffer[i++] = destinationIp;
|
buffer[i++] = destinationIp;
|
||||||
|
|
||||||
uint16_t checksum = InternetChecksum(buffer, i);
|
uint16_t checksum = Net::Utils::InternetChecksum(buffer, i);
|
||||||
buffer[10] = checksum;
|
buffer[10] = checksum;
|
||||||
buffer[11] = checksum >> 8;
|
buffer[11] = checksum >> 8;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "net.h"
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
enum IpProtocols
|
enum IpProtocols
|
||||||
{
|
{
|
||||||
|
|
|
@ -115,7 +115,7 @@ namespace Net::Tftp
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandlePacket(
|
void HandlePacket(
|
||||||
const EthernetFrameHeader ethernetReqHeader,
|
const Net::Ethernet::EthernetFrameHeader ethernetReqHeader,
|
||||||
const Ipv4Header ipv4ReqHeader,
|
const Ipv4Header ipv4ReqHeader,
|
||||||
const UdpDatagramHeader udpReqHeader,
|
const UdpDatagramHeader udpReqHeader,
|
||||||
const uint8_t* data
|
const uint8_t* data
|
||||||
|
@ -149,14 +149,14 @@ namespace Net::Tftp
|
||||||
);
|
);
|
||||||
Ipv4Header ipv4RespHeader(
|
Ipv4Header ipv4RespHeader(
|
||||||
IP_PROTO_UDP,
|
IP_PROTO_UDP,
|
||||||
Ipv4Address,
|
Net::Utils::Ipv4Address,
|
||||||
ipv4ReqHeader.sourceIp,
|
ipv4ReqHeader.sourceIp,
|
||||||
udpRespHeader.length + Ipv4Header::SerializedLength()
|
udpRespHeader.length + Ipv4Header::SerializedLength()
|
||||||
);
|
);
|
||||||
EthernetFrameHeader ethernetRespHeader(
|
Net::Ethernet::EthernetFrameHeader ethernetRespHeader(
|
||||||
Net::Arp::ArpTable[ipv4RespHeader.destinationIp],
|
Net::Arp::ArpTable[ipv4RespHeader.destinationIp],
|
||||||
GetMacAddress(),
|
Net::Utils::GetMacAddress(),
|
||||||
ETHERTYPE_IPV4
|
Net::Ethernet::ETHERTYPE_IPV4
|
||||||
);
|
);
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
|
@ -2,10 +2,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "net-udp.h"
|
||||||
struct EthernetFrameHeader;
|
|
||||||
struct Ipv4Header;
|
|
||||||
struct UdpDatagramHeader;
|
|
||||||
|
|
||||||
namespace Net::Tftp {
|
namespace Net::Tftp {
|
||||||
const size_t TFTP_BLOCK_SIZE = 512;
|
const size_t TFTP_BLOCK_SIZE = 512;
|
||||||
|
@ -76,7 +73,7 @@ namespace Net::Tftp {
|
||||||
};
|
};
|
||||||
|
|
||||||
void HandlePacket(
|
void HandlePacket(
|
||||||
const EthernetFrameHeader ethernetReqHeader,
|
const Net::Ethernet::EthernetFrameHeader ethernetReqHeader,
|
||||||
const Ipv4Header ipv4ReqHeader,
|
const Ipv4Header ipv4ReqHeader,
|
||||||
const UdpDatagramHeader udpReqHeader,
|
const UdpDatagramHeader udpReqHeader,
|
||||||
const uint8_t* buffer
|
const uint8_t* buffer
|
||||||
|
|
122
src/net-utils.cpp
Normal file
122
src/net-utils.cpp
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
#include "net-utils.h"
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include <uspi.h>
|
||||||
|
|
||||||
|
namespace Net::Utils
|
||||||
|
{
|
||||||
|
const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||||
|
uint32_t Ipv4Address = 0xC0A80164;
|
||||||
|
|
||||||
|
static const uint32_t crcTab32[256] = {
|
||||||
|
0x00000000ul, 0x77073096ul, 0xEE0E612Cul, 0x990951BAul,
|
||||||
|
0x076DC419ul, 0x706AF48Ful, 0xE963A535ul, 0x9E6495A3ul,
|
||||||
|
0x0EDB8832ul, 0x79DCB8A4ul, 0xE0D5E91Eul, 0x97D2D988ul,
|
||||||
|
0x09B64C2Bul, 0x7EB17CBDul, 0xE7B82D07ul, 0x90BF1D91ul,
|
||||||
|
0x1DB71064ul, 0x6AB020F2ul, 0xF3B97148ul, 0x84BE41DEul,
|
||||||
|
0x1ADAD47Dul, 0x6DDDE4EBul, 0xF4D4B551ul, 0x83D385C7ul,
|
||||||
|
0x136C9856ul, 0x646BA8C0ul, 0xFD62F97Aul, 0x8A65C9ECul,
|
||||||
|
0x14015C4Ful, 0x63066CD9ul, 0xFA0F3D63ul, 0x8D080DF5ul,
|
||||||
|
0x3B6E20C8ul, 0x4C69105Eul, 0xD56041E4ul, 0xA2677172ul,
|
||||||
|
0x3C03E4D1ul, 0x4B04D447ul, 0xD20D85FDul, 0xA50AB56Bul,
|
||||||
|
0x35B5A8FAul, 0x42B2986Cul, 0xDBBBC9D6ul, 0xACBCF940ul,
|
||||||
|
0x32D86CE3ul, 0x45DF5C75ul, 0xDCD60DCFul, 0xABD13D59ul,
|
||||||
|
0x26D930ACul, 0x51DE003Aul, 0xC8D75180ul, 0xBFD06116ul,
|
||||||
|
0x21B4F4B5ul, 0x56B3C423ul, 0xCFBA9599ul, 0xB8BDA50Ful,
|
||||||
|
0x2802B89Eul, 0x5F058808ul, 0xC60CD9B2ul, 0xB10BE924ul,
|
||||||
|
0x2F6F7C87ul, 0x58684C11ul, 0xC1611DABul, 0xB6662D3Dul,
|
||||||
|
0x76DC4190ul, 0x01DB7106ul, 0x98D220BCul, 0xEFD5102Aul,
|
||||||
|
0x71B18589ul, 0x06B6B51Ful, 0x9FBFE4A5ul, 0xE8B8D433ul,
|
||||||
|
0x7807C9A2ul, 0x0F00F934ul, 0x9609A88Eul, 0xE10E9818ul,
|
||||||
|
0x7F6A0DBBul, 0x086D3D2Dul, 0x91646C97ul, 0xE6635C01ul,
|
||||||
|
0x6B6B51F4ul, 0x1C6C6162ul, 0x856530D8ul, 0xF262004Eul,
|
||||||
|
0x6C0695EDul, 0x1B01A57Bul, 0x8208F4C1ul, 0xF50FC457ul,
|
||||||
|
0x65B0D9C6ul, 0x12B7E950ul, 0x8BBEB8EAul, 0xFCB9887Cul,
|
||||||
|
0x62DD1DDFul, 0x15DA2D49ul, 0x8CD37CF3ul, 0xFBD44C65ul,
|
||||||
|
0x4DB26158ul, 0x3AB551CEul, 0xA3BC0074ul, 0xD4BB30E2ul,
|
||||||
|
0x4ADFA541ul, 0x3DD895D7ul, 0xA4D1C46Dul, 0xD3D6F4FBul,
|
||||||
|
0x4369E96Aul, 0x346ED9FCul, 0xAD678846ul, 0xDA60B8D0ul,
|
||||||
|
0x44042D73ul, 0x33031DE5ul, 0xAA0A4C5Ful, 0xDD0D7CC9ul,
|
||||||
|
0x5005713Cul, 0x270241AAul, 0xBE0B1010ul, 0xC90C2086ul,
|
||||||
|
0x5768B525ul, 0x206F85B3ul, 0xB966D409ul, 0xCE61E49Ful,
|
||||||
|
0x5EDEF90Eul, 0x29D9C998ul, 0xB0D09822ul, 0xC7D7A8B4ul,
|
||||||
|
0x59B33D17ul, 0x2EB40D81ul, 0xB7BD5C3Bul, 0xC0BA6CADul,
|
||||||
|
0xEDB88320ul, 0x9ABFB3B6ul, 0x03B6E20Cul, 0x74B1D29Aul,
|
||||||
|
0xEAD54739ul, 0x9DD277AFul, 0x04DB2615ul, 0x73DC1683ul,
|
||||||
|
0xE3630B12ul, 0x94643B84ul, 0x0D6D6A3Eul, 0x7A6A5AA8ul,
|
||||||
|
0xE40ECF0Bul, 0x9309FF9Dul, 0x0A00AE27ul, 0x7D079EB1ul,
|
||||||
|
0xF00F9344ul, 0x8708A3D2ul, 0x1E01F268ul, 0x6906C2FEul,
|
||||||
|
0xF762575Dul, 0x806567CBul, 0x196C3671ul, 0x6E6B06E7ul,
|
||||||
|
0xFED41B76ul, 0x89D32BE0ul, 0x10DA7A5Aul, 0x67DD4ACCul,
|
||||||
|
0xF9B9DF6Ful, 0x8EBEEFF9ul, 0x17B7BE43ul, 0x60B08ED5ul,
|
||||||
|
0xD6D6A3E8ul, 0xA1D1937Eul, 0x38D8C2C4ul, 0x4FDFF252ul,
|
||||||
|
0xD1BB67F1ul, 0xA6BC5767ul, 0x3FB506DDul, 0x48B2364Bul,
|
||||||
|
0xD80D2BDAul, 0xAF0A1B4Cul, 0x36034AF6ul, 0x41047A60ul,
|
||||||
|
0xDF60EFC3ul, 0xA867DF55ul, 0x316E8EEFul, 0x4669BE79ul,
|
||||||
|
0xCB61B38Cul, 0xBC66831Aul, 0x256FD2A0ul, 0x5268E236ul,
|
||||||
|
0xCC0C7795ul, 0xBB0B4703ul, 0x220216B9ul, 0x5505262Ful,
|
||||||
|
0xC5BA3BBEul, 0xB2BD0B28ul, 0x2BB45A92ul, 0x5CB36A04ul,
|
||||||
|
0xC2D7FFA7ul, 0xB5D0CF31ul, 0x2CD99E8Bul, 0x5BDEAE1Dul,
|
||||||
|
0x9B64C2B0ul, 0xEC63F226ul, 0x756AA39Cul, 0x026D930Aul,
|
||||||
|
0x9C0906A9ul, 0xEB0E363Ful, 0x72076785ul, 0x05005713ul,
|
||||||
|
0x95BF4A82ul, 0xE2B87A14ul, 0x7BB12BAEul, 0x0CB61B38ul,
|
||||||
|
0x92D28E9Bul, 0xE5D5BE0Dul, 0x7CDCEFB7ul, 0x0BDBDF21ul,
|
||||||
|
0x86D3D2D4ul, 0xF1D4E242ul, 0x68DDB3F8ul, 0x1FDA836Eul,
|
||||||
|
0x81BE16CDul, 0xF6B9265Bul, 0x6FB077E1ul, 0x18B74777ul,
|
||||||
|
0x88085AE6ul, 0xFF0F6A70ul, 0x66063BCAul, 0x11010B5Cul,
|
||||||
|
0x8F659EFFul, 0xF862AE69ul, 0x616BFFD3ul, 0x166CCF45ul,
|
||||||
|
0xA00AE278ul, 0xD70DD2EEul, 0x4E048354ul, 0x3903B3C2ul,
|
||||||
|
0xA7672661ul, 0xD06016F7ul, 0x4969474Dul, 0x3E6E77DBul,
|
||||||
|
0xAED16A4Aul, 0xD9D65ADCul, 0x40DF0B66ul, 0x37D83BF0ul,
|
||||||
|
0xA9BCAE53ul, 0xDEBB9EC5ul, 0x47B2CF7Ful, 0x30B5FFE9ul,
|
||||||
|
0xBDBDF21Cul, 0xCABAC28Aul, 0x53B39330ul, 0x24B4A3A6ul,
|
||||||
|
0xBAD03605ul, 0xCDD70693ul, 0x54DE5729ul, 0x23D967BFul,
|
||||||
|
0xB3667A2Eul, 0xC4614AB8ul, 0x5D681B02ul, 0x2A6F2B94ul,
|
||||||
|
0xB40BBE37ul, 0xC30C8EA1ul, 0x5A05DF1Bul, 0x2D02EF8Dul
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t Crc32(const uint8_t *buffer, size_t size) {
|
||||||
|
uint32_t crc = 0xFFFFFFFFul;
|
||||||
|
|
||||||
|
for (size_t a = 0; a < size; a++)
|
||||||
|
{
|
||||||
|
auto index = (crc ^ (uint32_t)*buffer++) & 0x000000FFul;
|
||||||
|
crc = (crc >> 8) ^ crcTab32[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
return crc ^ 0xFFFFFFFFul;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t InternetChecksum(const void* data, size_t size)
|
||||||
|
{
|
||||||
|
uint32_t sum = 0;
|
||||||
|
while (size > 1) {
|
||||||
|
sum += *(uint16_t*)data;
|
||||||
|
data = (uint16_t*)data + 1;
|
||||||
|
|
||||||
|
size -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size > 0) {
|
||||||
|
sum += *(uint16_t*)data;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (sum >> 16) {
|
||||||
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ~sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
MacAddress GetMacAddress()
|
||||||
|
{
|
||||||
|
static MacAddress macAddress{0};
|
||||||
|
|
||||||
|
if (macAddress == MacAddress{0})
|
||||||
|
{
|
||||||
|
USPiGetMACAddress(macAddress.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
return macAddress;
|
||||||
|
}
|
||||||
|
}; // namespace Net::Utils
|
14
src/net-utils.h
Normal file
14
src/net-utils.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
namespace Net::Utils
|
||||||
|
{
|
||||||
|
typedef std::array<uint8_t, 6> MacAddress;
|
||||||
|
extern const MacAddress MacBroadcast;
|
||||||
|
extern uint32_t Ipv4Address;
|
||||||
|
|
||||||
|
uint32_t Crc32(const uint8_t* buffer, size_t size);
|
||||||
|
uint16_t InternetChecksum(const void* data, size_t size);
|
||||||
|
MacAddress GetMacAddress();
|
||||||
|
};
|
139
src/net.cpp
139
src/net.cpp
|
@ -18,7 +18,9 @@
|
||||||
// IPv4
|
// IPv4
|
||||||
//
|
//
|
||||||
void HandleIpv4Packet(
|
void HandleIpv4Packet(
|
||||||
const EthernetFrameHeader ethernetHeader, const uint8_t* buffer, const size_t size
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
|
const uint8_t* buffer,
|
||||||
|
const size_t size
|
||||||
) {
|
) {
|
||||||
const auto ipv4Header = Ipv4Header::Deserialize(buffer);
|
const auto ipv4Header = Ipv4Header::Deserialize(buffer);
|
||||||
const auto offset = Ipv4Header::SerializedLength();
|
const auto offset = Ipv4Header::SerializedLength();
|
||||||
|
@ -29,7 +31,7 @@ void HandleIpv4Packet(
|
||||||
|
|
||||||
if (ipv4Header.version != 4) return;
|
if (ipv4Header.version != 4) return;
|
||||||
if (ipv4Header.ihl != 5) return; // Not supported
|
if (ipv4Header.ihl != 5) return; // Not supported
|
||||||
if (ipv4Header.destinationIp != Ipv4Address) return;
|
if (ipv4Header.destinationIp != Net::Utils::Ipv4Address) return;
|
||||||
if (ipv4Header.fragmentOffset != 0) return; // TODO Support this
|
if (ipv4Header.fragmentOffset != 0) return; // TODO Support this
|
||||||
|
|
||||||
if (ipv4Header.protocol == IP_PROTO_ICMP)
|
if (ipv4Header.protocol == IP_PROTO_ICMP)
|
||||||
|
@ -46,7 +48,7 @@ void HandleIpv4Packet(
|
||||||
// UDP
|
// UDP
|
||||||
//
|
//
|
||||||
void HandleUdpDatagram(
|
void HandleUdpDatagram(
|
||||||
const EthernetFrameHeader ethernetHeader,
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
const Ipv4Header ipv4Header,
|
const Ipv4Header ipv4Header,
|
||||||
const uint8_t* buffer,
|
const uint8_t* buffer,
|
||||||
const size_t size
|
const size_t size
|
||||||
|
@ -83,9 +85,10 @@ void SendIcmpEchoRequest(MacAddress mac, uint32_t ip)
|
||||||
size_t ipv4TotalSize = IcmpPacketHeader::SerializedLength() +
|
size_t ipv4TotalSize = IcmpPacketHeader::SerializedLength() +
|
||||||
IcmpEchoHeader::SerializedLength() +
|
IcmpEchoHeader::SerializedLength() +
|
||||||
Ipv4Header::SerializedLength();
|
Ipv4Header::SerializedLength();
|
||||||
Ipv4Header ipv4Header(1, Ipv4Address, ip, ipv4TotalSize);
|
Ipv4Header ipv4Header(1, Net::Utils::Ipv4Address, ip, ipv4TotalSize);
|
||||||
|
|
||||||
EthernetFrameHeader ethernetHeader(mac, GetMacAddress(), ETHERTYPE_IPV4);
|
Net::Ethernet::EthernetFrameHeader ethernetHeader(
|
||||||
|
mac, Net::Utils::GetMacAddress(), Net::Ethernet::ETHERTYPE_IPV4);
|
||||||
|
|
||||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
@ -103,7 +106,7 @@ void HandleIcmpFrame(const uint8_t* buffer)
|
||||||
// TODO Don't re-parse the upper layers
|
// TODO Don't re-parse the upper layers
|
||||||
size_t requestSize = 0;
|
size_t requestSize = 0;
|
||||||
const auto requestEthernetHeader =
|
const auto requestEthernetHeader =
|
||||||
EthernetFrameHeader::Deserialize(buffer + requestSize);
|
Net::Ethernet::EthernetFrameHeader::Deserialize(buffer + requestSize);
|
||||||
requestSize += requestEthernetHeader.SerializedLength();
|
requestSize += requestEthernetHeader.SerializedLength();
|
||||||
const auto requestIpv4Header = Ipv4Header::Deserialize(buffer + requestSize);
|
const auto requestIpv4Header = Ipv4Header::Deserialize(buffer + requestSize);
|
||||||
requestSize += requestIpv4Header.SerializedLength();
|
requestSize += requestIpv4Header.SerializedLength();
|
||||||
|
@ -119,14 +122,18 @@ void HandleIcmpFrame(const uint8_t* buffer)
|
||||||
const IcmpPacketHeader responseIcmpHeader(ICMP_ECHO_REPLY, 0);
|
const IcmpPacketHeader responseIcmpHeader(ICMP_ECHO_REPLY, 0);
|
||||||
const Ipv4Header responseIpv4Header(
|
const Ipv4Header responseIpv4Header(
|
||||||
IP_PROTO_ICMP,
|
IP_PROTO_ICMP,
|
||||||
Ipv4Address,
|
Net::Utils::Ipv4Address,
|
||||||
requestIpv4Header.sourceIp,
|
requestIpv4Header.sourceIp,
|
||||||
requestIpv4Header.totalLength
|
requestIpv4Header.totalLength
|
||||||
);
|
);
|
||||||
const EthernetFrameHeader responseEthernetHeader(
|
const Net::Ethernet::EthernetFrameHeader responseEthernetHeader(
|
||||||
requestEthernetHeader.macSource, GetMacAddress(), ETHERTYPE_IPV4);
|
requestEthernetHeader.macSource,
|
||||||
|
Net::Utils::GetMacAddress(),
|
||||||
|
Net::Ethernet::ETHERTYPE_IPV4
|
||||||
|
);
|
||||||
|
|
||||||
const auto payloadLength = requestIpv4Header.totalLength -
|
const auto payloadLength =
|
||||||
|
requestIpv4Header.totalLength -
|
||||||
requestIpv4Header.SerializedLength() -
|
requestIpv4Header.SerializedLength() -
|
||||||
requestIcmpHeader.SerializedLength() -
|
requestIcmpHeader.SerializedLength() -
|
||||||
requestEchoHeader.SerializedLength();
|
requestEchoHeader.SerializedLength();
|
||||||
|
@ -145,119 +152,7 @@ void HandleIcmpFrame(const uint8_t* buffer)
|
||||||
//
|
//
|
||||||
// Helpers
|
// Helpers
|
||||||
//
|
//
|
||||||
static const std::uint32_t crcTab32[256] = {
|
|
||||||
0x00000000ul, 0x77073096ul, 0xEE0E612Cul, 0x990951BAul,
|
|
||||||
0x076DC419ul, 0x706AF48Ful, 0xE963A535ul, 0x9E6495A3ul,
|
|
||||||
0x0EDB8832ul, 0x79DCB8A4ul, 0xE0D5E91Eul, 0x97D2D988ul,
|
|
||||||
0x09B64C2Bul, 0x7EB17CBDul, 0xE7B82D07ul, 0x90BF1D91ul,
|
|
||||||
0x1DB71064ul, 0x6AB020F2ul, 0xF3B97148ul, 0x84BE41DEul,
|
|
||||||
0x1ADAD47Dul, 0x6DDDE4EBul, 0xF4D4B551ul, 0x83D385C7ul,
|
|
||||||
0x136C9856ul, 0x646BA8C0ul, 0xFD62F97Aul, 0x8A65C9ECul,
|
|
||||||
0x14015C4Ful, 0x63066CD9ul, 0xFA0F3D63ul, 0x8D080DF5ul,
|
|
||||||
0x3B6E20C8ul, 0x4C69105Eul, 0xD56041E4ul, 0xA2677172ul,
|
|
||||||
0x3C03E4D1ul, 0x4B04D447ul, 0xD20D85FDul, 0xA50AB56Bul,
|
|
||||||
0x35B5A8FAul, 0x42B2986Cul, 0xDBBBC9D6ul, 0xACBCF940ul,
|
|
||||||
0x32D86CE3ul, 0x45DF5C75ul, 0xDCD60DCFul, 0xABD13D59ul,
|
|
||||||
0x26D930ACul, 0x51DE003Aul, 0xC8D75180ul, 0xBFD06116ul,
|
|
||||||
0x21B4F4B5ul, 0x56B3C423ul, 0xCFBA9599ul, 0xB8BDA50Ful,
|
|
||||||
0x2802B89Eul, 0x5F058808ul, 0xC60CD9B2ul, 0xB10BE924ul,
|
|
||||||
0x2F6F7C87ul, 0x58684C11ul, 0xC1611DABul, 0xB6662D3Dul,
|
|
||||||
0x76DC4190ul, 0x01DB7106ul, 0x98D220BCul, 0xEFD5102Aul,
|
|
||||||
0x71B18589ul, 0x06B6B51Ful, 0x9FBFE4A5ul, 0xE8B8D433ul,
|
|
||||||
0x7807C9A2ul, 0x0F00F934ul, 0x9609A88Eul, 0xE10E9818ul,
|
|
||||||
0x7F6A0DBBul, 0x086D3D2Dul, 0x91646C97ul, 0xE6635C01ul,
|
|
||||||
0x6B6B51F4ul, 0x1C6C6162ul, 0x856530D8ul, 0xF262004Eul,
|
|
||||||
0x6C0695EDul, 0x1B01A57Bul, 0x8208F4C1ul, 0xF50FC457ul,
|
|
||||||
0x65B0D9C6ul, 0x12B7E950ul, 0x8BBEB8EAul, 0xFCB9887Cul,
|
|
||||||
0x62DD1DDFul, 0x15DA2D49ul, 0x8CD37CF3ul, 0xFBD44C65ul,
|
|
||||||
0x4DB26158ul, 0x3AB551CEul, 0xA3BC0074ul, 0xD4BB30E2ul,
|
|
||||||
0x4ADFA541ul, 0x3DD895D7ul, 0xA4D1C46Dul, 0xD3D6F4FBul,
|
|
||||||
0x4369E96Aul, 0x346ED9FCul, 0xAD678846ul, 0xDA60B8D0ul,
|
|
||||||
0x44042D73ul, 0x33031DE5ul, 0xAA0A4C5Ful, 0xDD0D7CC9ul,
|
|
||||||
0x5005713Cul, 0x270241AAul, 0xBE0B1010ul, 0xC90C2086ul,
|
|
||||||
0x5768B525ul, 0x206F85B3ul, 0xB966D409ul, 0xCE61E49Ful,
|
|
||||||
0x5EDEF90Eul, 0x29D9C998ul, 0xB0D09822ul, 0xC7D7A8B4ul,
|
|
||||||
0x59B33D17ul, 0x2EB40D81ul, 0xB7BD5C3Bul, 0xC0BA6CADul,
|
|
||||||
0xEDB88320ul, 0x9ABFB3B6ul, 0x03B6E20Cul, 0x74B1D29Aul,
|
|
||||||
0xEAD54739ul, 0x9DD277AFul, 0x04DB2615ul, 0x73DC1683ul,
|
|
||||||
0xE3630B12ul, 0x94643B84ul, 0x0D6D6A3Eul, 0x7A6A5AA8ul,
|
|
||||||
0xE40ECF0Bul, 0x9309FF9Dul, 0x0A00AE27ul, 0x7D079EB1ul,
|
|
||||||
0xF00F9344ul, 0x8708A3D2ul, 0x1E01F268ul, 0x6906C2FEul,
|
|
||||||
0xF762575Dul, 0x806567CBul, 0x196C3671ul, 0x6E6B06E7ul,
|
|
||||||
0xFED41B76ul, 0x89D32BE0ul, 0x10DA7A5Aul, 0x67DD4ACCul,
|
|
||||||
0xF9B9DF6Ful, 0x8EBEEFF9ul, 0x17B7BE43ul, 0x60B08ED5ul,
|
|
||||||
0xD6D6A3E8ul, 0xA1D1937Eul, 0x38D8C2C4ul, 0x4FDFF252ul,
|
|
||||||
0xD1BB67F1ul, 0xA6BC5767ul, 0x3FB506DDul, 0x48B2364Bul,
|
|
||||||
0xD80D2BDAul, 0xAF0A1B4Cul, 0x36034AF6ul, 0x41047A60ul,
|
|
||||||
0xDF60EFC3ul, 0xA867DF55ul, 0x316E8EEFul, 0x4669BE79ul,
|
|
||||||
0xCB61B38Cul, 0xBC66831Aul, 0x256FD2A0ul, 0x5268E236ul,
|
|
||||||
0xCC0C7795ul, 0xBB0B4703ul, 0x220216B9ul, 0x5505262Ful,
|
|
||||||
0xC5BA3BBEul, 0xB2BD0B28ul, 0x2BB45A92ul, 0x5CB36A04ul,
|
|
||||||
0xC2D7FFA7ul, 0xB5D0CF31ul, 0x2CD99E8Bul, 0x5BDEAE1Dul,
|
|
||||||
0x9B64C2B0ul, 0xEC63F226ul, 0x756AA39Cul, 0x026D930Aul,
|
|
||||||
0x9C0906A9ul, 0xEB0E363Ful, 0x72076785ul, 0x05005713ul,
|
|
||||||
0x95BF4A82ul, 0xE2B87A14ul, 0x7BB12BAEul, 0x0CB61B38ul,
|
|
||||||
0x92D28E9Bul, 0xE5D5BE0Dul, 0x7CDCEFB7ul, 0x0BDBDF21ul,
|
|
||||||
0x86D3D2D4ul, 0xF1D4E242ul, 0x68DDB3F8ul, 0x1FDA836Eul,
|
|
||||||
0x81BE16CDul, 0xF6B9265Bul, 0x6FB077E1ul, 0x18B74777ul,
|
|
||||||
0x88085AE6ul, 0xFF0F6A70ul, 0x66063BCAul, 0x11010B5Cul,
|
|
||||||
0x8F659EFFul, 0xF862AE69ul, 0x616BFFD3ul, 0x166CCF45ul,
|
|
||||||
0xA00AE278ul, 0xD70DD2EEul, 0x4E048354ul, 0x3903B3C2ul,
|
|
||||||
0xA7672661ul, 0xD06016F7ul, 0x4969474Dul, 0x3E6E77DBul,
|
|
||||||
0xAED16A4Aul, 0xD9D65ADCul, 0x40DF0B66ul, 0x37D83BF0ul,
|
|
||||||
0xA9BCAE53ul, 0xDEBB9EC5ul, 0x47B2CF7Ful, 0x30B5FFE9ul,
|
|
||||||
0xBDBDF21Cul, 0xCABAC28Aul, 0x53B39330ul, 0x24B4A3A6ul,
|
|
||||||
0xBAD03605ul, 0xCDD70693ul, 0x54DE5729ul, 0x23D967BFul,
|
|
||||||
0xB3667A2Eul, 0xC4614AB8ul, 0x5D681B02ul, 0x2A6F2B94ul,
|
|
||||||
0xB40BBE37ul, 0xC30C8EA1ul, 0x5A05DF1Bul, 0x2D02EF8Dul
|
|
||||||
};
|
|
||||||
|
|
||||||
std::uint32_t Crc32(const std::uint8_t *buffer, std::size_t size) {
|
|
||||||
std::uint32_t crc = 0xFFFFFFFFul;
|
|
||||||
|
|
||||||
for (std::size_t a = 0; a < size; a++)
|
|
||||||
{
|
|
||||||
auto index = (crc ^ (std::uint32_t)*buffer++) & 0x000000FFul;
|
|
||||||
crc = (crc >> 8) ^ crcTab32[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
return crc ^ 0xFFFFFFFFul;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint16_t InternetChecksum(const void* data, std::size_t size)
|
|
||||||
{
|
|
||||||
std::uint32_t sum = 0;
|
|
||||||
while (size > 1) {
|
|
||||||
sum += *(std::uint16_t*)data;
|
|
||||||
data = (std::uint16_t*)data + 1;
|
|
||||||
|
|
||||||
size -= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > 0) {
|
|
||||||
sum += *(std::uint16_t*)data;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (sum >> 16) {
|
|
||||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ~sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
MacAddress GetMacAddress()
|
|
||||||
{
|
|
||||||
static MacAddress macAddress{0};
|
|
||||||
|
|
||||||
if (macAddress == MacAddress{0})
|
|
||||||
{
|
|
||||||
USPiGetMACAddress(macAddress.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
return macAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t Ipv4Address = 0xC0A80164;
|
|
||||||
const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
||||||
|
|
||||||
bool FileUploaded = false;
|
bool FileUploaded = false;
|
||||||
|
|
32
src/net.h
32
src/net.h
|
@ -5,15 +5,10 @@
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
enum EtherType {
|
#include "net-arp.h"
|
||||||
ETHERTYPE_IPV4 = 0x0800,
|
#include "net-ethernet.h"
|
||||||
ETHERTYPE_ARP = 0x0806,
|
#include "net-ipv4.h"
|
||||||
};
|
#include "net-utils.h"
|
||||||
|
|
||||||
enum ArpOperation {
|
|
||||||
ARP_OPERATION_REQUEST = 1,
|
|
||||||
ARP_OPERATION_REPLY = 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum UdpPort {
|
enum UdpPort {
|
||||||
UDP_PORT_DHCP_SERVER = 67,
|
UDP_PORT_DHCP_SERVER = 67,
|
||||||
|
@ -21,23 +16,20 @@ enum UdpPort {
|
||||||
UDP_PORT_TFTP = 69, // nice
|
UDP_PORT_TFTP = 69, // nice
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::array<uint8_t, 6> MacAddress;
|
|
||||||
|
|
||||||
struct EthernetFrameHeader;
|
|
||||||
struct UdpDatagramHeader;
|
|
||||||
struct Ipv4Header;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// IPv4
|
// IPv4
|
||||||
//
|
//
|
||||||
void HandleIpv4Packet(
|
void HandleIpv4Packet(
|
||||||
const EthernetFrameHeader ethernetHeader, const uint8_t* buffer, const size_t size);
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
|
const uint8_t* buffer,
|
||||||
|
const size_t size
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// UDP
|
// UDP
|
||||||
//
|
//
|
||||||
void HandleUdpDatagram(
|
void HandleUdpDatagram(
|
||||||
const EthernetFrameHeader ethernetHeader,
|
const Net::Ethernet::EthernetFrameHeader ethernetHeader,
|
||||||
const Ipv4Header ipv4Header,
|
const Ipv4Header ipv4Header,
|
||||||
const uint8_t* buffer,
|
const uint8_t* buffer,
|
||||||
const size_t size
|
const size_t size
|
||||||
|
@ -52,11 +44,5 @@ void HandleIcmpFrame(const uint8_t* buffer);
|
||||||
//
|
//
|
||||||
// Helpers
|
// Helpers
|
||||||
//
|
//
|
||||||
std::uint32_t Crc32(const std::uint8_t* buffer, std::size_t size);
|
|
||||||
std::uint16_t InternetChecksum(const void* data, std::size_t size);
|
|
||||||
MacAddress GetMacAddress();
|
|
||||||
|
|
||||||
extern const MacAddress MacBroadcast;
|
|
||||||
extern uint32_t Ipv4Address;
|
|
||||||
|
|
||||||
extern bool FileUploaded;
|
extern bool FileUploaded;
|
||||||
|
|
Loading…
Reference in a new issue