DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
coaps.cpp
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 coaps.cpp
6 * @brief CoAP over DTLS (CoAPs, RFC 7252 §9). See coaps.h.
7 */
8
10
11#if DETWS_ENABLE_DTLS && DETWS_ENABLE_COAP
12
13#include "services/coap/coap.h"
14
15// Largest CoAP request/response carried in one DTLS application record. CoAP messages are expected to
16// fit a single datagram (RFC 7252 §4.6); anything larger is dropped rather than fragmented here.
17static constexpr size_t COAPS_MSG_CAP = 1152;
18
19int coaps_process(DtlsConn *c, const uint8_t *dgram, size_t len, uint8_t *out, size_t out_cap)
20{
21 if (!dtls_conn_established(c))
22 return dtls_conn_process(c, dgram, len, out, out_cap); // still handshaking (or -1 on fatal error)
23
24 // Established. A DTLSCiphertext unified header is 0b001CSLEE; the low two bits are the epoch mod 4,
25 // so epoch 3 (application data) is 0b001xxx11. Route application data through CoAP; route anything
26 // else (a retransmitted epoch-2 client Finished) back to the state machine to be re-acknowledged.
27 if (len >= 1 && (dgram[0] & 0xE0) == 0x20 && (dgram[0] & 0x03) == 3)
28 {
29 uint8_t req[COAPS_MSG_CAP];
30 size_t req_len = 0;
31 if (!dtls_conn_open_app(c, dgram, len, req, sizeof(req), &req_len))
32 return 0; // replay, truncated, or not application data
33 uint8_t resp[COAPS_MSG_CAP];
34 size_t resp_len = coap_server_process(req, req_len, resp, sizeof(resp));
35 if (!resp_len)
36 return 0; // no response (e.g. a Non-confirmable message with no resource match)
37 return (int)dtls_conn_seal_app(c, resp, resp_len, out, out_cap);
38 }
39 return dtls_conn_process(c, dgram, len, out, out_cap);
40}
41
42#endif // DETWS_ENABLE_DTLS && DETWS_ENABLE_COAP
Zero-heap CoAP server (RFC 7252): message codec + a fixed resource table.
CoAP over DTLS (CoAPs, RFC 7252 §9) - the bridge between the DTLS 1.3 server and the CoAP request han...