From e008d905fdb6b47678c903c55a8f58548fdedafc Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Fri, 16 Apr 2021 21:05:13 +0100 Subject: [PATCH] Multicast: Add in response delay See https://tools.ietf.org/html/rfc7252#section-8.2 Also updated coap_find_async() to search on token instead of mid as mid is not used in reliable transports. --- examples/coap-server.c | 3 ++- examples/etsi_iot_01.c | 3 ++- include/coap2/async.h | 8 +++---- include/coap2/coap_session.h | 9 +++++++ man/coap_async.txt.in | 7 +++--- src/async.c | 27 ++++++++++++++------- src/net.c | 46 ++++++++++++++++++++++++++---------- 7 files changed, 73 insertions(+), 30 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index adc8594a..02d4bad2 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -366,11 +366,12 @@ hnd_get_async(coap_context_t *context, unsigned long delay = 5; size_t size; coap_async_t *async; + coap_binary_t zero_token = {0, NULL}; /* * See if this is the initial, or delayed request */ - async = coap_find_async(context, session, request->mid); + async = coap_find_async(context, session, token ? *token : zero_token); if (!async) { /* Set up an async request to trigger delay in the future */ if (query) { diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 6200d894..db89b53e 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -360,8 +360,9 @@ hnd_get_separate(coap_context_t *ctx, coap_opt_filter_t f; unsigned long delay = 5; coap_async_t *async; + coap_binary_t zero_token = {0, NULL}; - async = coap_find_async(ctx, session, request->mid); + async = coap_find_async(ctx, session, token ? *token : zero_token); if (!async) { /* Set up an async request to trigger delay in the future */ diff --git a/include/coap2/async.h b/include/coap2/async.h index b2940136..f39f21cd 100644 --- a/include/coap2/async.h +++ b/include/coap2/async.h @@ -77,20 +77,20 @@ void coap_free_async(coap_context_t *context, coap_async_t *async); /** - * Retrieves the object identified by @p mid from the list of asynchronous + * Retrieves the object identified by @p token from the list of asynchronous * transactions that are registered with @p context. This function returns a * pointer to that object or @c NULL if not found. * * @param context The context where the asynchronous objects are registered * with. * @param session The session that is used for asynchronous transmissions. - * @param mid The mid of the object to retrieve. + * @param token The PDU's token of the object to retrieve. * - * @return A pointer to the object identified by @p mid or @c NULL if + * @return A pointer to the object identified by @p token or @c NULL if * not found. */ coap_async_t *coap_find_async(coap_context_t *context, - coap_session_t *session, coap_mid_t mid); + coap_session_t *session, coap_binary_t token); /** * Set the application data pointer held in @p async. This overwrites any diff --git a/include/coap2/coap_session.h b/include/coap2/coap_session.h index 115dee1b..3990bd3b 100644 --- a/include/coap2/coap_session.h +++ b/include/coap2/coap_session.h @@ -415,6 +415,15 @@ coap_session_t *coap_session_get_by_peer(const coap_context_t *ctx, */ #define COAP_DEFAULT_NSTART 1 + /** + * The maximum number of seconds before sending back a response to a + * multicast request. + * RFC 7252, Section 4.8 DEFAULT_LEISURE is 5. + */ +#ifndef COAP_DEFAULT_LEISURE +#define COAP_DEFAULT_LEISURE (5U) +#endif /* COAP_DEFAULT_LEISURE */ + /** * The MAX_TRANSMIT_SPAN definition for the session (s). * diff --git a/man/coap_async.txt.in b/man/coap_async.txt.in index 0737766a..2f7c6b85 100644 --- a/man/coap_async.txt.in +++ b/man/coap_async.txt.in @@ -31,7 +31,7 @@ coap_session_t *_session_, coap_pdu_t *_request_, coap_tick_t _delay_);* *void coap_free_async(coap_context_t *_context_, coap_async_t *_async_);* *coap_async_t *coap_find_async(coap_context_t *_context_, -coap_session_t *_session_, coap_mid_t _mid_);* +coap_session_t *_session_, coap_binary_t _token_);* *void coap_async_set_app_data(coap_async_t *_async_, void *_app_data_);* @@ -75,7 +75,7 @@ then when the response is ready at an indeterminate point in the future, The *coap_free_async*() function is used to delete an _async_ definition. The *coap_find_async*() function is used to determine if there is an async -definition based on the _context_, _session_ and message id _mid_. +definition based on the _context_, _session_ and token _token_. The *coap_async_set_app_data*() function is used to add in user defined _app_data_ to the _async_ definition. It is the responsibility of the @@ -115,11 +115,12 @@ hnd_get_with_delay(coap_context_t *context, unsigned long delay = 5; size_t size; coap_async_t *async; + coap_binary_t zero_token = {0, NULL}; /* * See if this is the initial, or delayed request */ - async = coap_find_async(context, session, request->mid); + async = coap_find_async(context, session, token ? *token : zero_token); if (!async) { /* Set up an async request to trigger delay in the future */ if (query) { diff --git a/src/async.c b/src/async.c index 8b83b32f..5ae871a5 100644 --- a/src/async.c +++ b/src/async.c @@ -1,6 +1,6 @@ /* async.c -- state management for asynchronous messages * - * Copyright (C) 2010,2011 Olaf Bergmann + * Copyright (C) 2010,2011,2021 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -16,13 +16,14 @@ #ifndef WITHOUT_ASYNC /* utlist-style macros for searching pairs in linked lists */ -#define SEARCH_PAIR(head,out,field1,val1,field2,val2) \ - SEARCH_PAIR2(head,out,field1,val1,field2,val2,next) +#define SEARCH_PAIR(head,out,field1,val1,field2,val2,field3,val3) \ + SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next) -#define SEARCH_PAIR2(head,out,field1,val1,field2,val2,next) \ +#define SEARCH_PAIR3(head,out,field1,val1,field2,val2,field3,val3,next) \ do { \ LL_FOREACH2(head,out,next) { \ - if ((out)->field1 == (val1) && (out)->field2 == (val2)) break; \ + if ((out)->field1 == (val1) && (out)->field2 == (val2) && \ + ((val2) == 0 || memcmp((out)->field3, (val3), (val2)) == 0)) break; \ } \ } while(0) @@ -37,7 +38,10 @@ coap_register_async(coap_context_t *context, coap_session_t *session, if (!COAP_PDU_IS_REQUEST(request)) return NULL; - SEARCH_PAIR(context->async_state,s,session,session,pdu->mid,mid); + SEARCH_PAIR(context->async_state, s, + session, session, + pdu->token_length, request->token_length, + pdu->token, request->token); if (s != NULL) { coap_log(LOG_DEBUG, @@ -73,6 +77,8 @@ coap_register_async(coap_context_t *context, coap_session_t *session, coap_ticks(&s->delay); s->delay += delay; } + coap_log(LOG_DEBUG, " %s: Request for delayed for %lu ticks\n", + coap_session_str(session), delay); LL_PREPEND(context->async_state, s); @@ -89,14 +95,19 @@ coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { } else async->delay = 0; + coap_log(LOG_DEBUG, " %s: Request for delayed for %lu ticks\n", + coap_session_str(async->session), delay); } coap_async_t * coap_find_async(coap_context_t *context, coap_session_t *session, - coap_mid_t mid) { + coap_binary_t token) { coap_async_t *tmp; - SEARCH_PAIR(context->async_state,tmp,session,session,pdu->mid,mid); + SEARCH_PAIR(context->async_state, tmp, + session, session, + pdu->token_length, token.length, + pdu->token, token.s); return tmp; } diff --git a/src/net.c b/src/net.c index df60bcbd..5178e09e 100644 --- a/src/net.c +++ b/src/net.c @@ -2491,22 +2491,43 @@ 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; - + coap_binary_t token = { pdu->token_length, pdu->token }; #ifndef WITHOUT_ASYNC - /* Need to check for duplicate request in case of network issues */ - if (!COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_CON) { - coap_async_t *async = coap_find_async(context, session, pdu->mid); + coap_async_t *async; +#endif /* WITHOUT_ASYNC */ - if (async) { - coap_tick_t now; - - coap_ticks(&now); - if (async->delay == 0 || async->delay > now) { - /* re-transmit missing ACK */ - coap_send_ack(session, pdu); - } + if (coap_is_mcast(&session->addr_info.local)) { + if (COAP_PROTO_RELIABLE(session->proto) || pdu->type != COAP_MESSAGE_NON) { + coap_log(LOG_INFO, "Invalid multicast packet received RFC7252 8.1\n"); + return; } } +#ifndef WITHOUT_ASYNC + async = coap_find_async(context, session, token); + if (async) { + coap_tick_t now; + + coap_ticks(&now); + if (async->delay == 0 || async->delay > now) { + /* re-transmit missing ACK (only if CON) */ + coap_log(LOG_INFO, "Retransmit async response\n"); + coap_send_ack(session, pdu); + /* and do not pass on to the upper layers */ + return; + } + } + else if (coap_is_mcast(&session->addr_info.local)) { + uint8_t r; + coap_tick_t delay; + + /* Need to delay sending mcast request to application layer, so response + is not immediate. */ + coap_prng(&r, sizeof(r)); + delay = (COAP_DEFAULT_LEISURE * COAP_TICKS_PER_SECOND * r) / 256; + /* Register request to be internally re-transmitted after delay */ + if (coap_register_async(context, session, pdu, delay)) + return; + } #endif /* WITHOUT_ASYNC */ coap_option_filter_clear(&opt_filter); @@ -2724,7 +2745,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu /* Implementation detail: coap_add_token() immediately returns 0 if response == NULL */ if (coap_add_token(response, pdu->token_length, pdu->token)) { - coap_binary_t token = { pdu->token_length, pdu->token }; coap_opt_t *observe = NULL; int observe_action = COAP_OBSERVE_CANCEL; coap_string_t *query = coap_get_query(pdu);