Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions src/main/java/org/ice4j/ice/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -1880,9 +1880,8 @@ protected void validatePair(CandidatePair validPair)
* {@link Component#getNominatedPair()}): the controlling agent nominates
* only one pair per component (RFC 8445, section 8.1.1). The nominated
* pair is never reset, so this agent does not nominate another pair when
* the connectivity check for the nominated pair fails either. (RFC 8445,
* section 7.2.5.3.4, has the check list fail in that case, which this
* agent does not do.)
* the connectivity check for the nominated pair fails either: the check
* list of the stream fails then (RFC 8445, section 7.2.5.3.4).
* <p>
* Applications only need to use this method if they disable this
* <tt>Agent</tt>'s internal nomination and implement their own nominator
Expand Down
73 changes: 69 additions & 4 deletions src/main/java/org/ice4j/ice/ConnectivityCheckClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void processResponse(StunResponseEvent ev)
{
logger.info("Received a non-symmetric response for pair: "
+ checkedPair.toRedactedShortString() + ". Failing.");
checkedPair.setStateFailed();
checkFailed(checkedPair, isNominating(ev.getRequest()));
}
else
{
Expand Down Expand Up @@ -543,7 +543,7 @@ private void processSuccessResponse(StunResponseEvent ev)
+ "XOR_MAPPED_ADDRESS attribute.");
logger.info("Pair failed (no XOR-MAPPED-ADDRESS): "
+ checkedPair.toRedactedShortString() + ".");
checkedPair.setStateFailed();
checkFailed(checkedPair, isNominating(request));
return; //malformed error response
}

Expand Down Expand Up @@ -894,7 +894,7 @@ private void processErrorResponse(StunResponseEvent ev)
"Error response for pair: " + pair.toRedactedShortString() +
", failing. Code = " + code +
"(class=" + cl + "; number=" + co + "): " + trimmedReason);
pair.setStateFailed();
checkFailed(pair, isNominating(originalRequest));
}
}

Expand All @@ -921,10 +921,69 @@ public void processTimeout(StunTimeoutEvent ev)
{
logger.info("timeout for pair: " + pair.toRedactedShortString() + ", failing.");
}
pair.setStateFailed();
checkFailed(
pair,
ev.getMessage() instanceof Request
&& isNominating((Request) ev.getMessage()));
updateCheckListAndTimerStates(pair);
}

/**
* Determines whether a connectivity check which we sent nominates its
* pair, i.e. whether we are the controlling agent and the request has the
* <tt>USE-CANDIDATE</tt> attribute.
*
* @param request the Binding request of the check.
* @return <tt>true</tt> if <tt>request</tt> nominates the pair that it
* checks.
*/
private boolean isNominating(Request request)
{
return parentAgent.isControlling()
&& request.containsAttribute(Attribute.USE_CANDIDATE);
}

/**
* Sets the state of <tt>pair</tt> to {@link CandidatePairState#FAILED}
* after a connectivity check for it has failed. If the check was the one
* which nominates the pair, and no pair has been selected for its
* component yet, the nomination has failed: we nominate only one pair for
* a component, so the pair is removed from the valid list and the check
* list fails (RFC 8445, section 7.2.5.3.4).
*
* @param pair the {@link CandidatePair} whose check has failed.
* @param nominating whether the check nominates the pair, see
* {@link #isNominating(Request)}.
*/
private void checkFailed(CandidatePair pair, boolean nominating)
{
pair.setStateFailed();

if (!nominating)
return;

Component component = pair.getParentComponent();
IceMediaStream stream = component.getParentStream();
CheckList checkList = stream.getCheckList();

//Once a pair has been selected, a check with USE-CANDIDATE is a
//repeated one, and its failure is not the failure of the nomination.
if (component.getSelectedPair() != null
|| checkList.getState() != CheckListState.RUNNING)
{
return;
}

logger.info(
"The nomination of pair " + pair.toRedactedShortString()
+ " failed. CheckList for stream " + stream.getName()
+ " FAILED");

stream.removeFromValidList(pair);
checkList.setState(CheckListState.FAILED);
parentAgent.checkListStatesUpdated();
}

/**
* A class to control periodically scheduled runnable that actually sends
* the checks for a particular check list in the pace defined in RFC 5245.
Expand Down Expand Up @@ -1009,6 +1068,12 @@ protected void run()
}
if (pairToCheck.getState() == CandidatePairState.FAILED)
{
//the check could not be sent. If it was the one which
//nominates the pair, the nomination has failed.
checkFailed(
pairToCheck,
parentAgent.isControlling()
&& pairToCheck.isNominated());
updateCheckListAndTimerStates(pairToCheck);
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/ice4j/ice/IceMediaStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,21 @@ protected void addToValidList(CandidatePair pair)
pair.validate();
}

/**
* Removes <tt>pair</tt>, or the pair equal to it (i.e. the one with the
* same candidates), from the <tt>valid list</tt> that this stream is
* maintaining.
*
* @param pair the {@link CandidatePair} to remove from our valid list.
*/
protected void removeFromValidList(CandidatePair pair)
{
synchronized (validList)
{
validList.remove(pair);
}
}

/**
* Returns the number of pairs in this stream's valid list. For tests.
*
Expand Down
212 changes: 212 additions & 0 deletions src/test/java/org/ice4j/ice/NominationFailureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
*
* Copyright @ 2026 - 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;

import static org.junit.jupiter.api.Assertions.*;

import java.util.concurrent.*;

import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
import org.junit.jupiter.api.*;

/**
* Tests what happens when the connectivity check which nominates a pair fails: the controlling agent nominates only
* one pair for a component, so the pair leaves the valid list and the check list fails (RFC 8445, section
* 7.2.5.3.4). The failure of any other check only fails its pair.
*/
public class NominationFailureTest
{
private Agent agent;
private IceMediaStream stream;
private Component component;
private ScheduledExecutorService scheduler;
private ExecutorService executor;
private ConnectivityCheckClient client;

/** A valid pair, and another valid pair of the same component. */
private CandidatePair pair;
private CandidatePair otherPair;

@BeforeEach
public void setUp()
{
agent = new Agent("test", null);
agent.setControlling(true);
stream = agent.createMediaStream("stream");
// Create the component directly on the stream rather than via Agent.createComponent(), which would harvest
// candidates on the real network interfaces.
component = stream.createComponent(KeepAliveStrategy.SELECTED_ONLY, false);
scheduler = Executors.newSingleThreadScheduledExecutor();
executor = Executors.newSingleThreadExecutor();
client = new ConnectivityCheckClient(agent, scheduler, executor);

HostCandidate host = new HostCandidate(new TransportAddress("192.0.2.1", 10000, Transport.UDP), component);
assertTrue(component.addLocalCandidate(host));
pair = createPair(host, "198.51.100.1", "1");
otherPair = createPair(host, "198.51.100.2", "2");
stream.addToValidList(pair);
stream.addToValidList(otherPair);
assertEquals(CheckListState.RUNNING, stream.getCheckList().getState());
}

@AfterEach
public void tearDown()
{
scheduler.shutdownNow();
executor.shutdownNow();
agent.free();
}

@Test
public void testTimeoutOfTheNominatingCheckFailsTheCheckList()
throws Exception
{
agent.nominate(pair);

client.processTimeout(createTimeoutEvent(pair, true));

assertEquals(CandidatePairState.FAILED, pair.getState());
assertEquals(CheckListState.FAILED, stream.getCheckList().getState());
assertEquals(IceProcessingState.FAILED, agent.getState());
assertSame(otherPair, stream.getValidPair(component), "The pair must have left the valid list.");
assertEquals(1, stream.getValidPairCount());
// The other valid pair is not nominated in its place.
agent.nominate(otherPair);
assertFalse(otherPair.isNominated());
}

@Test
public void testErrorResponseToTheNominatingCheckFailsTheCheckList()
throws Exception
{
agent.nominate(pair);

client.processResponse(createErrorResponseEvent(pair, true));

assertEquals(CandidatePairState.FAILED, pair.getState());
assertEquals(CheckListState.FAILED, stream.getCheckList().getState());
assertEquals(1, stream.getValidPairCount());
}

/** The failure of a check which does not nominate fails only its pair. */
@Test
public void testTimeoutOfAnOrdinaryCheckDoesNotFailTheCheckList()
throws Exception
{
client.processTimeout(createTimeoutEvent(pair, false));

assertEquals(CandidatePairState.FAILED, pair.getState());
assertEquals(CheckListState.RUNNING, stream.getCheckList().getState());
assertEquals(2, stream.getValidPairCount());
}

/**
* Once a pair has been selected, the checks for it still carry USE-CANDIDATE. When one of them fails, it is not
* the nomination which has failed.
*/
@Test
public void testTimeoutAfterAPairWasSelectedDoesNotFailTheCheckList()
throws Exception
{
agent.nominate(pair);
component.setSelectedPair(pair);

client.processTimeout(createTimeoutEvent(pair, true));

assertEquals(CheckListState.RUNNING, stream.getCheckList().getState());
assertEquals(2, stream.getValidPairCount());
}

/** Only the controlling agent nominates, so a controlled agent does not treat the failure as one of nomination. */
@Test
public void testControlledAgentDoesNotFailTheCheckList()
throws Exception
{
agent.setControlling(false);

client.processTimeout(createTimeoutEvent(pair, true));

assertEquals(CandidatePairState.FAILED, pair.getState());
assertEquals(CheckListState.RUNNING, stream.getCheckList().getState());
assertEquals(2, stream.getValidPairCount());
}

private CandidatePair createPair(HostCandidate local, String remoteAddress, String remoteFoundation)
{
RemoteCandidate remote = new RemoteCandidate(
new TransportAddress(remoteAddress, 20000, Transport.UDP),
component,
CandidateType.HOST_CANDIDATE,
remoteFoundation,
1000,
null);
return agent.createCandidatePair(local, remote);
}

/** Creates the Binding request of a check for {@code checkedPair}, with the given transaction. */
private static Request createRequest(TransactionID transactionID, boolean useCandidate)
throws Exception
{
Request request = MessageFactory.createBindingRequest(1000);
if (useCandidate)
{
request.putAttribute(AttributeFactory.createUseCandidateAttribute());
}
request.setTransactionID(transactionID.getBytes());
return request;
}

private static TransactionID createTransactionID(CandidatePair checkedPair)
{
TransactionID transactionID = TransactionID.createNewTransactionID();
transactionID.setApplicationData(checkedPair);
return transactionID;
}

private StunTimeoutEvent createTimeoutEvent(CandidatePair checkedPair, boolean useCandidate)
throws Exception
{
TransactionID transactionID = createTransactionID(checkedPair);

return new StunTimeoutEvent(
agent.getStunStack(),
createRequest(transactionID, useCandidate),
checkedPair.getLocalCandidate().getTransportAddress(),
transactionID);
}

private StunResponseEvent createErrorResponseEvent(CandidatePair checkedPair, boolean useCandidate)
throws Exception
{
TransactionID transactionID = createTransactionID(checkedPair);
Request request = createRequest(transactionID, useCandidate);
Response response = MessageFactory.createBindingErrorResponse(ErrorCodeAttribute.BAD_REQUEST);
response.setTransactionID(transactionID.getBytes());

RawMessage rawMessage = RawMessage.build(
new byte[0],
0,
checkedPair.getRemoteCandidate().getTransportAddress(),
checkedPair.getLocalCandidate().getTransportAddress());

return new StunResponseEvent(agent.getStunStack(), rawMessage, response, request, transactionID);
}
}
Loading