Skip to content

sys/net/nanocoap: Add CoAP over TCP support #21048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

maribu
Copy link
Member

@maribu maribu commented Nov 26, 2024

Contribution description

This PR adds CoAP over TCP support to nanocoap. It is still work in progress, with many rough edges. This will get updated and filled in with more details as the PR evolves.

Testing procedure

Run sudo ./dist/tools/tapsetup/tapsetup

Server

NETWORK_STACK=lwip make BOARD=native -C examples/nanocoap_server -j && ./examples/nanocoap_server/bin/native/nanocoap_server.elf -w tap1

Wait for the IPv6 address to show up

Client

USEMODULE=nanocoap_tcp make BOARD=native -C tests/net/nanocoap_cli all flash term -j

Then in the RIOT shell run e.g.

url get coap+tcp://[fe80::24de:d0ff:fe85:bdb2]/riot/ver

Issues/PRs references

Depends on and includes: #20900

@github-actions github-actions bot added Area: network Area: Networking Area: tests Area: tests and testing framework Area: build system Area: Build system Area: pkg Area: External package ports Area: CoAP Area: Constrained Application Protocol implementations Area: sys Area: System Area: examples Area: Example Applications labels Nov 26, 2024
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch 2 times, most recently from ec3b453 to 765fdf1 Compare December 17, 2024 14:42
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch from 765fdf1 to ec5a1f7 Compare January 7, 2025 19:51
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch 2 times, most recently from eca9688 to 647a436 Compare January 13, 2025 17:51
@github-actions github-actions bot added Area: doc Area: Documentation Area: tools Area: Supplementary tools labels Jan 13, 2025
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 14, 2025
@riot-ci
Copy link

riot-ci commented Jan 14, 2025

Murdock results

FAILED

b53fec2 net/nanocoap: Send separate response from server socket

Success Failures Total Runtime
105 0 9566 01m:39s

Artifacts

@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch 10 times, most recently from a8d54ba to e374672 Compare January 23, 2025 22:15
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch from e374672 to 5dfd557 Compare February 4, 2025 16:40
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch 2 times, most recently from fdd75a8 to 48f663a Compare February 12, 2025 21:11
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch from b3a7b43 to 8e55d8c Compare May 6, 2025 10:35
@github-actions github-actions bot removed the Area: pkg Area: External package ports label May 6, 2025
@crasbe crasbe added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label May 7, 2025
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch 2 times, most recently from d94dee9 to 1b81973 Compare May 12, 2025 12:38
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch from 1b81973 to a588366 Compare May 23, 2025 11:25
maribu added 18 commits August 19, 2025 10:10
This changes the API of nanocoap with the goal to reduce the expose of
UDP specifics in the API. The plan is to eventually support transports
such as CoAP over TCP and CoAP over WebSocket directly in nanocoap
while sharing most of the code, as e.g. the CoAP Option processing
remains identical. Specifically, the plan is to unlock a transport with
modules and introduce overhead for dispatching to specific transport
only when multiple transports are actually in use.

Support for OSCORE directly in nanocoap is probably not sensible, as
the serialization is very much unlike the other transports. A unified
CoAP API for multiple transports including OSCORE is probably best
implemented on top. But when limited to the boring set of CoAP
transports, we probably can support them well with nanocoap with less
overhead.

Breaking API Changes:
=====================

- `coap_parse()` now returns `ssize_t` instead of `int`
    - This function is not really user facing, so the impact should
      be limited
    - This is useful for stream transports where the buffer may
      contain data of more than one packet. The return value contains
      the number of bytes actually consumed, which will match the
      buffer size for non-stream transports.

API Changes:
============

- `coap_pkt_t` now contains a `uint8_t *buf` pointer instead of a
  `coap_hdr_t *hdr` pointer to the beginning of the buffer
    - This will also work when the buffer is used by non-UDP
      transports
    - A deprecated `coap_udp_hdr_t *hdr` has been crammed into
      an unnamed `union` with `uint8_t *buf`. For architectures
      where pointers have the same memory layout regardless of type
      (e.g. all of the supported ones), this will make `hdr` an
      alias for `buf`.
    - The alias will only be provided if no transport besides UDP is
      used in nanocoap. So existing apps will continue to work, new
      apps that want to support other transports need to move to
      adapt.
- `coap_hdr_t` has been renamed to `coap_udp_hdr_t`
    - A deprecated alias was created for deprecation
- `coap_hdr*()` functions have been deprecated
    - Equivalent `coap_pkt*()` functions have been created that work
      on `coap_pkt_t *` instead of `coap_hdr_t *`
    - If non-UDP transports are used, the deprecated `coap_hdr*()`
      will probably not be exposed to avoid footguns.
- `coap_build_hdr()` has been renamed to `coap_build_udp_hdr()` and
  that works on an `uint8_t *` buffer with a given length, rather than
  on a `coap_hdr_t *` with a *figers crossed* length
    - a deprecated `coap_build_hdr()` function was added that calls
      to `coap_build_udp_hdr()` and has the same signature, so that
      users have time to update
This changes the Makefile to easily setup lwIP as network stack, while
keeping GNRC as the default.

If lwIP is used on one of the larger boards, the app will be build with
CoAP over TCP support enabled.
And test this on native with CoAP over WebSocket+YOLO
And hook up CoAP over WebSocket+YOLO
This implements a trivial reverse proxy that can translate between
different transports for CoAP.

The proxy is relatively limited in that each proxy instance can only
forward one request at a time, reducing throughput and increasing
latency significantly. It also strips the forwarded reply of any CoAP
options (or sends an error if the response contained critical options).
This proxy allows using CoAP over YOLO with regular CoAP over WebSocket
clients by forwarding binary messages received via WebSocket to a given
UDP endpoint and the replies back to the WebSocket.
This makes sure the separate response is send using the network
interface the request was received on.
@maribu maribu force-pushed the sys/net/nanocoap/transport-tcp branch from a588366 to b53fec2 Compare August 19, 2025 08:18
@crasbe crasbe added State: waiting for other PR State: The PR requires another PR to be merged first CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: CoAP Area: Constrained Application Protocol implementations Area: doc Area: Documentation Area: examples Area: Example Applications Area: network Area: Networking Area: sys Area: System Area: tests Area: tests and testing framework Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR State: waiting for other PR State: The PR requires another PR to be merged first Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants