diff --git a/examples/coap-server.c b/examples/coap-server.c index b61a1b3f..cba31d1d 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1870,7 +1870,7 @@ usage(const char *program, const char *version) { "\t \t\ta response is received\n" "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" - "\t \t\t(Sum of one or more of 1,2,4 64, 128, 256 and 512)\n" + "\t \t\t(Sum of one or more of 1,2,4 64, 128 and 512)\n" "\t-N \t\tMake \"observe\" responses NON-confirmable. Even if set\n" "\t \t\tevery fifth response will still be sent as a confirmable\n" "\t \t\tresponse (RFC 7641 requirement)\n" diff --git a/examples/lwip/config/lwippools.h b/examples/lwip/config/lwippools.h index 01598a90..7ef3028b 100644 --- a/examples/lwip/config/lwippools.h +++ b/examples/lwip/config/lwippools.h @@ -155,7 +155,7 @@ LWIP_MALLOC_MEMPOOL_START LWIP_MALLOC_MEMPOOL(MEMP_NUM_COAPSTRING, 64) LWIP_MALLOC_MEMPOOL(4, 256) LWIP_MALLOC_MEMPOOL(2, 512) -LWIP_MALLOC_MEMPOOL(2, 1512) +LWIP_MALLOC_MEMPOOL(4, 1512) LWIP_MALLOC_MEMPOOL_END LWIP_MEMPOOL(COAP_STRING, 1, 1, "COAP_STRING") diff --git a/examples/oscore_testcases.sh b/examples/oscore_testcases.sh index dd468ea2..313d346e 100755 --- a/examples/oscore_testcases.sh +++ b/examples/oscore_testcases.sh @@ -224,7 +224,7 @@ if [ "$SUPPRESS" = no ] ; then fi echo -n "Test 11 - " timeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -m delete coap://$TARGET_IP:$S_PORT_B/oscore/test > /tmp/client_out 2>&1 -passfail 1 "^v:1 t:CON c:2.02 i:" +passfail 1 "^v:1 t:ACK c:2.02 i:" # Test 12 if [ "$SUPPRESS" = no ] ; then diff --git a/include/coap3/coap_block.h b/include/coap3/coap_block.h index 73e49254..11b3fe31 100644 --- a/include/coap3/coap_block.h +++ b/include/coap3/coap_block.h @@ -71,7 +71,6 @@ typedef struct { #define COAP_BLOCK_STLESS_BLOCK2 0x40 /* (svr)Server is stateless for handling Block2 */ #define COAP_BLOCK_NOT_RANDOM_BLOCK1 0x80 /* (svr)Disable server handling random order block1 */ -#define COAP_BLOCK_CACHE_RESPONSE 0x100 /* (svr)Cache CON request's response */ #define COAP_BLOCK_FORCE_Q_BLOCK 0x200 /* force Q-Block method without support check */ /* WARNING: Added defined values must not encroach into 0xff000000 which are defined elsewhere */ diff --git a/include/coap3/coap_block_internal.h b/include/coap3/coap_block_internal.h index c2abc044..030ea3e8 100644 --- a/include/coap3/coap_block_internal.h +++ b/include/coap3/coap_block_internal.h @@ -51,7 +51,6 @@ extern "C" { COAP_BLOCK_STLESS_FETCH | \ COAP_BLOCK_STLESS_BLOCK2 | \ COAP_BLOCK_NOT_RANDOM_BLOCK1 | \ - COAP_BLOCK_CACHE_RESPONSE | \ COAP_BLOCK_FORCE_Q_BLOCK) #else /* ! COAP_Q_BLOCK_SUPPORT */ #define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \ @@ -59,8 +58,7 @@ extern "C" { COAP_BLOCK_NO_PREEMPTIVE_RTAG | \ COAP_BLOCK_STLESS_FETCH | \ COAP_BLOCK_STLESS_BLOCK2 | \ - COAP_BLOCK_NOT_RANDOM_BLOCK1 | \ - COAP_BLOCK_CACHE_RESPONSE) + COAP_BLOCK_NOT_RANDOM_BLOCK1) #endif /* ! COAP_Q_BLOCK_SUPPORT */ #define COAP_BLOCK_MAX_SIZE_MASK 0x7000000 /* (svr)Mask to get the max supported block size */ @@ -250,7 +248,6 @@ struct coap_lg_srcv_t { uint8_t szx; /**< size of individual blocks */ uint16_t block_option; /**< Block option in use */ uint8_t dont_timeout; /**< Set if app handler in use */ - coap_mid_t last_mid; /**< Last received mid for this set of packets */ coap_tick_t last_used; /**< Last time data sent or 0 */ size_t total_len; /**< Length as indicated by SIZE1 option */ coap_binary_t *body_data; /**< Used for re-assembling entire body */ diff --git a/include/coap3/coap_session_internal.h b/include/coap3/coap_session_internal.h index 28f3f66b..7b2e85e3 100644 --- a/include/coap3/coap_session_internal.h +++ b/include/coap3/coap_session_internal.h @@ -240,16 +240,13 @@ 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 + coap_mid_t last_resp_mid; /**< The last response mid that has been been processed */ coap_response_t last_con_handler_res; /**< The result of calling the response handler of the last CON */ #if COAP_SERVER_SUPPORT coap_bin_const_t *client_cid; /**< Contains client CID or NULL */ - coap_pdu_t *cached_pdu; /**< Cached copy of last ACK response PDU */ - coap_digest_t cached_pdu_cksum; /**< Checksum of last CON request PDU */ + coap_pdu_t *last_resp_pdu; /**< Cached copy of last ACK response PDU */ #endif /* COAP_SERVER_SUPPORT */ #if COAP_CLIENT_SUPPORT coap_pdu_t *resp_pdu; /**< PDU returned in coap_send_recv() call */ diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index 0312ad02..66e4f122 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -169,7 +169,6 @@ OPTIONS - General COAP_BLOCK_TRY_Q_BLOCK 4 COAP_BLOCK_STLESS_BLOCK2 64 COAP_BLOCK_NOT_RANDOM_BLOCK1 128 - COAP_BLOCK_CACHE_RESPONSE 256 COAP_BLOCK_FORCE_Q_BLOCK 512 *-N* :: diff --git a/man/coap_block.txt.in b/man/coap_block.txt.in index a69c383d..a5516f98 100644 --- a/man/coap_block.txt.in +++ b/man/coap_block.txt.in @@ -203,7 +203,6 @@ session _block_mode_. #define COAP_BLOCK_STLESS_FETCH 0x20 /* (Client) Assume server supports stateless FETCH */ #define COAP_BLOCK_STLESS_BLOCK2 0x40 /* (Server) Server is stateless for Block2 transfers */ #define COAP_BLOCK_NOT_RANDOM_BLOCK1 0x80 /* (Server) Disable server handling random order block1 */ -#define COAP_BLOCK_CACHE_RESPONSE 0x100 /* (Server) Cache CON request's response */ #define COAP_BLOCK_FORCE_Q_BLOCK 0x200 /* force Q-Block method without support check */ ---- _block_mode_ is an or'd set of zero or more COAP_BLOCK_* definitions. @@ -270,11 +269,6 @@ for Block1 by the client. *NOTE:* This is ignored if Q-Block1 is being used. -If *COAP_BLOCK_CACHE_RESPONSE* is set, then the server will cache the response -for the latest unreliable CON request so that if the CON request is repeated, -the cached response gets re-transmitted. If it is not set, then the response is -sent as a separate response (empty ACK sent first) using CON. - If *COAP_BLOCK_FORCE_Q_BLOCK* is set, libcoap will not initiate a probe request to check if the peer does support Q-Block method as it does if *COAP_BLOCK_TRY_Q_BLOCK* is set and will assume the peer supports Q-Block diff --git a/src/coap_block.c b/src/coap_block.c index 2f9e4317..051b179b 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -3528,7 +3528,6 @@ coap_handle_request_put_block(coap_context_t *context, } #endif /* COAP_Q_BLOCK_SUPPORT */ - lg_srcv->last_mid = pdu->mid; lg_srcv->last_type = pdu->type; update_data = 0; diff --git a/src/coap_net.c b/src/coap_net.c index 0aea0c63..0fcaace5 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -1983,39 +1983,6 @@ error: return COAP_INVALID_MID; } -#if COAP_SERVER_SUPPORT -static int -coap_pdu_cksum(const coap_pdu_t *pdu, coap_digest_t *digest_buffer) { - coap_digest_ctx_t *digest_ctx = coap_digest_setup(); - - if (!digest_ctx || !pdu) { - goto fail; - } - if (pdu->used_size && pdu->token) { - if (!coap_digest_update(digest_ctx, pdu->token, pdu->used_size)) { - goto fail; - } - } - if (!coap_digest_update(digest_ctx, (const uint8_t *)&pdu->type, sizeof(pdu->type))) { - goto fail; - } - if (!coap_digest_update(digest_ctx, (const uint8_t *)&pdu->code, sizeof(pdu->code))) { - goto fail; - } - if (!coap_digest_update(digest_ctx, (const uint8_t *)&pdu->mid, sizeof(pdu->mid))) { - goto fail; - } - if (!coap_digest_final(digest_ctx, digest_buffer)) - return 0; - - return 1; - -fail: - coap_digest_free(digest_ctx); - return 0; -} -#endif /* COAP_SERVER_SUPPORT */ - static int prepend_508_ip(coap_session_t *session, coap_pdu_t *pdu) { char addr_str[INET6_ADDRSTRLEN + 8 + 1]; @@ -2273,15 +2240,13 @@ coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request bytes_written = coap_send_pdu(session, pdu, NULL); #if COAP_SERVER_SUPPORT - if ((session->block_mode & COAP_BLOCK_CACHE_RESPONSE) && - session->cached_pdu != pdu && + if (session->last_resp_pdu != pdu && request_pdu && COAP_PROTO_NOT_RELIABLE(session->proto) && COAP_PDU_IS_REQUEST(request_pdu) && COAP_PDU_IS_RESPONSE(pdu) && pdu->type == COAP_MESSAGE_ACK) { - coap_delete_pdu_lkd(session->cached_pdu); - session->cached_pdu = pdu; - coap_pdu_reference_lkd(session->cached_pdu); - coap_pdu_cksum(request_pdu, &session->cached_pdu_cksum); + coap_delete_pdu_lkd(session->last_resp_pdu); + session->last_resp_pdu = pdu; + coap_pdu_reference_lkd(session->last_resp_pdu); } #endif /* COAP_SERVER_SUPPORT */ @@ -3756,7 +3721,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu int is_proxy_scheme = 0; int skip_hop_limit_check = 0; int resp = 0; - int send_early_empty_ack = 0; coap_string_t *query = NULL; coap_opt_t *observe = NULL; coap_string_t *uri_path = NULL; @@ -4153,25 +4117,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu } } - if ((resource == context->proxy_uri_resource || - (resource == context->unknown_resource && - context->unknown_resource->is_reverse_proxy)) && - COAP_PROTO_NOT_RELIABLE(session->proto) && - pdu->type == COAP_MESSAGE_CON && - !(session->block_mode & COAP_BLOCK_CACHE_RESPONSE)) { - /* Make the proxy response separate and fix response later */ - send_early_empty_ack = 1; - } - if (send_early_empty_ack) { - coap_send_ack_lkd(session, pdu); - if (pdu->mid == session->last_con_mid) { - /* request has already been processed - do not process it again */ - coap_log_debug("Duplicate request with mid=0x%04x - not processed\n", - pdu->mid); - goto drop_it_no_debug; - } - session->last_con_mid = pdu->mid; - } #if COAP_WITH_OBSERVE_PERSIST /* If we are maintaining Observe persist */ if (resource == context->unknown_resource) { @@ -4260,15 +4205,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu } skip_handler: - if (send_early_empty_ack && - response->type == COAP_MESSAGE_ACK) { - /* Response is now separate - convert to CON as needed */ - response->type = COAP_MESSAGE_CON; - /* Check for empty ACK - need to drop as already sent */ - if (response->code == 0) { - goto drop_it_no_debug; - } - } respond = no_response(pdu, response, session, resource); if (respond != RESPONSE_DROP) { #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG) @@ -4464,7 +4400,7 @@ handle_response(coap_context_t *context, coap_session_t *session, /* Check for message duplication */ if (COAP_PROTO_NOT_RELIABLE(session->proto)) { if (rcvd->type == COAP_MESSAGE_CON) { - if (rcvd->mid == session->last_con_mid) { + if (rcvd->mid == session->last_resp_mid) { /* Duplicate response: send ACK/RST, but don't process */ if (session->last_con_handler_res == COAP_RESPONSE_OK) coap_send_ack_lkd(session, rcvd); @@ -4472,14 +4408,13 @@ handle_response(coap_context_t *context, coap_session_t *session, coap_send_rst_lkd(session, rcvd); return; } - session->last_con_mid = rcvd->mid; } else if (rcvd->type == COAP_MESSAGE_ACK) { - if (rcvd->mid == session->last_ack_mid) { + if (rcvd->mid == session->last_resp_mid) { /* Duplicate response */ return; } - session->last_ack_mid = rcvd->mid; } + session->last_resp_mid = rcvd->mid; } /* Check to see if checking out extended token support */ if (session->max_token_checked == COAP_EXT_T_CHECKING && @@ -4724,31 +4659,26 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, #if COAP_SERVER_SUPPORT /* See if this a repeat request */ - if (COAP_PDU_IS_REQUEST(pdu) && session->cached_pdu && - (session->block_mode & COAP_BLOCK_CACHE_RESPONSE)) { - coap_digest_t digest; - - coap_pdu_cksum(pdu, &digest); - if (memcmp(&digest, &session->cached_pdu_cksum, sizeof(digest)) == 0) { + if (COAP_PDU_IS_REQUEST(pdu) && session->last_resp_pdu && + pdu->mid == session->last_resp_pdu->mid) { #if COAP_OSCORE_SUPPORT - uint8_t oscore_encryption = session->oscore_encryption; + uint8_t oscore_encryption = session->oscore_encryption; - session->oscore_encryption = 0; + session->oscore_encryption = 0; #endif /* COAP_OSCORE_SUPPORT */ - /* Account for coap_send_internal() doing a coap_delete_pdu() and - cached_pdu must not be removed */ - coap_pdu_reference_lkd(session->cached_pdu); - coap_log_debug("Retransmit response to duplicate request\n"); - if (coap_send_internal(session, session->cached_pdu, NULL) != COAP_INVALID_MID) { -#if COAP_OSCORE_SUPPORT - session->oscore_encryption = oscore_encryption; -#endif /* COAP_OSCORE_SUPPORT */ - goto finish; - } + /* Account for coap_send_internal() doing a coap_delete_pdu() and + last_resp_pdu must not be removed */ + coap_pdu_reference_lkd(session->last_resp_pdu); + coap_log_debug("Retransmit response to duplicate request\n"); + if (coap_send_internal(session, session->last_resp_pdu, NULL) != COAP_INVALID_MID) { #if COAP_OSCORE_SUPPORT session->oscore_encryption = oscore_encryption; #endif /* COAP_OSCORE_SUPPORT */ + goto finish; } +#if COAP_OSCORE_SUPPORT + session->oscore_encryption = oscore_encryption; +#endif /* COAP_OSCORE_SUPPORT */ } #endif /* COAP_SERVER_SUPPORT */ if (pdu->type == COAP_MESSAGE_NON || pdu->type == COAP_MESSAGE_CON) { diff --git a/src/coap_oscore.c b/src/coap_oscore.c index fffba162..2b325a14 100644 --- a/src/coap_oscore.c +++ b/src/coap_oscore.c @@ -753,28 +753,6 @@ coap_oscore_new_pdu_encrypted_lkd(coap_session_t *session, oscore_delete_association(session, association); association = NULL; - if (!(session->block_mode & COAP_BLOCK_CACHE_RESPONSE)) { - /* - * If this is a response ACK with data, make it a separate response - * by sending an Empty ACK and changing osc_pdu's MID and type. This - * then allows lost response ACK (now CON) with data to be recovered. - */ - if (coap_request == 0 && osc_pdu->type == COAP_MESSAGE_ACK && - COAP_RESPONSE_CLASS(pdu->code) == 2 && - COAP_PROTO_NOT_RELIABLE(session->proto)) { - coap_pdu_t *empty = coap_pdu_init(COAP_MESSAGE_ACK, - 0, - osc_pdu->mid, - 0); - if (empty) { - if (coap_send_internal(session, empty, NULL) != COAP_INVALID_MID) { - osc_pdu->mid = coap_new_message_id_lkd(session); - osc_pdu->type = COAP_MESSAGE_CON; - } - } - } - } - if (!coap_pdu_encode_header(osc_pdu, session->proto)) { goto error; } @@ -2751,6 +2729,10 @@ coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu) { /* AAD overhead */ overhead += AES_CCM_TAG; + /* Extended Token Overhead */ + if (session->max_token_size) + overhead += session->max_token_size - pdu->actual_token.length; + /* End of options marker */ overhead += 1; diff --git a/src/coap_proxy.c b/src/coap_proxy.c index a97ac8ca..a1d12d64 100644 --- a/src/coap_proxy.c +++ b/src/coap_proxy.c @@ -835,9 +835,8 @@ coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent proxy_req->mid = resp_pdu->mid; - /* If sent early ACK, and this is an ACK, need to convert it to CON */ - if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK && - !(session->block_mode & COAP_BLOCK_CACHE_RESPONSE)) { + /* We sent early ACK, and this is an ACK, need to convert it to CON */ + if (COAP_PROTO_NOT_RELIABLE(session->proto) && resp_pdu->type == COAP_MESSAGE_ACK) { resp_pdu->type = COAP_MESSAGE_CON; } diff --git a/src/coap_session.c b/src/coap_session.c index dcd2a63e..0048265d 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -511,8 +511,7 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, #endif /* COAP_Q_BLOCK_SUPPORT */ 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; + session->last_resp_mid = COAP_INVALID_MID; session->last_con_handler_res = COAP_RESPONSE_OK; session->max_token_size = context->max_token_size; /* RFC8974 */ if (session->type != COAP_SESSION_TYPE_CLIENT) @@ -659,7 +658,7 @@ coap_session_free(coap_session_t *session) { coap_delete_bin_const(session->last_token); coap_delete_bin_const(session->echo); #if COAP_SERVER_SUPPORT - coap_delete_pdu_lkd(session->cached_pdu); + coap_delete_pdu_lkd(session->last_resp_pdu); #endif /* COAP_SERVER_SUPPORT */ if (session->app_cb) {