From 44d8643e89db093d6c518cb00fb806fdc1a2a13f Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Tue, 19 Sep 2023 16:14:50 +0200 Subject: [PATCH] Check iotivity-lite stack init before processing requests Check if the iotivity-lite stack is initialized before processing new requests during shutdown. --------- Co-authored-by: Daniel Adam --- api/oc_main.c | 6 ++---- messaging/coap/engine.c | 29 +++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/api/oc_main.c b/api/oc_main.c index 3a65e795be..0d936444ba 100644 --- a/api/oc_main.c +++ b/api/oc_main.c @@ -313,7 +313,7 @@ main_load_resources(void) int oc_main_init(const oc_handler_t *handler) { - if (g_initialized == true) { + if (g_initialized) { return 0; } @@ -367,7 +367,6 @@ oc_main_init(const oc_handler_t *handler) #endif /* OC_SERVER */ OC_DBG("oc_main: stack initialized"); - g_initialized = true; #ifdef OC_CLIENT @@ -420,10 +419,9 @@ oc_main_needs_poll(void) void oc_main_shutdown(void) { - if (g_initialized == false) { + if (!g_initialized) { return; } - g_initialized = false; #if defined(OC_CLIENT) && defined(OC_SERVER) && defined(OC_CLOUD) diff --git a/messaging/coap/engine.c b/messaging/coap/engine.c index 4fdd13badc..a76ec1cb7a 100644 --- a/messaging/coap/engine.c +++ b/messaging/coap/engine.c @@ -488,11 +488,9 @@ coap_receive_blockwise_block2(coap_receive_ctx_t *ctx, const char *href, } static coap_receive_status_t -coap_receive_method_payload(coap_receive_ctx_t *ctx, const char *href, - size_t href_len, const oc_endpoint_t *endpoint) +coap_receive_blockwise(coap_receive_ctx_t *ctx, const char *href, + size_t href_len, const oc_endpoint_t *endpoint) { - assert(ctx->request->code >= COAP_GET && ctx->request->code <= COAP_DELETE); - // block1 and block2 options are expected to be used with UDP protocol if (ctx->block1.enabled && // block1 is expected only for POST/PUT requests @@ -504,14 +502,27 @@ coap_receive_method_payload(coap_receive_ctx_t *ctx, const char *href, ctx->request->code == COAP_GET) { return coap_receive_blockwise_block2(ctx, href, href_len, endpoint); } + + COAP_ERR("unexpected block1 or block2 option(s)"); + return COAP_RECEIVE_ERROR; +} + +static coap_receive_status_t +coap_receive_method_payload(coap_receive_ctx_t *ctx, const char *href, + size_t href_len, const oc_endpoint_t *endpoint) +{ + assert(ctx->request->code >= COAP_GET && ctx->request->code <= COAP_DELETE); + if (ctx->block1.enabled || ctx->block2.enabled) { - COAP_ERR("unexpected block1 and block2 options"); + return coap_receive_blockwise(ctx, href, href_len, endpoint); + } + COAP_DBG("no block options; processing regular request"); + + if (!oc_main_initialized()) { + COAP_DBG("cannot process new requests during shutdown iotivity-lite stack"); return COAP_RECEIVE_ERROR; } - COAP_DBG("no block options; processing regular request"); - const uint8_t *incoming_block; - uint32_t incoming_block_len = coap_get_payload(ctx->request, &incoming_block); #ifdef OC_SECURITY // Drop unsecured (unicast/multicast) requests during reset the device. if (oc_reset_in_progress(endpoint->device) && @@ -521,6 +532,8 @@ coap_receive_method_payload(coap_receive_ctx_t *ctx, const char *href, } #endif /* OC_SECURITY */ + const uint8_t *incoming_block; + uint32_t incoming_block_len = coap_get_payload(ctx->request, &incoming_block); #ifdef OC_TCP bool is_valid_size = ((endpoint->flags & TCP) != 0 &&