Skip to content

[ONME-2927] Socket adaptation layer for nanostack #3517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

27 changes: 0 additions & 27 deletions features/nanostack/FEATURE_NANOSTACK/coap-service/module.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct internal_socket_s {
int16_t data_len;
uint8_t *data;

int8_t socket;
int8_t socket; //positive value = socket id, negative value virtual socket id
bool real_socket;
uint8_t usage_counter;
bool is_secure;
Expand All @@ -46,6 +46,11 @@ static NS_LIST_DEFINE(socket_list, internal_socket_t, link);

static void timer_cb(void* param);

static void recv_sckt_msg(void *cb_res);
#ifdef COAP_SECURITY_AVAILABLE
static void secure_recv_sckt_msg(void *cb_res);
#endif

#define TIMER_STATE_CANCELLED -1 /* cancelled */
#define TIMER_STATE_NO_EXPIRY 0 /* none of the delays is expired */
#define TIMER_STATE_INT_EXPIRY 1 /* the intermediate delay only is expired */
Expand Down Expand Up @@ -92,6 +97,17 @@ static secure_session_t *secure_session_find_by_timer_id(int8_t timer_id)
return this;
}

static bool is_secure_session_valid(secure_session_t *session)
{
secure_session_t *this = NULL;
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
if (cur_ptr == session) {
return true;
}
}
return false;
}

static void secure_session_delete(secure_session_t *this)
{
if (this) {
Expand All @@ -111,6 +127,17 @@ static void secure_session_delete(secure_session_t *this)
return;
}

static int8_t virtual_socket_id_allocate()
{
int8_t new_virtual_socket_id = -1; // must not overlap with real socket id's
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
if (cur_ptr->socket <= new_virtual_socket_id) {
new_virtual_socket_id = cur_ptr->socket - 1;
}
}
return new_virtual_socket_id;
}

static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port)
{
if(!address_ptr){
Expand Down Expand Up @@ -195,11 +222,6 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
return this;
}



static void recv_sckt_msg(void *cb_res);
static void secure_recv_sckt_msg(void *cb_res);

static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec)
{
internal_socket_t *this = ns_dyn_mem_alloc(sizeof(internal_socket_t));
Expand Down Expand Up @@ -245,8 +267,8 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
// Set socket option to receive packet info
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool));

}else{
this->socket = -1;
} else {
this->socket = virtual_socket_id_allocate();
}

ns_list_add_to_start(&socket_list, this);
Expand Down Expand Up @@ -300,16 +322,18 @@ static internal_socket_t *int_socket_find(uint16_t port, bool is_secure, bool is
return this;
}

static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address, const uint8_t source_address[static 16], const void *buffer, uint16_t length)
static int send_to_real_socket(int8_t socket_id, const ns_address_t *address, const uint8_t source_address[static 16], const void *buffer, uint16_t length)
{
ns_iovec_t msg_iov;
ns_msghdr_t msghdr;

msghdr.msg_name = (void*)address;
msghdr.msg_namelen = sizeof(ns_address_t);
msghdr.msg_iov = &msg_iov;
msghdr.msg_iovlen = 1;
msghdr.flags = 0;
ns_iovec_t msg_iov = {
.iov_base = (void *) buffer,
.iov_len = length
};
ns_msghdr_t msghdr = {
.msg_name = (void *) address,
.msg_namelen = sizeof(ns_address_t),
.msg_iov = &msg_iov,
.msg_iovlen = 1
};

if (memcmp(source_address, ns_in6addr_any, 16)) {
uint8_t ancillary_databuffer[NS_CMSG_SPACE(sizeof(ns_in6_pktinfo_t))];
Expand All @@ -328,14 +352,8 @@ static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address,
pktinfo = (ns_in6_pktinfo_t*)NS_CMSG_DATA(cmsg);
pktinfo->ipi6_ifindex = 0;
memcpy(pktinfo->ipi6_addr, source_address, 16);
} else {
msghdr.msg_control = NULL;
msghdr.msg_controllen = 0;
}

msg_iov.iov_base = (void *)buffer;
msg_iov.iov_len = length;

return socket_sendmsg(socket_id, &msghdr, 0);
}

Expand Down Expand Up @@ -364,7 +382,7 @@ static int secure_session_sendto(int8_t socket_id, void *handle, const void *buf
//For some reason socket_sendto returns 0 in success, while other socket impls return number of bytes sent!!!
//TODO: check if address_ptr is valid and use that instead if it is

int8_t ret = send_to_real_socket(sock->socket, &session->remote_host, session->local_address, buf, len);
int ret = send_to_real_socket(sock->socket, &session->remote_host, session->local_address, buf, len);
if (ret < 0) {
return ret;
}
Expand Down Expand Up @@ -395,7 +413,8 @@ static int secure_session_recvfrom(int8_t socket_id, unsigned char *buf, size_t
static void timer_cb(void *param)
{
secure_session_t *sec = param;
if( sec ){

if( sec && is_secure_session_valid(sec)){
if(sec->timer.fin_ms > sec->timer.int_ms){
/* Intermediate expiry */
sec->timer.fin_ms -= sec->timer.int_ms;
Expand Down Expand Up @@ -483,7 +502,6 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
msghdr.msg_iovlen = 1;
msghdr.msg_control = ancillary_databuffer;
msghdr.msg_controllen = sizeof(ancillary_databuffer);
msghdr.flags = 0;

msg_iov.iov_base = sock->data;
msg_iov.iov_len = sckt_data->d_len;
Expand Down Expand Up @@ -511,6 +529,8 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
} else {
goto return_failure;
}
} else {
goto return_failure;
}

return 0;
Expand All @@ -524,6 +544,7 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a

}

#ifdef COAP_SECURITY_AVAILABLE
static void secure_recv_sckt_msg(void *cb_res)
{
socket_callback_t *sckt_data = cb_res;
Expand Down Expand Up @@ -606,6 +627,7 @@ static void secure_recv_sckt_msg(void *cb_res)
}
}
}
#endif

static void recv_sckt_msg(void *cb_res)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ static int get_timer( void *sec_obj );
static int coap_security_handler_configure_keys( coap_security_t *sec, coap_security_keys_t keys );

int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen );

//Point these back to M2MConnectionHandler!!!
int f_send( void *ctx, const unsigned char *buf, size_t len );
int f_recv(void *ctx, unsigned char *buf, size_t len);

static int coap_security_handler_init(coap_security_t *sec){
const char *pers = "dtls_client";
#ifdef COAP_SERVICE_PROVIDE_STRONG_ENTROPY_SOURCE
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_STRONG;
#else
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_WEAK;
#endif

mbedtls_ssl_init( &sec->_ssl );
mbedtls_ssl_config_init( &sec->_conf );
mbedtls_ctr_drbg_init( &sec->_ctr_drbg );
Expand All @@ -97,10 +104,8 @@ static int coap_security_handler_init(coap_security_t *sec){

sec->_is_started = false;

//TODO: Must have at least 1 strong entropy source, otherwise DTLS will fail.
//This is NOT strong even we say it is!
if( mbedtls_entropy_add_source( &sec->_entropy, entropy_poll, NULL,
128, 1 ) < 0 ){
128, entropy_source_type ) < 0 ){
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
ns_address_t dest_addr;

if (!transaction_ptr || !data_ptr) {
return -1;
return 0;
}

tr_debug("Service %d, CoAP TX Function - mid: %d", transaction_ptr->service_id, common_read_16_bit(data_ptr + 2));

this = service_find(transaction_ptr->service_id);
if (!this) {
return -1;
return 0;
}

memcpy(&(dest_addr.address), address_ptr->addr_ptr, 16);
Expand Down Expand Up @@ -235,7 +235,7 @@ static int send_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t
{
coap_service_t *this = service_find_by_socket(socket_id);
if (this && this->virtual_socket_send_cb) {
tr_debug("send to virtual socket");
tr_debug("send to virtual socket, service: %d", this->service_id);
return this->virtual_socket_send_cb(this->service_id, (uint8_t*)address, port, data_ptr, data_len);
}
return -1;
Expand Down Expand Up @@ -380,7 +380,7 @@ int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_
int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr)
{
coap_service_t *this = service_find(service_id);
tr_debug("register virtual socket cb");
tr_debug("register virtual socket cb to service %d", service_id);
if (!this) {
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
#ifndef __COAP_SECURITY_HANDLER_H__
#define __COAP_SECURITY_HANDLER_H__

#include <stddef.h>
#include <inttypes.h>
#include <stdbool.h>
#include "ns_types.h"

#ifdef NS_USE_EXTERNAL_MBED_TLS
#include "mbedtls/ssl.h"
Expand Down Expand Up @@ -99,6 +97,8 @@ const void *coap_security_handler_keyblock(const coap_security_t *sec);

#else

NS_DUMMY_DEFINITIONS_OK

/* Dummy definitions, including needed error codes */
#define MBEDTLS_ERR_SSL_TIMEOUT (-1)
#define MBEDTLS_ERR_SSL_WANT_READ (-2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,22 @@ bool test_coap_callbacks()
addr.addr_len = 2;
addr.port = 4;
addr.addr_ptr = &data;
if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL))
if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL))
return false;

coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t));
memset(tr, 0, sizeof(coap_transaction_t));

if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
return false;

tr->service_id = 1;
thread_conn_handler_stub.int_value = -2;
if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
return false;

nsdynmemlib_stub.returnCounter = 1;
if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 2, &addr, tr))
if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 2, &addr, tr))
return false;

free(tr->data_ptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

eventOs_event_stub_def eventOs_event_stub;

int8_t eventOS_event_send(arm_event_s *event)
{
return eventOs_event_stub.int8_value;
}

int8_t eventOS_event_handler_create(void (*handler_func_ptr)(arm_event_s *), uint8_t init_event_type)
{
eventOs_event_stub.event_ptr = handler_func_ptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ int8_t socket_bind(int8_t socket, const ns_address_t *address)
return socket_api_stub.int8_value;
}

int8_t socket_send(int8_t socket, uint8_t *buffer, uint16_t length)
{
if( socket_api_stub.counter >= 0){
return socket_api_stub.values[socket_api_stub.counter--];
}

return socket_api_stub.int8_value;
}
int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length)
{
if( address ){
Expand All @@ -78,15 +70,7 @@ int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint1

return socket_api_stub.int8_value;
}
int8_t socket_sendto(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length)
{
if( socket_api_stub.counter >= 0){
return socket_api_stub.values[socket_api_stub.counter--];
}

return socket_api_stub.int8_value;
}
int8_t socket_read_session_address(int8_t socket, ns_address_t *address)
int8_t socket_getpeername(int8_t socket, ns_address_t *address)
{
if( socket_api_stub.counter >= 0){
return socket_api_stub.values[socket_api_stub.counter--];
Expand All @@ -110,8 +94,7 @@ int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *o

return socket_api_stub.int8_value;
}

int8_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags)
int16_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags)
{
if( socket_api_stub.counter >= 0){
return socket_api_stub.values[socket_api_stub.counter--];
Expand Down
Loading