- The responsibility for handling matching CoAP No-Response Options
has been split:
- `coap_build_reply()` only needs to report this and return
`-ECANCLED`
- `coap_handle_req()` does generate the empty ACK is needed.
==> As a result, writing CoAP request handlers correctly becomes a
lost easier. Correct error handling to be present is now
sufficient for correct handling of No-Response options.
==> This change is backward compatible with existing code.
- The API doc has been cleaned up and straightened
Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>
For in-band signalling that a content format is not valid / present,
the magic number `COAP_FORMAT_NONE` was introduced and the type
`uint16_t` was used. Some APIs however used different in-band signalling
values and types:
- coap_reply_simple(): No signal available, `unsigned int`
- coap_build_reply_header(): negative values, `int`
(Using `int` would prevent using larger content format numbers on
8-bit and 16-bit archs, where `int` and `int16_t` have the same
range.)
This changes the behavior to consistently use `COAP_FORMAT_NONE` as
"no content format" signal and `uint16_t` as type.
There is an `typdef void sock_udp_ep_t` hack in `nanocoap.h` that tries
allow using parts of nanocoap without a RIOT network stack.
There is a similar hack to allow it to be used without RIOT at all. This
has been used in the early days during development on Linux directly.
This however has not been tested and left bit rotting.
Both hacks are remove and nanocoap now just depends on sock/udp.h.
In a future cleanup, the CoAP packet parsing and building code of
nanocoap can be separated from the part that does the transmission and
reception of UDP packets. That way, using nanocoap's packet parsing and
building will again be available without using a network stack.
Until then, let's drop the hacks and just depend on a network stack.
This adds the new `nanocoap_server_observe` module that implements the
server side of the CoAP Observe option. It does require cooperation
from the resource handler to work, though.
Co-Authored-By: mguetschow <mikolai.guetschow@tu-dresden.de>
Co-authored-by: benpicco <benpicco@googlemail.com>
This allows sending a separate response with CoAP Options and adds a
helper to detect duplicate requests, so that resource handlers can
repeat their empty ACK on duplicates.
Calling `coap_get_token()` and `coap_get_token_length()` on an
(mostly) uninitialized `coap_pkt_t` did work so far due to
implementation details matching the expectations, but this is not
backed up by any API contract.
This fixes the API abuse by introducing and using a new API that does
read a token and token length from a CoAP over UDP packet out of a
buffer. This now provides the behavior expected by the caller and
commits to it via API contract.
Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>
Co-authored-by: benpicco <benpicco@googlemail.com>
When module `nanocoap_server_separate` is not used, the functions to
send separate responses are still provided, just in a broken version:
They will send the separate replies from a different endpoint than the
request was received at (even on machines with only one IP address, as
also the source port is randomized).
This changes the behavior to only provide the functions for separate
response when the do work, so that others will detect an invalid
configuration at compile time rather than at run time.
The documentation is duly updated.
When RFC 8974 support (module `nanocoap_token_ext`) is in use, the
request token may be longer than the buffer in the separate response
context is large. This adds a check to not overflow the buffer.
Sadly, this is an API change: Preparing the separate response context
can actually fail, so we need to report this with a return value.
The example application has been adapted to only proceed if the separate
reply context could have been prepared, and rather directly emit a
reset message if the token exceeds the static buffer.
Co-authored-by: benpicco <benpicco@googlemail.com>
It's very unlikely that a pkt snip will have more than 255 users.
Use a uint8_t here to save 4 bytes per snip as this now fits into
the ununsed struct padding.
This changes the API of xfa from
XFA(array_name, prio) type element_name = INITIALIZER;
to
XFA(type, array_name, prio) element_name = INITIALIZER;
this allows forcing natural alignment of the type, fixing failing tests
on `native64`.
Before, handlers writing blockwise transfer assumed that the response
header length will match the request header length. This is true for
UDP, but not for TCP: The CoAP over TCP header contains a Len field,
that gets extended for larger messages. Since the reply often is indeed
larger than the request, this is indeed often the case for CoAP over
TCP.
Note: Right now, no CoAP over TCP implementation is upstream. However,
getting rid of incorrect assumptions now will make life easier
later on.
gcoap contains a hack where a `coap_pkt_t` is pulled out of thin air,
parts of the members are left uninitialized and a function is called on
that mostly uninitialized data while crossing fingers hard that the
result will be correct. (With the current implementation of the used
function this hack does actually work.)
Estimated level of insanity: 😱😱😱😱😱
This adds to insane functions to get the length of a token and the
length of a header of a CoAP packet while crossing fingers hard that
the packet is valid and that the functions do not overread.
Estimated level of insanity: 😱😱😱
The newly introduced insane functions are used to replace the old
insane hack, resulting in an estimated reduction of insanity of 😱😱.
Side note: This actually does fix a bug, as the old code did not take
into account the length of the extended TKL field in case of
RFC 8974 being used. But that is a bug in the abused API,
and not in the caller abusing the API.
Some calls to `coap_build_hdr()` were done with the target buffer for
the header and the source buffer for the token overlapping:
They reuse the buffer that held the request to assemble the response in.
We cannot use `memcpy()` in this case to copy the token into the target
buffer, as source and destination would (fully) overlap.
This commit makes reusing the request buffer for the response a special
case: `memcpy()` is only used to copy the token if source and
destination address of the token differ.
An alternative fix would have been to use `memmove()` unconditionally.
But `memmove()` does not make any assumption about the layout of target
and source buffer, while we know that the token either will already be
at the right position (when reusing the request buffer for the response)
or be in a non-overlapping buffer (when generating a fresh token). This
approach is more efficient than `memmove()`.