idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
resolver.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
28#include <idfxx/net/endpoint>
29#include <idfxx/net/error>
30
31#include <memory>
32#include <optional>
33#include <string_view>
34#include <type_traits>
35#include <utility>
36#include <vector>
37
38namespace idfxx::net {
39
46 std::optional<address_family> family = std::nullopt;
47
49 bool numeric_host = false;
50
52 bool numeric_port = false;
53
55 bool passive = false;
56
58 std::optional<ip_protocol> protocol = std::nullopt;
59};
60
62namespace detail {
63
64// Type-erased iteration over resolution results. `sink` is invoked for each
65// resolved endpoint; returning false stops iteration early. The sink stopping
66// early is not an error — the returned result reflects only the resolution
67// itself. Backing the templated `resolve_each` keeps the lwIP-facing code
68// out of the public header while preserving zero-allocation iteration.
70 std::string_view host,
71 port_number port,
73 bool (*sink)(void* ctx, const endpoint&),
74 void* ctx
75);
77 std::string_view host,
78 std::string_view service,
80 bool (*sink)(void* ctx, const endpoint&),
81 void* ctx
82);
83
84template<typename F>
85bool invoke_resolve_sink(void* ctx, const endpoint& ep) {
86 F& fn = *static_cast<F*>(ctx);
87 if constexpr (std::is_void_v<std::invoke_result_t<F&, const endpoint&>>) {
88 fn(ep);
89 return true;
90 } else {
91 return fn(ep);
92 }
93}
94
95} // namespace detail
97
98#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
111[[nodiscard]] endpoint resolve_one(std::string_view host, port_number port, const resolver_options& opts = {});
112#endif
113
123try_resolve_one(std::string_view host, port_number port, const resolver_options& opts = {});
124
125#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
138[[nodiscard]] endpoint resolve_one(std::string_view host, std::string_view service, const resolver_options& opts = {});
139#endif
140
150try_resolve_one(std::string_view host, std::string_view service, const resolver_options& opts = {});
151
152#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
175template<typename F>
176void resolve_each(std::string_view host, port_number port, F&& f, const resolver_options& opts = {}) {
177 idfxx::unwrap(try_resolve_each(host, port, std::forward<F>(f), opts));
178}
179#endif
180
195template<typename F>
197try_resolve_each(std::string_view host, port_number port, F&& f, const resolver_options& opts = {}) {
198 using fn_type = std::remove_reference_t<F>;
199 return detail::resolve_for_each(host, port, opts, &detail::invoke_resolve_sink<fn_type>, std::addressof(f));
200}
201
202#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
219template<typename F>
220void resolve_each(std::string_view host, std::string_view service, F&& f, const resolver_options& opts = {}) {
221 idfxx::unwrap(try_resolve_each(host, service, std::forward<F>(f), opts));
222}
223#endif
224
239template<typename F>
241try_resolve_each(std::string_view host, std::string_view service, F&& f, const resolver_options& opts = {}) {
242 using fn_type = std::remove_reference_t<F>;
243 return detail::resolve_for_each(host, service, opts, &detail::invoke_resolve_sink<fn_type>, std::addressof(f));
244}
245
246#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
263[[nodiscard]] std::vector<endpoint> resolve(std::string_view host, port_number port, const resolver_options& opts = {});
264#endif
265
279try_resolve(std::string_view host, port_number port, const resolver_options& opts = {});
280
281#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
298[[nodiscard]] std::vector<endpoint>
299resolve(std::string_view host, std::string_view service, const resolver_options& opts = {});
300#endif
301
315try_resolve(std::string_view host, std::string_view service, const resolver_options& opts = {});
316
317} // namespace idfxx::net
318
// end of idfxx_net_resolver
Address/port pair identifying a transport endpoint.
Definition endpoint.hpp:129
std::vector< endpoint > resolve(std::string_view host, port_number port, const resolver_options &opts={})
Resolves a host and numeric port and collects every endpoint.
result< void > try_resolve_each(std::string_view host, port_number port, F &&f, const resolver_options &opts={})
Visits each resolved endpoint for a host and numeric port without allocating.
Definition resolver.hpp:197
result< std::vector< endpoint > > try_resolve(std::string_view host, port_number port, const resolver_options &opts={})
Resolves a host and numeric port and collects every endpoint.
uint16_t port_number
Port number type for transport endpoints.
Definition endpoint.hpp:60
endpoint resolve_one(std::string_view host, port_number port, const resolver_options &opts={})
Resolves a host and numeric port and returns the first matching endpoint.
void resolve_each(std::string_view host, port_number port, F &&f, const resolver_options &opts={})
Visits each resolved endpoint for a host and numeric port without allocating.
Definition resolver.hpp:176
result< endpoint > try_resolve_one(std::string_view host, port_number port, const resolver_options &opts={})
Resolves a host and numeric port and returns the first matching endpoint.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Resolver hints used by the resolution functions.
Definition resolver.hpp:44
bool passive
Request results suitable for bind() rather than connect().
Definition resolver.hpp:55
bool numeric_host
Require the host to be a numeric address — skip the DNS lookup.
Definition resolver.hpp:49
std::optional< address_family > family
Address family preference.
Definition resolver.hpp:46
std::optional< ip_protocol > protocol
Restrict results to a particular transport protocol.
Definition resolver.hpp:58
bool numeric_port
Require the service to be a numeric port — skip the service-name lookup.
Definition resolver.hpp:52