ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
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 PC_ENABLE_DTLS && PC_ENABLE_COAP
12
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.
17#define PC_COAPS_MSG_CAP 1152
18
19int pc_coaps_process(DtlsConn *c, const uint8_t *dgram, size_t len, uint8_t *out, size_t out_cap)
20{
21 if (!pc_dtls_conn_established(c))
22 {
23 return pc_dtls_conn_process(c, dgram, len, out, out_cap); // still handshaking (or -1 on fatal error)
24 }
25
26 // Established. A DTLSCiphertext unified header is 0b001CSLEE; the low two bits are the epoch mod 4,
27 // so epoch 3 (application data) is 0b001xxx11. Route application data through CoAP; route anything
28 // else (a retransmitted epoch-2 client Finished) back to the state machine to be re-acknowledged.
29 if (len >= 1 && (dgram[0] & 0xE0) == 0x20 && (dgram[0] & 0x03) == 3)
30 {
31 uint8_t req[PC_COAPS_MSG_CAP];
32 size_t req_len = 0;
33 if (!pc_dtls_conn_open_app(c, dgram, len, req, sizeof(req), &req_len))
34 {
35 return 0; // replay, truncated, or not application data
36 }
37 uint8_t resp[PC_COAPS_MSG_CAP];
38 size_t pc_resp_len = pc_coap_server_process(req, req_len, resp, sizeof(resp));
39 if (!pc_resp_len)
40 {
41 return 0; // no response (e.g. a Non-confirmable message with no resource match)
42 }
43 return (int)pc_dtls_conn_seal_app(c, resp, pc_resp_len, out, out_cap);
44 }
45 return pc_dtls_conn_process(c, dgram, len, out, out_cap);
46}
47
48#endif // PC_ENABLE_DTLS && PC_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...