diff --git a/examples/coap-client.c b/examples/coap-client.c index 3b317850..4d856792 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -371,6 +371,7 @@ event_handler(coap_session_t *session COAP_UNUSED, case COAP_EVENT_WS_PACKET_SIZE: case COAP_EVENT_WS_CLOSED: case COAP_EVENT_BAD_PACKET: + case COAP_EVENT_BLOCK_ISSUE: quit = 1; break; case COAP_EVENT_FIRST_PDU_FAIL: @@ -402,6 +403,8 @@ event_handler(coap_session_t *session COAP_UNUSED, default: break; } + if (quit) + coap_send_recv_terminate(); return 0; } diff --git a/examples/coap-server.c b/examples/coap-server.c index 942a65db..e46f3d8a 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1117,6 +1117,7 @@ proxy_event_handler(coap_session_t *session COAP_UNUSED, case COAP_EVENT_SESSION_FAILED: case COAP_EVENT_PARTIAL_BLOCK: case COAP_EVENT_XMIT_BLOCK_FAIL: + case COAP_EVENT_BLOCK_ISSUE: case COAP_EVENT_SERVER_SESSION_NEW: case COAP_EVENT_SERVER_SESSION_DEL: case COAP_EVENT_SERVER_SESSION_CONNECTED: diff --git a/include/coap3/coap_event.h b/include/coap3/coap_event.h index 32fac808..998cfd0d 100644 --- a/include/coap3/coap_event.h +++ b/include/coap3/coap_event.h @@ -75,6 +75,8 @@ typedef enum { COAP_EVENT_PARTIAL_BLOCK = 0x3001, /** Triggered when not all of a large body has been transmitted */ COAP_EVENT_XMIT_BLOCK_FAIL = 0x3002, + /** Triggered when a block transfer could not be handled */ + COAP_EVENT_BLOCK_ISSUE = 0x3003, /* * Server session events diff --git a/include/coap3/coap_net.h b/include/coap3/coap_net.h index 79d5ac63..959c9015 100644 --- a/include/coap3/coap_net.h +++ b/include/coap3/coap_net.h @@ -328,6 +328,15 @@ void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds); */ int coap_context_set_cid_tuple_change(coap_context_t *context, uint8_t every); +/** + * Set the maximum supported body size. + * + * @param context The coap_context_t object. + * @param max_body_size The maximum supported body size. 0 indicates unlimited. + */ +void coap_context_set_max_body_size(coap_context_t *context, + uint32_t max_body_size); + /** * Set the maximum token size (RFC8974). * diff --git a/include/coap3/coap_net_internal.h b/include/coap3/coap_net_internal.h index f42ceebb..1d08405b 100644 --- a/include/coap3/coap_net_internal.h +++ b/include/coap3/coap_net_internal.h @@ -224,8 +224,9 @@ struct coap_context_t { #endif /* COAP_CLIENT_SUPPORT */ uint32_t block_mode; /**< Zero or more COAP_BLOCK_ or'd options */ coap_resource_dynamic_create_t dyn_create_handler; /**< Dynamc resource create handler */ - uint32_t dynamic_cur; /* Current number of dynamic resources */ - uint32_t dynamic_max; /* Max number of dynamic resources or 0 is unlimited */ + uint32_t dynamic_cur; /**< Current number of dynamic resources */ + uint32_t dynamic_max; /**< Max number of dynamic resources or 0 is unlimited */ + uint32_t max_body_size; /**< Max supported body size or 0 is unlimited */ }; /** diff --git a/libcoap-3.map b/libcoap-3.map index 4d152523..572380fa 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -67,6 +67,7 @@ global: coap_context_set_csm_timeout_ms; coap_context_set_keepalive; coap_context_set_max_block_size; + coap_context_set_max_body_size; coap_context_set_max_handshake_sessions; coap_context_set_max_idle_sessions; coap_context_set_max_token_size; diff --git a/libcoap-3.sym b/libcoap-3.sym index 3c41ac21..297b5741 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -65,6 +65,7 @@ coap_context_set_csm_timeout coap_context_set_csm_timeout_ms coap_context_set_keepalive coap_context_set_max_block_size +coap_context_set_max_body_size coap_context_set_max_handshake_sessions coap_context_set_max_idle_sessions coap_context_set_max_token_size diff --git a/man/Makefile.am b/man/Makefile.am index 738838f1..cb013c54 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -117,6 +117,7 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_address.3" > coap_is_bcast.3 @echo ".so man3/coap_address.3" > coap_is_mcast.3 @echo ".so man3/coap_address.3" > coap_is_af_unix.3 + @echo ".so man3/coap_block.3" > coap_q_block_is_supported.3 @echo ".so man3/coap_block.3" > coap_register_block_data_handler.3 @echo ".so man3/coap_cache.3" > coap_cache_get_pdu.3 @echo ".so man3/coap_cache.3" > coap_cache_get_app_data.3 diff --git a/man/coap_block.txt.in b/man/coap_block.txt.in index 8b97b266..15259b7d 100644 --- a/man/coap_block.txt.in +++ b/man/coap_block.txt.in @@ -13,6 +13,7 @@ NAME coap_block, coap_context_set_block_mode, coap_context_set_max_block_size, +coap_context_set_max_body_size, coap_add_data_large_request, coap_add_data_large_request_app, coap_add_data_large_response, @@ -32,6 +33,9 @@ uint32_t _block_mode_);* *int coap_context_set_max_block_size(coap_context_t *_context_, size_t _max_block_size_);* +*void coap_context_set_max_body_size(coap_context_t *_context_, +uint32_t _max_body_size_);* + *int coap_add_data_large_request(coap_session_t *_session_, coap_pdu_t *_pdu_, size_t _length_, const uint8_t *_data_, coap_release_large_data_t _release_func_, void *_app_ptr_);* @@ -282,6 +286,15 @@ The *coap_context_set_max_block_size*() function is used to set the _max_block_size_ in the _context_ that a server supports when the Block1 or Block2 options are used. This must be set before a server session is created. +*Function: coap_context_set_max_body_size()* + +The *coap_context_set_max_body_size*() function is used to set the +_max_body_size_ in the _context_ that a server or client supports when the Block1 or +Block2 options are used. This must be set before a server session is created. +_max_body_size_ specifies the maximum size that the peer can handle after +re-assembling all the individual blocks. If set to 0 (the default), +_max_body_size_ is treated as unlimited. + *Function: coap_add_data_large_request()* The *coap_add_data_large_request*() function is similar to *coap_add_data*(), diff --git a/man/coap_call_home.txt.in b/man/coap_call_home.txt.in index 6a05dc03..5bdaac27 100644 --- a/man/coap_call_home.txt.in +++ b/man/coap_call_home.txt.in @@ -145,6 +145,7 @@ event_handler(coap_session_t *session COAP_UNUSED, case COAP_EVENT_SESSION_FAILED: case COAP_EVENT_PARTIAL_BLOCK: case COAP_EVENT_XMIT_BLOCK_FAIL: + case COAP_EVENT_BLOCK_ISSUE: case COAP_EVENT_SERVER_SESSION_NEW: case COAP_EVENT_SERVER_SESSION_DEL: case COAP_EVENT_MSG_RETRANSMITTED: diff --git a/man/coap_handler.txt.in b/man/coap_handler.txt.in index f29dda64..63f15ded 100644 --- a/man/coap_handler.txt.in +++ b/man/coap_handler.txt.in @@ -336,6 +336,8 @@ typedef enum { COAP_EVENT_PARTIAL_BLOCK = 0x3001, /** Triggered when not all of a large body has been transmitted */ COAP_EVENT_XMIT_BLOCK_FAIL = 0x3002, + /** Triggered when a block transfer could not be handled */ + COAP_EVENT_BLOCK_ISSUE = 0x3003, /* * Server session events diff --git a/src/coap_block.c b/src/coap_block.c index 57938abc..c9bcee1e 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -15,6 +15,8 @@ #include "coap3/coap_libcoap_build.h" +#include + #ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) #endif @@ -824,19 +826,21 @@ coap_add_data_large_internal(coap_session_t *session, if (COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_NON) pdu->type = COAP_MESSAGE_CON; - /* Block NUM max 20 bits and block size is "2**(SZX + 4)" + /* Block NUM max 20 bits (starting from 0) and block size is "2**(SZX + 4)" and using SZX max of 6 gives maximum size = 1,073,740,800 CSM Max-Message-Size theoretical maximum = 4,294,967,295 So, if using blocks, we are limited to 1,073,740,800. */ -#define MAX_BLK_LEN (((1UL << 20) - 1) * (1 << (6 + 4))) +#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4))) +#if UINT_MAX < MAX_BLK_LEN +#undef MAX_BLK_LEN +#define MAX_BLK_LEN UINT_MAX +#endif -#if UINT_MAX > MAX_BLK_LEN if (length > MAX_BLK_LEN) { coap_log_warn("Size of large buffer restricted to 0x%lx bytes\n", MAX_BLK_LEN); length = MAX_BLK_LEN; } -#endif /* UINT_MAX > MAX_BLK_LEN */ #if COAP_SERVER_SUPPORT /* Possible response code not yet set, so check if not request */ @@ -3164,6 +3168,19 @@ coap_handle_request_put_block(coap_context_t *context, if (!block_option || (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) { /* Not blocked, or a single block */ + if (context->max_body_size && total > context->max_body_size) { + uint8_t buf[4]; + + coap_update_option(response, + COAP_OPTION_SIZE1, + coap_encode_var_safe((uint8_t *)buf, sizeof(buf), + context->max_body_size), + (uint8_t *)buf); + response->code = COAP_RESPONSE_CODE(413); + coap_log_warn("Unable to handle data size %" PRIuS " (max %" PRIu32 ")\n", total, + context->max_body_size); + goto skip_app_handler; + } goto call_app_handler; } @@ -3194,6 +3211,37 @@ coap_handle_request_put_block(coap_context_t *context, } total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt), coap_opt_length(size_opt)) : 0; + if (total) { + uint32_t max_body; + + max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode); + if (max_block_szx == 0 || max_block_szx > block.szx) { + max_block_szx = block.szx; + } + max_body = ((1UL << 20) * (1 << (max_block_szx + 4))); + if (max_body > MAX_BLK_LEN) + max_body = MAX_BLK_LEN; + if ((context->max_body_size && total > context->max_body_size) || + (total > max_body)) { + /* Suggested body size larger than allowed */ + char buf[32]; + uint32_t max_body_size = context->max_body_size; + + if (max_body_size == 0 || max_body < max_body_size) { + max_body_size = max_body; + } + coap_update_option(response, + COAP_OPTION_SIZE1, + coap_encode_var_safe((uint8_t *)buf, sizeof(buf), + max_body_size), + (uint8_t *)buf); + snprintf(buf, sizeof(buf), "Max body size %" PRIu32, max_body_size); + coap_add_data(response, strlen(buf), (uint8_t *)buf); + response->code = COAP_RESPONSE_CODE(413); + coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", total, max_body_size); + goto skip_app_handler; + } + } offset = block.num << (block.szx + 4); if (!(session->block_mode & @@ -4190,6 +4238,31 @@ reinit: if (lg_crcv->total_len < size2) lg_crcv->total_len = size2; + /* Check whether we can handle this size */ + uint32_t max_body; + uint8_t max_block_szx; + + max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode); + if (max_block_szx == 0 || max_block_szx > block.szx) { + max_block_szx = block.szx; + } + max_body = ((1UL << 20) * (1 << (max_block_szx + 4))); + if (max_body > MAX_BLK_LEN) + max_body = MAX_BLK_LEN; + if ((context->max_body_size && size2 > context->max_body_size) || + (size2 > max_body)) { + uint32_t max_body_size = context->max_body_size; + + if (max_body_size == 0 || max_body < max_body_size) { + max_body_size = max_body; + } + coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", size2, max_body_size); + /* Try to hint to the server thare is an issue */ + coap_send_rst_lkd(session, rcvd); + coap_handle_event_lkd(session->context, COAP_EVENT_BLOCK_ISSUE, session); + return 1; + } + if (etag_opt) { if (!full_match(coap_opt_value(etag_opt), coap_opt_length(etag_opt), @@ -4481,6 +4554,14 @@ give_to_app: coap_opt_t *obs_opt = coap_check_option(rcvd, COAP_OPTION_OBSERVE, &opt_iter); + if (context->max_body_size && length > context->max_body_size) { + coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length, + context->max_body_size); + /* Try to hint to the server thare is an issue */ + coap_send_rst_lkd(session, rcvd); + coap_handle_event_lkd(session->context, COAP_EVENT_BLOCK_ISSUE, session); + return 1; + } if (obs_opt) { lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3); memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length); @@ -4542,6 +4623,9 @@ fail_resp: goto skip_app_handler; } } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) { + const uint8_t *data; + size_t length; + if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) { #if COAP_Q_BLOCK_SUPPORT if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) { @@ -4552,8 +4636,6 @@ fail_resp: have_block = 1; if (block.num != 0) { /* Assume random access and just give the single response to app */ - size_t length; - const uint8_t *data; size_t chunk = (size_t)1 << (block.szx + 4); coap_get_data(rcvd, &length, &data); @@ -4580,6 +4662,15 @@ fail_resp: COAP_RECURSE_NO); } } + coap_get_data(rcvd, &length, &data); + if (context->max_body_size && length > context->max_body_size) { + coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length, + context->max_body_size); + /* Try to hint to the server thare is an issue */ + coap_send_rst_lkd(session, rcvd); + coap_handle_event_lkd(session->context, COAP_EVENT_BLOCK_ISSUE, session); + return 1; + } track_echo(session, rcvd); } else if (rcvd->code == COAP_RESPONSE_CODE(401)) { lg_crcv = coap_block_new_lg_crcv(session, sent, NULL); diff --git a/src/coap_net.c b/src/coap_net.c index f915ca30..abdaeab2 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -476,12 +476,24 @@ coap_context_set_cid_tuple_change(coap_context_t *context, uint8_t every) { #endif /* ! COAP_CLIENT_SUPPORT */ } +void +coap_context_set_max_body_size(coap_context_t *context, + uint32_t max_body_size) { + assert(max_body_size == 0 || max_body_size > 1024); + if (max_body_size == 0 || max_body_size > 1024) { + context->max_body_size = max_body_size; + } +} + void coap_context_set_max_token_size(coap_context_t *context, size_t max_token_size) { assert(max_token_size >= COAP_TOKEN_DEFAULT_MAX && max_token_size <= COAP_TOKEN_EXT_MAX); - context->max_token_size = (uint32_t)max_token_size; + if (max_token_size >= COAP_TOKEN_DEFAULT_MAX && + max_token_size <= COAP_TOKEN_EXT_MAX) { + context->max_token_size = (uint32_t)max_token_size; + } } void @@ -4939,6 +4951,8 @@ coap_event_name(coap_event_t event) { return "COAP_EVENT_PARTIAL_BLOCK"; case COAP_EVENT_XMIT_BLOCK_FAIL: return "COAP_EVENT_XMIT_BLOCK_FAIL"; + case COAP_EVENT_BLOCK_ISSUE: + return "COAP_EVENT_BLOCK_ISSUE"; case COAP_EVENT_SERVER_SESSION_NEW: return "COAP_EVENT_SERVER_SESSION_NEW"; case COAP_EVENT_SERVER_SESSION_DEL: @@ -5052,6 +5066,7 @@ coap_handle_event_lkd(coap_context_t *context, coap_event_t event, case COAP_EVENT_SESSION_FAILED: case COAP_EVENT_PARTIAL_BLOCK: case COAP_EVENT_XMIT_BLOCK_FAIL: + case COAP_EVENT_BLOCK_ISSUE: break; case COAP_EVENT_SERVER_SESSION_NEW: /* Session will now be available as well - for call-home if not (D)TLS */