API for changing DTLS handshake message timeouts.

pull/3240/head
Tero Heinonen 2016-02-18 20:12:25 +02:00
parent a2588b622d
commit f5f70e2d8b
9 changed files with 60 additions and 18 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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 )

View File

@ -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);
}

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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){