Replace some sizeofs with SerializedLengths

This commit is contained in:
Sijmen 2020-12-12 18:12:03 +01:00
parent 60e186cdc9
commit 41ee2d9777
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
3 changed files with 21 additions and 19 deletions

View File

@ -19,6 +19,10 @@ struct EthernetFrameHeader
macDestination(macDestination), macSource(macSource), type(type) macDestination(macDestination), macSource(macSource), type(type)
{} {}
constexpr static std::size_t SerializedLength()
{
return sizeof(EthernetFrameHeader);
}
std::size_t Serialize(uint8_t* buffer) std::size_t Serialize(uint8_t* buffer)
{ {

View File

@ -94,7 +94,7 @@ static std::unique_ptr<TftpPacket> handleTftpData(const uint8_t* data, size_t le
return std::unique_ptr<TftpErrorPacket>(new TftpErrorPacket(0, "io error")); return std::unique_ptr<TftpErrorPacket>(new TftpErrorPacket(0, "io error"));
} }
if (packet.data.size() < 512) if (packet.data.size() < TFTP_BLOCK_SIZE)
{ {
// Close the file for the last packet. // Close the file for the last packet.
f_close(&outFile); f_close(&outFile);

View File

@ -15,11 +15,12 @@
// //
// ARP // ARP
// //
void SendArpPacket(ArpOperation operation, void SendArpPacket(
MacAddress targetMac, ArpOperation operation,
MacAddress senderMac, MacAddress targetMac,
uint32_t targetIp, MacAddress senderMac,
uint32_t senderIp) uint32_t targetIp,
uint32_t senderIp)
{ {
Ipv4ArpPacket arp(operation); Ipv4ArpPacket arp(operation);
arp.targetMac = targetMac; arp.targetMac = targetMac;
@ -36,18 +37,14 @@ void SendArpPacket(ArpOperation operation,
USPiSendFrame(buffer, size); USPiSendFrame(buffer, size);
} }
void SendArpRequest(MacAddress targetMac, void SendArpRequest(
MacAddress senderMac, MacAddress targetMac, MacAddress senderMac, uint32_t targetIp, uint32_t senderIp)
uint32_t targetIp,
uint32_t senderIp)
{ {
SendArpPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp); SendArpPacket(ARP_OPERATION_REQUEST, targetMac, senderMac, targetIp, senderIp);
} }
void SendArpReply(MacAddress targetMac, void SendArpReply(
MacAddress senderMac, MacAddress targetMac, MacAddress senderMac, uint32_t targetIp, uint32_t senderIp)
uint32_t targetIp,
uint32_t senderIp)
{ {
SendArpPacket(ARP_OPERATION_REPLY, targetMac, senderMac, targetIp, 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; const auto header = frame.payload.payload;
uint8_t* data = (uint8_t*)malloc(header.length); uint8_t* data = (uint8_t*)malloc(header.length);
memcpy( const auto size =
data, buffer +
buffer + sizeof(EthernetFrameHeader) + sizeof(Ipv4Header) + sizeof(UdpDatagramHeader), EthernetFrameHeader::SerializedLength() +
header.length Ipv4Header::SerializedLength() +
); UdpDatagramHeader::SerializedLength();
memcpy(data, size, header.length);
if (header.destinationPort == 69) // nice if (header.destinationPort == 69) // nice
{ {