@@ -64,6 +64,9 @@ typedef struct secure_session {
64
64
coap_security_t * sec_handler ; //owned
65
65
internal_socket_t * parent ; //not owned
66
66
67
+ uint8_t remote_address [16 ];
68
+ uint16_t remote_port ;
69
+
67
70
secure_timer_t timer ;
68
71
69
72
session_state_t session_state ;
@@ -72,7 +75,7 @@ typedef struct secure_session {
72
75
} secure_session_t ;
73
76
74
77
static NS_LIST_DEFINE (secure_session_list , secure_session_t , link ) ;
75
- static int send_to_socket (int8_t socket_id , const uint8_t * address_ptr , uint16_t port , const uint8_t source_addr [ static 16 ] , const void * buf , size_t len );
78
+ static int send_to_socket (int8_t socket_id , void * handle , const void * buf , size_t len );
76
79
static int receive_from_socket (int8_t socket_id , unsigned char * buf , size_t len );
77
80
static void start_timer (int8_t timer_id , uint32_t int_ms , uint32_t fin_ms );
78
81
static int timer_status (int8_t timer_id );
@@ -146,8 +149,10 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
146
149
timer_id ++ ;
147
150
}
148
151
this -> timer .id = timer_id ;
152
+ memcpy (this -> remote_address , address_ptr , 16 );
153
+ this -> remote_port = port ;
149
154
150
- this -> sec_handler = coap_security_create (parent -> listen_socket , this -> timer .id , address_ptr , port , ECJPAKE ,
155
+ this -> sec_handler = coap_security_create (parent -> listen_socket , this -> timer .id , this , ECJPAKE ,
151
156
& send_to_socket , & receive_from_socket , & start_timer , & timer_status );
152
157
if ( !this -> sec_handler ){
153
158
ns_dyn_mem_free (this );
@@ -178,8 +183,8 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
178
183
secure_session_t * this = NULL ;
179
184
ns_list_foreach (secure_session_t , cur_ptr , & secure_session_list ) {
180
185
if ( cur_ptr -> sec_handler ){
181
- if (cur_ptr -> parent == parent && cur_ptr -> sec_handler -> _remote_port == port &&
182
- memcmp (cur_ptr -> sec_handler -> _remote_address , address_ptr , 16 ) == 0 ) {
186
+ if (cur_ptr -> parent == parent && cur_ptr -> remote_port == port &&
187
+ memcmp (cur_ptr -> remote_address , address_ptr , 16 ) == 0 ) {
183
188
this = cur_ptr ;
184
189
// hack_save_remote_address(address_ptr, port);
185
190
break ;
@@ -329,15 +334,16 @@ static int8_t send_to_real_socket(int8_t socket_id, const ns_address_t *address,
329
334
return socket_sendmsg (socket_id , & msghdr , 0 );
330
335
}
331
336
332
- static int send_to_socket (int8_t socket_id , const uint8_t * address_ptr , uint16_t port , const uint8_t source_addr [ static 16 ] , const void * buf , size_t len )
337
+ static int send_to_socket (int8_t socket_id , void * handle , const void * buf , size_t len )
333
338
{
339
+ secure_session_t * session = handle ;
334
340
internal_socket_t * sock = int_socket_find_by_socket_id (socket_id );
335
341
if (!sock ){
336
342
return -1 ;
337
343
}
338
344
if (!sock -> real_socket ){
339
345
// Send to virtual socket cb
340
- int ret = sock -> parent -> _send_cb (sock -> listen_socket , address_ptr , port , buf , len );
346
+ int ret = sock -> parent -> _send_cb (sock -> listen_socket , session -> remote_address , session -> remote_port , buf , len );
341
347
if ( ret < 0 )
342
348
return ret ;
343
349
return len ;
@@ -353,7 +359,7 @@ static int send_to_socket(int8_t socket_id, const uint8_t *address_ptr, uint16_t
353
359
//For some reason socket_sendto returns 0 in success, while other socket impls return number of bytes sent!!!
354
360
//TODO: check if address_ptr is valid and use that instead if it is
355
361
356
- int8_t ret = send_to_real_socket (sock -> listen_socket , & sock -> dest_addr , source_addr , buf , len );
362
+ int8_t ret = send_to_real_socket (sock -> listen_socket , & sock -> dest_addr , session -> remote_address , buf , len );
357
363
if (ret < 0 ) {
358
364
return ret ;
359
365
}
@@ -536,8 +542,8 @@ static void secure_recv_sckt_msg(void *cb_res)
536
542
}
537
543
session -> last_contact_time = coap_service_get_internal_timer_ticks ();
538
544
// Start handshake
539
- if (!session -> sec_handler -> _is_started ) {
540
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
545
+ if (!coap_security_handler_is_started ( session -> sec_handler ) ) {
546
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
541
547
uint8_t pw_len ;
542
548
if ( sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> listen_socket , src_address .address , src_address .identifier , pw , & pw_len )){
543
549
//TODO: get_password_cb should support certs and PSK also
@@ -560,7 +566,7 @@ static void secure_recv_sckt_msg(void *cb_res)
560
566
if ( sock -> parent -> _security_done_cb ){
561
567
sock -> parent -> _security_done_cb (sock -> listen_socket , src_address .address ,
562
568
src_address .identifier ,
563
- session -> sec_handler -> _keyblk . value );
569
+ ( void * ) coap_security_handler_keyblock ( session -> sec_handler ) );
564
570
}
565
571
} else if (ret < 0 ){
566
572
// error handling
@@ -641,8 +647,8 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
641
647
642
648
session -> last_contact_time = coap_service_get_internal_timer_ticks ();
643
649
644
- if (!session -> sec_handler -> _is_started ) {
645
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
650
+ if (!coap_security_handler_is_started ( session -> sec_handler ) ) {
651
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
646
652
uint8_t pw_len ;
647
653
if (sock -> parent -> _get_password_cb && 0 == sock -> parent -> _get_password_cb (sock -> listen_socket , address , port , pw , & pw_len )) {
648
654
//TODO: get_password_cb should support certs and PSK also
@@ -665,7 +671,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
665
671
if ( handler -> _security_done_cb ){
666
672
handler -> _security_done_cb (sock -> listen_socket ,
667
673
address , port ,
668
- session -> sec_handler -> _keyblk . value );
674
+ ( void * ) coap_security_handler_keyblock ( session -> sec_handler ) );
669
675
}
670
676
return 0 ;
671
677
}
@@ -807,7 +813,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add
807
813
memcpy ( handler -> socket -> dest_addr .address , dest_addr -> address , 16 );
808
814
handler -> socket -> dest_addr .identifier = dest_addr -> identifier ;
809
815
handler -> socket -> dest_addr .type = dest_addr -> type ;
810
- uint8_t * pw = ( uint8_t * ) ns_dyn_mem_alloc (64 );
816
+ uint8_t * pw = ns_dyn_mem_alloc (64 );
811
817
if (!pw ) {
812
818
//todo: free secure session?
813
819
return -1 ;
0 commit comments