From f5f70e2d8b293107784f1c087268aea68a3e2a13 Mon Sep 17 00:00:00 2001 From: Tero Heinonen Date: Thu, 18 Feb 2016 20:12:25 +0200 Subject: [PATCH] API for changing DTLS handshake message timeouts. --- coap-service/coap_service_api.h | 1 + source/coap_connection_handler.c | 22 ++++++++++++++++--- source/coap_security_handler.c | 12 +++++++--- source/coap_service_api.c | 12 ++++++++++ source/include/coap_connection_handler.h | 2 ++ source/include/coap_security_handler.h | 2 +- .../test_coap_security_handler.c | 20 ++++++++--------- .../stub/coap_connection_handler_stub.c | 5 +++++ .../stub/coap_security_handler_stub.c | 2 +- 9 files changed, 60 insertions(+), 18 deletions(-) diff --git a/coap-service/coap_service_api.h b/coap-service/coap_service_api.h index 9e1c92d516..f5bc321d4b 100644 --- a/coap-service/coap_service_api.h +++ b/coap-service/coap_service_api.h @@ -260,6 +260,7 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co */ extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len); +extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max); #ifdef __cplusplus } #endif diff --git a/source/coap_connection_handler.c b/source/coap_connection_handler.c index 2b0cbe1306..58aa611f05 100644 --- a/source/coap_connection_handler.c +++ b/source/coap_connection_handler.c @@ -17,6 +17,9 @@ typedef struct internal_socket_s { coap_conn_handler_t *parent; + uint32_t timeout_min; + uint32_t timeout_max; + uint16_t listen_port; int8_t listen_socket; @@ -239,6 +242,8 @@ static internal_socket_t *int_socket_find_by_socket_id(int8_t id) static internal_socket_t *int_socket_find(uint16_t port, bool is_secure, bool is_real_socket, bool bypassSec) { + (void) bypassSec; + internal_socket_t *this = NULL; ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) { if( cur_ptr->listen_port == port && cur_ptr->real_socket == is_real_socket && @@ -419,7 +424,7 @@ static void secure_recv_sckt_msg(void *cb_res) coap_security_keys_t keys; keys._priv = pw; keys._priv_len = pw_len; - coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys); + coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max); //TODO: error handling } ns_dyn_mem_free(pw); @@ -517,7 +522,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a coap_security_keys_t keys; keys._priv = pw; keys._priv_len = pw_len; - coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys); + coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max); //TODO: error handling ns_dyn_mem_free(pw); return 0; @@ -680,7 +685,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, ns_address_t coap_security_keys_t keys; keys._priv = pw; keys._priv_len = pw_len; - coap_security_handler_connect_non_blocking(session->sec_handler, false, DTLS, keys); + coap_security_handler_connect_non_blocking(session->sec_handler, false, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max); ns_dyn_mem_free(pw); return -2; }else{ @@ -720,3 +725,14 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int } return false; } + +int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max) +{ + if(!handler || !handler->socket){ + return -1; + } + handler->socket->timeout_max = max; + handler->socket->timeout_min = min; + + return 0; +} diff --git a/source/coap_security_handler.c b/source/coap_security_handler.c index 3834030a6d..de2f58ddb3 100644 --- a/source/coap_security_handler.c +++ b/source/coap_security_handler.c @@ -356,7 +356,8 @@ int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSo return ret; } -int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys){ +int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max) +{ if( !sec ){ return -1; @@ -380,8 +381,13 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser return -1; } - //TODO: This should probably be modifiable by service??? - mbedtls_ssl_conf_handshake_timeout( &sec->_conf, 10000, 29000 ); + if(!timeout_max && !timeout_min){ + mbedtls_ssl_conf_handshake_timeout( &sec->_conf, 10000, 29000 ); + } + else{ + mbedtls_ssl_conf_handshake_timeout( &sec->_conf, timeout_min, timeout_max ); + } + mbedtls_ssl_conf_rng( &sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg ); if( ( mbedtls_ssl_setup( &sec->_ssl, &sec->_conf ) ) != 0 ) diff --git a/source/coap_service_api.c b/source/coap_service_api.c index 5eda0af1f9..c6cd34620d 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -275,6 +275,8 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb) { + (void) interface_id; + coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t)); if (!this) { return -1; @@ -320,6 +322,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_ } ns_list_add_to_start(&instance_list, this); + return id; } @@ -452,3 +455,12 @@ int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hd return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len); } +int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max) +{ + coap_service_t *this = service_find(service_id); + if(!this){ + return -1; + } + + return coap_connection_handler_set_timeout(this->conn_handler, min, max); +} diff --git a/source/include/coap_connection_handler.h b/source/include/coap_connection_handler.h index a378bbf87b..1efca984e1 100644 --- a/source/include/coap_connection_handler.h +++ b/source/include/coap_connection_handler.h @@ -58,4 +58,6 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int8_t socket_id); +int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max); + #endif diff --git a/source/include/coap_security_handler.h b/source/include/coap_security_handler.h index 609ae426ba..0b8f533f12 100644 --- a/source/include/coap_security_handler.h +++ b/source/include/coap_security_handler.h @@ -106,7 +106,7 @@ void coap_security_destroy(coap_security_t *sec); int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys); -int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys); +int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max); int coap_security_handler_continue_connecting(coap_security_t *sec); diff --git a/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c b/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c index 36d06bb4f6..6585a9ed4c 100644 --- a/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c +++ b/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c @@ -85,7 +85,7 @@ bool test_coap_security_handler_connect() coap_security_keys_t keys; keys._priv = &pw; keys._priv_len = 3; - if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.useCounter = true; mbedtls_stub.counter = 0; @@ -98,18 +98,18 @@ bool test_coap_security_handler_connect() mbedtls_stub.retArray[6] = -1; mbedtls_stub.retArray[7] = -1; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; // mbedtls_stub.retArray[0] = 0; mbedtls_stub.retArray[1] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; simple_cookie_t c; @@ -121,7 +121,7 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[0] = 0; // mbedtls_stub.retArray[1] = 0; mbedtls_stub.retArray[2] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; c.len = 8; @@ -135,7 +135,7 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[1] = 0; // mbedtls_stub.retArray[2] = 0; mbedtls_stub.retArray[3] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; @@ -144,7 +144,7 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[2] = 0; // mbedtls_stub.retArray[3] = 0; mbedtls_stub.retArray[4] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; @@ -155,19 +155,19 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[4] = 0; mbedtls_stub.retArray[6] = 0; mbedtls_stub.retArray[7] = 0; - if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; mbedtls_stub.counter = 0; mbedtls_stub.retArray[5] = HANDSHAKE_FINISHED_VALUE; - if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) ) + if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) return false; coap_security_destroy(handle); diff --git a/test/coap-service/unittest/stub/coap_connection_handler_stub.c b/test/coap-service/unittest/stub/coap_connection_handler_stub.c index 0f7e1e8c29..ec082117b4 100644 --- a/test/coap-service/unittest/stub/coap_connection_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_connection_handler_stub.c @@ -55,3 +55,8 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int { return thread_conn_handler_stub.bool_value; } + +int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max) +{ + return 0; +} diff --git a/test/coap-service/unittest/stub/coap_security_handler_stub.c b/test/coap-service/unittest/stub/coap_security_handler_stub.c index ba94d493ef..1583dc0aa7 100644 --- a/test/coap-service/unittest/stub/coap_security_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_security_handler_stub.c @@ -30,7 +30,7 @@ void coap_security_destroy(coap_security_t *sec) } -int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys) +int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max) { sec->_is_started = true; if( coap_security_handler_stub.counter >= 0){