From ac3690f2057fb93ce18f156ff5ffd720a6d6f60c Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sat, 24 Aug 2019 15:57:49 +0200 Subject: Input: UDP Client to provide motion and touch controls An implementation of the cemuhook motion/touch protocol, this adds the ability for users to connect several different devices to citra to send direct motion and touch data to citra. Co-Authored-By: jroweboy --- src/input_common/udp/protocol.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/input_common/udp/protocol.cpp (limited to 'src/input_common/udp/protocol.cpp') 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 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include "common/logging/log.h" +#include "input_common/udp/protocol.h" + +namespace InputCommon::CemuhookUDP { + +static const std::size_t GetSizeOfResponseType(Type t) { + switch (t) { + case Type::Version: + return sizeof(Response::Version); + case Type::PortInfo: + return sizeof(Response::PortInfo); + case Type::PadData: + return sizeof(Response::PadData); + } + return 0; +} + +namespace Response { + +/** + * Returns Type if the packet is valid, else none + * + * Note: Modifies the buffer to zero out the crc (since thats the easiest way to check without + * copying the buffer) + */ +std::optional Validate(u8* data, std::size_t size) { + if (size < sizeof(Header)) { + LOG_DEBUG(Input, "Invalid UDP packet received"); + return {}; + } + Header header; + std::memcpy(&header, data, sizeof(Header)); + if (header.magic != SERVER_MAGIC) { + LOG_ERROR(Input, "UDP Packet has an unexpected magic value"); + return {}; + } + if (header.protocol_version != PROTOCOL_VERSION) { + LOG_ERROR(Input, "UDP Packet protocol mismatch"); + return {}; + } + if (header.type < Type::Version || header.type > Type::PadData) { + LOG_ERROR(Input, "UDP Packet is an unknown type"); + return {}; + } + + // Packet size must equal sizeof(Header) + sizeof(Data) + // and also verify that the packet info mentions the correct size. Since the spec includes the + // type of the packet as part of the data, we need to include it in size calculations here + // ie: payload_length == sizeof(T) + sizeof(Type) + const std::size_t data_len = GetSizeOfResponseType(header.type); + if (header.payload_length != data_len + sizeof(Type) || size < data_len + sizeof(Header)) { + LOG_ERROR( + Input, + "UDP Packet payload length doesn't match. Received: {} PayloadLength: {} Expected: {}", + size, header.payload_length, data_len + sizeof(Type)); + return {}; + } + + const u32 crc32 = header.crc; + boost::crc_32_type result; + // zero out the crc in the buffer and then run the crc against it + std::memset(&data[offsetof(Header, crc)], 0, sizeof(u32_le)); + + 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 header.type; +} +} // namespace Response + +} // namespace InputCommon::CemuhookUDP -- cgit v1.2.3 From 0fe11746fcb37de2465cdbbe74be6ad4a59228e5 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Sun, 3 Nov 2019 07:04:28 +0100 Subject: Address review comments --- src/input_common/udp/protocol.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/input_common/udp/protocol.cpp') diff --git a/src/input_common/udp/protocol.cpp b/src/input_common/udp/protocol.cpp index d65069207..16da706d5 100644 --- a/src/input_common/udp/protocol.cpp +++ b/src/input_common/udp/protocol.cpp @@ -9,7 +9,7 @@ namespace InputCommon::CemuhookUDP { -static const std::size_t GetSizeOfResponseType(Type t) { +static constexpr std::size_t GetSizeOfResponseType(Type t) { switch (t) { case Type::Version: return sizeof(Response::Version); @@ -34,7 +34,7 @@ std::optional Validate(u8* data, std::size_t size) { LOG_DEBUG(Input, "Invalid UDP packet received"); return {}; } - Header header; + Header header{}; std::memcpy(&header, data, sizeof(Header)); if (header.magic != SERVER_MAGIC) { LOG_ERROR(Input, "UDP Packet has an unexpected magic value"); -- cgit v1.2.3 From bbd85a495a3576a5ec99cd69b54e983653b38ea4 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Sun, 3 Nov 2019 08:07:04 +0100 Subject: Address second part of review comments --- src/input_common/udp/protocol.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/input_common/udp/protocol.cpp') diff --git a/src/input_common/udp/protocol.cpp b/src/input_common/udp/protocol.cpp index 16da706d5..a982ac49d 100644 --- a/src/input_common/udp/protocol.cpp +++ b/src/input_common/udp/protocol.cpp @@ -32,21 +32,21 @@ namespace Response { std::optional 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 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 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; } -- cgit v1.2.3