Skip to content

Commit

Permalink
Register to multicast groups (ARMmbed#66)
Browse files Browse the repository at this point in the history
* Register to multicast groups

COAP service registers automatically to COAP multicast groups:
-ff02::fd (link-local according to RFC 7390)
-ff05::fd (site local according to RFC 7390)
  • Loading branch information
Arto Kinnunen authored Apr 27, 2017
1 parent dce323c commit 381d910
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions coap-service/coap_service_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ extern "C" {
#define COAP_REQUEST_OPTIONS_MULTICAST 0x04 //!< indicates that CoAP library support multicasting
#define COAP_REQUEST_OPTIONS_SECURE_BYPASS 0x08

extern const uint8_t COAP_MULTICAST_ADDR_LINK_LOCAL[16]; //!< ff02::fd, COAP link local multicast address
extern const uint8_t COAP_MULTICAST_ADDR_ADMIN_LOCAL[16]; //!< ff03::fd, COAP admin-local multicast address
extern const uint8_t COAP_MULTICAST_ADDR_SITE_LOCAL[16]; //!> ff05::fd, COAP site-local multicast address

/**
* \brief Service message response receive callback.
*
Expand Down
24 changes: 21 additions & 3 deletions source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ typedef struct internal_socket_s {
ns_list_link_t link;
} internal_socket_t;

const uint8_t COAP_MULTICAST_ADDR_LINK_LOCAL[16] = { 0xff, 0x02, [15] = 0xfd }; // ff02::fd, COAP link-local multicast (rfc7390)
const uint8_t COAP_MULTICAST_ADDR_ADMIN_LOCAL[16] = { 0xff, 0x03, [15] = 0xfd }; // ff02::fd, COAP admin-local multicast (rfc7390)
const uint8_t COAP_MULTICAST_ADDR_SITE_LOCAL[16] = { 0xff, 0x05, [15] = 0xfd }; // ff05::fd, COAP site-local multicast (rfc7390)

static NS_LIST_DEFINE(socket_list, internal_socket_t, link);

static void timer_cb(void* param);
Expand Down Expand Up @@ -223,7 +227,9 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui

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)
{
ns_ipv6_mreq_t ns_ipv6_mreq;
internal_socket_t *this = ns_dyn_mem_alloc(sizeof(internal_socket_t));

if (!this) {
return NULL;
}
Expand Down Expand Up @@ -265,10 +271,22 @@ 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));
if (socket_interface_selection > 0) {
// Interface selection requested as socket_interface_selection set
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_INTERFACE_SELECT, &socket_interface_selection, sizeof(socket_interface_selection));
}

// Join COAP multicast group(s)
ns_ipv6_mreq.ipv6mr_interface = socket_interface_selection; // It is OK to use 0 or real interface id here

memcpy(ns_ipv6_mreq.ipv6mr_multiaddr, COAP_MULTICAST_ADDR_LINK_LOCAL, 16);
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_JOIN_GROUP, &ns_ipv6_mreq, sizeof(ns_ipv6_mreq));

memcpy(ns_ipv6_mreq.ipv6mr_multiaddr, COAP_MULTICAST_ADDR_ADMIN_LOCAL, 16);
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_JOIN_GROUP, &ns_ipv6_mreq, sizeof(ns_ipv6_mreq));

memcpy(ns_ipv6_mreq.ipv6mr_multiaddr, COAP_MULTICAST_ADDR_SITE_LOCAL, 16);
socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_JOIN_GROUP, &ns_ipv6_mreq, sizeof(ns_ipv6_mreq));
} else {
this->socket = virtual_socket_id_allocate();
}
Expand Down
2 changes: 1 addition & 1 deletion source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +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)
{
int8_t socket_interface_selection = -1;
int8_t socket_interface_selection = 0; // zero is illegal interface ID
coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t));
if (!this) {
return -1;
Expand Down

0 comments on commit 381d910

Please sign in to comment.