Skip to content

Commit

Permalink
Squashed 'features/nanostack/FEATURE_NANOSTACK/coap-service/' changes…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
deepakvenugopal committed Feb 9, 2018
1 parent 4734147 commit df0dc6c
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 55 deletions.
19 changes: 12 additions & 7 deletions source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ static int timer_status(int8_t timer_id)
return TIMER_STATE_CANCELLED;
}

static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16])
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)
{
sock->data_len = 0;
if (sckt_data->event_type == SOCKET_DATA && sckt_data->d_len > 0) {
Expand Down Expand Up @@ -584,6 +584,7 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
}
if (pkt) {
memcpy(dst_address, pkt->ipi6_addr, 16);
*interface = pkt->ipi6_ifindex;
} else {
goto return_failure;
}
Expand Down Expand Up @@ -613,8 +614,9 @@ static void secure_recv_sckt_msg(void *cb_res)
ns_address_t src_address;
uint8_t dst_address[16] = {0};
memset(&src_address, 0, sizeof(ns_address_t));
int8_t interface_id = -1;

if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) {
if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) {
/* If received from multicast address, reject */
if (*(dst_address) == 0xFF) {
return;
Expand Down Expand Up @@ -683,7 +685,7 @@ static void secure_recv_sckt_msg(void *cb_res)
ns_dyn_mem_free(data);
} else {
if (sock->parent->_recv_cb) {
sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, data, len);
sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, data, len);
}
ns_dyn_mem_free(data);
}
Expand All @@ -699,10 +701,11 @@ static void recv_sckt_msg(void *cb_res)
internal_socket_t *sock = int_socket_find_by_socket_id(sckt_data->socket_id);
ns_address_t src_address;
uint8_t dst_address[16];
int8_t interface_id = -1;

if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) {
if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) {
if (sock->parent && sock->parent->_recv_cb) {
sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len);
sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len);
}
ns_dyn_mem_free(sock->data);
sock->data = NULL;
Expand All @@ -711,6 +714,8 @@ static void recv_sckt_msg(void *cb_res)

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)
{
int8_t interface_id = -1;

if(!handler || !handler->socket) {
return -1;
}
Expand Down Expand Up @@ -787,7 +792,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
return 0;
} else {
if (sock->parent->_recv_cb) {
sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, data, len);
sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, data, len);
}
ns_dyn_mem_free(data);
data = NULL;
Expand All @@ -798,7 +803,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
} else {
/* unsecure*/
if (sock->parent->_recv_cb) {
sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, sock->data, sock->data_len);
sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, sock->data, sock->data_len);
}
if (sock->data) {
ns_dyn_mem_free(sock->data);
Expand Down
37 changes: 30 additions & 7 deletions source/coap_message_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,28 @@ static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uin
return this;
}

/* retransmission valid time is calculated to be max. time that CoAP message sending can take: */
/* Number of retransmisisons, each retransmission is 2 * previous retransmisison time */
/* + random factor (max. 1.5) */
static uint32_t transaction_valid_time_calculate(void)
{
int i;
uint32_t time_valid = 0;

for (i = 0; i <= COAP_RESENDING_COUNT; i++) {
time_valid += (COAP_RESENDING_INTERVAL << i) * 1.5;
}

return time_valid + coap_service_get_internal_timer_ticks();
}

static coap_transaction_t *transaction_create(void)
{
coap_transaction_t *this = ns_dyn_mem_alloc(sizeof(coap_transaction_t));
if (this) {
memset(this, 0, sizeof(coap_transaction_t));
this->client_request = true;// default to client initiated method
this->create_time = coap_service_get_internal_timer_ticks();
this->valid_until = transaction_valid_time_calculate();
ns_list_add_to_start(&request_list, this);
}

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

/* Set default CoAP retransmission paramters */
sn_coap_protocol_set_retransmission_parameters(handle->coap, COAP_RESENDING_COUNT, COAP_RESENDING_INTERVAL);

return handle;
}

Expand Down Expand Up @@ -239,8 +257,8 @@ coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr,
return transaction_find_by_address( address_ptr, port );
}

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],
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *))
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],
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *))
{
sn_nsdl_addr_s src_addr;
sn_coap_hdr_s *coap_message;
Expand Down Expand Up @@ -285,7 +303,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
transaction_ptr->token_len = coap_message->token_len;
}
transaction_ptr->remote_port = port;
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
if (cb(socket_id, interface_id, coap_message, transaction_ptr) < 0) {
// negative return value = message ignored -> delete transaction
transaction_delete(transaction_ptr);
}
Expand Down Expand Up @@ -381,7 +399,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
//No response expected
return 0;
}
return transaction_ptr->msg_id;
return request.msg_id;
}

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)
Expand Down Expand Up @@ -518,8 +536,13 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti

// Remove outdated transactions from queue
ns_list_foreach_safe(coap_transaction_t, transaction, &request_list) {
if ((transaction->create_time + TRANSACTION_LIFETIME) < current_time) {
transaction_delete(transaction);
if (transaction->valid_until < current_time) {
tr_debug("transaction %d timed out", transaction->msg_id);
ns_list_remove(&request_list, transaction);
if (transaction->resp_cb) {
transaction->resp_cb(transaction->service_id, transaction->remote_address, transaction->remote_port, NULL);
}
transaction_free(transaction);
}
}

Expand Down
13 changes: 9 additions & 4 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "coap_message_handler.h"
#include "mbed-coap/sn_coap_protocol.h"

static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr);
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);

typedef struct uri_registration {
char *uri_ptr;
Expand Down Expand Up @@ -210,7 +210,7 @@ static void service_event_handler(arm_event_s *event)
eventOS_event_timer_request((uint8_t)COAP_TICK_TIMER, ARM_LIB_SYSTEM_TIMER_EVENT, tasklet_id, 1000);
}

static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr)
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)
{
coap_service_t *this;
if (!coap_message || !transaction_ptr) {
Expand All @@ -229,6 +229,11 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
return -1;
}

if ((interface_id != -1) && (this->interface_id != interface_id)) {
tr_debug("uri %.*s not registered to interface %d", coap_message->uri_path_len, coap_message->uri_path_ptr, interface_id);
return 0;
}

uri_registration_t *uri_reg_ptr = uri_registration_find(this, coap_message->uri_path_ptr, coap_message->uri_path_len);
if (uri_reg_ptr && uri_reg_ptr->request_recv_cb) {
tr_debug("Service %d, call request recv cb uri %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr);
Expand All @@ -244,7 +249,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
return -1;
}

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)
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)
{
uint8_t *data_ptr = NULL;
uint16_t data_len = 0;
Expand All @@ -263,7 +268,7 @@ static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t po
tr_debug("service recv socket data len %d ", data_len);

//parse coap message what CoAP to use
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);
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);
own_free(data_ptr);
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion source/include/coap_connection_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
struct internal_socket_s;

typedef int send_to_socket_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t port, const void *, int);
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);
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);
typedef int get_pw_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr);
typedef void security_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]);

Expand Down
10 changes: 7 additions & 3 deletions source/include/coap_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
/* Default value for CoAP duplicate message buffer (0 = disabled) */
#define DUPLICATE_MESSAGE_BUFFER_SIZE 0

/* Default values for CoAP resendings */
#define COAP_RESENDING_COUNT 3
#define COAP_RESENDING_INTERVAL 10

/**
* \brief Service message response receive callback.
*
Expand All @@ -51,7 +55,7 @@ typedef struct coap_transaction {
uint8_t remote_address[16];
uint8_t local_address[16];
uint8_t token[8];
uint32_t create_time;
uint32_t valid_until;
uint8_t *data_ptr;
coap_message_handler_response_recv *resp_cb;
uint16_t remote_port;
Expand All @@ -76,8 +80,8 @@ extern coap_transaction_t *coap_message_handler_transaction_valid(coap_transacti

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

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],
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *));
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],
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *));

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],
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,
Expand Down
2 changes: 1 addition & 1 deletion test/coap-service/unittest/coap_message_handler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ TEST_SRC_FILES = \

include ../MakefileWorker.mk

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

Loading

0 comments on commit df0dc6c

Please sign in to comment.