ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
stack_net.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 stack_net.h
6 * @brief The stack surface of the platform, over idemIP: the protocore_net_* names that the transport calls.
7 *
8 * With PROTOCORE_ENABLE_IDEMIP, the platform header takes these declarations in place of those of lwIP. The
9 * names, the types and the callbacks are those of the raw API of lwIP, so that the transport above runs as it
10 * does over lwIP. The stack module of the network layer answers each call: stack.c runs the units of idemIP,
11 * stack_tcp.c holds each TCP connection, and stack_net.c maps the raw API onto both.
12 *
13 * Each call runs under the lock of the stack. protocore_net_call_marshal takes the lock and runs its function
14 * in the thread of the caller. A callback runs in the pass of the stack, which holds the lock already.
15 *
16 * The platform header reaches this one before protocore_config.h defines anything. It thus reads embedded_types
17 * alone.
18 *
19 * @author Douglas Quigg (dstroy0)
20 * @date 2026
21 */
22
23#ifndef PROTOCORE_STACK_NET_H
24#define PROTOCORE_STACK_NET_H
25
26#include <stddef.h>
27#include <stdint.h>
28
29#include "embed_compiler_directives.h" // EMBED_BEGIN_DECLS
30#include "embed_types.h" // embed_bool and the widths
31
32EMBED_BEGIN_DECLS
33
34// --- results -------------------------------------------------------------------------
35
36/** @brief The result of a call of the stack, as lwIP numbers it. */
37typedef int8_t protocore_net_err;
38#define PROTOCORE_NET_OK 0
39#define PROTOCORE_NET_ERR_MEM (-1)
40#define PROTOCORE_NET_ERR_BUF (-2)
41#define PROTOCORE_NET_ERR_VAL (-6)
42#define PROTOCORE_NET_ERR_USE (-8)
43#define PROTOCORE_NET_ERR_CONN (-11)
44#define PROTOCORE_NET_ERR_ABRT (-13)
45#define PROTOCORE_NET_ERR_RST (-14)
46#define PROTOCORE_NET_ERR_CLSD (-15)
47#define PROTOCORE_NET_ERR_ARG (-16)
48#define PROTOCORE_NET_ERR_INPROGRESS (-5)
49
50// --- addresses -----------------------------------------------------------------------
51
52#define PROTOCORE_NET_TYPE_ANY 0
53#define PROTOCORE_NET_TYPE_V4 4
54#define PROTOCORE_NET_TYPE_V6 6
55#define PROTOCORE_NET_HAS_IGMP 1
56#define PROTOCORE_NET_HAS_IPV6 0
57
58/**
59 * @brief One address of the stack.
60 *
61 * @var protocore_net_ip::type PROTOCORE_NET_TYPE_V4, or PROTOCORE_NET_TYPE_ANY for the any address
62 * @var protocore_net_ip::bytes the address in network order, IPv4 in the first four octets
63 */
64typedef struct
65{
66 uint8_t type;
67 uint8_t bytes[16];
69
70/** @brief The any address, which a bind takes for each local address. */
72#define PROTOCORE_NET_ADDR_ANY (&protocore_stack_net_any)
73#define PROTOCORE_NET_ADDR_ANY4 (&protocore_stack_net_any)
74#define PROTOCORE_NET_ADDR_ANY4_P (&protocore_stack_net_any)
75
76#define protocore_net_ip_is_v4(a) ((a) != NULL && (a)->type == PROTOCORE_NET_TYPE_V4)
77#define protocore_net_ip_is_v6(a) ((a) != NULL && (a)->type == PROTOCORE_NET_TYPE_V6)
78#define protocore_net_ip_as_v4(a) (a)
79#define protocore_net_ip_as_v6(a) (a)
80#define protocore_net_ip6_bytes(a) ((const uint8_t *)(a)->bytes)
81#define protocore_net_ip6_wbytes(a) ((uint8_t *)(a)->bytes)
82#define protocore_net_ip6_mark(a) ((a)->type = PROTOCORE_NET_TYPE_V6)
83
84/** @brief The IPv4 address of @p a as the word that lwIP keeps: the four octets in network order in memory. */
86/** @brief Set @p a to the IPv4 address b0.b1.b2.b3. */
87void protocore_net_ip4_set(protocore_net_ip *a, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3);
88/** @brief 1 when @p a is an IPv4 host group, 224.0.0.0/4. */
90/** @brief Parse the dotted quad @p s into @p out. 1 on success, and 0 otherwise. */
92/** @brief The dotted quad of @p a in @p buf, of @p cap octets. */
93char *protocore_net_ip_print(const protocore_net_ip *a, char *buf, int cap);
94
95// --- packet buffers --------------------------------------------------------------------
96
97#define PROTOCORE_NET_PBUF_TRANSPORT 0
98#define PROTOCORE_NET_PBUF_RAM 0
99
100/**
101 * @brief One packet buffer, a chain of one link.
102 *
103 * @var protocore_pbuf::next the next link, NULL here
104 * @var protocore_pbuf::payload the octets
105 * @var protocore_pbuf::tot_len the octets of the chain
106 * @var protocore_pbuf::len the octets of this link
107 */
108typedef struct protocore_pbuf
109{
111 void *payload;
112 uint16_t tot_len;
113 uint16_t len;
115
116/** @brief A packet buffer of @p len octets from the pool, or NULL when the pool is spent. */
117protocore_pbuf *protocore_net_pbuf_alloc(int layer, uint16_t len, int type);
118/** @brief Give a packet buffer back to the pool. */
120/** @brief Copy at most @p len octets of @p p, from @p off, to @p dst. The octets copied. */
121uint16_t protocore_net_pbuf_copy(const protocore_pbuf *p, void *dst, uint16_t len, uint16_t off);
122
123// --- TCP -------------------------------------------------------------------------------
124
125#define PROTOCORE_NET_WRITE_COPY 0x01
126#define PROTOCORE_NET_OPT_REUSEADDR 0x04
127
136 uint16_t);
137
138/**
139 * @brief An active open that waits for the ARP row of its next hop.
140 *
141 * @var StackNetConnectWait::deadline_ms the millisecond that the wait ends at
142 * @var StackNetConnectWait::arp_sent_ms the millisecond that the last ARP request went out
143 * @var StackNetConnectWait::pending the wait runs
144 */
145typedef struct
146{
147 uint32_t deadline_ms;
148 uint32_t arp_sent_ms;
149 embed_bool pending;
151
152/**
153 * @brief One TCP control block of the stack.
154 *
155 * @var protocore_pcb::tos RFC 791 Type of Service of each segment
156 * @var protocore_pcb::ttl RFC 791 Time to Live of each segment
157 * @var protocore_pcb::state nonzero while the control block is open
158 * @var protocore_pcb::local_port the local port
159 * @var protocore_pcb::remote_port the port of the peer
160 * @var protocore_pcb::local_ip the local address
161 * @var protocore_pcb::remote_ip the address of the peer
162 * @var protocore_pcb::so_options the options that protocore_net_opt_set set
163 * @var protocore_pcb::snd_queuelen the segments of the connection that the peer did not acknowledge yet
164 * @var protocore_pcb::flags the flags of lwIP, which this stack does not read
165 * @var protocore_pcb::arg the argument of each callback
166 * @var protocore_pcb::on_recv the callback of text, and of the FIN with a NULL buffer
167 * @var protocore_pcb::on_sent the callback of octets that the peer acknowledged
168 * @var protocore_pcb::on_accept the callback of a connection that a listener took
169 * @var protocore_pcb::on_connect the callback of an active open that completed
170 * @var protocore_pcb::on_err the callback of a connection that a reset or a timeout ended
171 * @var protocore_pcb::connect_wait the active open that waits for the ARP row of its next hop
172 * @var protocore_pcb::entry the entry of the TCP of the stack, PROTOCORE_STACK_NET_NONE for none
173 * @var protocore_pcb::listen the control block listens
174 * @var protocore_pcb::in_use the control block is taken
175 */
199
200/** @brief The entry index that names none. */
201#define PROTOCORE_STACK_NET_NONE 0xFFu
202
207 protocore_net_connect_fn connected);
215protocore_net_err protocore_net_write(protocore_pcb *p, const void *data, uint16_t len, uint8_t flags);
217void protocore_net_recved(const protocore_pcb *p, uint16_t len);
220void protocore_net_rcv_wnd_update(const protocore_pcb *p, uint16_t len);
221void protocore_net_opt_set(void *p, uint32_t opt);
222
223// --- UDP and IGMP ----------------------------------------------------------------------
224
225/**
226 * @brief One UDP control block of the stack.
227 *
228 * @var protocore_udp_pcb::tos RFC 791 Type of Service of each datagram
229 * @var protocore_udp_pcb::local_port the bound port
230 * @var protocore_udp_pcb::arg the argument of the callback
231 * @var protocore_udp_pcb::on_recv the callback of each datagram
232 * @var protocore_udp_pcb::so_options the options that protocore_net_opt_set set
233 * @var protocore_udp_pcb::entry the entry of the UdpPcb unit of idemIP
234 * @var protocore_udp_pcb::opened the entry is open
235 * @var protocore_udp_pcb::in_use the control block is taken
236 */
238{
239 uint8_t tos;
240 uint16_t local_port;
241 void *arg;
243 uint32_t so_options;
244 uint16_t entry;
245 embed_bool opened;
246 embed_bool in_use;
247};
248
253 const protocore_net_ip *a, uint16_t port);
257
258// --- the context of the stack ------------------------------------------------------------
259
260/** @brief The record that a call of the stack carries, as the transport embeds it. */
265
266/** @brief Run @p fn on @p c under the lock of the stack, and return what it returns. */
268
269// --- names -----------------------------------------------------------------------------------
270
271/** @brief The callback of a name that the resolver of the stack found, with a NULL address for none. */
272typedef void (*protocore_net_dns_found_fn)(const char *, const protocore_net_ip *, void *);
273
274/**
275 * @brief Resolve @p host to @p addr, as dns_gethostbyname of lwIP does.
276 *
277 * A dotted quad completes in the call, with PROTOCORE_NET_OK. The stack has no resolver of its own, so each
278 * other name gets PROTOCORE_NET_ERR_VAL.
279 */
281 void *arg);
282
283// --- the pass ----------------------------------------------------------------------------------
284
285/**
286 * @brief One pass of the stack, under its lock: the units of idemIP, then each event of the TCP as a callback.
287 *
288 * @param now_ms the monotonic millisecond count of the caller
289 * @param random_word a random word for this pass
290 */
291void protocore_stack_net_pass(uint32_t now_ms, uint32_t random_word);
292
293/**
294 * @brief Start the task of the stack, which runs protocore_stack_net_pass once each tick of the platform.
295 *
296 * The task has the place of the tcpip thread of lwIP. Call it once, after Stack.start, Stack.bind and
297 * Stack.set_addr4. A second call does nothing.
298 *
299 * @return 1 when the task runs, and 0 when the platform refused it
300 */
302
303/**
304 * @brief One UDP datagram that Dispatch delivered to a UdpPcb entry, to the callback of its control block.
305 *
306 * @param frame the received frame
307 * @param entry the UdpPcb entry that the datagram matched
308 * @param ip_off the octets from the frame to the IPv4 header
309 * @param payload_off the octets from the frame to the UDP header
310 * @param payload_len the octets of the UDP header and its data
311 */
312void protocore_stack_net_udp_input(const uint8_t *frame, uint16_t entry, size_t ip_off, size_t payload_off,
313 size_t payload_len);
314
315EMBED_END_DECLS
316
317#endif // PROTOCORE_STACK_NET_H
protocore_net_err protocore_net_bind(protocore_pcb *p, const protocore_net_ip *a, uint16_t port)
void protocore_net_on_err(protocore_pcb *p, protocore_net_err_fn fn)
protocore_net_err protocore_net_write(protocore_pcb *p, const void *data, uint16_t len, uint8_t flags)
protocore_net_err(* protocore_net_recv_fn)(void *, protocore_pcb *, protocore_pbuf *, protocore_net_err)
Definition stack_net.h:130
protocore_net_err protocore_net_igmp_leave(const protocore_net_ip *nif, const protocore_net_ip *grp)
void protocore_net_udp_recv(protocore_udp_pcb *p, protocore_net_udp_recv_fn fn, void *arg)
protocore_net_err protocore_net_udp_sendto(const protocore_udp_pcb *p, const protocore_pbuf *b, const protocore_net_ip *a, uint16_t port)
protocore_net_err(* protocore_net_sent_fn)(void *, protocore_pcb *, uint16_t)
Definition stack_net.h:131
uint32_t protocore_net_ip4_u32(const protocore_net_ip *a)
The IPv4 address of a as the word that lwIP keeps: the four octets in network order in memory.
protocore_net_err protocore_net_close(protocore_pcb *p)
void(* protocore_net_udp_recv_fn)(void *, protocore_udp_pcb *, protocore_pbuf *, const protocore_net_ip *, uint16_t)
Definition stack_net.h:135
void protocore_net_recved(const protocore_pcb *p, uint16_t len)
void protocore_net_on_recv(protocore_pcb *p, protocore_net_recv_fn fn)
protocore_pcb * protocore_net_new(int type)
void protocore_stack_net_pass(uint32_t now_ms, uint32_t random_word)
One pass of the stack, under its lock: the units of idemIP, then each event of the TCP as a callback.
protocore_pbuf * protocore_net_pbuf_alloc(int layer, uint16_t len, int type)
A packet buffer of len octets from the pool, or NULL when the pool is spent.
void(* protocore_net_err_fn)(void *, protocore_net_err)
Definition stack_net.h:134
protocore_net_err protocore_net_connect(protocore_pcb *p, const protocore_net_ip *a, uint16_t port, protocore_net_connect_fn connected)
uint16_t protocore_net_pbuf_copy(const protocore_pbuf *p, void *dst, uint16_t len, uint16_t off)
Copy at most len octets of p, from off, to dst. The octets copied.
int protocore_net_ip4_is_multicast(const protocore_net_ip *a)
1 when a is an IPv4 host group, 224.0.0.0/4.
void protocore_net_on_sent(protocore_pcb *p, protocore_net_sent_fn fn)
const protocore_net_ip protocore_stack_net_any
The any address, which a bind takes for each local address.
protocore_net_err protocore_net_igmp_join(const protocore_net_ip *nif, const protocore_net_ip *grp)
char * protocore_net_ip_print(const protocore_net_ip *a, char *buf, int cap)
The dotted quad of a in buf, of cap octets.
protocore_pcb * protocore_net_listen(protocore_pcb *p, uint8_t backlog)
void protocore_net_nagle_disable(const protocore_pcb *p)
protocore_net_err protocore_net_dns_resolve(const char *host, protocore_net_ip *addr, protocore_net_dns_found_fn found, void *arg)
Resolve host to addr, as dns_gethostbyname of lwIP does.
int protocore_stack_net_start(void)
Start the task of the stack, which runs protocore_stack_net_pass once each tick of the platform.
void protocore_net_ip4_set(protocore_net_ip *a, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
Set a to the IPv4 address b0.b1.b2.b3.
int protocore_net_ip_parse(const char *s, protocore_net_ip *out)
Parse the dotted quad s into out. 1 on success, and 0 otherwise.
protocore_net_err(* protocore_net_accept_fn)(void *, protocore_pcb *, protocore_net_err)
Definition stack_net.h:132
protocore_net_err protocore_net_call_marshal(protocore_net_err(*fn)(protocore_net_call *), protocore_net_call *c)
Run fn on c under the lock of the stack, and return what it returns.
protocore_udp_pcb * protocore_net_udp_new(void)
void(* protocore_net_dns_found_fn)(const char *, const protocore_net_ip *, void *)
The callback of a name that the resolver of the stack found, with a NULL address for none.
Definition stack_net.h:272
uint8_t protocore_net_pbuf_free(const protocore_pbuf *p)
Give a packet buffer back to the pool.
protocore_net_err(* protocore_net_connect_fn)(void *, protocore_pcb *, protocore_net_err)
Definition stack_net.h:133
EMBED_BEGIN_DECLS typedef int8_t protocore_net_err
The result of a call of the stack, as lwIP numbers it.
Definition stack_net.h:37
protocore_net_err protocore_net_udp_bind(protocore_udp_pcb *p, const protocore_net_ip *a, uint16_t port)
void protocore_net_udp_remove(protocore_udp_pcb *p)
void protocore_stack_net_udp_input(const uint8_t *frame, uint16_t entry, size_t ip_off, size_t payload_off, size_t payload_len)
One UDP datagram that Dispatch delivered to a UdpPcb entry, to the callback of its control block.
uint16_t protocore_net_sndbuf(const protocore_pcb *p)
void protocore_net_rcv_wnd_update(const protocore_pcb *p, uint16_t len)
protocore_net_err protocore_net_output(const protocore_pcb *p)
void protocore_net_on_accept(protocore_pcb *p, protocore_net_accept_fn fn)
void protocore_net_abort(protocore_pcb *p)
void protocore_net_arg(protocore_pcb *p, void *arg)
void protocore_net_opt_set(void *p, uint32_t opt)
embed_bool pending
Definition stack_net.h:149
The record that a call of the stack carries, as the transport embeds it.
Definition stack_net.h:262
uint8_t bytes[16]
Definition stack_net.h:67
uint16_t tot_len
Definition stack_net.h:112
uint16_t len
Definition stack_net.h:113
struct protocore_pbuf * next
Definition stack_net.h:110
uint8_t flags
Definition stack_net.h:187
protocore_net_accept_fn on_accept
Definition stack_net.h:191
embed_bool in_use
Definition stack_net.h:197
protocore_net_sent_fn on_sent
Definition stack_net.h:190
embed_bool listen
Definition stack_net.h:196
uint32_t so_options
Definition stack_net.h:185
uint8_t tos
Definition stack_net.h:178
uint16_t local_port
Definition stack_net.h:181
protocore_net_recv_fn on_recv
Definition stack_net.h:189
uint8_t state
Definition stack_net.h:180
uint8_t ttl
Definition stack_net.h:179
protocore_net_connect_fn on_connect
Definition stack_net.h:192
protocore_net_ip local_ip
Definition stack_net.h:183
protocore_net_ip remote_ip
Definition stack_net.h:184
protocore_net_err_fn on_err
Definition stack_net.h:193
StackNetConnectWait connect_wait
Definition stack_net.h:194
uint16_t remote_port
Definition stack_net.h:182
uint8_t entry
Definition stack_net.h:195
uint16_t snd_queuelen
Definition stack_net.h:186
protocore_net_udp_recv_fn on_recv
Definition stack_net.h:242
uint32_t so_options
Definition stack_net.h:243
embed_bool in_use
Definition stack_net.h:246
embed_bool opened
Definition stack_net.h:245
uint16_t local_port
Definition stack_net.h:240