Skip to content

Commit df0dc6c

Browse files
Squashed 'features/nanostack/FEATURE_NANOSTACK/coap-service/' changes from 8689fca..f6281ed
f6281ed CoAP transaction delete improvements (ARMmbed#91) a4bb497 Add check for interface when receiving CoAP request (ARMmbed#92) bca55ce Fix invalid memory read error (ARMmbed#90) git-subtree-dir: features/nanostack/FEATURE_NANOSTACK/coap-service git-subtree-split: f6281ed
1 parent 4734147 commit df0dc6c

14 files changed

+168
-55
lines changed

source/coap_connection_handler.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ static int timer_status(int8_t timer_id)
537537
return TIMER_STATE_CANCELLED;
538538
}
539539

540-
static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16])
540+
static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16], int8_t *interface)
541541
{
542542
sock->data_len = 0;
543543
if (sckt_data->event_type == SOCKET_DATA && sckt_data->d_len > 0) {
@@ -584,6 +584,7 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
584584
}
585585
if (pkt) {
586586
memcpy(dst_address, pkt->ipi6_addr, 16);
587+
*interface = pkt->ipi6_ifindex;
587588
} else {
588589
goto return_failure;
589590
}
@@ -613,8 +614,9 @@ static void secure_recv_sckt_msg(void *cb_res)
613614
ns_address_t src_address;
614615
uint8_t dst_address[16] = {0};
615616
memset(&src_address, 0, sizeof(ns_address_t));
617+
int8_t interface_id = -1;
616618

617-
if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) {
619+
if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) {
618620
/* If received from multicast address, reject */
619621
if (*(dst_address) == 0xFF) {
620622
return;
@@ -683,7 +685,7 @@ static void secure_recv_sckt_msg(void *cb_res)
683685
ns_dyn_mem_free(data);
684686
} else {
685687
if (sock->parent->_recv_cb) {
686-
sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, data, len);
688+
sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, data, len);
687689
}
688690
ns_dyn_mem_free(data);
689691
}
@@ -699,10 +701,11 @@ static void recv_sckt_msg(void *cb_res)
699701
internal_socket_t *sock = int_socket_find_by_socket_id(sckt_data->socket_id);
700702
ns_address_t src_address;
701703
uint8_t dst_address[16];
704+
int8_t interface_id = -1;
702705

703-
if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) {
706+
if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) {
704707
if (sock->parent && sock->parent->_recv_cb) {
705-
sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len);
708+
sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len);
706709
}
707710
ns_dyn_mem_free(sock->data);
708711
sock->data = NULL;
@@ -711,6 +714,8 @@ static void recv_sckt_msg(void *cb_res)
711714

712715
int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t address[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len)
713716
{
717+
int8_t interface_id = -1;
718+
714719
if(!handler || !handler->socket) {
715720
return -1;
716721
}
@@ -787,7 +792,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
787792
return 0;
788793
} else {
789794
if (sock->parent->_recv_cb) {
790-
sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, data, len);
795+
sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, data, len);
791796
}
792797
ns_dyn_mem_free(data);
793798
data = NULL;
@@ -798,7 +803,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
798803
} else {
799804
/* unsecure*/
800805
if (sock->parent->_recv_cb) {
801-
sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, sock->data, sock->data_len);
806+
sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, sock->data, sock->data_len);
802807
}
803808
if (sock->data) {
804809
ns_dyn_mem_free(sock->data);

source/coap_message_handler.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,28 @@ static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uin
9797
return this;
9898
}
9999

100+
/* retransmission valid time is calculated to be max. time that CoAP message sending can take: */
101+
/* Number of retransmisisons, each retransmission is 2 * previous retransmisison time */
102+
/* + random factor (max. 1.5) */
103+
static uint32_t transaction_valid_time_calculate(void)
104+
{
105+
int i;
106+
uint32_t time_valid = 0;
107+
108+
for (i = 0; i <= COAP_RESENDING_COUNT; i++) {
109+
time_valid += (COAP_RESENDING_INTERVAL << i) * 1.5;
110+
}
111+
112+
return time_valid + coap_service_get_internal_timer_ticks();
113+
}
114+
100115
static coap_transaction_t *transaction_create(void)
101116
{
102117
coap_transaction_t *this = ns_dyn_mem_alloc(sizeof(coap_transaction_t));
103118
if (this) {
104119
memset(this, 0, sizeof(coap_transaction_t));
105120
this->client_request = true;// default to client initiated method
106-
this->create_time = coap_service_get_internal_timer_ticks();
121+
this->valid_until = transaction_valid_time_calculate();
107122
ns_list_add_to_start(&request_list, this);
108123
}
109124

@@ -199,6 +214,9 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
199214
/* Set default buffer size for CoAP duplicate message detection */
200215
sn_coap_protocol_set_duplicate_buffer_size(handle->coap, DUPLICATE_MESSAGE_BUFFER_SIZE);
201216

217+
/* Set default CoAP retransmission paramters */
218+
sn_coap_protocol_set_retransmission_parameters(handle->coap, COAP_RESENDING_COUNT, COAP_RESENDING_INTERVAL);
219+
202220
return handle;
203221
}
204222

@@ -239,8 +257,8 @@ coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr,
239257
return transaction_find_by_address( address_ptr, port );
240258
}
241259

242-
int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16],
243-
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *))
260+
int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t interface_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16],
261+
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *))
244262
{
245263
sn_nsdl_addr_s src_addr;
246264
sn_coap_hdr_s *coap_message;
@@ -285,7 +303,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
285303
transaction_ptr->token_len = coap_message->token_len;
286304
}
287305
transaction_ptr->remote_port = port;
288-
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
306+
if (cb(socket_id, interface_id, coap_message, transaction_ptr) < 0) {
289307
// negative return value = message ignored -> delete transaction
290308
transaction_delete(transaction_ptr);
291309
}
@@ -381,7 +399,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
381399
//No response expected
382400
return 0;
383401
}
384-
return transaction_ptr->msg_id;
402+
return request.msg_id;
385403
}
386404

387405
static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handle, sn_coap_hdr_s *coap_msg_ptr, coap_transaction_t *transaction_ptr)
@@ -518,8 +536,13 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti
518536

519537
// Remove outdated transactions from queue
520538
ns_list_foreach_safe(coap_transaction_t, transaction, &request_list) {
521-
if ((transaction->create_time + TRANSACTION_LIFETIME) < current_time) {
522-
transaction_delete(transaction);
539+
if (transaction->valid_until < current_time) {
540+
tr_debug("transaction %d timed out", transaction->msg_id);
541+
ns_list_remove(&request_list, transaction);
542+
if (transaction->resp_cb) {
543+
transaction->resp_cb(transaction->service_id, transaction->remote_address, transaction->remote_port, NULL);
544+
}
545+
transaction_free(transaction);
523546
}
524547
}
525548

source/coap_service_api.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "coap_message_handler.h"
3737
#include "mbed-coap/sn_coap_protocol.h"
3838

39-
static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr);
39+
static int16_t coap_msg_process_callback(int8_t socket_id, int8_t interface_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr);
4040

4141
typedef struct uri_registration {
4242
char *uri_ptr;
@@ -210,7 +210,7 @@ static void service_event_handler(arm_event_s *event)
210210
eventOS_event_timer_request((uint8_t)COAP_TICK_TIMER, ARM_LIB_SYSTEM_TIMER_EVENT, tasklet_id, 1000);
211211
}
212212

213-
static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr)
213+
static int16_t coap_msg_process_callback(int8_t socket_id, int8_t interface_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr)
214214
{
215215
coap_service_t *this;
216216
if (!coap_message || !transaction_ptr) {
@@ -229,6 +229,11 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
229229
return -1;
230230
}
231231

232+
if ((interface_id != -1) && (this->interface_id != interface_id)) {
233+
tr_debug("uri %.*s not registered to interface %d", coap_message->uri_path_len, coap_message->uri_path_ptr, interface_id);
234+
return 0;
235+
}
236+
232237
uri_registration_t *uri_reg_ptr = uri_registration_find(this, coap_message->uri_path_ptr, coap_message->uri_path_len);
233238
if (uri_reg_ptr && uri_reg_ptr->request_recv_cb) {
234239
tr_debug("Service %d, call request recv cb uri %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr);
@@ -244,7 +249,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
244249
return -1;
245250
}
246251

247-
static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len)
252+
static int recv_cb(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len)
248253
{
249254
uint8_t *data_ptr = NULL;
250255
uint16_t data_len = 0;
@@ -263,7 +268,7 @@ static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t po
263268
tr_debug("service recv socket data len %d ", data_len);
264269

265270
//parse coap message what CoAP to use
266-
int ret = coap_message_handler_coap_msg_process(coap_service_handle, socket_id, src_address, port, dst_address, data_ptr, data_len, &coap_msg_process_callback);
271+
int ret = coap_message_handler_coap_msg_process(coap_service_handle, socket_id, interface_id, src_address, port, dst_address, data_ptr, data_len, &coap_msg_process_callback);
267272
own_free(data_ptr);
268273
return ret;
269274
}

source/include/coap_connection_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
struct internal_socket_s;
3636

3737
typedef int send_to_socket_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t port, const void *, int);
38-
typedef int receive_from_socket_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int);
38+
typedef int receive_from_socket_cb(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int);
3939
typedef int get_pw_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr);
4040
typedef void security_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]);
4141

source/include/coap_message_handler.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
/* Default value for CoAP duplicate message buffer (0 = disabled) */
2727
#define DUPLICATE_MESSAGE_BUFFER_SIZE 0
2828

29+
/* Default values for CoAP resendings */
30+
#define COAP_RESENDING_COUNT 3
31+
#define COAP_RESENDING_INTERVAL 10
32+
2933
/**
3034
* \brief Service message response receive callback.
3135
*
@@ -51,7 +55,7 @@ typedef struct coap_transaction {
5155
uint8_t remote_address[16];
5256
uint8_t local_address[16];
5357
uint8_t token[8];
54-
uint32_t create_time;
58+
uint32_t valid_until;
5559
uint8_t *data_ptr;
5660
coap_message_handler_response_recv *resp_cb;
5761
uint16_t remote_port;
@@ -76,8 +80,8 @@ extern coap_transaction_t *coap_message_handler_transaction_valid(coap_transacti
7680

7781
extern coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, uint16_t port);
7882

79-
extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16],
80-
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *));
83+
extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t interface_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16],
84+
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *));
8185

8286
extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16],
8387
uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type,

test/coap-service/unittest/coap_message_handler/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ TEST_SRC_FILES = \
3737

3838
include ../MakefileWorker.mk
3939

40-
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
40+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT -I ../../../../source/
4141

0 commit comments

Comments
 (0)