Skip to content

Commit

Permalink
Fix join address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Sep 20, 2023
1 parent 97f3b17 commit ea4eb13
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/system/unix/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,32 +393,35 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
ret = _Z_ERR_GENERIC;
}

int optflag = 1;
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optflag, sizeof(optflag)) < 0)) {
int value = true;
if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) < 0)) {
ret = _Z_ERR_GENERIC;
}

#if defined(ZENOH_MACOS) || defined(ZENOH_BSD)
if ((ret == _Z_RES_OK) && (bind(sock->_fd, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen) < 0)) {
if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_REUSEPORT, &value, sizeof(value)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#elif defined(ZENOH_LINUX)

if (rep._iptcp->ai_family == AF_INET) {
struct sockaddr_in address = {AF_INET, ((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_port, {0}, {0}};
if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof address) < 0)) {
struct sockaddr_in address;
(void)memset(&address, 0, sizeof(address));
address.sin_family = rep._iptcp->ai_family;
address.sin_port = ((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_port;
inet_pton(address.sin_family, "0.0.0.0", &address.sin_addr);
if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof(address)) < 0)) {
ret = _Z_ERR_GENERIC;
}
} else if (rep._iptcp->ai_family == AF_INET6) {
struct sockaddr_in6 address = {
AF_INET6, ((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_port, 0, {{{0}}}, 0};
if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof address) < 0)) {
struct sockaddr_in6 address;
(void)memset(&address, 0, sizeof(address));
address.sin6_family = rep._iptcp->ai_family;
address.sin6_port = ((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_port;
inet_pton(address.sin6_family, "::", &address.sin6_addr);
if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof(address)) < 0)) {
ret = _Z_ERR_GENERIC;
}
} else {
ret = _Z_ERR_GENERIC;
}
#endif

// Join the multicast group
if (rep._iptcp->ai_family == AF_INET) {
Expand All @@ -443,39 +446,29 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
} else {
ret = _Z_ERR_GENERIC;
}
// Join any additional multicast group
if (join != NULL) {
char *joins = _z_str_clone(join);
struct addrinfo hints = {0};
hints.ai_family = PF_UNSPEC; // Allow IPv4 or IPv6
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
for (char *j = strsep(&joins, "|"); j != NULL; j = strsep(&joins, ";")) {
struct addrinfo *addr = NULL;
char *ip = strsep(&j, ":");
if (getaddrinfo(ip, j, &hints, &addr) < 0) {
ret = _Z_ERR_GENERIC;
}
if (addr->ai_family == AF_INET) {
for (char *ip = strsep(&joins, "|"); ip != NULL; ip = strsep(&joins, "|")) {
if (rep._iptcp->ai_family == AF_INET) {
struct ip_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr->ai_addr)->sin_addr.s_addr;
inet_pton(rep._iptcp->ai_family, ip, &mreq.imr_multiaddr);
mreq.imr_interface.s_addr = ((struct sockaddr_in *)lsockaddr)->sin_addr.s_addr;
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)) {
ret = _Z_ERR_GENERIC;
}
} else if (addr->ai_family == AF_INET6) {
} else if (rep._iptcp->ai_family == AF_INET6) {
struct ipv6_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
(void)memcpy(&mreq.ipv6mr_multiaddr, &((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr,
sizeof(struct in6_addr));
inet_pton(rep._iptcp->ai_family, ip, &mreq.ipv6mr_multiaddr);
mreq.ipv6mr_interface = if_nametoindex(iface);
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) < 0)) {
ret = _Z_ERR_GENERIC;
}
}
freeaddrinfo(addr);
}
z_free(joins);
}
Expand Down

0 comments on commit ea4eb13

Please sign in to comment.