Skip to content

Commit

Permalink
feat: switch multicast address to stack (eclipse-zenoh#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Nov 13, 2024
1 parent 963c02d commit 614aedb
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/link/multicast/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ size_t _z_f_link_write_all_bt(const _z_link_t *self, const uint8_t *ptr, size_t
size_t _z_f_link_read_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) {
size_t rb = _z_read_bt(self->_socket._bt._sock, ptr, len);
if ((rb > (size_t)0) && (addr != NULL)) {
*addr = _z_slice_make(strlen(self->_socket._bt._gname));
(void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, addr->len);
addr->len = strlen(self->_socket._bt._gname);
(void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, strlen(self->_socket._bt._gname));
}

return rb;
Expand All @@ -114,8 +114,8 @@ size_t _z_f_link_read_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_sli
size_t _z_f_link_read_exact_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) {
size_t rb = _z_read_exact_bt(self->_socket._bt._sock, ptr, len);
if ((rb == len) && (addr != NULL)) {
*addr = _z_slice_make(strlen(self->_socket._bt._gname));
(void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, addr->len);
addr->len = strlen(self->_socket._bt._gname);
(void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, strlen(self->_socket._bt._gname));
}

return rb;
Expand Down
4 changes: 2 additions & 2 deletions src/system/arduino/esp32/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t));
addr->len = sizeof(in_addr_t) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t));
(void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t));
}
Expand All @@ -513,7 +513,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
(memcmp(&a->sin6_addr, &b->sin6_addr, sizeof(struct in6_addr)) != 0)) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t));
addr->len = sizeof(struct in6_addr) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr));
(void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t));
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/arduino/opencr/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
IPAddress rip = sock._udp->remoteIP();
uint16_t rport = sock._udp->remotePort();

*addr = _z_slice_make(strlen((const char *)&rip[0]) + strlen((const char *)&rip[1]) +
strlen((const char *)&rip[2]) + strlen((const char *)&rip[3]) + sizeof(uint16_t));
addr->len = strlen((const char *)&rip[0]) + strlen((const char *)&rip[1]) +
strlen((const char *)&rip[2]) + strlen((const char *)&rip[3]) + sizeof(uint16_t);
uint8_t offset = 0;
for (uint8_t i = 0; i < (uint8_t)4; i++) {
(void)memcpy(const_cast<uint8_t *>(addr->start + offset), &rip[i], strlen((const char *)&rip[i]));
Expand Down
4 changes: 2 additions & 2 deletions src/system/espidf/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t));
addr->len = sizeof(in_addr_t) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t));
(void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t));
}
Expand All @@ -509,7 +509,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
(memcmp(&a->sin6_addr, &b->sin6_addr, sizeof(struct in6_addr)) != 0)) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t));
addr->len = sizeof(struct in6_addr) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr));
(void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t));
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/mbed/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
}

if (raddr.get_ip_version() == NSAPI_IPv4) {
*addr = _z_slice_make(NSAPI_IPv4_BYTES + sizeof(uint16_t));
addr->len = NSAPI_IPv4_BYTES + sizeof(uint16_t);
(void)memcpy(const_cast<uint8_t *>(addr->start), raddr.get_ip_bytes(), NSAPI_IPv4_BYTES);
uint16_t port = raddr.get_port();
(void)memcpy(const_cast<uint8_t *>(addr->start + NSAPI_IPv4_BYTES), &port, sizeof(uint16_t));
break;
} else if (raddr.get_ip_version() == NSAPI_IPv6) {
*addr = _z_slice_make(NSAPI_IPv6_BYTES + sizeof(uint16_t));
addr->len = NSAPI_IPv6_BYTES + sizeof(uint16_t);
(void)memcpy(const_cast<uint8_t *>(addr->start), raddr.get_ip_bytes(), NSAPI_IPv6_BYTES);
uint16_t port = raddr.get_port();
(void)memcpy(const_cast<uint8_t *>(addr->start + NSAPI_IPv6_BYTES), &port, sizeof(uint16_t));
Expand Down
2 changes: 1 addition & 1 deletion src/system/unix/link/raweth.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buf
// Copy sender mac if needed
if (addr != NULL) {
uint8_t *header_addr = (uint8_t *)buff;
*addr = _z_slice_make(sizeof(ETH_ALEN));
addr->len = sizeof(ETH_ALEN);
(void)memcpy((uint8_t *)addr->start, (header_addr + ETH_ALEN), sizeof(ETH_ALEN));
}
return (size_t)bytesRead;
Expand Down
7 changes: 5 additions & 2 deletions src/system/unix/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <ifaddrs.h>
#include <net/if.h>
Expand Down Expand Up @@ -535,7 +536,8 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) {
// If addr is not NULL, it means that the rep was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t));
assert(addr->len >= sizeof(in_addr_t) + sizeof(in_port_t));
addr->len = sizeof(in_addr_t) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t));
(void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t));
}
Expand All @@ -548,7 +550,8 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
(memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0))) {
// If addr is not NULL, it means that the rep was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t));
assert(addr->len >= sizeof(struct in6_addr) + sizeof(in_port_t));
addr->len = sizeof(struct in6_addr) + sizeof(in_port_t);
(void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr));
(void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t));
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/windows/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) {
// If addr is not NULL, it means that the rep was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(IN_ADDR) + sizeof(USHORT));
addr->len = sizeof(IN_ADDR) + sizeof(USHORT);
(void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(IN_ADDR));
(void)memcpy((uint8_t *)(addr->start + sizeof(IN_ADDR)), &b->sin_port, sizeof(USHORT));
}
Expand All @@ -561,7 +561,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
(memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0))) {
// If addr is not NULL, it means that the rep was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(USHORT));
addr->len = sizeof(struct in6_addr) + sizeof(USHORT);
(void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr));
(void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(USHORT));
}
Expand Down
4 changes: 2 additions & 2 deletions src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make(sizeof(uint32_t) + sizeof(uint16_t));
addr->len = sizeof(uint32_t) + sizeof(uint16_t);
(void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(uint32_t));
(void)memcpy((uint8_t *)(addr->start + sizeof(uint32_t)), &b->sin_port, sizeof(uint16_t));
}
Expand All @@ -523,7 +523,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_
(memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(uint32_t) * 4UL) == 0))) {
// If addr is not NULL, it means that the raddr was requested by the upper-layers
if (addr != NULL) {
*addr = _z_slice_make((sizeof(uint32_t) * 4UL) + sizeof(uint16_t));
addr->len = (sizeof(uint32_t) * 4UL) + sizeof(uint16_t);
(void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(uint32_t) * 4UL);
(void)memcpy((uint8_t *)(addr->start + (sizeof(uint32_t) * 4UL)), &b->sin6_port, sizeof(uint16_t));
}
Expand Down
11 changes: 6 additions & 5 deletions src/transport/multicast/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

#if Z_FEATURE_MULTICAST_TRANSPORT == 1

#define _Z_MULTICAST_ADDR_BUFF_SIZE 32 // Arbitrary size that must be able to contain any link address.

z_result_t _zp_multicast_read(_z_transport_multicast_t *ztm) {
z_result_t ret = _Z_RES_OK;

_z_slice_t addr;
static uint8_t addr_buff[_Z_MULTICAST_ADDR_BUFF_SIZE] = {0};
_z_slice_t addr = _z_slice_alias_buf(addr_buff, sizeof(addr_buff));
_z_transport_message_t t_msg;
ret = _z_multicast_recv_t_msg(ztm, &t_msg, &addr);
if (ret == _Z_RES_OK) {
Expand Down Expand Up @@ -60,7 +63,8 @@ void *_zp_multicast_read_task(void *ztm_arg) {
// Prepare the buffer
_z_zbuf_reset(&ztm->_zbuf);

_z_slice_t addr = _z_slice_empty();
uint8_t addr_buff[_Z_MULTICAST_ADDR_BUFF_SIZE] = {0};
_z_slice_t addr = _z_slice_alias_buf(addr_buff, sizeof(addr_buff));
while (ztm->_read_task_running == true) {
size_t to_read = 0;

Expand All @@ -70,7 +74,6 @@ void *_zp_multicast_read_task(void *ztm_arg) {
if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) {
_z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, &addr);
if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) {
_z_slice_clear(&addr);
_z_zbuf_compact(&ztm->_zbuf);
continue;
}
Expand Down Expand Up @@ -109,10 +112,8 @@ void *_zp_multicast_read_task(void *ztm_arg) {

if (ret == _Z_RES_OK) {
_z_t_msg_clear(&t_msg);
_z_slice_clear(&addr);
} else {
_Z_ERROR("Dropping message due to processing error: %d", ret);
_z_slice_clear(&addr);
continue;
}
} else {
Expand Down

0 comments on commit 614aedb

Please sign in to comment.