diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 40a8cba..37ee1e6 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -25,6 +25,7 @@ import org.ice4j.socket.*; * * @author Emil Ivov * @author George Politis + * @author Boris Grozev */ public class HostCandidateHarvester { @@ -51,11 +52,27 @@ public class HostCandidateHarvester */ private static String[] blockedInterfaces; + /** + * The list of allowed addresses. + */ + private static List allowedAddresses; + + /** + * The list of blocked addresses. + */ + private static List blockedAddresses; + /** * A boolean value that indicates whether the host candidate interface * filters have been initialized or not. */ - private static boolean interfaceFiltersinitialized = false; + private static boolean interfaceFiltersInitialized = false; + + /** + * A boolean value that indicates whether the host candidate address + * filters have been initialized or not. + */ + private static boolean addressFiltersInitialized = false; /** * Gets the array of allowed interfaces. @@ -63,7 +80,7 @@ public class HostCandidateHarvester * @return the non-empty String array of allowed interfaces or null. */ public static String[] getAllowedInterfaces(){ - if (!interfaceFiltersinitialized) + if (!interfaceFiltersInitialized) { try { @@ -85,7 +102,7 @@ public class HostCandidateHarvester * @return the non-empty String array of blocked interfaces or null. */ public static String[] getBlockedInterfaces() { - if (!interfaceFiltersinitialized) + if (!interfaceFiltersInitialized) { try { @@ -101,6 +118,108 @@ public class HostCandidateHarvester return blockedInterfaces; } + /** + * Gets the list of explicitly allowed addresses. + * @return the list of explicitly allowed addresses. + */ + public static List getAllowedAddresses() + { + synchronized (HostCandidateHarvester.class) + { + if (!addressFiltersInitialized) + { + initializeAddressFilters(); + } + } + + return allowedAddresses; + } + + /** + * Gets the list of blocked addresses. + * @return the list of blocked addresses. + */ + public static List getBlockedAddresses() + { + synchronized (HostCandidateHarvester.class) + { + if (!addressFiltersInitialized) + { + initializeAddressFilters(); + } + } + + return blockedAddresses; + } + + /** + * Initializes the lists of allowed and blocked addresses according to the + * configuration properties. + */ + private static void initializeAddressFilters() + { + synchronized (HostCandidateHarvester.class) + { + if (addressFiltersInitialized) + return; + + String[] allowedAddressesStr + = StackProperties.getStringArray( + StackProperties.ALLOWED_ADDRESSES, ";"); + + if (allowedAddressesStr != null) + { + for (String addressStr : allowedAddressesStr) + { + InetAddress address; + try + { + address = InetAddress.getByName(addressStr); + } + catch (Exception e) + { + logger.warning("Failed to add an allowed address: " + + addressStr); + continue; + } + + if (allowedAddresses == null) + allowedAddresses = new ArrayList(); + + allowedAddresses.add(address); + } + } + + String[] blockedAddressesStr + = StackProperties.getStringArray( + StackProperties.BLOCKED_ADDRESSES, ";"); + if (blockedAddressesStr != null) + { + for (String addressStr : blockedAddressesStr) + { + InetAddress address; + try + { + address = InetAddress.getByName(addressStr); + } + catch (Exception e) + { + logger.warning("Failed to add a blocked address: " + + addressStr); + continue; + } + + if (blockedAddresses == null) + blockedAddresses = new ArrayList(); + + blockedAddresses.add(address); + } + } + + addressFiltersInitialized = true; + } + } + /** * Gathers all candidate addresses on the local machine, binds sockets on * them and creates {@link HostCandidate}s. The harvester would always @@ -170,9 +289,8 @@ public class HostCandidateHarvester { InetAddress addr = addresses.nextElement(); - if (addr.isLoopbackAddress()) + if (!isAddressAllowed(addr)) { - //loopback again continue; } @@ -313,6 +431,42 @@ public class HostCandidateHarvester return true; } + /** + * Returns true if address is allowed to be used by this + * HostCandidateHarvester for the purposes of candidate allocation, + * and false otherwise. + * + * An address is considered allowed, if + * 1. It is not a loopback address. + * 2. Either no addresses have explicitly been configured allowed (via the + * StackProperties.ALLOWED_ADDRESSES property), or address + * is explicitly configured allowed. + * 3. address is not explicitly configured blocked (via the + * StackProperties.BLOCKED_ADDRESSES property). + * + * @param address the address to check + * @return true if address is allowed to be used by this + * HostCandidateHarvester. + */ + private static boolean isAddressAllowed(InetAddress address) + { + if (address.isLoopbackAddress()) + { + return false; + } + + boolean ret = true; + List allowed = getAllowedAddresses(); + List blocked = getBlockedAddresses(); + + if (allowed != null) + ret = allowed.contains(address); + if (blocked != null) + ret = ret && !blocked.contains(address); + + return ret; + } + /** * Creates a ServerSocket and binds it to the specified * localAddress and a port in the range specified by the @@ -559,10 +713,12 @@ public class HostCandidateHarvester synchronized (HostCandidateHarvester.class) { // We want this method to run only once. - if (interfaceFiltersinitialized) + if (interfaceFiltersInitialized) return; - interfaceFiltersinitialized = true; + interfaceFiltersInitialized = true; + + // Should we hold this lock until we are done modifying the fields? } // Initialize the allowed interfaces array.