net.c: Handle multiple same token request/responses

Sending off multiple CON requests with the same token without waiting for
any responses, causes the first one to be sent with the remainder ending
up in the delayqueue.

On receipt of ACK, coap_remove_from_queue() called in coap_dispatch(),
which returns the MID matching entry that was in the send queue, and then
the next entry on the delayqueue is removed from delayqueue and then sent
which puts the request on the sendqueue for any required re-transmissions.

coap_cancel_all_messages() should not be called in handle_response()
following the logic above if response is type ACK. If
coap_cancel_all_messages() is subsequently called with the same (possibly
empty) token as the next request that is being sent, then the next sendqueue
entry is inadvertantly deleted.

As there is now no sendqueue entry on receipt of the next ACK, the next
delayqueue entry is not sent off.

Only call coap_cancel_all_messages() if response is not of type ACK
(i.e. a separate response).

Report on tokens being the same as the previous one to encourage application
coders to use different tokens.
This commit is contained in:
Jon Shallow authored and mrdeep1 committed 2022-06-21 14:50:47 +01:00
1 parent d68f5d6f57
commit 132c726190
2 files changed
+14 -2

No files matched your search

+2 -1
View File
@@ -144,8 +144,9 @@ struct coap_session_t {
uint8_t block_mode; /**< Zero or more COAP_BLOCK_ or'd options */
uint8_t doing_first; /**< Set if doing client's first request */
uint8_t proxy_session; /**< Set if this is an ongoing proxy session */
uint8_t delay_recursive; /**< Set if in coap_client_delay_first() */
uint8_t delay_recursive; /**< Set if in coap_client_delay_first() */
uint64_t tx_token; /**< Next token number to use */
coap_bin_const_t *last_token; /** last token used to make a request */
};
#if COAP_SERVER_SUPPORT
+12 -1
View File
@@ -1144,6 +1144,17 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) {
if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block) &&
(block.m == 1 || block.bert == 1))
have_block1 = 1;
if (observe_action != COAP_OBSERVE_CANCEL) {
/* Warn about re-use of tokens */
coap_bin_const_t token = coap_pdu_get_token(pdu);
if (session->last_token &&
coap_binary_equal(&token, session->last_token)) {
coap_log(LOG_DEBUG, "Token reused - see https://www.rfc-editor.org/rfc/rfc9175.html#section-4.2\n");
}
coap_delete_bin_const(session->last_token);
session->last_token = coap_new_bin_const(token.s, token.length);
}
} else {
memset(&block, 0, sizeof(block));
}
@@ -2970,7 +2981,7 @@ handle_response(coap_context_t *context, coap_session_t *session,
* been lost, so we need to stop retransmitting requests with the
* same token.
*/
if (rcvd->type == COAP_MESSAGE_ACK)
if (rcvd->type != COAP_MESSAGE_ACK)
coap_cancel_all_messages(context, session, rcvd->token, rcvd->token_length);
if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) {