Skip to content

Commit feea33e

Browse files
author
Arto Kinnunen
authored
Add option to select used socket interface (ARMmbed#63)
* 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.
1 parent 5a5c0f9 commit feea33e

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

coap-service/coap_service_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern "C" {
4545
#define COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET 0x01
4646
#define COAP_SERVICE_OPTIONS_SECURE 0x02
4747
#define COAP_SERVICE_OPTIONS_EPHEMERAL_PORT 0x04
48+
/** Coap interface selected as socket interface */
49+
#define COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF 0x08
4850
/** Link-layer security bypass option is set*/
4951
#define COAP_SERVICE_OPTIONS_SECURE_BYPASS 0x80
5052

source/coap_connection_handler.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
222222
return this;
223223
}
224224

225-
static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec)
225+
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)
226226
{
227227
internal_socket_t *this = ns_dyn_mem_alloc(sizeof(internal_socket_t));
228228
if (!this) {
@@ -266,7 +266,10 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
266266

267267
// Set socket option to receive packet info
268268
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool));
269-
269+
if (socket_interface_selection != -1) {
270+
// Select socket interface if selection requested
271+
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_INTERFACE_SELECT, &socket_interface_selection, sizeof(int8_t));
272+
}
270273
} else {
271274
this->socket = virtual_socket_id_allocate();
272275
}
@@ -795,7 +798,7 @@ void connection_handler_close_secure_connection( coap_conn_handler_t *handler, u
795798
}
796799
}
797800

798-
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)
801+
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)
799802
{
800803
if (!handler) {
801804
return -1;
@@ -810,7 +813,7 @@ int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16
810813

811814
internal_socket_t *current = !use_ephemeral_port?int_socket_find(listen_port, is_secure, is_real_socket, bypassSec):NULL;
812815
if (!current) {
813-
handler->socket = int_socket_create(listen_port, use_ephemeral_port, is_secure, is_real_socket, bypassSec);
816+
handler->socket = int_socket_create(listen_port, use_ephemeral_port, is_secure, is_real_socket, bypassSec, socket_interface_selection);
814817
if (!handler->socket) {
815818
return -1;
816819
}

source/coap_service_api.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t
280280
int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options,
281281
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb)
282282
{
283-
(void) interface_id;
284-
283+
int8_t socket_interface_selection = -1;
285284
coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t));
286285
if (!this) {
287286
return -1;
@@ -293,6 +292,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
293292
while (service_find(id) && id < 127) {
294293
id++;
295294
}
295+
this->interface_id = interface_id;
296296
this->service_id = id;
297297
this->service_options = service_options;
298298

@@ -310,10 +310,16 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
310310
return -1;
311311
}
312312

313-
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),
314-
((this->service_options & COAP_SERVICE_OPTIONS_SECURE) == COAP_SERVICE_OPTIONS_SECURE),
315-
((this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET) != COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET),
316-
((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS))){
313+
if (this->service_options & COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF) {
314+
socket_interface_selection = this->interface_id;
315+
}
316+
317+
if (0 > coap_connection_handler_open_connection(this->conn_handler, listen_port,
318+
(this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT),
319+
(this->service_options & COAP_SERVICE_OPTIONS_SECURE),
320+
!(this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET),
321+
(this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS),
322+
socket_interface_selection)) {
317323
ns_dyn_mem_free(this->conn_handler);
318324
ns_dyn_mem_free(this);
319325
return -1;

source/include/coap_connection_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void connection_handler_destroy( coap_conn_handler_t *handler );
5555

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

58-
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);
58+
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);
5959

6060
//If returns -2, it means security was started and data was not send
6161
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);

test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,34 @@ bool test_coap_connection_handler_open_connection()
6767
nsdynmemlib_stub.returnCounter = 1;
6868
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL);
6969

70-
if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false) )
70+
if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false,-1) )
7171
return false;
7272

73-
if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false) )
73+
if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false,-1) )
7474
return false;
7575

7676
ns_dyn_mem_free(handler);
7777
nsdynmemlib_stub.returnCounter = 1;
7878
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
79-
if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
79+
if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false,-1) )
8080
return false;
8181

8282
nsdynmemlib_stub.returnCounter = 2;
83-
if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
83+
if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false,-1) )
8484
return false;
8585

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

9090
//open second one
9191
nsdynmemlib_stub.returnCounter = 1;
9292
coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
9393
nsdynmemlib_stub.returnCounter = 2;
94-
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true) )
94+
if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true,-1) )
9595
return false;
9696

97-
if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false) )
97+
if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false,-1) )
9898
return false;
9999

100100
connection_handler_destroy(handler2);
@@ -115,7 +115,7 @@ bool test_coap_connection_handler_send_data()
115115
nsdynmemlib_stub.returnCounter = 1;
116116
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
117117
nsdynmemlib_stub.returnCounter = 2;
118-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
118+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false,-1) )
119119
return false;
120120

121121
if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
@@ -128,7 +128,7 @@ bool test_coap_connection_handler_send_data()
128128
nsdynmemlib_stub.returnCounter = 1;
129129
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
130130
nsdynmemlib_stub.returnCounter = 4;
131-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
131+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false,-1) )
132132
return false;
133133

134134
if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
@@ -146,7 +146,7 @@ bool test_coap_connection_handler_send_data()
146146
nsdynmemlib_stub.returnCounter = 1;
147147
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
148148
nsdynmemlib_stub.returnCounter = 2;
149-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false) )
149+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false,-1) )
150150
return false;
151151

152152

@@ -157,7 +157,7 @@ bool test_coap_connection_handler_send_data()
157157
nsdynmemlib_stub.returnCounter = 1;
158158
handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
159159
nsdynmemlib_stub.returnCounter = 2;
160-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) )
160+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false,-1) )
161161
return false;
162162

163163
socket_api_stub.int8_value = 7;
@@ -181,7 +181,7 @@ bool test_coap_connection_handler_virtual_recv()
181181
nsdynmemlib_stub.returnCounter = 1;
182182
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
183183
nsdynmemlib_stub.returnCounter = 2;
184-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
184+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
185185
return false;
186186

187187
if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) )
@@ -214,7 +214,7 @@ bool test_coap_connection_handler_virtual_recv()
214214
nsdynmemlib_stub.returnCounter = 1;
215215
coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb);
216216
nsdynmemlib_stub.returnCounter = 2;
217-
if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false) )
217+
if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false,-1) )
218218
return false;
219219

220220
nsdynmemlib_stub.returnCounter = 3;
@@ -243,7 +243,7 @@ bool test_coap_connection_handler_virtual_recv()
243243
nsdynmemlib_stub.returnCounter = 1;
244244
coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb);
245245
nsdynmemlib_stub.returnCounter = 2;
246-
if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false) )
246+
if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false,-1) )
247247
return false;
248248

249249
nsdynmemlib_stub.returnCounter = 3;
@@ -265,7 +265,7 @@ bool test_coap_connection_handler_socket_belongs_to()
265265
nsdynmemlib_stub.returnCounter = 1;
266266
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
267267
nsdynmemlib_stub.returnCounter = 2;
268-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
268+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
269269
return false;
270270

271271
if( true != coap_connection_handler_socket_belongs_to(handler, 0) )
@@ -288,7 +288,7 @@ bool test_timer_callbacks()
288288
nsdynmemlib_stub.returnCounter = 1;
289289
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
290290
nsdynmemlib_stub.returnCounter = 2;
291-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
291+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
292292
return false;
293293

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

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

375375
if( socket_api_stub.recv_cb ){
@@ -425,7 +425,7 @@ bool test_security_callbacks()
425425
nsdynmemlib_stub.returnCounter = 1;
426426
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
427427
nsdynmemlib_stub.returnCounter = 2;
428-
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
428+
if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false,-1) )
429429
return false;
430430

431431
if( socket_api_stub.recv_cb ){

test/coap-service/unittest/stub/coap_connection_handler_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void connection_handler_close_secure_connection( coap_conn_handler_t *handler, u
4141

4242
}
4343

44-
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)
44+
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)
4545
{
4646
return thread_conn_handler_stub.int_value;
4747
}

0 commit comments

Comments
 (0)