ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ip.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ip.h
6 * @brief Layer 3 (Network) - a family-tagged IP address (IPv4 or IPv6) with RFC-faithful
7 * text parsing, canonical formatting, and scope classification.
8 *
9 * One representation for both address families so the rest of the stack can carry a peer
10 * address without caring whether it is v4 or v6. The address bytes are stored in network
11 * (big-endian, left-to-right) order: a v4 address uses bytes[0..3], a v6 address bytes[0..15].
12 *
13 * Pure and host-testable - no lwIP, no Arduino, no heap, no stdlib parsing. The parser
14 * implements RFC 4291 §2.2 text forms (dotted-quad v4; v6 with `::` zero-compression and the
15 * embedded-v4 `::ffff:a.b.c.d` tail); the formatter emits the RFC 5952 canonical form
16 * (lower-case, no leading zeros, the longest zero run compressed to `::`, v4-mapped shown as
17 * dotted). ESP32 dual-stack bring-up (enabling IPv6 on the netif) lives in the physical layer
18 * behind PROTOCORE_ENABLE_IPV6. The TCP/UDP listeners already bind IPADDR_TYPE_ANY, so the server
19 * accepts v6 connections the moment the interface has a v6 address.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef PROTOCORE_IP_H
26#define PROTOCORE_IP_H
27
28#include "protocore_config.h" // the entry point; it sets the widths and reaches protocore_types.h for embed_bool
29
30EMBED_BEGIN_DECLS
31
32/** @brief Address family tag. */
33EMBED_ENUM_BYTE(protocore_ip_family,
34 PROTOCORE_IP_NONE = 0, ///< empty / unparsed
35 PROTOCORE_IP_V4 = 4, ///< IPv4 (bytes[0..3])
36 PROTOCORE_IP_V6 = 6, ///< IPv6 (bytes[0..15])
37);
38static_assert(sizeof(protocore_ip_family) == 1, "protocore_ip_family must stay one byte (EMBED_ENUM_BYTE); "
39 "protocore_ip is embedded wherever an address is stored");
40
41/** @brief Address scope, in rough order of reachability (used for allow/deny policy + logging). */
42EMBED_ENUM_BYTE(protocore_ip_scope,
43 PROTOCORE_IP_SCOPE_UNSPECIFIED = 0, ///< 0.0.0.0 / ::
44 PROTOCORE_IP_SCOPE_LOOPBACK, ///< 127.0.0.0/8 / ::1
45 PROTOCORE_IP_SCOPE_LINK_LOCAL, ///< 169.254.0.0/16 / fe80::/10
46 PROTOCORE_IP_SCOPE_PRIVATE, ///< RFC1918 (10/8, 172.16/12, 192.168/16) / ULA fc00::/7
47 PROTOCORE_IP_SCOPE_MULTICAST, ///< 224.0.0.0/4 / ff00::/8
48 PROTOCORE_IP_SCOPE_GLOBAL, ///< globally routable unicast
49);
50static_assert(sizeof(protocore_ip_scope) == 1, "protocore_ip_scope must stay one byte (EMBED_ENUM_BYTE)");
51
52/** @brief A v4 or v6 address in network (big-endian) byte order. */
53typedef struct protocore_ip
54{
55 protocore_ip_family family; ///< address family tag
56 uint8_t bytes[16]; ///< network order; v4 uses the first 4
58
59/** @brief Longest text an ::IpNs::format can produce, including the NUL (RFC 5952 v4-mapped). */
60#define PROTOCORE_IP_STR_MAX 46
61
62/**
63 * @brief Parse an IPv4 or IPv6 textual address (RFC 4291 §2.2) into @p out.
64 * @return true on success (@p out->family set to PROTOCORE_IP_V4/V6), false if @p s is malformed.
65 */
66
67/**
68 * @brief Format @p ip into @p out as its RFC 5952 canonical text.
69 * @return the length written (excluding the NUL), or 0 if @p ip is empty or @p cap is too small
70 * (need up to ::PROTOCORE_IP_STR_MAX).
71 */
72
73/** @brief Classify @p ip into a ::protocore_ip_scope. */
74
75/** @brief True if @p a and @p b are the same family and address. */
76
77/** @brief True if @p ip is an IPv4-mapped IPv6 address (::ffff:a.b.c.d, RFC 4291 §2.5.5.2). */
79
80/**
81 * @brief Build a v4 ::protocore_ip from four octets (a.b.c.d).
82 */
83protocore_ip protocore_ip_from_v4_octets(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
84
85/**
86 * @brief Build a v6 ::protocore_ip from 16 address bytes in network (big-endian) order.
87 */
89
90/**
91 * @brief The v4 address as a big-endian (network-order) uint32 (a<<24 | b<<16 | c<<8 | d).
92 * @return 0 if @p ip is not a v4 (or v4-mapped) address.
93 */
95
96/** @brief True if @p ip is empty (PROTOCORE_IP_NONE) or the all-zero unspecified address (0.0.0.0 / ::). */
97
98/**
99 * @brief CIDR containment: is @p addr inside the @p net / @p prefix_len block?
100 *
101 * The two must be the same family. @p prefix_len is 0..32 for v4, 0..128 for v6. The top
102 * @p prefix_len bits of the address bytes must match @p net (a prefix of 0 matches everything).
103 * This is the standard v4/v6 allowlist match.
104 * @return true if @p addr is covered; false on a family mismatch or an out-of-range prefix.
105 */
106
107/** @brief The address, or pair of addresses, one call acts on. */
108typedef struct
109{
110 const char *text; ///< the textual address a parse reads
111 const protocore_ip *ip; ///< the address a call reads
112 const protocore_ip *b; ///< the second address a compare or a prefix test reads
113 protocore_ip *out; ///< where a parse lands its result
114 char *buf; ///< where a format writes
115 size_t cap; ///< how much room that has
116 uint8_t prefix_len; ///< the prefix length a match tests to
117} IpArgs;
118
119/**
120 * @brief An IP address, as a value.
121 *
122 * @var IpNs::parse read a textual address, v4 or v6, into @c out
123 * @var IpNs::format write @c ip as text into @c out, returning the length
124 * @var IpNs::classify what scope the address names
125 * @var IpNs::equal whether two addresses are the same address
126 * @var IpNs::is_unspecified whether the address names nothing
127 * @var IpNs::prefix_match whether @c addr falls inside @c net at @c prefix_len bits
128 *
129 * No storage member: every operation reads its operands and holds nothing. Reached as @ref Ip,
130 * or through network.ip, which points at it: set @c args, invoke the call with @c Ip.internal,
131 * then read the result member.
132 */
133typedef struct
134{
136 embed_bool ok;
137 size_t n;
138 protocore_ip_scope scope;
139} IpVars;
140
141/** @brief The operands and the outcome. */
142extern IpVars IpV;
143
144/** @brief The entries. */
145typedef struct
146{
147 void (*const parse)(const uint8_t *work);
148 void (*const format)(const uint8_t *work);
149 void (*const classify)(const uint8_t *work);
150 void (*const equal)(const uint8_t *work);
151 void (*const is_unspecified)(const uint8_t *work);
152 void (*const prefix_match)(const uint8_t *work);
153} IpNs;
154
155// What the table binds, defined once in the .c and taking one parameter each: everything
156// else an entry needs is an operand in IpV or a region of the borrow at a fixed offset.
157void protocore_ip_parse(const uint8_t *work);
158void protocore_ip_format(const uint8_t *work);
159void protocore_ip_classify(const uint8_t *work);
160void protocore_ip_equal(const uint8_t *work);
161void protocore_ip_is_unspecified(const uint8_t *work);
162void protocore_ip_prefix_match(const uint8_t *work);
163
164// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
165// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
166// `Ip.parse(work)` resolves to a named function and becomes a DIRECT call. An extern table
167// leaves the call indirect and the symbol live at every level, -O2 -flto included.
168static const IpNs Ip __attribute__((unused)) = {
169 .parse = protocore_ip_parse,
170 .format = protocore_ip_format,
171 .classify = protocore_ip_classify,
172 .equal = protocore_ip_equal,
173 .is_unspecified = protocore_ip_is_unspecified,
174 .prefix_match = protocore_ip_prefix_match,
175};
176
177EMBED_END_DECLS
178
179#endif // PROTOCORE_IP_H
uint32_t protocore_ip_to_v4_be(const protocore_ip *ip)
The v4 address as a big-endian (network-order) uint32 (a<<24 | b<<16 | c<<8 | d).
EMBED_BEGIN_DECLS EMBED_ENUM_BYTE(protocore_ip_family, PROTOCORE_IP_NONE=0, PROTOCORE_IP_V4=4, PROTOCORE_IP_V6=6,)
Address family tag.
protocore_ip protocore_ip_from_v4_octets(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Build a v4 protocore_ip from four octets (a.b.c.d).
void protocore_ip_classify(const uint8_t *work)
void protocore_ip_equal(const uint8_t *work)
protocore_ip protocore_ip_from_v6_bytes(const uint8_t bytes[16])
Build a v6 protocore_ip from 16 address bytes in network (big-endian) order.
void protocore_ip_is_unspecified(const uint8_t *work)
void protocore_ip_format(const uint8_t *work)
IpVars IpV
The operands and the outcome.
void protocore_ip_parse(const uint8_t *work)
void protocore_ip_prefix_match(const uint8_t *work)
embed_bool protocore_ip_is_v4_mapped(const protocore_ip *ip)
Parse an IPv4 or IPv6 textual address (RFC 4291 §2.2) into out.
True if ip is empty (PROTOCORE_IP_NONE) or the all-zero unspecified address (0.0.0....
Definition ip.h:109
char * buf
where a format writes
Definition ip.h:114
const char * text
the textual address a parse reads
Definition ip.h:110
uint8_t prefix_len
the prefix length a match tests to
Definition ip.h:116
size_t cap
how much room that has
Definition ip.h:115
const protocore_ip * b
the second address a compare or a prefix test reads
Definition ip.h:112
protocore_ip * out
where a parse lands its result
Definition ip.h:113
const protocore_ip * ip
the address a call reads
Definition ip.h:111
The entries.
Definition ip.h:146
void(*const equal)(const uint8_t *work)
Definition ip.h:150
void(*const parse)(const uint8_t *work)
Definition ip.h:147
void(*const is_unspecified)(const uint8_t *work)
Definition ip.h:151
void(*const classify)(const uint8_t *work)
Definition ip.h:149
void(*const format)(const uint8_t *work)
Definition ip.h:148
void(*const prefix_match)(const uint8_t *work)
Definition ip.h:152
Definition ip.h:134
protocore_ip_scope scope
Definition ip.h:138
embed_bool ok
Definition ip.h:136
IpArgs args
Definition ip.h:135
size_t n
Definition ip.h:137
A v4 or v6 address in network (big-endian) byte order.
Definition ip.h:54
uint8_t bytes[16]
network order; v4 uses the first 4
Definition ip.h:56
protocore_ip_family family
address family tag
Definition ip.h:55