diff --git a/src/net-arp.cpp b/src/net-arp.cpp index 18f0be5..7fabb6d 100644 --- a/src/net-arp.cpp +++ b/src/net-arp.cpp @@ -132,8 +132,8 @@ namespace Net::Arp const auto macAddress = Utils::GetMacAddress(); Packet arpPacket; - size_t arpSize = arpPacket.Deserialize(buffer, bufferSize); - if (arpSize == 0 || arpSize != arpPacket.SerializedLength()) + const auto arpSize = arpPacket.Deserialize(buffer, bufferSize); + if (arpSize == 0) { DEBUG_LOG( "Dropped ARP packet (invalid buffer size %u, expected %u)\r\n", diff --git a/src/net-dhcp.cpp b/src/net-dhcp.cpp index cbc2e40..06ad1a9 100644 --- a/src/net-dhcp.cpp +++ b/src/net-dhcp.cpp @@ -10,7 +10,6 @@ #include "debug.h" #include "types.h" #include -#include namespace Net::Dhcp { diff --git a/src/net.cpp b/src/net.cpp index 9e6865d..68fb050 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -34,21 +34,8 @@ namespace Net postInitializeTime = read32(ARM_SYSTIMER_CLO) + 30000; } - void Update() + void HandlePacket(const uint8_t* buffer, const size_t bufferSize) { - if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime) - { - postInitialize(); - postInitializeTime = 0; - } - - unsigned int bufferSize = 0; - uint8_t buffer[USPI_FRAME_BUFFER_SIZE]; - if (!USPiReceiveFrame(buffer, &bufferSize)) - { - return; - } - Ethernet::Header ethernetHeader; auto headerSize = Ethernet::Header::Deserialize(ethernetHeader, buffer, bufferSize); if (headerSize == 0 || headerSize != Ethernet::Header::SerializedLength()) @@ -71,6 +58,25 @@ namespace Net } } + + void Update() + { + if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime) + { + postInitialize(); + postInitializeTime = 0; + } + + unsigned int bufferSize = 0; + uint8_t buffer[USPI_FRAME_BUFFER_SIZE]; + if (!USPiReceiveFrame(buffer, &bufferSize)) + { + return; + } + + HandlePacket(buffer, sizeof(buffer)); + } + static void postInitialize() { DEBUG_LOG("Running network post-init\r\n"); diff --git a/src/net.h b/src/net.h index 42475e5..d8214f2 100644 --- a/src/net.h +++ b/src/net.h @@ -10,5 +10,6 @@ namespace Net { void Initialize(Options& options); + void HandlePacket(const uint8_t* buffer, const size_t bufferSize); void Update(); } // namespace Net diff --git a/tests/net-utils.cpp b/tests/net-utils.cpp index bbc4762..f625e30 100644 --- a/tests/net-utils.cpp +++ b/tests/net-utils.cpp @@ -9,19 +9,19 @@ void TestNetUtilsInternetChecksum() { { const char buffer[] = {1, 2, 3, 4, 5, 6, 7, 8}; - uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); + const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); TEST_CHECK(checksum == 61419); } { const char buffer[] = {1, 2, 3, 4, 5, 6, 7}; - uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); + const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); TEST_CHECK(checksum == 61427); } { const char buffer[] = {}; - uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); + const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer)); TEST_CHECK(checksum == 65535); } } @@ -30,20 +30,20 @@ void TestNetUtilsCrc32() { { const uint8_t buffer[] = {1, 2, 3, 4, 5, 6, 7, 8}; - uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer)); + const auto checksum = Utils::Crc32(buffer, sizeof(buffer)); TEST_CHECK(checksum == 1070237893); } { const uint8_t buffer[] = {}; - uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer)); + const auto checksum = Utils::Crc32(buffer, sizeof(buffer)); TEST_CHECK(checksum == 0); } } void TestNetUtilsGetMacAddress() { - auto mac = Utils::GetMacAddress(); - auto expected = Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50}; + const auto mac = Utils::GetMacAddress(); + const auto expected = Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50}; TEST_CHECK(mac == expected); }