Un-nest namespaces, because that's a C++17 feature

For some reason gcc doesn't tell us this
This commit is contained in:
Sijmen 2021-01-18 15:56:36 +01:00
parent 8275e5122d
commit 6e6feee88c
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
15 changed files with 1473 additions and 1414 deletions

View file

@ -8,163 +8,168 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Arp namespace Net
{ {
Packet::Packet() {} namespace Arp
Packet::Packet(const uint16_t operation) :
hardwareType(1), // Ethernet
protocolType(Ethernet::EtherType::Ipv4),
hardwareAddressLength(6),
protocolAddressLength(4),
operation(operation)
{ {
} Packet::Packet() {}
size_t Packet::Serialize(uint8_t* buffer, const size_t bufferSize) const Packet::Packet(const uint16_t operation) :
{ hardwareType(1), // Ethernet
if (bufferSize < SerializedLength()) protocolType(Ethernet::EtherType::Ipv4),
hardwareAddressLength(6),
protocolAddressLength(4),
operation(operation)
{ {
return 0;
} }
buffer[0] = hardwareType >> 8; size_t Packet::Serialize(uint8_t* buffer, const size_t bufferSize) const
buffer[1] = hardwareType;
buffer[2] = static_cast<uint16_t>(protocolType) >> 8;
buffer[3] = static_cast<uint16_t>(protocolType);
buffer[4] = hardwareAddressLength;
buffer[5] = protocolAddressLength;
buffer[6] = operation >> 8;
buffer[7] = operation;
memcpy(buffer + 8, senderMac.data(), 6);
buffer[14] = senderIp >> 24;
buffer[15] = senderIp >> 16;
buffer[16] = senderIp >> 8;
buffer[17] = senderIp;
memcpy(buffer + 18, targetMac.data(), 6);
buffer[24] = targetIp >> 24;
buffer[25] = targetIp >> 16;
buffer[26] = targetIp >> 8;
buffer[27] = targetIp;
return 28;
}
// Static
size_t Packet::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
buffer[0] = hardwareType >> 8;
buffer[1] = hardwareType;
buffer[2] = static_cast<uint16_t>(protocolType) >> 8;
buffer[3] = static_cast<uint16_t>(protocolType);
buffer[4] = hardwareAddressLength;
buffer[5] = protocolAddressLength;
buffer[6] = operation >> 8;
buffer[7] = operation;
memcpy(buffer + 8, senderMac.data(), 6);
buffer[14] = senderIp >> 24;
buffer[15] = senderIp >> 16;
buffer[16] = senderIp >> 8;
buffer[17] = senderIp;
memcpy(buffer + 18, targetMac.data(), 6);
buffer[24] = targetIp >> 24;
buffer[25] = targetIp >> 16;
buffer[26] = targetIp >> 8;
buffer[27] = targetIp;
return 28;
} }
hardwareType = buffer[0] << 8 | buffer[1]; // Static
protocolType = static_cast<Ethernet::EtherType>(buffer[2] << 8 | buffer[3]); size_t Packet::Deserialize(const uint8_t* buffer, const size_t bufferSize)
hardwareAddressLength = buffer[4];
protocolAddressLength = buffer[5];
operation = buffer[6] << 8 | buffer[7];
memcpy(senderMac.data(), buffer + 8, 6);
senderIp = buffer[14] << 24 | buffer[15] << 16 | buffer[16] << 8 | buffer[17];
memcpy(targetMac.data(), buffer + 18, 6);
targetIp = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
return 28;
}
void SendPacket(
const Operation operation,
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{
Packet arpPacket(operation);
arpPacket.targetMac = targetMac;
arpPacket.senderMac = senderMac;
arpPacket.targetIp = targetIp;
arpPacket.senderIp = senderIp;
Ethernet::Header ethernetHeader(targetMac, senderMac, Ethernet::EtherType::Arp);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += arpPacket.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() + arpPacket.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
void SendRequest(
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{
SendPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp);
}
void SendReply(
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{
SendPacket(ARP_OPERATION_REPLY, targetMac, senderMac, targetIp, senderIp);
}
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip)
{
SendReply(Utils::MacBroadcast, mac, ip, ip);
}
void HandlePacket(
const Ethernet::Header ethernetHeader, const uint8_t* buffer, const size_t bufferSize)
{
const auto macAddress = Utils::GetMacAddress();
Packet arpPacket;
const auto arpSize = arpPacket.Deserialize(buffer, bufferSize);
if (arpSize == 0)
{ {
DEBUG_LOG( if (bufferSize < SerializedLength())
"Dropped ARP packet (invalid buffer size %u, expected %u)\r\n", {
bufferSize, return 0;
arpPacket.SerializedLength()); }
return;
hardwareType = buffer[0] << 8 | buffer[1];
protocolType = static_cast<Ethernet::EtherType>(buffer[2] << 8 | buffer[3]);
hardwareAddressLength = buffer[4];
protocolAddressLength = buffer[5];
operation = buffer[6] << 8 | buffer[7];
memcpy(senderMac.data(), buffer + 8, 6);
senderIp = buffer[14] << 24 | buffer[15] << 16 | buffer[16] << 8 | buffer[17];
memcpy(targetMac.data(), buffer + 18, 6);
targetIp = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
return 28;
} }
if (arpPacket.hardwareType != 1 || arpPacket.protocolType != Ethernet::EtherType::Ipv4 || void SendPacket(
arpPacket.targetIp != Utils::Ipv4Address) const Operation operation,
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{ {
// Might want to disable because of spamminess Packet arpPacket(operation);
DEBUG_LOG("Dropped ARP packet (invalid parameters)\r\n"); arpPacket.targetMac = targetMac;
return; arpPacket.senderMac = senderMac;
arpPacket.targetIp = targetIp;
arpPacket.senderIp = senderIp;
Ethernet::Header ethernetHeader(targetMac, senderMac, Ethernet::EtherType::Arp);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += arpPacket.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize =
ethernetHeader.SerializedLength() + arpPacket.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
} }
switch (arpPacket.operation) void SendRequest(
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{ {
case ARP_OPERATION_REQUEST: SendPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp);
SendReply(arpPacket.senderMac, macAddress, arpPacket.senderIp, Utils::Ipv4Address);
break;
case ARP_OPERATION_REPLY:
ArpTable.insert(std::make_pair(arpPacket.senderIp, arpPacket.senderMac));
break;
default:
DEBUG_LOG("Dropped ARP packet (invalid operation %d)\r\n", arpPacket.operation);
break;
} }
}
std::unordered_map<uint32_t, Utils::MacAddress> ArpTable; void SendReply(
} // namespace Net::Arp const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp)
{
SendPacket(ARP_OPERATION_REPLY, targetMac, senderMac, targetIp, senderIp);
}
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip)
{
SendReply(Utils::MacBroadcast, mac, ip, ip);
}
void HandlePacket(
const Ethernet::Header ethernetHeader, const uint8_t* buffer, const size_t bufferSize)
{
const auto macAddress = Utils::GetMacAddress();
Packet arpPacket;
const auto arpSize = arpPacket.Deserialize(buffer, bufferSize);
if (arpSize == 0)
{
DEBUG_LOG(
"Dropped ARP packet (invalid buffer size %u, expected %u)\r\n",
bufferSize,
arpPacket.SerializedLength());
return;
}
if (arpPacket.hardwareType != 1 ||
arpPacket.protocolType != Ethernet::EtherType::Ipv4 ||
arpPacket.targetIp != Utils::Ipv4Address)
{
// Might want to disable because of spamminess
DEBUG_LOG("Dropped ARP packet (invalid parameters)\r\n");
return;
}
switch (arpPacket.operation)
{
case ARP_OPERATION_REQUEST:
SendReply(arpPacket.senderMac, macAddress, arpPacket.senderIp, Utils::Ipv4Address);
break;
case ARP_OPERATION_REPLY:
ArpTable.insert(std::make_pair(arpPacket.senderIp, arpPacket.senderMac));
break;
default:
DEBUG_LOG("Dropped ARP packet (invalid operation %d)\r\n", arpPacket.operation);
break;
}
}
std::unordered_map<uint32_t, Utils::MacAddress> ArpTable;
} // namespace Arp
} // namespace Net

View file

@ -4,64 +4,67 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-utils.h" #include "net-utils.h"
namespace Net::Arp namespace Net
{ {
enum Operation namespace Arp
{ {
ARP_OPERATION_REQUEST = 1, enum Operation
ARP_OPERATION_REPLY = 2,
};
struct Packet
{
uint16_t hardwareType;
Ethernet::EtherType protocolType;
uint8_t hardwareAddressLength;
uint8_t protocolAddressLength;
uint16_t operation;
Utils::MacAddress senderMac;
uint32_t senderIp;
Utils::MacAddress targetMac;
uint32_t targetIp;
Packet();
Packet(const uint16_t operation);
constexpr static size_t SerializedLength()
{ {
return sizeof(hardwareType) + sizeof(protocolType) + sizeof(hardwareAddressLength) + ARP_OPERATION_REQUEST = 1,
sizeof(protocolAddressLength) + sizeof(operation) + sizeof(senderMac) + ARP_OPERATION_REPLY = 2,
sizeof(senderIp) + sizeof(targetMac) + sizeof(targetIp); };
}
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; struct Packet
size_t Deserialize(const uint8_t* buffer, const size_t bufferSize); {
}; uint16_t hardwareType;
Ethernet::EtherType protocolType;
uint8_t hardwareAddressLength;
uint8_t protocolAddressLength;
uint16_t operation;
void Utils::MacAddress senderMac;
HandlePacket(const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize); uint32_t senderIp;
Utils::MacAddress targetMac;
uint32_t targetIp;
void SendPacket( Packet();
const Operation operation, Packet(const uint16_t operation);
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp);
void SendRequest( constexpr static size_t SerializedLength()
const Utils::MacAddress targetMac, {
const Utils::MacAddress senderMac, return sizeof(hardwareType) + sizeof(protocolType) + sizeof(hardwareAddressLength) +
const uint32_t targetIp, sizeof(protocolAddressLength) + sizeof(operation) + sizeof(senderMac) +
const uint32_t senderIp); sizeof(senderIp) + sizeof(targetMac) + sizeof(targetIp);
}
void SendReply( size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
const Utils::MacAddress targetMac, size_t Deserialize(const uint8_t* buffer, const size_t bufferSize);
const Utils::MacAddress senderMac, };
const uint32_t targetIp,
const uint32_t senderIp);
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip); void
HandlePacket(const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize);
extern std::unordered_map<uint32_t, Utils::MacAddress> ArpTable; void SendPacket(
} // namespace Net::Arp const Operation operation,
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp);
void SendRequest(
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp);
void SendReply(
const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac,
const uint32_t targetIp,
const uint32_t senderIp);
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip);
extern std::unordered_map<uint32_t, Utils::MacAddress> ArpTable;
} // namespace Arp
} // namespace Net

View file

@ -11,257 +11,265 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Dhcp namespace Net
{ {
Header::Header() {} namespace Dhcp
Header::Header(Opcode opcode, uint32_t transactionId) :
opcode(opcode),
hardwareAddressType(1), // Ethernet
hops(0),
transactionId(transactionId),
secondsElapsed(0),
flags(0), // TODO assumption
clientIpAddress(0),
yourIpAddress(0),
serverIpAddress(0),
relayIpAddress(0),
clientHardwareAddress{0},
serverHostname{0},
bootFile{0},
magicValue{99, 130, 83, 99}
{ {
const auto mac = Utils::GetMacAddress(); Header::Header() {}
hardwareAddressLength = mac.size();
std::memcpy(clientHardwareAddress.data(), mac.data(), mac.size());
}
size_t Header::Serialize(uint8_t* buffer, const size_t size) const Header::Header(Opcode opcode, uint32_t transactionId) :
{ opcode(opcode),
if (size < Header::SerializedLength()) hardwareAddressType(1), // Ethernet
hops(0),
transactionId(transactionId),
secondsElapsed(0),
flags(0), // TODO assumption
clientIpAddress(0),
yourIpAddress(0),
serverIpAddress(0),
relayIpAddress(0),
clientHardwareAddress{0},
serverHostname{0},
bootFile{0},
magicValue{99, 130, 83, 99}
{ {
return 0; const auto mac = Utils::GetMacAddress();
hardwareAddressLength = mac.size();
std::memcpy(clientHardwareAddress.data(), mac.data(), mac.size());
} }
size_t i = 0; size_t Header::Serialize(uint8_t* buffer, const size_t size) const
buffer[i++] = static_cast<uint8_t>(opcode);
buffer[i++] = hardwareAddressType;
buffer[i++] = hardwareAddressLength;
buffer[i++] = hops;
buffer[i++] = transactionId >> 24;
buffer[i++] = transactionId >> 16;
buffer[i++] = transactionId >> 8;
buffer[i++] = transactionId;
buffer[i++] = secondsElapsed >> 8;
buffer[i++] = secondsElapsed;
buffer[i++] = flags >> 8;
buffer[i++] = flags;
buffer[i++] = clientIpAddress >> 24;
buffer[i++] = clientIpAddress >> 16;
buffer[i++] = clientIpAddress >> 8;
buffer[i++] = clientIpAddress;
buffer[i++] = yourIpAddress >> 24;
buffer[i++] = yourIpAddress >> 16;
buffer[i++] = yourIpAddress >> 8;
buffer[i++] = yourIpAddress;
buffer[i++] = relayIpAddress >> 24;
buffer[i++] = relayIpAddress >> 16;
buffer[i++] = relayIpAddress >> 8;
buffer[i++] = relayIpAddress;
std::memcpy(buffer + i, clientHardwareAddress.data(), clientHardwareAddress.size());
i += clientHardwareAddress.size();
std::memcpy(buffer + i, serverHostname.data(), serverHostname.size());
i += serverHostname.size();
std::memcpy(buffer + i, bootFile.data(), bootFile.size());
i += bootFile.size();
std::memcpy(buffer + i, magicValue.data(), magicValue.size());
i += magicValue.size();
return i;
}
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
{
if (size < SerializedLength())
{ {
return 0; if (size < Header::SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint8_t>(opcode);
buffer[i++] = hardwareAddressType;
buffer[i++] = hardwareAddressLength;
buffer[i++] = hops;
buffer[i++] = transactionId >> 24;
buffer[i++] = transactionId >> 16;
buffer[i++] = transactionId >> 8;
buffer[i++] = transactionId;
buffer[i++] = secondsElapsed >> 8;
buffer[i++] = secondsElapsed;
buffer[i++] = flags >> 8;
buffer[i++] = flags;
buffer[i++] = clientIpAddress >> 24;
buffer[i++] = clientIpAddress >> 16;
buffer[i++] = clientIpAddress >> 8;
buffer[i++] = clientIpAddress;
buffer[i++] = yourIpAddress >> 24;
buffer[i++] = yourIpAddress >> 16;
buffer[i++] = yourIpAddress >> 8;
buffer[i++] = yourIpAddress;
buffer[i++] = relayIpAddress >> 24;
buffer[i++] = relayIpAddress >> 16;
buffer[i++] = relayIpAddress >> 8;
buffer[i++] = relayIpAddress;
std::memcpy(buffer + i, clientHardwareAddress.data(), clientHardwareAddress.size());
i += clientHardwareAddress.size();
std::memcpy(buffer + i, serverHostname.data(), serverHostname.size());
i += serverHostname.size();
std::memcpy(buffer + i, bootFile.data(), bootFile.size());
i += bootFile.size();
std::memcpy(buffer + i, magicValue.data(), magicValue.size());
i += magicValue.size();
return i;
} }
out.opcode = static_cast<Opcode>(buffer[0]); size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
out.hardwareAddressType = buffer[1];
out.hardwareAddressLength = buffer[2];
out.hops = buffer[3];
out.transactionId = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
out.secondsElapsed = buffer[8] << 8 | buffer[9];
out.flags = buffer[10] << 8 | buffer[11];
out.clientIpAddress = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
out.yourIpAddress = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
out.serverIpAddress = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
out.relayIpAddress = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
std::memcpy(
out.clientHardwareAddress.data(), buffer + 28, out.clientHardwareAddress.size());
std::memcpy(out.serverHostname.data(), buffer + 44, out.serverHostname.size());
std::memcpy(out.bootFile.data(), buffer + 108, out.bootFile.size());
std::memcpy(out.magicValue.data(), buffer + 236, out.magicValue.size());
assert(SerializedLength() == 240);
return 240;
}
static uint32_t transactionId;
static std::vector<uint32_t> offeredIpAddresses;
static std::vector<uint32_t> serverIpAddresses;
static std::vector<Utils::MacAddress> serverMacAddresses;
static bool serverSelected;
void sendRequest(
uint32_t clientIpAddress, Utils::MacAddress serverMacAddress, uint32_t serverIpAddress)
{
const Header dhcpHeader(Opcode::BootRequest, transactionId);
size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength();
const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength();
const Ipv4::Header ipv4Header(
Ipv4::Protocol::Udp, clientIpAddress, serverIpAddress, ipv4Length);
const Ethernet::Header ethernetHeader(
serverMacAddress, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
dhcpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
void discoverTimerHandler(unsigned int, void* callbackVoid, void*)
{
if (transactionId == 0 || offeredIpAddresses.empty())
{ {
// TODO retry every minute or so? if (size < SerializedLength())
return; {
return 0;
}
out.opcode = static_cast<Opcode>(buffer[0]);
out.hardwareAddressType = buffer[1];
out.hardwareAddressLength = buffer[2];
out.hops = buffer[3];
out.transactionId = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
out.secondsElapsed = buffer[8] << 8 | buffer[9];
out.flags = buffer[10] << 8 | buffer[11];
out.clientIpAddress =
buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
out.yourIpAddress = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
out.serverIpAddress =
buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
out.relayIpAddress = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
std::memcpy(
out.clientHardwareAddress.data(), buffer + 28, out.clientHardwareAddress.size());
std::memcpy(out.serverHostname.data(), buffer + 44, out.serverHostname.size());
std::memcpy(out.bootFile.data(), buffer + 108, out.bootFile.size());
std::memcpy(out.magicValue.data(), buffer + 236, out.magicValue.size());
assert(SerializedLength() == 240);
return 240;
} }
// Select the first IP address static uint32_t transactionId;
Utils::Ipv4Address = offeredIpAddresses[0]; static std::vector<uint32_t> offeredIpAddresses;
static std::vector<uint32_t> serverIpAddresses;
static std::vector<Utils::MacAddress> serverMacAddresses;
static bool serverSelected;
// Send DHCP Requests to every server with that IP address. void sendRequest(
for (size_t i = 0; i < serverIpAddresses.size(); i++) uint32_t clientIpAddress, Utils::MacAddress serverMacAddress, uint32_t serverIpAddress)
{ {
sendRequest(Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]); const Header dhcpHeader(Opcode::BootRequest, transactionId);
size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength();
const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength();
const Ipv4::Header ipv4Header(
Ipv4::Protocol::Udp, clientIpAddress, serverIpAddress, ipv4Length);
const Ethernet::Header ethernetHeader(
serverMacAddress, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
dhcpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
} }
// Run the callback indicating an IP has been obtained void discoverTimerHandler(unsigned int, void* callbackVoid, void*)
if (callbackVoid != nullptr)
{ {
const auto& callback = *static_cast<std::function<void()>*>(callbackVoid); if (transactionId == 0 || offeredIpAddresses.empty())
callback(); {
} // TODO retry every minute or so?
} return;
}
void sendDiscover() // Select the first IP address
{ Utils::Ipv4Address = offeredIpAddresses[0];
transactionId = std::rand();
offeredIpAddresses.clear();
const Header dhcpHeader(Opcode::BootRequest, transactionId);
size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength(); // Send DHCP Requests to every server with that IP address.
const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength); for (size_t i = 0; i < serverIpAddresses.size(); i++)
{
sendRequest(Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]);
}
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength(); // Run the callback indicating an IP has been obtained
const Ipv4::Header ipv4Header(Ipv4::Protocol::Udp, 0, 0xFFFFFFFF, ipv4Length); if (callbackVoid != nullptr)
const Ethernet::Header ethernetHeader(Utils::GetMacAddress(), Ethernet::EtherType::Ipv4); {
const auto& callback = *static_cast<std::function<void()>*>(callbackVoid);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE]; callback();
size_t size = 0; }
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
dhcpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
void ObtainIp(std::function<void()>& callback)
{
sendDiscover();
// Wait three seconds for responses
const auto callbackVoid = static_cast<void*>(&callback);
StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr);
}
static void handleOfferPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
{
offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
serverMacAddresses.push_back(ethernetHeader.macSource);
}
static void handleAckPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
{
Utils::Ipv4Address = dhcpHeader.yourIpAddress;
// TODO Schedule handler for end of lease.
transactionId = 0;
offeredIpAddresses.clear();
serverIpAddresses.clear();
serverMacAddresses.clear();
serverSelected = false;
}
void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size)
{
Header header;
const auto dhcpSize = Header::Deserialize(header, buffer, size);
if (dhcpSize != Header::SerializedLength())
{
DEBUG_LOG(
"Dropped DHCP packet (invalid buffer size %u, expected %u)\r\n",
size,
Header::SerializedLength());
return;
} }
if (header.opcode != Opcode::BootReply) void sendDiscover()
return; {
if (header.hardwareAddressType != 1) transactionId = std::rand();
return; offeredIpAddresses.clear();
if (header.hardwareAddressLength != 6) const Header dhcpHeader(Opcode::BootRequest, transactionId);
return;
if (header.transactionId != transactionId)
return;
if (!serverSelected) size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength();
{ const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
handleOfferPacket(ethernetHeader, header);
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength();
const Ipv4::Header ipv4Header(Ipv4::Protocol::Udp, 0, 0xFFFFFFFF, ipv4Length);
const Ethernet::Header ethernetHeader(
Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
dhcpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
} }
else
void ObtainIp(std::function<void()>& callback)
{ {
handleAckPacket(ethernetHeader, header); sendDiscover();
// Wait three seconds for responses
// const auto callbackVoid = static_cast<void*>(&callback);
// StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr);
} }
}
} // namespace Net::Dhcp static void
handleOfferPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
{
offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
serverMacAddresses.push_back(ethernetHeader.macSource);
}
static void handleAckPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
{
Utils::Ipv4Address = dhcpHeader.yourIpAddress;
// TODO Schedule handler for end of lease.
transactionId = 0;
offeredIpAddresses.clear();
serverIpAddresses.clear();
serverMacAddresses.clear();
serverSelected = false;
}
void
HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size)
{
Header header;
const auto dhcpSize = Header::Deserialize(header, buffer, size);
if (dhcpSize != Header::SerializedLength())
{
DEBUG_LOG(
"Dropped DHCP packet (invalid buffer size %u, expected %u)\r\n",
size,
Header::SerializedLength());
return;
}
if (header.opcode != Opcode::BootReply)
return;
if (header.hardwareAddressType != 1)
return;
if (header.hardwareAddressLength != 6)
return;
if (header.transactionId != transactionId)
return;
if (!serverSelected)
{
handleOfferPacket(ethernetHeader, header);
}
else
{
handleAckPacket(ethernetHeader, header);
}
}
} // namespace Dhcp
} // namespace Net

View file

@ -4,80 +4,85 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net.h" #include "net.h"
namespace Net::Dhcp namespace Net
{ {
enum class Opcode : uint8_t namespace Dhcp
{ {
BootRequest = 1, enum class Opcode : uint8_t
BootReply = 2,
};
struct Header
{
/// Message op code / message type. 1 = BOOTREQUEST, 2 = BOOTREPLY
Opcode opcode;
/// Hardware address type, see ARP section in "Assigned Numbers" RFC
uint8_t hardwareAddressType;
uint8_t hardwareAddressLength;
/// Client sets to zero, optionally used by relay agents when booting via a
/// relay agent.
uint8_t hops;
/// A random number chosen by the client, used by the client and server to
/// associate messages and responses between a client and a server.
uint32_t transactionId;
/// Filled in by client, seconds elapsed since client began address acquisition
/// or renewal process.
uint16_t secondsElapsed;
uint16_t flags;
/// Only filled in if client is in BOUND, RENEW or REBINDING state and can
/// respond to ARP requests.
uint32_t clientIpAddress;
/// 'your' (client) IP address.
uint32_t yourIpAddress;
/// IP address of next server to use in bootstrap; returned in DHCPOFFER,
/// DHCPACK by server.
uint32_t serverIpAddress;
/// Relay agent IP address, used in booting via a relay agent.
uint32_t relayIpAddress;
std::array<uint8_t, 16> clientHardwareAddress;
/// Optional server host name, null terminated string.
std::array<uint8_t, 64> serverHostname;
/// Boot file name, null terminated string; "generic" name or null in
/// DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
std::array<uint8_t, 128> bootFile;
/// Always 99, 130, 83, 99
std::array<uint8_t, 4> magicValue;
Header();
Header(Opcode opcode, uint32_t transactionId);
constexpr static size_t SerializedLength()
{ {
return sizeof(Opcode) + sizeof(hardwareAddressType) + sizeof(hardwareAddressLength) + BootRequest = 1,
sizeof(hops) + sizeof(transactionId) + sizeof(secondsElapsed) + sizeof(flags) + BootReply = 2,
sizeof(clientIpAddress) + sizeof(yourIpAddress) + sizeof(serverIpAddress) + };
sizeof(relayIpAddress) + sizeof(clientHardwareAddress) + sizeof(serverHostname) +
sizeof(bootFile) + sizeof(magicValue);
}
size_t Serialize(uint8_t* buffer, const size_t size) const; struct Header
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size); {
}; /// Message op code / message type. 1 = BOOTREQUEST, 2 = BOOTREPLY
Opcode opcode;
void ObtainIp(std::function<void()>& callback); /// Hardware address type, see ARP section in "Assigned Numbers" RFC
void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size); uint8_t hardwareAddressType;
} // namespace Net::Dhcp
uint8_t hardwareAddressLength;
/// Client sets to zero, optionally used by relay agents when booting via a
/// relay agent.
uint8_t hops;
/// A random number chosen by the client, used by the client and server to
/// associate messages and responses between a client and a server.
uint32_t transactionId;
/// Filled in by client, seconds elapsed since client began address acquisition
/// or renewal process.
uint16_t secondsElapsed;
uint16_t flags;
/// Only filled in if client is in BOUND, RENEW or REBINDING state and can
/// respond to ARP requests.
uint32_t clientIpAddress;
/// 'your' (client) IP address.
uint32_t yourIpAddress;
/// IP address of next server to use in bootstrap; returned in DHCPOFFER,
/// DHCPACK by server.
uint32_t serverIpAddress;
/// Relay agent IP address, used in booting via a relay agent.
uint32_t relayIpAddress;
std::array<uint8_t, 16> clientHardwareAddress;
/// Optional server host name, null terminated string.
std::array<uint8_t, 64> serverHostname;
/// Boot file name, null terminated string; "generic" name or null in
/// DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
std::array<uint8_t, 128> bootFile;
/// Always 99, 130, 83, 99
std::array<uint8_t, 4> magicValue;
Header();
Header(Opcode opcode, uint32_t transactionId);
constexpr static size_t SerializedLength()
{
return sizeof(Opcode) + sizeof(hardwareAddressType) +
sizeof(hardwareAddressLength) + sizeof(hops) + sizeof(transactionId) +
sizeof(secondsElapsed) + sizeof(flags) + sizeof(clientIpAddress) +
sizeof(yourIpAddress) + sizeof(serverIpAddress) + sizeof(relayIpAddress) +
sizeof(clientHardwareAddress) + sizeof(serverHostname) + sizeof(bootFile) +
sizeof(magicValue);
}
size_t Serialize(uint8_t* buffer, const size_t size) const;
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size);
};
void ObtainIp(std::function<void()>& callback);
void
HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size);
} // namespace Dhcp
} // namespace Net

View file

@ -2,56 +2,59 @@
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ethernet namespace Net
{ {
Header::Header() {} namespace Ethernet
Header::Header(EtherType type) :
macDestination(Utils::MacBroadcast), macSource{0, 0, 0, 0, 0, 0}, type(type)
{ {
} Header::Header() {}
Header::Header(MacAddress macSource, EtherType type) : Header::Header(EtherType type) :
macDestination(Utils::MacBroadcast), macSource(macSource), type(type) macDestination(Utils::MacBroadcast), macSource{0, 0, 0, 0, 0, 0}, type(type)
{
}
Header::Header(MacAddress macDestination, MacAddress macSource, EtherType type) :
macDestination(macDestination), macSource(macSource), type(type)
{
}
size_t Header::Serialize(uint8_t* buffer, const size_t size) const
{
if (size < SerializedLength())
{ {
return 0;
} }
size_t i = 0; Header::Header(MacAddress macSource, EtherType type) :
macDestination(Utils::MacBroadcast), macSource(macSource), type(type)
std::memcpy(buffer + i, macDestination.data(), macDestination.size());
i += sizeof(macDestination);
std::memcpy(buffer + i, macSource.data(), macSource.size());
i += sizeof(macSource);
buffer[i++] = static_cast<uint16_t>(type) >> 8;
buffer[i++] = static_cast<uint16_t>(type);
return i;
}
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
{
if (size < SerializedLength())
{ {
return 0;
} }
std::memcpy(out.macDestination.data(), buffer + 0, out.macDestination.size()); Header::Header(MacAddress macDestination, MacAddress macSource, EtherType type) :
std::memcpy(out.macSource.data(), buffer + 6, out.macSource.size()); macDestination(macDestination), macSource(macSource), type(type)
out.type = static_cast<EtherType>(buffer[12] << 8 | buffer[13]); {
return 14; }
}
} // namespace Net::Ethernet size_t Header::Serialize(uint8_t* buffer, const size_t size) const
{
if (size < SerializedLength())
{
return 0;
}
size_t i = 0;
std::memcpy(buffer + i, macDestination.data(), macDestination.size());
i += sizeof(macDestination);
std::memcpy(buffer + i, macSource.data(), macSource.size());
i += sizeof(macSource);
buffer[i++] = static_cast<uint16_t>(type) >> 8;
buffer[i++] = static_cast<uint16_t>(type);
return i;
}
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
{
if (size < SerializedLength())
{
return 0;
}
std::memcpy(out.macDestination.data(), buffer + 0, out.macDestination.size());
std::memcpy(out.macSource.data(), buffer + 6, out.macSource.size());
out.type = static_cast<EtherType>(buffer[12] << 8 | buffer[13]);
return 14;
}
} // namespace Ethernet
} // namespace Net

View file

@ -3,33 +3,36 @@
#include "net-utils.h" #include "net-utils.h"
namespace Net::Ethernet namespace Net
{ {
using Utils::MacAddress; namespace Ethernet
enum class EtherType : uint16_t
{ {
Ipv4 = 0x0800, using Utils::MacAddress;
Arp = 0x0806,
};
struct Header enum class EtherType : uint16_t
{
MacAddress macDestination;
MacAddress macSource;
EtherType type;
Header();
Header(EtherType type);
Header(MacAddress macSource, EtherType type);
Header(MacAddress macDestination, MacAddress macSource, EtherType type);
constexpr static size_t SerializedLength()
{ {
return sizeof(macDestination) + sizeof(macSource) + sizeof(type); Ipv4 = 0x0800,
} Arp = 0x0806,
};
size_t Serialize(uint8_t* buffer, const size_t size) const; struct Header
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size); {
}; MacAddress macDestination;
} // namespace Net::Ethernet MacAddress macSource;
EtherType type;
Header();
Header(EtherType type);
Header(MacAddress macSource, EtherType type);
Header(MacAddress macDestination, MacAddress macSource, EtherType type);
constexpr static size_t SerializedLength()
{
return sizeof(macDestination) + sizeof(macSource) + sizeof(type);
}
size_t Serialize(uint8_t* buffer, const size_t size) const;
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size);
};
} // namespace Ethernet
} // namespace Net

View file

@ -9,182 +9,189 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Icmp namespace Net
{ {
// namespace Icmp
// Header
//
Header::Header() : type(static_cast<Type>(0)), code(0), checksum(0) {}
Header::Header(Type type, uint8_t code) : type(type), code(code), checksum(0) {}
size_t Header::Serialize(
uint8_t* buffer, const size_t bufferSize, const uint8_t* data, const size_t dataSize) const
{ {
if (bufferSize < SerializedLength()) //
// Header
//
Header::Header() : type(static_cast<Type>(0)), code(0), checksum(0) {}
Header::Header(Type type, uint8_t code) : type(type), code(code), checksum(0) {}
size_t Header::Serialize(
uint8_t* buffer,
const size_t bufferSize,
const uint8_t* data,
const size_t dataSize) const
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint8_t>(type);
buffer[i++] = code;
buffer[i++] = 0;
buffer[i++] = 0;
std::memcpy(buffer + i, data, dataSize);
i += dataSize;
uint16_t checksum = Utils::InternetChecksum(buffer, i);
buffer[2] = checksum >> 8;
buffer[3] = checksum;
return i;
} }
size_t i = 0; size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
buffer[i++] = static_cast<uint8_t>(type);
buffer[i++] = code;
buffer[i++] = 0;
buffer[i++] = 0;
std::memcpy(buffer + i, data, dataSize);
i += dataSize;
uint16_t checksum = Utils::InternetChecksum(buffer, i);
buffer[2] = checksum >> 8;
buffer[3] = checksum;
return i;
}
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
out.type = static_cast<Type>(buffer[0]);
out.code = buffer[1];
out.checksum = buffer[2] << 8 | buffer[3];
return 4;
} }
out.type = static_cast<Type>(buffer[0]); //
out.code = buffer[1]; // EchoHeader
out.checksum = buffer[2] << 8 | buffer[3]; //
return 4; EchoHeader::EchoHeader() : EchoHeader(0, 0) {}
} EchoHeader::EchoHeader(uint16_t identifier, uint16_t sequenceNumber) :
identifier(identifier), sequenceNumber(sequenceNumber)
//
// EchoHeader
//
EchoHeader::EchoHeader() : EchoHeader(0, 0) {}
EchoHeader::EchoHeader(uint16_t identifier, uint16_t sequenceNumber) :
identifier(identifier), sequenceNumber(sequenceNumber)
{
}
size_t EchoHeader::Serialize(uint8_t* buffer, const size_t bufferSize) const
{
if (bufferSize < SerializedLength())
{ {
return 0;
} }
size_t i = 0; size_t EchoHeader::Serialize(uint8_t* buffer, const size_t bufferSize) const
buffer[i++] = identifier >> 8;
buffer[i++] = identifier;
buffer[i++] = sequenceNumber >> 8;
buffer[i++] = sequenceNumber;
return i;
}
size_t EchoHeader::Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = identifier >> 8;
buffer[i++] = identifier;
buffer[i++] = sequenceNumber >> 8;
buffer[i++] = sequenceNumber;
return i;
} }
out.identifier = buffer[0] << 8 | buffer[1]; size_t
out.sequenceNumber = buffer[2] << 8 | buffer[3]; EchoHeader::Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize)
return 4;
}
void SendEchoRequest(Utils::MacAddress mac, uint32_t ip)
{
Icmp::Header icmpHeader(Icmp::Type::EchoRequest, 0);
Icmp::EchoHeader pingHeader(0, 0);
size_t ipv4TotalSize = Icmp::Header::SerializedLength() +
Icmp::EchoHeader::SerializedLength() + Ipv4::Header::SerializedLength();
Ipv4::Header ipv4Header(Ipv4::Protocol::Icmp, Utils::Ipv4Address, ip, ipv4TotalSize);
Ethernet::Header ethernetHeader(mac, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
const uint8_t emptyBuffer[1] = {};
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += pingHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += icmpHeader.Serialize(buffer + size, sizeof(buffer) - size, emptyBuffer, 0);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + pingHeader.SerializedLength() +
icmpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
static void handleEchoRequest(
const Ethernet::Header& reqEthernetHeader,
const Ipv4::Header& reqIpv4Header,
const Icmp::Header& reqIcmpHeader,
const uint8_t* reqBuffer,
const size_t reqBufferSize)
{
const Ethernet::Header respEthernetHeader(
reqEthernetHeader.macSource, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
const Ipv4::Header respIpv4Header(
Ipv4::Protocol::Icmp,
Utils::Ipv4Address,
reqIpv4Header.sourceIp,
reqIpv4Header.totalLength);
Header respIcmpHeader(Type::EchoReply, 0);
const auto payloadSize = reqIpv4Header.totalLength - reqIpv4Header.SerializedLength() -
reqIcmpHeader.SerializedLength();
DEBUG_LOG("payloadSize: %u\r\n", payloadSize);
std::array<uint8_t, USPI_FRAME_BUFFER_SIZE> respBuffer;
size_t respSize = 0;
respSize += respEthernetHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize +=
respIpv4Header.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIcmpHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize, reqBuffer, payloadSize);
const auto expectedRespSize = respEthernetHeader.SerializedLength() +
respIpv4Header.SerializedLength() + respIcmpHeader.SerializedLength() + payloadSize;
assert(respSize == expectedRespSize);
assert(respSize <= respBuffer.size());
USPiSendFrame(respBuffer.data(), respSize);
}
void HandlePacket(
Ethernet::Header ethernetHeader,
Ipv4::Header ipv4Header,
const uint8_t* buffer,
const size_t bufferSize)
{
Header icmpHeader;
size_t headerSize = Icmp::Header::Deserialize(icmpHeader, buffer, bufferSize);
if (headerSize != Icmp::Header::SerializedLength())
{ {
DEBUG_LOG( if (bufferSize < SerializedLength())
"Dropped ICMP packet (invalid buffer size %u, expected at least %u)\r\n", {
bufferSize, return 0;
Icmp::Header::SerializedLength()); }
out.identifier = buffer[0] << 8 | buffer[1];
out.sequenceNumber = buffer[2] << 8 | buffer[3];
return 4;
} }
DEBUG_LOG("Got ICMP header type %u\r\n", static_cast<uint8_t>(icmpHeader.type)); void SendEchoRequest(Utils::MacAddress mac, uint32_t ip)
if (icmpHeader.type == Type::EchoRequest)
{ {
handleEchoRequest( Icmp::Header icmpHeader(Icmp::Type::EchoRequest, 0);
ethernetHeader, Icmp::EchoHeader pingHeader(0, 0);
ipv4Header,
icmpHeader, size_t ipv4TotalSize = Icmp::Header::SerializedLength() +
buffer + headerSize, Icmp::EchoHeader::SerializedLength() + Ipv4::Header::SerializedLength();
bufferSize - headerSize); Ipv4::Header ipv4Header(Ipv4::Protocol::Icmp, Utils::Ipv4Address, ip, ipv4TotalSize);
Ethernet::Header ethernetHeader(mac, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size_t size = 0;
const uint8_t emptyBuffer[1] = {};
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4Header.Serialize(buffer + size, sizeof(buffer) - size);
size += pingHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += icmpHeader.Serialize(buffer + size, sizeof(buffer) - size, emptyBuffer, 0);
const auto expectedSize = ethernetHeader.SerializedLength() +
ipv4Header.SerializedLength() + pingHeader.SerializedLength() +
icmpHeader.SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
} }
}
} // namespace Net::Icmp static void handleEchoRequest(
const Ethernet::Header& reqEthernetHeader,
const Ipv4::Header& reqIpv4Header,
const Icmp::Header& reqIcmpHeader,
const uint8_t* reqBuffer,
const size_t reqBufferSize)
{
const Ethernet::Header respEthernetHeader(
reqEthernetHeader.macSource, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
const Ipv4::Header respIpv4Header(
Ipv4::Protocol::Icmp,
Utils::Ipv4Address,
reqIpv4Header.sourceIp,
reqIpv4Header.totalLength);
Header respIcmpHeader(Type::EchoReply, 0);
const auto payloadSize = reqIpv4Header.totalLength - reqIpv4Header.SerializedLength() -
reqIcmpHeader.SerializedLength();
DEBUG_LOG("payloadSize: %u\r\n", payloadSize);
std::array<uint8_t, USPI_FRAME_BUFFER_SIZE> respBuffer;
size_t respSize = 0;
respSize += respEthernetHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIpv4Header.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIcmpHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize, reqBuffer, payloadSize);
const auto expectedRespSize = respEthernetHeader.SerializedLength() +
respIpv4Header.SerializedLength() + respIcmpHeader.SerializedLength() + payloadSize;
assert(respSize == expectedRespSize);
assert(respSize <= respBuffer.size());
USPiSendFrame(respBuffer.data(), respSize);
}
void HandlePacket(
Ethernet::Header ethernetHeader,
Ipv4::Header ipv4Header,
const uint8_t* buffer,
const size_t bufferSize)
{
Header icmpHeader;
size_t headerSize = Icmp::Header::Deserialize(icmpHeader, buffer, bufferSize);
if (headerSize != Icmp::Header::SerializedLength())
{
DEBUG_LOG(
"Dropped ICMP packet (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
Icmp::Header::SerializedLength());
}
DEBUG_LOG("Got ICMP header type %u\r\n", static_cast<uint8_t>(icmpHeader.type));
if (icmpHeader.type == Type::EchoRequest)
{
handleEchoRequest(
ethernetHeader,
ipv4Header,
icmpHeader,
buffer + headerSize,
bufferSize - headerSize);
}
}
} // namespace Icmp
} // namespace Net

View file

@ -13,165 +13,170 @@
#include <cstring> #include <cstring>
#include <uspi.h> #include <uspi.h>
namespace Net::Ipv4 namespace Net
{ {
Header::Header() {} namespace Ipv4
Header::Header(
Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength) :
version(4),
ihl(5),
dscp(0),
ecn(0),
totalLength(totalLength),
identification(0),
flags(0),
fragmentOffset(0),
ttl(64),
protocol(protocol),
headerChecksum(0),
sourceIp(sourceIp),
destinationIp(destinationIp)
{ {
} Header::Header() {}
size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const Header::Header(
{ Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength) :
if (bufferSize <= SerializedLength()) version(4),
ihl(5),
dscp(0),
ecn(0),
totalLength(totalLength),
identification(0),
flags(0),
fragmentOffset(0),
ttl(64),
protocol(protocol),
headerChecksum(0),
sourceIp(sourceIp),
destinationIp(destinationIp)
{ {
return 0;
} }
size_t i = 0; size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const
buffer[i++] = version << 4 | ihl;
buffer[i++] = dscp << 2 | ecn;
buffer[i++] = totalLength >> 8;
buffer[i++] = totalLength;
buffer[i++] = identification >> 8;
buffer[i++] = identification;
buffer[i++] = (flags << 13 | fragmentOffset) >> 8;
buffer[i++] = flags << 13 | fragmentOffset;
buffer[i++] = ttl;
buffer[i++] = static_cast<uint8_t>(protocol);
// Zero the checksum before calculating it
buffer[i++] = 0;
buffer[i++] = 0;
buffer[i++] = sourceIp >> 24;
buffer[i++] = sourceIp >> 16;
buffer[i++] = sourceIp >> 8;
buffer[i++] = sourceIp;
buffer[i++] = destinationIp >> 24;
buffer[i++] = destinationIp >> 16;
buffer[i++] = destinationIp >> 8;
buffer[i++] = destinationIp;
uint16_t checksum = Net::Utils::InternetChecksum(buffer, i);
buffer[10] = checksum >> 8;
buffer[11] = checksum;
return i;
}
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize <= SerializedLength())
{ {
return 0; if (bufferSize <= SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = version << 4 | ihl;
buffer[i++] = dscp << 2 | ecn;
buffer[i++] = totalLength >> 8;
buffer[i++] = totalLength;
buffer[i++] = identification >> 8;
buffer[i++] = identification;
buffer[i++] = (flags << 13 | fragmentOffset) >> 8;
buffer[i++] = flags << 13 | fragmentOffset;
buffer[i++] = ttl;
buffer[i++] = static_cast<uint8_t>(protocol);
// Zero the checksum before calculating it
buffer[i++] = 0;
buffer[i++] = 0;
buffer[i++] = sourceIp >> 24;
buffer[i++] = sourceIp >> 16;
buffer[i++] = sourceIp >> 8;
buffer[i++] = sourceIp;
buffer[i++] = destinationIp >> 24;
buffer[i++] = destinationIp >> 16;
buffer[i++] = destinationIp >> 8;
buffer[i++] = destinationIp;
uint16_t checksum = Net::Utils::InternetChecksum(buffer, i);
buffer[10] = checksum >> 8;
buffer[11] = checksum;
return i;
} }
out.version = buffer[0] >> 4; size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
out.ihl = buffer[0] & 0x0F;
out.dscp = buffer[1] >> 2;
out.ecn = buffer[1] & 0x03;
out.totalLength = buffer[2] << 8 | buffer[3];
out.identification = buffer[4] << 8 | buffer[5];
out.flags = buffer[6] >> 5;
out.fragmentOffset = (buffer[6] & 0x1F) << 8 | buffer[7];
out.ttl = buffer[8];
out.protocol = static_cast<Protocol>(buffer[9]);
out.headerChecksum = buffer[10] << 8 | buffer[11];
out.sourceIp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
out.destinationIp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
return 20;
}
void HandlePacket(
const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize)
{
Header header;
const auto headerSize = Header::Deserialize(header, buffer, bufferSize);
if (headerSize != Header::SerializedLength())
{ {
if (bufferSize <= SerializedLength())
{
return 0;
}
out.version = buffer[0] >> 4;
out.ihl = buffer[0] & 0x0F;
out.dscp = buffer[1] >> 2;
out.ecn = buffer[1] & 0x03;
out.totalLength = buffer[2] << 8 | buffer[3];
out.identification = buffer[4] << 8 | buffer[5];
out.flags = buffer[6] >> 5;
out.fragmentOffset = (buffer[6] & 0x1F) << 8 | buffer[7];
out.ttl = buffer[8];
out.protocol = static_cast<Protocol>(buffer[9]);
out.headerChecksum = buffer[10] << 8 | buffer[11];
out.sourceIp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
out.destinationIp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
return 20;
}
void HandlePacket(
const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize)
{
Header header;
const auto headerSize = Header::Deserialize(header, buffer, bufferSize);
if (headerSize != Header::SerializedLength())
{
DEBUG_LOG(
"Dropped IPv4 header (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
headerSize);
return;
}
DEBUG_LOG( DEBUG_LOG(
"Dropped IPv4 header (invalid buffer size %u, expected at least %u)\r\n", "IPv4 { src=%08lx, dst=%08lx, len=%u, protocol=%u }\r\n",
bufferSize, header.sourceIp,
headerSize); header.destinationIp,
return; header.totalLength,
} static_cast<uint8_t>(header.protocol));
DEBUG_LOG( if (bufferSize < header.totalLength)
"IPv4 { src=%08lx, dst=%08lx, len=%u, protocol=%u }\r\n", {
header.sourceIp, DEBUG_LOG(
header.destinationIp, "Dropped IPv4 packet (invalid buffer size %u, expected at least %u)\r\n",
header.totalLength, bufferSize,
static_cast<uint8_t>(header.protocol)); header.totalLength);
if (bufferSize < header.totalLength) return;
{ }
DEBUG_LOG(
"Dropped IPv4 packet (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
header.totalLength);
return;
}
// Update ARP table // Update ARP table
Arp::ArpTable.insert(std::make_pair(header.sourceIp, ethernetHeader.macSource)); Arp::ArpTable.insert(std::make_pair(header.sourceIp, ethernetHeader.macSource));
if (header.version != 4) if (header.version != 4)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped IPv4 packet (invalid header version %u, expected 4)\r\n", header.version); "Dropped IPv4 packet (invalid header version %u, expected 4)\r\n",
return; header.version);
} return;
if (header.ihl != 5) }
{ if (header.ihl != 5)
// Not supported {
DEBUG_LOG("Dropped IPv4 packet (unsupported IHL %u, expected 5)\r\n", header.ihl); // Not supported
return; DEBUG_LOG("Dropped IPv4 packet (unsupported IHL %u, expected 5)\r\n", header.ihl);
} return;
if (header.destinationIp != Utils::Ipv4Address) }
{ if (header.destinationIp != Utils::Ipv4Address)
DEBUG_LOG( {
"Dropped IPv4 packet (invalid destination IP address %08lx)\r\n", DEBUG_LOG(
header.destinationIp); "Dropped IPv4 packet (invalid destination IP address %08lx)\r\n",
return; header.destinationIp);
} return;
if (header.fragmentOffset != 0) }
{ if (header.fragmentOffset != 0)
// TODO Support this {
DEBUG_LOG( // TODO Support this
"Dropped IPv4 packet (unexpected fragment offset %u, expected 0)\r\n", DEBUG_LOG(
header.fragmentOffset); "Dropped IPv4 packet (unexpected fragment offset %u, expected 0)\r\n",
return; header.fragmentOffset);
} return;
}
if (header.protocol == Ipv4::Protocol::Icmp) if (header.protocol == Ipv4::Protocol::Icmp)
{ {
DEBUG_LOG("Ethernet -> IPv4 -> ICMP\r\n"); DEBUG_LOG("Ethernet -> IPv4 -> ICMP\r\n");
Icmp::HandlePacket( Icmp::HandlePacket(
ethernetHeader, header, buffer + headerSize, bufferSize - headerSize); ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
}
else if (header.protocol == Ipv4::Protocol::Udp)
{
DEBUG_LOG("Ethernet -> IPv4 -> UDP\r\n");
Udp::HandlePacket(
ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
}
} }
else if (header.protocol == Ipv4::Protocol::Udp) } // namespace Ipv4
{ } // namespace Net
DEBUG_LOG("Ethernet -> IPv4 -> UDP\r\n");
Udp::HandlePacket(ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
}
}
} // namespace Net::Ipv4

View file

@ -4,44 +4,48 @@
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ipv4 namespace Net
{ {
enum class Protocol : uint8_t namespace Ipv4
{ {
Icmp = 1, enum class Protocol : uint8_t
Tcp = 6,
Udp = 17,
};
struct Header
{
uint8_t version;
uint8_t ihl;
uint8_t dscp;
uint8_t ecn;
uint16_t totalLength;
uint16_t identification;
uint8_t flags;
uint16_t fragmentOffset;
uint8_t ttl;
Protocol protocol;
uint16_t headerChecksum;
uint32_t sourceIp;
uint32_t destinationIp;
Header();
Header(Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength);
static constexpr size_t SerializedLength()
{ {
// Hardcoded because of bitfields. Icmp = 1,
return 20; Tcp = 6,
} Udp = 17,
};
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; struct Header
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize); {
}; uint8_t version;
uint8_t ihl;
uint8_t dscp;
uint8_t ecn;
uint16_t totalLength;
uint16_t identification;
uint8_t flags;
uint16_t fragmentOffset;
uint8_t ttl;
Protocol protocol;
uint16_t headerChecksum;
uint32_t sourceIp;
uint32_t destinationIp;
void HandlePacket( Header();
const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize); Header(
} // namespace Net::Ipv4 Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength);
static constexpr size_t SerializedLength()
{
// Hardcoded because of bitfields.
return 20;
}
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize);
};
void HandlePacket(
const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize);
} // namespace Ipv4
} // namespace Net

View file

@ -14,360 +14,364 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Tftp namespace Net
{ {
// TODO Allow multiple files open namespace Tftp
static FIL outFile;
static bool shouldReboot = false;
static uint32_t currentBlockNumber = -1;
Packet::Packet() : opcode(static_cast<Opcode>(0)) {}
Packet::Packet(const Opcode opcode) : opcode(opcode) {}
static std::unique_ptr<Packet>
handleTftpWriteRequest(const uint8_t* buffer, const size_t bufferSize)
{ {
DEBUG_LOG("Received TFTP write request\r\n"); // TODO Allow multiple files open
WriteReadRequestPacket packet; static FIL outFile;
const auto size = packet.Deserialize(buffer, bufferSize); static bool shouldReboot = false;
if (size == 0) static uint32_t currentBlockNumber = -1;
Packet::Packet() : opcode(static_cast<Opcode>(0)) {}
Packet::Packet(const Opcode opcode) : opcode(opcode) {}
static std::unique_ptr<Packet>
handleTftpWriteRequest(const uint8_t* buffer, const size_t bufferSize)
{ {
DEBUG_LOG( DEBUG_LOG("Received TFTP write request\r\n");
"Dropped TFTP packet (invalid buffer size %u, expected at least %u)\r\n", WriteReadRequestPacket packet;
bufferSize, const auto size = packet.Deserialize(buffer, bufferSize);
sizeof(WriteReadRequestPacket::opcode) + 2); if (size == 0)
return nullptr; {
DEBUG_LOG(
"Dropped TFTP packet (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
sizeof(WriteReadRequestPacket::opcode) + 2);
return nullptr;
}
// TODO Implement netscii, maybe
if (packet.mode != "octet")
{
const auto pointer = new ErrorPacket(0, "please use mode octet");
return std::unique_ptr<ErrorPacket>(pointer);
}
currentBlockNumber = 0;
// TODO Return to the original working directory.
char workingDirectory[256];
f_getcwd(workingDirectory, sizeof(workingDirectory));
// Try opening the file
auto separator = packet.filename.rfind('/', packet.filename.size());
if (separator != std::string::npos)
{
auto path = "/" + packet.filename.substr(0, separator);
f_chdir(path.c_str());
}
else
{
f_chdir("/");
separator = 0;
}
// Open the output file.
auto filename = packet.filename.substr(separator + 1);
const auto result = f_open(&outFile, filename.c_str(), FA_CREATE_ALWAYS | FA_WRITE);
std::unique_ptr<Packet> response;
if (result != FR_OK)
{
response =
std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "error opening target file"));
}
else
{
shouldReboot = packet.filename == "kernel.img" || packet.filename == "options.txt";
response = std::unique_ptr<AcknowledgementPacket>(
new AcknowledgementPacket(currentBlockNumber));
}
// TODO Return to the original working directory here
return response;
} }
// TODO Implement netscii, maybe static std::unique_ptr<Packet> handleTftpData(const uint8_t* buffer, size_t size)
if (packet.mode != "octet")
{ {
const auto pointer = new ErrorPacket(0, "please use mode octet"); DEBUG_LOG("Received TFTP data\r\n");
return std::unique_ptr<ErrorPacket>(pointer); DataPacket packet;
} const auto tftpSize = packet.Deserialize(buffer, size);
if (tftpSize == 0)
{
DEBUG_LOG(
"Dropped TFTP data packet (invalid buffer size %u, expected at least %u)\r\n",
size,
sizeof(packet.opcode) + sizeof(packet.blockNumber));
return nullptr;
}
currentBlockNumber = 0; if (packet.blockNumber != currentBlockNumber + 1)
{
f_close(&outFile);
return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "invalid block number"));
}
currentBlockNumber = packet.blockNumber;
// TODO Return to the original working directory. unsigned int bytesWritten;
char workingDirectory[256]; const auto result =
f_getcwd(workingDirectory, sizeof(workingDirectory)); f_write(&outFile, packet.data.data(), packet.data.size(), &bytesWritten);
// Try opening the file if (result != FR_OK || bytesWritten != packet.data.size())
auto separator = packet.filename.rfind('/', packet.filename.size()); {
if (separator != std::string::npos) f_close(&outFile);
{ return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "io error"));
auto path = "/" + packet.filename.substr(0, separator); }
f_chdir(path.c_str());
}
else
{
f_chdir("/");
separator = 0;
}
// Open the output file. if (packet.data.size() < TFTP_BLOCK_SIZE)
auto filename = packet.filename.substr(separator + 1); {
const auto result = f_open(&outFile, filename.c_str(), FA_CREATE_ALWAYS | FA_WRITE); // Close the file for the last packet.
f_close(&outFile);
}
std::unique_ptr<Packet> response; return std::unique_ptr<AcknowledgementPacket>(
if (result != FR_OK)
{
response =
std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "error opening target file"));
}
else
{
shouldReboot = packet.filename == "kernel.img" || packet.filename == "options.txt";
response = std::unique_ptr<AcknowledgementPacket>(
new AcknowledgementPacket(currentBlockNumber)); new AcknowledgementPacket(currentBlockNumber));
} }
// TODO Return to the original working directory here void HandlePacket(
const Ethernet::Header ethernetReqHeader,
return response; const Ipv4::Header ipv4ReqHeader,
} const Udp::Header udpReqHeader,
const uint8_t* reqBuffer,
static std::unique_ptr<Packet> handleTftpData(const uint8_t* buffer, size_t size) const size_t reqBufferSize)
{
DEBUG_LOG("Received TFTP data\r\n");
DataPacket packet;
const auto tftpSize = packet.Deserialize(buffer, size);
if (tftpSize == 0)
{ {
DEBUG_LOG( const auto opcode = static_cast<Opcode>(reqBuffer[0] << 8 | reqBuffer[1]);
"Dropped TFTP data packet (invalid buffer size %u, expected at least %u)\r\n", DEBUG_LOG("Received TFTP %u packet\r\n", static_cast<uint16_t>(opcode));
size, std::unique_ptr<Packet> response;
sizeof(packet.opcode) + sizeof(packet.blockNumber));
return nullptr; const auto payloadSize = udpReqHeader.length - udpReqHeader.SerializedLength();
if (reqBufferSize < payloadSize)
{
DEBUG_LOG(
"Dropped TFTP packet (invalid buffer size %u, expected at least %u)\r\n",
reqBufferSize,
payloadSize);
}
if (opcode == Opcode::WriteRequest)
{
response = handleTftpWriteRequest(reqBuffer, payloadSize);
}
else if (opcode == Opcode::Data)
{
response = handleTftpData(reqBuffer, payloadSize);
}
else
{
response = std::unique_ptr<ErrorPacket>(new ErrorPacket(4, "not implemented yet"));
}
if (response != nullptr)
{
DEBUG_LOG("Sending TFTP response\r\n");
Udp::Header udpRespHeader(
udpReqHeader.destinationPort,
udpReqHeader.sourcePort,
response->SerializedLength() + Udp::Header::SerializedLength());
Ipv4::Header ipv4RespHeader(
Ipv4::Protocol::Udp,
Utils::Ipv4Address,
ipv4ReqHeader.sourceIp,
udpRespHeader.length + Ipv4::Header::SerializedLength());
Ethernet::Header ethernetRespHeader(
Arp::ArpTable[ipv4RespHeader.destinationIp],
Utils::GetMacAddress(),
Ethernet::EtherType::Ipv4);
size_t size = 0;
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size += ethernetRespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4RespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += udpRespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += response->Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetRespHeader.SerializedLength() +
ipv4RespHeader.SerializedLength() + udpRespHeader.SerializedLength() +
response->SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
else
{
DEBUG_LOG("TFTP response was nullptr\r\n");
}
// TODO Reboot the Pi when a system file was received
} }
if (packet.blockNumber != currentBlockNumber + 1) //
{ // WriteReadRequestPacket
f_close(&outFile); //
return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "invalid block number")); WriteReadRequestPacket::WriteReadRequestPacket() : Packet() {}
} WriteReadRequestPacket::WriteReadRequestPacket(const Opcode opcode) : Packet(opcode) {}
currentBlockNumber = packet.blockNumber;
unsigned int bytesWritten; size_t WriteReadRequestPacket::SerializedLength() const
const auto result =
f_write(&outFile, packet.data.data(), packet.data.size(), &bytesWritten);
if (result != FR_OK || bytesWritten != packet.data.size())
{ {
f_close(&outFile); return sizeof(opcode) + filename.size() + 1 + mode.size() + 1;
return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "io error"));
} }
if (packet.data.size() < TFTP_BLOCK_SIZE) size_t WriteReadRequestPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
{ {
// Close the file for the last packet. if (bufferSize < SerializedLength())
f_close(&outFile); {
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
i += filename.copy(reinterpret_cast<char*>(buffer + i), filename.size());
buffer[i++] = 0;
i += mode.copy(reinterpret_cast<char*>(buffer + i), mode.size());
buffer[i++] = 0;
return i;
} }
return std::unique_ptr<AcknowledgementPacket>( size_t WriteReadRequestPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
new AcknowledgementPacket(currentBlockNumber));
}
void HandlePacket(
const Ethernet::Header ethernetReqHeader,
const Ipv4::Header ipv4ReqHeader,
const Udp::Header udpReqHeader,
const uint8_t* reqBuffer,
const size_t reqBufferSize)
{
const auto opcode = static_cast<Opcode>(reqBuffer[0] << 8 | reqBuffer[1]);
DEBUG_LOG("Received TFTP %u packet\r\n", static_cast<uint16_t>(opcode));
std::unique_ptr<Packet> response;
const auto payloadSize = udpReqHeader.length - udpReqHeader.SerializedLength();
if (reqBufferSize < payloadSize)
{ {
DEBUG_LOG( // Can't use SerializedLength here, as it's variable. Check each field separately
"Dropped TFTP packet (invalid buffer size %u, expected at least %u)\r\n", // instead.
reqBufferSize, size_t i = 0;
payloadSize);
if (sizeof(Opcode) >= bufferSize - i)
return 0;
opcode = static_cast<Opcode>(buffer[i] << 8 | buffer[i + 1]);
i += sizeof(Opcode);
// Check if there's a null terminator within the remaining buffer
size_t j;
for (j = i; j < bufferSize; j++)
if (buffer[j] == 0)
break;
if (j == bufferSize)
return 0;
filename = std::string(reinterpret_cast<const char*>(buffer + i));
i += filename.size() + 1;
// Check if there's a null terminator within the remaining buffer
for (j = i; j < bufferSize; j++)
if (buffer[j] == 0)
break;
if (j == bufferSize)
return 0;
mode = std::string(reinterpret_cast<const char*>(buffer + i));
i += mode.size() + 1;
return i;
} }
if (opcode == Opcode::WriteRequest) //
// ErrorPacket
//
ErrorPacket::ErrorPacket() : Packet(Opcode::Error) {}
ErrorPacket::ErrorPacket(uint16_t errorCode, std::string message) :
Packet(Opcode::Error), errorCode(errorCode), message(message)
{ {
response = handleTftpWriteRequest(reqBuffer, payloadSize);
}
else if (opcode == Opcode::Data)
{
response = handleTftpData(reqBuffer, payloadSize);
}
else
{
response = std::unique_ptr<ErrorPacket>(new ErrorPacket(4, "not implemented yet"));
} }
if (response != nullptr) size_t ErrorPacket::SerializedLength() const
{ {
DEBUG_LOG("Sending TFTP response\r\n"); return sizeof(opcode) + sizeof(errorCode) + message.size() + 1;
Udp::Header udpRespHeader(
udpReqHeader.destinationPort,
udpReqHeader.sourcePort,
response->SerializedLength() + Udp::Header::SerializedLength());
Ipv4::Header ipv4RespHeader(
Ipv4::Protocol::Udp,
Utils::Ipv4Address,
ipv4ReqHeader.sourceIp,
udpRespHeader.length + Ipv4::Header::SerializedLength());
Ethernet::Header ethernetRespHeader(
Arp::ArpTable[ipv4RespHeader.destinationIp],
Utils::GetMacAddress(),
Ethernet::EtherType::Ipv4);
size_t size = 0;
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
size += ethernetRespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += ipv4RespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += udpRespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += response->Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetRespHeader.SerializedLength() +
ipv4RespHeader.SerializedLength() + udpRespHeader.SerializedLength() +
response->SerializedLength();
assert(size == expectedSize);
assert(size <= sizeof(buffer));
USPiSendFrame(buffer, size);
}
else
{
DEBUG_LOG("TFTP response was nullptr\r\n");
} }
// TODO Reboot the Pi when a system file was received size_t ErrorPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
}
//
// WriteReadRequestPacket
//
WriteReadRequestPacket::WriteReadRequestPacket() : Packet() {}
WriteReadRequestPacket::WriteReadRequestPacket(const Opcode opcode) : Packet(opcode) {}
size_t WriteReadRequestPacket::SerializedLength() const
{
return sizeof(opcode) + filename.size() + 1 + mode.size() + 1;
}
size_t WriteReadRequestPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
{
if (bufferSize < SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
buffer[i++] = errorCode >> 8;
buffer[i++] = errorCode;
i += message.copy(reinterpret_cast<char*>(buffer + i), message.size());
buffer[i++] = 0;
return i;
} }
size_t i = 0; //
buffer[i++] = static_cast<uint16_t>(opcode) >> 8; // AcknowledgementPacket
buffer[i++] = static_cast<uint16_t>(opcode); //
AcknowledgementPacket::AcknowledgementPacket() : Packet(Opcode::Acknowledgement) {}
i += filename.copy(reinterpret_cast<char*>(buffer + i), filename.size()); AcknowledgementPacket::AcknowledgementPacket(uint16_t blockNumber) :
buffer[i++] = 0; Packet(Opcode::Acknowledgement), blockNumber(blockNumber)
i += mode.copy(reinterpret_cast<char*>(buffer + i), mode.size());
buffer[i++] = 0;
return i;
}
size_t WriteReadRequestPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
// Can't use SerializedLength here, as it's variable. Check each field separately instead.
size_t i = 0;
if (sizeof(Opcode) >= bufferSize - i)
return 0;
opcode = static_cast<Opcode>(buffer[i] << 8 | buffer[i + 1]);
i += sizeof(Opcode);
// Check if there's a null terminator within the remaining buffer
size_t j;
for (j = i; j < bufferSize; j++)
if (buffer[j] == 0)
break;
if (j == bufferSize)
return 0;
filename = std::string(reinterpret_cast<const char*>(buffer + i));
i += filename.size() + 1;
// Check if there's a null terminator within the remaining buffer
for (j = i; j < bufferSize; j++)
if (buffer[j] == 0)
break;
if (j == bufferSize)
return 0;
mode = std::string(reinterpret_cast<const char*>(buffer + i));
i += mode.size() + 1;
return i;
}
//
// ErrorPacket
//
ErrorPacket::ErrorPacket() : Packet(Opcode::Error) {}
ErrorPacket::ErrorPacket(uint16_t errorCode, std::string message) :
Packet(Opcode::Error), errorCode(errorCode), message(message)
{
}
size_t ErrorPacket::SerializedLength() const
{
return sizeof(opcode) + sizeof(errorCode) + message.size() + 1;
}
size_t ErrorPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
{
if (bufferSize < SerializedLength())
{ {
return 0;
} }
size_t i = 0; size_t AcknowledgementPacket::SerializedLength() const
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
buffer[i++] = errorCode >> 8;
buffer[i++] = errorCode;
i += message.copy(reinterpret_cast<char*>(buffer + i), message.size());
buffer[i++] = 0;
return i;
}
//
// AcknowledgementPacket
//
AcknowledgementPacket::AcknowledgementPacket() : Packet(Opcode::Acknowledgement) {}
AcknowledgementPacket::AcknowledgementPacket(uint16_t blockNumber) :
Packet(Opcode::Acknowledgement), blockNumber(blockNumber)
{
}
size_t AcknowledgementPacket::SerializedLength() const
{
return sizeof(opcode) + sizeof(blockNumber);
}
size_t AcknowledgementPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
{
if (bufferSize < SerializedLength())
{ {
return 0; return sizeof(opcode) + sizeof(blockNumber);
} }
size_t i = 0; size_t AcknowledgementPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
buffer[i++] = blockNumber >> 8;
buffer[i++] = blockNumber;
return i;
}
//
// DataPacket
//
DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0) {}
size_t DataPacket::SerializedLength() const
{
return sizeof(opcode) + sizeof(blockNumber) + data.size();
}
size_t DataPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
{
if (bufferSize <= SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
buffer[i++] = blockNumber >> 8;
buffer[i++] = blockNumber;
return i;
} }
size_t i = 0; //
buffer[i++] = static_cast<uint16_t>(opcode) >> 8; // DataPacket
buffer[i++] = static_cast<uint16_t>(opcode); //
buffer[i++] = blockNumber >> 8; DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0) {}
buffer[i++] = blockNumber;
std::memcpy(buffer + i, data.data(), data.size()); size_t DataPacket::SerializedLength() const
i += data.size();
return i;
}
size_t DataPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < sizeof(opcode) + sizeof(blockNumber))
{ {
return 0; return sizeof(opcode) + sizeof(blockNumber) + data.size();
} }
opcode = static_cast<Opcode>(buffer[0] << 8 | buffer[1]); size_t DataPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
blockNumber = buffer[2] << 8 | buffer[3]; {
data = std::vector<uint8_t>(buffer + 4, buffer + bufferSize); if (bufferSize <= SerializedLength())
return bufferSize; {
} return 0;
} // namespace Net::Tftp }
size_t i = 0;
buffer[i++] = static_cast<uint16_t>(opcode) >> 8;
buffer[i++] = static_cast<uint16_t>(opcode);
buffer[i++] = blockNumber >> 8;
buffer[i++] = blockNumber;
std::memcpy(buffer + i, data.data(), data.size());
i += data.size();
return i;
}
size_t DataPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < sizeof(opcode) + sizeof(blockNumber))
{
return 0;
}
opcode = static_cast<Opcode>(buffer[0] << 8 | buffer[1]);
blockNumber = buffer[2] << 8 | buffer[3];
data = std::vector<uint8_t>(buffer + 4, buffer + bufferSize);
return bufferSize;
}
} // namespace Tftp
} // namespace Net

View file

@ -4,95 +4,98 @@
#include "debug.h" #include "debug.h"
namespace Net::Udp namespace Net
{ {
Header::Header() {} namespace Udp
Header::Header(Port sourcePort, Port destinationPort, uint16_t length) :
sourcePort(sourcePort), destinationPort(destinationPort), length(length), checksum(0)
{ {
} Header::Header() {}
size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const Header::Header(Port sourcePort, Port destinationPort, uint16_t length) :
{ sourcePort(sourcePort), destinationPort(destinationPort), length(length), checksum(0)
if (bufferSize < SerializedLength())
{ {
return 0;
} }
size_t i = 0; size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const
buffer[i++] = static_cast<uint16_t>(sourcePort) >> 8;
buffer[i++] = static_cast<uint16_t>(sourcePort);
buffer[i++] = static_cast<uint16_t>(destinationPort) >> 8;
buffer[i++] = static_cast<uint16_t>(destinationPort);
buffer[i++] = length >> 8;
buffer[i++] = length;
buffer[i++] = checksum >> 8;
buffer[i++] = checksum;
return i;
}
size_t Header::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < Header::SerializedLength())
{ {
return 0; if (bufferSize < SerializedLength())
{
return 0;
}
size_t i = 0;
buffer[i++] = static_cast<uint16_t>(sourcePort) >> 8;
buffer[i++] = static_cast<uint16_t>(sourcePort);
buffer[i++] = static_cast<uint16_t>(destinationPort) >> 8;
buffer[i++] = static_cast<uint16_t>(destinationPort);
buffer[i++] = length >> 8;
buffer[i++] = length;
buffer[i++] = checksum >> 8;
buffer[i++] = checksum;
return i;
} }
sourcePort = static_cast<Port>(buffer[0] << 8 | buffer[1]); size_t Header::Deserialize(const uint8_t* buffer, const size_t bufferSize)
destinationPort = static_cast<Port>(buffer[2] << 8 | buffer[3]);
length = buffer[4] << 8 | buffer[5];
checksum = buffer[6] << 8 | buffer[7];
return 8;
}
void HandlePacket(
const Ethernet::Header ethernetHeader,
const Ipv4::Header ipv4Header,
const uint8_t* buffer,
const size_t bufferSize)
{
Header udpHeader;
const auto headerSize = udpHeader.Deserialize(buffer, bufferSize);
if (headerSize == 0 || headerSize != udpHeader.SerializedLength())
{ {
if (bufferSize < Header::SerializedLength())
{
return 0;
}
sourcePort = static_cast<Port>(buffer[0] << 8 | buffer[1]);
destinationPort = static_cast<Port>(buffer[2] << 8 | buffer[3]);
length = buffer[4] << 8 | buffer[5];
checksum = buffer[6] << 8 | buffer[7];
return 8;
}
void HandlePacket(
const Ethernet::Header ethernetHeader,
const Ipv4::Header ipv4Header,
const uint8_t* buffer,
const size_t bufferSize)
{
Header udpHeader;
const auto headerSize = udpHeader.Deserialize(buffer, bufferSize);
if (headerSize == 0 || headerSize != udpHeader.SerializedLength())
{
DEBUG_LOG(
"Dropped UDP header (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
Header::SerializedLength());
return;
}
DEBUG_LOG( DEBUG_LOG(
"Dropped UDP header (invalid buffer size %u, expected at least %u)\r\n", "UDP { src=%u, dst=%u, len=%u, chk=%u }\r\n",
bufferSize, static_cast<uint16_t>(udpHeader.sourcePort),
Header::SerializedLength()); static_cast<uint16_t>(udpHeader.destinationPort),
return; udpHeader.length,
} udpHeader.checksum);
if (bufferSize < udpHeader.length)
{
DEBUG_LOG(
"Dropped UDP packet (invalid buffer size %u, expected at least %u)\r\n",
bufferSize,
udpHeader.length);
return;
}
DEBUG_LOG( if (udpHeader.destinationPort == Port::DhcpClient)
"UDP { src=%u, dst=%u, len=%u, chk=%u }\r\n", {
static_cast<uint16_t>(udpHeader.sourcePort), Dhcp::HandlePacket(
static_cast<uint16_t>(udpHeader.destinationPort), ethernetHeader,
udpHeader.length, buffer + udpHeader.SerializedLength(),
udpHeader.checksum); bufferSize - udpHeader.SerializedLength());
if (bufferSize < udpHeader.length) }
{ else if (udpHeader.destinationPort == Port::Tftp)
DEBUG_LOG( {
"Dropped UDP packet (invalid buffer size %u, expected at least %u)\r\n", Tftp::HandlePacket(
bufferSize, ethernetHeader,
udpHeader.length); ipv4Header,
return; udpHeader,
buffer + udpHeader.SerializedLength(),
bufferSize - udpHeader.SerializedLength());
}
} }
} // namespace Udp
if (udpHeader.destinationPort == Port::DhcpClient) } // namespace Net
{
Dhcp::HandlePacket(
ethernetHeader,
buffer + udpHeader.SerializedLength(),
bufferSize - udpHeader.SerializedLength());
}
else if (udpHeader.destinationPort == Port::Tftp)
{
Tftp::HandlePacket(
ethernetHeader,
ipv4Header,
udpHeader,
buffer + udpHeader.SerializedLength(),
bufferSize - udpHeader.SerializedLength());
}
}
} // namespace Net::Udp

View file

@ -6,37 +6,41 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-ipv4.h" #include "net-ipv4.h"
namespace Net::Udp namespace Net
{ {
enum class Port : uint16_t namespace Udp
{ {
DhcpServer = 67, enum class Port : uint16_t
DhcpClient = 68,
Tftp = 69, // nice
};
struct Header
{
Port sourcePort;
Port destinationPort;
uint16_t length;
uint16_t checksum;
Header();
Header(Port sourcePort, Port destinationPort, uint16_t length);
static constexpr size_t SerializedLength()
{ {
return sizeof(sourcePort) + sizeof(destinationPort) + sizeof(length) + sizeof(checksum); DhcpServer = 67,
} DhcpClient = 68,
Tftp = 69, // nice
};
size_t Serialize(uint8_t* buffer, const size_t size) const; struct Header
size_t Deserialize(const uint8_t* buffer, const size_t size); {
}; Port sourcePort;
Port destinationPort;
uint16_t length;
uint16_t checksum;
void HandlePacket( Header();
const Ethernet::Header ethernetHeader, Header(Port sourcePort, Port destinationPort, uint16_t length);
const Ipv4::Header ipv4Header,
const uint8_t* buffer, static constexpr size_t SerializedLength()
const size_t size); {
} // namespace Net::Udp return sizeof(sourcePort) + sizeof(destinationPort) + sizeof(length) +
sizeof(checksum);
}
size_t Serialize(uint8_t* buffer, const size_t size) const;
size_t Deserialize(const uint8_t* buffer, const size_t size);
};
void HandlePacket(
const Ethernet::Header ethernetHeader,
const Ipv4::Header ipv4Header,
const uint8_t* buffer,
const size_t size);
} // namespace Udp
} // namespace Net

View file

@ -3,97 +3,100 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Utils namespace Net
{ {
const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; namespace Utils
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; const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint32_t Ipv4Address = 0xC0A80164;
for (size_t a = 0; a < size; a++) 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)
{ {
auto index = (crc ^ (uint32_t)*buffer++) & 0x000000FFul; uint32_t crc = 0xFFFFFFFFul;
crc = (crc >> 8) ^ crcTab32[index];
for (size_t a = 0; a < size; a++)
{
auto index = (crc ^ (uint32_t)*buffer++) & 0x000000FFul;
crc = (crc >> 8) ^ crcTab32[index];
}
return crc ^ 0xFFFFFFFFul;
} }
return crc ^ 0xFFFFFFFFul; uint16_t InternetChecksum(const void* data, size_t size)
}
uint16_t InternetChecksum(const void* data, size_t size)
{
if (size == 0)
return ~0;
auto u8data = static_cast<const uint8_t*>(data);
uint32_t sum = 0;
for (size_t i = 0; i < size - 1; i += 2)
sum += u8data[i] << 8 | u8data[i + 1];
if (size % 2 == 1)
sum += u8data[size - 1] << 8;
sum = (sum >> 16) + (sum & 0xFFFF);
return ~sum;
}
MacAddress GetMacAddress()
{
static MacAddress macAddress{0};
if (macAddress == MacAddress{0})
{ {
USPiGetMACAddress(macAddress.data()); if (size == 0)
return ~0;
auto u8data = static_cast<const uint8_t*>(data);
uint32_t sum = 0;
for (size_t i = 0; i < size - 1; i += 2)
sum += u8data[i] << 8 | u8data[i + 1];
if (size % 2 == 1)
sum += u8data[size - 1] << 8;
sum = (sum >> 16) + (sum & 0xFFFF);
return ~sum;
} }
return macAddress; MacAddress GetMacAddress()
} {
} // namespace Net::Utils static MacAddress macAddress{0};
if (macAddress == MacAddress{0})
{
USPiGetMACAddress(macAddress.data());
}
return macAddress;
}
} // namespace Utils
} // namespace Net

View file

@ -2,13 +2,16 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
namespace Net::Utils namespace Net
{ {
typedef std::array<uint8_t, 6> MacAddress; namespace Utils
extern const MacAddress MacBroadcast; {
extern uint32_t Ipv4Address; 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); uint32_t Crc32(const uint8_t* buffer, size_t size);
uint16_t InternetChecksum(const void* data, size_t size); uint16_t InternetChecksum(const void* data, size_t size);
MacAddress GetMacAddress(); MacAddress GetMacAddress();
}; // namespace Net::Utils } // namespace Utils
} // namespace Net

View file

@ -58,7 +58,6 @@ namespace Net
} }
} }
void Update() void Update()
{ {
if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime) if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime)