Address second part of review comments

This commit is contained in:
FearlessTobi 2019-11-03 08:07:04 +01:00
parent 0fe11746fc
commit bbd85a495a
4 changed files with 18 additions and 14 deletions

View file

@ -32,21 +32,21 @@ namespace Response {
std::optional<Type> Validate(u8* data, std::size_t size) {
if (size < sizeof(Header)) {
LOG_DEBUG(Input, "Invalid UDP packet received");
return {};
return std::nullopt;
}
Header header{};
std::memcpy(&header, data, sizeof(Header));
if (header.magic != SERVER_MAGIC) {
LOG_ERROR(Input, "UDP Packet has an unexpected magic value");
return {};
return std::nullopt;
}
if (header.protocol_version != PROTOCOL_VERSION) {
LOG_ERROR(Input, "UDP Packet protocol mismatch");
return {};
return std::nullopt;
}
if (header.type < Type::Version || header.type > Type::PadData) {
LOG_ERROR(Input, "UDP Packet is an unknown type");
return {};
return std::nullopt;
}
// Packet size must equal sizeof(Header) + sizeof(Data)
@ -59,7 +59,7 @@ std::optional<Type> Validate(u8* data, std::size_t size) {
Input,
"UDP Packet payload length doesn't match. Received: {} PayloadLength: {} Expected: {}",
size, header.payload_length, data_len + sizeof(Type));
return {};
return std::nullopt;
}
const u32 crc32 = header.crc;
@ -70,7 +70,7 @@ std::optional<Type> Validate(u8* data, std::size_t size) {
result.process_bytes(data, data_len + sizeof(Header));
if (crc32 != result.checksum()) {
LOG_ERROR(Input, "UDP Packet CRC check failed. Offset: {}", offsetof(Header, crc));
return {};
return std::nullopt;
}
return header.type;
}