From: Arran Cudbard-Bell Date: Thu, 1 Oct 2020 18:26:04 +0000 (-0500) Subject: inet: Add function for checking multicast addresses X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7bf527b2a7030cd276972b16ce3eae71ed7dce73;p=thirdparty%2Ffreeradius-server.git inet: Add function for checking multicast addresses --- diff --git a/src/lib/util/inet.c b/src/lib/util/inet.c index 62ecf309cf6..d4a98404145 100644 --- a/src/lib/util/inet.c +++ b/src/lib/util/inet.c @@ -82,6 +82,36 @@ int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr) return 0; } +/** Determine if an address is a multicast address + * + * @param ipaddr to check. + * @return + * - 0 if it's not. + * - 1 if it is. + * - -1 on error. + */ +int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr) +{ + if (ipaddr->af == AF_INET) { + /* + * 224.0.0.0 (3758096384) - 239.255.255.255 (4026531839) + */ + if ((ipaddr->addr.v4.s_addr >= 3758096384) && (ipaddr->addr.v4.s_addr <= 4026531839)) return 1; +#ifdef HAVE_STRUCT_SOCKADDR_IN6 + } else if (ipaddr->af == AF_INET6) { + if (IN6_IS_ADDR_MULTICAST(&(ipaddr->addr.v6))) { + return 1; + } +#endif + + } else { + fr_strerror_printf("Unknown address family"); + return -1; + } + + return 0; +} + /** Determine if an address is a prefix * * @param ipaddr to check. diff --git a/src/lib/util/inet.h b/src/lib/util/inet.h index 3a1e6da80a0..7701c578b1d 100644 --- a/src/lib/util/inet.h +++ b/src/lib/util/inet.h @@ -113,6 +113,7 @@ extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */ * Utility functions */ int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr); +int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr); int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr); /* diff --git a/src/modules/proto_dhcpv6/proto_dhcpv6_udp.c b/src/modules/proto_dhcpv6/proto_dhcpv6_udp.c index 86fc09d0ca3..777a23ec18d 100644 --- a/src/modules/proto_dhcpv6/proto_dhcpv6_udp.c +++ b/src/modules/proto_dhcpv6/proto_dhcpv6_udp.c @@ -285,7 +285,7 @@ static void mod_network_get(void *instance, int *ipproto, bool *dynamic_clients, } -/** Open a UDP listener for DHCPV4 +/** Open a UDP listener for DHCPv6 * */ static int mod_open(fr_listen_t *li) @@ -479,7 +479,7 @@ static int mod_bootstrap(void *instance, CONF_SECTION *cs) /* * Remember if we're a multicast socket. */ - inst->multicast = (inst->ipaddr.addr.v6.s6_addr[0] == 0xff); + inst->multicast = (fr_ipaddr_is_multicast(&inst->ipaddr) == 1); /* * Set src_ipaddr to ipaddr if not otherwise specified @@ -507,7 +507,7 @@ static int mod_bootstrap(void *instance, CONF_SECTION *cs) } else { /* - * Multicase addresses MUST specify an interface. + * Multicast addresses MUST specify an interface. */ if (!inst->interface) goto interface_fail;