From 19843e49a886a8b57308814a83a4ac3757fb00e7 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Tue, 21 May 2024 13:33:32 -0500 Subject: [PATCH] feat(config): Port allowed and blocked interfaces config to HarvestConfig. --- src/main/java/org/ice4j/StackProperties.java | 26 -------- .../ice/harvest/HostCandidateHarvester.java | 59 +++++++++---------- .../org/ice4j/ice/harvest/HarvestConfig.kt | 22 +++++++ src/main/resources/reference.conf | 6 ++ 4 files changed, 55 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/ice4j/StackProperties.java b/src/main/java/org/ice4j/StackProperties.java index 4930ef76..adefeca4 100644 --- a/src/main/java/org/ice4j/StackProperties.java +++ b/src/main/java/org/ice4j/StackProperties.java @@ -120,32 +120,6 @@ public class StackProperties */ public static final String NO_KEEP_ALIVES = "org.ice4j.NO_KEEP_ALIVES"; - /** - * THIS PROPERTY IS CURRENTLY UNUSED. IF YOU WANT TO SPEED UP NOMINATIONS - * THEN CONSIDER SPEEDING UP TRANSACTION FAILURE FOR THE TIME BEING. - * The maximum number of milliseconds that we should wait for a check list - * to complete before nominating one of its valid pairs (unless there are - * none in which case we may have to wait until one appears or the whole - * list fails). Default value is -1 which causes the nominator - * to wait until the check list completes or fails. - */ - public static final String NOMINATION_TIMER - = "org.ice4j.NOMINATION_TIMER"; - - /** - * The name of the allowed interfaces property which specifies the allowed - * interfaces for host candidate allocations. - */ - public static final String ALLOWED_INTERFACES - = "org.ice4j.ice.harvest.ALLOWED_INTERFACES"; - - /** - * The name of the allowed interfaces property which specifies the blocked - * interfaces for host candidate allocations. - */ - public static final String BLOCKED_INTERFACES - = "org.ice4j.ice.harvest.BLOCKED_INTERFACES"; - /** * Returns the String value of the specified property (minus all * encompassing whitespaces)and null in case no property value was mapped diff --git a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java index ee8d1379..a233ad0c 100644 --- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -53,16 +53,14 @@ public class HostCandidateHarvester private HarvestStatistics harvestStatistics = new HarvestStatistics(); /** - * Holds the list of allowed interfaces. It's either a non-empty array or - * null. + * Holds the list of allowed interfaces. It's either a non-empty array or null. */ - private static String[] allowedInterfaces; + private static List allowedInterfaces; /** - * Holds the list of blocked interfaces. It's either a non-empty array or - * null. + * Holds the list of blocked interfaces. It's either a non-empty array or null. */ - private static String[] blockedInterfaces; + private static List blockedInterfaces; /** * The list of allowed addresses. @@ -91,7 +89,8 @@ public class HostCandidateHarvester * * @return the non-empty String array of allowed interfaces or null. */ - public static String[] getAllowedInterfaces(){ + public static List getAllowedInterfaces() + { if (!interfaceFiltersInitialized) { try @@ -113,7 +112,8 @@ public static String[] getAllowedInterfaces(){ * * @return the non-empty String array of blocked interfaces or null. */ - public static String[] getBlockedInterfaces() { + public static List getBlockedInterfaces() + { if (!interfaceFiltersInitialized) { try @@ -450,27 +450,25 @@ public static boolean isInterfaceAllowed(NetworkInterface iface) ? iface.getDisplayName() : iface.getName(); - String[] allowedInterfaces = getAllowedInterfaces(); + List allowedInterfaces = getAllowedInterfaces(); // NOTE The blocked interfaces list is taken into account only if the // allowed interfaces list is not defined. - // getAllowedInterfaces returns null if the array is empty. - if (allowedInterfaces != null) + if (!allowedInterfaces.isEmpty()) { // A list of allowed interfaces exists. - return Arrays.asList(allowedInterfaces).contains(ifName); + return allowedInterfaces.contains(ifName); } else { // A list of allowed interfaces does not exist. - String[] blockedInterfaces = getBlockedInterfaces(); + List blockedInterfaces = getBlockedInterfaces(); - // getBlockedInterfaces returns null if the array is empty. - if (blockedInterfaces != null) + if (!blockedInterfaces.isEmpty()) { // but a list of blocked interfaces exists. - return !Arrays.asList(blockedInterfaces).contains(ifName); + return !blockedInterfaces.contains(ifName); } } @@ -795,11 +793,10 @@ public static synchronized void initializeInterfaceFilters() interfaceFiltersInitialized = true; // Initialize the allowed interfaces array. - allowedInterfaces = StackProperties.getStringArray( - StackProperties.ALLOWED_INTERFACES, ";"); + allowedInterfaces = config.getAllowedInterfaces(); // getStringArray returns null if the array is empty. - if (allowedInterfaces != null) + if (!allowedInterfaces.isEmpty()) { // Validate the allowed interfaces array. @@ -812,8 +809,7 @@ public static synchronized void initializeInterfaceFilters() } catch (SocketException e) { - throw new IllegalStateException("there is no network " + - "interface with the name " + iface, e); + throw new IllegalStateException("there is no network interface with the name " + iface, e); } // the allowedInterfaces array is not empty and its items represent @@ -827,27 +823,26 @@ public static synchronized void initializeInterfaceFilters() // defined. // Initialize the blocked interfaces array. - blockedInterfaces = StackProperties.getStringArray( - StackProperties.BLOCKED_INTERFACES, ";"); + blockedInterfaces = config.getBlockedInterfaces(); // getStringArray returns null if the array is empty. - if (blockedInterfaces != null) + if (!blockedInterfaces.isEmpty()) { // Validate the blocked interfaces array. // 1. Make sure the blockedInterfaces list contains interfaces // that exist on the system. for (String iface : blockedInterfaces) + { try { NetworkInterface.getByName(iface); } catch (SocketException e) { - throw new IllegalStateException("there is no " + - "network interface with the name " + iface, - e); + throw new IllegalStateException("there is no network interface with the name " + iface, e); } + } // 2. Make sure there's at least one allowed interface. Enumeration allInterfaces; @@ -857,8 +852,7 @@ public static synchronized void initializeInterfaceFilters() } catch (SocketException e) { - throw new IllegalStateException("could not get the " + - "list of the available network interfaces", e); + throw new IllegalStateException("could not get the list of the available network interfaces", e); } int count = 0; @@ -868,9 +862,10 @@ public static synchronized void initializeInterfaceFilters() count++; } - if (blockedInterfaces.length >= count) - throw new IllegalStateException("all network " + - "interfaces are blocked"); + if (blockedInterfaces.size() >= count) + { + throw new IllegalStateException("all network interfaces are blocked"); + } } } } diff --git a/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt index 8f903f98..322eef6b 100644 --- a/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt +++ b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt @@ -128,6 +128,28 @@ class HarvestConfig { "ice4j.harvest.blocked-addresses".from(configSource) } + /** + * The allowed interfaces for host candidate allocations. If none are specified all interfaces are allowed unless + * blocked. + */ + val allowedInterfaces: List by config { + "org.ice4j.ice.harvest.ALLOWED_INTERFACES".from(configSource).convertFrom { l -> + l.split(";").filter { it.isNotEmpty() } + } + "ice4j.harvest.allowed-interfaces".from(configSource) + } + + /** + * The blocked interfaces for host candidate allocations. Note that this can not be used in conjunction with + * [allowedInterfaces]. If [allowedInterfaces] are defined then [blockedInterfaces] is not used. + */ + val blockedInterfaces: List by config { + "org.ice4j.ice.harvest.BLOCKED_INTERFACES".from(configSource).convertFrom { l -> + l.split(";").filter { it.isNotEmpty() } + } + "ice4j.harvest.blocked-interfaces".from(configSource) + } + val staticMappings: Set = let { if (legacyNatHarvesterLocalAddress != null && legacyNatHarvesterPublicAddress != null) { setOf( diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 66b71cdf..b6de64ac 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -62,6 +62,12 @@ ice4j { // The list of IP addresses that are not allowed to be used for host candidate allocations. When empty, any address // is allowed. blocked-addresses = [] + // The allowed interfaces for host candidate allocations. If none are specified all interfaces are allowed + // unless blocked. + allowed-interfaces = [] + // The blocked interfaces for host candidate allocations. Note that this can not be used in conjunction with + // allowed-interfaces. If allowed-interfaces are defined then blocked-interfaces is not used. + blocked-interfaces = [] // Configuration for the set of "mapping" harvesters. mapping {