diff --git a/src/stream.cpp b/src/stream.cpp index f6fe2eeaa..04d75c05f 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -4,9 +4,12 @@ */ // standard includes +#include #include #include +#include #include +#include // lib includes #include @@ -630,6 +633,29 @@ namespace stream { static auto broadcast = safe::make_shared(start_broadcast, end_broadcast); + /** + * @brief Parse a control message from an ENet packet. + * + * @param packet ENet packet containing the control message type and payload. + * @return The message type and payload view, or `std::nullopt` when the packet lacks a complete type. + */ + std::optional> parse_control_packet(const ENetPacket &packet) { + if (packet.data == nullptr || packet.dataLength < sizeof(std::uint16_t)) { + return std::nullopt; + } + + std::uint16_t type; + std::memcpy(&type, packet.data, sizeof(type)); + + return std::pair { + type, + std::string_view { + reinterpret_cast(packet.data) + sizeof(type), + packet.dataLength - sizeof(type), + }, + }; + } + session_t *control_server_t::get_session(const net::peer_t peer, uint32_t connect_data) { { // Fast path - look up existing session by peer @@ -740,11 +766,10 @@ namespace stream { case ENET_EVENT_TYPE_RECEIVE: { net::packet_t packet {event.packet}; - - auto type = *(std::uint16_t *) packet->data; - std::string_view payload {(char *) packet->data + sizeof(type), packet->dataLength - sizeof(type)}; - - call(type, session, payload, false); + auto message = parse_control_packet(*packet); + if (message) { + call(message->first, session, message->second, false); + } } break; case ENET_EVENT_TYPE_CONNECT: diff --git a/tests/unit/test_stream.cpp b/tests/unit/test_stream.cpp index 8e367661c..da89e28ec 100644 --- a/tests/unit/test_stream.cpp +++ b/tests/unit/test_stream.cpp @@ -7,14 +7,22 @@ #include "../tests_common.h" // standard includes +#include #include #include +#include #include +#include +#include #include +// local includes +#include + namespace stream { std::vector concat_and_insert(uint64_t insert_size, uint64_t slice_size, const std::string_view &data1, const std::string_view &data2); -} + std::optional> parse_control_packet(const ENetPacket &packet); +} // namespace stream TEST(ConcatAndInsertTests, ConcatNoInsertionTest) { char b1[] = {'a', 'b'}; @@ -39,3 +47,41 @@ TEST(ConcatAndInsertTests, ConcatSmallStrideTest) { auto expected = std::vector {0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e'}; ASSERT_EQ(res, expected); } + +TEST(ControlPacketTests, RejectsZeroLengthPacket) { + net::packet_t packet {enet_packet_create(nullptr, 0, 0)}; + + ASSERT_NE(packet, nullptr); + EXPECT_EQ(packet->data, nullptr); + EXPECT_EQ(stream::parse_control_packet(*packet), std::nullopt); +} + +TEST(ControlPacketTests, RejectsOneBytePacket) { + const std::uint8_t data {0x06}; + net::packet_t packet {enet_packet_create(&data, sizeof(data), 0)}; + + ASSERT_NE(packet, nullptr); + EXPECT_EQ(stream::parse_control_packet(*packet), std::nullopt); +} + +TEST(ControlPacketTests, AcceptsTypeWithoutPayload) { + const std::array data {0x06, 0x02}; + net::packet_t packet {enet_packet_create(data.data(), data.size(), 0)}; + + ASSERT_NE(packet, nullptr); + auto message = stream::parse_control_packet(*packet); + ASSERT_TRUE(message); + EXPECT_EQ(message->first, 0x0206); + EXPECT_TRUE(message->second.empty()); +} + +TEST(ControlPacketTests, AcceptsTypeAndPayload) { + const std::array data {0x06, 0x02, 'a', 'b', 'c'}; + net::packet_t packet {enet_packet_create(data.data(), data.size(), 0)}; + + ASSERT_NE(packet, nullptr); + auto message = stream::parse_control_packet(*packet); + ASSERT_TRUE(message); + EXPECT_EQ(message->first, 0x0206); + EXPECT_EQ(message->second, "abc"); +}