util: Refactor isValidPrefix to reduce address family re-use
Change-Id: I13abd5b110709210b98b4ea74e3129c3ab42bd46
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/util.hpp b/src/util.hpp
index dece269..f98a7d5 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -2,6 +2,7 @@
#include "types.hpp"
#include <netinet/ether.h>
+#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
@@ -26,10 +27,6 @@
using EthernetInterfaceIntf =
sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
-constexpr auto IPV4_MIN_PREFIX_LENGTH = 1;
-constexpr auto IPV4_MAX_PREFIX_LENGTH = 32;
-constexpr auto IPV6_MAX_PREFIX_LENGTH = 128;
-
namespace mac_address
{
@@ -75,6 +72,23 @@
constexpr auto networkdService = "systemd-networkd.service";
constexpr auto timeSynchdService = "systemd-timesyncd.service";
+template <int family>
+struct FamilyTraits
+{
+};
+
+template <>
+struct FamilyTraits<AF_INET>
+{
+ using addr = in_addr;
+};
+
+template <>
+struct FamilyTraits<AF_INET6>
+{
+ using addr = in6_addr;
+};
+
/* @brief converts a sockaddr for the specified address family into
* a type_safe InAddrAny.
* @param[in] addressFamily - The address family of the buf
@@ -98,11 +112,16 @@
bool isValidIP(int addressFamily, stdplus::const_zstring address);
/* @brief checks that the given prefix is valid or not.
- * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
+ * @param[in] family - IP address family(AF_INET/AF_INET6).
* @param[in] prefix - prefix length.
* @returns true if it is valid otherwise false.
*/
-bool isValidPrefix(int addressFamily, uint8_t prefixLength);
+template <int family>
+constexpr bool isValidPrefix(uint8_t prefix) noexcept
+{
+ return prefix <= sizeof(typename FamilyTraits<family>::addr) * 8;
+}
+bool isValidPrefix(int family, uint8_t prefixLength);
/** @brief Get all the interfaces from the system.
* @returns list of interface names.