From 41ee2d9777fbd955609ff2410ed367ac089c603e Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 12 Dec 2020 18:12:03 +0100 Subject: [PATCH] Replace some sizeofs with SerializedLengths --- src/net-ethernet.h | 4 ++++ src/net-tftp.cpp | 2 +- src/net.cpp | 34 ++++++++++++++++------------------ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/net-ethernet.h b/src/net-ethernet.h index 53f09fd..9730fa9 100644 --- a/src/net-ethernet.h +++ b/src/net-ethernet.h @@ -19,6 +19,10 @@ struct EthernetFrameHeader macDestination(macDestination), macSource(macSource), type(type) {} + constexpr static std::size_t SerializedLength() + { + return sizeof(EthernetFrameHeader); + } std::size_t Serialize(uint8_t* buffer) { diff --git a/src/net-tftp.cpp b/src/net-tftp.cpp index b837e60..e5b9d24 100644 --- a/src/net-tftp.cpp +++ b/src/net-tftp.cpp @@ -94,7 +94,7 @@ static std::unique_ptr handleTftpData(const uint8_t* data, size_t le return std::unique_ptr(new TftpErrorPacket(0, "io error")); } - if (packet.data.size() < 512) + if (packet.data.size() < TFTP_BLOCK_SIZE) { // Close the file for the last packet. f_close(&outFile); diff --git a/src/net.cpp b/src/net.cpp index e591127..577e365 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -15,11 +15,12 @@ // // ARP // -void SendArpPacket(ArpOperation operation, - MacAddress targetMac, - MacAddress senderMac, - uint32_t targetIp, - uint32_t senderIp) +void SendArpPacket( + ArpOperation operation, + MacAddress targetMac, + MacAddress senderMac, + uint32_t targetIp, + uint32_t senderIp) { Ipv4ArpPacket arp(operation); arp.targetMac = targetMac; @@ -36,18 +37,14 @@ void SendArpPacket(ArpOperation operation, USPiSendFrame(buffer, size); } -void SendArpRequest(MacAddress targetMac, - MacAddress senderMac, - uint32_t targetIp, - uint32_t senderIp) +void SendArpRequest( + MacAddress targetMac, MacAddress senderMac, uint32_t targetIp, uint32_t senderIp) { SendArpPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp); } -void SendArpReply(MacAddress targetMac, - MacAddress senderMac, - uint32_t targetIp, - uint32_t senderIp) +void SendArpReply( + MacAddress targetMac, MacAddress senderMac, uint32_t targetIp, uint32_t senderIp) { SendArpPacket(ARP_OPERATION_REPLY, targetMac, senderMac, targetIp, senderIp); } @@ -116,11 +113,12 @@ void HandleUdpFrame(const uint8_t* buffer) const auto header = frame.payload.payload; uint8_t* data = (uint8_t*)malloc(header.length); - memcpy( - data, - buffer + sizeof(EthernetFrameHeader) + sizeof(Ipv4Header) + sizeof(UdpDatagramHeader), - header.length - ); + const auto size = + buffer + + EthernetFrameHeader::SerializedLength() + + Ipv4Header::SerializedLength() + + UdpDatagramHeader::SerializedLength(); + memcpy(data, size, header.length); if (header.destinationPort == 69) // nice {