diff --git a/.github/workflows/cmake-linux.yml b/.github/workflows/cmake-linux.yml index 21247335f4..f238eff1ac 100644 --- a/.github/workflows/cmake-linux.yml +++ b/.github/workflows/cmake-linux.yml @@ -54,8 +54,8 @@ jobs: - args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_PKI_ENABLED=OFF" # cloud on (ipv4+tcp on), collections create on - args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON" - # cloud on (ipv4+tcp on), collections create on, custom message buffer size - - args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=1024" + # cloud on (ipv4+tcp on), collections create on, custom message buffer size, custom message buffer pool size, custom app data buffer size, custom app data buffer pool size + - args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=2048 -DOC_INOUT_BUFFER_POOL=4 -DOC_APP_DATA_BUFFER_SIZE=2048 -DOC_APP_DATA_BUFFER_POOL=4" # debug on - args: "-DOC_DEBUG_ENABLED=ON" # debug on, cloud on (ipv4+tcp on) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38a3e58497..822efe1d31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,8 +56,10 @@ if (OC_DEBUG_ENABLED) else() set(OC_LOG_MAXIMUM_LOG_LEVEL "DISABLED" CACHE STRING "Maximum supported log level in compile time.") endif() -set(OC_INOUT_BUFFER_SIZE "" CACHE STRING "Custom buffer size for network messages.") -set(OC_INOUT_BUFFER_POOL_SIZE "" CACHE STRING "Custom pool size of network messages.") +set(OC_INOUT_BUFFER_SIZE "" CACHE STRING "Custom static buffer size for network messages.") +set(OC_INOUT_BUFFER_POOL "" CACHE STRING "Custom static pool size of network messages.") +set(OC_APP_DATA_BUFFER_SIZE "" CACHE STRING "Custom static buffer size for application messages.") +set(OC_APP_DATA_BUFFER_POOL "" CACHE STRING "Custom static size of application messages.") set(PLGD_DEV_TIME_ENABLED OFF CACHE BOOL "Enable plgd time feature.") set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -367,16 +369,34 @@ endif() if (NOT("${OC_INOUT_BUFFER_SIZE}" STREQUAL "")) if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) - message(FATAL_ERROR "Cannot set custom buffer size for network messages without dynamic allocation") + message(FATAL_ERROR "Cannot set custom static buffer size for network messages without dynamic allocation") endif() list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})") list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})") endif() -if (NOT("${OC_INOUT_BUFFER_POOL_SIZE}" STREQUAL "")) + +if (NOT("${OC_INOUT_BUFFER_POOL}" STREQUAL "")) + if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) + message(FATAL_ERROR "Cannot set custom static pool size for network messages without dynamic allocation") + endif() + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_POOL=(${OC_INOUT_BUFFER_POOL})") + list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_POOL=(${OC_INOUT_BUFFER_POOL})") +endif() + +if (NOT("${OC_APP_DATA_BUFFER_SIZE}" STREQUAL "")) + if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) + message(FATAL_ERROR "Cannot set custom static buffer size for application messages without dynamic allocation") + endif() + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_APP_DATA_BUFFER_SIZE=(${OC_APP_DATA_BUFFER_SIZE})") + list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_APP_DATA_BUFFER_SIZE=(${OC_APP_DATA_BUFFER_SIZE})") +endif() + +if (NOT("${OC_APP_DATA_BUFFER_POOL}" STREQUAL "")) if(NOT OC_DYNAMIC_ALLOCATION_ENABLED) - message(FATAL_ERROR "Cannot set custom pool size for network messages without dynamic allocation") + message(FATAL_ERROR "Cannot set custom static pool size for application messages without dynamic allocation") endif() - list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_POOL_SIZE=(${OC_INOUT_BUFFER_POOL_SIZE})") + list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_APP_DATA_BUFFER_POOL=(${OC_APP_DATA_BUFFER_POOL})") + list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_APP_DATA_BUFFER_POOL=(${OC_APP_DATA_BUFFER_POOL})") endif() if(PLGD_DEV_TIME_ENABLED) diff --git a/api/oc_blockwise.c b/api/oc_blockwise.c index f9a0fad39c..fb3068d382 100644 --- a/api/oc_blockwise.c +++ b/api/oc_blockwise.c @@ -391,7 +391,7 @@ oc_blockwise_find_response_buffer(const char *href, size_t href_len, endpoint, method, query, query_len, role); } -const void * +void * oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, uint32_t block_offset, uint32_t requested_block_size, uint32_t *payload_size) @@ -404,7 +404,7 @@ oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, uint32_t block_offset, (uint32_t)(buffer->payload_size - block_offset)); } buffer->next_block_offset = block_offset + *payload_size; - return (const void *)&buffer->buffer[block_offset]; + return (void *)&buffer->buffer[block_offset]; } return NULL; } diff --git a/api/oc_buffer.c b/api/oc_buffer.c index d18093b9e3..10e34e7191 100644 --- a/api/oc_buffer.c +++ b/api/oc_buffer.c @@ -41,13 +41,13 @@ #endif /* OC_DYNAMIC_ALLOCATION */ OC_PROCESS(oc_message_buffer_handler, "OC Message Buffer Handler"); -#ifdef OC_INOUT_BUFFER_POOL_SIZE -OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE); -OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE); -#else /* OC_INOUT_BUFFER_POOL_SIZE */ +#ifdef OC_INOUT_BUFFER_POOL +OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL); +OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL); +#else /* !OC_INOUT_BUFFER_POOL */ OC_MEMB(oc_incoming_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS); OC_MEMB(oc_outgoing_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS); -#endif /* !OC_INOUT_BUFFER_POOL_SIZE */ +#endif /* OC_INOUT_BUFFER_POOL */ static void message_deallocate(oc_message_t *message, struct oc_memb *pool) diff --git a/api/oc_client_api.c b/api/oc_client_api.c index 311a759feb..5e31d1a867 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -75,7 +75,7 @@ dispatch_coap_request(void) #else /* OC_TCP */ if ((long)payload_size > OC_BLOCK_SIZE) { #endif /* !OC_TCP */ - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( g_request_buffer, 0, (uint32_t)OC_BLOCK_SIZE, &block_size); if (payload) { coap_set_payload(g_request, payload, block_size); @@ -219,7 +219,8 @@ prepare_coap_request(oc_client_cb_t *cb) } if (oc_string_len(cb->query) > 0) { - coap_set_header_uri_query(g_request, oc_string(cb->query)); + coap_set_header_uri_query(g_request, oc_string(cb->query), + oc_string_len(cb->query)); } g_dispatch.client_cb = cb; @@ -314,7 +315,7 @@ oc_init_multicast_update(const char *uri, const char *query) coap_set_header_uri_path(g_request, uri, strlen(uri)); if (query) { - coap_set_header_uri_query(g_request, query); + coap_set_header_uri_query(g_request, query, strlen(query)); } return true; diff --git a/api/oc_discovery.c b/api/oc_discovery.c index 389388c4cb..1edd9febf7 100644 --- a/api/oc_discovery.c +++ b/api/oc_discovery.c @@ -637,12 +637,11 @@ oc_core_1_1_discovery_handler(oc_request_t *request, { (void)data; int matches = 0; - size_t device; switch (iface_mask) { case OC_IF_LL: { oc_rep_start_links_array(); - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { matches += process_oic_1_1_device_object(oc_rep_array(links), request, device, false); } @@ -650,7 +649,7 @@ oc_core_1_1_discovery_handler(oc_request_t *request, } break; case OC_IF_BASELINE: { oc_rep_start_links_array(); - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { matches += process_oic_1_1_device_object(oc_rep_array(links), request, device, true); } diff --git a/api/oc_main.c b/api/oc_main.c index c8908b5f3e..33b62b814c 100644 --- a/api/oc_main.c +++ b/api/oc_main.c @@ -251,8 +251,7 @@ oc_get_block_size(void) static void oc_shutdown_all_devices(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_connectivity_shutdown(device); } diff --git a/api/oc_resource_factory.c b/api/oc_resource_factory.c index 53862131a3..a354947bc9 100644 --- a/api/oc_resource_factory.c +++ b/api/oc_resource_factory.c @@ -208,7 +208,8 @@ oc_rt_factory_create_resource(oc_collection_t *collection, } void -oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, oc_rt_factory_t *rf) +oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, + const oc_rt_factory_t *rf) { if (oc_list_remove2(created_res, rtc) == NULL) { /* protection against cyclical call of oc_rt_factory_free_created_resource @@ -231,7 +232,7 @@ oc_fi_factory_free_all_created_resources(void) } oc_rt_created_t * -oc_rt_get_factory_create_for_resource(oc_resource_t *resource) +oc_rt_get_factory_create_for_resource(const oc_resource_t *resource) { oc_rt_created_t *rtc = (oc_rt_created_t *)oc_list_head(created_res); while (rtc) { diff --git a/api/oc_resource_factory.h b/api/oc_resource_factory.h index 334e29be4f..9de3e89adb 100644 --- a/api/oc_resource_factory.h +++ b/api/oc_resource_factory.h @@ -49,11 +49,12 @@ oc_rt_created_t *oc_rt_factory_create_resource(oc_collection_t *collection, size_t device); void oc_rt_factory_free_created_resource(oc_rt_created_t *rtc, - oc_rt_factory_t *rf); + const oc_rt_factory_t *rf); void oc_rt_factory_free_created_resources(size_t device); -oc_rt_created_t *oc_rt_get_factory_create_for_resource(oc_resource_t *resource); +oc_rt_created_t *oc_rt_get_factory_create_for_resource( + const oc_resource_t *resource); void oc_fi_factory_free_all_created_resources(void); diff --git a/api/oc_ri.c b/api/oc_ri.c index 7ac882910f..f900df281c 100644 --- a/api/oc_ri.c +++ b/api/oc_ri.c @@ -1388,29 +1388,43 @@ oc_ri_invoke_coap_entity_handler(const coap_packet_t *request, bool response_state_allocated = false; bool enable_realloc_rep = false; #endif /* OC_DYNAMIC_ALLOCATION */ - if (cur_resource && !bad_request && *ctx.response_state == NULL) { - OC_DBG("creating new block-wise response state"); - *ctx.response_state = oc_blockwise_alloc_response_buffer( - uri_path, uri_path_len, endpoint, method, OC_BLOCKWISE_SERVER, - OC_MIN_APP_DATA_SIZE); + if (cur_resource && !bad_request) { if (*ctx.response_state == NULL) { - OC_ERR("failure to alloc response state"); - bad_request = true; - } else { + OC_DBG("creating new block-wise response state"); + *ctx.response_state = oc_blockwise_alloc_response_buffer( + uri_path, uri_path_len, endpoint, method, OC_BLOCKWISE_SERVER, + OC_MIN_APP_DATA_SIZE); + if (*ctx.response_state == NULL) { + OC_ERR("failure to alloc response state"); + bad_request = true; + } else { #ifdef OC_DYNAMIC_ALLOCATION #ifdef OC_APP_DATA_BUFFER_POOL - if (!request_buffer->block) + if (!(*ctx.response_state)->block) #endif /* OC_APP_DATA_BUFFER_POOL */ - { - response_state_allocated = true; - } + { + response_state_allocated = true; + } +#endif /* OC_DYNAMIC_ALLOCATION */ + if (uri_query_len > 0) { + oc_new_string(&(*ctx.response_state)->uri_query, uri_query, + uri_query_len); + } + response_buffer.buffer = (*ctx.response_state)->buffer; +#ifdef OC_DYNAMIC_ALLOCATION + response_buffer.buffer_size = (*ctx.response_state)->buffer_size; +#else /* !OC_DYNAMIC_ALLOCATION */ + response_buffer.buffer_size = sizeof((*ctx.response_state)->buffer); #endif /* OC_DYNAMIC_ALLOCATION */ - if (uri_query_len > 0) { - oc_new_string(&(*ctx.response_state)->uri_query, uri_query, - uri_query_len); } + } else { + OC_DBG("using existing block-wise response state"); response_buffer.buffer = (*ctx.response_state)->buffer; - response_buffer.buffer_size = OC_MIN_APP_DATA_SIZE; +#ifdef OC_DYNAMIC_ALLOCATION + response_buffer.buffer_size = (*ctx.response_state)->buffer_size; +#else /* !OC_DYNAMIC_ALLOCATION */ + response_buffer.buffer_size = sizeof((*ctx.response_state)->buffer); +#endif /* OC_DYNAMIC_ALLOCATION */ } } #else /* OC_BLOCK_WISE */ diff --git a/api/oc_server_api.c b/api/oc_server_api.c index f00e29bc49..fbdd97e66c 100644 --- a/api/oc_server_api.c +++ b/api/oc_server_api.c @@ -225,7 +225,7 @@ oc_reset_delayed_callback_ms(void *cb_data, oc_trigger_t callback, } bool -oc_has_delayed_callback(void *cb_data, oc_trigger_t callback, +oc_has_delayed_callback(const void *cb_data, oc_trigger_t callback, bool ignore_cb_data) { return oc_ri_has_timed_event_callback(cb_data, callback, ignore_cb_data); @@ -242,7 +242,7 @@ oc_remove_delayed_callback_by_filter(oc_trigger_t cb, } void -oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback) +oc_remove_delayed_callback(const void *cb_data, oc_trigger_t callback) { oc_ri_remove_timed_event_callback(cb_data, callback); } @@ -653,8 +653,7 @@ oc_resource_set_secure_mcast(oc_resource_t *resource, bool supported) void oc_set_con_write_cb(oc_con_write_cb_t callback) { - size_t i; - for (i = 0; i < oc_core_get_num_devices(); i++) { + for (size_t i = 0; i < oc_core_get_num_devices(); i++) { oc_resource_t *res = oc_core_get_resource_by_index(OCF_CON, i); if (res) { res->post_handler.user_data = *(void **)(&callback); @@ -754,7 +753,7 @@ handle_separate_response_request(coap_separate_t *request, response_state->payload_size = (uint32_t)response_buffer->response_length; uint32_t payload_size = 0; - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( response_state, 0, request->block2_size, &payload_size); if (payload != NULL) { coap_set_payload(&response, payload, payload_size); diff --git a/api/unittest/eventcallbacktest.cpp b/api/unittest/eventcallbacktest.cpp index 40f62c8432..4b8ac91561 100644 --- a/api/unittest/eventcallbacktest.cpp +++ b/api/unittest/eventcallbacktest.cpp @@ -471,8 +471,6 @@ class TestObserveCallbackWithServer : public testing::Test { static void SetUpTestCase() { - oc_log_set_level(OC_LOG_LEVEL_DEBUG); - ASSERT_TRUE(oc::TestDevice::StartServer()); #if defined(OC_SERVER) && defined(OC_COLLECTIONS) diff --git a/api/unittest/introspectiontest.cpp b/api/unittest/introspectiontest.cpp index 2e41af7dfe..c537495abc 100644 --- a/api/unittest/introspectiontest.cpp +++ b/api/unittest/introspectiontest.cpp @@ -61,7 +61,6 @@ class TestIntrospectionWithServer : public testing::Test { static void SetUpTestCase() { - oc_log_set_level(OC_LOG_LEVEL_DEBUG); ASSERT_EQ(0, oc::TestStorage.Config()); #ifdef OC_IDD_API diff --git a/api/unittest/ocapitest.cpp b/api/unittest/ocapitest.cpp index f592d93ac6..545019f61d 100644 --- a/api/unittest/ocapitest.cpp +++ b/api/unittest/ocapitest.cpp @@ -47,7 +47,8 @@ #include #endif /* _WIN32 */ -#ifdef OC_DYNAMIC_ALLOCATION +#if defined(OC_DYNAMIC_ALLOCATION) && !defined(OC_INOUT_BUFFER_POOL) && \ + !defined(OC_APP_DATA_BUFFER_POOL) // discovery requests are so large that they only work with dynamic allocation static constexpr uint16_t kMaxWaitTime{ 10 }; @@ -923,4 +924,5 @@ TEST_F(TestObt, DiscoverUnownedResources) } #endif /* OC_SECURITY */ -#endif /* OC_DYNAMIC_ALLOCATION */ +#endif /* OC_DYNAMIC_ALLOCATION && !OC_INOUT_BUFFER_POOL && \ + !OC_APP_DATA_BUFFER_POOL */ diff --git a/api/unittest/plgdtimetest.cpp b/api/unittest/plgdtimetest.cpp index e8f46f922e..13ee1e75bb 100644 --- a/api/unittest/plgdtimetest.cpp +++ b/api/unittest/plgdtimetest.cpp @@ -611,6 +611,8 @@ TEST_F(TestPlgdTimeWithServer, FetchTimeFail) #endif /* OC_SECURITY */ } +#ifndef OC_INOUT_BUFFER_POOL + #if defined(OC_TCP) && defined(OC_SESSION_EVENTS) struct TCPSessionData @@ -801,6 +803,8 @@ TEST_F(TestPlgdTimeWithServer, FetchTimeAlreadyConnectedSecure) #endif /* OC_SECURITY && OC_PKI */ +#endif /* !OC_INOUT_BUFFER_POOL */ + #endif /* OC_CLIENT */ #ifdef OC_SECURITY diff --git a/apps/client_block_linux.c b/apps/client_block_linux.c index 151f9e594e..ae0482b8b6 100644 --- a/apps/client_block_linux.c +++ b/apps/client_block_linux.c @@ -63,7 +63,7 @@ stop_observe(void *data) if (oc_init_post(array_1, array_server, NULL, &post_array, LOW_QOS, NULL)) { for (int i = 0; i < 100; i++) { - large_array[i] = oc_random_value(); + large_array[i] = (int)oc_random_value(); OC_PRINTF("(%d %d) ", i, large_array[i]); } OC_PRINTF("\n"); diff --git a/apps/client_collections_linux.c b/apps/client_collections_linux.c index 51bc45c84c..08042b6307 100644 --- a/apps/client_collections_linux.c +++ b/apps/client_collections_linux.c @@ -79,15 +79,14 @@ post_lights_oic_if_create(oc_client_response_t *data) case OC_REP_STRING: OC_PRINTF("%s\n\n", oc_string(rep->value.string)); break; - case OC_REP_STRING_ARRAY: { - size_t i; - for (i = 0; i < oc_string_array_get_allocated_size(rep->value.array); - i++) { + case OC_REP_STRING_ARRAY: + for (size_t i = 0; + i < oc_string_array_get_allocated_size(rep->value.array); i++) { OC_PRINTF(" %s ", oc_string_array_get_item(rep->value.array, i)); } OC_PRINTF("\n"); - } break; + break; case OC_REP_INT: OC_PRINTF(" %" PRId64 "\n", rep->value.integer); break; diff --git a/apps/cloud_server.c b/apps/cloud_server.c index 64f0af0d53..62127c6870 100644 --- a/apps/cloud_server.c +++ b/apps/cloud_server.c @@ -606,35 +606,36 @@ get_switch_instance(const char *href, oc_string_array_t *types, size_t device) { oc_switch_t *cswitch = (oc_switch_t *)oc_memb_alloc(&switch_s); - if (cswitch) { - cswitch->resource = oc_new_resource( - NULL, href, oc_string_array_get_allocated_size(*types), device); - if (cswitch->resource) { - size_t i; - for (i = 0; i < oc_string_array_get_allocated_size(*types); i++) { - const char *rt = oc_string_array_get_item(*types, i); - oc_resource_bind_resource_type(cswitch->resource, rt); - } - oc_resource_bind_resource_interface(cswitch->resource, iface_mask); - cswitch->resource->properties = bm; - oc_resource_set_default_interface(cswitch->resource, OC_IF_A); - oc_resource_set_request_handler(cswitch->resource, OC_GET, get_cswitch, - cswitch); - oc_resource_set_request_handler(cswitch->resource, OC_DELETE, - delete_cswitch, cswitch); - oc_resource_set_request_handler(cswitch->resource, OC_POST, post_cswitch, - cswitch); - oc_resource_set_properties_cbs(cswitch->resource, get_switch_properties, - cswitch, set_switch_properties, cswitch); - oc_add_resource(cswitch->resource); - oc_set_delayed_callback(cswitch->resource, register_to_cloud, 0); - oc_list_add(switches, cswitch); - return cswitch->resource; - } else { - oc_memb_free(&switch_s, cswitch); - } - } - return NULL; + if (cswitch == NULL) { + printf("ERROR: insufficient memory to add new switch instance"); + return NULL; + } + cswitch->resource = oc_new_resource( + NULL, href, oc_string_array_get_allocated_size(*types), device); + if (cswitch->resource == NULL) { + printf("ERROR: could not create /switch instance"); + oc_memb_free(&switch_s, cswitch); + return NULL; + } + for (size_t i = 0; i < oc_string_array_get_allocated_size(*types); i++) { + const char *rt = oc_string_array_get_item(*types, i); + oc_resource_bind_resource_type(cswitch->resource, rt); + } + oc_resource_bind_resource_interface(cswitch->resource, iface_mask); + cswitch->resource->properties = bm; + oc_resource_set_default_interface(cswitch->resource, OC_IF_A); + oc_resource_set_request_handler(cswitch->resource, OC_GET, get_cswitch, + cswitch); + oc_resource_set_request_handler(cswitch->resource, OC_DELETE, delete_cswitch, + cswitch); + oc_resource_set_request_handler(cswitch->resource, OC_POST, post_cswitch, + cswitch); + oc_resource_set_properties_cbs(cswitch->resource, get_switch_properties, + cswitch, set_switch_properties, cswitch); + oc_add_resource(cswitch->resource); + oc_set_delayed_callback(cswitch->resource, register_to_cloud, 0); + oc_list_add(switches, cswitch); + return cswitch->resource; } static void @@ -891,7 +892,8 @@ simulate_tpm_mbedtls_pk_parse_key(size_t device, mbedtls_pk_context *pk, return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; } uint8_t identity_private_key[4096]; - int ret = fread(identity_private_key, 1, sizeof(identity_private_key), f); + size_t ret = + fread(identity_private_key, 1, sizeof(identity_private_key), f); fclose(f); return mbedtls_pk_parse_key(pk, identity_private_key, ret, NULL, 0, f_rng, p_rng); diff --git a/include/oc_api.h b/include/oc_api.h index b0f1d6f00e..bf8b65fe7b 100644 --- a/include/oc_api.h +++ b/include/oc_api.h @@ -2645,7 +2645,7 @@ void oc_set_delayed_callback_ms_v1(void *cb_data, oc_trigger_t callback, * @return false otherwise */ OC_API -bool oc_has_delayed_callback(void *cb_data, oc_trigger_t callback, +bool oc_has_delayed_callback(const void *cb_data, oc_trigger_t callback, bool ignore_cb_data); /** @@ -2681,7 +2681,7 @@ void oc_remove_delayed_callback_by_filter( * @param[in] callback the delayed callback that is being removed */ OC_API -void oc_remove_delayed_callback(void *cb_data, oc_trigger_t callback); +void oc_remove_delayed_callback(const void *cb_data, oc_trigger_t callback); /** API for setting handlers for interrupts */ diff --git a/include/oc_blockwise.h b/include/oc_blockwise.h index ac7c66a2b9..a7d034d182 100644 --- a/include/oc_blockwise.h +++ b/include/oc_blockwise.h @@ -228,12 +228,12 @@ void oc_blockwise_free_response_buffer(oc_blockwise_state_t *buffer); * @param block_offset the block offset * @param requested_block_size blocksize to be send * @param payload_size the send payload size - * @return const void* + * @return void* */ -const void *oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, - uint32_t block_offset, - uint32_t requested_block_size, - uint32_t *payload_size); +void *oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, + uint32_t block_offset, + uint32_t requested_block_size, + uint32_t *payload_size); /** * @brief handle the incomming block (partial message) diff --git a/messaging/coap/coap.c b/messaging/coap/coap.c index d3e8fdc806..8d0c302071 100644 --- a/messaging/coap/coap.c +++ b/messaging/coap/coap.c @@ -47,6 +47,8 @@ * This file is part of the Contiki operating system. */ +#include "api/oc_buffer_internal.h" + #include "coap.h" #include "coap_internal.h" #include "oc_ri.h" @@ -66,71 +68,85 @@ #include "security/oc_tls_internal.h" #endif /* OC_SECURITY */ +#include #include #include #include /* option format serialization */ -#define COAP_SERIALIZE_INT_OPTION(number, field, text) \ - if (IS_OPTION(coap_pkt, number)) { \ - option_length += coap_serialize_int_option(number, current_number, option, \ - coap_pkt->field); \ - if (option) { \ - OC_DBG(text " [%u]", (unsigned int)coap_pkt->field); \ - option = option_array + option_length; \ - } \ - current_number = number; \ - } -#define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \ - if (IS_OPTION(coap_pkt, number)) { \ - option_length += coap_serialize_array_option(number, current_number, \ - option, coap_pkt->field, \ - coap_pkt->field##_len, '\0'); \ - if (option) { \ - OC_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", \ - (unsigned int)coap_pkt->field##_len, coap_pkt->field[0], \ - coap_pkt->field[1], coap_pkt->field[2], coap_pkt->field[3], \ - coap_pkt->field[4], coap_pkt->field[5], coap_pkt->field[6], \ - coap_pkt->field[7]); /* FIXME always prints 8 bytes */ \ - option = option_array + option_length; \ +#define COAP_SERIALIZE_INT_OPTION(packet, number, field, text) \ + do { \ + if (IS_OPTION(packet, number)) { \ + option_length += coap_serialize_int_option(number, current_number, \ + option, (packet)->field); \ + if (option) { \ + OC_DBG(text " [%u]", (unsigned int)(packet)->field); \ + option = option_array + option_length; \ + } \ + current_number = number; \ } \ - current_number = number; \ - } -#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \ - if (IS_OPTION(coap_pkt, number)) { \ - option_length += coap_serialize_array_option( \ - number, current_number, option, (uint8_t *)coap_pkt->field, \ - coap_pkt->field##_len, splitter); \ - if (option) { \ - OC_DBG(text " [%.*s]", (int)coap_pkt->field##_len, coap_pkt->field); \ - option = option_array + option_length; \ + } while (0) + +#define COAP_SERIALIZE_BYTE_OPTION(packet, number, field, text) \ + do { \ + if (IS_OPTION(packet, number)) { \ + option_length += coap_serialize_array_option( \ + number, current_number, option, (packet)->field, \ + (packet)->field##_len, '\0'); \ + if (option) { \ + OC_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", \ + (unsigned int)(packet)->field##_len, (packet)->field[0], \ + (packet)->field[1], (packet)->field[2], (packet)->field[3], \ + (packet)->field[4], (packet)->field[5], (packet)->field[6], \ + (packet)->field[7]); /* FIXME always prints 8 bytes */ \ + option = option_array + option_length; \ + } \ + current_number = number; \ } \ - current_number = number; \ - } -#define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \ - if (IS_OPTION(coap_pkt, number)) { \ - uint32_t block = coap_pkt->field##_num << 4; \ - if (coap_pkt->field##_more) { \ - block |= 0x8; \ + } while (0) + +#define COAP_SERIALIZE_STRING_OPTION(packet, number, field, splitter, text) \ + do { \ + if (IS_OPTION(packet, number)) { \ + option_length += coap_serialize_array_option( \ + number, current_number, option, (uint8_t *)(packet)->field, \ + (packet)->field##_len, splitter); \ + if (option) { \ + OC_DBG(text " [%.*s]", (int)(packet)->field##_len, (packet)->field); \ + option = option_array + option_length; \ + } \ + current_number = number; \ } \ - block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \ - option_length += \ - coap_serialize_int_option(number, current_number, option, block); \ - if (option) { \ - OC_DBG(text " [%lu%s (%u B/blk)]", (unsigned long)coap_pkt->field##_num, \ - coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \ - OC_DBG(text " encoded: 0x%lX", (unsigned long)block); \ - option = option_array + option_length; \ + } while (0) + +#define COAP_SERIALIZE_BLOCK_OPTION(packet, number, field, text) \ + do { \ + if (IS_OPTION(packet, number)) { \ + uint32_t block = (packet)->field##_num << 4; \ + if ((packet)->field##_more) { \ + block |= 0x8; \ + } \ + block |= 0xF & coap_log_2((packet)->field##_size / 16); \ + option_length += \ + coap_serialize_int_option(number, current_number, option, block); \ + if (option) { \ + OC_DBG(text " [%lu%s (%u B/blk)]", \ + (unsigned long)(packet)->field##_num, \ + (packet)->field##_more ? "+" : "", (packet)->field##_size); \ + OC_DBG(text " encoded: 0x%lX", (unsigned long)block); \ + option = option_array + option_length; \ + } \ + current_number = number; \ } \ - current_number = number; \ - } + } while (0) /*---------------------------------------------------------------------------*/ /*- Variables ---------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -static uint16_t current_mid = 0; +static uint16_t g_current_mid = 0; static coap_status_t g_coap_status_code = COAP_NO_ERROR; + /*---------------------------------------------------------------------------*/ /*- Local helper functions --------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -146,7 +162,7 @@ coap_log_2(uint16_t value) return (result - 1); } -/*---------------------------------------------------------------------------*/ + static uint32_t coap_parse_int_option(const uint8_t *bytes, size_t length) { @@ -159,19 +175,19 @@ coap_parse_int_option(const uint8_t *bytes, size_t length) } return var; } -/*---------------------------------------------------------------------------*/ + static uint8_t coap_option_nibble(size_t value) { if (value < 13) { return (uint8_t)value; - } else if (value <= 0xFF + 13) { + } + if (value <= 0xFF + 13) { return 13; - } else { - return 14; } + return 14; } -/*---------------------------------------------------------------------------*/ + size_t coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer) { @@ -219,7 +235,7 @@ coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer) return ++written; } -/*---------------------------------------------------------------------------*/ + static size_t coap_serialize_int_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint32_t value) @@ -270,7 +286,7 @@ coap_serialize_int_option(unsigned int number, unsigned int current_number, } return i; } -/*---------------------------------------------------------------------------*/ + static size_t coap_serialize_array_option(unsigned int number, unsigned int current_number, uint8_t *buffer, uint8_t *array, size_t length, @@ -338,7 +354,7 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number, return i; } -/*---------------------------------------------------------------------------*/ + static void coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t option_len, char separator) @@ -359,7 +375,7 @@ coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, *dst_len = option_len; } } -/*---------------------------------------------------------------------------*/ + #if 0 static int coap_get_variable(const char *buffer, size_t length, const char *name, @@ -396,45 +412,45 @@ coap_get_variable(const char *buffer, size_t length, const char *name, return 0; } #endif -/*---------------------------------------------------------------------------*/ + #ifdef OC_TCP static size_t -coap_serialize_signal_options(void *packet, uint8_t *option_array) +coap_serialize_signal_options(const coap_packet_t *packet, + uint8_t *option_array) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; uint8_t *option = option_array; unsigned int current_number = 0; size_t option_length = 0; - switch (coap_pkt->code) { + switch (packet->code) { case CSM_7_01: - COAP_SERIALIZE_INT_OPTION(COAP_SIGNAL_OPTION_MAX_MSG_SIZE, max_msg_size, - "Max-Message-Size"); - if (coap_pkt->blockwise_transfer) { - COAP_SERIALIZE_INT_OPTION( - COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER, - blockwise_transfer - coap_pkt->blockwise_transfer, "Bert"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_SIGNAL_OPTION_MAX_MSG_SIZE, + max_msg_size, "Max-Message-Size"); + if (packet->blockwise_transfer) { + COAP_SERIALIZE_INT_OPTION(packet, COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER, + blockwise_transfer - packet->blockwise_transfer, + "Bert"); } break; case PING_7_02: case PONG_7_03: - if (coap_pkt->custody) { - COAP_SERIALIZE_INT_OPTION(COAP_SIGNAL_OPTION_CUSTODY, - custody - coap_pkt->custody, "Custody"); + if (packet->custody) { + COAP_SERIALIZE_INT_OPTION(packet, COAP_SIGNAL_OPTION_CUSTODY, + custody - packet->custody, "Custody"); } break; case RELEASE_7_04: - COAP_SERIALIZE_STRING_OPTION(COAP_SIGNAL_OPTION_ALT_ADDR, alt_addr, '\0', - "Alternative-Address"); - COAP_SERIALIZE_INT_OPTION(COAP_SIGNAL_OPTION_HOLD_OFF, hold_off, + COAP_SERIALIZE_STRING_OPTION(packet, COAP_SIGNAL_OPTION_ALT_ADDR, alt_addr, + '\0', "Alternative-Address"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_SIGNAL_OPTION_HOLD_OFF, hold_off, "Hold-off"); break; case ABORT_7_05: - COAP_SERIALIZE_INT_OPTION(COAP_SIGNAL_OPTION_BAD_CSM, bad_csm_opt, + COAP_SERIALIZE_INT_OPTION(packet, COAP_SIGNAL_OPTION_BAD_CSM, bad_csm_opt, "Bad-CSM-Option"); break; default: - OC_ERR("unknown signal message.[%u]", coap_pkt->code); + OC_ERR("unknown signal message.[%u]", packet->code); return 0; } @@ -445,20 +461,55 @@ coap_serialize_signal_options(void *packet, uint8_t *option_array) return option_length; } #endif /* OC_TCP */ -/*---------------------------------------------------------------------------*/ + +static size_t +coap_serialize_accept_option(uint16_t accept, unsigned int current_number, + uint8_t *buffer) +{ + if (accept == APPLICATION_VND_OCF_CBOR) { + return coap_serialize_int_option(OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER, + current_number, buffer, OCF_VER_1_0_0); + } +#ifdef OC_SPEC_VER_OIC + if (accept == APPLICATION_CBOR) { + return coap_serialize_int_option(OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER, + current_number, buffer, OIC_VER_1_1_0); + } +#endif /* OC_SPEC_VER_OIC */ + return 0; +} + +static size_t +coap_serialize_content_format_option(uint16_t content_format, + unsigned int current_number, + uint8_t *buffer) + +{ + if (content_format == APPLICATION_VND_OCF_CBOR) { + return coap_serialize_int_option(OCF_OPTION_CONTENT_FORMAT_VER, + current_number, buffer, OCF_VER_1_0_0); + } +#ifdef OC_SPEC_VER_OIC + if (content_format == APPLICATION_CBOR) { + return coap_serialize_int_option(OCF_OPTION_CONTENT_FORMAT_VER, + current_number, buffer, OIC_VER_1_1_0); + } +#endif /* OC_SPEC_VER_OIC */ + return 0; +} + /* It just caculates size of option when option_array is NULL */ static size_t -coap_serialize_options(void *packet, uint8_t *option_array, bool inner, +coap_serialize_options(coap_packet_t *packet, uint8_t *option_array, bool inner, bool outer, bool oscore) { (void)oscore; - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; uint8_t *option = option_array; unsigned int current_number = 0; size_t option_length = 0; #if OC_DBG_IS_ENABLED - if (option) { + if (option != NULL) { OC_DBG("Serializing options at %p", (void *)option); } else { OC_DBG("Calculating size of options"); @@ -473,118 +524,91 @@ coap_serialize_options(void *packet, uint8_t *option_array, bool inner, #if 0 /* The options must be serialized in the order of their number */ - COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match"); + COAP_SERIALIZE_BYTE_OPTION(packet, COAP_OPTION_IF_MATCH, if_match, "If-Match"); #endif if (outer) { - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0', + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_URI_HOST, uri_host, '\0', "Uri-Host"); } if (inner) { - COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag"); + COAP_SERIALIZE_BYTE_OPTION(packet, COAP_OPTION_ETAG, etag, "ETag"); } #if 0 - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH, - content_format - coap_pkt-> content_format /* hack to get a zero field */, + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_IF_NONE_MATCH, + content_format - packet-> content_format /* hack to get a zero field */, "If-None-Match"); #endif - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_OBSERVE, observe, "Observe"); if (outer) { - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_URI_PORT, uri_port, + "Uri-Port"); } #if 0 - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_PATH, location_path, + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_LOCATION_PATH, location_path, '/', "Location-Path"); #endif #if defined(OC_OSCORE) && defined(OC_SECURITY) - if (oscore && outer && IS_OPTION(coap_pkt, COAP_OPTION_OSCORE)) { + if (oscore && outer && IS_OPTION(packet, COAP_OPTION_OSCORE)) { option_length += - coap_serialize_oscore_option(¤t_number, coap_pkt, option); + coap_serialize_oscore_option(¤t_number, packet, option); if (option) { option = option_array + option_length; } } #endif /* OC_OSCORE && OC_SECURITY */ if (inner) { - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/', + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_URI_PATH, uri_path, '/', "Uri-Path"); if (option) { - OC_DBG("Serialize content format: %d", coap_pkt->content_format); + OC_DBG("Serialize content format: %d", packet->content_format); } - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format, - "Content-Format"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_CONTENT_FORMAT, + content_format, "Content-Format"); } if (outer) { - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_MAX_AGE, max_age, "Max-Age"); } if (inner) { - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query, '&', + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_URI_QUERY, uri_query, '&', "Uri-Query"); - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_ACCEPT, accept, "Accept"); } #if 0 - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query, + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_LOCATION_QUERY, location_query, '&', "Location-Query"); #endif if (inner) { - COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2"); - COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1"); - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2, "Size2"); + COAP_SERIALIZE_BLOCK_OPTION(packet, COAP_OPTION_BLOCK2, block2, "Block2"); + COAP_SERIALIZE_BLOCK_OPTION(packet, COAP_OPTION_BLOCK1, block1, "Block1"); + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_SIZE2, size2, "Size2"); } if (outer) { - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0', + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_PROXY_URI, proxy_uri, '\0', "Proxy-Uri"); } #if 0 - COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme, + COAP_SERIALIZE_STRING_OPTION(packet, COAP_OPTION_PROXY_SCHEME, proxy_scheme, '\0', "Proxy-Scheme"); #endif if (inner) { - COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1"); - } - - if (inner && IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) { - if (coap_pkt->accept == APPLICATION_VND_OCF_CBOR) { + COAP_SERIALIZE_INT_OPTION(packet, COAP_OPTION_SIZE1, size1, "Size1"); + if (IS_OPTION(packet, COAP_OPTION_ACCEPT)) { option_length += - coap_serialize_int_option(OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER, - current_number, option, OCF_VER_1_0_0); - if (option) { - option = option_array + option_length; - } - } -#ifdef OC_SPEC_VER_OIC - else if (coap_pkt->accept == APPLICATION_CBOR) { - - option_length += - coap_serialize_int_option(OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER, - current_number, option, OIC_VER_1_1_0); - if (option) { - option = option_array + option_length; - } - } -#endif /* OC_SPEC_VER_OIC */ - - current_number = OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER; - } - if (inner && IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) { - if (coap_pkt->content_format == APPLICATION_VND_OCF_CBOR) { - - option_length += coap_serialize_int_option( - OCF_OPTION_CONTENT_FORMAT_VER, current_number, option, OCF_VER_1_0_0); + coap_serialize_accept_option(packet->accept, current_number, option); if (option) { option = option_array + option_length; } + current_number = OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER; } -#ifdef OC_SPEC_VER_OIC - else if (coap_pkt->content_format == APPLICATION_CBOR) { - option_length += coap_serialize_int_option( - OCF_OPTION_CONTENT_FORMAT_VER, current_number, option, OIC_VER_1_1_0); + if (IS_OPTION(packet, COAP_OPTION_CONTENT_FORMAT)) { + option_length += coap_serialize_content_format_option( + packet->content_format, current_number, option); if (option) { option = option_array + option_length; } } -#endif /* OC_SPEC_VER_OIC */ } if (option) { @@ -593,351 +617,344 @@ coap_serialize_options(void *packet, uint8_t *option_array, bool inner, return option_length; } -/*---------------------------------------------------------------------------*/ + #ifdef OC_TCP static coap_status_t -coap_parse_signal_options(void *packet, unsigned int option_number, +coap_parse_signal_options(coap_packet_t *packet, unsigned int option_number, uint8_t *current_option, size_t option_length, bool inner) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; if (!inner) { return BAD_OPTION_4_02; } - switch (coap_pkt->code) { + switch (packet->code) { case CSM_7_01: if (option_number == COAP_SIGNAL_OPTION_MAX_MSG_SIZE) { - coap_pkt->max_msg_size = + packet->max_msg_size = coap_parse_int_option(current_option, option_length); - OC_DBG(" Max-Message-Size [%" PRIu32 "]", coap_pkt->max_msg_size); + OC_DBG(" Max-Message-Size [%" PRIu32 "]", packet->max_msg_size); } else if (option_number == COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER) { - coap_pkt->blockwise_transfer = 1; - OC_DBG(" Bert [%u]", coap_pkt->blockwise_transfer); + packet->blockwise_transfer = 1; + OC_DBG(" Bert [%u]", packet->blockwise_transfer); } break; case PING_7_02: case PONG_7_03: if (option_number == COAP_SIGNAL_OPTION_CUSTODY) { - coap_pkt->custody = 1; - OC_DBG(" Custody [%u]", coap_pkt->custody); + packet->custody = 1; + OC_DBG(" Custody [%u]", packet->custody); } break; case RELEASE_7_04: if (option_number == COAP_SIGNAL_OPTION_ALT_ADDR) { - coap_pkt->alt_addr = (char *)current_option; - coap_pkt->alt_addr_len = option_length; - OC_DBG(" Alternative-Address [%.*s]", (int)coap_pkt->alt_addr_len, - coap_pkt->alt_addr); + packet->alt_addr = (char *)current_option; + packet->alt_addr_len = option_length; + OC_DBG(" Alternative-Address [%.*s]", (int)packet->alt_addr_len, + packet->alt_addr); } else if (option_number == COAP_SIGNAL_OPTION_HOLD_OFF) { - coap_pkt->hold_off = coap_parse_int_option(current_option, option_length); - OC_DBG(" Hold-Off [%" PRIu32 "]", coap_pkt->hold_off); + packet->hold_off = coap_parse_int_option(current_option, option_length); + OC_DBG(" Hold-Off [%" PRIu32 "]", packet->hold_off); } break; case ABORT_7_05: if (option_number == COAP_SIGNAL_OPTION_BAD_CSM) { - coap_pkt->bad_csm_opt = + packet->bad_csm_opt = (uint16_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Bad-CSM-Option [%u]", coap_pkt->bad_csm_opt); + OC_DBG(" Bad-CSM-Option [%u]", packet->bad_csm_opt); } break; default: - OC_ERR("unknown signal message.[%u]", coap_pkt->code); + OC_ERR("unknown signal message.[%u]", packet->code); return BAD_REQUEST_4_00; } return COAP_NO_ERROR; } #endif /* OC_TCP */ -/*---------------------------------------------------------------------------*/ static coap_status_t -coap_oscore_parse_option(coap_packet_t *const coap_pkt, uint8_t *current_option, +coap_oscore_parse_inner_option(coap_packet_t *packet, + unsigned int option_number, uint8_t *option, + size_t option_length, bool validate) +{ + switch (option_number) { + case COAP_OPTION_CONTENT_FORMAT: { + uint16_t content_format = + (uint16_t)coap_parse_int_option(option, option_length); + OC_DBG(" Content-Format [%u]", content_format); + if (content_format != APPLICATION_VND_OCF_CBOR +#ifdef OC_SPEC_VER_OIC + && content_format != APPLICATION_CBOR +#endif /* OC_SPEC_VER_OIC */ + ) { + return UNSUPPORTED_MEDIA_TYPE_4_15; + } + packet->content_format = content_format; + return COAP_NO_ERROR; + } + case COAP_OPTION_ETAG: + packet->etag_len = (uint8_t)MIN(COAP_ETAG_LEN, option_length); + memcpy(packet->etag, option, packet->etag_len); + OC_DBG(" ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", packet->etag_len, + packet->etag[0], packet->etag[1], packet->etag[2], packet->etag[3], + packet->etag[4], packet->etag[5], packet->etag[6], + packet->etag[7]); /*FIXME always prints 8 bytes */ + return COAP_NO_ERROR; + case COAP_OPTION_ACCEPT: { + uint16_t accept = (uint16_t)coap_parse_int_option(option, option_length); + OC_DBG(" Accept [%u]", accept); + if (accept != APPLICATION_VND_OCF_CBOR +#ifdef OC_SPEC_VER_OIC + && accept != APPLICATION_CBOR +#endif /* OC_SPEC_VER_OIC */ +#ifdef OC_WKCORE + && accept != APPLICATION_LINK_FORMAT +#endif /* OC_SPEC_VER_OIC */ +#ifdef OC_CBOR + && accept != APPLICATION_CBOR +#endif /* OC_SPEC_VER_OIC */ + ) { + return NOT_ACCEPTABLE_4_06; + } + packet->accept = accept; + return COAP_NO_ERROR; + } + case COAP_OPTION_URI_PATH: + if (validate) { + return COAP_NO_ERROR; + } + /* coap_merge_multi_option() operates in-place on the IPBUF, but final + * packet field should be const string -> cast to string */ + coap_merge_multi_option((char **)&(packet->uri_path), + &(packet->uri_path_len), option, option_length, + '/'); + OC_DBG(" Uri-Path [%.*s]", (int)packet->uri_path_len, packet->uri_path); + return COAP_NO_ERROR; + case COAP_OPTION_URI_QUERY: + if (validate) { + return COAP_NO_ERROR; + } + /* coap_merge_multi_option() operates in-place on the IPBUF, but final + * packet field should be const string -> cast to string */ + coap_merge_multi_option((char **)&(packet->uri_query), + &(packet->uri_query_len), option, option_length, + '&'); + OC_DBG(" Uri-Query [%.*s]", (int)packet->uri_query_len, packet->uri_query); + return COAP_NO_ERROR; + case COAP_OPTION_BLOCK2: + packet->block2_num = coap_parse_int_option(option, option_length); + packet->block2_more = (packet->block2_num & 0x08) >> 3; + packet->block2_size = (uint16_t)(16 << (packet->block2_num & 0x07)); + packet->block2_offset = (packet->block2_num & ~0x0000000F) + << (packet->block2_num & 0x07); + packet->block2_num >>= 4; + OC_DBG(" Block2 [%lu%s (%u B/blk)]", (unsigned long)packet->block2_num, + packet->block2_more ? "+" : "", packet->block2_size); + return COAP_NO_ERROR; + case COAP_OPTION_BLOCK1: + packet->block1_num = coap_parse_int_option(option, option_length); + packet->block1_more = (packet->block1_num & 0x08) >> 3; + packet->block1_size = (uint16_t)(16 << (packet->block1_num & 0x07)); + packet->block1_offset = (packet->block1_num & ~0x0000000F) + << (packet->block1_num & 0x07); + packet->block1_num >>= 4; + OC_DBG(" Block1 [%lu%s (%u B/blk)]", (unsigned long)packet->block1_num, + packet->block1_more ? "+" : "", packet->block1_size); + return COAP_NO_ERROR; + case COAP_OPTION_SIZE2: + packet->size2 = coap_parse_int_option(option, option_length); + OC_DBG(" Size2 [%lu]", (unsigned long)packet->size2); + return COAP_NO_ERROR; + case COAP_OPTION_SIZE1: + packet->size1 = coap_parse_int_option(option, option_length); + OC_DBG(" Size1 [%lu]", (unsigned long)packet->size1); + return COAP_NO_ERROR; + case OCF_OPTION_CONTENT_FORMAT_VER: + case OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER: { + uint16_t version = (uint16_t)coap_parse_int_option(option, option_length); + OC_DBG(" Content-format/accept-Version: [%u]", version); + if (version < OCF_VER_1_0_0 +#ifdef OC_SPEC_VER_OIC + && version != OIC_VER_1_1_0 +#endif /* OC_SPEC_VER_OIC */ + ) { + OC_WRN("Unsupported version %d %u", option_number, version); + return UNSUPPORTED_MEDIA_TYPE_4_15; + } + return COAP_NO_ERROR; + } +#if 0 + case COAP_OPTION_IF_MATCH: + /* TODO support multiple ETags */ + packet->if_match_len = MIN(COAP_ETAG_LEN, option_length); + memcpy(packet->if_match, option, packet->if_match_len); + OC_DBG("If-Match %u", packet->if_match_len); + OC_LOGbytes(packet->if_match, packet->if_match_len); + return COAP_NO_ERROR; + case COAP_OPTION_IF_NONE_MATCH: + packet->if_none_match = 1; + OC_DBG("If-None-Match"); + return COAP_NO_ERROR; + case COAP_OPTION_LOCATION_PATH: + if (validate) { + return COAP_NO_ERROR; + } + /* coap_merge_multi_option() operates in-place on the IPBUF, but final + * packet field should be const string -> cast to string */ + coap_merge_multi_option((char **)&(packet->location_path), + &(packet->location_path_len), option, option_length, + '/'); + OC_DBG("Location-Path [%.*s]", (int)packet->location_path_len, + packet->location_path); + return COAP_NO_ERROR; + case COAP_OPTION_LOCATION_QUERY: + if (validate) { + return COAP_NO_ERROR; + } + /* coap_merge_multi_option() operates in-place on the IPBUF, but final + * packet field should be const string -> cast to string */ + coap_merge_multi_option((char **)&(packet->location_query), + &(packet->location_query_len), option, + option_length, '&'); + OC_DBG("Location-Query [%.*s]", (int)packet->location_query_len, + packet->location_query); + return COAP_NO_ERROR; +#endif + } + + return BAD_OPTION_4_02; +} + +static coap_status_t +coap_oscore_parse_option(coap_packet_t *packet, uint8_t *current_option, bool inner, bool outer, bool oscore, bool validate, unsigned int option_number, size_t option_length) { (void)oscore; #ifdef OC_TCP - if (coap_check_signal_message(coap_pkt)) { - return coap_parse_signal_options(coap_pkt, option_number, current_option, + if (coap_check_signal_message(packet)) { + return coap_parse_signal_options(packet, option_number, current_option, option_length, inner); - } else + } #endif /* OC_TCP */ - { - switch (option_number) { + + switch (option_number) { #if defined(OC_OSCORE) && defined(OC_SECURITY) - case COAP_OPTION_OSCORE: - if (!outer || !oscore) { - return BAD_OPTION_4_02; - } - if (coap_parse_oscore_option(coap_pkt, current_option, option_length) != - 0) { - return BAD_OPTION_4_02; - } - break; + case COAP_OPTION_OSCORE: + if (!outer || !oscore) { + return BAD_OPTION_4_02; + } + if (coap_parse_oscore_option(packet, current_option, option_length) != 0) { + return BAD_OPTION_4_02; + } + break; #endif /* OC_OSCORE && OC_SECURITY */ - case COAP_OPTION_CONTENT_FORMAT: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->content_format = - (uint16_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Content-Format [%u]", coap_pkt->content_format); - if (coap_pkt->content_format != APPLICATION_VND_OCF_CBOR -#ifdef OC_SPEC_VER_OIC - && coap_pkt->content_format != APPLICATION_CBOR -#endif /* OC_SPEC_VER_OIC */ - ) - return UNSUPPORTED_MEDIA_TYPE_4_15; - break; - case COAP_OPTION_MAX_AGE: - coap_pkt->max_age = coap_parse_int_option(current_option, option_length); - OC_DBG(" Max-Age [%lu]", (unsigned long)coap_pkt->max_age); - break; - case COAP_OPTION_ETAG: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->etag_len = (uint8_t)MIN(COAP_ETAG_LEN, option_length); - memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len); - OC_DBG(" ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", - coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1], - coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4], - coap_pkt->etag[5], coap_pkt->etag[6], - coap_pkt->etag[7]); /*FIXME always prints 8 bytes */ - break; - case COAP_OPTION_ACCEPT: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->accept = - (uint16_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Accept [%u]", coap_pkt->accept); - if (coap_pkt->accept != APPLICATION_VND_OCF_CBOR -#ifdef OC_SPEC_VER_OIC - && coap_pkt->accept != APPLICATION_CBOR -#endif /* OC_SPEC_VER_OIC */ -#ifdef OC_WKCORE - && coap_pkt->accept != APPLICATION_LINK_FORMAT -#endif /* OC_SPEC_VER_OIC */ -#ifdef OC_CBOR - && coap_pkt->accept != APPLICATION_CBOR -#endif /* OC_SPEC_VER_OIC */ - ) - return NOT_ACCEPTABLE_4_06; - break; - case COAP_OPTION_PROXY_URI: - if (!outer) { - return BAD_OPTION_4_02; - } - if (validate) { - break; - } - /* coap_merge_multi_option() operates in-place on the IPBUF, but final - * packet field should be const string -> cast to string */ - coap_merge_multi_option((char **)&(coap_pkt->proxy_uri), - &(coap_pkt->proxy_uri_len), current_option, - option_length, '\0'); - OC_DBG("Proxy-Uri [%.*s]", (int)coap_pkt->proxy_uri_len, - coap_pkt->proxy_uri); - break; + case COAP_OPTION_CONTENT_FORMAT: + case COAP_OPTION_ETAG: + case COAP_OPTION_ACCEPT: + case COAP_OPTION_URI_PATH: + case COAP_OPTION_URI_QUERY: + case COAP_OPTION_BLOCK2: + case COAP_OPTION_BLOCK1: + case COAP_OPTION_SIZE2: + case COAP_OPTION_SIZE1: + case OCF_OPTION_CONTENT_FORMAT_VER: + case OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER: #if 0 - case COAP_OPTION_IF_MATCH: - if (!inner) { - return BAD_OPTION_4_02; - } - /* TODO support multiple ETags */ - coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length); - memcpy(coap_pkt->if_match, current_option, - coap_pkt->if_match_len); - OC_DBG("If-Match %u", coap_pkt->if_match_len); - OC_LOGbytes(coap_pkt->if_match, coap_pkt->if_match_len); - break; - case COAP_OPTION_IF_NONE_MATCH: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->if_none_match = 1; - OC_DBG("If-None-Match"); + case COAP_OPTION_IF_MATCH: + case COAP_OPTION_IF_NONE_MATCH: + case COAP_OPTION_LOCATION_PATH: + case COAP_OPTION_LOCATION_QUERY: +#endif + { + if (!inner) { + return BAD_OPTION_4_02; + } + coap_status_t ret = coap_oscore_parse_inner_option( + packet, option_number, current_option, option_length, validate); + if (ret != COAP_NO_ERROR) { + return ret; + } + break; + } + case COAP_OPTION_MAX_AGE: + packet->max_age = coap_parse_int_option(current_option, option_length); + OC_DBG(" Max-Age [%lu]", (unsigned long)packet->max_age); + break; + case COAP_OPTION_PROXY_URI: + if (!outer) { + return BAD_OPTION_4_02; + } + if (validate) { break; + } + /* coap_merge_multi_option() operates in-place on the IPBUF, but final + * packet field should be const string -> cast to string */ + coap_merge_multi_option((char **)&(packet->proxy_uri), + &(packet->proxy_uri_len), current_option, + option_length, '\0'); + OC_DBG("Proxy-Uri [%.*s]", (int)packet->proxy_uri_len, packet->proxy_uri); + break; +#if 0 case COAP_OPTION_PROXY_SCHEME: if (!outer) { return BAD_OPTION_4_02; } #if COAP_PROXY_OPTION_PROCESSING - coap_pkt->proxy_scheme = (char *)current_option; - coap_pkt->proxy_scheme_len = option_length; + packet->proxy_scheme = (char *)current_option; + packet->proxy_scheme_len = option_length; #endif OC_DBG("Proxy-Scheme NOT IMPLEMENTED [%.*s]", - (int)coap_pkt->proxy_scheme_len, coap_pkt->proxy_scheme); + (int)packet->proxy_scheme_len, packet->proxy_scheme); return PROXYING_NOT_SUPPORTED_5_05; break; #endif - case COAP_OPTION_URI_HOST: - if (!outer) { - return BAD_OPTION_4_02; - } - coap_pkt->uri_host = (char *)current_option; - coap_pkt->uri_host_len = option_length; - OC_DBG("Uri-Host [%.*s]", (int)coap_pkt->uri_host_len, - coap_pkt->uri_host); - break; - case COAP_OPTION_URI_PORT: - if (!outer) { - return BAD_OPTION_4_02; - } - coap_pkt->uri_port = - (uint16_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Uri-Port [%u]", coap_pkt->uri_port); - break; - case COAP_OPTION_URI_PATH: - if (!inner) { - return BAD_OPTION_4_02; - } - if (validate) { - break; - } - /* coap_merge_multi_option() operates in-place on the IPBUF, but final - * packet field should be const string -> cast to string */ - coap_merge_multi_option((char **)&(coap_pkt->uri_path), - &(coap_pkt->uri_path_len), current_option, - option_length, '/'); - OC_DBG(" Uri-Path [%.*s]", (int)coap_pkt->uri_path_len, - coap_pkt->uri_path); - break; - case COAP_OPTION_URI_QUERY: - if (!inner) { - return BAD_OPTION_4_02; - } - if (validate) { - break; - } - /* coap_merge_multi_option() operates in-place on the IPBUF, but final - * packet field should be const string -> cast to string */ - coap_merge_multi_option((char **)&(coap_pkt->uri_query), - &(coap_pkt->uri_query_len), current_option, - option_length, '&'); - OC_DBG(" Uri-Query [%.*s]", (int)coap_pkt->uri_query_len, - coap_pkt->uri_query); - break; -#if 0 - case COAP_OPTION_LOCATION_PATH: - if (!inner) { - return BAD_OPTION_4_02; - } - /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */ - coap_merge_multi_option( - (char **)&(coap_pkt->location_path), - &(coap_pkt->location_path_len), - current_option, option_length, - '/'); - OC_DBG("Location-Path [%.*s]", (int)coap_pkt->location_path_len, - coap_pkt->location_path); - break; - case COAP_OPTION_LOCATION_QUERY: - if (!inner) { - return BAD_OPTION_4_02; - } - /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */ - coap_merge_multi_option( - (char **)&(coap_pkt->location_query), - &(coap_pkt->location_query_len), - current_option, option_length, - '&'); - OC_DBG("Location-Query [%.*s]", (int)coap_pkt->location_query_len, - coap_pkt->location_query); - break; -#endif - case COAP_OPTION_OBSERVE: - coap_pkt->observe = - (int32_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Observe [%lu]", (unsigned long)coap_pkt->observe); - break; - case COAP_OPTION_BLOCK2: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->block2_num = - coap_parse_int_option(current_option, option_length); - coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3; - coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07); - coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F) - << (coap_pkt->block2_num & 0x07); - coap_pkt->block2_num >>= 4; - OC_DBG(" Block2 [%lu%s (%u B/blk)]", (unsigned long)coap_pkt->block2_num, - coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size); - break; - case COAP_OPTION_BLOCK1: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->block1_num = - coap_parse_int_option(current_option, option_length); - coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3; - coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07); - coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F) - << (coap_pkt->block1_num & 0x07); - coap_pkt->block1_num >>= 4; - OC_DBG(" Block1 [%lu%s (%u B/blk)]", (unsigned long)coap_pkt->block1_num, - coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size); - break; - case COAP_OPTION_SIZE2: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->size2 = coap_parse_int_option(current_option, option_length); - OC_DBG(" Size2 [%lu]", (unsigned long)coap_pkt->size2); - break; - case COAP_OPTION_SIZE1: - if (!inner) { - return BAD_OPTION_4_02; - } - coap_pkt->size1 = coap_parse_int_option(current_option, option_length); - OC_DBG(" Size1 [%lu]", (unsigned long)coap_pkt->size1); - break; - case OCF_OPTION_CONTENT_FORMAT_VER: - case OCF_OPTION_ACCEPT_CONTENT_FORMAT_VER: { - if (!inner) { - return BAD_OPTION_4_02; - } - uint16_t version = - (uint16_t)coap_parse_int_option(current_option, option_length); - OC_DBG(" Content-format/accept-Version: [%u]", version); - if (version < OCF_VER_1_0_0 -#ifdef OC_SPEC_VER_OIC - && version != OIC_VER_1_1_0 -#endif /* OC_SPEC_VER_OIC */ - ) { - OC_WRN("Unsupported version %d %u", option_number, version); - return UNSUPPORTED_MEDIA_TYPE_4_15; - } - } break; - default: - OC_DBG(" unknown (%u)", option_number); - /* check if critical (odd) */ - if (option_number & 1) { - OC_WRN("Unsupported critical option"); - return BAD_OPTION_4_02; - } + case COAP_OPTION_URI_HOST: + if (!outer) { + return BAD_OPTION_4_02; + } + packet->uri_host = (char *)current_option; + packet->uri_host_len = option_length; + OC_DBG("Uri-Host [%.*s]", (int)packet->uri_host_len, packet->uri_host); + break; + case COAP_OPTION_URI_PORT: + if (!outer) { + return BAD_OPTION_4_02; + } + packet->uri_port = + (uint16_t)coap_parse_int_option(current_option, option_length); + OC_DBG(" Uri-Port [%u]", packet->uri_port); + break; + case COAP_OPTION_OBSERVE: + packet->observe = + (int32_t)coap_parse_int_option(current_option, option_length); + OC_DBG(" Observe [%lu]", (unsigned long)packet->observe); + break; + default: + OC_DBG(" unknown (%u)", option_number); + /* check if critical (odd) */ + if ((option_number & 1) != 0) { + OC_WRN("Unsupported critical option"); + return BAD_OPTION_4_02; } } return COAP_NO_ERROR; } coap_status_t -coap_oscore_parse_options(void *packet, const uint8_t *data, size_t data_len, - uint8_t *current_option, bool inner, bool outer, - bool oscore, bool validate) +coap_oscore_parse_options(coap_packet_t *packet, const uint8_t *data, + size_t data_len, uint8_t *current_option, bool inner, + bool outer, bool oscore, bool validate) { if (data_len > UINT32_MAX) { OC_WRN("message size(%zu) exceeds limit for coap message(%lu)", data_len, (long unsigned)UINT32_MAX); return BAD_REQUEST_4_00; } - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; /* parse options */ - memset(coap_pkt->options, 0, sizeof(coap_pkt->options)); + memset(packet->options, 0, sizeof(packet->options)); unsigned int option_number = 0; unsigned int option_delta = 0; @@ -948,21 +965,22 @@ coap_oscore_parse_options(void *packet, const uint8_t *data, size_t data_len, /* payload marker 0xFF, currently only checking for 0xF* because rest is * reserved */ if ((current_option[0] & 0xF0) == 0xF0) { - coap_pkt->payload = ++current_option; - coap_pkt->payload_len = (uint32_t)(data_len - (coap_pkt->payload - data)); + ++current_option; + packet->payload = current_option; + packet->payload_len = (uint32_t)(data_len - (packet->payload - data)); - if (coap_pkt->transport_type == COAP_TRANSPORT_UDP && - coap_pkt->payload_len > (uint32_t)OC_MAX_APP_DATA_SIZE) { - coap_pkt->payload_len = (uint32_t)OC_MAX_APP_DATA_SIZE; + if (packet->transport_type == COAP_TRANSPORT_UDP && + packet->payload_len > (uint32_t)OC_MAX_APP_DATA_SIZE) { + packet->payload_len = (uint32_t)OC_MAX_APP_DATA_SIZE; /* null-terminate payload */ } if (!validate) { - coap_pkt->payload[coap_pkt->payload_len] = + packet->payload[packet->payload_len] = '\0'; // TODO: this writes after the payload, if the message was // shrank so the allocation matches the message length this // causes a memory corruption OC_DBG("Got payload:"); - OC_LOGbytes(coap_pkt->payload, coap_pkt->payload_len); + OC_LOGbytes(packet->payload, packet->payload_len); } break; } @@ -998,14 +1016,14 @@ coap_oscore_parse_options(void *packet, const uint8_t *data, size_t data_len, if (option_number <= COAP_OPTION_SIZE1) { OC_DBG("OPTION %u (delta %u, len %zu):", option_number, option_delta, option_length); - SET_OPTION(coap_pkt, option_number); + SET_OPTION(packet, option_number); } if (current_option + option_length > data + data_len) { OC_WRN("Invalid option - option length exceeds packet length"); return BAD_REQUEST_4_00; } coap_status_t s = - coap_oscore_parse_option(coap_pkt, current_option, inner, outer, oscore, + coap_oscore_parse_option(packet, current_option, inner, outer, oscore, validate, option_number, option_length); if (s != COAP_NO_ERROR) { if (!validate) { @@ -1027,38 +1045,35 @@ coap_oscore_parse_options(void *packet, const uint8_t *data, size_t data_len, /*---------------------------------------------------------------------------*/ #ifdef OC_TCP static void -coap_tcp_set_header_fields(void *packet, uint8_t *num_extended_length_bytes, - uint8_t *len, size_t *extended_len) +coap_tcp_set_header_fields(coap_packet_t *packet, + uint8_t num_extended_length_bytes, uint8_t len, + size_t extended_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->buffer[0] = 0x00; - coap_pkt->buffer[0] |= - COAP_TCP_HEADER_LEN_MASK & (*len) << COAP_TCP_HEADER_LEN_POSITION; - coap_pkt->buffer[0] |= - COAP_HEADER_TOKEN_LEN_MASK & (coap_pkt->token_len) - << COAP_HEADER_TOKEN_LEN_POSITION; + packet->buffer[0] = 0x00; + packet->buffer[0] |= + COAP_TCP_HEADER_LEN_MASK & len << COAP_TCP_HEADER_LEN_POSITION; + packet->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK & + (packet->token_len) << COAP_HEADER_TOKEN_LEN_POSITION; - int i = 0; - for (i = 1; i <= *num_extended_length_bytes; i++) { - coap_pkt->buffer[i] = - (uint8_t)((*extended_len) >> (8 * (*num_extended_length_bytes - i))); + for (int i = 1; i <= num_extended_length_bytes; i++) { + packet->buffer[i] = + (uint8_t)(extended_len >> (8 * (num_extended_length_bytes - i))); } - coap_pkt->buffer[1 + *num_extended_length_bytes] = coap_pkt->code; + packet->buffer[1 + num_extended_length_bytes] = packet->code; } + static void -coap_tcp_compute_message_length(void *packet, size_t option_length, +coap_tcp_compute_message_length(const coap_packet_t *packet, + size_t option_length, uint8_t *num_extended_length_bytes, uint8_t *len, size_t *extended_len) { - - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; *len = 0; *extended_len = 0; size_t total_length = option_length; - if (coap_pkt->payload_len > 0) { - total_length += COAP_PAYLOAD_MARKER_LEN + coap_pkt->payload_len; + if (packet->payload_len > 0) { + total_length += COAP_PAYLOAD_MARKER_LEN + packet->payload_len; } if (total_length < COAP_TCP_EXTENDED_LENGTH_1_DEFAULT_LEN) { @@ -1140,13 +1155,13 @@ void coap_init_connection(void) { /* initialize transaction ID */ - current_mid = (uint16_t)oc_random_value(); + g_current_mid = (uint16_t)oc_random_value(); } uint16_t coap_get_mid(void) { - return ++current_mid; + return ++g_current_mid; } void @@ -1213,36 +1228,26 @@ coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, return 0; } - uint8_t *option; - unsigned int current_number = 0; - uint8_t token_location = 0; - size_t option_length = 0; - size_t option_length_calculation = 0; - size_t header_length_calculation = 0; - /* Initialize */ packet->buffer = buffer; packet->version = 1; /* coap header option serialize first to know total length about options */ - option_length_calculation = + size_t option_length_calculation = coap_serialize_options(packet, NULL, inner, outer, oscore); - header_length_calculation += option_length_calculation; + size_t header_length_calculation = option_length_calculation; - /* accoridng to spec COAP_PAYLOAD_MARKER_LEN should be included - if payload exists */ - if (packet->payload_len > 0) { - header_length_calculation += COAP_PAYLOAD_MARKER_LEN; - } + uint8_t token_location = 0; + uint8_t *option; if (outer) { header_length_calculation += packet->token_len; #ifdef OC_TCP if (packet->transport_type == COAP_TRANSPORT_TCP) { - uint8_t num_extended_length_bytes = 0, len = 0; + uint8_t len = 0; + uint8_t num_extended_length_bytes = 0; size_t extended_len = 0; - coap_tcp_compute_message_length(packet, option_length_calculation, &num_extended_length_bytes, &len, &extended_len); @@ -1257,13 +1262,13 @@ coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, goto exit; } /* set header fields */ - coap_tcp_set_header_fields(packet, &num_extended_length_bytes, &len, - &extended_len); + coap_tcp_set_header_fields(packet, num_extended_length_bytes, len, + extended_len); } else #endif /* OC_TCP */ { - /* set header fields */ token_location = COAP_HEADER_LEN; + /* set header fields */ header_length_calculation += token_location; if (!coap_oscore_check_packet_header_size(header_length_calculation, buffer_size)) { @@ -1274,52 +1279,52 @@ coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, OC_DBG("-Serializing MID %u to %p", packet->mid, (void *)packet->buffer); coap_udp_set_header_fields(packet); } - } - /* empty packet, dont need to do more stuff */ - if (outer && !packet->code) { - OC_DBG("Done serializing empty message at %p-", (void *)packet->buffer); - return token_location; - } - if (outer) { + /* empty packet, dont need to do more stuff */ + if (!packet->code) { + OC_DBG("Done serializing empty message at %p-", (void *)packet->buffer); + return token_location; + } + if (oscore) { OC_DBG("Outer CoAP code: %d", packet->code); } + /* set Token */ OC_DBG("Token (len %u)", packet->token_len); OC_LOGbytes(packet->token, packet->token_len); option = packet->buffer + token_location; - for (current_number = 0; current_number < packet->token_len; - ++current_number) { - *option = packet->token[current_number]; - ++option; - } + memcpy(option, packet->token, packet->token_len); + option += packet->token_len; } else { OC_DBG("Inner CoAP code: %d", packet->code); - packet->buffer[0] = packet->code; - option = packet->buffer + 1; ++header_length_calculation; if (!coap_oscore_check_packet_header_size(header_length_calculation, buffer_size)) { - OC_ERR("cannot serialize inner packet"); + OC_ERR("cannot serialize payload: cannot serialize inner packet"); goto exit; } + packet->buffer[0] = packet->code; + option = packet->buffer + 1; } - option_length = coap_serialize_options(packet, option, inner, outer, oscore); - option += option_length; - - /* Pack payload */ - size_t header_len = (size_t)(option - packet->buffer); - if (!coap_oscore_check_packet_header_size(header_len, buffer_size)) { - OC_ERR("cannot serialize packet"); - goto exit; - } + size_t written = coap_serialize_options(packet, option, inner, outer, oscore); + option += written; + buffer_size -= header_length_calculation; + assert(header_length_calculation == (size_t)(option - packet->buffer)); /* Payload marker */ if (packet->payload_len > 0) { - *option = 0xFF; - ++option; + if (buffer_size < packet->payload_len + COAP_PAYLOAD_MARKER_LEN) { + OC_ERR("cannot serialize payload: no space left for payload(required: " + "%" PRIu32 ", remaining: %zu)", + packet->payload_len + COAP_PAYLOAD_MARKER_LEN, buffer_size); + goto exit; + } + /* according to spec COAP_PAYLOAD_MARKER_LEN should be included if payload + * exists */ + *option = COAP_PAYLOAD_MARKER; + option += COAP_PAYLOAD_MARKER_LEN; memmove(option, packet->payload, packet->payload_len); } OC_DBG("Serialized payload:"); @@ -1333,11 +1338,12 @@ coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer, OC_LOGbytes(packet->buffer, (packet->payload_len + option - buffer)); return (size_t)((option - buffer) + packet->payload_len); + exit: packet->buffer = NULL; return 0; } -/*---------------------------------------------------------------------------*/ + void coap_send_message(oc_message_t *message) { @@ -1363,9 +1369,9 @@ coap_serialize_message(coap_packet_t *packet, uint8_t *buffer, return coap_oscore_serialize_message(packet, buffer, buffer_size, true, true, false); } -/*---------------------------------------------------------------------------*/ + coap_status_t -coap_udp_parse_message(void *packet, uint8_t *data, size_t data_len, +coap_udp_parse_message(coap_packet_t *packet, uint8_t *data, size_t data_len, bool validate) { if (data_len > UINT16_MAX) { @@ -1373,39 +1379,38 @@ coap_udp_parse_message(void *packet, uint8_t *data, size_t data_len, (unsigned)UINT16_MAX); return BAD_REQUEST_4_00; } - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; /* initialize packet */ - memset(coap_pkt, 0, sizeof(coap_packet_t)); + memset(packet, 0, sizeof(coap_packet_t)); /* pointer to packet bytes */ - coap_pkt->buffer = data; - coap_pkt->transport_type = COAP_TRANSPORT_UDP; + packet->buffer = data; + packet->transport_type = COAP_TRANSPORT_UDP; /* parse header fields */ - coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0]) >> - COAP_HEADER_VERSION_POSITION; - coap_pkt->type = - (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_TYPE_POSITION; - coap_pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0]) >> - COAP_HEADER_TOKEN_LEN_POSITION; - coap_pkt->code = coap_pkt->buffer[1]; - coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3]; - - if (coap_pkt->version != 1) { + packet->version = (COAP_HEADER_VERSION_MASK & packet->buffer[0]) >> + COAP_HEADER_VERSION_POSITION; + packet->type = + (COAP_HEADER_TYPE_MASK & packet->buffer[0]) >> COAP_HEADER_TYPE_POSITION; + packet->token_len = (COAP_HEADER_TOKEN_LEN_MASK & packet->buffer[0]) >> + COAP_HEADER_TOKEN_LEN_POSITION; + packet->code = packet->buffer[1]; + packet->mid = (uint16_t)(packet->buffer[2] << 8 | packet->buffer[3]); + + if (packet->version != 1) { OC_WRN("CoAP version must be 1"); return BAD_REQUEST_4_00; } - if (coap_pkt->token_len > COAP_TOKEN_LEN) { + if (packet->token_len > COAP_TOKEN_LEN) { OC_WRN("Token Length must not be more than 8"); return BAD_REQUEST_4_00; } uint8_t *current_option = data + COAP_HEADER_LEN; - memcpy(coap_pkt->token, current_option, coap_pkt->token_len); - OC_DBG("Token (len %u)", coap_pkt->token_len); - OC_LOGbytes(coap_pkt->token, coap_pkt->token_len); + memcpy(packet->token, current_option, packet->token_len); + OC_DBG("Token (len %u)", packet->token_len); + OC_LOGbytes(packet->token, packet->token_len); - current_option += coap_pkt->token_len; + current_option += packet->token_len; coap_status_t ret = coap_oscore_parse_options( packet, data, data_len, current_option, true, true, false, validate); @@ -1416,7 +1421,7 @@ coap_udp_parse_message(void *packet, uint8_t *data, size_t data_len, return COAP_NO_ERROR; } -/*---------------------------------------------------------------------------*/ + #ifdef OC_TCP size_t coap_tcp_get_packet_size(const uint8_t *data) @@ -1435,9 +1440,9 @@ coap_tcp_get_packet_size(const uint8_t *data) return total_length; } -/*---------------------------------------------------------------------------*/ + coap_status_t -coap_tcp_parse_message(void *packet, uint8_t *data, size_t data_len, +coap_tcp_parse_message(coap_packet_t *packet, uint8_t *data, size_t data_len, bool validate) { if (data_len > UINT32_MAX) { @@ -1445,27 +1450,26 @@ coap_tcp_parse_message(void *packet, uint8_t *data, size_t data_len, (long unsigned)UINT32_MAX); return BAD_REQUEST_4_00; } - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; /* initialize packet */ - memset(coap_pkt, 0, sizeof(coap_packet_t)); + memset(packet, 0, sizeof(coap_packet_t)); /* pointer to packet bytes */ - coap_pkt->buffer = data; - coap_pkt->transport_type = COAP_TRANSPORT_TCP; + packet->buffer = data; + packet->transport_type = COAP_TRANSPORT_TCP; /* parse header fields */ size_t message_length = 0; uint8_t num_extended_length_bytes = 0; coap_tcp_parse_message_length(data, &message_length, &num_extended_length_bytes); - coap_pkt->type = COAP_TYPE_NON; - coap_pkt->mid = 0; - coap_pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0]) >> - COAP_HEADER_TOKEN_LEN_POSITION; - coap_pkt->code = coap_pkt->buffer[1 + num_extended_length_bytes]; + packet->type = COAP_TYPE_NON; + packet->mid = 0; + packet->token_len = (COAP_HEADER_TOKEN_LEN_MASK & packet->buffer[0]) >> + COAP_HEADER_TOKEN_LEN_POSITION; + packet->code = packet->buffer[1 + num_extended_length_bytes]; - if (coap_pkt->token_len > COAP_TOKEN_LEN) { + if (packet->token_len > COAP_TOKEN_LEN) { OC_DBG("Token Length must not be more than 8"); return BAD_REQUEST_4_00; } @@ -1473,11 +1477,11 @@ coap_tcp_parse_message(void *packet, uint8_t *data, size_t data_len, uint8_t *current_option = data + COAP_TCP_DEFAULT_HEADER_LEN + num_extended_length_bytes; - memcpy(coap_pkt->token, current_option, coap_pkt->token_len); - OC_DBG("Token (len %u)", coap_pkt->token_len); - OC_LOGbytes(coap_pkt->token, coap_pkt->token_len); + memcpy(packet->token, current_option, packet->token_len); + OC_DBG("Token (len %u)", packet->token_len); + OC_LOGbytes(packet->token, packet->token_len); - current_option += coap_pkt->token_len; + current_option += packet->token_len; coap_status_t ret = coap_oscore_parse_options( packet, data, data_len, current_option, true, true, false, validate); @@ -1489,337 +1493,325 @@ coap_tcp_parse_message(void *packet, uint8_t *data, size_t data_len, return COAP_NO_ERROR; } #endif /* OC_TCP */ -/*---------------------------------------------------------------------------*/ + #if 0 int -coap_get_query_variable(void *packet, const char *name, const char **output) +coap_get_query_variable(coap_packet_t *packet, const char *name, const char **output) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - if(IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) { - return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len, + if(IS_OPTION(packet, COAP_OPTION_URI_QUERY)) { + return coap_get_variable(packet->uri_query, packet->uri_query_len, name, output); } return 0; } int -coap_get_post_variable(void *packet, const char *name, const char **output) +coap_get_post_variable(coap_packet_t *packet, const char *name, const char **output) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - if(coap_pkt->payload_len) { - return coap_get_variable((const char *)coap_pkt->payload, - coap_pkt->payload_len, name, output); + if(packet->payload_len) { + return coap_get_variable((const char *)packet->payload, + packet->payload_len, name, output); } return 0; } #endif -/*---------------------------------------------------------------------------*/ + int -coap_set_status_code(void *packet, unsigned int code) +coap_set_status_code(coap_packet_t *packet, unsigned int code) { if (code <= 0xFF) { - ((coap_packet_t *)packet)->code = (uint8_t)code; + packet->code = (uint8_t)code; return 1; - } else { - return 0; } + return 0; } -/*---------------------------------------------------------------------------*/ + int -coap_set_token(void *packet, const uint8_t *token, size_t token_len) +coap_set_token(coap_packet_t *packet, const uint8_t *token, size_t token_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->token_len = (uint8_t)MIN(COAP_TOKEN_LEN, token_len); - memcpy(coap_pkt->token, token, coap_pkt->token_len); - - return coap_pkt->token_len; + packet->token_len = (uint8_t)MIN(COAP_TOKEN_LEN, token_len); + memcpy(packet->token, token, packet->token_len); + return packet->token_len; } int -coap_get_header_content_format(const void *packet, oc_content_format_t *format) +coap_get_header_content_format(const coap_packet_t *packet, + oc_content_format_t *format) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) { + if (!IS_OPTION(packet, COAP_OPTION_CONTENT_FORMAT)) { return 0; } - *format = coap_pkt->content_format; + *format = packet->content_format; return 1; } int -coap_set_header_content_format(void *packet, oc_content_format_t format) +coap_set_header_content_format(coap_packet_t *packet, + oc_content_format_t format) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->content_format = format; - SET_OPTION(coap_pkt, COAP_OPTION_CONTENT_FORMAT); + assert(format > 0 && format <= UINT16_MAX); + packet->content_format = (uint16_t)format; + SET_OPTION(packet, COAP_OPTION_CONTENT_FORMAT); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_accept(const void *packet, unsigned int *accept) +coap_get_header_accept(const coap_packet_t *packet, unsigned int *accept) { - const coap_packet_t *coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) { + if (!IS_OPTION(packet, COAP_OPTION_ACCEPT)) { return 0; } - *accept = coap_pkt->accept; + *accept = packet->accept; return 1; } + int -coap_set_header_accept(void *packet, unsigned int accept) +coap_set_header_accept(coap_packet_t *packet, unsigned int accept) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->accept = (uint16_t)accept; - SET_OPTION(coap_pkt, COAP_OPTION_ACCEPT); + packet->accept = (uint16_t)accept; + SET_OPTION(packet, COAP_OPTION_ACCEPT); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_max_age(const void *packet, uint32_t *age) +coap_get_header_max_age(const coap_packet_t *packet, uint32_t *age) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) { + if (!IS_OPTION(packet, COAP_OPTION_MAX_AGE)) { *age = COAP_DEFAULT_MAX_AGE; } else { - *age = coap_pkt->max_age; + *age = packet->max_age; } return 1; } + int -coap_set_header_max_age(void *packet, uint32_t age) +coap_set_header_max_age(coap_packet_t *packet, uint32_t age) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->max_age = age; - SET_OPTION(coap_pkt, COAP_OPTION_MAX_AGE); + packet->max_age = age; + SET_OPTION(packet, COAP_OPTION_MAX_AGE); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_etag(const void *packet, const uint8_t **etag) +coap_get_header_etag(const coap_packet_t *packet, const uint8_t **etag) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) { + if (!IS_OPTION(packet, COAP_OPTION_ETAG)) { return 0; } - *etag = coap_pkt->etag; - return coap_pkt->etag_len; + *etag = packet->etag; + return packet->etag_len; } + int -coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len) +coap_set_header_etag(coap_packet_t *packet, const uint8_t *etag, + size_t etag_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->etag_len = (uint8_t)MIN(COAP_ETAG_LEN, etag_len); - memcpy(coap_pkt->etag, etag, coap_pkt->etag_len); - - SET_OPTION(coap_pkt, COAP_OPTION_ETAG); - return coap_pkt->etag_len; + packet->etag_len = (uint8_t)MIN(COAP_ETAG_LEN, etag_len); + memcpy(packet->etag, etag, packet->etag_len); + SET_OPTION(packet, COAP_OPTION_ETAG); + return packet->etag_len; } -/*---------------------------------------------------------------------------*/ + size_t -coap_get_header_proxy_uri(const void *packet, const char **uri) +coap_get_header_proxy_uri(const coap_packet_t *packet, const char **uri) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) { + if (!IS_OPTION(packet, COAP_OPTION_PROXY_URI)) { return 0; } - *uri = coap_pkt->proxy_uri; - return coap_pkt->proxy_uri_len; + *uri = packet->proxy_uri; + return packet->proxy_uri_len; } + size_t -coap_set_header_proxy_uri(void *packet, const char *uri) +coap_set_header_proxy_uri(coap_packet_t *packet, const char *uri, + size_t uri_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->proxy_uri = uri; - coap_pkt->proxy_uri_len = strlen(uri); + packet->proxy_uri = uri; + packet->proxy_uri_len = uri_len; - SET_OPTION(coap_pkt, COAP_OPTION_PROXY_URI); - return coap_pkt->proxy_uri_len; + SET_OPTION(packet, COAP_OPTION_PROXY_URI); + return packet->proxy_uri_len; } -/*---------------------------------------------------------------------------*/ + #if 0 /*FIXME support multiple ETags */ -int coap_get_header_if_match(void *packet, const uint8_t **etag) +int +coap_get_header_if_match(coap_packet_t *packet, const uint8_t **etag) { - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - - if(!IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) { + if (!IS_OPTION(packet, COAP_OPTION_IF_MATCH)) { return 0; } - *etag = coap_pkt->if_match; - return coap_pkt->if_match_len; + *etag = packet->if_match; + return packet->if_match_len; } -int coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len) -{ - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len); - memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len); +int +coap_set_header_if_match(coap_packet_t *packet, const uint8_t *etag, + size_t etag_len) +{ + packet->if_match_len = MIN(COAP_ETAG_LEN, etag_len); + memcpy(packet->if_match, etag, packet->if_match_len); - SET_OPTION(coap_pkt, COAP_OPTION_IF_MATCH); - return coap_pkt->if_match_len; + SET_OPTION(packet, COAP_OPTION_IF_MATCH); + return packet->if_match_len; } -/*---------------------------------------------------------------------------*/ -int coap_get_header_if_none_match(void *packet) + +int +coap_get_header_if_none_match(coap_packet_t *packet) { - return IS_OPTION((coap_packet_t *)packet, - COAP_OPTION_IF_NONE_MATCH) ? 1 : 0; + return IS_OPTION(packet, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0; } -int coap_set_header_if_none_match(void *packet) + +int +coap_set_header_if_none_match(coap_packet_t *packet) { - SET_OPTION((coap_packet_t * )packet, COAP_OPTION_IF_NONE_MATCH); + SET_OPTION(packet, COAP_OPTION_IF_NONE_MATCH); return 1; } -/*---------------------------------------------------------------------------*/ -int coap_get_header_uri_host(void *packet, const char **host) -{ - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - if(!IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) { +int +coap_get_header_uri_host(coap_packet_t *packet, const char **host) +{ + if (!IS_OPTION(packet, COAP_OPTION_URI_HOST)) { return 0; } - *host = coap_pkt->uri_host; - return coap_pkt->uri_host_len; + *host = packet->uri_host; + return packet->uri_host_len; } -int coap_set_header_uri_host(void *packet, const char *host) -{ - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - coap_pkt->uri_host = host; - coap_pkt->uri_host_len = strlen(host); +int +coap_set_header_uri_host(coap_packet_t *packet, const char *host) +{ + packet->uri_host = host; + packet->uri_host_len = strlen(host); - SET_OPTION(coap_pkt, COAP_OPTION_URI_HOST); - return coap_pkt->uri_host_len; + SET_OPTION(packet, COAP_OPTION_URI_HOST); + return packet->uri_host_len; } + #endif -/*---------------------------------------------------------------------------*/ + size_t -coap_get_header_uri_path(const void *packet, const char **path) +coap_get_header_uri_path(const coap_packet_t *packet, const char **path) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) { + if (!IS_OPTION(packet, COAP_OPTION_URI_PATH)) { return 0; } - *path = coap_pkt->uri_path; - return coap_pkt->uri_path_len; + *path = packet->uri_path; + return packet->uri_path_len; } + size_t -coap_set_header_uri_path(void *packet, const char *path, size_t path_len) +coap_set_header_uri_path(coap_packet_t *packet, const char *path, + size_t path_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - while (path[0] == '/') { ++path; --path_len; } - coap_pkt->uri_path = path; - coap_pkt->uri_path_len = path_len; + packet->uri_path = path; + packet->uri_path_len = path_len; - SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH); - return coap_pkt->uri_path_len; + SET_OPTION(packet, COAP_OPTION_URI_PATH); + return packet->uri_path_len; } -/*---------------------------------------------------------------------------*/ + size_t -coap_get_header_uri_query(const void *packet, const char **query) +coap_get_header_uri_query(const coap_packet_t *packet, const char **query) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) { + if (!IS_OPTION(packet, COAP_OPTION_URI_QUERY)) { return 0; } - *query = coap_pkt->uri_query; - return coap_pkt->uri_query_len; + *query = packet->uri_query; + return packet->uri_query_len; } + #ifdef OC_CLIENT size_t -coap_set_header_uri_query(void *packet, const char *query) +coap_set_header_uri_query(coap_packet_t *packet, const char *query, + size_t query_len) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - while (query[0] == '?') + while (query[0] == '?') { ++query; + } - coap_pkt->uri_query = query; - coap_pkt->uri_query_len = strlen(query); + packet->uri_query = query; + packet->uri_query_len = query_len; - SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY); - return coap_pkt->uri_query_len; + SET_OPTION(packet, COAP_OPTION_URI_QUERY); + return packet->uri_query_len; } -#endif -/*---------------------------------------------------------------------------*/ +#endif /* OC_CLIENT */ + #if 0 -int coap_get_header_location_path(void *packet, const char **path) -{ - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - if(!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) { +int coap_get_header_location_path(coap_packet_t *packet, const char **path) + OC_NONNULL(); /* in-place string might not be 0-terminated. */ +int coap_set_header_location_path(coap_packet_t *packet, const char *path) + OC_NONNULL(); /* also splits optional query into Location-Query option. */ + +int +coap_get_header_location_path(coap_packet_t *packet, const char **path) +{ + if (!IS_OPTION(packet, COAP_OPTION_LOCATION_PATH)) { return 0; } - *path = coap_pkt->location_path; - return coap_pkt->location_path_len; + *path = packet->location_path; + return packet->location_path_len; } -int coap_set_header_location_path(void *packet, const char *path) +int +coap_set_header_location_path(coap_packet_t *packet, const char *path) { - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - - char *query; - - while(path[0] == '/') + while (path[0] == '/') { ++path; + } - if((query = strchr(path, '?'))) { + char *query; + if ((query = strchr(path, '?'))) { coap_set_header_location_query(packet, query + 1); - coap_pkt->location_path_len = query - path; + packet->location_path_len = query - path; } else { - coap_pkt->location_path_len = strlen(path); + packet->location_path_len = strlen(path); } - coap_pkt->location_path = path; + packet->location_path = path; - if(coap_pkt->location_path_len > 0) { - SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH); + if (packet->location_path_len > 0) { + SET_OPTION(packet, COAP_OPTION_LOCATION_PATH); } - return coap_pkt->location_path_len; + return packet->location_path_len; } -/*---------------------------------------------------------------------------*/ -int coap_get_header_location_query(void *packet, const char **query) -{ - coap_packet_t * const coap_pkt = (coap_packet_t *)packet; - if(!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) { +int coap_get_header_location_query(coap_packet_t *packet, const char **query) + OC_NONNULL(); /* in-place string might not be 0-terminated. */ + +size_t coap_set_header_location_query(coap_packet_t *packet, const char *query) + OC_NONNULL(); + +int +coap_get_header_location_query(coap_packet_t *packet, const char **query) +{ + if (!IS_OPTION(packet, COAP_OPTION_LOCATION_QUERY)) { return 0; } - *query = coap_pkt->location_query; - return coap_pkt->location_query_len; + *query = packet->location_query; + return packet->location_query_len; } -#endif + size_t -coap_set_header_location_query(void *packet, const char *query) +coap_set_header_location_query(coap_packet_t *packet, const char *query) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - while (query[0] == '?') ++query; - coap_pkt->location_query = query; - coap_pkt->location_query_len = strlen(query); + packet->location_query = query; + packet->location_query_len = strlen(query); - SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY); - return coap_pkt->location_query_len; + SET_OPTION(packet, COAP_OPTION_LOCATION_QUERY); + return packet->location_query_len; } -/*---------------------------------------------------------------------------*/ + +#endif + int coap_get_header_observe(const coap_packet_t *packet, int32_t *observe) { @@ -1837,36 +1829,33 @@ coap_set_header_observe(coap_packet_t *packet, int32_t observe) SET_OPTION(packet, COAP_OPTION_OBSERVE); } -/*---------------------------------------------------------------------------*/ int -coap_get_header_block2(const void *packet, uint32_t *num, uint8_t *more, - uint16_t *size, uint32_t *offset) +coap_get_header_block2(const coap_packet_t *packet, uint32_t *num, + uint8_t *more, uint16_t *size, uint32_t *offset) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) { + if (!IS_OPTION(packet, COAP_OPTION_BLOCK2)) { return 0; } /* pointers may be NULL to get only specific block parameters */ if (num != NULL) { - *num = coap_pkt->block2_num; + *num = packet->block2_num; } if (more != NULL) { - *more = coap_pkt->block2_more; + *more = packet->block2_more; } if (size != NULL) { - *size = coap_pkt->block2_size; + *size = packet->block2_size; } if (offset != NULL) { - *offset = coap_pkt->block2_offset; + *offset = packet->block2_offset; } return 1; } + int -coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size) +coap_set_header_block2(coap_packet_t *packet, uint32_t num, uint8_t more, + uint16_t size) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - if (size < 16) { return 0; } @@ -1876,43 +1865,41 @@ coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size) if (num > 0x0FFFFF) { return 0; } - coap_pkt->block2_num = num; - coap_pkt->block2_more = more ? 1 : 0; - coap_pkt->block2_size = size; + packet->block2_num = num; + packet->block2_more = more ? 1 : 0; + packet->block2_size = size; - SET_OPTION(coap_pkt, COAP_OPTION_BLOCK2); + SET_OPTION(packet, COAP_OPTION_BLOCK2); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_block1(const void *packet, uint32_t *num, uint8_t *more, - uint16_t *size, uint32_t *offset) +coap_get_header_block1(const coap_packet_t *packet, uint32_t *num, + uint8_t *more, uint16_t *size, uint32_t *offset) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) { + if (!IS_OPTION(packet, COAP_OPTION_BLOCK1)) { return 0; } /* pointers may be NULL to get only specific block parameters */ if (num != NULL) { - *num = coap_pkt->block1_num; + *num = packet->block1_num; } if (more != NULL) { - *more = coap_pkt->block1_more; + *more = packet->block1_more; } if (size != NULL) { - *size = coap_pkt->block1_size; + *size = packet->block1_size; } if (offset != NULL) { - *offset = coap_pkt->block1_offset; + *offset = packet->block1_offset; } return 1; } + int -coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size) +coap_set_header_block1(coap_packet_t *packet, uint32_t num, uint8_t more, + uint16_t size) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - if (size < 16) { return 0; } @@ -1922,84 +1909,75 @@ coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size) if (num > 0x0FFFFF) { return 0; } - coap_pkt->block1_num = num; - coap_pkt->block1_more = more; - coap_pkt->block1_size = size; + packet->block1_num = num; + packet->block1_more = more; + packet->block1_size = size; - SET_OPTION(coap_pkt, COAP_OPTION_BLOCK1); + SET_OPTION(packet, COAP_OPTION_BLOCK1); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_size2(const void *packet, uint32_t *size) +coap_get_header_size2(const coap_packet_t *packet, uint32_t *size) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_SIZE2)) { + if (!IS_OPTION(packet, COAP_OPTION_SIZE2)) { return 0; } - *size = coap_pkt->size2; + *size = packet->size2; return 1; } + int -coap_set_header_size2(void *packet, uint32_t size) +coap_set_header_size2(coap_packet_t *packet, uint32_t size) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->size2 = size; - SET_OPTION(coap_pkt, COAP_OPTION_SIZE2); + packet->size2 = size; + SET_OPTION(packet, COAP_OPTION_SIZE2); return 1; } -/*---------------------------------------------------------------------------*/ + int -coap_get_header_size1(const void *packet, uint32_t *size) +coap_get_header_size1(const coap_packet_t *packet, uint32_t *size) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (!IS_OPTION(coap_pkt, COAP_OPTION_SIZE1)) { + if (!IS_OPTION(packet, COAP_OPTION_SIZE1)) { return 0; } - *size = coap_pkt->size1; + *size = packet->size1; return 1; } + int -coap_set_header_size1(void *packet, uint32_t size) +coap_set_header_size1(coap_packet_t *packet, uint32_t size) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->size1 = size; - SET_OPTION(coap_pkt, COAP_OPTION_SIZE1); + packet->size1 = size; + SET_OPTION(packet, COAP_OPTION_SIZE1); return 1; } -/*---------------------------------------------------------------------------*/ + uint32_t -coap_get_payload(const void *packet, const uint8_t **payload) +coap_get_payload(const coap_packet_t *packet, const uint8_t **payload) { - const coap_packet_t *const coap_pkt = (const coap_packet_t *)packet; - - if (coap_pkt->payload) { - *payload = coap_pkt->payload; - return coap_pkt->payload_len; + if (packet->payload) { + *payload = packet->payload; + return packet->payload_len; } *payload = NULL; return 0; } + uint32_t -coap_set_payload(void *packet, const void *payload, uint32_t length) +coap_set_payload(coap_packet_t *packet, uint8_t *payload, uint32_t length) { - coap_packet_t *const coap_pkt = (coap_packet_t *)packet; - - coap_pkt->payload = (uint8_t *)payload; + packet->payload = payload; #ifdef OC_TCP - if (coap_pkt->transport_type == COAP_TRANSPORT_TCP) { - coap_pkt->payload_len = length; + if (packet->transport_type == COAP_TRANSPORT_TCP) { + packet->payload_len = length; } else #endif /* OC_TCP */ { - coap_pkt->payload_len = MIN((uint32_t)OC_BLOCK_SIZE, length); + packet->payload_len = MIN((uint32_t)OC_BLOCK_SIZE, length); } - return coap_pkt->payload_len; + return packet->payload_len; } coap_status_t diff --git a/messaging/coap/coap.h b/messaging/coap/coap.h index e084cfe9ac..14818c8252 100644 --- a/messaging/coap/coap.h +++ b/messaging/coap/coap.h @@ -89,8 +89,8 @@ typedef enum { /* parsed message struct */ typedef struct { - uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory - to serialize packet */ + uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / + memory to serialize packet */ coap_transport_type_t transport_type; uint8_t version; coap_message_type_t type; @@ -110,21 +110,29 @@ typedef struct uint8_t etag[COAP_ETAG_LEN]; size_t proxy_uri_len; const char *proxy_uri; +#if 0 size_t proxy_scheme_len; const char *proxy_scheme; +#endif size_t uri_host_len; const char *uri_host; +#if 0 size_t location_path_len; const char *location_path; +#endif uint16_t uri_port; +#if 0 size_t location_query_len; const char *location_query; +#endif size_t uri_path_len; const char *uri_path; int32_t observe; uint16_t accept; +#if 0 uint8_t if_match_len; uint8_t if_match[COAP_ETAG_LEN]; +#endif uint32_t block2_num; uint8_t block2_more; uint16_t block2_size; @@ -137,7 +145,9 @@ typedef struct uint32_t size1; size_t uri_query_len; const char *uri_query; +#if 0 uint8_t if_none_match; +#endif #ifdef OC_TCP /* CoAP over TCP Signal option values */ @@ -203,10 +213,12 @@ void coap_send_message(oc_message_t *message) OC_NONNULL(); * first BAD_REQUEST * @return coap_status_t */ -coap_status_t coap_oscore_parse_options(void *packet, const uint8_t *data, - size_t data_len, +coap_status_t coap_oscore_parse_options(coap_packet_t *packet, + const uint8_t *data, size_t data_len, uint8_t *current_option, bool inner, - bool outer, bool oscore, bool validate); + bool outer, bool oscore, bool validate) + OC_NONNULL(); + /** * @brief Parse UDP CoAP message * @@ -216,84 +228,81 @@ coap_status_t coap_oscore_parse_options(void *packet, const uint8_t *data, * @param validate if true, it doesn't modify data * @return coap_status_t */ -coap_status_t coap_udp_parse_message(void *request, uint8_t *data, - size_t data_len, bool validate); +coap_status_t coap_udp_parse_message(coap_packet_t *request, uint8_t *data, + size_t data_len, bool validate) + OC_NONNULL(); /*---------------------------------------------------------------------------*/ -int coap_set_status_code(void *packet, unsigned int code); - -int coap_set_token(void *packet, const uint8_t *token, size_t token_len); - -int coap_get_header_content_format(const void *packet, - oc_content_format_t *format); -int coap_set_header_content_format(void *packet, oc_content_format_t format); - -int coap_get_header_accept(const void *packet, unsigned int *accept); -int coap_set_header_accept(void *packet, unsigned int accept); - -int coap_get_header_max_age(const void *packet, uint32_t *age); -int coap_set_header_max_age(void *packet, uint32_t age); - -int coap_get_header_etag(const void *packet, const uint8_t **etag); -int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len); - -size_t coap_get_header_proxy_uri( - const void *packet, - const char **uri); /* in-place string might not be 0-terminated. */ -size_t coap_set_header_proxy_uri(void *packet, const char *uri); - -int coap_get_header_proxy_scheme( - void *packet, - const char **scheme); /* in-place string might not be 0-terminated. */ -int coap_set_header_proxy_scheme(void *packet, const char *scheme); - -size_t coap_get_header_uri_path( - const void *packet, - const char **path); /* in-place string might not be 0-terminated. */ -size_t coap_set_header_uri_path(void *packet, const char *path, - size_t path_len); - -size_t coap_get_header_uri_query( - const void *packet, - const char **query); /* in-place string might not be 0-terminated. */ -size_t coap_set_header_uri_query(void *packet, const char *query); - -int coap_get_header_location_path( - void *packet, - const char **path); /* in-place string might not be 0-terminated. */ -int coap_set_header_location_path(void *packet, - const char *path); /* also splits optional - query into - Location-Query option. - */ - -int coap_get_header_location_query( - void *packet, - const char **query); /* in-place string might not be 0-terminated. */ -size_t coap_set_header_location_query(void *packet, const char *query); - -int coap_get_header_observe(const coap_packet_t *packet, int32_t *observe); -void coap_set_header_observe(coap_packet_t *packet, int32_t observe); - -int coap_get_header_block2(const void *packet, uint32_t *num, uint8_t *more, - uint16_t *size, uint32_t *offset); -int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, - uint16_t size); - -int coap_get_header_block1(const void *packet, uint32_t *num, uint8_t *more, - uint16_t *size, uint32_t *offset); -int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, - uint16_t size); - -int coap_get_header_size2(const void *packet, uint32_t *size); -int coap_set_header_size2(void *packet, uint32_t size); - -int coap_get_header_size1(const void *packet, uint32_t *size); -int coap_set_header_size1(void *packet, uint32_t size); - -uint32_t coap_get_payload(const void *packet, const uint8_t **payload); -uint32_t coap_set_payload(void *packet, const void *payload, uint32_t length); +int coap_set_status_code(coap_packet_t *packet, unsigned int code) OC_NONNULL(); + +int coap_set_token(coap_packet_t *packet, const uint8_t *token, + size_t token_len) OC_NONNULL(); + +int coap_get_header_content_format(const coap_packet_t *packet, + oc_content_format_t *format) OC_NONNULL(); +int coap_set_header_content_format(coap_packet_t *packet, + oc_content_format_t format) OC_NONNULL(); + +int coap_get_header_accept(const coap_packet_t *packet, unsigned int *accept) + OC_NONNULL(); +int coap_set_header_accept(coap_packet_t *packet, unsigned int accept) + OC_NONNULL(); + +int coap_get_header_max_age(const coap_packet_t *packet, uint32_t *age) + OC_NONNULL(); +int coap_set_header_max_age(coap_packet_t *packet, uint32_t age) OC_NONNULL(); + +int coap_get_header_etag(const coap_packet_t *packet, const uint8_t **etag) + OC_NONNULL(); +int coap_set_header_etag(coap_packet_t *packet, const uint8_t *etag, + size_t etag_len) OC_NONNULL(); + +size_t coap_get_header_proxy_uri(const coap_packet_t *packet, const char **uri) + OC_NONNULL(); /* in-place string might not be 0-terminated. */ +size_t coap_set_header_proxy_uri(coap_packet_t *packet, const char *uri, + size_t uri_len) OC_NONNULL(); + +size_t coap_get_header_uri_path(const coap_packet_t *packet, const char **path) + OC_NONNULL(); /* in-place string might not be 0-terminated. */ +size_t coap_set_header_uri_path(coap_packet_t *packet, const char *path, + size_t path_len) OC_NONNULL(); + +size_t coap_get_header_uri_query(const coap_packet_t *packet, + const char **query) + OC_NONNULL(); /* in-place string might not be 0-terminated. */ +size_t coap_set_header_uri_query(coap_packet_t *packet, const char *query, + size_t query_len) OC_NONNULL(); + +int coap_get_header_observe(const coap_packet_t *packet, int32_t *observe) + OC_NONNULL(); +void coap_set_header_observe(coap_packet_t *packet, int32_t observe) + OC_NONNULL(); + +int coap_get_header_block2(const coap_packet_t *packet, uint32_t *num, + uint8_t *more, uint16_t *size, uint32_t *offset) + OC_NONNULL(1); +int coap_set_header_block2(coap_packet_t *packet, uint32_t num, uint8_t more, + uint16_t size) OC_NONNULL(); + +int coap_get_header_block1(const coap_packet_t *packet, uint32_t *num, + uint8_t *more, uint16_t *size, uint32_t *offset) + OC_NONNULL(1); +int coap_set_header_block1(coap_packet_t *packet, uint32_t num, uint8_t more, + uint16_t size) OC_NONNULL(); + +int coap_get_header_size2(const coap_packet_t *packet, uint32_t *size) + OC_NONNULL(); +int coap_set_header_size2(coap_packet_t *packet, uint32_t size) OC_NONNULL(); + +int coap_get_header_size1(const coap_packet_t *packet, uint32_t *size) + OC_NONNULL(); +int coap_set_header_size1(coap_packet_t *packet, uint32_t size) OC_NONNULL(); + +uint32_t coap_get_payload(const coap_packet_t *packet, const uint8_t **payload) + OC_NONNULL(); +uint32_t coap_set_payload(coap_packet_t *packet, uint8_t *payload, + uint32_t length) OC_NONNULL(); size_t coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer); @@ -301,7 +310,7 @@ size_t coap_set_option_header(unsigned int delta, size_t length, #ifdef OC_TCP void coap_tcp_init_message(coap_packet_t *packet, uint8_t code) OC_NONNULL(); -size_t coap_tcp_get_packet_size(const uint8_t *data); +size_t coap_tcp_get_packet_size(const uint8_t *data) OC_NONNULL(); /** * @brief Parse TCP CoAP message @@ -312,11 +321,13 @@ size_t coap_tcp_get_packet_size(const uint8_t *data); * @param validate if true, it doesn't modify data * @return coap_status_t */ -coap_status_t coap_tcp_parse_message(void *packet, uint8_t *data, - size_t data_len, bool validate); +coap_status_t coap_tcp_parse_message(coap_packet_t *packet, uint8_t *data, + size_t data_len, bool validate) + OC_NONNULL(); void coap_tcp_parse_message_length(const uint8_t *data, size_t *message_length, - uint8_t *num_extended_length_bytes); + uint8_t *num_extended_length_bytes) + OC_NONNULL(); #endif /* OC_TCP */ #ifdef __cplusplus diff --git a/messaging/coap/constants.h b/messaging/coap/constants.h index 854939c990..aef8cf2711 100644 --- a/messaging/coap/constants.h +++ b/messaging/coap/constants.h @@ -95,7 +95,8 @@ extern "C" { 2 /* | len:0xF0 tkl:0x0F | .... | code | \ */ #define COAP_TCP_MAX_EXTENDED_LENGTH_LEN 4 -#define COAP_PAYLOAD_MARKER_LEN 1 /* 0xFF */ +#define COAP_PAYLOAD_MARKER (0xFF) +#define COAP_PAYLOAD_MARKER_LEN (1) /* 0xFF */ #define COAP_TCP_HEADER_LEN_MASK 0xF0 #define COAP_TCP_HEADER_LEN_POSITION 4 diff --git a/messaging/coap/engine.c b/messaging/coap/engine.c index 172f2d8158..85d513419f 100644 --- a/messaging/coap/engine.c +++ b/messaging/coap/engine.c @@ -97,8 +97,7 @@ static uint8_t g_idx = 0; bool oc_coap_check_if_duplicate(uint16_t mid, uint32_t device) { - size_t i; - for (i = 0; i < OC_REQUEST_HISTORY_SIZE; i++) { + for (size_t i = 0; i < OC_REQUEST_HISTORY_SIZE; i++) { if (g_history[i] == mid && g_history_dev[i] == device) { OC_DBG("dropping duplicate request"); return true; @@ -114,16 +113,16 @@ coap_send_empty_response(coap_message_type_t type, uint16_t mid, const oc_endpoint_t *endpoint) { OC_DBG("CoAP send empty message: mid=%u, code=%u", mid, code); - coap_packet_t msg[1]; // empty response - coap_udp_init_message(msg, type, code, mid); + coap_packet_t packet; // empty response + coap_udp_init_message(&packet, type, code, mid); oc_message_t *message = oc_message_allocate_outgoing(); if (message) { memcpy(&message->endpoint, endpoint, sizeof(*endpoint)); if (token && token_len > 0) { - coap_set_token(msg, token, token_len); + coap_set_token(&packet, token, token_len); } size_t len = - coap_serialize_message(msg, message->data, oc_message_buffer_size()); + coap_serialize_message(&packet, message->data, oc_message_buffer_size()); if (len > 0) { message->length = len; coap_send_message(message); @@ -181,11 +180,16 @@ coap_receive(oc_message_t *msg) transaction = NULL; /* block options */ - uint32_t block1_num = 0, block1_offset = 0, block2_num = 0, block2_offset = 0; - uint16_t block1_size = (uint16_t)OC_BLOCK_SIZE, - block2_size = (uint16_t)OC_BLOCK_SIZE; - uint8_t block1_more = 0, block2_more = 0; - bool block1 = false, block2 = false; + uint32_t block1_num = 0; + uint32_t block1_offset = 0; + uint32_t block2_num = 0; + uint32_t block2_offset = 0; + uint16_t block1_size = (uint16_t)OC_BLOCK_SIZE; + uint16_t block2_size = (uint16_t)OC_BLOCK_SIZE; + uint8_t block1_more = 0; + uint8_t block2_more = 0; + bool block1 = false; + bool block2 = false; #ifdef OC_BLOCK_WISE oc_blockwise_state_t *request_buffer = NULL, *response_buffer = NULL; @@ -402,7 +406,7 @@ coap_receive(oc_message_t *msg) if (response_buffer) { OC_DBG("continuing ongoing block-wise transfer"); uint32_t payload_size = 0; - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( response_buffer, block2_offset, block2_size, &payload_size); if (payload) { OC_DBG("dispatching next block"); @@ -569,7 +573,7 @@ coap_receive(oc_message_t *msg) uint32_t payload_size = 0; #ifdef OC_TCP if (msg->endpoint.flags & TCP) { - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( response_buffer, 0, response_buffer->payload_size + 1, &payload_size); if (payload && response_buffer->payload_size > 0) { @@ -578,7 +582,7 @@ coap_receive(oc_message_t *msg) response_buffer->ref_count = 0; } else { #endif /* OC_TCP */ - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( response_buffer, 0, block2_size, &payload_size); if (payload) { coap_set_payload(response, payload, payload_size); @@ -661,7 +665,7 @@ coap_receive(oc_message_t *msg) oc_string(request_buffer->href)); client_cb = (oc_client_cb_t *)request_buffer->client_cb; uint32_t payload_size = 0; - const void *payload = 0; + void *payload = NULL; if (block1) { payload = oc_blockwise_dispatch_block(request_buffer, @@ -701,7 +705,8 @@ coap_receive(oc_message_t *msg) coap_set_header_size1(response, request_buffer->payload_size); } if (oc_string_len(client_cb->query) > 0) { - coap_set_header_uri_query(response, oc_string(client_cb->query)); + coap_set_header_uri_query(response, oc_string(client_cb->query), + oc_string_len(client_cb->query)); } coap_set_header_accept(response, APPLICATION_VND_OCF_CBOR); coap_set_header_content_format(response, APPLICATION_VND_OCF_CBOR); @@ -772,8 +777,8 @@ coap_receive(oc_message_t *msg) coap_set_header_uri_path(response, oc_string(client_cb->uri), oc_string_len(client_cb->uri)); if (oc_string_len(client_cb->query) > 0) { - coap_set_header_uri_query(response, - oc_string(client_cb->query)); + coap_set_header_uri_query(response, oc_string(client_cb->query), + oc_string_len(client_cb->query)); } goto send_message; } @@ -883,6 +888,8 @@ coap_receive(oc_message_t *msg) memcpy(transaction->token, response->token, response->token_len); transaction->token_len = response->token_len; } + OC_DBG("data buffer from:%p to:%p", (void *)transaction->message->data, + (void *)(transaction->message->data + oc_message_buffer_size())); transaction->message->length = coap_serialize_message( response, transaction->message->data, oc_message_buffer_size()); if (transaction->message->length > 0) { diff --git a/messaging/coap/observe.c b/messaging/coap/observe.c index 759753dd2d..d7e34be9f4 100644 --- a/messaging/coap/observe.c +++ b/messaging/coap/observe.c @@ -537,7 +537,7 @@ send_notification(coap_observer_t *obs, oc_response_t *response, response_state->payload_size = response->response_buffer->response_length; uint32_t payload_size = 0; - const void *payload = oc_blockwise_dispatch_block( + void *payload = oc_blockwise_dispatch_block( response_state, 0, obs->block2_size, &payload_size); if (payload) { coap_set_payload(notification, payload, payload_size); @@ -1346,10 +1346,10 @@ coap_observe_handler(const coap_packet_t *request, /*---------------------------------------------------------------------------*/ bool -coap_want_be_notified(oc_resource_t *resource) +coap_want_be_notified(const oc_resource_t *resource) { #if defined(OC_DISCOVERY_RESOURCE_OBSERVABLE) && defined(OC_RES_BATCH_SUPPORT) - oc_resource_t *discover_resource = + const oc_resource_t *discover_resource = oc_core_get_resource_by_index(OCF_RES, resource->device); #endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE && OC_RES_BATCH_SUPPORT */ /* iterate over observers */ @@ -1365,7 +1365,8 @@ coap_want_be_notified(oc_resource_t *resource) } #endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE */ #if defined(OC_COLLECTIONS) && defined(OC_COLLECTIONS_IF_CREATE) - oc_rt_created_t *rtc = oc_rt_get_factory_create_for_resource(resource); + const oc_rt_created_t *rtc = + oc_rt_get_factory_create_for_resource(resource); if ((rtc != NULL) && (obs->resource == (oc_resource_t *)rtc->collection) && (obs->iface_mask & OC_IF_B)) { return true; diff --git a/messaging/coap/observe.h b/messaging/coap/observe.h index 2a5d19e7fe..383ef78824 100644 --- a/messaging/coap/observe.h +++ b/messaging/coap/observe.h @@ -95,7 +95,7 @@ void coap_notify_discovery_batch_observers(oc_resource_t *resource); int coap_notify_observers(oc_resource_t *resource, oc_response_buffer_t *response_buf, const oc_endpoint_t *endpoint); -bool coap_want_be_notified(oc_resource_t *resource); +bool coap_want_be_notified(const oc_resource_t *resource); void notify_resource_defaults_observer(oc_resource_t *resource, oc_interface_mask_t iface_mask, oc_response_buffer_t *response_buf); diff --git a/messaging/coap/oscore.c b/messaging/coap/oscore.c index b772c59828..3758c97b77 100644 --- a/messaging/coap/oscore.c +++ b/messaging/coap/oscore.c @@ -69,8 +69,8 @@ oscore_read_piv(const uint8_t *piv, uint8_t piv_len, uint64_t *ssn) { *ssn = 0; - uint8_t i, j = sizeof(uint64_t) - piv_len; - for (i = 0; i < piv_len; i++, j++) { + uint8_t j = sizeof(uint64_t) - piv_len; + for (uint8_t i = 0; i < piv_len; i++, j++) { memcpy((char *)ssn + j, &piv[i], 1); } diff --git a/messaging/coap/transactions.c b/messaging/coap/transactions.c index 16e7fb8a0e..84e69ec46d 100644 --- a/messaging/coap/transactions.c +++ b/messaging/coap/transactions.c @@ -211,10 +211,9 @@ coap_clear_transaction(coap_transaction_t *t) coap_transaction_t * coap_get_transaction_by_mid(uint16_t mid) { - coap_transaction_t *t = NULL; - - for (t = (coap_transaction_t *)oc_list_head(transactions_list); t; - t = t->next) { + for (coap_transaction_t *t = + (coap_transaction_t *)oc_list_head(transactions_list); + t != NULL; t = t->next) { if (t->mid == mid) { OC_DBG("Found transaction for MID %u: %p", t->mid, (void *)t); return t; @@ -226,10 +225,9 @@ coap_get_transaction_by_mid(uint16_t mid) coap_transaction_t * coap_get_transaction_by_token(uint8_t *token, uint8_t token_len) { - coap_transaction_t *t = NULL; - - for (t = (coap_transaction_t *)oc_list_head(transactions_list); t; - t = t->next) { + for (coap_transaction_t *t = + (coap_transaction_t *)oc_list_head(transactions_list); + t != NULL; t = t->next) { if (t->token_len == token_len && memcmp(t->token, token, token_len) == 0) { OC_DBG("Found transaction by token %p", (void *)t); return t; @@ -241,10 +239,9 @@ coap_get_transaction_by_token(uint8_t *token, uint8_t token_len) void coap_check_transactions(void) { - coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list), - *next; + coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list); while (t != NULL) { - next = t->next; + coap_transaction_t *next = t->next; if (oc_etimer_expired(&t->retrans_timer)) { ++(t->retrans_counter); OC_DBG("Retransmitting %u (%u)", t->mid, t->retrans_counter); @@ -262,10 +259,9 @@ coap_check_transactions(void) void coap_free_all_transactions(void) { - coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list), - *next; + coap_transaction_t *t = (coap_transaction_t *)oc_list_head(transactions_list); while (t != NULL) { - next = t->next; + coap_transaction_t *next = t->next; coap_clear_transaction(t); t = next; } diff --git a/port/android/tcpadapter.c b/port/android/tcpadapter.c index e465472e7a..ac802f9267 100644 --- a/port/android/tcpadapter.c +++ b/port/android/tcpadapter.c @@ -18,7 +18,7 @@ #define __USE_GNU -#include "tcpadapter.h" +#include "api/oc_buffer_internal.h" #include "api/oc_session_events_internal.h" #include "api/oc_tcp_internal.h" #include "ipcontext.h" @@ -27,6 +27,7 @@ #include "oc_session_events.h" #include "port/oc_assert.h" #include "port/oc_log_internal.h" +#include "tcpadapter.h" #include "util/oc_memb.h" #include #include @@ -424,11 +425,11 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) ret_with_code(ADAPTER_STATUS_ERROR); } total_length = get_total_length_from_header(message, &session->endpoint); - if (total_length > - (unsigned)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)) { - OC_ERR("total receive length(%zu) is bigger than max pdu size(%ld)", - total_length, (OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)); - OC_ERR("It may occur buffer overflow."); + // check to avoid buffer overflow + if (total_length > oc_message_buffer_size()) { + OC_ERR( + "total receive length(%zu) is bigger than message buffer size(%zu)", + total_length, oc_message_buffer_size()); free_tcp_session(session); ret_with_code(ADAPTER_STATUS_ERROR); } diff --git a/port/esp32/adapter/src/tcpadapter.c b/port/esp32/adapter/src/tcpadapter.c index 0ebe2bc01a..1d4dd02a33 100644 --- a/port/esp32/adapter/src/tcpadapter.c +++ b/port/esp32/adapter/src/tcpadapter.c @@ -18,7 +18,7 @@ #define __USE_GNU -#include "tcpadapter.h" +#include "api/oc_buffer_internal.h" #include "api/oc_session_events_internal.h" #include "api/oc_tcp_internal.h" #include "ipcontext.h" @@ -27,6 +27,7 @@ #include "oc_session_events.h" #include "port/oc_assert.h" #include "port/oc_log_internal.h" +#include "tcpadapter.h" #include "util/oc_memb.h" #include #include @@ -411,11 +412,11 @@ oc_tcp_receive_message(ip_context_t *dev, fd_set *fds, oc_message_t *message) ret_with_code(ADAPTER_STATUS_ERROR); } total_length = get_total_length_from_header(message, &session->endpoint); - if (total_length > - (unsigned)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)) { - OC_ERR("total receive length(%zu) is bigger than max pdu size(%ld)", - total_length, (OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)); - OC_ERR("It may occur buffer overflow."); + // check to avoid buffer overflow + if (total_length > oc_message_buffer_size()) { + OC_ERR( + "total receive length(%zu) is bigger than message buffer size(%zu)", + total_length, oc_message_buffer_size()); free_tcp_session(session); ret_with_code(ADAPTER_STATUS_ERROR); } diff --git a/port/linux/ipadapter.c b/port/linux/ipadapter.c index 4083c1fd78..2b8fdfec8a 100644 --- a/port/linux/ipadapter.c +++ b/port/linux/ipadapter.c @@ -127,12 +127,13 @@ add_ip_interface(int target_index) static bool check_new_ip_interfaces(void) { - struct ifaddrs *ifs = NULL, *interface = NULL; + struct ifaddrs *ifs = NULL; if (getifaddrs(&ifs) < 0) { OC_ERR("querying interface address"); return false; } - for (interface = ifs; interface != NULL; interface = interface->ifa_next) { + for (struct ifaddrs *interface = ifs; interface != NULL; + interface = interface->ifa_next) { /* Ignore interfaces that are down and the loopback interface */ if (!(interface->ifa_flags & IFF_UP) || (interface->ifa_flags & IFF_LOOPBACK)) { diff --git a/port/linux/tcpsession.c b/port/linux/tcpsession.c index 81d9b99cc3..7b685f3b84 100644 --- a/port/linux/tcpsession.c +++ b/port/linux/tcpsession.c @@ -18,6 +18,7 @@ #define __USE_GNU +#include "api/oc_buffer_internal.h" #include "api/oc_network_events_internal.h" #include "api/oc_session_events_internal.h" #include "api/oc_tcp_internal.h" @@ -413,6 +414,8 @@ tcp_session_receive_message_locked(tcp_session_t *session, OC_DBG("recv(): %zu bytes.", (size_t)count); message->length += (size_t)count; want_read -= (size_t)count; + OC_DBG("written message buffer from=%p to=%p", (void *)message->data, + (void *)(message->data + message->length)); if (total_length == 0) { memcpy(&message->endpoint, &session->endpoint, sizeof(oc_endpoint_t)); @@ -428,11 +431,10 @@ tcp_session_receive_message_locked(tcp_session_t *session, } total_length = get_total_length_from_header(message, &session->endpoint); // check to avoid buffer overflow - if (total_length > - (unsigned)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)) { - OC_ERR("total receive length(%zu) is bigger than max pdu size(%ld)", - total_length, - (long)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)); + if (total_length > oc_message_buffer_size()) { + OC_ERR( + "total receive length(%zu) is bigger than message buffer size(%zu)", + total_length, oc_message_buffer_size()); free_session_locked(session, true); return ADAPTER_STATUS_ERROR; } diff --git a/port/windows/tcpadapter.c b/port/windows/tcpadapter.c index 65486a799f..c95ea29673 100644 --- a/port/windows/tcpadapter.c +++ b/port/windows/tcpadapter.c @@ -17,6 +17,7 @@ ****************************************************************************/ #define WIN32_LEAN_AND_MEAN +#include "api/oc_buffer_internal.h" #include "api/oc_network_events_internal.h" #include "api/oc_session_events_internal.h" #include "api/oc_tcp_internal.h" @@ -547,12 +548,11 @@ recv_message_with_tcp_session(tcp_session_t *session, oc_message_t *message) return ADAPTER_STATUS_ERROR; } total_length = get_total_length_from_header(message, &session->endpoint); - if (total_length > - (unsigned)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)) { - OC_ERR("total receive length(%zu) is bigger than max pdu size(%zu)", - total_length, - (size_t)(OC_MAX_APP_DATA_SIZE + COAP_MAX_HEADER_SIZE)); - OC_ERR("It may occur buffer overflow."); + // check to avoid buffer overflow + if (total_length > oc_message_buffer_size()) { + OC_ERR( + "total receive length(%zu) is bigger than message buffer size(%zu)", + total_length, oc_message_buffer_size()); free_tcp_session(session); return ADAPTER_STATUS_ERROR; } diff --git a/security/oc_ael.c b/security/oc_ael.c index 7b2090b83c..5eb26256f2 100644 --- a/security/oc_ael.c +++ b/security/oc_ael.c @@ -122,8 +122,7 @@ oc_sec_ael_init(void) oc_abort("Insufficient memory"); } #endif /* OC_DYNAMIC_ALLOCATION */ - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { OC_LIST_STRUCT_INIT(&ael[device], events); } } @@ -131,8 +130,7 @@ oc_sec_ael_init(void) void oc_sec_ael_free(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_ael_reset(device); } #ifdef OC_DYNAMIC_ALLOCATION @@ -492,8 +490,7 @@ oc_sec_ael_create_event(size_t device, uint8_t category, uint8_t priority, oc_new_string(&res->message, message, strlen(message)); } if (aux_info && aux_size > 0) { - size_t i; - for (i = 0; i < aux_size; i++) { + for (size_t i = 0; i < aux_size; i++) { oc_sec_ael_aux_info_t *a_info = (oc_sec_ael_aux_info_t *)oc_memb_alloc(&aux_s); if (a_info) { diff --git a/security/oc_cred.c b/security/oc_cred.c index f4aafe8bac..82b8642dee 100644 --- a/security/oc_cred.c +++ b/security/oc_cred.c @@ -95,8 +95,7 @@ oc_sec_cred_init(void) oc_abort("Insufficient memory"); } #endif /* OC_DYNAMIC_ALLOCATION */ - size_t i; - for (i = 0; i < oc_core_get_num_devices(); i++) { + for (size_t i = 0; i < oc_core_get_num_devices(); i++) { OC_LIST_STRUCT_INIT(&devices[i], creds); } } @@ -319,8 +318,7 @@ oc_sec_cred_default(size_t device) void oc_sec_cred_free(void) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_cred_clear(device, NULL, NULL); } #ifdef OC_DYNAMIC_ALLOCATION @@ -1248,8 +1246,7 @@ is_valid_oscore_id(const char *id, size_t id_len) if (id_len != 14) { return false; } - size_t i; - for (i = 0; i < id_len; i++) { + for (size_t i = 0; i < id_len; i++) { if (!isxdigit(id[i])) { return false; } diff --git a/security/oc_obt.c b/security/oc_obt.c index 2700d3600c..acb945ceac 100644 --- a/security/oc_obt.c +++ b/security/oc_obt.c @@ -465,9 +465,8 @@ get_endpoints(oc_client_response_t *data) if (data->code >= OC_STATUS_BAD_REQUEST) { return; } - oc_rep_t *links = data->payload; - oc_uuid_t di; + oc_rep_t *links = data->payload; oc_rep_t *link = links != NULL ? links->value.object : NULL; while (link != NULL) { switch (link->type) { @@ -485,7 +484,7 @@ get_endpoints(oc_client_response_t *data) } const oc_uuid_t *my_uuid = oc_core_get_device_id(0); - if (memcmp(my_uuid->id, di.id, 16) == 0) { + if (memcmp(my_uuid->id, di.id, sizeof(di.id)) == 0) { return; } @@ -509,9 +508,11 @@ get_endpoints(oc_client_response_t *data) oc_free_server_endpoints(device->endpoint); device->endpoint = NULL; - oc_endpoint_t *eps_cur = NULL; + if (links == NULL) { + return; + } link = links->value.object; - oc_endpoint_t temp_ep; + oc_endpoint_t *eps_cur = NULL; while (link != NULL) { switch (link->type) { case OC_REP_OBJECT_ARRAY: { @@ -523,6 +524,7 @@ get_endpoints(oc_client_response_t *data) case OC_REP_STRING: { if (oc_string_len(ep->name) == 2 && memcmp(oc_string(ep->name), "ep", 2) == 0) { + oc_endpoint_t temp_ep; if (oc_string_to_endpoint(&ep->value.string, &temp_ep, NULL) == 0) { if (((data->endpoint->flags & IPV4) && @@ -3567,15 +3569,15 @@ oc_obt_general_post(const oc_uuid_t *uuid, const char *query, const char *url, oc_rep_start_root_object(); for (int i = 0; i < array_size; i++) { if (strstr(payload_types[i], "bool") != NULL) { - int payload_int = strtol(payload_values[i], NULL, 10); - bool payload_bool = (payload_int ? true : false); + long payload_int = strtol(payload_values[i], NULL, 10); + bool payload_bool = (payload_int != 0 ? true : false); oc_rep_encode_text_string(&root_map, payload_properties[i], strlen(payload_properties[i])); oc_rep_encode_boolean(&root_map, payload_bool); } else if (strstr(payload_types[i], "int") != NULL) { - int payload_int = strtol(payload_values[i], NULL, 10); + long payload_int = strtol(payload_values[i], NULL, 10); oc_rep_encode_text_string(&root_map, payload_properties[i], strlen(payload_properties[i])); diff --git a/security/oc_oscore_context.c b/security/oc_oscore_context.c index da1cf04770..3b6c36c5b2 100644 --- a/security/oc_oscore_context.c +++ b/security/oc_oscore_context.c @@ -110,7 +110,7 @@ oc_oscore_find_context_by_token_mid(size_t device, uint8_t *token, } oc_oscore_context_t * -oc_oscore_find_context_by_UUID(size_t device, oc_uuid_t *uuid) +oc_oscore_find_context_by_UUID(size_t device, const c_uuid_t *uuid) { oc_oscore_context_t *ctx = (oc_oscore_context_t *)oc_list_head(contexts); while (ctx != NULL) { @@ -235,9 +235,9 @@ oc_oscore_add_context(size_t device, const char *senderid, int oc_oscore_context_derive_param(const uint8_t *id, uint8_t id_len, - uint8_t *id_ctx, uint8_t id_ctx_len, - const char *type, uint8_t *secret, - uint8_t secret_len, uint8_t *salt, + const uint8_t *id_ctx, uint8_t id_ctx_len, + const char *type, const uint8_t *secret, + uint8_t secret_len, const uint8_t *salt, uint8_t salt_len, uint8_t *param, uint8_t param_len) { diff --git a/security/oc_oscore_context.h b/security/oc_oscore_context.h index cf29296155..ec11e5017a 100644 --- a/security/oc_oscore_context.h +++ b/security/oc_oscore_context.h @@ -56,9 +56,9 @@ typedef struct oc_oscore_context_t } oc_oscore_context_t; int oc_oscore_context_derive_param(const uint8_t *id, uint8_t id_len, - uint8_t *id_ctx, uint8_t id_ctx_len, - const char *type, uint8_t *secret, - uint8_t secret_len, uint8_t *salt, + const uint8_t *id_ctx, uint8_t id_ctx_len, + const char *type, const uint8_t *secret, + uint8_t secret_len, const uint8_t *salt, uint8_t salt_len, uint8_t *param, uint8_t param_len); @@ -70,7 +70,7 @@ oc_oscore_context_t *oc_oscore_add_context(size_t device, const char *senderid, void *cred, bool from_storagw); oc_oscore_context_t *oc_oscore_find_context_by_UUID(size_t device, - oc_uuid_t *uuid); + const oc_uuid_t *uuid); oc_oscore_context_t *oc_oscore_find_context_by_kid(oc_oscore_context_t *ctx, size_t device, diff --git a/security/oc_oscore_crypto.c b/security/oc_oscore_crypto.c index 465a01f256..2599ac187a 100644 --- a/security/oc_oscore_crypto.c +++ b/security/oc_oscore_crypto.c @@ -135,8 +135,8 @@ HKDF_Expand(const uint8_t *prk, const uint8_t *info, uint8_t info_len, int HKDF_SHA256(const uint8_t *salt, uint8_t salt_len, const uint8_t *ikm, - uint8_t ikm_len, uint8_t *info, uint8_t info_len, uint8_t *okm, - uint8_t okm_len) + uint8_t ikm_len, const uint8_t *info, uint8_t info_len, + uint8_t *okm, uint8_t okm_len) { uint8_t PRK[HMAC_SHA256_HASHLEN]; if (HKDF_Extract(salt, salt_len, ikm, ikm_len, PRK) != 0) { @@ -149,8 +149,9 @@ HKDF_SHA256(const uint8_t *salt, uint8_t salt_len, const uint8_t *ikm, } void -oc_oscore_AEAD_nonce(uint8_t *id, uint8_t id_len, uint8_t *piv, uint8_t piv_len, - uint8_t *civ, uint8_t *nonce, uint8_t nonce_len) +oc_oscore_AEAD_nonce(const uint8_t *id, uint8_t id_len, const uint8_t *piv, + uint8_t piv_len, const uint8_t *civ, uint8_t *nonce, + uint8_t nonce_len) { OC_DBG("### computing AEAD nonce ###"); OC_DBG("Sender ID:"); @@ -189,7 +190,7 @@ oc_oscore_AEAD_nonce(uint8_t *id, uint8_t id_len, uint8_t *piv, uint8_t piv_len, } int -oc_oscore_compose_AAD(uint8_t *kid, uint8_t kid_len, uint8_t *piv, +oc_oscore_compose_AAD(const uint8_t *kid, uint8_t kid_len, const uint8_t *piv, uint8_t piv_len, uint8_t *AAD, uint8_t *AAD_len) { uint8_t aad_array[OSCORE_AAD_MAX_LEN]; @@ -256,8 +257,8 @@ oc_oscore_compose_AAD(uint8_t *kid, uint8_t kid_len, uint8_t *piv, int oc_oscore_encrypt(uint8_t *plaintext, size_t plaintext_len, size_t tag_len, - uint8_t *key, size_t key_len, uint8_t *nonce, - size_t nonce_len, uint8_t *AAD, size_t AAD_len, + const uint8_t *key, size_t key_len, const uint8_t *nonce, + size_t nonce_len, const uint8_t *AAD, size_t AAD_len, uint8_t *output) { mbedtls_ccm_context ccm; @@ -277,10 +278,10 @@ oc_oscore_encrypt(uint8_t *plaintext, size_t plaintext_len, size_t tag_len, } int -oc_oscore_decrypt(uint8_t *ciphertext, size_t ciphertext_len, size_t tag_len, - uint8_t *key, size_t key_len, uint8_t *nonce, - size_t nonce_len, uint8_t *AAD, size_t AAD_len, - uint8_t *output) +oc_oscore_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, + size_t tag_len, const uint8_t *key, size_t key_len, + const uint8_t *nonce, size_t nonce_len, const uint8_t *AAD, + size_t AAD_len, uint8_t *output) { mbedtls_ccm_context ccm; mbedtls_ccm_init(&ccm); diff --git a/security/oc_oscore_crypto.h b/security/oc_oscore_crypto.h index a0d560fb5f..6b940025e5 100644 --- a/security/oc_oscore_crypto.h +++ b/security/oc_oscore_crypto.h @@ -27,24 +27,25 @@ extern "C" { #endif int HKDF_SHA256(const uint8_t *salt, uint8_t salt_len, const uint8_t *ikm, - uint8_t ikm_len, uint8_t *info, uint8_t info_len, uint8_t *okm, - uint8_t okm_len); + uint8_t ikm_len, const uint8_t *info, uint8_t info_len, + uint8_t *okm, uint8_t okm_len); -void oc_oscore_AEAD_nonce(uint8_t *id, uint8_t id_len, uint8_t *piv, - uint8_t piv_len, uint8_t *civ, uint8_t *nonce, +void oc_oscore_AEAD_nonce(const uint8_t *id, uint8_t id_len, const uint8_t *piv, + uint8_t piv_len, const uint8_t *civ, uint8_t *nonce, uint8_t nonce_len); -int oc_oscore_compose_AAD(uint8_t *kid, uint8_t kid_len, uint8_t *piv, - uint8_t piv_len, uint8_t *AAD, uint8_t *AAD_len); +int oc_oscore_compose_AAD(const uint8_t *kid, uint8_t kid_len, + const uint8_t *piv, uint8_t piv_len, uint8_t *AAD, + uint8_t *AAD_len); -int oc_oscore_decrypt(uint8_t *ciphertext, size_t ciphertext_len, - size_t tag_len, uint8_t *key, size_t key_len, - uint8_t *nonce, size_t nonce_len, uint8_t *AAD, - size_t AAD_len, uint8_t *output); +int oc_oscore_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, + size_t tag_len, const uint8_t *key, size_t key_len, + const uint8_t *nonce, size_t nonce_len, + const uint8_t *AAD, size_t AAD_len, uint8_t *output); int oc_oscore_encrypt(uint8_t *plaintext, size_t plaintext_len, size_t tag_len, - uint8_t *key, size_t key_len, uint8_t *nonce, - size_t nonce_len, uint8_t *AAD, size_t AAD_len, + const uint8_t *key, size_t key_len, const uint8_t *nonce, + size_t nonce_len, const uint8_t *AAD, size_t AAD_len, uint8_t *output); #ifdef __cplusplus diff --git a/security/oc_oscore_engine.c b/security/oc_oscore_engine.c index d0b84dec43..d2aaafb566 100644 --- a/security/oc_oscore_engine.c +++ b/security/oc_oscore_engine.c @@ -50,12 +50,11 @@ dump_cred(void *data) static bool check_if_replayed_request(oc_oscore_context_t *oscore_ctx, uint64_t piv) { - uint8_t i; if (piv == 0 && oscore_ctx->rwin[0] == 0 && oscore_ctx->rwin[OSCORE_REPLAY_WINDOW_SIZE - 1] == 0) { goto fresh_request; } - for (i = 0; i < OSCORE_REPLAY_WINDOW_SIZE; i++) { + for (uint8_t i = 0; i < OSCORE_REPLAY_WINDOW_SIZE; i++) { if (oscore_ctx->rwin[i] == piv) { return true; } @@ -791,7 +790,8 @@ oc_oscore_send_message(oc_message_t *msg) oc_uuid_to_str(&message->endpoint.di, uuid, OC_UUID_LEN); oc_string_t proxy_uri; oc_concat_strings(&proxy_uri, "ocf://", uuid); - coap_set_header_proxy_uri(coap_pkt, oc_string(proxy_uri)); + coap_set_header_proxy_uri(coap_pkt, oc_string(proxy_uri), + oc_string_len(proxy_uri)); /* Serialize OSCORE message to oc_message_t */ OC_DBG("### serializing OSCORE message ###"); diff --git a/security/oc_pstat.c b/security/oc_pstat.c index c17a3c4215..533f909e81 100644 --- a/security/oc_pstat.c +++ b/security/oc_pstat.c @@ -675,8 +675,7 @@ oc_reset_device_v1(size_t device, bool close_all_tls_connections_immediately) void oc_reset_v1(bool close_all_tls_connections_immediately) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_reset_device_v1(device, close_all_tls_connections_immediately); } } diff --git a/security/oc_tls.c b/security/oc_tls.c index 8e24ef16bc..e30ecc1405 100644 --- a/security/oc_tls.c +++ b/security/oc_tls.c @@ -292,9 +292,9 @@ oc_mbedtls_debug(void *ctx, int level, const char *file, int line, #endif /* OC_DEBUG */ static bool -is_peer_active(oc_tls_peer_t *peer) +is_peer_active(const oc_tls_peer_t *peer) { - oc_tls_peer_t *p = (oc_tls_peer_t *)oc_list_head(g_tls_peers); + const oc_tls_peer_t *p = (oc_tls_peer_t *)oc_list_head(g_tls_peers); while (p != NULL) { if (p == peer) { return true; @@ -714,7 +714,7 @@ oc_tls_pbkdf2(const unsigned char *pin, size_t pin_len, const oc_uuid_t *uuid, static void oc_tls_audit_log(const char *aeid, const char *message, uint8_t category, - uint8_t priority, oc_tls_peer_t *peer) + uint8_t priority, const oc_tls_peer_t *peer) { char buff[OC_IPADDR_BUFF_SIZE]; if (peer) { @@ -822,7 +822,7 @@ ssl_get_timer(void *ctx) } #ifdef OC_PKI -typedef bool (*check_if_known_cert_cb)(oc_sec_cred_t *cred); +typedef bool (*check_if_known_cert_cb)(const oc_sec_cred_t *cred); typedef void (*add_new_cert_cb)(oc_sec_cred_t *cred, size_t device); static void @@ -830,8 +830,7 @@ oc_tls_add_new_certs(oc_sec_credusage_t credusage, check_if_known_cert_cb is_known_cert, add_new_cert_cb add_new_cert) { - size_t device; - for (device = 0; device < oc_core_get_num_devices(); device++) { + for (size_t device = 0; device < oc_core_get_num_devices(); device++) { oc_sec_creds_t *creds = oc_sec_get_creds(device); oc_sec_cred_t *cred = (oc_sec_cred_t *)oc_list_head(creds->creds); for (; cred != NULL; cred = cred->next) { @@ -849,7 +848,7 @@ oc_tls_add_new_certs(oc_sec_credusage_t credusage, } static bool -is_known_identity_cert(oc_sec_cred_t *cred) +is_known_identity_cert(const oc_sec_cred_t *cred) { oc_x509_crt_t *certs = (oc_x509_crt_t *)oc_list_head(g_identity_certs); @@ -1042,7 +1041,7 @@ oc_tls_get_identity_cert_for_cred(const oc_sec_cred_t *cred) } bool -oc_tls_remove_identity_cert(oc_sec_cred_t *cred) +oc_tls_remove_identity_cert(const oc_sec_cred_t *cred) { oc_x509_crt_t *cert = oc_tls_find_identity_cert(cred); if (!cert) { @@ -1115,7 +1114,7 @@ oc_tls_reload_trust_anchors(void) } bool -oc_tls_remove_trust_anchor(oc_sec_cred_t *cred) +oc_tls_remove_trust_anchor(const oc_sec_cred_t *cred) { oc_x509_cacrt_t *cert = oc_tls_find_trust_anchor_for_cred(cred); if (cert == NULL) { @@ -1134,7 +1133,7 @@ oc_tls_remove_trust_anchor(oc_sec_cred_t *cred) #ifdef OC_TEST static oc_sec_cred_t * -oc_tls_find_cert_cred(size_t device, oc_sec_cred_t *cert_cred) +oc_tls_find_cert_cred(size_t device, const oc_sec_cred_t *cert_cred) { if (cert_cred == NULL) { return NULL; @@ -1200,7 +1199,7 @@ oc_tls_validate_identity_certs_consistency(void) } static oc_x509_cacrt_t * -oc_tls_find_ca_cert(mbedtls_x509_crt *cert) +oc_tls_find_ca_cert(const mbedtls_x509_crt *cert) { if (cert == NULL) { return NULL; @@ -1216,7 +1215,7 @@ oc_tls_find_ca_cert(mbedtls_x509_crt *cert) } static mbedtls_x509_crt * -oc_tls_find_trust_anchor(oc_x509_cacrt_t *cacert) +oc_tls_find_trust_anchor(const oc_x509_cacrt_t *cacert) { if (cacert == NULL) { return NULL; @@ -1367,9 +1366,9 @@ oc_tls_load_identity_cert_chain(mbedtls_ssl_config *conf, size_t device, } static bool -is_known_trust_anchor(oc_sec_cred_t *cred) +is_known_trust_anchor(const oc_sec_cred_t *cred) { - oc_x509_cacrt_t *cert = (oc_x509_cacrt_t *)oc_list_head(g_ca_certs); + const oc_x509_cacrt_t *cert = (oc_x509_cacrt_t *)oc_list_head(g_ca_certs); for (; cert != NULL; cert = cert->next) { if (cert->cred == cred) { diff --git a/security/oc_tls_internal.h b/security/oc_tls_internal.h index 3f13f382c7..bc6cb07dee 100644 --- a/security/oc_tls_internal.h +++ b/security/oc_tls_internal.h @@ -273,7 +273,7 @@ void oc_tls_resolve_new_identity_certs(void); * @return true identity certificate was found and removed * @return false otherwise */ -bool oc_tls_remove_identity_cert(oc_sec_cred_t *cred); +bool oc_tls_remove_identity_cert(const oc_sec_cred_t *cred); /** * @brief Get parsed mbedtls x509 certificate for given credential. @@ -304,7 +304,7 @@ void oc_tls_resolve_new_trust_anchors(void); * global mbedtls trust anchor chain was reloaded * @return false otherwise */ -bool oc_tls_remove_trust_anchor(oc_sec_cred_t *cred); +bool oc_tls_remove_trust_anchor(const oc_sec_cred_t *cred); /** * @brief Get parsed mbedtls x509 certificate for given credential. diff --git a/security/unittest/tls_cert_test.cpp b/security/unittest/tls_cert_test.cpp index 0be0ef6783..0fe52d5409 100644 --- a/security/unittest/tls_cert_test.cpp +++ b/security/unittest/tls_cert_test.cpp @@ -179,8 +179,7 @@ TEST_F(TestTlsCertificates, RemoveTrustAnchors) TEST_F(TestTlsCertificates, VerifyCredCerts) { - auto verify_cert_validity = [](const oc_sec_certs_data_t *data, - void *) -> bool { + auto verify_cert_validity = [](const oc_sec_certs_data_t *data, void *) { return (time_t)data->valid_from <= TestTlsCertificates::now_ && (time_t)data->valid_to > TestTlsCertificates::now_; }; diff --git a/tests/libfaketime/unittest/etimertest.cpp b/tests/libfaketime/unittest/etimertest.cpp index 35dab9e406..c0e9680ce7 100644 --- a/tests/libfaketime/unittest/etimertest.cpp +++ b/tests/libfaketime/unittest/etimertest.cpp @@ -36,9 +36,6 @@ class TestEventTimer : public testing::Test { public: static void SetUpTestCase() { - // TODO: remove me - oc_log_set_level(OC_LOG_LEVEL_DEBUG); - oc_clock_init(); oc::SetTestStartTime(); oc_process_init(); @@ -52,9 +49,6 @@ class TestEventTimer : public testing::Test { oc_process_exit(&oc_test_process); oc_process_exit(&oc_etimer_process); oc_process_shutdown(); - - // TODO: remove me - oc_log_set_level(OC_LOG_LEVEL_INFO); } static oc_clock_time_t Poll() diff --git a/util/oc_list.c b/util/oc_list.c index de8d960293..e0af5dd293 100644 --- a/util/oc_list.c +++ b/util/oc_list.c @@ -217,7 +217,7 @@ oc_list_pop(oc_list_t list) */ /*---------------------------------------------------------------------------*/ void -oc_list_remove(oc_list_t list, void *item) +oc_list_remove(oc_list_t list, const void *item) { struct list **l; diff --git a/util/oc_list.h b/util/oc_list.h index 7ac1c9cd9c..d06216c1c9 100644 --- a/util/oc_list.h +++ b/util/oc_list.h @@ -136,7 +136,7 @@ void oc_list_push(oc_list_t list, void *item); void *oc_list_chop(oc_list_t list); void oc_list_add(oc_list_t list, void *item); -void oc_list_remove(oc_list_t list, void *item); +void oc_list_remove(oc_list_t list, const void *item); void *oc_list_remove2(oc_list_t list, void *item); int oc_list_length(oc_list_t list); diff --git a/util/oc_memb.c b/util/oc_memb.c index d94b2271c1..a21dac5f39 100644 --- a/util/oc_memb.c +++ b/util/oc_memb.c @@ -145,14 +145,14 @@ _oc_memb_free( } /*---------------------------------------------------------------------------*/ int -oc_memb_inmemb(struct oc_memb *m, void *ptr) +oc_memb_inmemb(const struct oc_memb *m, const void *ptr) { return ((char *)ptr >= (char *)m->mem) && ((char *)ptr < ((char *)m->mem + (ptrdiff_t)(m->num * m->size))); } /*---------------------------------------------------------------------------*/ int -oc_memb_numfree(struct oc_memb *m) +oc_memb_numfree(const struct oc_memb *m) { int num_free = 0; for (unsigned short i = 0; i < m->num; ++i) { diff --git a/util/oc_memb.h b/util/oc_memb.h index 2d7aa6c3e7..163939ea37 100644 --- a/util/oc_memb.h +++ b/util/oc_memb.h @@ -185,9 +185,9 @@ char _oc_memb_free( void oc_memb_set_buffers_avail_cb(struct oc_memb *m, oc_memb_buffers_avail_callback_t cb); -int oc_memb_inmemb(struct oc_memb *m, void *ptr); +int oc_memb_inmemb(const struct oc_memb *m, const void *ptr); -int oc_memb_numfree(struct oc_memb *m); +int oc_memb_numfree(const struct oc_memb *m); #ifdef __cplusplus } diff --git a/util/oc_process.c b/util/oc_process.c index 7b6120936c..782220523e 100644 --- a/util/oc_process.c +++ b/util/oc_process.c @@ -136,7 +136,7 @@ oc_process_start(struct oc_process *p, oc_process_data_t data) } /*---------------------------------------------------------------------------*/ static void -exit_process(struct oc_process *p, struct oc_process *fromprocess) +exit_process(struct oc_process *p, const struct oc_process *fromprocess) { register struct oc_process *q; struct oc_process *old_current = oc_process_current; @@ -491,7 +491,7 @@ oc_process_poll(struct oc_process *p) } /*---------------------------------------------------------------------------*/ int -oc_process_is_running(struct oc_process *p) +oc_process_is_running(const struct oc_process *p) { return OC_ATOMIC_LOAD8(p->state) != OC_PROCESS_STATE_NONE; } diff --git a/util/oc_process.h b/util/oc_process.h index 1166161bca..89538d1c1c 100644 --- a/util/oc_process.h +++ b/util/oc_process.h @@ -531,7 +531,7 @@ int oc_process_run(void); * \retval Non-zero if the process is running. * \retval Zero if the process is not running. */ -int oc_process_is_running(struct oc_process *p); +int oc_process_is_running(const struct oc_process *p); /** * Number of events waiting to be processed.