Skip to content

Commit

Permalink
udp/mcast: Allow joining multicast group on a specific local interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfitch1 committed Jul 30, 2024
1 parent a64cb83 commit ed5a03e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/re_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ re_sock_t udp_sock_fd(const struct udp_sock *us, int af);

int udp_multicast_join(struct udp_sock *us, const struct sa *group);
int udp_multicast_leave(struct udp_sock *us, const struct sa *group);
int udp_multicast_join_local(struct udp_sock *us, const struct sa *group,
const struct sa *local);
int udp_multicast_leave_local(struct udp_sock *us, const struct sa *group,
const struct sa *local);
int udp_settos(struct udp_sock *us, uint8_t tos);
void udp_flush(const struct udp_sock *us);
void udp_recv_packet(struct udp_sock *us, const struct sa *src,
Expand Down
43 changes: 36 additions & 7 deletions src/udp/mcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,40 @@
*/
#include <re_types.h>
#include <re_fmt.h>
#include <re_net.h>
#include <re_sa.h>
#include <re_udp.h>


static int multicast_update(struct udp_sock *us, const struct sa *group,
bool join)
const struct sa *local, bool join)
{
struct ip_mreq mreq;
struct ip_mreqn mreq;
struct ipv6_mreq mreq6;
int err;
int if_index = 0;

if (!us || !group)
return EINVAL;

if (local && !sa_is_any(local)) {
char name[64];
if (net_if_getname(name, sizeof(name), sa_af(local), local) != 0)
return EADDRNOTAVAIL;

if ((if_index = if_nametoindex(name)) == 0)
return ENXIO;
}


switch (sa_af(group)) {

case AF_INET:
mreq.imr_multiaddr = group->u.in.sin_addr;
mreq.imr_interface.s_addr = 0;
mreq.imr_ifindex = if_index;
if (if_index == 0) {
mreq.imr_address.s_addr = 0;
}

err = udp_setsockopt(us, IPPROTO_IP,
join
Expand All @@ -34,7 +49,10 @@ static int multicast_update(struct udp_sock *us, const struct sa *group,

case AF_INET6:
mreq6.ipv6mr_multiaddr = group->u.in6.sin6_addr;
mreq6.ipv6mr_interface = sa_scopeid(group);
mreq6.ipv6mr_interface = if_index;
if (if_index = 0) {
mreq6.ipv6mr_interface = sa_scopeid(group);
}

err = udp_setsockopt(us, IPPROTO_IPV6,
join
Expand All @@ -50,14 +68,25 @@ static int multicast_update(struct udp_sock *us, const struct sa *group,
return err;
}


int udp_multicast_join(struct udp_sock *us, const struct sa *group)
{
return multicast_update(us, group, true);
return multicast_update(us, group, NULL, true);
}


int udp_multicast_leave(struct udp_sock *us, const struct sa *group)
{
return multicast_update(us, group, false);
return multicast_update(us, group, NULL, false);
}

int udp_multicast_join_local(struct udp_sock *us, const struct sa *group,
const struct sa *local)
{
return multicast_update(us, group, local, true);
}

int udp_multicast_leave_local(struct udp_sock *us, const struct sa *group,
const struct sa *local)
{
return multicast_update(us, group, local, false);
}

0 comments on commit ed5a03e

Please sign in to comment.