Skip to content

Commit 58f0ed5

Browse files
author
Tero Heinonen
authored
Limit number of ongoing handshakes (ARMmbed#77)
* Limit number of ongoing handshakes Set limit for ongoing handshakes to save memory during multiple simultaneous handshake attempts. * API to set handshake limits Added API to change handshake limit parameters. Default values are defined in coap_connection_handler.h * Unittests updated
1 parent 42c1169 commit 58f0ed5

File tree

8 files changed

+76
-6
lines changed

8 files changed

+76
-6
lines changed

coap-service/coap_service_api.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,19 @@ extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
297297
*/
298298
extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
299299

300+
/**
301+
* \brief Set DTLS handshake limit values
302+
*
303+
* Configures the limits for DTLS sessions. Values must be > 0.
304+
*
305+
* \param handshakes_max Maximum amount of simultaneous handshakes.
306+
* \param connections_max Maximum amount of sessions.
307+
*
308+
* \return -1 For failure
309+
*- 0 For success
310+
*/
311+
extern int8_t coap_service_handshake_limits_set(uint8_t handshakes_max, uint8_t connections_max);
312+
300313
/**
301314
* \brief Set CoAP duplication message buffer size
302315
*

source/coap_connection_handler.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const uint8_t COAP_MULTICAST_ADDR_SITE_LOCAL[16] = { 0xff, 0x05, [15] = 0xfd };
4848

4949
static NS_LIST_DEFINE(socket_list, internal_socket_t, link);
5050

51+
static uint8_t max_handshakes = MAX_ONGOING_HANDSHAKES;
52+
static uint8_t max_sessions = MAX_SECURE_SESSION_COUNT;
53+
5154
static void timer_cb(void* param);
5255

5356
static void recv_sckt_msg(void *cb_res);
@@ -143,11 +146,12 @@ static int8_t virtual_socket_id_allocate()
143146

144147
static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port, SecureConnectionMode secure_mode)
145148
{
149+
uint8_t handshakes = 0;
146150
if(!address_ptr){
147151
return NULL;
148152
}
149153

150-
if(MAX_SECURE_SESSION_COUNT <= ns_list_count(&secure_session_list)){
154+
if(max_sessions <= ns_list_count(&secure_session_list)){
151155
// Seek & destroy oldest session where close notify have been sent
152156
secure_session_t *to_be_removed = NULL;
153157
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
@@ -164,6 +168,16 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
164168
secure_session_delete(to_be_removed);
165169
}
166170

171+
// Count for ongoing handshakes
172+
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
173+
if(cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING){
174+
handshakes++;
175+
}
176+
}
177+
if(handshakes >= max_handshakes) {
178+
return NULL;
179+
}
180+
167181
secure_session_t *this = ns_dyn_mem_alloc(sizeof(secure_session_t));
168182
if (!this) {
169183
return NULL;
@@ -939,22 +953,35 @@ int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_
939953
return 0;
940954
}
941955

956+
int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, uint8_t connections_limit)
957+
{
958+
if (!handshakes_limit || !connections_limit) {
959+
return -1;
960+
}
961+
max_handshakes = handshakes_limit;
962+
max_sessions = connections_limit;
963+
964+
return 0;
965+
}
966+
942967
/* No need to call every second - call rather like every minute (SECURE_SESSION_CLEAN_INTERVAL sets this) */
943968
void coap_connection_handler_exec(uint32_t time)
944969
{
945970
if(ns_list_count(&secure_session_list)){
946971
// Seek & destroy old sessions where close notify have been sent
947972
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
948-
if(cur_ptr->session_state == SECURE_SESSION_CLOSED ||
949-
cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING){
973+
if(cur_ptr->session_state == SECURE_SESSION_CLOSED) {
950974
if((cur_ptr->last_contact_time + CLOSED_SECURE_SESSION_TIMEOUT) <= time){
951975
secure_session_delete(cur_ptr);
952976
}
953-
}
954-
if(cur_ptr->session_state == SECURE_SESSION_OK){
977+
} else if(cur_ptr->session_state == SECURE_SESSION_OK){
955978
if((cur_ptr->last_contact_time + OPEN_SECURE_SESSION_TIMEOUT) <= time){
956979
secure_session_delete(cur_ptr);
957980
}
981+
} else if(cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING){
982+
if((cur_ptr->last_contact_time + ONGOING_HANDSHAKE_TIMEOUT) <= time){
983+
secure_session_delete(cur_ptr);
984+
}
958985
}
959986
}
960987
}

source/coap_service_api.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint3
531531
return coap_connection_handler_set_timeout(this->conn_handler, min, max);
532532
}
533533

534+
int8_t coap_service_handshake_limits_set(uint8_t handshakes_max, uint8_t connections_max)
535+
{
536+
return coap_connection_handler_handshake_limits_set(handshakes_max, connections_max);
537+
}
538+
534539
int8_t coap_service_set_duplicate_message_buffer(int8_t service_id, uint8_t size)
535540
{
536541
(void) service_id;

source/include/coap_connection_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include "coap_security_handler.h"
2828

2929
#define MAX_SECURE_SESSION_COUNT 3
30+
#define MAX_ONGOING_HANDSHAKES 2
3031
#define CLOSED_SECURE_SESSION_TIMEOUT 3600 // Seconds
31-
#define OPEN_SECURE_SESSION_TIMEOUT 18000 // Seconds
32+
#define ONGOING_HANDSHAKE_TIMEOUT 600 // Seconds
33+
#define OPEN_SECURE_SESSION_TIMEOUT 18000 // Seconds
3234
#define SECURE_SESSION_CLEAN_INTERVAL 60 // Seconds
3335

3436
struct internal_socket_s;
@@ -71,6 +73,8 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int
7173

7274
int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max);
7375

76+
int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, uint8_t connections_limit);
77+
7478
void coap_connection_handler_exec(uint32_t time);
7579

7680
#endif

test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ TEST(coap_service_api, test_coap_service_if_find_by_socket)
9999
{
100100
CHECK(test_coap_service_if_find_by_socket())
101101
}
102+
103+
TEST(coap_service_api, test_coap_service_handshake_limit_set)
104+
{
105+
CHECK(test_coap_service_handshake_limit_set())
106+
}

test/coap-service/unittest/coap_service_api/test_coap_service_api.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,12 @@ bool test_coap_service_if_find_by_socket()
567567

568568
return true;
569569
}
570+
571+
bool test_coap_service_handshake_limit_set()
572+
{
573+
if (0 != coap_service_handshake_limits_set(2, 2)) {
574+
return false;
575+
}
576+
577+
return true;
578+
}

test/coap-service/unittest/coap_service_api/test_coap_service_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ bool test_coap_service_get_internal_timer_ticks();
5858

5959
bool test_coap_service_if_find_by_socket();
6060

61+
bool test_coap_service_handshake_limit_set();
62+
6163

6264
#ifdef __cplusplus
6365
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_
6161
return 0;
6262
}
6363

64+
int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, uint8_t connections_limit)
65+
{
66+
return 0;
67+
}
68+
6469
void coap_connection_handler_exec(uint32_t time)
6570
{
6671

0 commit comments

Comments
 (0)