idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
endpoint.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
17#include <idfxx/net>
18
19#include <cstddef>
20#include <cstdint>
21#include <optional>
22#include <span>
23#include <string>
24#include <string_view>
25#include <utility>
26#include <variant>
27
28namespace idfxx::net {
29
37enum class address_family : int {
38 ipv4 = 2,
39 ipv6 = 10,
40};
41
50enum class ip_protocol : uint8_t {
51 icmp = 1,
52 igmp = 2,
53 tcp = 6,
54 udp = 17,
55 icmpv6 = 58,
56 udp_lite = 136,
57};
58
61
70enum class direction : int {
71 receive,
72 send,
73};
74
84enum class dscp : uint8_t {
85 default_ = 0,
86
87 // Class selectors (RFC 2474 ยง4.2.2 โ€” backwards-compat with legacy IP precedence).
88 cs1 = 8,
89 cs2 = 16,
90 cs3 = 24,
91 cs4 = 32,
92 cs5 = 40,
93 cs6 = 48,
94 cs7 = 56,
95
96 // Assured forwarding (RFC 2597).
97 af11 = 10,
98 af12 = 12,
99 af13 = 14,
100 af21 = 18,
101 af22 = 20,
102 af23 = 22,
103 af31 = 26,
104 af32 = 28,
105 af33 = 30,
106 af41 = 34,
107 af42 = 36,
108 af43 = 38,
109
110 voice_admit = 44,
111 ef = 46,
112};
113
129class endpoint {
130public:
138 : _ep(std::pair<ipv4_addr, port_number>{addr, port}) {}
139
147 : _ep(std::pair<ipv6_addr, port_number>{addr, port}) {}
148
162 [[nodiscard]] static std::optional<endpoint> parse(std::string_view s) noexcept;
163
170 if (std::holds_alternative<std::pair<ipv4_addr, port_number>>(_ep)) {
172 }
174 }
175
182 return std::visit([](const auto& p) { return p.second; }, _ep);
183 }
184
199 template<ip_address T>
200 [[nodiscard]] constexpr std::optional<T> address() const noexcept {
201 if (auto* p = std::get_if<std::pair<T, port_number>>(&_ep)) {
202 return p->first;
203 }
204 return std::nullopt;
205 }
206
208 [[nodiscard]] constexpr bool operator==(const endpoint&) const noexcept = default;
209
210private:
211 std::variant<std::pair<ipv4_addr, port_number>, std::pair<ipv6_addr, port_number>> _ep;
212};
213
228struct datagram {
229 std::span<std::byte> data;
233 bool truncated = false;
234};
235
236} // namespace idfxx::net
237
238namespace idfxx {
239
249[[nodiscard]] std::string to_string(const net::endpoint& ep);
250
251} // namespace idfxx
252
253#include "sdkconfig.h"
254#ifdef CONFIG_IDFXX_STD_FORMAT
256#include <format>
257#include <string_view>
258namespace std {
259
260// Composes std::formatter<std::string_view> so that the standard fill, align,
261// width, and precision spec all work on an endpoint:
262// std::format("{}", ep) -> "1.2.3.4:8080"
263// std::format("{:>30}", ep) -> right-aligned to width 30
264// std::format("{:*^25}", ep) -> center-padded with '*' to width 25
265template<>
266struct formatter<idfxx::net::endpoint> {
267 std::formatter<std::string_view> _underlying;
268
269 constexpr auto parse(format_parse_context& ctx) { return _underlying.parse(ctx); }
270
271 template<typename FormatContext>
272 auto format(const idfxx::net::endpoint& ep, FormatContext& ctx) const {
273 return _underlying.format(idfxx::to_string(ep), ctx);
274 }
275};
276
277} // namespace std
279#endif // CONFIG_IDFXX_STD_FORMAT
280
// end of idfxx_net_endpoint
Address/port pair identifying a transport endpoint.
Definition endpoint.hpp:129
constexpr bool operator==(const endpoint &) const noexcept=default
Compares two endpoints for equality.
constexpr endpoint(ipv4_addr addr, port_number port) noexcept
Constructs an IPv4 endpoint.
Definition endpoint.hpp:137
constexpr port_number port() const noexcept
Returns the port number in host byte order.
Definition endpoint.hpp:181
static std::optional< endpoint > parse(std::string_view s) noexcept
Parses an endpoint from a host:port or [host]:port string.
constexpr endpoint(ipv6_addr addr, port_number port) noexcept
Constructs an IPv6 endpoint.
Definition endpoint.hpp:146
constexpr std::optional< T > address() const noexcept
Returns the address held by this endpoint, if it is of the requested type.
Definition endpoint.hpp:200
constexpr address_family family() const noexcept
Returns the address family of this endpoint.
Definition endpoint.hpp:169
IPv4 address value type.
Definition net.hpp:41
IPv6 address value type.
Definition net.hpp:132
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
address_family
Address family.
Definition endpoint.hpp:37
ip_protocol
IP-layer protocol number, per IANA assigned protocol numbers.
Definition endpoint.hpp:50
@ icmp
ICMP for IPv4.
@ icmpv6
ICMP for IPv6.
@ udp_lite
UDP-Lite (RFC 3828).
direction
One direction of a bidirectional transport.
Definition endpoint.hpp:70
@ send
The send direction.
@ receive
The receive direction.
uint16_t port_number
Port number type for transport endpoints.
Definition endpoint.hpp:60
dscp
Differentiated Services Code Point โ€” the top 6 bits of the IPv4/IPv6 traffic-class byte,...
Definition endpoint.hpp:84
@ default_
CS0 โ€” best-effort (default).
@ voice_admit
Voice-admit (RFC 5865).
@ ef
Expedited forwarding (RFC 3246).
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
A datagram received together with the sender's endpoint.
Definition endpoint.hpp:228
bool truncated
True if the datagram was larger than the buffer; data holds the leading portion and the remainder was...
Definition endpoint.hpp:233
endpoint from
Source endpoint of the datagram.
Definition endpoint.hpp:230
std::span< std::byte > data
Filled portion of the caller's receive buffer (aliases it).
Definition endpoint.hpp:229