diff options
Diffstat (limited to 'src/input_common/udp/protocol.cpp')
| -rw-r--r-- | src/input_common/udp/protocol.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/input_common/udp/protocol.cpp b/src/input_common/udp/protocol.cpp new file mode 100644 index 000000000..d65069207 --- /dev/null +++ b/src/input_common/udp/protocol.cpp | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <cstring> | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "input_common/udp/protocol.h" | ||
| 9 | |||
| 10 | namespace InputCommon::CemuhookUDP { | ||
| 11 | |||
| 12 | static const std::size_t GetSizeOfResponseType(Type t) { | ||
| 13 | switch (t) { | ||
| 14 | case Type::Version: | ||
| 15 | return sizeof(Response::Version); | ||
| 16 | case Type::PortInfo: | ||
| 17 | return sizeof(Response::PortInfo); | ||
| 18 | case Type::PadData: | ||
| 19 | return sizeof(Response::PadData); | ||
| 20 | } | ||
| 21 | return 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | namespace Response { | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Returns Type if the packet is valid, else none | ||
| 28 | * | ||
| 29 | * Note: Modifies the buffer to zero out the crc (since thats the easiest way to check without | ||
| 30 | * copying the buffer) | ||
| 31 | */ | ||
| 32 | std::optional<Type> Validate(u8* data, std::size_t size) { | ||
| 33 | if (size < sizeof(Header)) { | ||
| 34 | LOG_DEBUG(Input, "Invalid UDP packet received"); | ||
| 35 | return {}; | ||
| 36 | } | ||
| 37 | Header header; | ||
| 38 | std::memcpy(&header, data, sizeof(Header)); | ||
| 39 | if (header.magic != SERVER_MAGIC) { | ||
| 40 | LOG_ERROR(Input, "UDP Packet has an unexpected magic value"); | ||
| 41 | return {}; | ||
| 42 | } | ||
| 43 | if (header.protocol_version != PROTOCOL_VERSION) { | ||
| 44 | LOG_ERROR(Input, "UDP Packet protocol mismatch"); | ||
| 45 | return {}; | ||
| 46 | } | ||
| 47 | if (header.type < Type::Version || header.type > Type::PadData) { | ||
| 48 | LOG_ERROR(Input, "UDP Packet is an unknown type"); | ||
| 49 | return {}; | ||
| 50 | } | ||
| 51 | |||
| 52 | // Packet size must equal sizeof(Header) + sizeof(Data) | ||
| 53 | // and also verify that the packet info mentions the correct size. Since the spec includes the | ||
| 54 | // type of the packet as part of the data, we need to include it in size calculations here | ||
| 55 | // ie: payload_length == sizeof(T) + sizeof(Type) | ||
| 56 | const std::size_t data_len = GetSizeOfResponseType(header.type); | ||
| 57 | if (header.payload_length != data_len + sizeof(Type) || size < data_len + sizeof(Header)) { | ||
| 58 | LOG_ERROR( | ||
| 59 | Input, | ||
| 60 | "UDP Packet payload length doesn't match. Received: {} PayloadLength: {} Expected: {}", | ||
| 61 | size, header.payload_length, data_len + sizeof(Type)); | ||
| 62 | return {}; | ||
| 63 | } | ||
| 64 | |||
| 65 | const u32 crc32 = header.crc; | ||
| 66 | boost::crc_32_type result; | ||
| 67 | // zero out the crc in the buffer and then run the crc against it | ||
| 68 | std::memset(&data[offsetof(Header, crc)], 0, sizeof(u32_le)); | ||
| 69 | |||
| 70 | result.process_bytes(data, data_len + sizeof(Header)); | ||
| 71 | if (crc32 != result.checksum()) { | ||
| 72 | LOG_ERROR(Input, "UDP Packet CRC check failed. Offset: {}", offsetof(Header, crc)); | ||
| 73 | return {}; | ||
| 74 | } | ||
| 75 | return header.type; | ||
| 76 | } | ||
| 77 | } // namespace Response | ||
| 78 | |||
| 79 | } // namespace InputCommon::CemuhookUDP | ||