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
180 constexpr explicit ipv6_addr(std::array<uint32_t, 4> words, uint8_t zone = 0) noexcept
181 : _addr(words)
182 , _zone(zone) {}
183
189 [[nodiscard]] constexpr const std::array<uint32_t, 4>& addr() const noexcept { return _addr; }
190
196 [[nodiscard]] constexpr uint8_t zone() const noexcept { return _zone; }
197
203 [[nodiscard]] constexpr bool is_any() const noexcept {
204 return _addr[0] == 0 && _addr[1] == 0 && _addr[2] == 0 && _addr[3] == 0;
205 }
206
210 [[nodiscard]] constexpr bool operator==(const ipv6_addr&) const noexcept = default;
211
212private:
213 std::array<uint32_t, 4> _addr = {};
214 uint8_t _zone = 0;
215};
216
223template<typename T>
224concept ip_address = std::same_as<T, ipv4_addr> || std::same_as<T, ipv6_addr>;
225
232struct ipv4_info {
240 [[nodiscard]] constexpr bool operator==(const ipv4_info&) const noexcept = default;
241};
242
249struct ipv6_info {
255 [[nodiscard]] constexpr bool operator==(const ipv6_info&) const noexcept = default;
256};
257
259// ----- backward-compatibility aliases -----
260//
261// `ipv4_addr` / `ipv6_addr` / `ipv4_info` / `ipv6_info` were originally named
262// `ip4_addr` / `ip6_addr` / `ip4_info` / `ip6_info` (the public spelling shipped
263// in the released 1.0.0 line). The deprecated aliases preserve the old names so
264// code written against 1.0.0 keeps compiling; remove them only on a major bump.
265using ip4_addr [[deprecated("use ipv4_addr")]] = ipv4_addr;
266using ip6_addr [[deprecated("use ipv6_addr")]] = ipv6_addr;
267using ip4_info [[deprecated("use ipv4_info")]] = ipv4_info;
268using ip6_info [[deprecated("use ipv6_info")]] = ipv6_info;
270
271} // namespace idfxx::net
272
273// =============================================================================
274// String conversions (in idfxx namespace)
275// =============================================================================
276
277namespace idfxx {
278
286[[nodiscard]] std::string to_string(net::ipv4_addr addr);
287
297[[nodiscard]] std::string to_string(net::ipv6_addr addr);
298
306[[nodiscard]] std::string to_string(net::ipv4_info info);
307
315[[nodiscard]] std::string to_string(net::ipv6_info info);
316
317} // namespace idfxx
318
319#include "sdkconfig.h"
320#ifdef CONFIG_IDFXX_STD_FORMAT
322#include <format>
323#include <string_view>
324namespace std {
325
326// Each formatter composes std::formatter<std::string_view> so the standard
327// fill, align, width, and precision spec all work:
328// std::format("{}", addr) -> "192.168.1.1"
329// std::format("{:>30}", addr) -> right-aligned to width 30
330
331template<>
332struct formatter<idfxx::net::ipv4_addr> {
333 std::formatter<std::string_view> _underlying;
334
335 constexpr auto parse(format_parse_context& ctx) { return _underlying.parse(ctx); }
336
337 template<typename FormatContext>
338 auto format(idfxx::net::ipv4_addr addr, FormatContext& ctx) const {
339 return _underlying.format(idfxx::to_string(addr), ctx);
340 }
341};
342
343template<>
344struct formatter<idfxx::net::ipv6_addr> {
345 std::formatter<std::string_view> _underlying;
346
347 constexpr auto parse(format_parse_context& ctx) { return _underlying.parse(ctx); }
348
349 template<typename FormatContext>
350 auto format(idfxx::net::ipv6_addr addr, FormatContext& ctx) const {
351 return _underlying.format(idfxx::to_string(addr), ctx);
352 }
353};
354
355template<>
356struct formatter<idfxx::net::ipv4_info> {
357 std::formatter<std::string_view> _underlying;
358
359 constexpr auto parse(format_parse_context& ctx) { return _underlying.parse(ctx); }
360
361 template<typename FormatContext>
362 auto format(idfxx::net::ipv4_info info, FormatContext& ctx) const {
363 return _underlying.format(idfxx::to_string(info), ctx);
364 }
365};
366
367template<>
368struct formatter<idfxx::net::ipv6_info> {
369 std::formatter<std::string_view> _underlying;
370
371 constexpr auto parse(format_parse_context& ctx) { return _underlying.parse(ctx); }
372
373 template<typename FormatContext>
374 auto format(idfxx::net::ipv6_info info, FormatContext& ctx) const {
375 return _underlying.format(idfxx::to_string(info), ctx);
376 }
377};
378
379} // namespace std
381#endif // CONFIG_IDFXX_STD_FORMAT
382
// 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:196
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:189
constexpr bool is_any() const noexcept
Tests whether this is a zero (unset) address.
Definition net.hpp:203
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:180
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:224
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:232
ipv4_addr gateway
Definition net.hpp:235
ipv4_addr netmask
Definition net.hpp:234
constexpr bool operator==(const ipv4_info &) const noexcept=default
Compares two ipv4_info structs for equality.
IPv6 network interface information.
Definition net.hpp:249
constexpr bool operator==(const ipv6_info &) const noexcept=default
Compares two ipv6_info structs for equality.