Skip to content

Commit

Permalink
fixup! Check iotivity-lite stack init before processing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 19, 2023
1 parent 1b61468 commit d9f7f6b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
6 changes: 2 additions & 4 deletions api/oc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 21 additions & 13 deletions messaging/coap/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) &&
Expand All @@ -521,11 +532,8 @@ coap_receive_method_payload(coap_receive_ctx_t *ctx, const char *href,
}
#endif /* OC_SECURITY */

if (!oc_main_initialized()) {
COAP_DBG("cannot process new requests during shutdown iotivity-lite stack");
return COAP_RECEIVE_ERROR;
}

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 &&
Expand Down

0 comments on commit d9f7f6b

Please sign in to comment.