Adds a StackProperties.initialize() method that initializes and validates the allowed/blocked interface list.

This commit is contained in:
gp 2014-05-28 12:29:16 +00:00
parent e14e8b6a9f
commit d8552464e0
2 changed files with 137 additions and 20 deletions

View File

@ -7,8 +7,8 @@
*/
package org.ice4j;
import java.util.ArrayList;
import java.util.List;
import java.net.*;
import java.util.*;
import java.util.logging.*;
/**
@ -138,6 +138,36 @@ public class StackProperties
*/
public static final String DISABLE_IPv6 = "org.ice4j.ipv6.DISABLED";
/**
* 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";
/**
* Holds the list of allowed interfaces. It's either a non-empty array or null.
*/
private static String[] allowedInterfaces;
/**
* Holds the list of blocked interfaces. It's either a non-empty array or null.
*/
private static String[] blockedInterfaces;
/**
* A boolean value that indicates whether the StackProperties class has been
* initialized or not.
*/
private static boolean initialized = false;
/**
* Returns the String value of the specified property (minus all
* encompassing whitespaces)and null in case no property value was mapped
@ -179,7 +209,7 @@ public class StackProperties
* @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)
private static String[] getStringArray(String propertyName, String regex)
{
String str = getString(propertyName);
if (str == null)
@ -199,6 +229,32 @@ public class StackProperties
return res.toArray(new String[res.size()]);
}
/**
* Gets the array of allowed interfaces.
*
* @return the non-empty String array of allowed interfaces or null.
*/
public static String[] getAllowedInterfaces(){
if (!initialized)
throw new IllegalStateException("The StackProperties class has " +
"not been initialized.");
return allowedInterfaces;
}
/**
* Gets the array of blocked interfaces.
*
* @return the non-empty String array of blocked interfaces or null.
*/
public static String[] getBlockedInterfaces() {
if (!initialized)
throw new IllegalStateException("The StackProperties class has " +
"not been initialized.");
return blockedInterfaces;
}
/**
* Returns the value of a specific property as a signed decimal integer. If
* a property with the specified property name exists, its string
@ -262,4 +318,73 @@ public class StackProperties
return (str == null) ? defaultValue : Boolean.parseBoolean(str);
}
public static void initialize()
{
// Initialize the allowed interfaces array.
allowedInterfaces = StackProperties
.getStringArray(ALLOWED_INTERFACES, ";");
if (allowedInterfaces != null) // getStringArray returns null if the array is empty.
{
// Validate the allowed interfaces array.
// 1. Make sure the allowedInterfaces list contains interfaces that exist
// on the system.
for (String iface : allowedInterfaces)
try {
NetworkInterface.getByName(iface);
} catch (SocketException e) {
throw new IllegalStateException("there is no network interface " +
"with the name " + iface, e);
}
// the allowedInterfaces array is not empty and its items represent valid
// interfaces => there's at least one listening interface.
}
else
{
// NOTE The blocked interfaces list is taken into account only if the allowed
// interfaces list is not defined => initialize the blocked interfaces
// only if the allowed interfaces list is not defined.
// Initialize the blocked interfaces array.
blockedInterfaces = StackProperties
.getStringArray(BLOCKED_INTERFACES, ";");
if (blockedInterfaces != null) // getStringArray returns null if the array is empty.
{
// 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);
}
// 2. Make sure there's at least one allowed interface.
Enumeration<NetworkInterface> allInterfaces;
try {
allInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new IllegalStateException("could not get the list of the available " +
"network interfaces", e);
}
int count = 0;
while (allInterfaces.hasMoreElements()) {
allInterfaces.nextElement();
count++;
}
if (blockedInterfaces.length >= count)
throw new IllegalStateException("all network interfaces are blocked");
}
}
initialized = true;
}
}

View File

@ -27,20 +27,6 @@ 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.
*/
@ -214,7 +200,8 @@ public class HostCandidateHarvester
* <tt>org.ice4j.ice.harvest.BLOCKED_INTERFACES</tt> list. It returns
* <tt>false</tt> otherwise.
*/
private boolean isInterfaceAllowed(NetworkInterface iface) {
private boolean isInterfaceAllowed(NetworkInterface iface)
{
if (iface == null)
throw new IllegalArgumentException("iface cannot be null");
@ -226,8 +213,12 @@ public class HostCandidateHarvester
: iface.getName();
String[] allowedInterfaces = StackProperties
.getStringArray(ALLOWED_INTERFACES, ";");
.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)
{
// A list of allowed interfaces exists.
@ -237,8 +228,9 @@ public class HostCandidateHarvester
{
// A list of allowed interfaces does not exist.
String[] blockedInterfaces = StackProperties
.getStringArray(BLOCKED_INTERFACES, ";");
.getBlockedInterfaces();
// getBlockedInterfaces returns null if the array is empty.
if (blockedInterfaces != null)
{
// but a list of blocked interfaces exists.