diff --git a/include/coap3/coap_session_internal.h b/include/coap3/coap_session_internal.h index bbca8e69..35bee756 100644 --- a/include/coap3/coap_session_internal.h +++ b/include/coap3/coap_session_internal.h @@ -149,6 +149,10 @@ struct coap_session_t { uint64_t tx_token; /**< Next token number to use */ coap_bin_const_t *last_token; /** last token used to make a request */ coap_bin_const_t *echo; /**< Echo value to send with next request */ + coap_mid_t last_ack_mid; /**< The last ACK mid that has been + been processed */ + coap_mid_t last_con_mid; /**< The last CON mid that has been + been processed */ }; #if COAP_SERVER_SUPPORT diff --git a/src/coap_session.c b/src/coap_session.c index 6e80c381..e21f331b 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -209,6 +209,8 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, session->probing_rate = COAP_DEFAULT_PROBING_RATE; session->dtls_event = -1; session->last_ping_mid = COAP_INVALID_MID; + session->last_ack_mid = COAP_INVALID_MID; + session->last_con_mid = COAP_INVALID_MID; /* Randomly initialize */ coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid)); diff --git a/src/net.c b/src/net.c index 9691bf23..82c56293 100644 --- a/src/net.c +++ b/src/net.c @@ -3033,6 +3033,23 @@ handle_response(coap_context_t *context, coap_session_t *session, if (rcvd->type != COAP_MESSAGE_ACK) coap_cancel_all_messages(context, session, rcvd->token, rcvd->token_length); + /* Check for message duplication */ + if (COAP_PROTO_NOT_RELIABLE(session->proto)) { + if (rcvd->type == COAP_MESSAGE_CON) { + if (rcvd->mid == session->last_con_mid) { + /* Duplicate response */ + return; + } + session->last_con_mid = rcvd->mid; + } else if (rcvd->type == COAP_MESSAGE_ACK) { + if (rcvd->mid == session->last_ack_mid) { + /* Duplicate response */ + return; + } + session->last_ack_mid = rcvd->mid; + } + } + if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) { /* See if need to send next block to server */ if (coap_handle_response_send_block(session, sent, rcvd)) {