@@ -225,9 +225,30 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui
225
225
return this ;
226
226
}
227
227
228
- 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 )
228
+ static void coap_multicast_group_join_or_leave ( int8_t socket_id , uint8_t opt_name , int8_t interface_id )
229
229
{
230
230
ns_ipv6_mreq_t ns_ipv6_mreq ;
231
+ int8_t ret_val ;
232
+
233
+ // Join or leave COAP multicast groups
234
+ ns_ipv6_mreq .ipv6mr_interface = interface_id ;
235
+
236
+ memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_LINK_LOCAL , 16 );
237
+ ret_val = socket_setsockopt (socket_id , SOCKET_IPPROTO_IPV6 , opt_name , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
238
+
239
+ memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_ADMIN_LOCAL , 16 );
240
+ ret_val |= socket_setsockopt (socket_id , SOCKET_IPPROTO_IPV6 , opt_name , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
241
+
242
+ memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_SITE_LOCAL , 16 );
243
+ ret_val |= socket_setsockopt (socket_id , SOCKET_IPPROTO_IPV6 , opt_name , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
244
+
245
+ if (ret_val ) {
246
+ tr_error ("Multicast group access failed, err=%d, name=%d" , ret_val , opt_name );
247
+ }
248
+ }
249
+
250
+ 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 , bool multicast_registration )
251
+ {
231
252
internal_socket_t * this = ns_dyn_mem_alloc (sizeof (internal_socket_t ));
232
253
233
254
if (!this ) {
@@ -276,17 +297,9 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
276
297
socket_setsockopt (this -> socket , SOCKET_IPPROTO_IPV6 , SOCKET_INTERFACE_SELECT , & socket_interface_selection , sizeof (socket_interface_selection ));
277
298
}
278
299
279
- // Join COAP multicast group(s)
280
- ns_ipv6_mreq .ipv6mr_interface = socket_interface_selection ; // It is OK to use 0 or real interface id here
281
-
282
- memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_LINK_LOCAL , 16 );
283
- socket_setsockopt (this -> socket , SOCKET_IPPROTO_IPV6 , SOCKET_IPV6_JOIN_GROUP , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
284
-
285
- memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_ADMIN_LOCAL , 16 );
286
- socket_setsockopt (this -> socket , SOCKET_IPPROTO_IPV6 , SOCKET_IPV6_JOIN_GROUP , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
287
-
288
- memcpy (ns_ipv6_mreq .ipv6mr_multiaddr , COAP_MULTICAST_ADDR_SITE_LOCAL , 16 );
289
- socket_setsockopt (this -> socket , SOCKET_IPPROTO_IPV6 , SOCKET_IPV6_JOIN_GROUP , & ns_ipv6_mreq , sizeof (ns_ipv6_mreq ));
300
+ if (multicast_registration ) {
301
+ coap_multicast_group_join_or_leave (this -> socket , SOCKET_IPV6_JOIN_GROUP , socket_interface_selection );
302
+ }
290
303
} else {
291
304
this -> socket = virtual_socket_id_allocate ();
292
305
}
@@ -793,9 +806,13 @@ coap_conn_handler_t *connection_handler_create(receive_from_socket_cb *recv_from
793
806
794
807
return handler ;
795
808
}
796
- void connection_handler_destroy (coap_conn_handler_t * handler )
809
+
810
+ void connection_handler_destroy (coap_conn_handler_t * handler , bool multicast_group_leave )
797
811
{
798
812
if (handler ){
813
+ if (multicast_group_leave ) {
814
+ coap_multicast_group_join_or_leave (handler -> socket -> socket , SOCKET_IPV6_LEAVE_GROUP , handler -> socket_interface_selection );
815
+ }
799
816
int_socket_delete (handler -> socket );
800
817
ns_dyn_mem_free (handler );
801
818
}
@@ -815,7 +832,7 @@ void connection_handler_close_secure_connection( coap_conn_handler_t *handler, u
815
832
}
816
833
}
817
834
818
- 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 )
835
+ 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 )
819
836
{
820
837
if (!handler ) {
821
838
return -1 ;
@@ -830,7 +847,7 @@ int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16
830
847
831
848
internal_socket_t * current = !use_ephemeral_port ?int_socket_find (listen_port , is_secure , is_real_socket , bypassSec ):NULL ;
832
849
if (!current ) {
833
- handler -> socket = int_socket_create (listen_port , use_ephemeral_port , is_secure , is_real_socket , bypassSec , socket_interface_selection );
850
+ handler -> socket = int_socket_create (listen_port , use_ephemeral_port , is_secure , is_real_socket , bypassSec , handler -> socket_interface_selection , handler -> registered_to_multicast );
834
851
if (!handler -> socket ) {
835
852
return -1 ;
836
853
}
0 commit comments