diff options
Diffstat (limited to 'include/wireframe/net/udp.hpp')
| -rw-r--r-- | include/wireframe/net/udp.hpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/include/wireframe/net/udp.hpp b/include/wireframe/net/udp.hpp new file mode 100644 index 0000000..07664c2 --- /dev/null +++ b/include/wireframe/net/udp.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <cstdint> +#include <optional> +#include <span> + +#include "wireframe/byteio.hpp" + +namespace wireframe::net { + +inline constexpr std::size_t kUdpHeaderLen = 8; + +struct UdpHeader { + std::uint16_t src_port; + std::uint16_t dst_port; + std::uint16_t length; +}; + +struct UdpDatagram { + UdpHeader header; + std::span<const unsigned char> payload; +}; + +inline std::optional<UdpDatagram> parse_udp(std::span<const unsigned char> bytes) { + if (bytes.size() < kUdpHeaderLen) return std::nullopt; + + UdpHeader header{}; + header.src_port = read_be16(bytes, 0); + header.dst_port = read_be16(bytes, 2); + header.length = read_be16(bytes, 4); + + return UdpDatagram{header, bytes.subspan(kUdpHeaderLen)}; +} + +} // namespace wireframe::net |