Skip to content

Commit

Permalink
Merge pull request #21166 from maribu/backport-pr-21163
Browse files Browse the repository at this point in the history
[BACKPORT] sys/net/nanocoap: align request handling with spec
  • Loading branch information
maribu authored Jan 27, 2025
2 parents 3e77360 + 0d6cb6f commit d06a7d9
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,40 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
{
assert(ctx);

switch (coap_get_type(pkt)) {
case COAP_TYPE_CON:
case COAP_TYPE_NON:
/* could be a request ==> proceed */
break;
default:
DEBUG_PUTS("coap_handle_req(): ignoring RST/ACK");
return -EBADMSG;
}

if (coap_get_code_class(pkt) != COAP_REQ) {
DEBUG("coap_handle_req(): not a request.\n");
DEBUG_PUTS("coap_handle_req(): not a request --> ignore");
return -EBADMSG;
}

if (pkt->hdr->code == 0) {
return coap_build_reply(pkt, COAP_CODE_EMPTY, resp_buf, resp_buf_len, 0);
if (coap_get_code_raw(pkt) == COAP_CODE_EMPTY) {
/* we are not able to process a CON/NON message with an empty code,
* so we reply with a RST, unless we got a multicast message */
if (!sock_udp_ep_is_multicast(coap_request_ctx_get_local_udp(ctx))) {
return coap_build_reply(pkt, COAP_CODE_EMPTY, resp_buf, resp_buf_len, 0);
}
}

ssize_t retval = coap_tree_handler(pkt, resp_buf, resp_buf_len, ctx,
coap_resources, coap_resources_numof);
if (retval < 0) {
/* handlers were not able to process this, so we reply with a RST,
* unless we got a multicast message */
if (!sock_udp_ep_is_multicast(coap_request_ctx_get_local_udp(ctx))) {
return coap_build_reply(pkt, COAP_CODE_EMPTY, resp_buf, resp_buf_len, 0);
}
}
return coap_tree_handler(pkt, resp_buf, resp_buf_len, ctx,
coap_resources, coap_resources_numof);

return retval;
}

ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len,
Expand Down

0 comments on commit d06a7d9

Please sign in to comment.