Skip to content

Commit

Permalink
feat(config): Port allowed and blocked interfaces config to HarvestCo…
Browse files Browse the repository at this point in the history
…nfig.
  • Loading branch information
bgrozev committed May 21, 2024
1 parent 3f7815f commit 19843e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
26 changes: 0 additions & 26 deletions src/main/java/org/ice4j/StackProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>-1</tt> 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
Expand Down
59 changes: 27 additions & 32 deletions src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> blockedInterfaces;

/**
* The list of allowed addresses.
Expand Down Expand Up @@ -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<String> getAllowedInterfaces()
{
if (!interfaceFiltersInitialized)
{
try
Expand All @@ -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<String> getBlockedInterfaces()
{
if (!interfaceFiltersInitialized)
{
try
Expand Down Expand Up @@ -450,27 +450,25 @@ public static boolean isInterfaceAllowed(NetworkInterface iface)
? iface.getDisplayName()
: iface.getName();

String[] allowedInterfaces = getAllowedInterfaces();
List<String> 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<String> 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);
}
}

Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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<NetworkInterface> allInterfaces;
Expand All @@ -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;
Expand All @@ -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");
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> by config {
"org.ice4j.ice.harvest.ALLOWED_INTERFACES".from(configSource).convertFrom<String> { 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<String> by config {
"org.ice4j.ice.harvest.BLOCKED_INTERFACES".from(configSource).convertFrom<String> { l ->
l.split(";").filter { it.isNotEmpty() }
}
"ice4j.harvest.blocked-interfaces".from(configSource)
}

val staticMappings: Set<StaticMapping> = let {
if (legacyNatHarvesterLocalAddress != null && legacyNatHarvesterPublicAddress != null) {
setOf(
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 19843e4

Please sign in to comment.