ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
relay.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 relay.h
6 * @brief TCP relay / DNAT port forwarding (PROTOCORE_ENABLE_RELAY) - a bidirectional byte pump.
7 *
8 * Publishes an internal `host:port` through the server: an inbound (accepted) connection is relayed
9 * to an origin (an outbound connection to the internal service), moving bytes in both directions.
10 * The engine is pure - it touches the two sockets only through send/recv seams - so it is
11 * host-testable and rides `protocore_client` on the device. The app drives it: each poll tick (or whenever
12 * a socket is readable/writable) it calls protocore_relay_step() until the relay reports DONE, then closes
13 * both sockets.
14 *
15 * Correctness details:
16 * - **Backpressure**: a `send` seam may accept fewer bytes than offered. The un-accepted bytes are
17 * carried in a per-direction buffer and retried on the next step before more are read.
18 * - **Independent half-close**: each direction finishes when its source signals EOF and its buffer
19 * drains. When a direction finishes, the opposite peer's optional `shutdown` seam is called once
20 * (propagating the half-close so the origin sees the client's FIN). The relay is DONE only when
21 * both directions finished.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_RELAY_H
28#define PROTOCORE_RELAY_H
29
30#include "protocore_config.h" // the entry point: protocore_types.h for the widths
31
32#if PROTOCORE_ENABLE_RELAY
33
34EMBED_BEGIN_DECLS
35
36// This module holds nothing between calls, so it carves no borrow and states none. An entry
37// takes one all the same, and never reads it, so every namespace in the tree is invoked the
38// same way.
39
40/** @brief protocore_relay_step() outcome. */
41EMBED_ENUM_BYTE(protocore_relay_status,
42 PROTOCORE_RELAY_ERROR = -1, ///< a send/recv seam reported an error; the caller should close both sides
43 PROTOCORE_RELAY_RUNNING = 0, ///< still relaying (keep stepping)
44 PROTOCORE_RELAY_DONE = 1, ///< both directions finished (EOF + drained); the caller closes both sides
45);
46
47/**
48 * @brief Read up to @p cap bytes from the peer into @p buf.
49 * @return bytes read (> 0), 0 if none are available now, or < 0 once the peer closed its send
50 * side (EOF) or errored.
51 */
52typedef int (*protocore_relay_recv_fn)(void *ctx, uint8_t *buf, size_t cap);
53
54/**
55 * @brief Write up to @p len bytes to the peer.
56 * @return bytes accepted (> 0, may be < @p len under backpressure), 0 if none can be accepted right
57 * now, or < 0 on error.
58 */
59typedef int (*protocore_relay_send_fn)(void *ctx, const uint8_t *buf, size_t len);
60
61/** @brief Optional: signal the peer that no more data will be sent to it (a write-side half-close). */
62typedef void (*protocore_relay_shutdown_fn)(void *ctx);
63
64/** @brief One end of a relay (a socket, behind seams). @p shutdown may be null. */
65typedef struct
66{
67 protocore_relay_recv_fn recv;
68 protocore_relay_send_fn send;
69 protocore_relay_shutdown_fn shutdown;
70 void *ctx;
71} protocore_relay_end;
72
73/** @brief A relay between two ends. Owns the per-direction carry buffers; zero heap. */
74typedef struct
75{
76 protocore_relay_end a;
77 protocore_relay_end b;
78 uint8_t buf_a2b[PROTOCORE_RELAY_BUF];
79 uint8_t buf_b2a[PROTOCORE_RELAY_BUF];
80 uint16_t a2b_len; ///< bytes read from a pending send to b
81 uint16_t a2b_off; ///< how many of those already sent
82 uint16_t b2a_len;
83 uint16_t b2a_off;
84 embed_bool a_eof; ///< the recv side of a has hit EOF
85 embed_bool b_eof; ///< the recv side of b has hit EOF
86 embed_bool a2b_done; ///< the a->b direction finished (EOF + drained)
87 embed_bool b2a_done; ///< the b->a direction finished (EOF + drained)
88 embed_bool a_shut_sent; ///< the shutdown seam of a has been called
89 embed_bool b_shut_sent; ///< the shutdown seam of b has been called
90 uint32_t bytes_a2b; ///< bytes relayed a->b (observability)
91 uint32_t bytes_b2a; ///< bytes relayed b->a (observability)
92} protocore_relay;
93
94/** @brief What init takes: r, client, origin. */
95typedef struct
96{
97 protocore_relay *r;
98 const protocore_relay_end *client;
99 const protocore_relay_end *origin;
100} RelayInitArgs;
101
102/** @brief What step takes: r. */
103typedef struct
104{
105 protocore_relay *r;
106} RelayStepArgs;
107
108/** @brief What note_eof takes: r, origin. */
109typedef struct
110{
111 protocore_relay *r;
112 embed_bool origin; ///< false for the client (inbound) side, true for the origin (outbound) side
113} RelayNoteEofArgs;
114
115/**
116 * @brief TCP relay / DNAT port forwarding (PROTOCORE_ENABLE_RELAY) - a bidirectional byte pump. Publishes an internal
117 * ...
118 *
119 * A caller sets the members a call takes, invokes it through ::Relay with the bytes it runs
120 * out of, and reads the outcome off the same handle.
121 *
122 * Relay.init_args.r = ...;
123 * Relay.init_args.client = ...;
124 * Relay.init_args.origin = ...;
125 * Relay.init(work);
126 *
127 * @var RelayNs::init_args what init takes: r, client, origin
128 * @var RelayNs::step_args what step takes: r
129 * @var RelayNs::note_eof_args what note_eof takes: r, origin
130 * @var RelayNs::ok a call's true/false outcome
131 * @var RelayNs::status a ::protocore_relay_status. Call repeatedly (per poll tick) until ...
132 * @var RelayNs::init initialize a relay between client (the inbound connection) and ...
133 * @var RelayNs::step do one non-blocking pass: flush any pending bytes and read more, in ...
134 * @var RelayNs::note_eof signal that a peer's send side closed, when the transport ...
135 *
136 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
137 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
138 * a caller drives every namespace the same way.
139 */
140typedef struct
141{
142 RelayInitArgs init_args;
143 RelayStepArgs step_args;
144 RelayNoteEofArgs note_eof_args;
145 embed_bool ok;
146 protocore_relay_status status;
147} RelayVars;
148
149/** @brief The operands and the outcome. */
150extern RelayVars RelayV;
151
152/** @brief The entries. */
153typedef struct
154{
155 void (*const init)(const uint8_t *work);
156 void (*const step)(const uint8_t *work);
157 void (*const note_eof)(const uint8_t *work);
158} RelayNs;
159
160// What the table binds, defined once in the .c and taking one parameter each: everything
161// else an entry needs is an operand in RelayV or a region of the borrow at a fixed offset.
162void protocore_relay_init(const uint8_t *work);
163void protocore_relay_step(const uint8_t *work);
164void protocore_relay_note_eof(const uint8_t *work);
165
166// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
167// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
168// `Relay.init(work)` resolves to a named function and becomes a DIRECT call. An extern table
169// leaves the call indirect and the symbol live at every level, -O2 -flto included.
170static const RelayNs Relay __attribute__((unused)) = {
171 .init = protocore_relay_init,
172 .step = protocore_relay_step,
173 .note_eof = protocore_relay_note_eof,
174};
175
176EMBED_END_DECLS
177
178#endif // PROTOCORE_ENABLE_RELAY
179
180#endif // PROTOCORE_RELAY_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
#define PROTOCORE_RELAY_BUF
Per-direction relay buffer size (bytes) for server/net/relay (PROTOCORE_ENABLE_RELAY).