diff --git a/src/org/ice4j/StackProperties.java b/src/org/ice4j/StackProperties.java index db0920b..ef8d88b 100644 --- a/src/org/ice4j/StackProperties.java +++ b/src/org/ice4j/StackProperties.java @@ -151,6 +151,25 @@ public class StackProperties public static final String BLOCKED_INTERFACES = "org.ice4j.ice.harvest.BLOCKED_INTERFACES"; + /** + * The name of the property which specifies a ";"-separated list of IP + * addresses that are allowed to be used for host candidate allocations. + * + * NOTE: this is currently only supported by + * {@link org.ice4j.ice.harvest.MultiplexingTcpHostHarvester}. + */ + public static final String ALLOWED_ADDRESSES + = "org.ice4j.ice.harvest.ALLOWED_ADDRESSES"; + + /** + * The name of the property which specifies a ";"-separated list of IP + * addresses that are not allowed to be used for host candidate allocations. + * + * NOTE: this is currently only supported by + * {@link org.ice4j.ice.harvest.MultiplexingTcpHostHarvester}. + */ + public static final String BLOCKED_ADDRESSES + = "org.ice4j.ice.harvest.BLOCKED_ADDRESSES"; /** * Returns the String value of the specified property (minus all diff --git a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java index 7f9f106..2c4f9e8 100644 --- a/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java +++ b/src/org/ice4j/ice/harvest/MultiplexingTcpHostHarvester.java @@ -172,7 +172,7 @@ public class MultiplexingTcpHostHarvester throws IOException { this.ssltcp = ssltcp; - this.localAddresses.addAll(transportAddresses); + addLocalAddresses(transportAddresses); init(); } @@ -203,7 +203,7 @@ public class MultiplexingTcpHostHarvester throws IOException { this.ssltcp = ssltcp; - initializeLocalAddresses(port, interfaces); + addLocalAddresses(getLocalAddresses(port, interfaces)); init(); } @@ -232,20 +232,21 @@ public class MultiplexingTcpHostHarvester } /** - * Initializes {@link #localAddresses}. + * Returns a list of all addresses on the interfaces in interfaces + * which are found suitable for candidate allocations (are not loopback, + * are up, and are allowed by the configuration. * * @param port the port to use. * @param interfaces the list of interfaces to use. */ - private void initializeLocalAddresses( + private List getLocalAddresses( int port, List interfaces) throws IOException { - boolean useIPv6 = !StackProperties.getBoolean( - StackProperties.DISABLE_IPv6, - false); + List addresses + = new LinkedList(); for (NetworkInterface iface : interfaces) { @@ -257,27 +258,122 @@ public class MultiplexingTcpHostHarvester continue; } - Enumeration addresses = iface.getInetAddresses(); + Enumeration ifaceAddresses = iface.getInetAddresses(); - while(addresses.hasMoreElements()) + while(ifaceAddresses.hasMoreElements()) { - InetAddress addr = addresses.nextElement(); + InetAddress addr = ifaceAddresses.nextElement(); - if (addr.isLoopbackAddress()) + TransportAddress transportAddress + = new TransportAddress(addr, + port, + Transport.TCP); + addresses.add(transportAddress); + } + } + + return addresses; + } + + /** + * Adds to {@link #localAddresses} those addresses from + * transportAddresses which are found suitable for candidate + * allocation. + * + * @param transportAddresses the list of addresses to add. + */ + private void addLocalAddresses(List transportAddresses) + throws IOException + { + boolean useIPv6 = !StackProperties.getBoolean( + StackProperties.DISABLE_IPv6, + false); + + // White list from the configuration + String[] allowedAddressesStr + = StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES, + ";"); + InetAddress[] allowedAddresses = null; + if (allowedAddressesStr != null) + { + allowedAddresses = new InetAddress[allowedAddressesStr.length]; + for (int i = 0; i < allowedAddressesStr.length; i++) + { + allowedAddresses[i] + = InetAddress.getByName(allowedAddressesStr[i]); + } + } + + // Black list from the configuration + String[] blockedAddressesStr + = StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES, + ";"); + InetAddress[] blockedAddresses = null; + if (blockedAddressesStr != null) + { + blockedAddresses = new InetAddress[blockedAddressesStr.length]; + for (int i = 0; i < blockedAddressesStr.length; i++) + { + blockedAddresses[i] + = InetAddress.getByName(blockedAddressesStr[i]); + } + } + + for (TransportAddress transportAddress : transportAddresses) + { + InetAddress address = transportAddress.getAddress(); + + if (address.isLoopbackAddress()) + { + //loopback again + continue; + } + + if (!useIPv6 && (address instanceof Inet6Address)) + continue; + + if (allowedAddresses != null) + { + boolean found = false; + for (InetAddress allowedAddress : allowedAddresses) { - //loopback again + if (allowedAddress.equals(address)) + { + found = true; + break; + } + } + + if (!found) + { + logger.info("Not using " + address +" for TCP candidates, " + + "because it is not in the allowed list."); continue; } + } - if((addr instanceof Inet4Address) || useIPv6) + if (blockedAddresses != null) + { + boolean found = false; + for (InetAddress blockedAddress : blockedAddresses) { - TransportAddress transportAddress - = new TransportAddress(addr, - port, - Transport.TCP); - localAddresses.add(transportAddress); + if (blockedAddress.equals(address)) + { + found = true; + break; + } + } + + if (found) + { + logger.info("Not using " + address + " for TCP candidates, " + + "because it is in the blocked list."); + continue; } } + + // Passed all checks + localAddresses.add(transportAddress); } }