Run clang-format on net files
This commit is contained in:
parent
9e41e71dfa
commit
24002b225a
16 changed files with 260 additions and 395 deletions
|
@ -1,5 +1,12 @@
|
|||
---
|
||||
---
|
||||
AlignAfterOpenBracket: AlwaysBreak
|
||||
AlignConsecutiveMacros: 'false'
|
||||
AlignConsecutiveAssignments: 'false'
|
||||
AlignConsecutiveDeclarations: 'false'
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AlignOperands: 'false'
|
||||
AlignTrailingComments: 'false'
|
||||
AllowAllArgumentsOnNextLine: 'true'
|
||||
AllowShortBlocksOnASingleLine: 'false'
|
||||
BinPackArguments: 'false'
|
||||
BinPackParameters: 'false'
|
||||
|
@ -14,5 +21,4 @@ PointerAlignment: Left
|
|||
SortIncludes: 'true'
|
||||
TabWidth: '4'
|
||||
UseTab: Always
|
||||
|
||||
...
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <unordered_map>
|
||||
|
||||
#include "net-ethernet.h"
|
||||
#include "net-utils.h"
|
||||
|
||||
|
@ -29,46 +30,36 @@ namespace Net::Arp
|
|||
|
||||
constexpr size_t SerializedLength() const
|
||||
{
|
||||
return
|
||||
sizeof(hardwareType) +
|
||||
sizeof(protocolType) +
|
||||
sizeof(hardwareAddressLength) +
|
||||
sizeof(protocolAddressLength) +
|
||||
sizeof(operation) +
|
||||
senderMac.size() +
|
||||
sizeof(senderIp) +
|
||||
targetMac.size() +
|
||||
sizeof(targetIp);
|
||||
return sizeof(hardwareType) + sizeof(protocolType) + 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 Deserialize(const uint8_t* buffer, const size_t bufferSize);
|
||||
};
|
||||
|
||||
void HandlePacket(
|
||||
const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize);
|
||||
void
|
||||
HandlePacket(const Ethernet::Header header, const uint8_t* buffer, const size_t bufferSize);
|
||||
|
||||
void SendPacket(
|
||||
const Operation operation,
|
||||
const Utils::MacAddress targetMac,
|
||||
const Utils::MacAddress senderMac,
|
||||
const uint32_t senderIp,
|
||||
const uint32_t targetIp
|
||||
);
|
||||
const uint32_t targetIp);
|
||||
|
||||
void SendRequest(
|
||||
const Utils::MacAddress targetMac,
|
||||
const Utils::MacAddress senderMac,
|
||||
const uint32_t senderIp,
|
||||
const uint32_t targetIp
|
||||
);
|
||||
const uint32_t targetIp);
|
||||
|
||||
void SendReply(
|
||||
const Utils::MacAddress targetMac,
|
||||
const Utils::MacAddress senderMac,
|
||||
const uint32_t senderIp,
|
||||
const uint32_t targetIp
|
||||
);
|
||||
const uint32_t targetIp);
|
||||
|
||||
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip);
|
||||
|
||||
|
|
105
src/net-dhcp.cpp
105
src/net-dhcp.cpp
|
@ -1,11 +1,11 @@
|
|||
#include <random>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
|
||||
#include "net-dhcp.h"
|
||||
#include "net-udp.h"
|
||||
#include "net-ipv4.h"
|
||||
#include "net-ethernet.h"
|
||||
#include "net-ipv4.h"
|
||||
#include "net-udp.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "types.h"
|
||||
|
@ -14,8 +14,7 @@
|
|||
|
||||
namespace Net::Dhcp
|
||||
{
|
||||
Header::Header()
|
||||
{}
|
||||
Header::Header() {}
|
||||
|
||||
Header::Header(Opcode opcode, uint32_t transactionId) :
|
||||
opcode(opcode),
|
||||
|
@ -86,9 +85,8 @@ namespace Net::Dhcp
|
|||
return i;
|
||||
}
|
||||
|
||||
size_t Header::Deserialize(
|
||||
Header& out, const uint8_t* buffer, const size_t size
|
||||
) {
|
||||
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t size)
|
||||
{
|
||||
if (size < SerializedLength())
|
||||
{
|
||||
return 0;
|
||||
|
@ -98,24 +96,16 @@ namespace Net::Dhcp
|
|||
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.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];
|
||||
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()
|
||||
);
|
||||
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());
|
||||
|
@ -131,16 +121,12 @@ namespace Net::Dhcp
|
|||
static bool serverSelected;
|
||||
|
||||
void sendRequest(
|
||||
uint32_t clientIpAddress,
|
||||
Utils::MacAddress serverMacAddress,
|
||||
uint32_t serverIpAddress
|
||||
) {
|
||||
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 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(
|
||||
|
@ -155,10 +141,8 @@ namespace Net::Dhcp
|
|||
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() +
|
||||
const auto expectedSize = ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
|
||||
dhcpHeader.SerializedLength();
|
||||
assert(size == expectedSize);
|
||||
assert(size <= sizeof(buffer));
|
||||
|
@ -180,8 +164,7 @@ namespace Net::Dhcp
|
|||
// Send DHCP Requests to every server with that IP address.
|
||||
for (size_t i = 0; i < serverIpAddresses.size(); i++)
|
||||
{
|
||||
sendRequest(
|
||||
Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]);
|
||||
sendRequest(Utils::Ipv4Address, serverMacAddresses[i], serverIpAddresses[i]);
|
||||
}
|
||||
|
||||
// Run the callback indicating an IP has been obtained
|
||||
|
@ -198,15 +181,12 @@ namespace Net::Dhcp
|
|||
offeredIpAddresses.clear();
|
||||
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 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, 0, 0xFFFFFFFF, ipv4Length);
|
||||
const Ethernet::Header ethernetHeader(
|
||||
Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
|
||||
const Ethernet::Header ethernetHeader(Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
|
||||
|
||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||
size_t size = 0;
|
||||
|
@ -216,10 +196,8 @@ namespace Net::Dhcp
|
|||
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() +
|
||||
const auto expectedSize = ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() + udpHeader.SerializedLength() +
|
||||
dhcpHeader.SerializedLength();
|
||||
assert(size == expectedSize);
|
||||
assert(size <= sizeof(buffer));
|
||||
|
@ -236,19 +214,15 @@ namespace Net::Dhcp
|
|||
StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr);
|
||||
}
|
||||
|
||||
static void handleOfferPacket(
|
||||
const Ethernet::Header ethernetHeader,
|
||||
const Header dhcpHeader
|
||||
) {
|
||||
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
|
||||
) {
|
||||
static void handleAckPacket(const Ethernet::Header ethernetHeader, const Header dhcpHeader)
|
||||
{
|
||||
Utils::Ipv4Address = dhcpHeader.yourIpAddress;
|
||||
|
||||
// TODO Schedule handler for end of lease.
|
||||
|
@ -260,26 +234,27 @@ namespace Net::Dhcp
|
|||
serverSelected = false;
|
||||
}
|
||||
|
||||
void HandlePacket(
|
||||
const Ethernet::Header& ethernetHeader,
|
||||
const uint8_t* buffer,
|
||||
size_t size
|
||||
) {
|
||||
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 %lu, expected %lu)\r\n",
|
||||
size, Header::SerializedLength()
|
||||
);
|
||||
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 (header.opcode != Opcode::BootReply)
|
||||
return;
|
||||
if (header.hardwareAddressType != 1)
|
||||
return;
|
||||
if (header.hardwareAddressLength != 6)
|
||||
return;
|
||||
if (header.transactionId != transactionId)
|
||||
return;
|
||||
|
||||
if (!serverSelected)
|
||||
{
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
#include <functional>
|
||||
#include "net.h"
|
||||
|
||||
#include "net-ethernet.h"
|
||||
#include "net.h"
|
||||
|
||||
namespace Net::Dhcp
|
||||
{
|
||||
|
@ -66,33 +67,17 @@ namespace Net::Dhcp
|
|||
|
||||
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);
|
||||
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);
|
||||
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
|
||||
);
|
||||
void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size);
|
||||
} // namespace Net::Dhcp
|
||||
|
|
|
@ -1,32 +1,25 @@
|
|||
#include <cstring>
|
||||
|
||||
#include "net-ethernet.h"
|
||||
|
||||
namespace Net::Ethernet
|
||||
{
|
||||
Header::Header()
|
||||
{}
|
||||
Header::Header() {}
|
||||
|
||||
Header::Header(EtherType type) :
|
||||
macDestination(Utils::MacBroadcast),
|
||||
macSource{0, 0, 0, 0, 0, 0},
|
||||
type(type)
|
||||
{}
|
||||
macDestination(Utils::MacBroadcast), macSource{0, 0, 0, 0, 0, 0}, type(type)
|
||||
{
|
||||
}
|
||||
|
||||
Header::Header(
|
||||
MacAddress macSource, EtherType type
|
||||
) :
|
||||
macDestination(Utils::MacBroadcast),
|
||||
macSource(macSource),
|
||||
type(type)
|
||||
{}
|
||||
Header::Header(MacAddress macSource, EtherType type) :
|
||||
macDestination(Utils::MacBroadcast), macSource(macSource), type(type)
|
||||
{
|
||||
}
|
||||
|
||||
Header::Header(
|
||||
MacAddress macDestination, MacAddress macSource, EtherType type
|
||||
) :
|
||||
macDestination(macDestination),
|
||||
macSource(macSource),
|
||||
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
|
||||
{
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <array>
|
||||
|
||||
#include "net-utils.h"
|
||||
|
||||
namespace Net::Ethernet
|
||||
|
@ -29,7 +30,6 @@ namespace Net::Ethernet
|
|||
}
|
||||
|
||||
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);
|
||||
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t size);
|
||||
};
|
||||
} // namespace Net::Ethernet
|
||||
|
|
100
src/net-icmp.cpp
100
src/net-icmp.cpp
|
@ -30,9 +30,8 @@ namespace Net::Icmp
|
|||
return i;
|
||||
}
|
||||
|
||||
size_t Header::Deserialize(
|
||||
Header& out, const uint8_t* buffer, const size_t bufferSize
|
||||
) {
|
||||
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
|
||||
{
|
||||
if (bufferSize < SerializedLength())
|
||||
{
|
||||
return 0;
|
||||
|
@ -49,7 +48,9 @@ namespace Net::Icmp
|
|||
//
|
||||
EchoHeader::EchoHeader() : EchoHeader(0, 0) {}
|
||||
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
|
||||
{
|
||||
|
@ -66,9 +67,8 @@ namespace Net::Icmp
|
|||
return i;
|
||||
}
|
||||
|
||||
size_t EchoHeader::Deserialize(
|
||||
EchoHeader& out, const uint8_t* buffer, const size_t bufferSize
|
||||
) {
|
||||
size_t EchoHeader::Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize)
|
||||
{
|
||||
if (bufferSize < SerializedLength())
|
||||
{
|
||||
return 0;
|
||||
|
@ -85,13 +85,10 @@ namespace Net::Icmp
|
|||
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);
|
||||
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);
|
||||
Ethernet::Header ethernetHeader(mac, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
|
||||
|
||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||
size_t size = 0;
|
||||
|
@ -101,10 +98,8 @@ namespace Net::Icmp
|
|||
size += pingHeader.Serialize(buffer + size, sizeof(buffer) - size);
|
||||
size += icmpHeader.Serialize(buffer + size, sizeof(buffer) - size);
|
||||
|
||||
const auto expectedSize =
|
||||
ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() +
|
||||
pingHeader.SerializedLength() +
|
||||
const auto expectedSize = ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() + pingHeader.SerializedLength() +
|
||||
icmpHeader.SerializedLength();
|
||||
assert(size == expectedSize);
|
||||
assert(size <= sizeof(buffer));
|
||||
|
@ -117,18 +112,17 @@ namespace Net::Icmp
|
|||
const Ipv4::Header& reqIpv4Header,
|
||||
const Icmp::Header& reqIcmpHeader,
|
||||
const uint8_t* reqBuffer,
|
||||
const size_t reqBufferSize
|
||||
) {
|
||||
const size_t reqBufferSize)
|
||||
{
|
||||
EchoHeader reqEchoHeader;
|
||||
const auto reqEchoHeaderSize =
|
||||
Icmp::EchoHeader::Deserialize(reqEchoHeader, reqBuffer, reqBufferSize);
|
||||
if (reqEchoHeaderSize == 0 || reqBufferSize < reqEchoHeaderSize)
|
||||
{
|
||||
DEBUG_LOG(
|
||||
"Dropped ICMP packet "
|
||||
"(invalid buffer size %ul, expected at least %ul)\r\n",
|
||||
reqBufferSize, EchoHeader::SerializedLength()
|
||||
);
|
||||
"Dropped ICMP packet (invalid buffer size %ul, expected at least %ul)\r\n",
|
||||
reqBufferSize,
|
||||
EchoHeader::SerializedLength());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -137,41 +131,27 @@ namespace Net::Icmp
|
|||
Ipv4::Protocol::Icmp,
|
||||
Utils::Ipv4Address,
|
||||
reqIpv4Header.sourceIp,
|
||||
reqIpv4Header.totalLength
|
||||
);
|
||||
reqIpv4Header.totalLength);
|
||||
const Ethernet::Header respEthernetHeader(
|
||||
reqEthernetHeader.macSource,
|
||||
Utils::GetMacAddress(),
|
||||
Ethernet::EtherType::Ipv4
|
||||
);
|
||||
reqEthernetHeader.macSource, Utils::GetMacAddress(), Ethernet::EtherType::Ipv4);
|
||||
|
||||
const auto payloadSize =
|
||||
reqIpv4Header.totalLength -
|
||||
reqIpv4Header.SerializedLength() -
|
||||
reqIcmpHeader.SerializedLength() -
|
||||
reqEchoHeaderSize;
|
||||
const auto payloadSize = reqIpv4Header.totalLength - reqIpv4Header.SerializedLength() -
|
||||
reqIcmpHeader.SerializedLength() - reqEchoHeaderSize;
|
||||
|
||||
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);
|
||||
std::memcpy(
|
||||
respBuffer.data() + respSize,
|
||||
reqBuffer + reqEchoHeaderSize,
|
||||
payloadSize
|
||||
);
|
||||
respSize +=
|
||||
respIpv4Header.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize);
|
||||
respSize +=
|
||||
respIcmpHeader.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize);
|
||||
std::memcpy(respBuffer.data() + respSize, reqBuffer + reqEchoHeaderSize, payloadSize);
|
||||
respSize += payloadSize;
|
||||
|
||||
const auto expectedRespSize =
|
||||
respEthernetHeader.SerializedLength() +
|
||||
respIpv4Header.SerializedLength() +
|
||||
respIcmpHeader.SerializedLength() +
|
||||
payloadSize;
|
||||
const auto expectedRespSize = respEthernetHeader.SerializedLength() +
|
||||
respIpv4Header.SerializedLength() + respIcmpHeader.SerializedLength() + payloadSize;
|
||||
assert(respSize == expectedRespSize);
|
||||
assert(respSize <= respBuffer.size());
|
||||
|
||||
|
@ -188,24 +168,21 @@ namespace Net::Icmp
|
|||
ethernetHeader, buffer + headerSize, bufferSize - headerSize);
|
||||
|
||||
Ipv4::Header ipv4Header;
|
||||
headerSize += Ipv4::Header::Deserialize(
|
||||
ipv4Header, buffer + headerSize, bufferSize - headerSize);
|
||||
headerSize +=
|
||||
Ipv4::Header::Deserialize(ipv4Header, buffer + headerSize, bufferSize - headerSize);
|
||||
|
||||
Header icmpHeader;
|
||||
headerSize += Icmp::Header::Deserialize(
|
||||
icmpHeader, buffer + headerSize, bufferSize - headerSize);
|
||||
headerSize +=
|
||||
Icmp::Header::Deserialize(icmpHeader, buffer + headerSize, bufferSize - headerSize);
|
||||
|
||||
const auto expectedHeaderSize =
|
||||
ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() +
|
||||
icmpHeader.SerializedLength();
|
||||
const auto expectedHeaderSize = ethernetHeader.SerializedLength() +
|
||||
ipv4Header.SerializedLength() + icmpHeader.SerializedLength();
|
||||
if (headerSize != expectedHeaderSize)
|
||||
{
|
||||
DEBUG_LOG(
|
||||
"Dropped ICMP packet "
|
||||
"(invalid buffer size %ul, expected at least %ul)\r\n",
|
||||
bufferSize, expectedHeaderSize
|
||||
);
|
||||
"Dropped ICMP packet (invalid buffer size %ul, expected at least %ul)\r\n",
|
||||
bufferSize,
|
||||
expectedHeaderSize);
|
||||
}
|
||||
|
||||
if (icmpHeader.type == Type::EchoRequest)
|
||||
|
@ -215,8 +192,7 @@ namespace Net::Icmp
|
|||
ipv4Header,
|
||||
icmpHeader,
|
||||
buffer + headerSize,
|
||||
bufferSize - headerSize
|
||||
);
|
||||
bufferSize - headerSize);
|
||||
}
|
||||
}
|
||||
} // namespace Net::Icmp
|
||||
|
|
|
@ -24,8 +24,7 @@ namespace Net::Icmp
|
|||
}
|
||||
|
||||
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);
|
||||
static size_t Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize);
|
||||
};
|
||||
|
||||
struct EchoHeader
|
||||
|
@ -42,8 +41,7 @@ namespace Net::Icmp
|
|||
}
|
||||
|
||||
size_t Serialize(uint8_t* buffer, const size_t bufferSize) const;
|
||||
static size_t Deserialize(
|
||||
EchoHeader& out, const uint8_t* buffer, const size_t bufferSize);
|
||||
static size_t Deserialize(EchoHeader& out, const uint8_t* buffer, const size_t bufferSize);
|
||||
};
|
||||
|
||||
void SendEchoRequest(const Utils::MacAddress mac, const uint32_t ip);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include <cassert>
|
||||
|
||||
#include "net-ipv4.h"
|
||||
#include "net-ethernet.h"
|
||||
#include "net-arp.h"
|
||||
#include "net-ethernet.h"
|
||||
#include "net-icmp.h"
|
||||
#include "net-ipv4.h"
|
||||
#include "net-udp.h"
|
||||
#include "net-utils.h"
|
||||
|
||||
|
@ -14,8 +14,7 @@ namespace Net::Ipv4
|
|||
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),
|
||||
ihl(5),
|
||||
dscp(0),
|
||||
|
@ -29,7 +28,8 @@ namespace Net::Ipv4
|
|||
headerChecksum(0),
|
||||
sourceIp(sourceIp),
|
||||
destinationIp(destinationIp)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
size_t Header::Serialize(uint8_t* buffer, const size_t bufferSize) const
|
||||
{
|
||||
|
@ -70,9 +70,8 @@ namespace Net::Ipv4
|
|||
return i;
|
||||
}
|
||||
|
||||
size_t Header::Deserialize(
|
||||
Header& out, const uint8_t* buffer, const size_t bufferSize
|
||||
) {
|
||||
size_t Header::Deserialize(Header& out, const uint8_t* buffer, const size_t bufferSize)
|
||||
{
|
||||
if (bufferSize <= SerializedLength())
|
||||
{
|
||||
return 0;
|
||||
|
@ -95,36 +94,36 @@ namespace Net::Ipv4
|
|||
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];
|
||||
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
|
||||
) {
|
||||
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 packet (invalid buffer size %lu, expected at least %lu)\r\n"
|
||||
bufferSize, headerSize
|
||||
);
|
||||
"Dropped IPv4 packet (invalid buffer size %lu, expected at least %lu)\r\n",
|
||||
bufferSize,
|
||||
headerSize);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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) return;
|
||||
if (header.ihl != 5) return; // Not supported
|
||||
if (header.destinationIp != Utils::Ipv4Address) return;
|
||||
if (header.fragmentOffset != 0) return; // TODO Support this
|
||||
if (header.version != 4)
|
||||
return;
|
||||
if (header.ihl != 5)
|
||||
return; // Not supported
|
||||
if (header.destinationIp != Utils::Ipv4Address)
|
||||
return;
|
||||
if (header.fragmentOffset != 0)
|
||||
return; // TODO Support this
|
||||
|
||||
if (header.protocol == Ipv4::Protocol::Icmp)
|
||||
{
|
||||
|
@ -132,8 +131,7 @@ namespace Net::Ipv4
|
|||
}
|
||||
else if (header.protocol == Ipv4::Protocol::Udp)
|
||||
{
|
||||
Udp::HandlePacket(
|
||||
ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
|
||||
Udp::HandlePacket(ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
|
||||
}
|
||||
}
|
||||
} // namespace Net::Ipv4
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "net-ethernet.h"
|
||||
|
||||
namespace Net::Ipv4
|
||||
|
@ -29,12 +30,7 @@ namespace Net::Ipv4
|
|||
uint32_t destinationIp;
|
||||
|
||||
Header();
|
||||
Header(
|
||||
Protocol protocol,
|
||||
uint32_t sourceIp,
|
||||
uint32_t destinationIp,
|
||||
uint16_t totalLength
|
||||
);
|
||||
Header(Protocol protocol, uint32_t sourceIp, uint32_t destinationIp, uint16_t totalLength);
|
||||
|
||||
static constexpr size_t SerializedLength()
|
||||
{
|
||||
|
@ -43,13 +39,9 @@ namespace Net::Ipv4
|
|||
}
|
||||
|
||||
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);
|
||||
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
|
||||
);
|
||||
const Ethernet::Header& ethernetHeader, const uint8_t* buffer, const size_t bufferSize);
|
||||
} // namespace Net::Ipv4
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <memory>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
#include "net-arp.h"
|
||||
#include "net-ethernet.h"
|
||||
|
@ -24,17 +24,17 @@ namespace Net::Tftp
|
|||
Packet::Packet() : opcode(static_cast<Opcode>(0)) {}
|
||||
Packet::Packet(const Opcode opcode) : opcode(opcode) {}
|
||||
|
||||
static std::unique_ptr<Packet> handleTftpWriteRequest(
|
||||
const uint8_t* data, const size_t dataSize
|
||||
) {
|
||||
static std::unique_ptr<Packet>
|
||||
handleTftpWriteRequest(const uint8_t* data, const size_t dataSize)
|
||||
{
|
||||
WriteReadRequestPacket packet;
|
||||
const auto size = packet.Deserialize(data, dataSize);
|
||||
if (size == 0)
|
||||
{
|
||||
DEBUG_LOG(
|
||||
"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;
|
||||
}
|
||||
|
||||
|
@ -71,17 +71,14 @@ namespace Net::Tftp
|
|||
std::unique_ptr<Packet> response;
|
||||
if (result != FR_OK)
|
||||
{
|
||||
response = std::unique_ptr<ErrorPacket>(
|
||||
new ErrorPacket(0, "error opening target file")
|
||||
);
|
||||
response =
|
||||
std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "error opening target file"));
|
||||
}
|
||||
else
|
||||
{
|
||||
shouldReboot =
|
||||
packet.filename == "kernel.img" || packet.filename == "options.txt";
|
||||
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
|
||||
|
@ -96,19 +93,16 @@ namespace Net::Tftp
|
|||
if (tftpSize == 0)
|
||||
{
|
||||
DEBUG_LOG(
|
||||
"Dropped TFTP data packet "
|
||||
"(invalid buffer size %lu, expected at least %lu)\r\n",
|
||||
size, sizeof(packet.opcode) + sizeof(packet.blockNumber)
|
||||
)
|
||||
"Dropped TFTP data packet (invalid buffer size %lu, expected at least %lu)\r\n",
|
||||
size,
|
||||
sizeof(packet.opcode) + sizeof(packet.blockNumber))
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (packet.blockNumber != currentBlockNumber + 1)
|
||||
{
|
||||
f_close(&outFile);
|
||||
return std::unique_ptr<ErrorPacket>(
|
||||
new ErrorPacket(0, "invalid block number")
|
||||
);
|
||||
return std::unique_ptr<ErrorPacket>(new ErrorPacket(0, "invalid block number"));
|
||||
}
|
||||
currentBlockNumber = packet.blockNumber;
|
||||
|
||||
|
@ -129,8 +123,7 @@ namespace Net::Tftp
|
|||
}
|
||||
|
||||
return std::unique_ptr<AcknowledgementPacket>(
|
||||
new AcknowledgementPacket(currentBlockNumber)
|
||||
);
|
||||
new AcknowledgementPacket(currentBlockNumber));
|
||||
}
|
||||
|
||||
void HandlePacket(
|
||||
|
@ -138,8 +131,8 @@ namespace Net::Tftp
|
|||
const Ipv4::Header ipv4ReqHeader,
|
||||
const Udp::Header udpReqHeader,
|
||||
const uint8_t* reqBuffer,
|
||||
const size_t reqBufferSize
|
||||
) {
|
||||
const size_t reqBufferSize)
|
||||
{
|
||||
const auto opcode = static_cast<Opcode>(reqBuffer[0] << 8 | reqBuffer[1]);
|
||||
std::unique_ptr<Packet> response;
|
||||
|
||||
|
@ -148,8 +141,8 @@ namespace Net::Tftp
|
|||
{
|
||||
DEBUG_LOG(
|
||||
"Dropped TFTP packet (invalid buffer size %lu, expected at least %lu)\r\n",
|
||||
reqBufferSize, payloadSize
|
||||
);
|
||||
reqBufferSize,
|
||||
payloadSize);
|
||||
}
|
||||
|
||||
if (opcode == Opcode::WriteRequest)
|
||||
|
@ -162,9 +155,7 @@ namespace Net::Tftp
|
|||
}
|
||||
else
|
||||
{
|
||||
response = std::unique_ptr<ErrorPacket>(
|
||||
new ErrorPacket(4, "not implemented yet")
|
||||
);
|
||||
response = std::unique_ptr<ErrorPacket>(new ErrorPacket(4, "not implemented yet"));
|
||||
}
|
||||
|
||||
if (response != nullptr)
|
||||
|
@ -172,19 +163,16 @@ namespace Net::Tftp
|
|||
Udp::Header udpRespHeader(
|
||||
udpReqHeader.destinationPort,
|
||||
udpReqHeader.sourcePort,
|
||||
response->SerializedLength() + Udp::Header::SerializedLength()
|
||||
);
|
||||
response->SerializedLength() + Udp::Header::SerializedLength());
|
||||
Ipv4::Header ipv4RespHeader(
|
||||
Ipv4::Protocol::Udp,
|
||||
Utils::Ipv4Address,
|
||||
ipv4ReqHeader.sourceIp,
|
||||
udpRespHeader.length + Ipv4::Header::SerializedLength()
|
||||
);
|
||||
udpRespHeader.length + Ipv4::Header::SerializedLength());
|
||||
Ethernet::Header ethernetRespHeader(
|
||||
Arp::ArpTable[ipv4RespHeader.destinationIp],
|
||||
Utils::GetMacAddress(),
|
||||
Ethernet::EtherType::Ipv4
|
||||
);
|
||||
Ethernet::EtherType::Ipv4);
|
||||
|
||||
size_t size = 0;
|
||||
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
|
||||
|
@ -193,10 +181,8 @@ namespace Net::Tftp
|
|||
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() +
|
||||
const auto expectedSize = ethernetRespHeader.SerializedLength() +
|
||||
ipv4RespHeader.SerializedLength() + udpRespHeader.SerializedLength() +
|
||||
response->SerializedLength();
|
||||
assert(size == expectedSize);
|
||||
assert(size <= sizeof(buffer));
|
||||
|
@ -211,16 +197,14 @@ namespace Net::Tftp
|
|||
// WriteReadRequestPacket
|
||||
//
|
||||
WriteReadRequestPacket::WriteReadRequestPacket() : Packet() {}
|
||||
WriteReadRequestPacket::WriteReadRequestPacket(const Opcode opcode) : Packet(opcode)
|
||||
{}
|
||||
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
|
||||
size_t WriteReadRequestPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
|
||||
{
|
||||
if (bufferSize < SerializedLength())
|
||||
{
|
||||
|
@ -240,24 +224,26 @@ namespace Net::Tftp
|
|||
return i;
|
||||
}
|
||||
|
||||
size_t WriteReadRequestPacket::Deserialize(
|
||||
const uint8_t* buffer, const size_t bufferSize
|
||||
) {
|
||||
size_t WriteReadRequestPacket::Deserialize(const uint8_t* buffer, const size_t bufferSize)
|
||||
{
|
||||
// Can't use SerializedLength here, as it's variable.
|
||||
// Check for each field instead.
|
||||
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]);
|
||||
i += 2;
|
||||
|
||||
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);
|
||||
i += filename.size() + 1;
|
||||
|
||||
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);
|
||||
i += mode.size() + 1;
|
||||
|
||||
|
@ -270,7 +256,8 @@ namespace Net::Tftp
|
|||
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
|
||||
{
|
||||
|
@ -303,15 +290,15 @@ namespace Net::Tftp
|
|||
|
||||
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
|
||||
size_t AcknowledgementPacket::Serialize(uint8_t* buffer, const size_t bufferSize) const
|
||||
{
|
||||
if (bufferSize < SerializedLength())
|
||||
{
|
||||
|
@ -329,8 +316,7 @@ namespace Net::Tftp
|
|||
//
|
||||
// DataPacket
|
||||
//
|
||||
DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0)
|
||||
{}
|
||||
DataPacket::DataPacket() : Packet(Opcode::Data), blockNumber(0) {}
|
||||
|
||||
size_t DataPacket::SerializedLength() const
|
||||
{
|
||||
|
@ -356,7 +342,8 @@ namespace Net::Tftp
|
|||
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))
|
||||
{
|
||||
return 0;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "net-udp.h"
|
||||
|
||||
namespace Net::Tftp
|
||||
|
@ -76,6 +77,5 @@ namespace Net::Tftp
|
|||
const Ipv4::Header ipv4ReqHeader,
|
||||
const Udp::Header udpReqHeader,
|
||||
const uint8_t* data,
|
||||
const size_t dataSize
|
||||
);
|
||||
const size_t dataSize);
|
||||
} // namespace Net::Tftp
|
||||
|
|
|
@ -6,19 +6,12 @@
|
|||
|
||||
namespace Net::Udp
|
||||
{
|
||||
Header::Header()
|
||||
{}
|
||||
Header::Header() {}
|
||||
|
||||
Header::Header(
|
||||
Port sourcePort,
|
||||
Port destinationPort,
|
||||
uint16_t length
|
||||
) :
|
||||
sourcePort(sourcePort),
|
||||
destinationPort(destinationPort),
|
||||
length(length),
|
||||
checksum(0)
|
||||
{}
|
||||
Header::Header(Port sourcePort, 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
|
||||
{
|
||||
|
@ -57,24 +50,24 @@ namespace Net::Udp
|
|||
const Ethernet::Header ethernetHeader,
|
||||
const Ipv4::Header ipv4Header,
|
||||
const uint8_t* buffer,
|
||||
const size_t bufferSize
|
||||
) {
|
||||
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 %lu, expected at least %lu)\r\n",
|
||||
bufferSize, Header::SerializedLength()
|
||||
);
|
||||
bufferSize,
|
||||
Header::SerializedLength());
|
||||
return;
|
||||
}
|
||||
if (udpHeader.length <= bufferSize)
|
||||
{
|
||||
DEBUG_LOG(
|
||||
"Dropped UDP packet (invalid buffer size %lu, expected at least %lu)\r\n",
|
||||
bufferSize, udpHeader.length
|
||||
);
|
||||
bufferSize,
|
||||
udpHeader.length);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -83,8 +76,7 @@ namespace Net::Udp
|
|||
Dhcp::HandlePacket(
|
||||
ethernetHeader,
|
||||
buffer + udpHeader.SerializedLength(),
|
||||
bufferSize - udpHeader.SerializedLength()
|
||||
);
|
||||
bufferSize - udpHeader.SerializedLength());
|
||||
}
|
||||
else if (udpHeader.destinationPort == Port::Tftp)
|
||||
{
|
||||
|
@ -93,8 +85,7 @@ namespace Net::Udp
|
|||
ipv4Header,
|
||||
udpHeader,
|
||||
buffer + udpHeader.SerializedLength(),
|
||||
bufferSize - udpHeader.SerializedLength()
|
||||
);
|
||||
bufferSize - udpHeader.SerializedLength());
|
||||
}
|
||||
}
|
||||
} // namespace Net::Udp
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "net-ethernet.h"
|
||||
#include "net-ipv4.h"
|
||||
|
||||
|
@ -26,11 +27,7 @@ namespace Net::Udp
|
|||
|
||||
static constexpr size_t SerializedLength()
|
||||
{
|
||||
return
|
||||
sizeof(sourcePort) +
|
||||
sizeof(destinationPort) +
|
||||
sizeof(length) +
|
||||
sizeof(checksum);
|
||||
return sizeof(sourcePort) + sizeof(destinationPort) + sizeof(length) + sizeof(checksum);
|
||||
}
|
||||
|
||||
size_t Serialize(uint8_t* buffer, const size_t size) const;
|
||||
|
@ -41,6 +38,5 @@ namespace Net::Udp
|
|||
const Ethernet::Header ethernetHeader,
|
||||
const Ipv4::Header ipv4Header,
|
||||
const uint8_t* buffer,
|
||||
const size_t size
|
||||
);
|
||||
const size_t size);
|
||||
} // namespace Net::Udp
|
||||
|
|
|
@ -8,75 +8,52 @@ namespace Net::Utils
|
|||
const MacAddress MacBroadcast{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint32_t Ipv4Address = 0xC0A80164;
|
||||
|
||||
static const uint32_t crcTab32[256] =
|
||||
{
|
||||
0x00000000ul, 0x77073096ul, 0xEE0E612Cul, 0x990951BAul,
|
||||
0x076DC419ul, 0x706AF48Ful, 0xE963A535ul, 0x9E6495A3ul,
|
||||
0x0EDB8832ul, 0x79DCB8A4ul, 0xE0D5E91Eul, 0x97D2D988ul,
|
||||
0x09B64C2Bul, 0x7EB17CBDul, 0xE7B82D07ul, 0x90BF1D91ul,
|
||||
0x1DB71064ul, 0x6AB020F2ul, 0xF3B97148ul, 0x84BE41DEul,
|
||||
0x1ADAD47Dul, 0x6DDDE4EBul, 0xF4D4B551ul, 0x83D385C7ul,
|
||||
0x136C9856ul, 0x646BA8C0ul, 0xFD62F97Aul, 0x8A65C9ECul,
|
||||
0x14015C4Ful, 0x63066CD9ul, 0xFA0F3D63ul, 0x8D080DF5ul,
|
||||
0x3B6E20C8ul, 0x4C69105Eul, 0xD56041E4ul, 0xA2677172ul,
|
||||
0x3C03E4D1ul, 0x4B04D447ul, 0xD20D85FDul, 0xA50AB56Bul,
|
||||
0x35B5A8FAul, 0x42B2986Cul, 0xDBBBC9D6ul, 0xACBCF940ul,
|
||||
0x32D86CE3ul, 0x45DF5C75ul, 0xDCD60DCFul, 0xABD13D59ul,
|
||||
0x26D930ACul, 0x51DE003Aul, 0xC8D75180ul, 0xBFD06116ul,
|
||||
0x21B4F4B5ul, 0x56B3C423ul, 0xCFBA9599ul, 0xB8BDA50Ful,
|
||||
0x2802B89Eul, 0x5F058808ul, 0xC60CD9B2ul, 0xB10BE924ul,
|
||||
0x2F6F7C87ul, 0x58684C11ul, 0xC1611DABul, 0xB6662D3Dul,
|
||||
0x76DC4190ul, 0x01DB7106ul, 0x98D220BCul, 0xEFD5102Aul,
|
||||
0x71B18589ul, 0x06B6B51Ful, 0x9FBFE4A5ul, 0xE8B8D433ul,
|
||||
0x7807C9A2ul, 0x0F00F934ul, 0x9609A88Eul, 0xE10E9818ul,
|
||||
0x7F6A0DBBul, 0x086D3D2Dul, 0x91646C97ul, 0xE6635C01ul,
|
||||
0x6B6B51F4ul, 0x1C6C6162ul, 0x856530D8ul, 0xF262004Eul,
|
||||
0x6C0695EDul, 0x1B01A57Bul, 0x8208F4C1ul, 0xF50FC457ul,
|
||||
0x65B0D9C6ul, 0x12B7E950ul, 0x8BBEB8EAul, 0xFCB9887Cul,
|
||||
0x62DD1DDFul, 0x15DA2D49ul, 0x8CD37CF3ul, 0xFBD44C65ul,
|
||||
0x4DB26158ul, 0x3AB551CEul, 0xA3BC0074ul, 0xD4BB30E2ul,
|
||||
0x4ADFA541ul, 0x3DD895D7ul, 0xA4D1C46Dul, 0xD3D6F4FBul,
|
||||
0x4369E96Aul, 0x346ED9FCul, 0xAD678846ul, 0xDA60B8D0ul,
|
||||
0x44042D73ul, 0x33031DE5ul, 0xAA0A4C5Ful, 0xDD0D7CC9ul,
|
||||
0x5005713Cul, 0x270241AAul, 0xBE0B1010ul, 0xC90C2086ul,
|
||||
0x5768B525ul, 0x206F85B3ul, 0xB966D409ul, 0xCE61E49Ful,
|
||||
0x5EDEF90Eul, 0x29D9C998ul, 0xB0D09822ul, 0xC7D7A8B4ul,
|
||||
0x59B33D17ul, 0x2EB40D81ul, 0xB7BD5C3Bul, 0xC0BA6CADul,
|
||||
0xEDB88320ul, 0x9ABFB3B6ul, 0x03B6E20Cul, 0x74B1D29Aul,
|
||||
0xEAD54739ul, 0x9DD277AFul, 0x04DB2615ul, 0x73DC1683ul,
|
||||
0xE3630B12ul, 0x94643B84ul, 0x0D6D6A3Eul, 0x7A6A5AA8ul,
|
||||
0xE40ECF0Bul, 0x9309FF9Dul, 0x0A00AE27ul, 0x7D079EB1ul,
|
||||
0xF00F9344ul, 0x8708A3D2ul, 0x1E01F268ul, 0x6906C2FEul,
|
||||
0xF762575Dul, 0x806567CBul, 0x196C3671ul, 0x6E6B06E7ul,
|
||||
0xFED41B76ul, 0x89D32BE0ul, 0x10DA7A5Aul, 0x67DD4ACCul,
|
||||
0xF9B9DF6Ful, 0x8EBEEFF9ul, 0x17B7BE43ul, 0x60B08ED5ul,
|
||||
0xD6D6A3E8ul, 0xA1D1937Eul, 0x38D8C2C4ul, 0x4FDFF252ul,
|
||||
0xD1BB67F1ul, 0xA6BC5767ul, 0x3FB506DDul, 0x48B2364Bul,
|
||||
0xD80D2BDAul, 0xAF0A1B4Cul, 0x36034AF6ul, 0x41047A60ul,
|
||||
0xDF60EFC3ul, 0xA867DF55ul, 0x316E8EEFul, 0x4669BE79ul,
|
||||
0xCB61B38Cul, 0xBC66831Aul, 0x256FD2A0ul, 0x5268E236ul,
|
||||
0xCC0C7795ul, 0xBB0B4703ul, 0x220216B9ul, 0x5505262Ful,
|
||||
0xC5BA3BBEul, 0xB2BD0B28ul, 0x2BB45A92ul, 0x5CB36A04ul,
|
||||
0xC2D7FFA7ul, 0xB5D0CF31ul, 0x2CD99E8Bul, 0x5BDEAE1Dul,
|
||||
0x9B64C2B0ul, 0xEC63F226ul, 0x756AA39Cul, 0x026D930Aul,
|
||||
0x9C0906A9ul, 0xEB0E363Ful, 0x72076785ul, 0x05005713ul,
|
||||
0x95BF4A82ul, 0xE2B87A14ul, 0x7BB12BAEul, 0x0CB61B38ul,
|
||||
0x92D28E9Bul, 0xE5D5BE0Dul, 0x7CDCEFB7ul, 0x0BDBDF21ul,
|
||||
0x86D3D2D4ul, 0xF1D4E242ul, 0x68DDB3F8ul, 0x1FDA836Eul,
|
||||
0x81BE16CDul, 0xF6B9265Bul, 0x6FB077E1ul, 0x18B74777ul,
|
||||
0x88085AE6ul, 0xFF0F6A70ul, 0x66063BCAul, 0x11010B5Cul,
|
||||
0x8F659EFFul, 0xF862AE69ul, 0x616BFFD3ul, 0x166CCF45ul,
|
||||
0xA00AE278ul, 0xD70DD2EEul, 0x4E048354ul, 0x3903B3C2ul,
|
||||
0xA7672661ul, 0xD06016F7ul, 0x4969474Dul, 0x3E6E77DBul,
|
||||
0xAED16A4Aul, 0xD9D65ADCul, 0x40DF0B66ul, 0x37D83BF0ul,
|
||||
0xA9BCAE53ul, 0xDEBB9EC5ul, 0x47B2CF7Ful, 0x30B5FFE9ul,
|
||||
0xBDBDF21Cul, 0xCABAC28Aul, 0x53B39330ul, 0x24B4A3A6ul,
|
||||
0xBAD03605ul, 0xCDD70693ul, 0x54DE5729ul, 0x23D967BFul,
|
||||
0xB3667A2Eul, 0xC4614AB8ul, 0x5D681B02ul, 0x2A6F2B94ul,
|
||||
0xB40BBE37ul, 0xC30C8EA1ul, 0x5A05DF1Bul, 0x2D02EF8Dul
|
||||
};
|
||||
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 Crc32(const uint8_t* buffer, size_t size)
|
||||
{
|
||||
uint32_t crc = 0xFFFFFFFFul;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Net::Utils
|
||||
{
|
||||
|
@ -11,4 +11,4 @@ namespace Net::Utils
|
|||
uint32_t Crc32(const uint8_t* buffer, size_t size);
|
||||
uint16_t InternetChecksum(const void* data, size_t size);
|
||||
MacAddress GetMacAddress();
|
||||
};
|
||||
}; // namespace Net::Utils
|
||||
|
|
Loading…
Reference in a new issue