Skip to content

Commit

Permalink
Add option to select used socket interface (ARMmbed#63)
Browse files Browse the repository at this point in the history
* Add option to select used socket interface

Add new COAP service option COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF.
When option is set then COAP interface ID will be selected as socket
interface.

* Review correction, reduce wordiness.

* Update unit tests.
  • Loading branch information
Arto Kinnunen committed Mar 3, 2017
1 parent 5a5c0f9 commit feea33e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
2 changes: 2 additions & 0 deletions coap-service/coap_service_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern "C" {
#define COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET 0x01
#define COAP_SERVICE_OPTIONS_SECURE 0x02
#define COAP_SERVICE_OPTIONS_EPHEMERAL_PORT 0x04
/** Coap interface selected as socket interface */
#define COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF 0x08
/** Link-layer security bypass option is set*/
#define COAP_SERVICE_OPTIONS_SECURE_BYPASS 0x80

Expand Down
11 changes: 7 additions & 4 deletions source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
return this;
}

static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec)
static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec, int8_t socket_interface_selection)
{
internal_socket_t *this = ns_dyn_mem_alloc(sizeof(internal_socket_t));
if (!this) {
Expand Down Expand Up @@ -266,7 +266,10 @@ 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));

if (socket_interface_selection != -1) {
// Select socket interface if selection requested
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_INTERFACE_SELECT, &socket_interface_selection, sizeof(int8_t));
}
} else {
this->socket = virtual_socket_id_allocate();
}
Expand Down Expand Up @@ -795,7 +798,7 @@ void connection_handler_close_secure_connection( coap_conn_handler_t *handler, u
}
}

int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool is_real_socket, bool bypassSec)
int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool is_real_socket, bool bypassSec, int8_t socket_interface_selection)
{
if (!handler) {
return -1;
Expand All @@ -810,7 +813,7 @@ int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16

internal_socket_t *current = !use_ephemeral_port?int_socket_find(listen_port, is_secure, is_real_socket, bypassSec):NULL;
if (!current) {
handler->socket = int_socket_create(listen_port, use_ephemeral_port, is_secure, is_real_socket, bypassSec);
handler->socket = int_socket_create(listen_port, use_ephemeral_port, is_secure, is_real_socket, bypassSec, socket_interface_selection);
if (!handler->socket) {
return -1;
}
Expand Down
18 changes: 12 additions & 6 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t
int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options,
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb)
{
(void) interface_id;

int8_t socket_interface_selection = -1;
coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t));
if (!this) {
return -1;
Expand All @@ -293,6 +292,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
while (service_find(id) && id < 127) {
id++;
}
this->interface_id = interface_id;
this->service_id = id;
this->service_options = service_options;

Expand All @@ -310,10 +310,16 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
return -1;
}

if (0 > coap_connection_handler_open_connection(this->conn_handler, listen_port, ((this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT) == COAP_SERVICE_OPTIONS_EPHEMERAL_PORT),
((this->service_options & COAP_SERVICE_OPTIONS_SECURE) == COAP_SERVICE_OPTIONS_SECURE),
((this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET) != COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET),
((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS))){
if (this->service_options & COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF) {
socket_interface_selection = this->interface_id;
}

if (0 > coap_connection_handler_open_connection(this->conn_handler, listen_port,
(this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT),
(this->service_options & COAP_SERVICE_OPTIONS_SECURE),
!(this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET),
(this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS),
socket_interface_selection)) {
ns_dyn_mem_free(this->conn_handler);
ns_dyn_mem_free(this);
return -1;
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 @@ -55,7 +55,7 @@ void connection_handler_destroy( coap_conn_handler_t *handler );

void connection_handler_close_secure_connection( coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port );

int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec);
int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec, int8_t socket_interface_selection);

//If returns -2, it means security was started and data was not send
int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_address_t *dest_addr, const uint8_t src_address[static 16], uint8_t *data_ptr, uint16_t data_len, bool bypass_link_sec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ bool test_coap_connection_handler_open_connection()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL);

if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false) )
if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false,-1) )
return false;

if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false) )
if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false,-1) )
return false;

ns_dyn_mem_free(handler);
nsdynmemlib_stub.returnCounter = 1;
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false,-1) )
return false;

nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false,-1) )
return false;

nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,true) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,true,-1) )
return false;

//open second one
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true) )
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true,-1) )
return false;

if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false) )
if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false,-1) )
return false;

connection_handler_destroy(handler2);
Expand All @@ -115,7 +115,7 @@ bool test_coap_connection_handler_send_data()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false,-1) )
return false;

if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
Expand All @@ -128,7 +128,7 @@ bool test_coap_connection_handler_send_data()
nsdynmemlib_stub.returnCounter = 1;
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 4;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false,-1) )
return false;

if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
Expand All @@ -146,7 +146,7 @@ bool test_coap_connection_handler_send_data()
nsdynmemlib_stub.returnCounter = 1;
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false,-1) )
return false;


Expand All @@ -157,7 +157,7 @@ bool test_coap_connection_handler_send_data()
nsdynmemlib_stub.returnCounter = 1;
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false,-1) )
return false;

socket_api_stub.int8_value = 7;
Expand All @@ -181,7 +181,7 @@ bool test_coap_connection_handler_virtual_recv()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
return false;

if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) )
Expand Down Expand Up @@ -214,7 +214,7 @@ bool test_coap_connection_handler_virtual_recv()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false,-1) )
return false;

nsdynmemlib_stub.returnCounter = 3;
Expand Down Expand Up @@ -243,7 +243,7 @@ bool test_coap_connection_handler_virtual_recv()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false) )
if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false,-1) )
return false;

nsdynmemlib_stub.returnCounter = 3;
Expand All @@ -265,7 +265,7 @@ bool test_coap_connection_handler_socket_belongs_to()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
return false;

if( true != coap_connection_handler_socket_belongs_to(handler, 0) )
Expand All @@ -288,7 +288,7 @@ bool test_timer_callbacks()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
return false;

//handler->socket->data still in memory
Expand Down Expand Up @@ -347,7 +347,7 @@ bool test_socket_api_callbacks()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false,-1) )
return false;

if( socket_api_stub.recv_cb ){
Expand All @@ -369,7 +369,7 @@ bool test_socket_api_callbacks()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,false,-1) )
return false;

if( socket_api_stub.recv_cb ){
Expand Down Expand Up @@ -425,7 +425,7 @@ bool test_security_callbacks()
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
return false;

if( socket_api_stub.recv_cb ){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void connection_handler_close_secure_connection( coap_conn_handler_t *handler, u

}

int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool is_real_socket, bool bypassSec)
int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool is_real_socket, bool bypassSec, int8_t socket_interface_selection)
{
return thread_conn_handler_stub.int_value;
}
Expand Down

0 comments on commit feea33e

Please sign in to comment.