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

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

View file

@ -8,8 +8,10 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Arp namespace Net
{ {
namespace Arp
{
Packet::Packet() {} Packet::Packet() {}
Packet::Packet(const uint16_t operation) : Packet::Packet(const uint16_t operation) :
@ -96,7 +98,8 @@ namespace Net::Arp
size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size); size += ethernetHeader.Serialize(buffer + size, sizeof(buffer) - size);
size += arpPacket.Serialize(buffer + size, sizeof(buffer) - size); size += arpPacket.Serialize(buffer + size, sizeof(buffer) - size);
const auto expectedSize = ethernetHeader.SerializedLength() + arpPacket.SerializedLength(); const auto expectedSize =
ethernetHeader.SerializedLength() + arpPacket.SerializedLength();
assert(size == expectedSize); assert(size == expectedSize);
assert(size <= sizeof(buffer)); assert(size <= sizeof(buffer));
@ -142,7 +145,8 @@ namespace Net::Arp
return; return;
} }
if (arpPacket.hardwareType != 1 || arpPacket.protocolType != Ethernet::EtherType::Ipv4 || if (arpPacket.hardwareType != 1 ||
arpPacket.protocolType != Ethernet::EtherType::Ipv4 ||
arpPacket.targetIp != Utils::Ipv4Address) arpPacket.targetIp != Utils::Ipv4Address)
{ {
// Might want to disable because of spamminess // Might want to disable because of spamminess
@ -167,4 +171,5 @@ namespace Net::Arp
} }
std::unordered_map<uint32_t, Utils::MacAddress> ArpTable; std::unordered_map<uint32_t, Utils::MacAddress> ArpTable;
} // namespace Net::Arp } // namespace Arp
} // namespace Net

View file

@ -4,8 +4,10 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-utils.h" #include "net-utils.h"
namespace Net::Arp namespace Net
{ {
namespace Arp
{
enum Operation enum Operation
{ {
ARP_OPERATION_REQUEST = 1, ARP_OPERATION_REQUEST = 1,
@ -64,4 +66,5 @@ namespace Net::Arp
void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip); void SendAnnouncement(const Utils::MacAddress mac, const uint32_t ip);
extern std::unordered_map<uint32_t, Utils::MacAddress> ArpTable; extern std::unordered_map<uint32_t, Utils::MacAddress> ArpTable;
} // namespace Net::Arp } // namespace Arp
} // namespace Net

View file

@ -11,8 +11,10 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Dhcp namespace Net
{ {
namespace Dhcp
{
Header::Header() {} Header::Header() {}
Header::Header(Opcode opcode, uint32_t transactionId) : Header::Header(Opcode opcode, uint32_t transactionId) :
@ -98,9 +100,11 @@ namespace Net::Dhcp
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.secondsElapsed = buffer[8] << 8 | buffer[9];
out.flags = buffer[10] << 8 | buffer[11]; out.flags = buffer[10] << 8 | buffer[11];
out.clientIpAddress = buffer[12] << 24 | buffer[13] << 16 | buffer[14] << 8 | buffer[15]; 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.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.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.relayIpAddress = buffer[24] << 24 | buffer[25] << 16 | buffer[26] << 8 | buffer[27];
std::memcpy( std::memcpy(
@ -185,7 +189,8 @@ namespace Net::Dhcp
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(Utils::GetMacAddress(), Ethernet::EtherType::Ipv4); const Ethernet::Header ethernetHeader(
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;
@ -209,11 +214,12 @@ namespace Net::Dhcp
sendDiscover(); sendDiscover();
// Wait three seconds for responses // Wait three seconds for responses
const auto callbackVoid = static_cast<void*>(&callback); // const auto callbackVoid = static_cast<void*>(&callback);
StartKernelTimer(3 * HZ, discoverTimerHandler, callbackVoid, nullptr); // 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); offeredIpAddresses.push_back(dhcpHeader.yourIpAddress);
serverIpAddresses.push_back(dhcpHeader.serverIpAddress); serverIpAddresses.push_back(dhcpHeader.serverIpAddress);
@ -233,7 +239,8 @@ namespace Net::Dhcp
serverSelected = false; 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; Header header;
const auto dhcpSize = Header::Deserialize(header, buffer, size); const auto dhcpSize = Header::Deserialize(header, buffer, size);
@ -264,4 +271,5 @@ namespace Net::Dhcp
handleAckPacket(ethernetHeader, header); handleAckPacket(ethernetHeader, header);
} }
} }
} // namespace Net::Dhcp } // namespace Dhcp
} // namespace Net

View file

@ -4,8 +4,10 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net.h" #include "net.h"
namespace Net::Dhcp namespace Net
{ {
namespace Dhcp
{
enum class Opcode : uint8_t enum class Opcode : uint8_t
{ {
BootRequest = 1, BootRequest = 1,
@ -67,11 +69,12 @@ namespace Net::Dhcp
constexpr static size_t SerializedLength() constexpr static size_t SerializedLength()
{ {
return sizeof(Opcode) + sizeof(hardwareAddressType) + sizeof(hardwareAddressLength) + return sizeof(Opcode) + sizeof(hardwareAddressType) +
sizeof(hops) + sizeof(transactionId) + sizeof(secondsElapsed) + sizeof(flags) + sizeof(hardwareAddressLength) + sizeof(hops) + sizeof(transactionId) +
sizeof(clientIpAddress) + sizeof(yourIpAddress) + sizeof(serverIpAddress) + sizeof(secondsElapsed) + sizeof(flags) + sizeof(clientIpAddress) +
sizeof(relayIpAddress) + sizeof(clientHardwareAddress) + sizeof(serverHostname) + sizeof(yourIpAddress) + sizeof(serverIpAddress) + sizeof(relayIpAddress) +
sizeof(bootFile) + sizeof(magicValue); 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;
@ -79,5 +82,7 @@ namespace Net::Dhcp
}; };
void ObtainIp(std::function<void()>& callback); void ObtainIp(std::function<void()>& callback);
void HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size); void
} // namespace Net::Dhcp HandlePacket(const Ethernet::Header& ethernetHeader, const uint8_t* buffer, size_t size);
} // namespace Dhcp
} // namespace Net

View file

@ -2,8 +2,10 @@
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ethernet namespace Net
{ {
namespace Ethernet
{
Header::Header() {} Header::Header() {}
Header::Header(EtherType type) : Header::Header(EtherType type) :
@ -54,4 +56,5 @@ namespace Net::Ethernet
out.type = static_cast<EtherType>(buffer[12] << 8 | buffer[13]); out.type = static_cast<EtherType>(buffer[12] << 8 | buffer[13]);
return 14; return 14;
} }
} // namespace Net::Ethernet } // namespace Ethernet
} // namespace Net

View file

@ -3,8 +3,10 @@
#include "net-utils.h" #include "net-utils.h"
namespace Net::Ethernet namespace Net
{ {
namespace Ethernet
{
using Utils::MacAddress; using Utils::MacAddress;
enum class EtherType : uint16_t enum class EtherType : uint16_t
@ -32,4 +34,5 @@ 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(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 } // namespace Ethernet
} // namespace Net

View file

@ -9,8 +9,10 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Icmp namespace Net
{ {
namespace Icmp
{
// //
// Header // Header
// //
@ -18,7 +20,10 @@ namespace Net::Icmp
Header::Header(Type type, uint8_t code) : type(type), code(code), checksum(0) {} Header::Header(Type type, uint8_t code) : type(type), code(code), checksum(0) {}
size_t Header::Serialize( size_t Header::Serialize(
uint8_t* buffer, const size_t bufferSize, const uint8_t* data, const size_t dataSize) const uint8_t* buffer,
const size_t bufferSize,
const uint8_t* data,
const size_t dataSize) const
{ {
if (bufferSize < SerializedLength()) if (bufferSize < SerializedLength())
{ {
@ -78,7 +83,8 @@ namespace Net::Icmp
return i; 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()) if (bufferSize < SerializedLength())
{ {
@ -146,8 +152,8 @@ namespace Net::Icmp
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 += respSize += respIpv4Header.Serialize(
respIpv4Header.Serialize(respBuffer.data() + respSize, respBuffer.size() - respSize); respBuffer.data() + respSize, respBuffer.size() - respSize);
respSize += respIcmpHeader.Serialize( respSize += respIcmpHeader.Serialize(
respBuffer.data() + respSize, respBuffer.size() - respSize, reqBuffer, payloadSize); respBuffer.data() + respSize, respBuffer.size() - respSize, reqBuffer, payloadSize);
@ -187,4 +193,5 @@ namespace Net::Icmp
bufferSize - headerSize); bufferSize - headerSize);
} }
} }
} // namespace Net::Icmp } // namespace Icmp
} // namespace Net

View file

@ -13,8 +13,10 @@
#include <cstring> #include <cstring>
#include <uspi.h> #include <uspi.h>
namespace Net::Ipv4 namespace Net
{ {
namespace Ipv4
{
Header::Header() {} Header::Header() {}
Header::Header( Header::Header(
@ -137,7 +139,8 @@ namespace Net::Ipv4
if (header.version != 4) if (header.version != 4)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped IPv4 packet (invalid header version %u, expected 4)\r\n", header.version); "Dropped IPv4 packet (invalid header version %u, expected 4)\r\n",
header.version);
return; return;
} }
if (header.ihl != 5) if (header.ihl != 5)
@ -171,7 +174,9 @@ namespace Net::Ipv4
else if (header.protocol == Ipv4::Protocol::Udp) else if (header.protocol == Ipv4::Protocol::Udp)
{ {
DEBUG_LOG("Ethernet -> IPv4 -> UDP\r\n"); DEBUG_LOG("Ethernet -> IPv4 -> UDP\r\n");
Udp::HandlePacket(ethernetHeader, header, buffer + headerSize, bufferSize - headerSize); Udp::HandlePacket(
ethernetHeader, header, buffer + headerSize, bufferSize - headerSize);
} }
} }
} // namespace Net::Ipv4 } // namespace Ipv4
} // namespace Net

View file

@ -4,8 +4,10 @@
#include "net-ethernet.h" #include "net-ethernet.h"
namespace Net::Ipv4 namespace Net
{ {
namespace Ipv4
{
enum class Protocol : uint8_t enum class Protocol : uint8_t
{ {
Icmp = 1, Icmp = 1,
@ -30,7 +32,8 @@ namespace Net::Ipv4
uint32_t destinationIp; uint32_t destinationIp;
Header(); 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() static constexpr size_t SerializedLength()
{ {
@ -44,4 +47,5 @@ namespace Net::Ipv4
void HandlePacket( 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 } // namespace Ipv4
} // namespace Net

View file

@ -14,8 +14,10 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Tftp namespace Net
{ {
namespace Tftp
{
// TODO Allow multiple files open // TODO Allow multiple files open
static FIL outFile; static FIL outFile;
static bool shouldReboot = false; static bool shouldReboot = false;
@ -234,7 +236,8 @@ namespace Net::Tftp
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 each field separately instead. // Can't use SerializedLength here, as it's variable. Check each field separately
// instead.
size_t i = 0; size_t i = 0;
if (sizeof(Opcode) >= bufferSize - i) if (sizeof(Opcode) >= bufferSize - i)
@ -370,4 +373,5 @@ namespace Net::Tftp
data = std::vector<uint8_t>(buffer + 4, buffer + bufferSize); data = std::vector<uint8_t>(buffer + 4, buffer + bufferSize);
return bufferSize; return bufferSize;
} }
} // namespace Net::Tftp } // namespace Tftp
} // namespace Net

View file

@ -4,8 +4,10 @@
#include "debug.h" #include "debug.h"
namespace Net::Udp namespace Net
{ {
namespace Udp
{
Header::Header() {} Header::Header() {}
Header::Header(Port sourcePort, Port destinationPort, uint16_t length) : Header::Header(Port sourcePort, Port destinationPort, uint16_t length) :
@ -95,4 +97,5 @@ namespace Net::Udp
bufferSize - udpHeader.SerializedLength()); bufferSize - udpHeader.SerializedLength());
} }
} }
} // namespace Net::Udp } // namespace Udp
} // namespace Net

View file

@ -6,8 +6,10 @@
#include "net-ethernet.h" #include "net-ethernet.h"
#include "net-ipv4.h" #include "net-ipv4.h"
namespace Net::Udp namespace Net
{ {
namespace Udp
{
enum class Port : uint16_t enum class Port : uint16_t
{ {
DhcpServer = 67, DhcpServer = 67,
@ -27,7 +29,8 @@ namespace Net::Udp
static constexpr size_t SerializedLength() 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; size_t Serialize(uint8_t* buffer, const size_t size) const;
@ -39,4 +42,5 @@ namespace Net::Udp
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 Udp
} // namespace Net

View file

@ -3,8 +3,10 @@
#include "types.h" #include "types.h"
#include <uspi.h> #include <uspi.h>
namespace Net::Utils namespace Net
{ {
namespace 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;
@ -96,4 +98,5 @@ namespace Net::Utils
return macAddress; return macAddress;
} }
} // namespace Net::Utils } // namespace Utils
} // namespace Net

View file

@ -2,8 +2,10 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
namespace Net::Utils namespace Net
{ {
namespace Utils
{
typedef std::array<uint8_t, 6> MacAddress; typedef std::array<uint8_t, 6> MacAddress;
extern const MacAddress MacBroadcast; extern const MacAddress MacBroadcast;
extern uint32_t Ipv4Address; extern uint32_t Ipv4Address;
@ -11,4 +13,5 @@ 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 } // namespace Utils
} // namespace Net

View file

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