Skip to content

Commit 769712f

Browse files
authored
Merge pull request #3517 from hasnainvirk/ONME_2927
[ONME-2927] Socket adaptation layer for nanostack
2 parents 21b91c7 + 1ac5700 commit 769712f

File tree

106 files changed

+1216
-570
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1216
-570
lines changed

features/nanostack/FEATURE_NANOSTACK/coap-service/.yotta_ignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

features/nanostack/FEATURE_NANOSTACK/coap-service/module.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

features/nanostack/FEATURE_NANOSTACK/coap-service/source/coap_connection_handler.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct internal_socket_s {
3232
int16_t data_len;
3333
uint8_t *data;
3434

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

4747
static void timer_cb(void* param);
4848

49+
static void recv_sckt_msg(void *cb_res);
50+
#ifdef COAP_SECURITY_AVAILABLE
51+
static void secure_recv_sckt_msg(void *cb_res);
52+
#endif
53+
4954
#define TIMER_STATE_CANCELLED -1 /* cancelled */
5055
#define TIMER_STATE_NO_EXPIRY 0 /* none of the delays is expired */
5156
#define TIMER_STATE_INT_EXPIRY 1 /* the intermediate delay only is expired */
@@ -92,6 +97,17 @@ static secure_session_t *secure_session_find_by_timer_id(int8_t timer_id)
9297
return this;
9398
}
9499

100+
static bool is_secure_session_valid(secure_session_t *session)
101+
{
102+
secure_session_t *this = NULL;
103+
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
104+
if (cur_ptr == session) {
105+
return true;
106+
}
107+
}
108+
return false;
109+
}
110+
95111
static void secure_session_delete(secure_session_t *this)
96112
{
97113
if (this) {
@@ -111,6 +127,17 @@ static void secure_session_delete(secure_session_t *this)
111127
return;
112128
}
113129

130+
static int8_t virtual_socket_id_allocate()
131+
{
132+
int8_t new_virtual_socket_id = -1; // must not overlap with real socket id's
133+
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
134+
if (cur_ptr->socket <= new_virtual_socket_id) {
135+
new_virtual_socket_id = cur_ptr->socket - 1;
136+
}
137+
}
138+
return new_virtual_socket_id;
139+
}
140+
114141
static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port)
115142
{
116143
if(!address_ptr){
@@ -195,11 +222,6 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
195222
return this;
196223
}
197224

198-
199-
200-
static void recv_sckt_msg(void *cb_res);
201-
static void secure_recv_sckt_msg(void *cb_res);
202-
203225
static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec)
204226
{
205227
internal_socket_t *this = ns_dyn_mem_alloc(sizeof(internal_socket_t));
@@ -245,8 +267,8 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
245267
// Set socket option to receive packet info
246268
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool));
247269

248-
}else{
249-
this->socket = -1;
270+
} else {
271+
this->socket = virtual_socket_id_allocate();
250272
}
251273

252274
ns_list_add_to_start(&socket_list, this);
@@ -300,16 +322,18 @@ static internal_socket_t *int_socket_find(uint16_t port, bool is_secure, bool is
300322
return this;
301323
}
302324

303-
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)
325+
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)
304326
{
305-
ns_iovec_t msg_iov;
306-
ns_msghdr_t msghdr;
307-
308-
msghdr.msg_name = (void*)address;
309-
msghdr.msg_namelen = sizeof(ns_address_t);
310-
msghdr.msg_iov = &msg_iov;
311-
msghdr.msg_iovlen = 1;
312-
msghdr.flags = 0;
327+
ns_iovec_t msg_iov = {
328+
.iov_base = (void *) buffer,
329+
.iov_len = length
330+
};
331+
ns_msghdr_t msghdr = {
332+
.msg_name = (void *) address,
333+
.msg_namelen = sizeof(ns_address_t),
334+
.msg_iov = &msg_iov,
335+
.msg_iovlen = 1
336+
};
313337

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

336-
msg_iov.iov_base = (void *)buffer;
337-
msg_iov.iov_len = length;
338-
339357
return socket_sendmsg(socket_id, &msghdr, 0);
340358
}
341359

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

367-
int8_t ret = send_to_real_socket(sock->socket, &session->remote_host, session->local_address, buf, len);
385+
int ret = send_to_real_socket(sock->socket, &session->remote_host, session->local_address, buf, len);
368386
if (ret < 0) {
369387
return ret;
370388
}
@@ -395,7 +413,8 @@ static int secure_session_recvfrom(int8_t socket_id, unsigned char *buf, size_t
395413
static void timer_cb(void *param)
396414
{
397415
secure_session_t *sec = param;
398-
if( sec ){
416+
417+
if( sec && is_secure_session_valid(sec)){
399418
if(sec->timer.fin_ms > sec->timer.int_ms){
400419
/* Intermediate expiry */
401420
sec->timer.fin_ms -= sec->timer.int_ms;
@@ -483,7 +502,6 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
483502
msghdr.msg_iovlen = 1;
484503
msghdr.msg_control = ancillary_databuffer;
485504
msghdr.msg_controllen = sizeof(ancillary_databuffer);
486-
msghdr.flags = 0;
487505

488506
msg_iov.iov_base = sock->data;
489507
msg_iov.iov_len = sckt_data->d_len;
@@ -511,6 +529,8 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a
511529
} else {
512530
goto return_failure;
513531
}
532+
} else {
533+
goto return_failure;
514534
}
515535

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

525545
}
526546

547+
#ifdef COAP_SECURITY_AVAILABLE
527548
static void secure_recv_sckt_msg(void *cb_res)
528549
{
529550
socket_callback_t *sckt_data = cb_res;
@@ -606,6 +627,7 @@ static void secure_recv_sckt_msg(void *cb_res)
606627
}
607628
}
608629
}
630+
#endif
609631

610632
static void recv_sckt_msg(void *cb_res)
611633
{

features/nanostack/FEATURE_NANOSTACK/coap-service/source/coap_security_handler.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,19 @@ static int get_timer( void *sec_obj );
7575
static int coap_security_handler_configure_keys( coap_security_t *sec, coap_security_keys_t keys );
7676

7777
int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen );
78+
7879
//Point these back to M2MConnectionHandler!!!
7980
int f_send( void *ctx, const unsigned char *buf, size_t len );
8081
int f_recv(void *ctx, unsigned char *buf, size_t len);
8182

8283
static int coap_security_handler_init(coap_security_t *sec){
8384
const char *pers = "dtls_client";
85+
#ifdef COAP_SERVICE_PROVIDE_STRONG_ENTROPY_SOURCE
86+
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_STRONG;
87+
#else
88+
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_WEAK;
89+
#endif
90+
8491
mbedtls_ssl_init( &sec->_ssl );
8592
mbedtls_ssl_config_init( &sec->_conf );
8693
mbedtls_ctr_drbg_init( &sec->_ctr_drbg );
@@ -97,10 +104,8 @@ static int coap_security_handler_init(coap_security_t *sec){
97104

98105
sec->_is_started = false;
99106

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

features/nanostack/FEATURE_NANOSTACK/coap-service/source/coap_service_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
127127
ns_address_t dest_addr;
128128

129129
if (!transaction_ptr || !data_ptr) {
130-
return -1;
130+
return 0;
131131
}
132132

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

135135
this = service_find(transaction_ptr->service_id);
136136
if (!this) {
137-
return -1;
137+
return 0;
138138
}
139139

140140
memcpy(&(dest_addr.address), address_ptr->addr_ptr, 16);
@@ -235,7 +235,7 @@ static int send_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t
235235
{
236236
coap_service_t *this = service_find_by_socket(socket_id);
237237
if (this && this->virtual_socket_send_cb) {
238-
tr_debug("send to virtual socket");
238+
tr_debug("send to virtual socket, service: %d", this->service_id);
239239
return this->virtual_socket_send_cb(this->service_id, (uint8_t*)address, port, data_ptr, data_len);
240240
}
241241
return -1;
@@ -380,7 +380,7 @@ int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_
380380
int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr)
381381
{
382382
coap_service_t *this = service_find(service_id);
383-
tr_debug("register virtual socket cb");
383+
tr_debug("register virtual socket cb to service %d", service_id);
384384
if (!this) {
385385
return -1;
386386
}

features/nanostack/FEATURE_NANOSTACK/coap-service/source/include/coap_security_handler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
#ifndef __COAP_SECURITY_HANDLER_H__
1919
#define __COAP_SECURITY_HANDLER_H__
2020

21-
#include <stddef.h>
22-
#include <inttypes.h>
23-
#include <stdbool.h>
21+
#include "ns_types.h"
2422

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

10098
#else
10199

100+
NS_DUMMY_DEFINITIONS_OK
101+
102102
/* Dummy definitions, including needed error codes */
103103
#define MBEDTLS_ERR_SSL_TIMEOUT (-1)
104104
#define MBEDTLS_ERR_SSL_WANT_READ (-2)

features/nanostack/FEATURE_NANOSTACK/coap-service/test/coap-service/unittest/coap_service_api/test_coap_service_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,22 @@ bool test_coap_callbacks()
252252
addr.addr_len = 2;
253253
addr.port = 4;
254254
addr.addr_ptr = &data;
255-
if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL))
255+
if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL))
256256
return false;
257257

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

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

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

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

273273
free(tr->data_ptr);

features/nanostack/FEATURE_NANOSTACK/coap-service/test/coap-service/unittest/stub/eventOS_event_stub.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
eventOs_event_stub_def eventOs_event_stub;
1111

12-
int8_t eventOS_event_send(arm_event_s *event)
13-
{
14-
return eventOs_event_stub.int8_value;
15-
}
16-
1712
int8_t eventOS_event_handler_create(void (*handler_func_ptr)(arm_event_s *), uint8_t init_event_type)
1813
{
1914
eventOs_event_stub.event_ptr = handler_func_ptr;

features/nanostack/FEATURE_NANOSTACK/coap-service/test/coap-service/unittest/stub/socket_api_stub.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ int8_t socket_bind(int8_t socket, const ns_address_t *address)
5858
return socket_api_stub.int8_value;
5959
}
6060

61-
int8_t socket_send(int8_t socket, uint8_t *buffer, uint16_t length)
62-
{
63-
if( socket_api_stub.counter >= 0){
64-
return socket_api_stub.values[socket_api_stub.counter--];
65-
}
66-
67-
return socket_api_stub.int8_value;
68-
}
6961
int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length)
7062
{
7163
if( address ){
@@ -78,15 +70,7 @@ int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint1
7870

7971
return socket_api_stub.int8_value;
8072
}
81-
int8_t socket_sendto(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length)
82-
{
83-
if( socket_api_stub.counter >= 0){
84-
return socket_api_stub.values[socket_api_stub.counter--];
85-
}
86-
87-
return socket_api_stub.int8_value;
88-
}
89-
int8_t socket_read_session_address(int8_t socket, ns_address_t *address)
73+
int8_t socket_getpeername(int8_t socket, ns_address_t *address)
9074
{
9175
if( socket_api_stub.counter >= 0){
9276
return socket_api_stub.values[socket_api_stub.counter--];
@@ -110,8 +94,7 @@ int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *o
11094

11195
return socket_api_stub.int8_value;
11296
}
113-
114-
int8_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags)
97+
int16_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags)
11598
{
11699
if( socket_api_stub.counter >= 0){
117100
return socket_api_stub.values[socket_api_stub.counter--];

0 commit comments

Comments
 (0)