From e14e8b6a9f3d3f3fb54cafeb34eb7d0cf0ab771a Mon Sep 17 00:00:00 2001 From: gp Date: Wed, 28 May 2014 10:27:59 +0000 Subject: [PATCH] Adds two configuration properties that give the possibility to define lists of allowed or lists of blocked interfaces for host candidate allocation. --- src/org/ice4j/StackProperties.java | 32 ++++++++++ .../ice/harvest/HostCandidateHarvester.java | 64 ++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/org/ice4j/StackProperties.java b/src/org/ice4j/StackProperties.java index 7062d8a..d307f07 100644 --- a/src/org/ice4j/StackProperties.java +++ b/src/org/ice4j/StackProperties.java @@ -7,6 +7,8 @@ */ package org.ice4j; +import java.util.ArrayList; +import java.util.List; import java.util.logging.*; /** @@ -167,6 +169,36 @@ public class StackProperties return str; } + /** + * Returns the String array of the specified property, or null in case + * the returned property string array had zero length. + * + * @param propertyName the name of the property that is being queried. + * @param regex the delimiting regular expression + * + * @return the array of strings computed by splitting the specified + * property value around matches of the given regular expression + */ + public static String[] getStringArray(String propertyName, String regex) + { + String str = getString(propertyName); + if (str == null) + return null; + + String[] parts = str.split(regex); + + // Remove mal-formatted entries. + List res = new ArrayList(); + for (String s : parts) + if (s != null && s.trim().length() != 0) + res.add(s); + + if (res.size() == 0) + return null; + + return res.toArray(new String[res.size()]); + } + /** * Returns the value of a specific property as a signed decimal integer. If * a property with the specified property name exists, its string diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 298c0ef..dbc7c4f 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -27,6 +27,20 @@ import org.ice4j.socket.*; */ public class HostCandidateHarvester { + /** + * 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"; + /** * Our class logger. */ @@ -90,7 +104,8 @@ public class HostCandidateHarvester NetworkInterface iface = interfaces.nextElement(); if (NetworkUtils.isInterfaceLoopback(iface) - || !NetworkUtils.isInterfaceUp(iface)) + || !NetworkUtils.isInterfaceUp(iface) + || !isInterfaceAllowed(iface)) { //this one is obviously not going to do continue; @@ -187,6 +202,53 @@ public class HostCandidateHarvester .stopHarvestTiming(component.getLocalCandidateCount()); } + /** + * Returns a boolean value indicating whether ice4j should allocate a host + * candidate for the specified interface. + * + * @param iface The {@link NetworkInterface}. + * + * @return true if the {@link NetworkInterface} is listed in the + * org.ice4j.ice.harvest.ALLOWED_INTERFACES list. If that list + * isn't defined, returns true if it's not listed in the + * org.ice4j.ice.harvest.BLOCKED_INTERFACES list. It returns + * false otherwise. + */ + private boolean isInterfaceAllowed(NetworkInterface iface) { + if (iface == null) + throw new IllegalArgumentException("iface cannot be null"); + + // gp: use getDisplayName() on Windows and getName() on Linux. Also + // see NetworkAddressManagementServiceImpl in Jitsi. + String ifName = (System.getProperty("os.name") == null + || System.getProperty("os.name").startsWith("Windows")) + ? iface.getDisplayName() + : iface.getName(); + + String[] allowedInterfaces = StackProperties + .getStringArray(ALLOWED_INTERFACES, ";"); + + if (allowedInterfaces != null) + { + // A list of allowed interfaces exists. + return Arrays.asList(allowedInterfaces).contains(ifName); + } + else + { + // A list of allowed interfaces does not exist. + String[] blockedInterfaces = StackProperties + .getStringArray(BLOCKED_INTERFACES, ";"); + + if (blockedInterfaces != null) + { + // but a list of blocked interfaces exists. + return !Arrays.asList(blockedInterfaces).contains(ifName); + } + } + + return true; + } + /** * Creates a ServerSocket and binds it to the specified * localAddress and a port in the range specified by the