diff --git a/examples/contiki/coap-observer.c b/examples/contiki/coap-observer.c index b71cab4c..b399f84c 100644 --- a/examples/contiki/coap-observer.c +++ b/examples/contiki/coap-observer.c @@ -133,7 +133,7 @@ PROCESS_THREAD(coap_server_process, ev, data) request = coap_pdu_init(COAP_MESSAGE_CON, COAP_REQUEST_GET, coap_new_message_id(coap_context), - COAP_PDU_SIZE_DYNAMIC); + COAP_DEFAULT_MTU); coap_split_uri((unsigned char *)resource, strlen(resource), &uri); diff --git a/examples/tiny.c b/examples/tiny.c index 5a196a64..0f84b7a5 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -29,7 +29,7 @@ make_pdu( unsigned int value ) { static unsigned char buf[20]; int len, ls; - if (!(pdu = coap_pdu_init(0, 0, 0, COAP_PDU_SIZE_DYNAMIC))) + if (!(pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU))) return NULL; pdu->type = COAP_MESSAGE_NON; diff --git a/include/coap/pdu.h b/include/coap/pdu.h index 06cb0b4f..30436ac6 100644 --- a/include/coap/pdu.h +++ b/include/coap/pdu.h @@ -32,8 +32,12 @@ struct coap_session_t; #define COAP_DEFAULT_MTU 1152 #endif /* COAP_DEFAULT_MTU */ #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE +#if defined(WITH_CONTIKI) || defined(WITH_LWIP) +#define COAP_DEFAULT_MAX_PDU_RX_SIZE 65804 +#else #define COAP_DEFAULT_MAX_PDU_RX_SIZE 8*1024*1024 -#endif /* COAP_DEFAULT_MTU */ +#endif +#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */ #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */ #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */ @@ -309,8 +313,6 @@ typedef uint8_t coap_proto_t; #define COAP_PROTO_TCP 3 #define COAP_PROTO_TLS 4 -#define COAP_PDU_SIZE_DYNAMIC ((size_t)-1) - /** * Creates a new CoAP PDU with at least enough storage space for the given * @p size maximum message size. The function returns a pointer to the @@ -323,8 +325,6 @@ typedef uint8_t coap_proto_t; * @param code The message code. * @param tid The transcation id to set or 0 if unknown / not applicable. * @param size The maximum allowed number of byte for the message. - * Use COAP_PDU_SIZE_DYNAMIC to get a variable size PDU that - * will grow automatically. * @return A pointer to the new PDU object or @c NULL on error. */ coap_pdu_t * diff --git a/src/coap_session.c b/src/coap_session.c index e29f5d15..42aa6a1d 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -133,13 +133,15 @@ void coap_session_free(coap_session_t *session) { } size_t coap_session_max_pdu_size(coap_session_t *session) { - if (session->mtu > 0) - return (size_t)(session->mtu - session->tls_overhead); - return COAP_PDU_SIZE_DYNAMIC; + return (size_t)(session->mtu - session->tls_overhead); } void coap_session_set_mtu(coap_session_t *session, unsigned mtu) { - session->mtu = (uint16_t)mtu; +#if defined(WITH_CONTIKI) || defined(WITH_LWIP) + if (mtu >= 65805) + mtu = 65804; +#endif + session->mtu = mtu; if (session->tls_overhead >= session->mtu) { session->tls_overhead = session->mtu; coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n"); diff --git a/src/net.c b/src/net.c index 05b32642..3cd9ed53 100644 --- a/src/net.c +++ b/src/net.c @@ -1942,8 +1942,8 @@ handle_signaling(coap_context_t *context, coap_session_t *session, if (pdu->code == COAP_SIGNALING_CSM) { while ((option = coap_option_next(&opt_iter))) { if (opt_iter.type == COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE) { - session->mtu = coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option)); + coap_session_set_mtu(session, coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); } else if (opt_iter.type == COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER) { /* ... */ } diff --git a/src/pdu.c b/src/pdu.c index a37c0592..67c5aefa 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -84,18 +84,13 @@ coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) { coap_pdu_t *pdu; uint8_t *buf; -#ifdef WITH_CONTIKI - if (size == COAP_PDU_SIZE_DYNAMIC) - size = COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE; - assert(size <= COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE); - if (size > COAP_RXBUFFER_SIZE - COAP_PDU_MAX_UDP_HEADER_SIZE) - return NULL; -#endif - pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t)); if (!pdu) return NULL; #if defined(WITH_CONTIKI) || defined(WITH_LWIP) + assert(size < 65805); + if (size >= 65805) + return NULL; pdu->max_hdr_size = COAP_PDU_MAX_UDP_HEADER_SIZE; #else pdu->max_hdr_size = COAP_PDU_MAX_TCP_HEADER_SIZE; @@ -108,16 +103,8 @@ coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) { pdu = NULL; } pdu->token = pdu->pbuf->payload + pdu->max_hdr_size; - if (size == COAP_PDU_SIZE_DYNAMIC) - size = pbuf->tot_len - pdu->max_hdr_size; - assert(size <= pbuf->tot_len - pdu->max_hdr_size); - if (size > pbuf->tot_len - pdu->max_hdr_size) - return NULL; #else /* WITH_LWIP */ - if (size != COAP_PDU_SIZE_DYNAMIC) - pdu->alloc_size = size; - else - pdu->alloc_size = 256; + pdu->alloc_size = min(size, 256); buf = coap_malloc_type(COAP_PDU_BUF, pdu->alloc_size + pdu->max_hdr_size); if (buf == NULL) { coap_free_type(COAP_PDU, pdu); @@ -192,6 +179,11 @@ coap_pdu_check_resize(coap_pdu_t *pdu, size_t size) { size_t new_size = max(256, pdu->alloc_size * 2); while (size > new_size) new_size *= 2; + if (pdu->max_size && new_size > pdu->max_size) { + new_size = pdu->max_size; + if (new_size < size) + return 0; + } if (!coap_pdu_resize(pdu, new_size)) return 0; } diff --git a/tests/test_error_response.c b/tests/test_error_response.c index b21f0be3..dde9ffb2 100644 --- a/tests/test_error_response.c +++ b/tests/test_error_response.c @@ -321,7 +321,7 @@ t_error_response8(void) { static int t_error_response_tests_create(void) { - pdu = coap_pdu_init(0, 0, 0, COAP_PDU_SIZE_DYNAMIC); + pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU); return pdu == NULL; } diff --git a/tests/test_pdu.c b/tests/test_pdu.c index b887e6e4..a83851d2 100644 --- a/tests/test_pdu.c +++ b/tests/test_pdu.c @@ -618,7 +618,7 @@ t_encode_pdu11(void) { static int t_pdu_tests_create(void) { - pdu = coap_pdu_init(0, 0, 0, COAP_PDU_SIZE_DYNAMIC); + pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU); return pdu == NULL; }