Run clang-format on net files

This commit is contained in:
Sijmen 2020-12-29 13:52:02 +01:00
parent 9e41e71dfa
commit 24002b225a
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
16 changed files with 260 additions and 395 deletions

View file

@ -1,5 +1,12 @@
--- ---
AlignAfterOpenBracket: AlwaysBreak AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: 'false'
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: DontAlign
AlignOperands: 'false'
AlignTrailingComments: 'false'
AllowAllArgumentsOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'false' AllowShortBlocksOnASingleLine: 'false'
BinPackArguments: 'false' BinPackArguments: 'false'
BinPackParameters: 'false' BinPackParameters: 'false'
@ -14,5 +21,4 @@ PointerAlignment: Left
SortIncludes: 'true' SortIncludes: 'true'
TabWidth: '4' TabWidth: '4'
UseTab: Always UseTab: Always
... ...

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <unordered_map> #include <unordered_map>
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-utils.h" #include "net-utils.h"
@ -29,46 +30,36 @@ namespace Net::Arp
constexpr size_t SerializedLength() const constexpr size_t SerializedLength() const
{ {
return return sizeof(hardwareType) + sizeof(protocolType) + sizeof(hardwareAddressLength) +
sizeof(hardwareType) + sizeof(protocolAddressLength) + sizeof(operation) + senderMac.size() +
sizeof(protocolType) + sizeof(senderIp) + targetMac.size() + sizeof(targetIp);
sizeof(hardwareAddressLength) +
sizeof(protocolAddressLength) +
sizeof(operation) +
senderMac.size() +
sizeof(senderIp) +
targetMac.size() +
sizeof(targetIp);
} }
size_t Serialize(uint8_t* buffer, const size_t bufferSize); size_t Serialize(uint8_t* buffer, const size_t bufferSize);
size_t Deserialize(const uint8_t* buffer, const size_t bufferSize); size_t Deserialize(const uint8_t* buffer, const size_t bufferSize);
}; };
void HandlePacket( void
const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize); HandlePacket(const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize);
void SendPacket( void SendPacket(
const Operation operation, const Operation operation,
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, const Utils::MacAddress senderMac,
const uint32_t senderIp, const uint32_t senderIp,
const uint32_t targetIp const uint32_t targetIp);
);
void SendRequest( void SendRequest(
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, const Utils::MacAddress senderMac,
const uint32_t senderIp, const uint32_t senderIp,
const uint32_t targetIp const uint32_t targetIp);
);
void SendReply( void SendReply(
const Utils::MacAddress targetMac, const Utils::MacAddress targetMac,
const Utils::MacAddress senderMac, const Utils::MacAddress senderMac,
const uint32_t senderIp, const uint32_t senderIp,
const uint32_t targetIp const uint32_t targetIp);
);
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip); void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip);

View file

@ -1,11 +1,11 @@
#include <random>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <random>
#include "net-dhcp.h" #include "net-dhcp.h"
#include "net-udp.h"
#include "net-ipv4.h"
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-ipv4.h"
#include "net-udp.h"
#include "debug.h" #include "debug.h"
#include "types.h" #include "types.h"
@ -14,8 +14,7 @@
namespace Net::Dhcp namespace Net::Dhcp
{ {
Header::Header() Header::Header() {}
{}
Header::Header(Opcode opcode, uint32_t transactionId) : Header::Header(Opcode opcode, uint32_t transactionId) :
opcode(opcode), opcode(opcode),
@ -86,9 +85,8 @@ namespace Net::Dhcp
return i; return i;
} }
size_t Header::Deserialize( size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
Header& out, const uint8_t* buffer, const size_t size {
) {
if (size < SerializedLength()) if (size < SerializedLength())
{ {
return 0; return 0;
@ -98,24 +96,16 @@ namespace Net::Dhcp
out.hardwareAddressType = buffer[1]; out.hardwareAddressType = buffer[1];
out.hardwareAddressLength = buffer[2]; out.hardwareAddressLength = buffer[2];
out.hops = buffer[3]; out.hops = buffer[3];
out.transactionId = out.transactionId = buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
buffer[4] << 24 | buffer[5] << 16 | buffer[6] << 8 | buffer[7];
out.secondsElapsed = buffer[8] << 8 | buffer[9]; out.secondsElapsed = buffer[8] << 8 | buffer[9];
out.flags = buffer[10] << 8 | buffer[11]; out.flags = buffer[10] << 8 | buffer[11];
out.clientIpAddress = out.clientIpAddress = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
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.yourIpAddress = out.serverIpAddress = buffer[20] << 24 | buffer[21] << 16 | buffer[22] << 8 | buffer[23];
buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19]; out.relayIpAddress = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
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( std::memcpy(
out.clientHardwareAddress.data(), out.clientHardwareAddress.data(), buffer + 28, out.clientHardwareAddress.size());
buffer + 28,
out.clientHardwareAddress.size()
);
std::memcpy(out.serverHostname.data(), buffer + 44, out.serverHostname.size()); std::memcpy(out.serverHostname.data(), buffer + 44, out.serverHostname.size());
std::memcpy(out.bootFile.data(), buffer + 108, out.bootFile.size()); std::memcpy(out.bootFile.data(), buffer + 108, out.bootFile.size());
std::memcpy(out.magicValue.data(), buffer + 236, out.magicValue.size()); std::memcpy(out.magicValue.data(), buffer + 236, out.magicValue.size());
@ -131,16 +121,12 @@ namespace Net::Dhcp
static bool serverSelected; static bool serverSelected;
void sendRequest( void sendRequest(
uint32_t clientIpAddress, uint32_t clientIpAddress, Utils::MacAddress serverMacAddress, uint32_t serverIpAddress)
Utils::MacAddress serverMacAddress, {
uint32_t serverIpAddress
) {
const Header dhcpHeader(Opcode::BootRequest, transactionId); const Header dhcpHeader(Opcode::BootRequest, transactionId);
size_t udpLength = size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength();
dhcpHeader.SerializedLength() + Udp::Header::SerializedLength(); const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
const Udp::Header udpHeader(
Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength(); size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength();
const Ipv4::Header ipv4Header( const Ipv4::Header ipv4Header(
@ -155,10 +141,8 @@ namespace Net::Dhcp
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size); size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size); size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = const auto expectedSize = ethernetHeader.SerializedLength() +
ethernetHeader.SerializedLength() + ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
ipv4Header.SerializedLength() +
udpHeader.SerializedLength() +
dhcpHeader.SerializedLength(); dhcpHeader.SerializedLength();
assert(size == expectedSize); assert(size == expectedSize);
assert(size <= sizeof(buffer)); assert(size <= sizeof(buffer));
@ -180,8 +164,7 @@ namespace Net::Dhcp
// Send DHCP Requests to every server with that IP address. // Send DHCP Requests to every server with that IP address.
for (size_t i = 0; i < serverIpAddresses.size(); i++) for (size_t i = 0; i < serverIpAddresses.size(); i++)
{ {
sendRequest( sendRequest(Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]);
Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]);
} }
// Run the callback indicating an IP has been obtained // Run the callback indicating an IP has been obtained
@ -198,15 +181,12 @@ namespace Net::Dhcp
offeredIpAddresses.clear(); offeredIpAddresses.clear();
const Header dhcpHeader(Opcode::BootRequest, transactionId); const Header dhcpHeader(Opcode::BootRequest, transactionId);
size_t udpLength = size_t udpLength = dhcpHeader.SerializedLength() + Udp::Header::SerializedLength();
dhcpHeader.SerializedLength() + Udp::Header::SerializedLength(); const Udp::Header udpHeader(Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
const Udp::Header udpHeader(
Udp::Port::DhcpClient, Udp::Port::DhcpServer, udpLength);
size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength(); size_t ipv4Length = udpLength + Ipv4::Header::SerializedLength();
const Ipv4::Header ipv4Header(Ipv4::Protocol::Udp, 0, 0xFFFFFFFF, ipv4Length); const Ipv4::Header ipv4Header(Ipv4::Protocol::Udp, 0, 0xFFFFFFFF, ipv4Length);
const Ethernet::Header ethernetHeader( const Ethernet::Header ethernetHeader(Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
Utils::GetMacAddress(), 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;
@ -216,10 +196,8 @@ namespace Net::Dhcp
size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size); size += udpHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size); size += dhcpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = const auto expectedSize = ethernetHeader.SerializedLength() +
ethernetHeader.SerializedLength() + ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
ipv4Header.SerializedLength() +
udpHeader.SerializedLength() +
dhcpHeader.SerializedLength(); dhcpHeader.SerializedLength();
assert(size == expectedSize); assert(size == expectedSize);
assert(size <= sizeof(buffer)); assert(size <= sizeof(buffer));
@ -236,19 +214,15 @@ namespace Net::Dhcp
StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr); StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr);
} }
static void handleOfferPacket( static void handleOfferPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
const Ethernet::Header ethernetHeader, {
const Header dhcpHeader
) {
offeredIpAddresses.push_back(dhcpHeader.yourIpAddress); offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
serverIpAddresses.push_back(dhcpHeader.serverIpAddress); serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
serverMacAddresses.push_back(ethernetHeader.macSource); serverMacAddresses.push_back(ethernetHeader.macSource);
} }
static void handleAckPacket( static void handleAckPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
const Ethernet::Header ethernetHeader, {
const Header dhcpHeader
) {
Utils::Ipv4Address = dhcpHeader.yourIpAddress; Utils::Ipv4Address = dhcpHeader.yourIpAddress;
// TODO Schedule handler for end of lease. // TODO Schedule handler for end of lease.
@ -260,26 +234,27 @@ namespace Net::Dhcp
serverSelected = false; serverSelected = false;
} }
void HandlePacket( void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size)
const Ethernet::Header& ethernetHeader, {
const uint8_t* buffer,
size_t size
) {
Header header; Header header;
const auto dhcpSize = Header::Deserialize(header, buffer, size); const auto dhcpSize = Header::Deserialize(header, buffer, size);
if (dhcpSize != Header::SerializedLength()) if (dhcpSize != Header::SerializedLength())
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped DHCP packet (invalid buffer size %lu, expected %lu)\r\n", "Dropped DHCP packet (invalid buffer size %lu, expected %lu)\r\n",
size, Header::SerializedLength() size,
); Header::SerializedLength());
return; return;
} }
if (header.opcode != Opcode::BootReply) return; if (header.opcode != Opcode::BootReply)
if (header.hardwareAddressType != 1) return; return;
if (header.hardwareAddressLength != 6) return; if (header.hardwareAddressType != 1)
if (header.transactionId != transactionId) return; return;
if (header.hardwareAddressLength != 6)
return;
if (header.transactionId != transactionId)
return;
if (!serverSelected) if (!serverSelected)
{ {

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include "net.h"
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net.h"
namespace Net::Dhcp namespace Net::Dhcp
{ {
@ -66,33 +67,17 @@ namespace Net::Dhcp
constexpr static size_t SerializedLength() constexpr static size_t SerializedLength()
{ {
return return sizeof(Opcode) + sizeof(hardwareAddressType) + sizeof(hardwareAddressLength) +
sizeof(Opcode) + sizeof(hops) + sizeof(transactionId) + sizeof(secondsElapsed) + sizeof(flags) +
sizeof(hardwareAddressType) + sizeof(clientIpAddress) + sizeof(yourIpAddress) + sizeof(serverIpAddress) +
sizeof(hardwareAddressLength) + sizeof(relayIpAddress) + sizeof(clientHardwareAddress) + sizeof(serverHostname) +
sizeof(hops) + sizeof(bootFile) + sizeof(magicValue);
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; size_t Serialize(uint8_t* buffer, const size_t size) const;
static size_t Deserialize( static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size);
Header& out, const uint8_t* buffer, const size_t size);
}; };
void ObtainIp(std::function<void()>& callback); void ObtainIp(std::function<void()>& callback);
void HandlePacket( void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size);
const Ethernet::Header& ethernetHeader,
const uint8_t* buffer,
size_t size
);
} // namespace Net::Dhcp } // namespace Net::Dhcp

View file

@ -1,32 +1,25 @@
#include <cstring> #include <cstring>
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ethernet namespace Net::Ethernet
{ {
Header::Header() Header::Header() {}
{}
Header::Header(EtherType type) : Header::Header(EtherType type) :
macDestination(Utils::MacBroadcast), macDestination(Utils::MacBroadcast), macSource{0, 0, 0, 0, 0, 0}, type(type)
macSource{0, 0, 0, 0, 0, 0}, {
type(type) }
{}
Header::Header( Header::Header(MacAddress macSource, EtherType type) :
MacAddress macSource, EtherType type macDestination(Utils::MacBroadcast), macSource(macSource), type(type)
) : {
macDestination(Utils::MacBroadcast), }
macSource(macSource),
type(type)
{}
Header::Header( Header::Header(MacAddress macDestination, MacAddress macSource, EtherType type) :
MacAddress macDestination, MacAddress macSource, EtherType type macDestination(macDestination), macSource(macSource), type(type)
) : {
macDestination(macDestination), }
macSource(macSource),
type(type)
{}
size_t Header::Serialize(uint8_t* buffer, const size_t size) const size_t Header::Serialize(uint8_t* buffer, const size_t size) const
{ {

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <array> #include <array>
#include "net-utils.h" #include "net-utils.h"
namespace Net::Ethernet namespace Net::Ethernet
@ -29,7 +30,6 @@ namespace Net::Ethernet
} }
size_t Serialize(uint8_t* buffer, const size_t size) const; size_t Serialize(uint8_t* buffer, const size_t size) const;
static size_t Deserialize( static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size);
Header& out, const uint8_t* buffer, const size_t size);
}; };
} // namespace Net::Ethernet } // namespace Net::Ethernet

View file

@ -30,9 +30,8 @@ namespace Net::Icmp
return i; return i;
} }
size_t Header::Deserialize( size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
Header& out, const uint8_t* buffer, const size_t bufferSize {
) {
if (bufferSize < SerializedLength()) if (bufferSize < SerializedLength())
{ {
return 0; return 0;
@ -49,7 +48,9 @@ namespace Net::Icmp
// //
EchoHeader::EchoHeader() : EchoHeader(0, 0) {} EchoHeader::EchoHeader() : EchoHeader(0, 0) {}
EchoHeader::EchoHeader(uint16_t identifier, uint16_t sequenceNumber) : EchoHeader::EchoHeader(uint16_t identifier, uint16_t sequenceNumber) :
identifier(identifier), sequenceNumber(sequenceNumber) {} identifier(identifier), sequenceNumber(sequenceNumber)
{
}
size_t EchoHeader::Serialize(uint8_t* buffer, const size_t bufferSize) const size_t EchoHeader::Serialize(uint8_t* buffer, const size_t bufferSize) const
{ {
@ -66,9 +67,8 @@ namespace Net::Icmp
return i; return i;
} }
size_t EchoHeader::Deserialize( size_t EchoHeader::Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize)
EchoHeader& out, const uint8_t* buffer, const size_t bufferSize {
) {
if (bufferSize < SerializedLength()) if (bufferSize < SerializedLength())
{ {
return 0; return 0;
@ -85,13 +85,10 @@ namespace Net::Icmp
Icmp::EchoHeader pingHeader(0, 0); Icmp::EchoHeader pingHeader(0, 0);
size_t ipv4TotalSize = Icmp::Header::SerializedLength() + size_t ipv4TotalSize = Icmp::Header::SerializedLength() +
Icmp::EchoHeader::SerializedLength() + Icmp::EchoHeader::SerializedLength() + Ipv4::Header::SerializedLength();
Ipv4::Header::SerializedLength(); Ipv4::Header ipv4Header(Ipv4::Protocol::Icmp, Utils::Ipv4Address, ip, ipv4TotalSize);
Ipv4::Header ipv4Header(
Ipv4::Protocol::Icmp, Utils::Ipv4Address, ip, ipv4TotalSize);
Ethernet::Header ethernetHeader( Ethernet::Header ethernetHeader(mac, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
mac, Utils::GetMacAddress(), 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;
@ -101,10 +98,8 @@ namespace Net::Icmp
size += pingHeader.Serialize(buffer + size, sizeof(buffer) - size); size += pingHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += icmpHeader.Serialize(buffer + size, sizeof(buffer) - size); size += icmpHeader.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = const auto expectedSize = ethernetHeader.SerializedLength() +
ethernetHeader.SerializedLength() + ipv4Header.SerializedLength() + pingHeader.SerializedLength() +
ipv4Header.SerializedLength() +
pingHeader.SerializedLength() +
icmpHeader.SerializedLength(); icmpHeader.SerializedLength();
assert(size == expectedSize); assert(size == expectedSize);
assert(size <= sizeof(buffer)); assert(size <= sizeof(buffer));
@ -117,18 +112,17 @@ namespace Net::Icmp
const Ipv4::Header& reqIpv4Header, const Ipv4::Header& reqIpv4Header,
const Icmp::Header& reqIcmpHeader, const Icmp::Header& reqIcmpHeader,
const uint8_t* reqBuffer, const uint8_t* reqBuffer,
const size_t reqBufferSize const size_t reqBufferSize)
) { {
EchoHeader reqEchoHeader; EchoHeader reqEchoHeader;
const auto reqEchoHeaderSize = const auto reqEchoHeaderSize =
Icmp::EchoHeader::Deserialize(reqEchoHeader, reqBuffer, reqBufferSize); Icmp::EchoHeader::Deserialize(reqEchoHeader, reqBuffer, reqBufferSize);
if (reqEchoHeaderSize == 0 || reqBufferSize < reqEchoHeaderSize) if (reqEchoHeaderSize == 0 || reqBufferSize < reqEchoHeaderSize)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped ICMP packet " "Dropped ICMP packet (invalid buffer size %ul, expected at least %ul)\r\n",
"(invalid buffer size %ul, expected at least %ul)\r\n", reqBufferSize,
reqBufferSize, EchoHeader::SerializedLength() EchoHeader::SerializedLength());
);
return; return;
} }
@ -137,41 +131,27 @@ namespace Net::Icmp
Ipv4::Protocol::Icmp, Ipv4::Protocol::Icmp,
Utils::Ipv4Address, Utils::Ipv4Address,
reqIpv4Header.sourceIp, reqIpv4Header.sourceIp,
reqIpv4Header.totalLength reqIpv4Header.totalLength);
);
const Ethernet::Header respEthernetHeader( const Ethernet::Header respEthernetHeader(
reqEthernetHeader.macSource, reqEthernetHeader.macSource, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
Utils::GetMacAddress(),
Ethernet::EtherType::Ipv4
);
const auto payloadSize = const auto payloadSize = reqIpv4Header.totalLength - reqIpv4Header.SerializedLength() -
reqIpv4Header.totalLength - reqIcmpHeader.SerializedLength() - reqEchoHeaderSize;
reqIpv4Header.SerializedLength() -
reqIcmpHeader.SerializedLength() -
reqEchoHeaderSize;
std::array<uint8_t, USPI_FRAME_BUFFER_SIZE> respBuffer; std::array<uint8_t, USPI_FRAME_BUFFER_SIZE> respBuffer;
size_t respSize = 0; size_t respSize = 0;
respSize += respEthernetHeader.Serialize( respSize += respEthernetHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize); respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIpv4Header.Serialize( respSize +=
respBuffer.data() + respSize, respBuffer.size() - respSize); respIpv4Header.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIcmpHeader.Serialize( respSize +=
respBuffer.data() + respSize, respBuffer.size() - respSize); respIcmpHeader.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize);
std::memcpy( std::memcpy(respBuffer.data() + respSize, reqBuffer + reqEchoHeaderSize, payloadSize);
respBuffer.data() + respSize,
reqBuffer + reqEchoHeaderSize,
payloadSize
);
respSize += payloadSize; respSize += payloadSize;
const auto expectedRespSize = const auto expectedRespSize = respEthernetHeader.SerializedLength() +
respEthernetHeader.SerializedLength() + respIpv4Header.SerializedLength() + respIcmpHeader.SerializedLength() + payloadSize;
respIpv4Header.SerializedLength() +
respIcmpHeader.SerializedLength() +
payloadSize;
assert(respSize == expectedRespSize); assert(respSize == expectedRespSize);
assert(respSize <= respBuffer.size()); assert(respSize <= respBuffer.size());
@ -188,24 +168,21 @@ namespace Net::Icmp
ethernetHeader, buffer + headerSize, bufferSize - headerSize); ethernetHeader, buffer + headerSize, bufferSize - headerSize);
Ipv4::Header ipv4Header; Ipv4::Header ipv4Header;
headerSize += Ipv4::Header::Deserialize( headerSize +=
ipv4Header, buffer + headerSize, bufferSize - headerSize); Ipv4::Header::Deserialize(ipv4Header, buffer + headerSize, bufferSize - headerSize);
Header icmpHeader; Header icmpHeader;
headerSize += Icmp::Header::Deserialize( headerSize +=
icmpHeader, buffer + headerSize, bufferSize - headerSize); Icmp::Header::Deserialize(icmpHeader, buffer + headerSize, bufferSize - headerSize);
const auto expectedHeaderSize = const auto expectedHeaderSize = ethernetHeader.SerializedLength() +
ethernetHeader.SerializedLength() + ipv4Header.SerializedLength() + icmpHeader.SerializedLength();
ipv4Header.SerializedLength() +
icmpHeader.SerializedLength();
if (headerSize != expectedHeaderSize) if (headerSize != expectedHeaderSize)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped ICMP packet " "Dropped ICMP packet (invalid buffer size %ul, expected at least %ul)\r\n",
"(invalid buffer size %ul, expected at least %ul)\r\n", bufferSize,
bufferSize, expectedHeaderSize expectedHeaderSize);
);
} }
if (icmpHeader.type == Type::EchoRequest) if (icmpHeader.type == Type::EchoRequest)
@ -215,8 +192,7 @@ namespace Net::Icmp
ipv4Header, ipv4Header,
icmpHeader, icmpHeader,
buffer + headerSize, buffer + headerSize,
bufferSize - headerSize bufferSize - headerSize);
);
} }
} }
} // namespace Net::Icmp } // namespace Net::Icmp

View file

@ -24,8 +24,7 @@ namespace Net::Icmp
} }
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
static size_t Deserialize( static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize);
Header& out, const uint8_t* buffer, const size_t bufferSize);
}; };
struct EchoHeader struct EchoHeader
@ -42,8 +41,7 @@ namespace Net::Icmp
} }
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
static size_t Deserialize( static size_t Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize);
EchoHeader& out, const uint8_t* buffer, const size_t bufferSize);
}; };
void SendEchoRequest(const Utils::MacAddress mac, const uint32_t ip); void SendEchoRequest(const Utils::MacAddress mac, const uint32_t ip);

View file

@ -1,9 +1,9 @@
#include <cassert> #include <cassert>
#include "net-ipv4.h"
#include "net-ethernet.h"
#include "net-arp.h" #include "net-arp.h"
#include "net-ethernet.h"
#include "net-icmp.h" #include "net-icmp.h"
#include "net-ipv4.h"
#include "net-udp.h" #include "net-udp.h"
#include "net-utils.h" #include "net-utils.h"
@ -14,8 +14,7 @@ namespace Net::Ipv4
Header::Header() {} Header::Header() {}
Header::Header( Header::Header(
Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength) :
) :
version(4), version(4),
ihl(5), ihl(5),
dscp(0), dscp(0),
@ -29,7 +28,8 @@ namespace Net::Ipv4
headerChecksum(0), headerChecksum(0),
sourceIp(sourceIp), sourceIp(sourceIp),
destinationIp(destinationIp) destinationIp(destinationIp)
{} {
}
size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const
{ {
@ -70,9 +70,8 @@ namespace Net::Ipv4
return i; return i;
} }
size_t Header::Deserialize( size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
Header& out, const uint8_t* buffer, const size_t bufferSize {
) {
if (bufferSize <= SerializedLength()) if (bufferSize <= SerializedLength())
{ {
return 0; return 0;
@ -95,36 +94,36 @@ namespace Net::Ipv4
out.headerChecksum = buffer[10] << 8 | buffer[11]; out.headerChecksum = buffer[10] << 8 | buffer[11];
out.sourceIp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15]; out.sourceIp = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15];
out.destinationIp = out.destinationIp = buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
buffer[16] << 24 | buffer[17] << 16 | buffer[18] << 8 | buffer[19];
return 20; return 20;
} }
void HandlePacket( void HandlePacket(
const Ethernet::Header& ethernetHeader, const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize)
const uint8_t* buffer, {
const size_t bufferSize
) {
Header header; Header header;
const auto headerSize = Header::Deserialize(header, buffer, bufferSize); const auto headerSize = Header::Deserialize(header, buffer, bufferSize);
if (headerSize != Header::SerializedLength()) if (headerSize != Header::SerializedLength())
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped IPv4 packet (invalid buffer size %lu, expected at least %lu)\r\n" "Dropped IPv4 packet (invalid buffer size %lu, expected at least %lu)\r\n",
bufferSize, headerSize bufferSize,
); headerSize);
return; return;
} }
// Update ARP table // Update ARP table
Arp::ArpTable.insert( Arp::ArpTable.insert(std::make_pair(header.sourceIp, ethernetHeader.macSource));
std::make_pair(header.sourceIp, ethernetHeader.macSource));
if (header.version != 4) return; if (header.version != 4)
if (header.ihl != 5) return; // Not supported return;
if (header.destinationIp != Utils::Ipv4Address) return; if (header.ihl != 5)
if (header.fragmentOffset != 0) return; // TODO Support this return; // Not supported
if (header.destinationIp != Utils::Ipv4Address)
return;
if (header.fragmentOffset != 0)
return; // TODO Support this
if (header.protocol == Ipv4::Protocol::Icmp) if (header.protocol == Ipv4::Protocol::Icmp)
{ {
@ -132,8 +131,7 @@ namespace Net::Ipv4
} }
else if (header.protocol == Ipv4::Protocol::Udp) else if (header.protocol == Ipv4::Protocol::Udp)
{ {
Udp::HandlePacket( Udp::HandlePacket(ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
} }
} }
} // namespace Net::Ipv4 } // namespace Net::Ipv4

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint>
#include <cstddef> #include <cstddef>
#include <cstdint>
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ipv4 namespace Net::Ipv4
@ -29,12 +30,7 @@ namespace Net::Ipv4
uint32_t destinationIp; uint32_t destinationIp;
Header(); Header();
Header( Header(Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength);
Protocol protocol,
uint32_t sourceIp,
uint32_t destinationIp,
uint16_t totalLength
);
static constexpr size_t SerializedLength() static constexpr size_t SerializedLength()
{ {
@ -43,13 +39,9 @@ namespace Net::Ipv4
} }
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const; size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
static size_t Deserialize( static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize);
Header& out, const uint8_t* buffer, const size_t bufferSize);
}; };
void HandlePacket( void HandlePacket(
const Ethernet::Header& ethernetHeader, const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize);
const uint8_t* buffer,
const size_t bufferSize
);
} // namespace Net::Ipv4 } // namespace Net::Ipv4

View file

@ -1,6 +1,6 @@
#include <memory>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <memory>
#include "net-arp.h" #include "net-arp.h"
#include "net-ethernet.h" #include "net-ethernet.h"
@ -24,17 +24,17 @@ namespace Net::Tftp
Packet::Packet() : opcode(static_cast<Opcode>(0)) {} Packet::Packet() : opcode(static_cast<Opcode>(0)) {}
Packet::Packet(const Opcode opcode) : opcode(opcode) {} Packet::Packet(const Opcode opcode) : opcode(opcode) {}
static std::unique_ptr<Packet> handleTftpWriteRequest( static std::unique_ptr<Packet>
const uint8_t* data, const size_t dataSize handleTftpWriteRequest(const uint8_t* data, const size_t dataSize)
) { {
WriteReadRequestPacket packet; WriteReadRequestPacket packet;
const auto size = packet.Deserialize(data, dataSize); const auto size = packet.Deserialize(data, dataSize);
if (size == 0) if (size == 0)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped TFTP packet (invalid buffer size %lu, expected at least %lu)\r\n", "Dropped TFTP packet (invalid buffer size %lu, expected at least %lu)\r\n",
dataSize, sizeof(WriteReadRequestPacket::opcode) + 2 dataSize,
) sizeof(WriteReadRequestPacket::opcode) + 2)
return nullptr; return nullptr;
} }
@ -71,17 +71,14 @@ namespace Net::Tftp
std::unique_ptr<Packet> response; std::unique_ptr<Packet> response;
if (result != FR_OK) if (result != FR_OK)
{ {
response = std::unique_ptr<ErrorPacket>( response =
new ErrorPacket(0, "error opening target file") std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "error opening target file"));
);
} }
else else
{ {
shouldReboot = shouldReboot = packet.filename == "kernel.img" || packet.filename == "options.txt";
packet.filename == "kernel.img" || packet.filename == "options.txt";
response = std::unique_ptr<AcknowledgementPacket>( response = std::unique_ptr<AcknowledgementPacket>(
new AcknowledgementPacket(currentBlockNumber) new AcknowledgementPacket(currentBlockNumber));
);
} }
// TODO Return to the original working directory here // TODO Return to the original working directory here
@ -96,19 +93,16 @@ namespace Net::Tftp
if (tftpSize == 0) if (tftpSize == 0)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped TFTP data packet " "Dropped TFTP data packet (invalid buffer size %lu, expected at least %lu)\r\n",
"(invalid buffer size %lu, expected at least %lu)\r\n", size,
size, sizeof(packet.opcode) + sizeof(packet.blockNumber) sizeof(packet.opcode) + sizeof(packet.blockNumber))
)
return nullptr; return nullptr;
} }
if (packet.blockNumber != currentBlockNumber + 1) if (packet.blockNumber != currentBlockNumber + 1)
{ {
f_close(&outFile); f_close(&outFile);
return std::unique_ptr<ErrorPacket>( return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "invalid block number"));
new ErrorPacket(0, "invalid block number")
);
} }
currentBlockNumber = packet.blockNumber; currentBlockNumber = packet.blockNumber;
@ -129,8 +123,7 @@ namespace Net::Tftp
} }
return std::unique_ptr<AcknowledgementPacket>( return std::unique_ptr<AcknowledgementPacket>(
new AcknowledgementPacket(currentBlockNumber) new AcknowledgementPacket(currentBlockNumber));
);
} }
void HandlePacket( void HandlePacket(
@ -138,8 +131,8 @@ namespace Net::Tftp
const Ipv4::Header ipv4ReqHeader, const Ipv4::Header ipv4ReqHeader,
const Udp::Header udpReqHeader, const Udp::Header udpReqHeader,
const uint8_t* reqBuffer, const uint8_t* reqBuffer,
const size_t reqBufferSize const size_t reqBufferSize)
) { {
const auto opcode = static_cast<Opcode>(reqBuffer[0] << 8 | reqBuffer[1]); const auto opcode = static_cast<Opcode>(reqBuffer[0] << 8 | reqBuffer[1]);
std::unique_ptr<Packet> response; std::unique_ptr<Packet> response;
@ -148,8 +141,8 @@ namespace Net::Tftp
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped TFTP packet (invalid buffer size %lu, expected at least %lu)\r\n", "Dropped TFTP packet (invalid buffer size %lu, expected at least %lu)\r\n",
reqBufferSize, payloadSize reqBufferSize,
); payloadSize);
} }
if (opcode == Opcode::WriteRequest) if (opcode == Opcode::WriteRequest)
@ -162,9 +155,7 @@ namespace Net::Tftp
} }
else else
{ {
response = std::unique_ptr<ErrorPacket>( response = std::unique_ptr<ErrorPacket>(new ErrorPacket(4, "not implemented yet"));
new ErrorPacket(4, "not implemented yet")
);
} }
if (response != nullptr) if (response != nullptr)
@ -172,19 +163,16 @@ namespace Net::Tftp
Udp::Header udpRespHeader( Udp::Header udpRespHeader(
udpReqHeader.destinationPort, udpReqHeader.destinationPort,
udpReqHeader.sourcePort, udpReqHeader.sourcePort,
response->SerializedLength() + Udp::Header::SerializedLength() response->SerializedLength() + Udp::Header::SerializedLength());
);
Ipv4::Header ipv4RespHeader( Ipv4::Header ipv4RespHeader(
Ipv4::Protocol::Udp, Ipv4::Protocol::Udp,
Utils::Ipv4Address, Utils::Ipv4Address,
ipv4ReqHeader.sourceIp, ipv4ReqHeader.sourceIp,
udpRespHeader.length + Ipv4::Header::SerializedLength() udpRespHeader.length + Ipv4::Header::SerializedLength());
);
Ethernet::Header ethernetRespHeader( Ethernet::Header ethernetRespHeader(
Arp::ArpTable[ipv4RespHeader.destinationIp], Arp::ArpTable[ipv4RespHeader.destinationIp],
Utils::GetMacAddress(), Utils::GetMacAddress(),
Ethernet::EtherType::Ipv4 Ethernet::EtherType::Ipv4);
);
size_t size = 0; size_t size = 0;
uint8_t buffer[USPI_FRAME_BUFFER_SIZE]; uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
@ -193,10 +181,8 @@ namespace Net::Tftp
size += udpRespHeader.Serialize(buffer + size, sizeof(buffer) - size); size += udpRespHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += response->Serialize(buffer + size, sizeof(buffer) - size); size += response->Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = const auto expectedSize = ethernetRespHeader.SerializedLength() +
ethernetRespHeader.SerializedLength() + ipv4RespHeader.SerializedLength() + udpRespHeader.SerializedLength() +
ipv4RespHeader.SerializedLength() +
udpRespHeader.SerializedLength() +
response->SerializedLength(); response->SerializedLength();
assert(size == expectedSize); assert(size == expectedSize);
assert(size <= sizeof(buffer)); assert(size <= sizeof(buffer));
@ -211,16 +197,14 @@ namespace Net::Tftp
// WriteReadRequestPacket // WriteReadRequestPacket
// //
WriteReadRequestPacket::WriteReadRequestPacket() : Packet() {} WriteReadRequestPacket::WriteReadRequestPacket() : Packet() {}
WriteReadRequestPacket::WriteReadRequestPacket(const Opcode opcode) : Packet(opcode) WriteReadRequestPacket::WriteReadRequestPacket(const Opcode opcode) : Packet(opcode) {}
{}
size_t WriteReadRequestPacket::SerializedLength() const size_t WriteReadRequestPacket::SerializedLength() const
{ {
return sizeof(opcode) + filename.size() + 1 + mode.size() + 1; return sizeof(opcode) + filename.size() + 1 + mode.size() + 1;
} }
size_t WriteReadRequestPacket::Serialize(uint8_t* buffer, const size_t bufferSize) size_t WriteReadRequestPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
const
{ {
if (bufferSize < SerializedLength()) if (bufferSize < SerializedLength())
{ {
@ -240,24 +224,26 @@ namespace Net::Tftp
return i; return i;
} }
size_t WriteReadRequestPacket::Deserialize( size_t WriteReadRequestPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
const uint8_t* buffer, const size_t bufferSize {
) {
// Can't use SerializedLength here, as it's variable. // Can't use SerializedLength here, as it's variable.
// Check for each field instead. // Check for each field instead.
size_t i = 0; size_t i = 0;
if (sizeof(Opcode) >= bufferSize - i) return 0; if (sizeof(Opcode) >= bufferSize - i)
return 0;
opcode = static_cast<Opcode>(buffer[i] << 8 | buffer[i + 1]); opcode = static_cast<Opcode>(buffer[i] << 8 | buffer[i + 1]);
i += 2; i += 2;
const char* filenameStr = reinterpret_cast<const char*>(buffer + i); const char* filenameStr = reinterpret_cast<const char*>(buffer + i);
if (std::strlen(filenameStr) + 1 >= bufferSize - i) return 0; if (std::strlen(filenameStr) + 1 >= bufferSize - i)
return 0;
filename = std::string(filenameStr); filename = std::string(filenameStr);
i += filename.size() + 1; i += filename.size() + 1;
const char* modeStr = reinterpret_cast<const char*>(buffer + i); const char* modeStr = reinterpret_cast<const char*>(buffer + i);
if (std::strlen(modeStr) + 1 >= bufferSize - i) return 0; if (std::strlen(modeStr) + 1 >= bufferSize - i)
return 0;
mode = std::string(modeStr); mode = std::string(modeStr);
i += mode.size() + 1; i += mode.size() + 1;
@ -270,7 +256,8 @@ namespace Net::Tftp
ErrorPacket::ErrorPacket() : Packet(Opcode::Error) {} ErrorPacket::ErrorPacket() : Packet(Opcode::Error) {}
ErrorPacket::ErrorPacket(uint16_t errorCode, std::string message) : ErrorPacket::ErrorPacket(uint16_t errorCode, std::string message) :
Packet(Opcode::Error), errorCode(errorCode), message(message) Packet(Opcode::Error), errorCode(errorCode), message(message)
{} {
}
size_t ErrorPacket::SerializedLength() const size_t ErrorPacket::SerializedLength() const
{ {
@ -303,15 +290,15 @@ namespace Net::Tftp
AcknowledgementPacket::AcknowledgementPacket(uint16_t blockNumber) : AcknowledgementPacket::AcknowledgementPacket(uint16_t blockNumber) :
Packet(Opcode::Acknowledgement), blockNumber(blockNumber) Packet(Opcode::Acknowledgement), blockNumber(blockNumber)
{} {
}
size_t AcknowledgementPacket::SerializedLength() const size_t AcknowledgementPacket::SerializedLength() const
{ {
return sizeof(opcode) + sizeof(blockNumber); return sizeof(opcode) + sizeof(blockNumber);
} }
size_t AcknowledgementPacket::Serialize(uint8_t* buffer, const size_t bufferSize) size_t AcknowledgementPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
const
{ {
if (bufferSize < SerializedLength()) if (bufferSize < SerializedLength())
{ {
@ -329,8 +316,7 @@ namespace Net::Tftp
// //
// DataPacket // DataPacket
// //
DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0) DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0) {}
{}
size_t DataPacket::SerializedLength() const size_t DataPacket::SerializedLength() const
{ {
@ -356,7 +342,8 @@ namespace Net::Tftp
return i; return i;
} }
size_t DataPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize) { size_t DataPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
{
if (bufferSize < sizeof(opcode) + sizeof(blockNumber)) if (bufferSize < sizeof(opcode) + sizeof(blockNumber))
{ {
return 0; return 0;

View file

@ -2,6 +2,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#include "net-udp.h" #include "net-udp.h"
namespace Net::Tftp namespace Net::Tftp
@ -76,6 +77,5 @@ namespace Net::Tftp
const Ipv4::Header ipv4ReqHeader, const Ipv4::Header ipv4ReqHeader,
const Udp::Header udpReqHeader, const Udp::Header udpReqHeader,
const uint8_t* data, const uint8_t* data,
const size_t dataSize const size_t dataSize);
);
} // namespace Net::Tftp } // namespace Net::Tftp

View file

@ -6,19 +6,12 @@
namespace Net::Udp namespace Net::Udp
{ {
Header::Header() Header::Header() {}
{}
Header::Header( Header::Header(Port sourcePort, Port destinationPort, uint16_t length) :
Port sourcePort, sourcePort(sourcePort), destinationPort(destinationPort), length(length), checksum(0)
Port destinationPort, {
uint16_t length }
) :
sourcePort(sourcePort),
destinationPort(destinationPort),
length(length),
checksum(0)
{}
size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const
{ {
@ -57,24 +50,24 @@ namespace Net::Udp
const Ethernet::Header ethernetHeader, const Ethernet::Header ethernetHeader,
const Ipv4::Header ipv4Header, const Ipv4::Header ipv4Header,
const uint8_t* buffer, const uint8_t* buffer,
const size_t bufferSize const size_t bufferSize)
) { {
Header udpHeader; Header udpHeader;
const auto headerSize = udpHeader.Deserialize(buffer, bufferSize); const auto headerSize = udpHeader.Deserialize(buffer, bufferSize);
if (headerSize == 0 || headerSize != udpHeader.SerializedLength()) if (headerSize == 0 || headerSize != udpHeader.SerializedLength())
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped UDP header (invalid buffer size %lu, expected at least %lu)\r\n", "Dropped UDP header (invalid buffer size %lu, expected at least %lu)\r\n",
bufferSize, Header::SerializedLength() bufferSize,
); Header::SerializedLength());
return; return;
} }
if (udpHeader.length <= bufferSize) if (udpHeader.length <= bufferSize)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped UDP packet (invalid buffer size %lu, expected at least %lu)\r\n", "Dropped UDP packet (invalid buffer size %lu, expected at least %lu)\r\n",
bufferSize, udpHeader.length bufferSize,
); udpHeader.length);
return; return;
} }
@ -83,8 +76,7 @@ namespace Net::Udp
Dhcp::HandlePacket( Dhcp::HandlePacket(
ethernetHeader, ethernetHeader,
buffer + udpHeader.SerializedLength(), buffer + udpHeader.SerializedLength(),
bufferSize - udpHeader.SerializedLength() bufferSize - udpHeader.SerializedLength());
);
} }
else if (udpHeader.destinationPort == Port::Tftp) else if (udpHeader.destinationPort == Port::Tftp)
{ {
@ -93,8 +85,7 @@ namespace Net::Udp
ipv4Header, ipv4Header,
udpHeader, udpHeader,
buffer + udpHeader.SerializedLength(), buffer + udpHeader.SerializedLength(),
bufferSize - udpHeader.SerializedLength() bufferSize - udpHeader.SerializedLength());
);
} }
} }
} // namespace Net::Udp } // namespace Net::Udp

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include <vector>
#include <string>
#include <cstdint> #include <cstdint>
#include <string>
#include <vector>
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-ipv4.h" #include "net-ipv4.h"
@ -26,11 +27,7 @@ namespace Net::Udp
static constexpr size_t SerializedLength() static constexpr size_t SerializedLength()
{ {
return return sizeof(sourcePort) + sizeof(destinationPort) + sizeof(length) + sizeof(checksum);
sizeof(sourcePort) +
sizeof(destinationPort) +
sizeof(length) +
sizeof(checksum);
} }
size_t Serialize(uint8_t* buffer, const size_t size) const; size_t Serialize(uint8_t* buffer, const size_t size) const;
@ -41,6 +38,5 @@ namespace Net::Udp
const Ethernet::Header ethernetHeader, const Ethernet::Header ethernetHeader,
const Ipv4::Header ipv4Header, const Ipv4::Header ipv4Header,
const uint8_t* buffer, const uint8_t* buffer,
const size_t size const size_t size);
);
} // namespace Net::Udp } // namespace Net::Udp

View file

@ -8,75 +8,52 @@ namespace Net::Utils
const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint32_t Ipv4Address = 0xC0A80164; uint32_t Ipv4Address = 0xC0A80164;
static const uint32_t crcTab32[256] = static const uint32_t crcTab32[256] = {
{ 0x00000000ul, 0x77073096ul, 0xEE0E612Cul, 0x990951BAul, 0x076DC419ul, 0x706AF48Ful,
0x00000000ul, 0x77073096ul, 0xEE0E612Cul, 0x990951BAul, 0xE963A535ul, 0x9E6495A3ul, 0x0EDB8832ul, 0x79DCB8A4ul, 0xE0D5E91Eul, 0x97D2D988ul,
0x076DC419ul, 0x706AF48Ful, 0xE963A535ul, 0x9E6495A3ul, 0x09B64C2Bul, 0x7EB17CBDul, 0xE7B82D07ul, 0x90BF1D91ul, 0x1DB71064ul, 0x6AB020F2ul,
0x0EDB8832ul, 0x79DCB8A4ul, 0xE0D5E91Eul, 0x97D2D988ul, 0xF3B97148ul, 0x84BE41DEul, 0x1ADAD47Dul, 0x6DDDE4EBul, 0xF4D4B551ul, 0x83D385C7ul,
0x09B64C2Bul, 0x7EB17CBDul, 0xE7B82D07ul, 0x90BF1D91ul, 0x136C9856ul, 0x646BA8C0ul, 0xFD62F97Aul, 0x8A65C9ECul, 0x14015C4Ful, 0x63066CD9ul,
0x1DB71064ul, 0x6AB020F2ul, 0xF3B97148ul, 0x84BE41DEul, 0xFA0F3D63ul, 0x8D080DF5ul, 0x3B6E20C8ul, 0x4C69105Eul, 0xD56041E4ul, 0xA2677172ul,
0x1ADAD47Dul, 0x6DDDE4EBul, 0xF4D4B551ul, 0x83D385C7ul, 0x3C03E4D1ul, 0x4B04D447ul, 0xD20D85FDul, 0xA50AB56Bul, 0x35B5A8FAul, 0x42B2986Cul,
0x136C9856ul, 0x646BA8C0ul, 0xFD62F97Aul, 0x8A65C9ECul, 0xDBBBC9D6ul, 0xACBCF940ul, 0x32D86CE3ul, 0x45DF5C75ul, 0xDCD60DCFul, 0xABD13D59ul,
0x14015C4Ful, 0x63066CD9ul, 0xFA0F3D63ul, 0x8D080DF5ul, 0x26D930ACul, 0x51DE003Aul, 0xC8D75180ul, 0xBFD06116ul, 0x21B4F4B5ul, 0x56B3C423ul,
0x3B6E20C8ul, 0x4C69105Eul, 0xD56041E4ul, 0xA2677172ul, 0xCFBA9599ul, 0xB8BDA50Ful, 0x2802B89Eul, 0x5F058808ul, 0xC60CD9B2ul, 0xB10BE924ul,
0x3C03E4D1ul, 0x4B04D447ul, 0xD20D85FDul, 0xA50AB56Bul, 0x2F6F7C87ul, 0x58684C11ul, 0xC1611DABul, 0xB6662D3Dul, 0x76DC4190ul, 0x01DB7106ul,
0x35B5A8FAul, 0x42B2986Cul, 0xDBBBC9D6ul, 0xACBCF940ul, 0x98D220BCul, 0xEFD5102Aul, 0x71B18589ul, 0x06B6B51Ful, 0x9FBFE4A5ul, 0xE8B8D433ul,
0x32D86CE3ul, 0x45DF5C75ul, 0xDCD60DCFul, 0xABD13D59ul, 0x7807C9A2ul, 0x0F00F934ul, 0x9609A88Eul, 0xE10E9818ul, 0x7F6A0DBBul, 0x086D3D2Dul,
0x26D930ACul, 0x51DE003Aul, 0xC8D75180ul, 0xBFD06116ul, 0x91646C97ul, 0xE6635C01ul, 0x6B6B51F4ul, 0x1C6C6162ul, 0x856530D8ul, 0xF262004Eul,
0x21B4F4B5ul, 0x56B3C423ul, 0xCFBA9599ul, 0xB8BDA50Ful, 0x6C0695EDul, 0x1B01A57Bul, 0x8208F4C1ul, 0xF50FC457ul, 0x65B0D9C6ul, 0x12B7E950ul,
0x2802B89Eul, 0x5F058808ul, 0xC60CD9B2ul, 0xB10BE924ul, 0x8BBEB8EAul, 0xFCB9887Cul, 0x62DD1DDFul, 0x15DA2D49ul, 0x8CD37CF3ul, 0xFBD44C65ul,
0x2F6F7C87ul, 0x58684C11ul, 0xC1611DABul, 0xB6662D3Dul, 0x4DB26158ul, 0x3AB551CEul, 0xA3BC0074ul, 0xD4BB30E2ul, 0x4ADFA541ul, 0x3DD895D7ul,
0x76DC4190ul, 0x01DB7106ul, 0x98D220BCul, 0xEFD5102Aul, 0xA4D1C46Dul, 0xD3D6F4FBul, 0x4369E96Aul, 0x346ED9FCul, 0xAD678846ul, 0xDA60B8D0ul,
0x71B18589ul, 0x06B6B51Ful, 0x9FBFE4A5ul, 0xE8B8D433ul, 0x44042D73ul, 0x33031DE5ul, 0xAA0A4C5Ful, 0xDD0D7CC9ul, 0x5005713Cul, 0x270241AAul,
0x7807C9A2ul, 0x0F00F934ul, 0x9609A88Eul, 0xE10E9818ul, 0xBE0B1010ul, 0xC90C2086ul, 0x5768B525ul, 0x206F85B3ul, 0xB966D409ul, 0xCE61E49Ful,
0x7F6A0DBBul, 0x086D3D2Dul, 0x91646C97ul, 0xE6635C01ul, 0x5EDEF90Eul, 0x29D9C998ul, 0xB0D09822ul, 0xC7D7A8B4ul, 0x59B33D17ul, 0x2EB40D81ul,
0x6B6B51F4ul, 0x1C6C6162ul, 0x856530D8ul, 0xF262004Eul, 0xB7BD5C3Bul, 0xC0BA6CADul, 0xEDB88320ul, 0x9ABFB3B6ul, 0x03B6E20Cul, 0x74B1D29Aul,
0x6C0695EDul, 0x1B01A57Bul, 0x8208F4C1ul, 0xF50FC457ul, 0xEAD54739ul, 0x9DD277AFul, 0x04DB2615ul, 0x73DC1683ul, 0xE3630B12ul, 0x94643B84ul,
0x65B0D9C6ul, 0x12B7E950ul, 0x8BBEB8EAul, 0xFCB9887Cul, 0x0D6D6A3Eul, 0x7A6A5AA8ul, 0xE40ECF0Bul, 0x9309FF9Dul, 0x0A00AE27ul, 0x7D079EB1ul,
0x62DD1DDFul, 0x15DA2D49ul, 0x8CD37CF3ul, 0xFBD44C65ul, 0xF00F9344ul, 0x8708A3D2ul, 0x1E01F268ul, 0x6906C2FEul, 0xF762575Dul, 0x806567CBul,
0x4DB26158ul, 0x3AB551CEul, 0xA3BC0074ul, 0xD4BB30E2ul, 0x196C3671ul, 0x6E6B06E7ul, 0xFED41B76ul, 0x89D32BE0ul, 0x10DA7A5Aul, 0x67DD4ACCul,
0x4ADFA541ul, 0x3DD895D7ul, 0xA4D1C46Dul, 0xD3D6F4FBul, 0xF9B9DF6Ful, 0x8EBEEFF9ul, 0x17B7BE43ul, 0x60B08ED5ul, 0xD6D6A3E8ul, 0xA1D1937Eul,
0x4369E96Aul, 0x346ED9FCul, 0xAD678846ul, 0xDA60B8D0ul, 0x38D8C2C4ul, 0x4FDFF252ul, 0xD1BB67F1ul, 0xA6BC5767ul, 0x3FB506DDul, 0x48B2364Bul,
0x44042D73ul, 0x33031DE5ul, 0xAA0A4C5Ful, 0xDD0D7CC9ul, 0xD80D2BDAul, 0xAF0A1B4Cul, 0x36034AF6ul, 0x41047A60ul, 0xDF60EFC3ul, 0xA867DF55ul,
0x5005713Cul, 0x270241AAul, 0xBE0B1010ul, 0xC90C2086ul, 0x316E8EEFul, 0x4669BE79ul, 0xCB61B38Cul, 0xBC66831Aul, 0x256FD2A0ul, 0x5268E236ul,
0x5768B525ul, 0x206F85B3ul, 0xB966D409ul, 0xCE61E49Ful, 0xCC0C7795ul, 0xBB0B4703ul, 0x220216B9ul, 0x5505262Ful, 0xC5BA3BBEul, 0xB2BD0B28ul,
0x5EDEF90Eul, 0x29D9C998ul, 0xB0D09822ul, 0xC7D7A8B4ul, 0x2BB45A92ul, 0x5CB36A04ul, 0xC2D7FFA7ul, 0xB5D0CF31ul, 0x2CD99E8Bul, 0x5BDEAE1Dul,
0x59B33D17ul, 0x2EB40D81ul, 0xB7BD5C3Bul, 0xC0BA6CADul, 0x9B64C2B0ul, 0xEC63F226ul, 0x756AA39Cul, 0x026D930Aul, 0x9C0906A9ul, 0xEB0E363Ful,
0xEDB88320ul, 0x9ABFB3B6ul, 0x03B6E20Cul, 0x74B1D29Aul, 0x72076785ul, 0x05005713ul, 0x95BF4A82ul, 0xE2B87A14ul, 0x7BB12BAEul, 0x0CB61B38ul,
0xEAD54739ul, 0x9DD277AFul, 0x04DB2615ul, 0x73DC1683ul, 0x92D28E9Bul, 0xE5D5BE0Dul, 0x7CDCEFB7ul, 0x0BDBDF21ul, 0x86D3D2D4ul, 0xF1D4E242ul,
0xE3630B12ul, 0x94643B84ul, 0x0D6D6A3Eul, 0x7A6A5AA8ul, 0x68DDB3F8ul, 0x1FDA836Eul, 0x81BE16CDul, 0xF6B9265Bul, 0x6FB077E1ul, 0x18B74777ul,
0xE40ECF0Bul, 0x9309FF9Dul, 0x0A00AE27ul, 0x7D079EB1ul, 0x88085AE6ul, 0xFF0F6A70ul, 0x66063BCAul, 0x11010B5Cul, 0x8F659EFFul, 0xF862AE69ul,
0xF00F9344ul, 0x8708A3D2ul, 0x1E01F268ul, 0x6906C2FEul, 0x616BFFD3ul, 0x166CCF45ul, 0xA00AE278ul, 0xD70DD2EEul, 0x4E048354ul, 0x3903B3C2ul,
0xF762575Dul, 0x806567CBul, 0x196C3671ul, 0x6E6B06E7ul, 0xA7672661ul, 0xD06016F7ul, 0x4969474Dul, 0x3E6E77DBul, 0xAED16A4Aul, 0xD9D65ADCul,
0xFED41B76ul, 0x89D32BE0ul, 0x10DA7A5Aul, 0x67DD4ACCul, 0x40DF0B66ul, 0x37D83BF0ul, 0xA9BCAE53ul, 0xDEBB9EC5ul, 0x47B2CF7Ful, 0x30B5FFE9ul,
0xF9B9DF6Ful, 0x8EBEEFF9ul, 0x17B7BE43ul, 0x60B08ED5ul, 0xBDBDF21Cul, 0xCABAC28Aul, 0x53B39330ul, 0x24B4A3A6ul, 0xBAD03605ul, 0xCDD70693ul,
0xD6D6A3E8ul, 0xA1D1937Eul, 0x38D8C2C4ul, 0x4FDFF252ul, 0x54DE5729ul, 0x23D967BFul, 0xB3667A2Eul, 0xC4614AB8ul, 0x5D681B02ul, 0x2A6F2B94ul,
0xD1BB67F1ul, 0xA6BC5767ul, 0x3FB506DDul, 0x48B2364Bul, 0xB40BBE37ul, 0xC30C8EA1ul, 0x5A05DF1Bul, 0x2D02EF8Dul};
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 Crc32(const uint8_t* buffer, size_t size)
{ {
uint32_t crc = 0xFFFFFFFFul; uint32_t crc = 0xFFFFFFFFul;

View file

@ -1,6 +1,6 @@
#include <cstdint>
#include <cstddef>
#include <array> #include <array>
#include <cstddef>
#include <cstdint>
namespace Net::Utils namespace Net::Utils
{ {
@ -11,4 +11,4 @@ namespace Net::Utils
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