diff --git a/include/re_sa.h b/include/re_sa.h index 9670bfbcd..1d26a0e5f 100644 --- a/include/re_sa.h +++ b/include/re_sa.h @@ -68,6 +68,7 @@ bool sa_cmp(const struct sa *l, const struct sa *r, int flag); bool sa_is_linklocal(const struct sa *sa); bool sa_is_loopback(const struct sa *sa); +bool sa_is_multicast(const struct sa *sa); bool sa_is_any(const struct sa *sa); void sa_set_scopeid(struct sa *sa, uint32_t scopeid); diff --git a/src/sa/sa.c b/src/sa/sa.c index 10306efc8..8f6abb7f8 100644 --- a/src/sa/sa.c +++ b/src/sa/sa.c @@ -666,6 +666,30 @@ bool sa_is_loopback(const struct sa *sa) } } +/** + * Check if socket address is a multicast address + * + * @param sa Socket address + * + * @return true if multicast address, otherwise false + */ +bool sa_is_multicast(const struct sa *sa) +{ + if (!sa) + return false; + + switch (sa_af(sa)) { + + case AF_INET: + return IN_MULTICAST(ntohl(sa->u.in.sin_addr.s_addr)); + + case AF_INET6: + return IN6_IS_ADDR_MULTICAST(&sa->u.in6.sin6_addr); + + default: + return false; + } +} /** * Check if socket address is any/unspecified address