ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
forwarded_trust.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 forwarded_trust.h
6 * @brief Trusted-reverse-proxy resolution of a forwarded client address (PC_ENABLE_FORWARDED_TRUST).
7 *
8 * A `Forwarded` (RFC 7239) / `X-Forwarded-For` header is CLIENT-SPOOFABLE, so the client address it
9 * carries may only be believed when the connection's real TCP peer is a reverse proxy the operator
10 * trusts. This keeps a fixed BSS table of trusted-upstream CIDRs and resolves the effective client
11 * address for the abuse-prevention layer: when the TCP peer matches a trusted CIDR and the forwarded
12 * token is a valid, specified address, the forwarded client is used; otherwise the real TCP peer is
13 * used. Fail-safe by construction - an empty table trusts no header, and a malformed / obfuscated /
14 * unspecified token falls back to the TCP peer, so a direct (untrusted) client cannot spoof its way
15 * out of, or another peer into, the auth lockout. Pure (no sockets, no heap), host-tested.
16 *
17 * The table is a single owned instance reached only through this API (mirrors the source-IP allowlist
18 * and the auth-lockout table). Register upstreams with pc_forwarded_trust_add_cidr("10.0.0.0/8").
19 */
20
21#ifndef PROTOCORE_FORWARDED_TRUST_H
22#define PROTOCORE_FORWARDED_TRUST_H
23
24#include "protocore_config.h"
25
26#if PC_ENABLE_FORWARDED_TRUST
27
29#include <stdint.h>
30
31/** @brief Empty the trusted-upstream table (trust no forwarded header). */
32void pc_forwarded_trust_reset(void);
33
34/**
35 * @brief Add a trusted-upstream network.
36 * @return true if added; false on a null / malformed-family @p network, an out-of-range @p prefix_len,
37 * or a full table (PC_TRUSTED_PROXY_MAX).
38 */
39bool pc_forwarded_trust_add(const pc_ip *network, uint8_t prefix_len);
40
41/**
42 * @brief Add a trusted-upstream network from a CIDR string ("10.0.0.0/8" / "2001:db8::/32"; a bare
43 * address is taken as a host route).
44 * @return true if parsed and added; false otherwise.
45 */
46bool pc_forwarded_trust_add_cidr(const char *cidr);
47
48/** @brief True if @p peer falls inside any trusted-upstream CIDR (always false when the table is empty). */
49bool pc_forwarded_trust_contains(const pc_ip *peer);
50
51/**
52 * @brief Resolve the effective client address behind a possibly-trusted proxy.
53 *
54 * @p out is set to the forwarded client ONLY when @p peer is a trusted upstream AND @p fwd_ip_str is a
55 * valid, specified address; otherwise @p out is set to @p peer. @p out is always written when non-null.
56 *
57 * @param peer the connection's real TCP source address.
58 * @param fwd_ip_str the recovered forwarded client address text, or nullptr/"" if none was present.
59 * @param out receives the effective client address.
60 * @return true if the forwarded client was honored; false if the real TCP peer was kept.
61 */
62bool pc_forwarded_effective_ip(const pc_ip *peer, const char *fwd_ip_str, pc_ip *out);
63
64#endif // PC_ENABLE_FORWARDED_TRUST
65#endif // PROTOCORE_FORWARDED_TRUST_H
Layer 3 (Network) - a family-tagged IP address (IPv4 or IPv6) with RFC-faithful text parsing,...
User-facing configuration for ProtoCore.
A v4 or v6 address in network (big-endian) byte order.
Definition ip.h:52