ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
dns_resolver.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file dns_resolver.h
6 * @brief DNS resolver with answer verification (PC_ENABLE_DNS_RESOLVER).
7 *
8 * Resolves a hostname to an IPv4 address via lwIP (dns_gethostbyname, marshalled
9 * to tcpip_thread like the http_client), and classifies / verifies the answer:
10 * a remote name resolving to 0.0.0.0, the broadcast address, loopback, or a
11 * multicast address is rejected as a spoof / DNS-rebinding indicator. The
12 * classifier + verifier are pure and host-tested; the resolve is ESP32-only and
13 * blocking (call it off the request hot path).
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef PROTOCORE_DNS_RESOLVER_H
20#define PROTOCORE_DNS_RESOLVER_H
21
22#include "protocore_config.h"
23#include <stddef.h>
24#include <stdint.h>
25
26#if PC_NEED_DNS_RESOLVER
27
28/** @brief IPv4 address category (RFC special-purpose ranges). */
29enum class pc_ip_class : uint8_t
30{
31 PC_IP_UNSPECIFIED = 0, ///< 0.0.0.0
32 PC_IP_LOOPBACK, ///< 127.0.0.0/8
33 PC_IP_PRIVATE, ///< 10/8, 172.16/12, 192.168/16
34 PC_IP_LINKLOCAL, ///< 169.254.0.0/16
35 PC_IP_MULTICAST, ///< 224.0.0.0/4
36 PC_IP_BROADCAST, ///< 255.255.255.255
37 PC_IP_PUBLIC, ///< globally-routable unicast
38};
39
40// ---------------------------------------------------------------------------
41// Host-testable core
42// ---------------------------------------------------------------------------
43
44/** @brief Classify a host-order IPv4 word (e.g. (10u << 24) | (0u << 16) | (0u << 8) | 1u). */
45pc_ip_class pc_dns_resolver_classify(uint32_t ip);
46
47/**
48 * @brief Is @p ip a plausible A-record answer for a remote host?
49 *
50 * Rejects unspecified / broadcast / loopback / multicast (spoof / rebinding
51 * indicators); accepts private / link-local / public. Host order.
52 */
53bool pc_dns_resolver_verify(uint32_t ip);
54
55// ---------------------------------------------------------------------------
56// Resolve (ESP32; returns false on host)
57// ---------------------------------------------------------------------------
58
59/**
60 * @brief Resolve @p host to an IPv4 address (host order) into @p out_ip.
61 *
62 * Accepts a dotted-quad directly; otherwise queries DNS with a
63 * PC_DNS_TIMEOUT_MS deadline. Blocking. @return true on success.
64 */
65bool pc_dns_resolver_resolve(const char *host, uint32_t *out_ip);
66
67/**
68 * @brief Resolve @p host and require the answer to pass pc_dns_resolver_verify().
69 * @return true only if it resolved AND the address is a plausible answer.
70 */
71bool pc_dns_resolver_resolve_verified(const char *host, uint32_t *out_ip);
72
73#if !defined(ARDUINO)
74/** @brief Host test hook: make pc_dns_resolver_resolve() return @p ip (host order) when @p ok, else fail. */
75void pc_dns_resolver_test_set_resolve(bool ok, uint32_t ip);
76#endif
77
78#endif // PC_NEED_DNS_RESOLVER
79#endif // PROTOCORE_DNS_RESOLVER_H
User-facing configuration for ProtoCore.