-
Notifications
You must be signed in to change notification settings - Fork 2k
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
sys/net/nanocoap: improve coap_build_reply()
#21155
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, nice start! Some suggestions below.
e2464a0
to
58ca598
Compare
Using a constant is easier than explaining where the magic 1 came from in size estimations.
7141f4f
to
38397bc
Compare
38397bc
to
526310d
Compare
coap_build_reply()
coap_build_reply()
- 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 <[email protected]>
526310d
to
d1da109
Compare
I changed the API for handling with the No-Response case, so that less foot-shooting should occur. The API change is not breaking:
|
Let me try to explain the motivation why I believe changing the API is required. The current API does return The API doc (in
The condition that For figuring out the position where to continue writing data to, we usually do: size_t max_data_len = COAP_OPT_FOO_MAX_LEN + COAP_OPT_BAR_MAX_LEN
+ COAP_PAYLOAD_MARKER_SIZE + payload_len;
ssize_t hdr_len = coap_build_reply(pkt, COAP_CODE_CONTENT, buf, len, max_data_len);
if (hdr_len > max_data_len) {
return hdr_len;
}
/* per API doc: return value is actually written header length + max_data_en */
hdr_len -= max_data_len;
uint8_t *pos = response_buf + hdr_len; The intuitive alternative to just subtracting
However, this would work only when support for the Extended Token Length RFC is not enabled. The correct way would actually be parsing back the header written:
This however would yet again add another builtin assumption that the format is CoAP over UDP. And there still is a footgun: Because the return value was larger than the requested IMO this API has so many different ways to shoot your foot that just returning an error on matching No-Response Option and letting the CoAP Server handle that error by ignoring it (request was a NON) or sending an empty ACK (request was a CON) is a lot more sane. Specifically: The API doc already states, that |
With the API in Because of that, I'd also like to interest @MrKevinWeiss in this PR with regard to whether we should backport this. |
if (type != COAP_TYPE_ACK) { | ||
return 0; | ||
} | ||
return -ECANCELED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation of coap_build_reply_header()
(and coap_reply_simple()
which makes use of this) will now also need an update.
Contribution description
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.
Testing procedure
(CoAP reply handlers that did not get error handling correctly will stay broken.)
Issues/PRs references
None