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)
{}
constexpr static std::size_t SerializedLength()
{
return sizeof(EthernetFrameHeader);
}
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"));
}
if (packet.data.size() < 512)
if (packet.data.size() < TFTP_BLOCK_SIZE)
{
// Close the file for the last packet.
f_close(&outFile);

View File

@ -15,7 +15,8 @@
//
// ARP
//
void SendArpPacket(ArpOperation operation,
void SendArpPacket(
ArpOperation operation,
MacAddress targetMac,
MacAddress senderMac,
uint32_t targetIp,
@ -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
{