From 29ae8061add54f3c4f6315ac3b5ee2f8984156ae Mon Sep 17 00:00:00 2001 From: bgrozev Date: Mon, 14 Sep 2020 12:43:56 -0500 Subject: [PATCH] Port more config properties. (#214) * ref: Port the "disable link-local addresses" property. * ref: Port the receive buffer size property. * ref: Port the "disable ipv6" property. * ref: Port the "dynamic ports" propertty. * ref: Port the timeout property. * test: Add tests for the new properties. --- src/main/java/org/ice4j/StackProperties.java | 25 ----- src/main/java/org/ice4j/ice/Agent.java | 36 +------ .../ice/harvest/AbstractTcpListener.java | 20 ++-- .../ice/harvest/AbstractUdpListener.java | 19 ++-- .../ice/harvest/CandidateHarvesterSet.java | 2 +- .../ice/harvest/HostCandidateHarvester.java | 48 +++------ .../org/ice4j/ice/harvest/HarvestConfig.kt | 63 +++++++++++ src/main/resources/reference.conf | 44 +++++--- .../ice4j/ice/RoleConflictResolutionTest.java | 4 +- .../ice4j/ice/harvest/HarvestConfigTest.kt | 101 ++++++++++++++++++ 10 files changed, 224 insertions(+), 138 deletions(-) create mode 100644 src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt create mode 100644 src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt diff --git a/src/main/java/org/ice4j/StackProperties.java b/src/main/java/org/ice4j/StackProperties.java index db1789e..dc35a6f 100644 --- a/src/main/java/org/ice4j/StackProperties.java +++ b/src/main/java/org/ice4j/StackProperties.java @@ -138,11 +138,6 @@ public class StackProperties public static final String NOMINATION_TIMER = "org.ice4j.NOMINATION_TIMER"; - /** - * The name of the property used to disabled IPv6 support. - */ - 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. @@ -167,13 +162,6 @@ public class StackProperties public static final String ALLOWED_ADDRESSES = "org.ice4j.ice.harvest.ALLOWED_ADDRESSES"; - /** - * The name of the property which, if set to true, specifies that - * link local addresses should not be used for candidate allocations. - */ - public static final String DISABLE_LINK_LOCAL_ADDRESSES - = "org.ice4j.ice.harvest.DISABLE_LINK_LOCAL_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. @@ -184,19 +172,6 @@ public class StackProperties public static final String BLOCKED_ADDRESSES = "org.ice4j.ice.harvest.BLOCKED_ADDRESSES"; - /** - * The name of the property which specifies whether the dynamic port UDP - * host harvester should be used by Agent instances. - */ - public static final String USE_DYNAMIC_HOST_HARVESTER - = "org.ice4j.ice.harvest.USE_DYNAMIC_HOST_HARVESTER"; - - /** - * Timeout, in seconds, of how long to wait for an individual harvest before timing out - */ - public static final String HARVESTING_TIMEOUT - = "org.ice4j.ice.harvest.HARVESTING_TIMEOUT"; - /** * The name of the property which specifies whether the per-component * merging socket should be enabled by default (the default value can be diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 8cc5107..3dc16ca 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -296,12 +296,6 @@ public class Agent */ private boolean performConsentFreshness = false; - /** - * The flag which specifies whether {@link #hostCandidateHarvester} should - * be used or not. - */ - private Boolean useHostHarvester = null; - /** * The {@link Logger} used by {@link Agent} instances. */ @@ -628,7 +622,7 @@ public class Agent logger.info("Gathering candidates for component " + component.toShortString() +"."); - if (useHostHarvester()) + if (HarvestConfig.config.useDynamicPorts()) { hostCandidateHarvester.harvest( component, @@ -2588,34 +2582,6 @@ public class Agent this.performConsentFreshness = performConsentFreshness; } - /** - * Checks whether the dynamic host harvester should be used or not. - * @return true if the dynamic host harvester should be used and - * false otherwise. - */ - public boolean useHostHarvester() - { - if (useHostHarvester == null) - { - useHostHarvester - = StackProperties.getBoolean( - StackProperties.USE_DYNAMIC_HOST_HARVESTER, - true); - } - - return useHostHarvester; - } - - /** - * Sets the flag which indicates whether the dynamic host harvester will - * be used or not by this Agent. - * @param useHostHarvester the value to set. - */ - public void setUseHostHarvester(boolean useHostHarvester) - { - this.useHostHarvester = useHostHarvester; - } - /** * Sets the logging level for this {@link Agent} and its components. * @param level the level to set. diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java index 17f3e4d..a6bd283 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java @@ -29,6 +29,8 @@ import java.nio.channels.*; import java.util.*; import java.util.logging.*; +import static org.ice4j.ice.harvest.HarvestConfig.config; + /** * An abstract class that binds on a set of sockets and accepts sessions that * start with a STUN Binding Request (preceded by an optional fake SSL @@ -283,13 +285,6 @@ public abstract class AbstractTcpListener protected void addLocalAddresses(List transportAddresses) throws IOException { - boolean useIPv6 = !StackProperties.getBoolean( - StackProperties.DISABLE_IPv6, - false); - boolean useLinkLocalAddresses = !StackProperties.getBoolean( - StackProperties.DISABLE_LINK_LOCAL_ADDRESSES, - false); - // White list from the configuration String[] allowedAddressesStr = StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES, @@ -301,8 +296,7 @@ public abstract class AbstractTcpListener allowedAddresses = new InetAddress[allowedAddressesStr.length]; for (int i = 0; i < allowedAddressesStr.length; i++) { - allowedAddresses[i] - = InetAddress.getByName(allowedAddressesStr[i]); + allowedAddresses[i] = InetAddress.getByName(allowedAddressesStr[i]); } } @@ -332,14 +326,12 @@ public abstract class AbstractTcpListener continue; } - if (!useIPv6 && (address instanceof Inet6Address)) + if (!config.useIpv6() && (address instanceof Inet6Address)) continue; - if (!useLinkLocalAddresses - && address.isLinkLocalAddress()) + if (!config.useLinkLocalAddresses() && address.isLinkLocalAddress()) { - logger.info("Not using link-local address " + address +" for" - + " TCP candidates."); + logger.info("Not using link-local address " + address +" for TCP candidates."); continue; } diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java index 0f041e9..f4171dd 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java @@ -29,6 +29,8 @@ import java.util.concurrent.*; import java.util.logging.*; import java.util.logging.Logger; +import static org.ice4j.ice.harvest.HarvestConfig.config; + /** * A class which holds a {@link DatagramSocket} and runs a thread * ({@link #thread}) which perpetually reads from it. @@ -43,14 +45,6 @@ import java.util.logging.Logger; */ public abstract class AbstractUdpListener { - /** - * The name of the property which controls the size of the receive buffer - * which {@link SinglePortUdpHarvester} will request for the sockets that - * it creates. - */ - public static final String SO_RCVBUF_PNAME - = AbstractUdpListener.class.getName() + ".SO_RCVBUF"; - /** * Our class logger. */ @@ -229,8 +223,8 @@ public abstract class AbstractUdpListener socket = new DatagramSocket( this.localAddress ); - int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1); - if (receiveBufferSize > 0) + Integer receiveBufferSize = config.udpReceiveBufferSize(); + if (receiveBufferSize != null) { socket.setReceiveBufferSize(receiveBufferSize); } @@ -238,7 +232,7 @@ public abstract class AbstractUdpListener String logMessage = "Initialized AbstractUdpListener with address " + this.localAddress; logMessage += ". Receive buffer size " + socket.getReceiveBufferSize(); - if (receiveBufferSize > 0) + if (receiveBufferSize != null) { logMessage += " (asked for " + receiveBufferSize + ")"; } @@ -253,8 +247,7 @@ public abstract class AbstractUdpListener } }; - thread.setName(AbstractUdpListener.class.getName() + " thread for " - + this.localAddress); + thread.setName(AbstractUdpListener.class.getName() + " thread for " + this.localAddress); thread.setDaemon(true); thread.start(); } diff --git a/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSet.java b/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSet.java index ebee607..b1c37be 100644 --- a/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSet.java +++ b/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSet.java @@ -198,7 +198,7 @@ public class CandidateHarvesterSet { try { - future.get(StackProperties.getInt(StackProperties.HARVESTING_TIMEOUT, 15), TimeUnit.SECONDS); + future.get(HarvestConfig.config.timeout().toMillis(), TimeUnit.MILLISECONDS); break; } catch (TimeoutException te) diff --git a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java index 1bc5645..b9d5ab0 100644 --- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -26,6 +26,8 @@ import org.ice4j.*; import org.ice4j.ice.*; import org.ice4j.socket.*; +import static org.ice4j.ice.harvest.HarvestConfig.config; + /** * A HostCandidateHarvester gathers host Candidates for a * specified {@link org.ice4j.ice.Component}. Most CandidateHarvesters @@ -229,13 +231,6 @@ public class HostCandidateHarvester public static List getAllAllowedAddresses() { List addresses = new LinkedList<>(); - boolean isIPv6Disabled = StackProperties.getBoolean( - StackProperties.DISABLE_IPv6, - false); - boolean isLinkLocalAddressesDisabled = StackProperties.getBoolean( - StackProperties.DISABLE_LINK_LOCAL_ADDRESSES, - false); - try { for (NetworkInterface iface @@ -257,10 +252,9 @@ public class HostCandidateHarvester if (!isAddressAllowed(address)) continue; - if (isIPv6Disabled && address instanceof Inet6Address) + if (!config.useIpv6() && address instanceof Inet6Address) continue; - if (isLinkLocalAddressesDisabled - && address.isLinkLocalAddress()) + if (!config.useLinkLocalAddresses() && address.isLinkLocalAddress()) continue; addresses.add(address); @@ -308,21 +302,11 @@ public class HostCandidateHarvester { harvestStatistics.startHarvestTiming(); - Enumeration interfaces - = NetworkInterface.getNetworkInterfaces(); - - boolean isIPv6Disabled = StackProperties.getBoolean( - StackProperties.DISABLE_IPv6, - false); - - boolean isLinkLocalAddressesDisabled = StackProperties.getBoolean( - StackProperties.DISABLE_LINK_LOCAL_ADDRESSES, - false); + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); if(transport != Transport.UDP && transport != Transport.TCP) { - throw new IllegalArgumentException( - "Transport protocol not supported: " + transport); + throw new IllegalArgumentException("Transport protocol not supported: " + transport); } boolean boundAtLeastOneSocket = false; @@ -352,22 +336,20 @@ public class HostCandidateHarvester continue; } - if (isLinkLocalAddressesDisabled - && addr.isLinkLocalAddress()) + if (!config.useLinkLocalAddresses() && addr.isLinkLocalAddress()) { continue; } foundAtLeastOneUsableAddress = true; - if((addr instanceof Inet4Address) || !isIPv6Disabled) + if((addr instanceof Inet4Address) || !config.useIpv6()) { IceSocketWrapper sock = null; try { if(transport == Transport.UDP) { - sock = createDatagramSocket( - addr, preferredPort, minPort, maxPort); + sock = createDatagramSocket(addr, preferredPort, minPort, maxPort); boundAtLeastOneSocket = true; } else if(transport == Transport.TCP) @@ -402,10 +384,8 @@ public class HostCandidateHarvester continue; } - HostCandidate candidate - = new HostCandidate(sock, component, transport); - candidate.setVirtual( - NetworkUtils.isInterfaceVirtual(iface)); + HostCandidate candidate = new HostCandidate(sock, component, transport); + candidate.setVirtual(NetworkUtils.isInterfaceVirtual(iface)); component.addLocalCandidate(candidate); if (transport == Transport.TCP) @@ -421,8 +401,7 @@ public class HostCandidateHarvester // them as well while harvesting reflexive candidates. createAndRegisterStunSocket(candidate); - ComponentSocket componentSocket - = component.getComponentSocket(); + ComponentSocket componentSocket = component.getComponentSocket(); if (componentSocket != null) { componentSocket.add(sock); @@ -445,8 +424,7 @@ public class HostCandidateHarvester + foundAtLeastOneUsableAddress); } - this.harvestStatistics - .stopHarvestTiming(component.getLocalCandidateCount()); + this.harvestStatistics.stopHarvestTiming(component.getLocalCandidateCount()); } /** diff --git a/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt new file mode 100644 index 0000000..fccb47e --- /dev/null +++ b/src/main/kotlin/org/ice4j/ice/harvest/HarvestConfig.kt @@ -0,0 +1,63 @@ +/* + * Copyright @ 2020 - Present, 8x8 Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ice4j.ice.harvest + +import org.jitsi.metaconfig.config +import org.jitsi.metaconfig.optionalconfig +import java.time.Duration + +import org.jitsi.config.JitsiConfig.Companion.newConfig as configSource + +class HarvestConfig { + val useLinkLocalAddresses: Boolean by config { + "org.ice4j.ice.harvest.DISABLE_LINK_LOCAL_ADDRESSES".from(configSource) + .transformedBy { !it } + "ice4j.harvest.use-link-local-addresses".from(configSource) + } + fun useLinkLocalAddresses() = useLinkLocalAddresses + + val udpReceiveBufferSize: Int? by optionalconfig { + "org.ice4j.ice.harvest.AbstractUdpListener.SO_RCVBUF".from(configSource) + "ice4j.harvest.udp.receive-buffer-size".from(configSource) + } + fun udpReceiveBufferSize() = udpReceiveBufferSize + + val useIpv6: Boolean by config { + "org.ice4j.ipv6.DISABLED".from(configSource) + .transformedBy { !it } + "ice4j.harvest.use-ipv6".from(configSource) + } + fun useIpv6() = useIpv6 + + val useDynamicPorts: Boolean by config { + "org.ice4j.ice.harvest.USE_DYNAMIC_HOST_HARVESTER".from(configSource) + "ice4j.harvest.udp.use-dynamic-ports".from(configSource) + } + fun useDynamicPorts() = useDynamicPorts + + val timeout: Duration by config { + "org.ice4j.ice.harvest.HARVESTING_TIMEOUT".from(configSource) + .convertFrom { Duration.ofSeconds(it) } + "ice4j.harvest.timeout".from(configSource) + } + fun timeout() = timeout + + companion object { + @JvmField + val config = HarvestConfig() + } +} \ No newline at end of file diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 2179a49..2c94fca 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -1,25 +1,45 @@ ice4j { ice { max-check-list-size = 100 - # The amount of time that we must wait after ICE processing enters a COMPLETED state before we free candidates - # and move into the TERMINATED state. + // The amount of time that we must wait after ICE processing enters a COMPLETED state before we free candidates + // and move into the TERMINATED state. termination-delay = 3 seconds } consent-freshness { - # How often a STUN Binding request used for consent freshness check will be sent. + // How often a STUN Binding request used for consent freshness check will be sent. interval = 15 seconds - # The amount of time without a valid STUN Binding response after which a STUN Binding request is to be - # retransmitted according to `STUN Usage for Consent Freshness` (RFC7675). - # This is the original value for back-off strategy, while `max-wait-interval` sets the upper limit. + // The amount of time without a valid STUN Binding response after which a STUN Binding request is to be + // retransmitted according to `STUN Usage for Consent Freshness` (RFC7675). + // This is the original value for back-off strategy, while `max-wait-interval` sets the upper limit. original-wait-interval = 500 milliseconds - # The amount of time without a valid STUN Binding response after which a STUN Binding request is to be - # retransmitted according to `STUN Usage for Consent Freshness` (RFC7675). - # This is the final value for the back-off strategy, while `original-wait-interval` defines the initial interval for - # the first request sent. + // The amount of time without a valid STUN Binding response after which a STUN Binding request is to be + // retransmitted according to `STUN Usage for Consent Freshness` (RFC7675). + // This is the final value for the back-off strategy, while `original-wait-interval` defines the initial interval for + // the first request sent. max-wait-interval = 500 milliseconds - # The maximum number of retransmissions of a STUN Binding request without a valid STUN Binding response after which - # consent freshness is to be considered unconfirmed according to `STUN Usage for Consent Freshness` (RFC7675). + // The maximum number of retransmissions of a STUN Binding request without a valid STUN Binding response after which + // consent freshness is to be considered unconfirmed according to `STUN Usage for Consent Freshness` (RFC7675). max-retransmissions = 30 } + + // Configuration related to harvesting (aka gathering) of local candidates. + harvest { + // Whether to harvest IPv6 addresses. + use-ipv6 = true + // Whether to use link-local addresses when harvesting candidates. + use-link-local-addresses = true + + // How long to wait for an individual harvest before timing out. + timeout = 15 seconds + + // Configuration for the "single port" UDP harvester. + udp { + // The size in bytes to set on the UDP socket (SO_RCVBUF). If not specified, the default from the OS will be used. + #receive-buffer-size = 10485760 + + // Whether to allocate ephemeral ports for local candidates. + use-dynamic-ports = true + } + } } diff --git a/src/test/java/org/ice4j/ice/RoleConflictResolutionTest.java b/src/test/java/org/ice4j/ice/RoleConflictResolutionTest.java index 75303ce..c57e918 100644 --- a/src/test/java/org/ice4j/ice/RoleConflictResolutionTest.java +++ b/src/test/java/org/ice4j/ice/RoleConflictResolutionTest.java @@ -102,10 +102,8 @@ public class RoleConflictResolutionTest throws IOException { final Agent agent = new Agent(label, null); - agent.setUseHostHarvester(true); agent.setControlling(iceControlling); - IceMediaStream iceStream - = agent.createMediaStream("media-stream"); + IceMediaStream iceStream = agent.createMediaStream("media-stream"); agent.createComponent( iceStream, diff --git a/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt b/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt new file mode 100644 index 0000000..e69d18b --- /dev/null +++ b/src/test/kotlin/org/ice4j/ice/harvest/HarvestConfigTest.kt @@ -0,0 +1,101 @@ +/* + * Copyright @ 2020 - present 8x8, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ice4j.ice.harvest + +import io.kotest.matchers.shouldBe +import org.ice4j.ConfigTest +import org.ice4j.ice.harvest.HarvestConfig.Companion.config +import org.jitsi.utils.secs + +class HarvestConfigTest : ConfigTest() { + init { + context("Default values") { + config.timeout shouldBe 15.secs + config.useDynamicPorts shouldBe true + config.useIpv6 shouldBe true + config.useLinkLocalAddresses shouldBe true + config.udpReceiveBufferSize shouldBe null + } + context("Setting via legacy config (system properties)") { + withLegacyConfig(legacyConfig) { + config.timeout shouldBe 555.secs + config.useDynamicPorts shouldBe false + config.useIpv6 shouldBe false + config.useLinkLocalAddresses shouldBe false + config.udpReceiveBufferSize shouldBe 555 + } + } + context("Setting via new config") { + withNewConfig(newConfigNonDefault) { + config.timeout shouldBe 666.secs + config.useDynamicPorts shouldBe false + config.useIpv6 shouldBe false + config.useLinkLocalAddresses shouldBe false + config.udpReceiveBufferSize shouldBe 666 + } + } + context("Legacy config must take precedence") { + withLegacyConfig(legacyConfig) { + withNewConfig(newConfigDefault) { + config.timeout shouldBe 555.secs + config.useDynamicPorts shouldBe false + config.useIpv6 shouldBe false + config.useLinkLocalAddresses shouldBe false + config.udpReceiveBufferSize shouldBe 555 + } + } + } + } +} + +private val legacyConfig = mapOf( + "org.ice4j.ice.harvest.HARVESTING_TIMEOUT" to "555", + "org.ice4j.ice.harvest.USE_DYNAMIC_HOST_HARVESTER" to "false", + "org.ice4j.ipv6.DISABLED" to "true", + "org.ice4j.ice.harvest.DISABLE_LINK_LOCAL_ADDRESSES" to "true", + "org.ice4j.ice.harvest.AbstractUdpListener.SO_RCVBUF" to "555" +) + +// New config which overrides the default +private val newConfigNonDefault = """ + ice4j { + harvest { + timeout = 666 seconds + use-ipv6 = false + use-link-local-addresses = false + udp { + receive-buffer-size = 666 + use-dynamic-ports = false + } + } + } +""".trimIndent() + +// New config which does not override the defaults (to test precedence). +private val newConfigDefault = """ + ice4j { + harvest { + timeout = 666 seconds + use-ipv6 = true + use-link-local-addresses = true + udp { + receive-buffer-size = 666 + use-dynamic-ports = true + } + } + } +""".trimIndent()