From 41afb92141c5396bf4e2665454c963490adf0e4b Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 7 Jun 2022 09:23:49 +0100 Subject: [PATCH] net.c: Update .well-known/core handling to use common logic This removes a lot of code duplication by creating a pseudo resource for .well-known/core that is not included in the .well-known/core output. In particular specific Block2 option handling is no longer required. Updated tests which now include checking that queries work. --- include/coap3/coap_resource_internal.h | 2 +- include/coap3/resource.h | 3 + src/net.c | 244 +++++++------------------ src/resource.c | 20 +- tests/test_wellknown.c | 104 ++--------- 5 files changed, 100 insertions(+), 273 deletions(-) diff --git a/include/coap3/coap_resource_internal.h b/include/coap3/coap_resource_internal.h index 1a653775..7154eff9 100644 --- a/include/coap3/coap_resource_internal.h +++ b/include/coap3/coap_resource_internal.h @@ -136,7 +136,7 @@ void coap_delete_attr(coap_attr_t *attr); coap_print_status_t coap_print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, - coap_opt_t *); + const coap_string_t *); /** @} */ diff --git a/include/coap3/resource.h b/include/coap3/resource.h index 35e75e38..1052c4cf 100644 --- a/include/coap3/resource.h +++ b/include/coap3/resource.h @@ -75,6 +75,9 @@ typedef void (*coap_method_handler_t) */ #define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS 0x4 +/* Place holder for other work - not tested against in this PR */ +#define COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT 0x8 + /** * Creates a new resource object and initializes the link field to the string * @p uri_path. This function returns the new coap_resource_t object. diff --git a/src/net.c b/src/net.c index 47fd2d0b..a39c1f9c 100644 --- a/src/net.c +++ b/src/net.c @@ -2281,166 +2281,72 @@ coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, * Quick hack to determine the size of the resource description for * .well-known/core. */ -COAP_STATIC_INLINE size_t -get_wkc_len(coap_context_t *context, coap_opt_t *query_filter) { +COAP_STATIC_INLINE ssize_t +get_wkc_len(coap_context_t *context, const coap_string_t *query_filter) { unsigned char buf[1]; size_t len = 0; - if (coap_print_wellknown(context, buf, &len, UINT_MAX, query_filter) - & COAP_PRINT_STATUS_ERROR) { + if (coap_print_wellknown(context, buf, &len, UINT_MAX, query_filter) & + COAP_PRINT_STATUS_ERROR) { coap_log(LOG_WARNING, "cannot determine length of /.well-known/core\n"); - return 0; + return -1L; } - coap_log(LOG_DEBUG, "get_wkc_len: coap_print_wellknown() returned %zu\n", len); - return len; } #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4))) -coap_pdu_t * -coap_wellknown_response(coap_context_t *context, coap_session_t *session, - coap_pdu_t *request) { - coap_pdu_t *resp; - coap_opt_iterator_t opt_iter; - size_t len, wkc_len; - uint8_t buf[4]; +static void +free_wellknown_response(coap_session_t *session COAP_UNUSED, void *app_ptr) { + coap_delete_string(app_ptr); +} + +static void +hnd_get_wellknown(coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query, + coap_pdu_t *response) { + size_t len = 0; + coap_string_t *data_string = NULL; int result = 0; - int need_block2 = 0; /* set to 1 if Block2 option is required */ - coap_block_b_t block; - coap_opt_t *query_filter; - size_t offset = 0; - uint8_t *data; + ssize_t wkc_len = get_wkc_len(session->context, query); - resp = coap_pdu_init(request->type == COAP_MESSAGE_CON - ? COAP_MESSAGE_ACK - : COAP_MESSAGE_NON, - COAP_RESPONSE_CODE(205), - request->mid, coap_session_max_pdu_size(session)); - if (!resp) { - coap_log(LOG_DEBUG, "coap_wellknown_response: cannot create PDU\n"); - return NULL; - } + if (wkc_len) { + if (wkc_len < 0) + goto error; + data_string = coap_new_string(wkc_len); + if (!data_string) + goto error; - if (!coap_add_token(resp, request->token_length, request->token)) { - coap_log(LOG_DEBUG, "coap_wellknown_response: cannot add token\n"); - goto error; - } - - query_filter = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter); - wkc_len = get_wkc_len(context, query_filter); - - /* The value of some resources is undefined and get_wkc_len will return 0.*/ - if (wkc_len == 0) { - coap_log(LOG_DEBUG, "coap_wellknown_response: undefined resource\n"); - /* set error code 4.00 Bad Request*/ - resp->code = COAP_RESPONSE_CODE(400); - resp->used_size = resp->token_length; - return resp; - } - - /* check whether the request contains the Block2 option */ - if (coap_get_block_b(session, request, COAP_OPTION_BLOCK2, &block)) { - coap_log(LOG_DEBUG, "create block\n"); - offset = block.num << (block.szx + 4); - if (block.szx > 6) { /* invalid, MUST lead to 4.00 Bad Request */ - resp->code = COAP_RESPONSE_CODE(400); - return resp; - } else if (block.szx > COAP_MAX_BLOCK_SZX) { - block.szx = COAP_MAX_BLOCK_SZX; - block.num = (unsigned int)(offset >> (block.szx + 4)); - } - - need_block2 = 1; - } - - /* Check if there is sufficient space to add Content-Format option - * and data. We do this before adding the Content-Format option to - * avoid sending error responses with that option but no actual - * content. */ - if (resp->max_size && resp->max_size <= resp->used_size + 8) { - coap_log(LOG_DEBUG, "coap_wellknown_response: insufficient storage space\n"); - goto error; - } - - /* check if Block2 option is required even if not requested */ - if (!need_block2 && resp->max_size && resp->max_size - resp->used_size < wkc_len + 1) { - assert(resp->used_size <= resp->max_size); - const size_t payloadlen = resp->max_size - resp->used_size; - /* yes, need block-wise transfer */ - block.num = 0; - block.m = 0; /* the M bit is set by coap_write_block_opt() */ - block.szx = COAP_MAX_BLOCK_SZX; - while (payloadlen < SZX_TO_BYTES(block.szx) + 6) { - if (block.szx == 0) { - coap_log(LOG_DEBUG, - "coap_wellknown_response: message to small even for szx == 0\n"); - goto error; - } else { - block.szx--; - } - } - - need_block2 = 1; - } - - if (need_block2) { - /* Add in a pseudo etag (use wkc_len) in case .well-known/core - changes over time */ - coap_add_option_internal(resp, - COAP_OPTION_ETAG, - coap_encode_var_safe8(buf, sizeof(buf), wkc_len), - buf); - } - - /* Add Content-Format. As we have checked for available storage, - * nothing should go wrong here. */ - assert(coap_encode_var_safe(buf, sizeof(buf), - COAP_MEDIATYPE_APPLICATION_LINK_FORMAT) == 1); - coap_add_option_internal(resp, COAP_OPTION_CONTENT_FORMAT, - coap_encode_var_safe(buf, sizeof(buf), - COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf); - - - /* write Block2 option if necessary */ - if (need_block2) { - if (coap_write_block_b_opt(session, &block, COAP_OPTION_BLOCK2, resp, - wkc_len) < 0) { - coap_log(LOG_DEBUG, - "coap_wellknown_response: cannot add Block2 option\n"); + len = wkc_len; + result = coap_print_wellknown(session->context, data_string->s, &len, 0, + query); + if ((result & COAP_PRINT_STATUS_ERROR) != 0) { + coap_log(LOG_DEBUG, "coap_print_wellknown failed\n"); goto error; } + assert (len <= (size_t)wkc_len); + data_string->length = len; + + if (!coap_add_data_large_response(resource, session, request, response, + query, + COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, + -1, 0, data_string->length, + data_string->s, free_wellknown_response, + data_string)) + goto error; } - - coap_add_option_internal(resp, - COAP_OPTION_SIZE2, - coap_encode_var_safe8(buf, sizeof(buf), wkc_len), - buf); - - len = need_block2 ? - min(SZX_TO_BYTES(block.szx), wkc_len - (block.num << (block.szx + 4))) : - resp->max_size && resp->used_size + wkc_len + 1 > resp->max_size ? - resp->max_size - resp->used_size - 1 : wkc_len; - data = coap_add_data_after(resp, len); - if (!data) { - coap_log(LOG_DEBUG, "coap_wellknown_response: coap_add_data failed\n" ); - goto error; - } - - result = coap_print_wellknown(context, data, &len, offset, query_filter); - if ((result & COAP_PRINT_STATUS_ERROR) != 0) { - coap_log(LOG_DEBUG, "coap_print_wellknown failed\n"); - goto error; - } - - return resp; + response->code = COAP_RESPONSE_CODE(205); + return; error: + coap_delete_string(data_string); /* set error code 5.03 and remove all options and data from response */ - resp->code = COAP_RESPONSE_CODE(503); - resp->used_size = resp->token_length; - return resp; + response->code = COAP_RESPONSE_CODE(503); + response->used_size = response->token_length; + } #endif /* COAP_SERVER_SUPPORT */ @@ -2600,6 +2506,9 @@ static coap_str_const_t coap_default_uri_wellknown = { sizeof(COAP_DEFAULT_URI_WELLKNOWN)-1, (const uint8_t *)COAP_DEFAULT_URI_WELLKNOWN }; +/* Initialized in coap_startup() */ +static coap_resource_t resource_uri_wellknown; + static void handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu) { coap_method_handler_t h = NULL; @@ -2618,6 +2527,9 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu int skip_hop_limit_check = 0; int resp; coap_binary_t token = { pdu->token_length, pdu->token }; + coap_string_t *query = NULL; + coap_opt_t *observe = NULL; + coap_string_t *uri_path = NULL; #ifndef WITHOUT_ASYNC coap_bin_const_t tokenc = { pdu->token_length, pdu->token }; coap_async_t *async; @@ -2769,7 +2681,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu } } - coap_string_t *uri_path = coap_get_uri_path(pdu); + uri_path = coap_get_uri_path(pdu); if (!uri_path) return; @@ -2804,15 +2716,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu resource = context->proxy_uri_resource; } else if (coap_string_equal(uri_path, &coap_default_uri_wellknown)) { /* request for .well-known/core */ - if (pdu->code == COAP_REQUEST_CODE_GET) { /* GET */ - coap_log(LOG_INFO, "create default response for %s\n", - COAP_DEFAULT_URI_WELLKNOWN); - response = coap_wellknown_response(context, session, pdu); - } else { - coap_log(LOG_DEBUG, "method not allowed for .well-known/core\n"); - response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(405), - &opt_filter); - } + resource = &resource_uri_wellknown; } else if ((context->unknown_resource != NULL) && ((size_t)pdu->code - 1 < (sizeof(resource->handler) / sizeof(coap_method_handler_t))) && @@ -2903,12 +2807,11 @@ 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_opt_t *observe = NULL; int observe_action = COAP_OBSERVE_CANCEL; - coap_string_t *query = coap_get_query(pdu); coap_block_b_t block; int added_block = 0; + query = coap_get_query(pdu); /* check for Observe option RFC7641 and RFC8132 */ if (resource->observable && (pdu->code == COAP_REQUEST_CODE_GET || @@ -3012,29 +2915,11 @@ skip_handler: coap_log(LOG_WARNING, "cannot generate response\r\n"); coap_delete_pdu(response); } - response = NULL; } else { - if (coap_string_equal(uri_path, &coap_default_uri_wellknown)) { - /* request for .well-known/core */ - coap_log(LOG_DEBUG, "create default response for %s\n", - COAP_DEFAULT_URI_WELLKNOWN); - response = coap_wellknown_response(context, session, pdu); - coap_log(LOG_DEBUG, "have wellknown response %p\n", (void *)response); - } else - response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(405), - &opt_filter); - - if (response && (no_response(pdu, response, session) != RESPONSE_DROP)) { - coap_mid_t mid = pdu->mid; - if (coap_send_internal(session, response) == COAP_INVALID_MID) - coap_log(LOG_DEBUG, "cannot send response for mid=0x%x\n", mid); - } else { - coap_delete_pdu(response); - } - response = NULL; + resp = 405; + goto fail_response; } - assert(response == NULL); coap_delete_string(uri_path); return; @@ -3042,11 +2927,9 @@ fail_response: response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(resp), &opt_filter); - if (response) { - coap_mid_t mid = pdu->mid; - if (coap_send_internal(session, response) == COAP_INVALID_MID) - coap_log(LOG_WARNING, "cannot send response for mid=0x%x\n", mid); - } + if (response) + goto skip_handler; + coap_delete_string(uri_path); } #endif /* COAP_SERVER_SUPPORT */ @@ -3402,6 +3285,9 @@ static int coap_started = 0; void coap_startup(void) { coap_tick_t now; uint64_t us; + static coap_str_const_t well_known = { sizeof(".well-known/core")-1, + (const uint8_t *)".well-known/core" }; + if (coap_started) return; coap_started = 1; @@ -3417,6 +3303,10 @@ void coap_startup(void) { coap_prng_init((unsigned int)us); coap_memory_init(); coap_dtls_startup(); + memset(&resource_uri_wellknown, 0, sizeof(resource_uri_wellknown)); + resource_uri_wellknown.handler[COAP_REQUEST_GET-1] = hnd_get_wellknown; + resource_uri_wellknown.flags = COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT; + resource_uri_wellknown.uri_path = &well_known; } void coap_cleanup(void) { diff --git a/src/resource.c b/src/resource.c index fddc5705..03f13ba3 100644 --- a/src/resource.c +++ b/src/resource.c @@ -171,11 +171,11 @@ match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_p coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, - coap_opt_t *query_filter COAP_UNUSED) { + const coap_string_t *query_filter COAP_UNUSED) { #else /* not a GCC */ coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, - size_t offset, coap_opt_t *query_filter) { + size_t offset, const coap_string_t *query_filter) { #endif /* GCC */ size_t output_length = 0; unsigned char *p = buf; @@ -200,12 +200,12 @@ coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen #ifndef WITHOUT_QUERY_FILTER /* split query filter, if any */ if (query_filter) { - resource_param.s = coap_opt_value(query_filter); - while (resource_param.length < coap_opt_length(query_filter) - && resource_param.s[resource_param.length] != '=') + resource_param.s = query_filter->s; + while (resource_param.length < query_filter->length && + resource_param.s[resource_param.length] != '=') resource_param.length++; - if (resource_param.length < coap_opt_length(query_filter)) { + if (resource_param.length < query_filter->length) { const coap_str_const_t *rt_attributes; if (resource_param.length == 4 && memcmp(resource_param.s, "href", 4) == 0) @@ -221,11 +221,11 @@ coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen /* rest is query-pattern */ query_pattern.s = - coap_opt_value(query_filter) + resource_param.length + 1; + query_filter->s + resource_param.length + 1; - assert((resource_param.length + 1) <= coap_opt_length(query_filter)); + assert((resource_param.length + 1) <= query_filter->length); query_pattern.length = - coap_opt_length(query_filter) - (resource_param.length + 1); + query_filter->length - (resource_param.length + 1); if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) { query_pattern.s++; @@ -321,7 +321,7 @@ coap_resource_init(coap_str_const_t *uri_path, int flags) { uri_path = coap_new_str_const(null_path->s, null_path->length); } else if (!uri_path) { - /* Do not expecte this, but ... */ + /* Do not expect this, but ... */ uri_path = coap_new_str_const(null_path->s, null_path->length); } diff --git a/tests/test_wellknown.c b/tests/test_wellknown.c index 401400f7..897f6d49 100644 --- a/tests/test_wellknown.c +++ b/tests/test_wellknown.c @@ -163,94 +163,30 @@ t_wellknown3(void) { CU_ASSERT(COAP_PRINT_OUTPUT_LENGTH(result) > 0); } -/* Create wellknown response for request without Block-option. */ static void t_wellknown4(void) { - coap_pdu_t *response; - coap_block_t block; + coap_print_status_t result; + coap_string_t *query; + unsigned char buf[40]; + size_t buflen = sizeof(buf); + char teststr[] = { /* ,;if="one";obs (21 chars) */ + '<', '/', 'a', 'b', 'c', 'd', '>', ';', + 'i', 'f', '=', '"', 'o', 'n', 'e', '"', + ';', 'o', 'b', 's' + }; - response = coap_wellknown_response(ctx, session, pdu); - - CU_ASSERT_PTR_NOT_NULL(response); - - CU_ASSERT(coap_get_block(response, COAP_OPTION_BLOCK2, &block) != 0); - - CU_ASSERT(block.num == 0); - CU_ASSERT(block.m == 1); - CU_ASSERT(1 << (block.szx + 4) - == response->token + response->used_size - response->data); - - coap_delete_pdu(response); + /* Check for the resource added in t_wellknown2 */ + query = coap_new_string(sizeof("if=one")-1); + CU_ASSERT(query != NULL); + memcpy(query->s, "if=one", sizeof("if=one")-1); + result = coap_print_wellknown(ctx, buf, &buflen, 0, query); + CU_ASSERT((result & COAP_PRINT_STATUS_ERROR) == 0 ); + CU_ASSERT(COAP_PRINT_OUTPUT_LENGTH(result) == sizeof(teststr)); + CU_ASSERT(buflen == sizeof(teststr)); + CU_ASSERT(memcmp(buf, teststr, buflen) == 0); + coap_delete_string(query); } -/* Create wellknown response for request with Block2-option and an szx - * value smaller than COAP_MAX_BLOCK_SZX. - */ -static void -t_wellknown5(void) { - coap_pdu_t *response; - coap_block_t inblock = { .num = 1, .m = 0, .szx = 1 }; - coap_block_t block; - unsigned char buf[3]; - - if (!coap_add_option(pdu, COAP_OPTION_BLOCK2, - coap_encode_var_safe(buf, sizeof(buf), - ((inblock.num << 4) | - (inblock.m << 3) | - inblock.szx)), buf)) { - CU_FAIL("cannot add Block2 option"); - return; - } - - response = coap_wellknown_response(ctx, session, pdu); - - CU_ASSERT_PTR_NOT_NULL(response); - - CU_ASSERT(coap_get_block(response, COAP_OPTION_BLOCK2, &block) != 0); - - CU_ASSERT(block.num == inblock.num); - CU_ASSERT(block.m == 1); - CU_ASSERT(1 << (block.szx + 4) - == response->token + response->used_size - response->data); - - coap_delete_pdu(response); -} - -static void -t_wellknown6(void) { - coap_pdu_t *response; - coap_block_t block = { .num = 0, .szx = 6 }; - unsigned char buf[TEST_PDU_SIZE]; - - - do { - coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ - - pdu->type = COAP_MESSAGE_NON; - pdu->code = COAP_REQUEST_CODE_GET; - pdu->mid = 0x1234; - - CU_ASSERT_PTR_NOT_NULL(pdu); - - if (!pdu || !coap_add_option(pdu, COAP_OPTION_BLOCK2, - coap_encode_var_safe(buf, sizeof(buf), - ((block.num << 4) | block.szx)), buf)) { - CU_FAIL("cannot create request"); - return; - } - - response = coap_wellknown_response(ctx, session, pdu); - - CU_ASSERT_PTR_NOT_NULL(response); - - /* coap_show_pdu(LOG_INFO, response); */ - - CU_ASSERT(coap_get_block(response, COAP_OPTION_BLOCK2, &block) != 0); - - block.num++; - coap_delete_pdu(response); - } while (block.m == 1); -} static int t_wkc_tests_create(void) { @@ -334,8 +270,6 @@ t_init_wellknown_tests(void) { WKC_TEST(suite, t_wellknown2); WKC_TEST(suite, t_wellknown3); WKC_TEST(suite, t_wellknown4); - WKC_TEST(suite, t_wellknown5); - WKC_TEST(suite, t_wellknown6); return suite; }