36
36
#include "coap_message_handler.h"
37
37
#include "mbed-coap/sn_coap_protocol.h"
38
38
39
- static int16_t coap_msg_process_callback (int8_t socket_id , sn_coap_hdr_s * coap_message , coap_transaction_t * transaction_ptr , const uint8_t * local_addr );
39
+ static int16_t coap_msg_process_callback (int8_t socket_id , int8_t recv_if_id , sn_coap_hdr_s * coap_message , coap_transaction_t * transaction_ptr , const uint8_t * local_addr );
40
40
41
41
typedef struct uri_registration {
42
42
char * uri_ptr ;
@@ -70,6 +70,13 @@ static uint32_t coap_ticks = 1;
70
70
71
71
#define COAP_TICK_TIMER 0xf1
72
72
73
+ #define TRACE_DEEP
74
+ #ifdef TRACE_DEEP
75
+ #define tr_deep tr_debug
76
+ #else
77
+ #define tr_deep (...)
78
+ #endif
79
+
73
80
static uri_registration_t * uri_registration_find (coap_service_t * this , const void * uri_ptr , uint16_t uri_len )
74
81
{
75
82
ns_list_foreach (uri_registration_t , cur_ptr , & this -> uri_list ) {
@@ -165,7 +172,7 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
165
172
return 0 ;
166
173
}
167
174
168
- tr_debug ("Service %d, CoAP TX Function - mid: %d" , transaction_ptr -> service_id , common_read_16_bit (data_ptr + 2 ));
175
+ tr_debug ("Service %d, CoAP TX - mid: %d" , transaction_ptr -> service_id , common_read_16_bit (data_ptr + 2 ));
169
176
170
177
this = service_find (transaction_ptr -> service_id );
171
178
if (!this ) {
@@ -211,17 +218,44 @@ static void service_event_handler(arm_event_s *event)
211
218
eventOS_event_timer_request ((uint8_t )COAP_TICK_TIMER , ARM_LIB_SYSTEM_TIMER_EVENT , tasklet_id , 1000 );
212
219
}
213
220
214
- static int16_t coap_msg_process_callback (int8_t socket_id , sn_coap_hdr_s * coap_message , coap_transaction_t * transaction_ptr , const uint8_t * local_addr )
221
+ static int16_t coap_msg_process_callback (int8_t socket_id , int8_t recv_if_id , sn_coap_hdr_s * coap_message , coap_transaction_t * transaction_ptr , const uint8_t * local_addr )
215
222
{
216
223
coap_service_t * this ;
224
+ coap_service_msg_prevalidate_cb * msg_prevalidate_callback ;
225
+ uint16_t listen_socket_port ;
226
+
217
227
if (!coap_message || !transaction_ptr ) {
218
228
return -1 ;
219
229
}
220
230
221
- // Message is request, find correct handle
231
+ // Message is request, find correct handle based on URI
222
232
this = service_find_by_uri (socket_id , coap_message -> uri_path_ptr , coap_message -> uri_path_len );
223
233
if (!this ) {
224
- tr_debug ("not registered uri %.*s" , coap_message -> uri_path_len , coap_message -> uri_path_ptr );
234
+ tr_deep ("URI %.*s not registered" , coap_message -> uri_path_len , coap_message -> uri_path_ptr );
235
+ // URI is not available, find any service that holds the same shared socket so that we can get msg_prevalidate_callback to validate addresses
236
+ this = service_find_by_socket (socket_id );
237
+ if (!this ) {
238
+ return -1 ;
239
+ }
240
+ }
241
+
242
+ msg_prevalidate_callback = (coap_service_msg_prevalidate_cb * )coap_connection_handler_msg_prevalidate_callback_get (this -> conn_handler , & listen_socket_port );
243
+ if (msg_prevalidate_callback ) {
244
+ // message prevalidation activated for the port
245
+ char request_uri [coap_message -> uri_path_len + 1 ];
246
+ memcpy (request_uri , coap_message -> uri_path_ptr , coap_message -> uri_path_len );
247
+ request_uri [coap_message -> uri_path_len ] = 0 ;
248
+
249
+ int msg_prevalidate_status = msg_prevalidate_callback (this -> interface_id , (uint8_t * )local_addr , listen_socket_port , recv_if_id , transaction_ptr -> remote_address , transaction_ptr -> remote_port , request_uri );
250
+ if (msg_prevalidate_status >= 1 ) {
251
+ tr_deep ("Drop CoAP msg %s from %s to %s" , request_uri , trace_ipv6 (transaction_ptr -> remote_address ), trace_ipv6 (local_addr ));
252
+ return -1 ;
253
+ }
254
+ }
255
+
256
+ uri_registration_t * uri_reg_ptr = uri_registration_find (this , coap_message -> uri_path_ptr , coap_message -> uri_path_len );
257
+ if (!uri_reg_ptr ) {
258
+ /* URI is not available, stop further processing */
225
259
if (coap_message -> msg_type == COAP_MSG_TYPE_CONFIRMABLE ) {
226
260
coap_message_handler_response_send (coap_service_handle , transaction_ptr -> service_id , COAP_SERVICE_OPTIONS_NONE , coap_message ,
227
261
COAP_MSG_CODE_RESPONSE_NOT_FOUND , COAP_CT_NONE , NULL , 0 );
@@ -230,40 +264,22 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
230
264
return -1 ;
231
265
}
232
266
233
- uri_registration_t * uri_reg_ptr = uri_registration_find (this , coap_message -> uri_path_ptr , coap_message -> uri_path_len );
234
- if (uri_reg_ptr && uri_reg_ptr -> request_recv_cb ) {
235
- tr_debug ("Service %d, call request recv cb uri %.*s" , this -> service_id , coap_message -> uri_path_len , coap_message -> uri_path_ptr );
236
-
267
+ if (uri_reg_ptr -> request_recv_cb ) {
237
268
if ((this -> service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS ) == COAP_SERVICE_OPTIONS_SECURE_BYPASS ) { //TODO Add secure bypass option
238
269
// Service has secure bypass active TODO this is not defined in interface
239
270
// this check can be removed I think
240
271
transaction_ptr -> options = COAP_REQUEST_OPTIONS_SECURE_BYPASS ;
241
272
}
242
- coap_service_msg_prevalidate_cb * msg_prevalidate_callback ;
243
- uint16_t listen_socket_port ;
244
273
245
274
transaction_ptr -> service_id = this -> service_id ;
246
-
247
- msg_prevalidate_callback = (coap_service_msg_prevalidate_cb * )coap_connection_handler_msg_prevalidate_callback_get (this -> conn_handler , & listen_socket_port );
248
- if (msg_prevalidate_callback ) {
249
- // message prevalidation activated for the port
250
- char request_uri [coap_message -> uri_path_len + 1 ];
251
- memcpy (request_uri , coap_message -> uri_path_ptr , coap_message -> uri_path_len );
252
- request_uri [coap_message -> uri_path_len ] = 0 ;
253
-
254
- int msg_prevalidate_status = msg_prevalidate_callback (this -> interface_id , transaction_ptr -> remote_address , transaction_ptr -> remote_port , (uint8_t * )local_addr , listen_socket_port , request_uri );
255
- if (msg_prevalidate_status == 1 ) {
256
- tr_warn ("Drop msg %s" , request_uri );
257
- return -1 ;
258
- }
259
- }
260
-
275
+ tr_debug ("Service %d, recv msg: %.*s" , this -> service_id , coap_message -> uri_path_len , coap_message -> uri_path_ptr );
261
276
return uri_reg_ptr -> request_recv_cb (this -> service_id , transaction_ptr -> remote_address , transaction_ptr -> remote_port , coap_message );
262
277
}
278
+
263
279
return -1 ;
264
280
}
265
281
266
- static int recv_cb (int8_t socket_id , uint8_t src_address [static 16 ], uint16_t port , const uint8_t dst_address [static 16 ], unsigned char * data , int len )
282
+ static int recv_cb (int8_t socket_id , int8_t recv_if_id , uint8_t src_address [static 16 ], uint16_t port , const uint8_t dst_address [static 16 ], unsigned char * data , int len )
267
283
{
268
284
uint8_t * data_ptr = NULL ;
269
285
uint16_t data_len = 0 ;
@@ -279,10 +295,9 @@ static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t po
279
295
}
280
296
memcpy (data_ptr , data , len );
281
297
data_len = len ;
282
- tr_debug ("Service recv %d bytes" , data_len );
283
298
284
299
//parse coap message what CoAP to use
285
- int ret = coap_message_handler_coap_msg_process (coap_service_handle , socket_id , src_address , port , dst_address , data_ptr , data_len , & coap_msg_process_callback );
300
+ int ret = coap_message_handler_coap_msg_process (coap_service_handle , socket_id , recv_if_id , src_address , port , dst_address , data_ptr , data_len , & coap_msg_process_callback );
286
301
own_free (data_ptr );
287
302
return ret ;
288
303
}
@@ -645,11 +660,11 @@ int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size)
645
660
return sn_coap_protocol_set_block_size (coap_service_handle -> coap , size );
646
661
}
647
662
648
- int8_t coap_service_msg_prevalidate_callback_set (int8_t service_id , coap_service_msg_prevalidate_cb * msg_prevalidate_cb )
663
+ int8_t coap_service_msg_prevalidate_callback_set (uint16_t listen_socket , coap_service_msg_prevalidate_cb * msg_prevalidate_cb )
649
664
{
650
- coap_service_t * this = service_find ( service_id );
651
- if (this ) {
652
- return (int8_t )coap_connection_handler_msg_prevalidate_callback_set (this -> conn_handler , (cch_func_cb * )msg_prevalidate_cb );
665
+ coap_conn_handler_t * conn_handler = coap_connection_handler_find_by_socket_port ( listen_socket );
666
+ if (conn_handler ) {
667
+ return (int8_t )coap_connection_handler_msg_prevalidate_callback_set (conn_handler , (cch_func_cb * )msg_prevalidate_cb );
653
668
}
654
669
return -1 ;
655
670
}
0 commit comments