Skip to content

Commit cd5895a

Browse files
author
Tero Heinonen
committed
Merge pull request #17 from ARMmbed/timeout_api
API for changing DTLS handshake message timeouts.
2 parents a2588b6 + f5f70e2 commit cd5895a

File tree

9 files changed

+60
-18
lines changed

9 files changed

+60
-18
lines changed

coap-service/coap_service_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co
260260
*/
261261
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);
262262

263+
extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
263264
#ifdef __cplusplus
264265
}
265266
#endif

source/coap_connection_handler.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
typedef struct internal_socket_s {
1818
coap_conn_handler_t *parent;
1919

20+
uint32_t timeout_min;
21+
uint32_t timeout_max;
22+
2023
uint16_t listen_port;
2124
int8_t listen_socket;
2225

@@ -239,6 +242,8 @@ static internal_socket_t *int_socket_find_by_socket_id(int8_t id)
239242

240243
static internal_socket_t *int_socket_find(uint16_t port, bool is_secure, bool is_real_socket, bool bypassSec)
241244
{
245+
(void) bypassSec;
246+
242247
internal_socket_t *this = NULL;
243248
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
244249
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)
419424
coap_security_keys_t keys;
420425
keys._priv = pw;
421426
keys._priv_len = pw_len;
422-
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys);
427+
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max);
423428
//TODO: error handling
424429
}
425430
ns_dyn_mem_free(pw);
@@ -517,7 +522,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
517522
coap_security_keys_t keys;
518523
keys._priv = pw;
519524
keys._priv_len = pw_len;
520-
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys);
525+
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max);
521526
//TODO: error handling
522527
ns_dyn_mem_free(pw);
523528
return 0;
@@ -680,7 +685,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, ns_address_t
680685
coap_security_keys_t keys;
681686
keys._priv = pw;
682687
keys._priv_len = pw_len;
683-
coap_security_handler_connect_non_blocking(session->sec_handler, false, DTLS, keys);
688+
coap_security_handler_connect_non_blocking(session->sec_handler, false, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max);
684689
ns_dyn_mem_free(pw);
685690
return -2;
686691
}else{
@@ -720,3 +725,14 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int
720725
}
721726
return false;
722727
}
728+
729+
int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max)
730+
{
731+
if(!handler || !handler->socket){
732+
return -1;
733+
}
734+
handler->socket->timeout_max = max;
735+
handler->socket->timeout_min = min;
736+
737+
return 0;
738+
}

source/coap_security_handler.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSo
356356
return ret;
357357
}
358358

359-
int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys){
359+
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)
360+
{
360361

361362
if( !sec ){
362363
return -1;
@@ -380,8 +381,13 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
380381
return -1;
381382
}
382383

383-
//TODO: This should probably be modifiable by service???
384-
mbedtls_ssl_conf_handshake_timeout( &sec->_conf, 10000, 29000 );
384+
if(!timeout_max && !timeout_min){
385+
mbedtls_ssl_conf_handshake_timeout( &sec->_conf, 10000, 29000 );
386+
}
387+
else{
388+
mbedtls_ssl_conf_handshake_timeout( &sec->_conf, timeout_min, timeout_max );
389+
}
390+
385391
mbedtls_ssl_conf_rng( &sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg );
386392

387393
if( ( mbedtls_ssl_setup( &sec->_ssl, &sec->_conf ) ) != 0 )

source/coap_service_api.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t
275275
int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options,
276276
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb)
277277
{
278+
(void) interface_id;
279+
278280
coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t));
279281
if (!this) {
280282
return -1;
@@ -320,6 +322,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
320322
}
321323

322324
ns_list_add_to_start(&instance_list, this);
325+
323326
return id;
324327
}
325328

@@ -452,3 +455,12 @@ int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hd
452455
return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len);
453456
}
454457

458+
int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max)
459+
{
460+
coap_service_t *this = service_find(service_id);
461+
if(!this){
462+
return -1;
463+
}
464+
465+
return coap_connection_handler_set_timeout(this->conn_handler, min, max);
466+
}

source/include/coap_connection_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
5858

5959
bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int8_t socket_id);
6060

61+
int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max);
62+
6163
#endif

source/include/coap_security_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void coap_security_destroy(coap_security_t *sec);
106106

107107
int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys);
108108

109-
int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys);
109+
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);
110110

111111
int coap_security_handler_continue_connecting(coap_security_t *sec);
112112

test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bool test_coap_security_handler_connect()
8585
coap_security_keys_t keys;
8686
keys._priv = &pw;
8787
keys._priv_len = 3;
88-
if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys) )
88+
if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1) )
8989
return false;
9090
mbedtls_stub.useCounter = true;
9191
mbedtls_stub.counter = 0;
@@ -98,18 +98,18 @@ bool test_coap_security_handler_connect()
9898
mbedtls_stub.retArray[6] = -1;
9999
mbedtls_stub.retArray[7] = -1;
100100

101-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
101+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
102102
return false;
103103

104104
mbedtls_stub.counter = 0;
105105
mbedtls_stub.retArray[0] = 0;
106-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
106+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
107107
return false;
108108

109109
mbedtls_stub.counter = 0;
110110
// mbedtls_stub.retArray[0] = 0;
111111
mbedtls_stub.retArray[1] = 0;
112-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
112+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
113113
return false;
114114

115115
simple_cookie_t c;
@@ -121,7 +121,7 @@ bool test_coap_security_handler_connect()
121121
// mbedtls_stub.retArray[0] = 0;
122122
// mbedtls_stub.retArray[1] = 0;
123123
mbedtls_stub.retArray[2] = 0;
124-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
124+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
125125
return false;
126126

127127
c.len = 8;
@@ -135,7 +135,7 @@ bool test_coap_security_handler_connect()
135135
// mbedtls_stub.retArray[1] = 0;
136136
// mbedtls_stub.retArray[2] = 0;
137137
mbedtls_stub.retArray[3] = 0;
138-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
138+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
139139
return false;
140140

141141
mbedtls_stub.counter = 0;
@@ -144,7 +144,7 @@ bool test_coap_security_handler_connect()
144144
// mbedtls_stub.retArray[2] = 0;
145145
// mbedtls_stub.retArray[3] = 0;
146146
mbedtls_stub.retArray[4] = 0;
147-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
147+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
148148
return false;
149149

150150
mbedtls_stub.counter = 0;
@@ -155,19 +155,19 @@ bool test_coap_security_handler_connect()
155155
// mbedtls_stub.retArray[4] = 0;
156156
mbedtls_stub.retArray[6] = 0;
157157
mbedtls_stub.retArray[7] = 0;
158-
if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
158+
if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
159159
return false;
160160

161161
mbedtls_stub.counter = 0;
162162
mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
163163

164-
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
164+
if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
165165
return false;
166166

167167
mbedtls_stub.counter = 0;
168168
mbedtls_stub.retArray[5] = HANDSHAKE_FINISHED_VALUE;
169169

170-
if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys) )
170+
if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
171171
return false;
172172

173173
coap_security_destroy(handle);

test/coap-service/unittest/stub/coap_connection_handler_stub.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int
5555
{
5656
return thread_conn_handler_stub.bool_value;
5757
}
58+
59+
int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max)
60+
{
61+
return 0;
62+
}

test/coap-service/unittest/stub/coap_security_handler_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void coap_security_destroy(coap_security_t *sec)
3030

3131
}
3232

33-
int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys)
33+
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)
3434
{
3535
sec->_is_started = true;
3636
if( coap_security_handler_stub.counter >= 0){

0 commit comments

Comments
 (0)