From be0ab56e9c8df772163164e88c7dd6c464e87835 Mon Sep 17 00:00:00 2001 From: emcho Date: Tue, 18 May 2010 07:25:15 +0000 Subject: [PATCH] Fixes transaction management issues --- src/org/ice4j/StunResponseEvent.java | 10 ++- .../attribute/MessageIntegrityAttribute.java | 2 +- src/org/ice4j/ice/Agent.java | 18 +++-- .../ice4j/ice/ConnectivityCheckClient.java | 3 +- .../ice4j/ice/ConnectivityCheckServer.java | 7 +- src/org/ice4j/ice/LocalCandidate.java | 3 + .../ice/harvest/StunCandidateHarvester.java | 2 +- src/org/ice4j/stack/Connector.java | 2 +- .../ice4j/stack/StunClientTransaction.java | 76 ++++++++++--------- src/org/ice4j/stack/StunStack.java | 27 ++++++- 10 files changed, 96 insertions(+), 54 deletions(-) diff --git a/src/org/ice4j/StunResponseEvent.java b/src/org/ice4j/StunResponseEvent.java index 615127e..101ee3e 100644 --- a/src/org/ice4j/StunResponseEvent.java +++ b/src/org/ice4j/StunResponseEvent.java @@ -37,13 +37,17 @@ public class StunResponseEvent * @param rawMessage the crude message we got off the wire. * @param response the STUN {@link Response} that we've just received. * @param request the message itself + * @param transactionID a reference to the exact {@link TransactionID} + * instance that represents the corresponding client transaction. */ - public StunResponseEvent(RawMessage rawMessage, - Response response, - Request request) + public StunResponseEvent(RawMessage rawMessage, + Response response, + Request request, + TransactionID transactionID) { super(rawMessage, response); this.request = request; + super.setTransactionID(transactionID); } /** diff --git a/src/org/ice4j/attribute/MessageIntegrityAttribute.java b/src/org/ice4j/attribute/MessageIntegrityAttribute.java index 53ac976..e0bd936 100644 --- a/src/org/ice4j/attribute/MessageIntegrityAttribute.java +++ b/src/org/ice4j/attribute/MessageIntegrityAttribute.java @@ -88,7 +88,7 @@ public class MessageIntegrityAttribute /** * The HMAC-SHA1 algorithm. */ - private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; + public static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; /** * The HMAC-SHA1 algorithm. diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 7c09b7e..65f55b0 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -1178,8 +1178,7 @@ public class Agent } else { - setState(IceProcessingState.FAILED); - + terminate(IceProcessingState.FAILED); } } } @@ -1383,21 +1382,30 @@ public class Agent +"speed up termination", e); } - parentAgent.terminate(); + parentAgent.terminate(IceProcessingState.TERMINATED); } } /** * Prepares everything associated with this {@link Agent} for garbage * collection and moves it into the terminated state. + * + * @param terminationState the state that we'd like processing to terminate + * with or in other words {@link IceProcessingState#TERMINATED} or + * {@link IceProcessingState#FAILED} */ - private void terminate() + private void terminate(IceProcessingState terminationState) { // free candidates + for(IceMediaStream stream : getStreams()) + { + removeStream(stream); + } + // stop listening for checks connCheckClient.stop(); connCheckServer.stop(); - setState(IceProcessingState.TERMINATED); + setState(terminationState); } } diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 2ed9fb4..bc6acf8 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -263,6 +263,7 @@ class ConnectivityCheckClient checkList.setState(CheckListState.FAILED); } + //For each frozen check list, the agent groups together all of the //pairs with the same foundation, and for each group, sets the //state of the pair with the lowest component ID to Waiting. If @@ -497,7 +498,7 @@ class ConnectivityCheckClient CandidatePair pair = ((CandidatePair)evt.getTransactionID() .getApplicationData()); - logger.finest("Received error code " + errorCode.getErrorCode()); + logger.finer("Received error code " + (int)errorCode.getErrorCode()); //RESOLVE ROLE_CONFLICTS if(errorCode.getErrorCode() == ErrorCodeAttribute.ROLE_CONFLICT) diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index 4daccd2..19389fc 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -68,6 +68,9 @@ class ConnectivityCheckServer public void processRequest(StunMessageEvent evt) throws IllegalArgumentException { + if(logger.isLoggable(Level.FINER)) + logger.finer("Received request " + evt); + Request request = (Request)evt.getMessage(); //ignore incoming requests that are not meant for the local user. @@ -210,7 +213,7 @@ class ConnectivityCheckServer //role. else { - logger.finest( + logger.finer( "Swithing to controlled because theirTieBreaker=" + theirTieBreaker + " and ourTieBreaker=" + ourTieBreaker); @@ -232,7 +235,7 @@ class ConnectivityCheckServer //the controlling role. if(ourTieBreaker >= theirTieBreaker) { - logger.finest( + logger.finer( "Swithing to controlling because theirTieBreaker=" + theirTieBreaker + " and ourTieBreaker=" + ourTieBreaker); diff --git a/src/org/ice4j/ice/LocalCandidate.java b/src/org/ice4j/ice/LocalCandidate.java index 2655688..8649434 100644 --- a/src/org/ice4j/ice/LocalCandidate.java +++ b/src/org/ice4j/ice/LocalCandidate.java @@ -10,6 +10,7 @@ package org.ice4j.ice; import java.net.*; import org.ice4j.*; +import org.ice4j.stack.*; /** * LocalCandidates are obtained by an agent for every stream component @@ -52,6 +53,8 @@ public abstract class LocalCandidate */ protected void free() { + //remove our socket from the stack. + StunStack.getInstance().removeSocket(getTransportAddress()); getSocket().close(); } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index e7ea8d7..88a7584 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -79,7 +79,7 @@ public class StunCandidateHarvester //these should be configurable. System.setProperty(StackProperties.MAX_CTRAN_RETRANS_TIMER, "400"); - System.setProperty(StackProperties.MAX_CTRAN_RETRANSMISSIONS, "2"); + System.setProperty(StackProperties.MAX_CTRAN_RETRANSMISSIONS, "3"); } /** diff --git a/src/org/ice4j/stack/Connector.java b/src/org/ice4j/stack/Connector.java index 3820c83..0b5ae26 100644 --- a/src/org/ice4j/stack/Connector.java +++ b/src/org/ice4j/stack/Connector.java @@ -146,7 +146,7 @@ class Connector if (isRunning) { logger.log(Level.WARNING, - "A net access point has gone useless:", ex); + "A connector died:", ex); stop(); //Something wrong has happened diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 8088413..c71a135 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -213,42 +213,48 @@ class StunClientTransaction retransmissionsThread.setName("ice4j.ClientTransaction"); nextWaitInterval = originalWaitInterval; - for (retransmissionCounter = 0; - retransmissionCounter < maxRetransmissions; - retransmissionCounter ++) + synchronized(this) { + for (retransmissionCounter = 0; + retransmissionCounter < maxRetransmissions; + retransmissionCounter ++) + { + waitFor(nextWaitInterval); + + //did someone tell us to get lost? + if(cancelled) + return; + + if(nextWaitInterval < maxWaitInterval) + nextWaitInterval *= 2; + + try + { + sendRequest0(); + } + catch (Exception ex) + { + //I wonder whether we should notify anyone that a + //retransmission has failed + logger.log(Level.INFO, + "A client tran retransmission failed", ex); + } + } + + //before stating that a transaction has timeout-ed we should first + //wait for a reception of the response + if(nextWaitInterval < maxWaitInterval) + nextWaitInterval *= 2; + waitFor(nextWaitInterval); - //did someone tell us to get lost? if(cancelled) return; - if(nextWaitInterval < maxWaitInterval) - nextWaitInterval *= 2; - - try - { - sendRequest0(); - } - catch (Exception ex) - { - //I wonder whether we should notify anyone that a retransmission - //has failed - logger.log(Level.WARNING, - "A client tran retransmission failed", ex); - } + stackCallback.removeClientTransaction(this); + responseCollector.processTimeout( new StunTimeoutEvent( + this.request, getLocalAddress(), transactionID)); } - - //before stating that a transaction has timeout-ed we should first wait - //for a reception of the response - if(nextWaitInterval < maxWaitInterval) - nextWaitInterval *= 2; - - waitFor(nextWaitInterval); - - responseCollector.processTimeout( new StunTimeoutEvent( - this.request, getLocalAddress(), transactionID)); - stackCallback.removeClientTransaction(this); } /** @@ -306,7 +312,7 @@ class StunClientTransaction * * @param millis the number of milliseconds to wait for. */ - synchronized void waitFor(long millis) + void waitFor(long millis) { try { @@ -349,16 +355,14 @@ class StunClientTransaction * * @param evt the event that contains the newly received message */ - void handleResponse(StunMessageEvent evt) + synchronized void handleResponse(StunMessageEvent evt) { - String keepTran = System.getProperty( - StackProperties.KEEP_CRANS_AFTER_A_RESPONSE); - - if( keepTran == null || !keepTran.trim().equalsIgnoreCase("true")) + if( !Boolean.getBoolean(StackProperties.KEEP_CRANS_AFTER_A_RESPONSE) ) this.cancel(); this.responseCollector.processResponse(new StunResponseEvent( - evt.getRawMessage(), (Response)evt.getMessage(), this.request)); + evt.getRawMessage(), (Response)evt.getMessage(), + this.request, getTransactionID())); } /** diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 29bd4fc..ba37e5f 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -8,9 +8,12 @@ package org.ice4j.stack; import java.io.*; import java.net.*; +import java.security.*; import java.util.*; import java.util.logging.*; +import javax.crypto.*; + import org.ice4j.*; import org.ice4j.attribute.*; import org.ice4j.message.*; @@ -84,7 +87,21 @@ public class StunStack public static synchronized StunStack getInstance() { if (stackInstance == null) + { + //the Mac instantiation used in MessageIntegrityAttribute could + //take several hundred milliseconds so we don't want it instantiated + //only after we get a response because the delay may cause the + //transaction to fail. + try + { + Mac.getInstance(MessageIntegrityAttribute.HMAC_SHA1_ALGORITHM); + } + catch (NoSuchAlgorithmException e) + { + e.printStackTrace(); + } stackInstance = new StunStack(); + } return stackInstance; } @@ -478,7 +495,7 @@ public class StunStack Message msg = event.getMessage(); if(logger.isLoggable(Level.FINEST)) - logger.finest("Received a message on NetAP" + logger.finest("Received a message on " + event.getLocalAddress() + " of type:" + (int)msg.getMessageType()); @@ -517,7 +534,7 @@ public class StunStack } else { - logger.finest("exising transaction not found"); + logger.finest("existing transaction not found"); sTran = new StunServerTransaction(this, serverTid, event.getLocalAddress(), event.getRemoteAddress()); @@ -532,7 +549,8 @@ public class StunStack } catch(Exception exc) { - //validation failed. get lost. + //validation failed. log get lost. + logger.log(Level.FINE, "Failed to validate msg: " + event, exc); return; } @@ -543,7 +561,7 @@ public class StunStack catch (Throwable t) { Response error; -t.printStackTrace(); + logger.log(Level.FINER, "Received an invalid request.", t); if(t instanceof IllegalArgumentException) @@ -809,6 +827,7 @@ t.printStackTrace(); if (logger.isLoggable(Level.FINEST)) logger.finest("Successfully verified msg integrity"); + return true; }