Skip to content
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

nanocoap: add coap_build_reply_header() #20284

Merged
merged 4 commits into from
Jan 25, 2024

Conversation

benpicco
Copy link
Contributor

Contribution description

The coap_build_reply() function is kind of awkward to use.
coap_reply_simple() is nicer, but always requires keeping a separate buffer for the payload that then gets copied into the reply buffer anyway.

Add a new function coap_build_reply_header() that covers all uses of coap_build_reply() but is simpler to use: It builds the response header in the response buffer and sets the payload pointer to after the header, so a user can write the payload directly into the response buffer without having to copy it.

Testing procedure

coap_reply_simple() has been re-implemented using the new function.

Issues/PRs references

@github-actions github-actions bot added Area: network Area: Networking Area: CoAP Area: Constrained Application Protocol implementations Area: sys Area: System labels Jan 22, 2024
@benpicco benpicco force-pushed the coap_build_reply_header branch from c561699 to 7c4faab Compare January 22, 2024 14:58
@benpicco benpicco requested review from chrysn and mguetschow January 22, 2024 14:59
Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice addition to me, but I'm maybe not experienced enough with CoAP and its RIOT implementation to judge whether it adds the right abstraction layer.

Some comments on the code follow.

sys/include/net/nanocoap.h Outdated Show resolved Hide resolved
sys/include/net/nanocoap.h Outdated Show resolved Hide resolved
sys/include/net/nanocoap.h Outdated Show resolved Hide resolved
sys/net/application_layer/nanocoap/nanocoap.c Show resolved Hide resolved
? COAP_TYPE_ACK
: COAP_TYPE_NON;

bufpos += coap_build_hdr(buf, type, coap_get_token(pkt), coap_get_token_len(pkt),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the coap_build_hdr docs:

Caller must ensure hdr can hold the header and the full token!

You also don't check whether buf is large enough to hold the content type (format) option. *payload_len_max might underflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also don't check whether buf is large enough to hold the content type (format) option.

Our CoAP API is unfortunately kind of insane in that it expects you to write to a buffer and then check after the fact that it overflowed.
There is no way to detect if the result of coap_put_option_ct() will fit into the supplied buffer beforehand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our CoAP API is unfortunately kind of insane in that it expects you to write to a buffer and then check after the fact that it overflowed.

Oh wow, that's crazy o.O Any plans for this to be fixed on our roadmap?

There is no way to detect if the result of coap_put_option_ct() will fit into the supplied buffer beforehand.

According to the RFC it can at most be 2B long if I read it correctly.

sys/net/application_layer/nanocoap/nanocoap.c Outdated Show resolved Hide resolved
sys/include/net/nanocoap.h Show resolved Hide resolved
sys/net/application_layer/nanocoap/nanocoap.c Outdated Show resolved Hide resolved
@benpicco benpicco force-pushed the coap_build_reply_header branch 2 times, most recently from 8930092 to 5d6ae60 Compare January 23, 2024 13:33
sys/include/net/coap.h Show resolved Hide resolved
sys/include/net/nanocoap.h Show resolved Hide resolved
sys/net/application_layer/nanocoap/nanocoap.c Show resolved Hide resolved
? COAP_TYPE_ACK
: COAP_TYPE_NON;

bufpos += coap_build_hdr(buf, type, coap_get_token(pkt), coap_get_token_len(pkt),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our CoAP API is unfortunately kind of insane in that it expects you to write to a buffer and then check after the fact that it overflowed.

Oh wow, that's crazy o.O Any plans for this to be fixed on our roadmap?

There is no way to detect if the result of coap_put_option_ct() will fit into the supplied buffer beforehand.

According to the RFC it can at most be 2B long if I read it correctly.

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM then, thanks for bearing with me!

@benpicco benpicco force-pushed the coap_build_reply_header branch from 668a1d9 to c71b5ae Compare January 23, 2024 18:17
@benpicco
Copy link
Contributor Author

Thank you for the review!

@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 24, 2024
@riot-ci
Copy link

riot-ci commented Jan 24, 2024

Murdock results

✔️ PASSED

c71b5ae nanocoap: implement coap_reply_simple() using coap_build_reply_header()

Success Failures Total Runtime
8629 0 8629 11m:53s

Artifacts

@benpicco benpicco added this pull request to the merge queue Jan 24, 2024
Merged via the queue into RIOT-OS:master with commit 2d45915 Jan 25, 2024
26 checks passed
@benpicco benpicco deleted the coap_build_reply_header branch January 25, 2024 08:36
@MrKevinWeiss MrKevinWeiss added this to the Release 2024.04 milestone Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: CoAP Area: Constrained Application Protocol implementations Area: network Area: Networking Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants