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 <cstdint>
22#include <optional>
23#include <string>
24#include <string_view>
25
26namespace idfxx::net {
27
40class ip4_addr {
41public:
58 [[nodiscard]] static std::optional<ip4_addr> parse(std::string_view s) noexcept;
59
63 constexpr ip4_addr() noexcept = default;
64
72
82 : _addr(
83 static_cast<uint32_t>(a) | (static_cast<uint32_t>(b) << 8) | (static_cast<uint32_t>(c) << 16) |
84 (static_cast<uint32_t>(d) << 24)
85 ) {}
86
92 [[nodiscard]] constexpr uint32_t addr() const noexcept { return _addr; }
93
99 [[nodiscard]] constexpr bool is_any() const noexcept { return _addr == 0; }
100
104 [[nodiscard]] constexpr bool operator==(const ip4_addr&) const noexcept = default;
105
106private:
107 uint32_t _addr = 0;
108};
109
121class ip6_addr {
122public:
141 [[nodiscard]] static std::optional<ip6_addr> parse(std::string_view s) noexcept;
142
146 constexpr ip6_addr() noexcept = default;
147
155 : _addr(words)
156 , _zone(zone) {}
157
163 [[nodiscard]] constexpr const std::array<uint32_t, 4>& addr() const noexcept { return _addr; }
164
170 [[nodiscard]] constexpr uint8_t zone() const noexcept { return _zone; }
171
177 [[nodiscard]] constexpr bool is_any() const noexcept {
178 return _addr[0] == 0 && _addr[1] == 0 && _addr[2] == 0 && _addr[3] == 0;
179 }
180
184 [[nodiscard]] constexpr bool operator==(const ip6_addr&) const noexcept = default;
185
186private:
187 std::array<uint32_t, 4> _addr = {};
188 uint8_t _zone = 0;
189};
190
197struct ip4_info {
205 [[nodiscard]] constexpr bool operator==(const ip4_info&) const noexcept = default;
206};
207
214struct ip6_info {
216};
217
218} // namespace idfxx::net
219
220// =============================================================================
221// String conversions (in idfxx namespace)
222// =============================================================================
223
224namespace idfxx {
225
233[[nodiscard]] std::string to_string(net::ip4_addr addr);
234
244[[nodiscard]] std::string to_string(net::ip6_addr addr);
245
253[[nodiscard]] std::string to_string(net::ip4_info info);
254
255} // namespace idfxx
256
257#include "sdkconfig.h"
258#ifdef CONFIG_IDFXX_STD_FORMAT
260#include <algorithm>
261#include <format>
262namespace std {
263
264template<>
265struct formatter<idfxx::net::ip4_addr> {
266 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
267
268 template<typename FormatContext>
269 auto format(idfxx::net::ip4_addr addr, FormatContext& ctx) const {
270 auto s = idfxx::to_string(addr);
271 return std::copy(s.begin(), s.end(), ctx.out());
272 }
273};
274
275template<>
276struct formatter<idfxx::net::ip6_addr> {
277 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
278
279 template<typename FormatContext>
280 auto format(idfxx::net::ip6_addr addr, FormatContext& ctx) const {
281 auto s = idfxx::to_string(addr);
282 return std::copy(s.begin(), s.end(), ctx.out());
283 }
284};
285
286template<>
287struct formatter<idfxx::net::ip4_info> {
288 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
289
290 template<typename FormatContext>
291 auto format(idfxx::net::ip4_info info, FormatContext& ctx) const {
292 auto s = idfxx::to_string(info);
293 return std::copy(s.begin(), s.end(), ctx.out());
294 }
295};
296
297} // namespace std
299#endif // CONFIG_IDFXX_STD_FORMAT
300
// end of idfxx_core_net
IPv4 address value type.
Definition net.hpp:40
constexpr ip4_addr() noexcept=default
Constructs a zero-initialized IPv4 address (0.0.0.0).
constexpr bool is_any() const noexcept
Tests whether this is a zero (unset) address.
Definition net.hpp:99
constexpr uint32_t addr() const noexcept
Returns the raw address value in network byte order.
Definition net.hpp:92
constexpr bool operator==(const ip4_addr &) const noexcept=default
Compares two IPv4 addresses for equality.
constexpr ip4_addr(uint8_t a, uint8_t b, uint8_t c, uint8_t d) noexcept
Constructs an IPv4 address from individual octets.
Definition net.hpp:81
static std::optional< ip4_addr > parse(std::string_view s) noexcept
Parses an IPv4 address from dotted-decimal notation.
IPv6 address value type.
Definition net.hpp:121
constexpr const std::array< uint32_t, 4 > & addr() const noexcept
Returns the four 32-bit words of the address.
Definition net.hpp:163
constexpr bool is_any() const noexcept
Tests whether this is a zero (unset) address.
Definition net.hpp:177
constexpr bool operator==(const ip6_addr &) const noexcept=default
Compares two IPv6 addresses for equality (including zone).
constexpr uint8_t zone() const noexcept
Returns the zone ID.
Definition net.hpp:170
static std::optional< ip6_addr > parse(std::string_view s) noexcept
Parses an IPv6 address from its text representation.
constexpr ip6_addr() noexcept=default
Constructs a zero-initialized IPv6 address (::).
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:197
ip4_addr gateway
Definition net.hpp:200
constexpr bool operator==(const ip4_info &) const noexcept=default
Compares two ip4_info structs for equality.
ip4_addr netmask
Definition net.hpp:199
IPv6 network interface information.
Definition net.hpp:214