Skip to content

Commit 68beb52

Browse files
committed
sys/net/nanocoap: add and use coap_get_response_hdr_len()
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.
1 parent afb16bb commit 68beb52

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

examples/nanocoap_server/coap_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, c
5353
(void)context;
5454
coap_block_slicer_t slicer;
5555
coap_block2_init(pkt, &slicer);
56-
uint8_t *payload = buf + coap_get_total_hdr_len(pkt);
56+
uint8_t *payload = buf + coap_get_response_hdr_len(pkt);
5757

5858
uint8_t *bufpos = payload;
5959

sys/include/net/nanocoap.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,22 @@ static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
677677
coap_get_token_len(pkt);
678678
}
679679

680+
/**
681+
* @brief Get the header length a response to the given packet will have
682+
*
683+
* @param[in] pkt CoAP packet to reply to
684+
* @return Length of the response header including token excluding
685+
* CoAP options and any payload marker
686+
*
687+
* @note The main use case is the use of @ref coap_block2_build_reply, which
688+
* is building the CoAP header of the response after options and
689+
* payload have been added.
690+
*/
691+
static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
692+
{
693+
return coap_get_total_hdr_len(pkt);
694+
}
695+
680696
/**
681697
* @brief Write the given raw message code to given CoAP header
682698
*
@@ -1939,6 +1955,9 @@ static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
19391955
* @param[in] payload_len length of payload
19401956
* @param[in] slicer slicer to use
19411957
*
1958+
* @warning Use @ref coap_get_response_hdr_len to determine the size of the
1959+
* header this will write.
1960+
*
19421961
* @returns size of reply packet on success
19431962
* @returns <0 on error
19441963
*/

sys/include/net/nanocoap_sock.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@
110110
* Start with coap_block2_init() to read the client request and initialize a
111111
* coap_slicer_t struct with the size and location for this slice of the
112112
* overall payload. Then write the block2 option in the response with
113-
* coap_opt_put_block2(). The option includes an indicator ("more") that a
114-
* slice completes the overall payload transfer. You may not know the value for
115-
* _more_ at this point, but you must initialize the space in the packet for
116-
* the option before writing the payload. The option is rewritten later.
113+
* coap_opt_put_block2(). Use @ref coap_get_response_hdr_len to get the length
114+
* of the response header: This will be the offset in the buffer where you
115+
* should start adding options. The Block2 option includes an indicator
116+
* ("more") that a slice completes the overall payload transfer. You may not
117+
* know the value for _more_ at this point, but you must initialize the space
118+
* in the packet for the option before writing the payload. The option is
119+
* rewritten later.
117120
*
118121
* Next, use the coap_blockwise_put_xxx() functions to write the payload
119122
* content. These functions use the coap_block_slicer_t to enable or disable

sys/net/application_layer/nanocoap/nanocoap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \
14151415
(void)context;
14161416
coap_block_slicer_t slicer;
14171417
coap_block2_init(pkt, &slicer);
1418-
uint8_t *payload = buf + coap_get_total_hdr_len(pkt);
1418+
uint8_t *payload = buf + coap_get_response_hdr_len(pkt);
14191419
uint8_t *bufpos = payload;
14201420
bufpos += coap_put_option_ct(bufpos, 0, COAP_FORMAT_LINK);
14211421
bufpos += coap_opt_put_block2(bufpos, COAP_OPT_CONTENT_FORMAT, &slicer, 1);

0 commit comments

Comments
 (0)