idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
net.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
20#include <array>
21#include <concepts>
22#include <cstdint>
23#include <optional>
24#include <string>
25#include <string_view>
26
27namespace idfxx::net {
28
41class ipv4_addr {
42public:
59 [[nodiscard]] static std::optional<ipv4_addr> parse(std::string_view s) noexcept;
60
64 constexpr ipv4_addr() noexcept = default;
65
75
81 constexpr explicit ipv4_addr(uint32_t addr) noexcept
82 : _addr(addr) {}
83
93 : _addr(
94 static_cast<uint32_t>(a) | (static_cast<uint32_t>(b) << 8) | (static_cast<uint32_t>(c) << 16) |
95 (static_cast<uint32_t>(d) << 24)
96 ) {}
97
103 [[nodiscard]] constexpr uint32_t addr() const noexcept { return _addr; }
104
110 [[nodiscard]] constexpr bool is_any() const noexcept { return _addr == 0; }
111
115 [[nodiscard]] constexpr bool operator==(const ipv4_addr&) const noexcept = default;
116
117private:
118 uint32_t _addr = 0;
119};
120
133public:
152 [[nodiscard]] static std::optional<ipv6_addr> parse(std::string_view s) noexcept;
153
158
168
175 constexpr explicit ipv6_addr(std::array<uint32_t, 4> words, uint8_t zone = 0) noexcept
176 : _addr(words)
177 , _zone(zone) {}
178
184 [[nodiscard]] constexpr const std::array<uint32_t, 4>& addr() const noexcept { return _addr; }
185
191 [[nodiscard]] constexpr uint8_t zone() const noexcept { return _zone; }
192
198 [[nodiscard]] constexpr bool is_any() const noexcept {
199 return _addr[0] == 0 && _addr[1] == 0 && _addr[2] == 0 && _addr[3] == 0;
200 }
201
205 [[nodiscard]] constexpr bool operator==(const ipv6_addr&) const noexcept = default;
206
207private:
208 std::array<uint32_t, 4> _addr = {};
209 uint8_t _zone = 0;
210};
211
218template<typename T>
219concept ip_address = std::same_as<T, ipv4_addr> || std::same_as<T, ipv6_addr>;
220
227struct ipv4_info {
235 [[nodiscard]] constexpr bool operator==(const ipv4_info&) const noexcept = default;
236};
237
244struct ipv6_info {
246};
247
249// ----- backward-compatibility aliases -----
250//
251// `ipv4_addr` / `ipv6_addr` / `ipv4_info` / `ipv6_info` were originally named
252// `ip4_addr` / `ip6_addr` / `ip4_info` / `ip6_info`. The aliases preserve the
253// old spelling for code written against earlier releases.
254using ip4_addr [[deprecated("use ipv4_addr")]] = ipv4_addr;
255using ip6_addr [[deprecated("use ipv6_addr")]] = ipv6_addr;
256using ip4_info [[deprecated("use ipv4_info")]] = ipv4_info;
257using ip6_info [[deprecated("use ipv6_info")]] = ipv6_info;
259
260} // namespace idfxx::net
261
262// =============================================================================
263// String conversions (in idfxx namespace)
264// =============================================================================
265
266namespace idfxx {
267
275[[nodiscard]] std::string to_string(net::ipv4_addr addr);
276
286[[nodiscard]] std::string to_string(net::ipv6_addr addr);
287
295[[nodiscard]] std::string to_string(net::ipv4_info info);
296
297} // namespace idfxx
298
299#include "sdkconfig.h"
300#ifdef CONFIG_IDFXX_STD_FORMAT
302#include <algorithm>
303#include <format>
304namespace std {
305
306template<>
307struct formatter<idfxx::net::ipv4_addr> {
308 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
309
310 template<typename FormatContext>
311 auto format(idfxx::net::ipv4_addr addr, FormatContext& ctx) const {
312 auto s = idfxx::to_string(addr);
313 return std::copy(s.begin(), s.end(), ctx.out());
314 }
315};
316
317template<>
318struct formatter<idfxx::net::ipv6_addr> {
319 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
320
321 template<typename FormatContext>
322 auto format(idfxx::net::ipv6_addr addr, FormatContext& ctx) const {
323 auto s = idfxx::to_string(addr);
324 return std::copy(s.begin(), s.end(), ctx.out());
325 }
326};
327
328template<>
329struct formatter<idfxx::net::ipv4_info> {
330 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
331
332 template<typename FormatContext>
333 auto format(idfxx::net::ipv4_info info, FormatContext& ctx) const {
334 auto s = idfxx::to_string(info);
335 return std::copy(s.begin(), s.end(), ctx.out());
336 }
337};
338
339} // namespace std
341#endif // CONFIG_IDFXX_STD_FORMAT
342
// end of idfxx_core_net
IPv4 address value type.
Definition net.hpp:41
constexpr uint32_t addr() const noexcept
Returns the raw address value in network byte order.
Definition net.hpp:103
constexpr ipv4_addr(uint8_t a, uint8_t b, uint8_t c, uint8_t d) noexcept
Constructs an IPv4 address from individual octets.
Definition net.hpp:92
constexpr bool operator==(const ipv4_addr &) const noexcept=default
Compares two IPv4 addresses for equality.
static constexpr ipv4_addr any() noexcept
Returns the IPv4 wildcard address (0.0.0.0).
Definition net.hpp:74
constexpr ipv4_addr(uint32_t addr) noexcept
Constructs an IPv4 address from a raw network-byte-order value.
Definition net.hpp:81
static std::optional< ipv4_addr > parse(std::string_view s) noexcept
Parses an IPv4 address from dotted-decimal notation.
constexpr bool is_any() const noexcept
Tests whether this is a zero (unset) address.
Definition net.hpp:110
constexpr ipv4_addr() noexcept=default
Constructs a zero-initialized IPv4 address (0.0.0.0).
IPv6 address value type.
Definition net.hpp:132
constexpr uint8_t zone() const noexcept
Returns the zone ID.
Definition net.hpp:191
constexpr ipv6_addr() noexcept=default
Constructs a zero-initialized IPv6 address (::).
constexpr const std::array< uint32_t, 4 > & addr() const noexcept
Returns the four 32-bit words of the address.
Definition net.hpp:184
constexpr bool is_any() const noexcept
Tests whether this is a zero (unset) address.
Definition net.hpp:198
constexpr bool operator==(const ipv6_addr &) const noexcept=default
Compares two IPv6 addresses for equality (including zone).
static constexpr ipv6_addr any() noexcept
Returns the IPv6 wildcard address (::).
Definition net.hpp:167
constexpr ipv6_addr(std::array< uint32_t, 4 > words, uint8_t zone=0) noexcept
Constructs an IPv6 address from four 32-bit words.
Definition net.hpp:175
static std::optional< ipv6_addr > parse(std::string_view s) noexcept
Parses an IPv6 address from its text representation.
Concept matching either ipv4_addr or ipv6_addr.
Definition net.hpp:219
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
IPv4 network interface information.
Definition net.hpp:227
ipv4_addr gateway
Definition net.hpp:230
ipv4_addr netmask
Definition net.hpp:229
constexpr bool operator==(const ipv4_info &) const noexcept=default
Compares two ipv4_info structs for equality.
IPv6 network interface information.
Definition net.hpp:244