Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize consent check interval (follow rfc7675). #270

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/main/java/org/ice4j/ice/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void run()
private final Map<String, IceMediaStream> mediaStreams
= new LinkedHashMap<>();

SecureRandom random = new SecureRandom();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs SecureRandom; ThreadLocalRandom.current() should be fine.


/**
* The candidate harvester that we use to gather candidate on the local
* machine.
Expand Down Expand Up @@ -326,7 +328,6 @@ public Agent(Logger parentLogger)
*/
public Agent(String ufragPrefix, Logger parentLogger)
{
SecureRandom random = new SecureRandom();

String ufrag = ufragPrefix == null ? "" : ufragPrefix;
ufrag += new BigInteger(24, random).toString(32);
Expand Down Expand Up @@ -2656,7 +2657,19 @@ protected Duration getDelayUntilNextRun()
{
if (shouldRunStunKeepAlive())
{
return Duration.ofMillis(keepAliveSent == 0 ? 0 : consentFreshnessInterval);
if (keepAliveSent == 0)
{
return Duration.ZERO;
}
else
{
double r = 1;
if (config.getRandomizeConsentFreshnessInterval())
{
r = 0.8d + random.nextDouble() * 0.4;
}
return Duration.ofMillis((long) (consentFreshnessInterval * r));
}
}
return Duration.ofMillis(-1);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/org/ice4j/ice/AgentConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AgentConfig {
"ice4j.consent-freshness.interval".from(configSource)
}

val randomizeConsentFreshnessInterval: Boolean by config {
"ice4j.consent-freshness.randomize".from(configSource)
}

val consentFreshnessOriginalWaitInterval: Duration by config {
"org.ice4j.ice.CONSENT_FRESHNESS_WAIT_INTERVAL".from(configSource)
.convertFrom<Long> { Duration.ofMillis(it) }
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ ice4j {
// 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
// Whether to randomize the period between any two checks between 0.8 and 1.2 of the configured interval as
// recommended in RFC7675 Section 5.1. We keep this configurable in case the previous behavior is desired.
randomize-interval = true
}

// Configuration related to harvesting (aka gathering) of local candidates.
Expand Down
Loading