ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
coaps.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 coaps.h
6 * @brief CoAP over DTLS (RFC 7252 sec 9): the bridge between one DTLS connection and the CoAP server.
7 *
8 * RFC 7252 sec 9.1 binds CoAP to DTLS, and sec 6.2 gives that binding the "coaps" URI scheme; sec
9 * 12.7 registers its port, 5684. This is the transport-neutral half: it drives one @ref DtlsConn
10 * through its handshake and, once the connection is established, opens each protected application
11 * record, answers the CoAP message inside it through @ref Coap, and seals the response back into one
12 * record. The socket and the per-peer routing sit above it in coaps_server.h, so nothing here binds a
13 * port and the whole path is host-testable against an in-test DTLS client.
14 *
15 * The record layer is RFC 9147 (DTLS 1.3). Its sec 4 Figure 3 gives the DTLSCiphertext unified
16 * header: the three high bits of the first byte are 001, the C bit (0x10) marks a Connection ID, and
17 * the two low bits (0x03) carry the low-order bits of the epoch. Epoch 3 is application data;
18 * anything else is a handshake record and goes back to the state machine, which is what re-answers a
19 * retransmitted client Finished whose acknowledgement was lost (RFC 9147 sec 5.8.3).
20 *
21 * The module exports one symbol, @ref Coaps. Everything in coaps.c has internal linkage.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_COAPS_H
28#define PROTOCORE_COAPS_H
29
30#include "protocore_config.h" // the entry point: protocore_types.h for the widths
31
32#if PROTOCORE_ENABLE_DTLS && PROTOCORE_ENABLE_COAP
33
34#include "network_drivers/presentation/security/dtls/dtls_conn/dtls_conn.h" // DtlsConn: the connection a call drives
35
37
38/** @brief One inbound datagram and the buffer whatever it owes is written into. */
39typedef struct
40{
41 const uint8_t *data; ///< the received datagram's octets
42 size_t len; ///< how many
43 uint8_t *out; ///< where the outbound datagram is written
44 size_t out_cap; ///< how much room that has
45} CoapsBridgeArgs;
46
47/**
48 * @brief CoAP carried over one DTLS connection.
49 *
50 * A caller sets the members a call takes, invokes it through ::Coaps, and reads the outcome off the
51 * same handle.
52 *
53 * No storage member: the connection is the caller's @ref DtlsConn and the plaintext scratch lives for
54 * one call, so the bridge holds nothing between calls.
55 *
56 * @var CoapsNs::conn the DTLS connection every call acts on
57 * @var CoapsNs::dgram the datagram a call reads and the buffer it writes
58 * @var CoapsNs::i32 octets written to @c dgram.out, 0 when there is nothing to send, or -1 when
59 * the handshake failed and @c conn is FAILED
60 * @var CoapsNs::process turn one received datagram: drive the handshake, or answer the CoAP message
61 * inside an epoch-3 application record and seal the response
62 */
63typedef struct
64{
65 DtlsConn *conn; ///< the connection every call names
66 CoapsBridgeArgs dgram; ///< what turning one datagram takes
67 int32_t i32;
68} CoapsVars;
69
70/** @brief The operands and the outcome. */
71extern CoapsVars CoapsV;
72
73/** @brief The entries. */
74typedef struct
75{
76 void (*const process)(uint8_t *work);
77} CoapsNs;
78
79// What the table binds, defined once in the .c and taking one parameter each: everything
80// else an entry needs is an operand in CoapsV or a region of the borrow at a fixed offset.
81void protocore_coaps_process(uint8_t *work);
82
83// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
84// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
85// `Coaps.process(work)` resolves to a named function and becomes a DIRECT call. An extern table
86// leaves the call indirect and the symbol live at every level, -O2 -flto included.
87static const CoapsNs Coaps __attribute__((unused)) = {
88 .process = protocore_coaps_process,
89};
90
92
93#endif // PROTOCORE_ENABLE_DTLS && PROTOCORE_ENABLE_COAP
94
95#endif // PROTOCORE_COAPS_H
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
#define PROTOCORE_END_DECLS
Definition types.h:97