From 59deb864f7d7beb3f650d7107fe2495c4219817a Mon Sep 17 00:00:00 2001 From: boris Date: Mon, 2 Mar 2015 14:05:58 +0000 Subject: [PATCH] Adds a utility method for creating single-port harvesters for the IP addresses considered "allowed" by ice4j. --- .../ice/harvest/HostCandidateHarvester.java | 2 +- .../ice/harvest/SinglePortUdpHarvester.java | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 37ee1e6..1ae3efe 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -448,7 +448,7 @@ public class HostCandidateHarvester * @return true if address is allowed to be used by this * HostCandidateHarvester. */ - private static boolean isAddressAllowed(InetAddress address) + static boolean isAddressAllowed(InetAddress address) { if (address.isLoopbackAddress()) { diff --git a/src/org/ice4j/ice/harvest/SinglePortUdpHarvester.java b/src/org/ice4j/ice/harvest/SinglePortUdpHarvester.java index 3b5cfcd..0119f7c 100644 --- a/src/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -63,6 +63,65 @@ public class SinglePortUdpHarvester */ private static final int POOL_SIZE = 256; + /** + * Creates a new SinglePortUdpHarvester instance for each allowed + * IP address found on each allowed network interface, with the given port. + * + * @param port the UDP port number to use. + * @return the list of created SinglePortUdpHarvesters. + */ + public static List + createHarvesters(int port) + { + List harvesters + = new LinkedList(); + + List addresses = new LinkedList(); + + try + { + for (NetworkInterface iface + : Collections.list(NetworkInterface.getNetworkInterfaces())) + { + if (NetworkUtils.isInterfaceLoopback(iface) + || !NetworkUtils.isInterfaceUp(iface) + || !HostCandidateHarvester.isInterfaceAllowed(iface)) + { + continue; + } + + Enumeration ifaceAddresses + = iface.getInetAddresses(); + while (ifaceAddresses.hasMoreElements()) + { + InetAddress address = ifaceAddresses.nextElement(); + + addresses.add( + new TransportAddress(address, port, Transport.UDP)); + } + } + } + catch (SocketException se) + { + logger.info("Failed to get network interfaces: " + se); + } + + for (TransportAddress address : addresses) + { + try + { + harvesters.add(new SinglePortUdpHarvester(address)); + } + catch (IOException ioe) + { + logger.info("Failed to create SinglePortUdpHarvester for " + + "address " + address + ": " + ioe); + } + } + + return harvesters; + } + /** * The map which keeps the known remote addresses and their associated * candidateSockets.