Skip to content

Commit

Permalink
net_udp: enforce v4 sock only for mac+mcast+iface
Browse files Browse the repository at this point in the history
No longer enforce v4 socket for dot-decimal IPv4 mcast addresses unless
macOS used with mcast4 addr and interface set.

The default behavior now is to use dual-home v6 socket (with v4-mapped
addresses). macOS treates the v4 mcast addresses transparently as
v6 addresses (using v6 sockopts). But interface specification doesn't
currently seem to work in macOS, neither for native v6 mcast addresses
so enforce v4 sockets here.
  • Loading branch information
MartinPulec committed Dec 4, 2024
1 parent d1023bd commit e88ad54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
30 changes: 25 additions & 5 deletions src/rtp/net_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,30 @@ static bool set_sock_opts_and_bind(fd_t fd, bool ipv6, uint16_t rx_port, int ttl
return true;
}

/**
* - checks force_ip_version validity
* - for macOS with ipv4 mcast addr and iface set, return 4 if 0 requested
* - otherwise return force_ip_version param
*/
static int
adjust_ip_version(int force_ip_version, const char *addr, const char *iface)
{
assert(force_ip_version == 0 || force_ip_version == 4 ||
force_ip_version == 6);
#ifdef __APPLE__
if (force_ip_version == 0 && iface != NULL && is_addr4(addr) &&
is_addr_multicast(addr)) {
MSG(INFO, "enforcing IPv4 mode on macOS for v4 mcast address "
"and iface set\n");
return IPv4;
}
return force_ip_version;
#else
(void) addr, (void) iface;
return force_ip_version;
#endif
}

static unsigned
get_ifindex(const char *iface)
{
Expand Down Expand Up @@ -960,11 +984,7 @@ socket_udp *udp_init_if(const char *addr, const char *iface, uint16_t rx_port,
pthread_cond_init(&s->local->boss_cv, NULL);
pthread_cond_init(&s->local->reader_cv, NULL);

assert(force_ip_version == 0 || force_ip_version == 4 || force_ip_version == 6);
s->local->mode = force_ip_version;
if (s->local->mode == 0 && is_addr_multicast(addr)) {
s->local->mode = strchr(addr, '.') != NULL ? 4 : 6;
}
s->local->mode = adjust_ip_version(force_ip_version, addr, iface);

if ((ret = resolve_address(s, addr, tx_port)) != 0) {
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Can't resolve IP address for %s: %s\n", addr,
Expand Down
8 changes: 6 additions & 2 deletions src/utils/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ bool get_local_addresses(struct sockaddr_storage *addrs, size_t *len, int ip_ver
/**
* Checks if the address is a dot-separated numeric IPv4 address.
*/
static bool is_addr4(const char *addr) {
bool
is_addr4(const char *addr)
{
// only basic check
while (*addr != '\0') {
if (!isdigit(*addr) && *addr != '.') {
Expand All @@ -370,7 +372,9 @@ static bool is_addr4(const char *addr) {
* Checks if the address is a colon-separated numeric IPv6 address
* (with optional zone index).
*/
static bool is_addr6(const char *addr) {
bool
is_addr6(const char *addr)
{
while (*addr != '\0') {
if (*addr == '%') { // skip zone identification at the end
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ extern "C" {

struct sockaddr;
struct sockaddr_storage;
bool is_addr4(const char *addr);
bool is_addr6(const char *addr);
bool is_addr_linklocal(struct sockaddr *sa);
bool is_addr_loopback(struct sockaddr *sa);
bool is_addr_private(struct sockaddr *sa);
Expand Down

0 comments on commit e88ad54

Please sign in to comment.