diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 7db0cdc..d503b2c 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -393,4 +393,28 @@ class StunClientTransaction } } } + + /** + * Returns the local TransportAddress that this transaction is + * sending requests from. + * + * @return the local TransportAddress that this transaction is + * sending requests from. + */ + public TransportAddress getLocalAddress() + { + return localAddress; + } + + /** + * Returns the remote TransportAddress that this transaction is + * sending requests to. + * + * @return the remote TransportAddress that this transaction is + * sending requests to. + */ + public TransportAddress getRemoteAddress() + { + return requestDestination; + } } diff --git a/src/org/ice4j/stack/StunServerTransaction.java b/src/org/ice4j/stack/StunServerTransaction.java index 49bbbdb..bb9068a 100644 --- a/src/org/ice4j/stack/StunServerTransaction.java +++ b/src/org/ice4j/stack/StunServerTransaction.java @@ -47,19 +47,29 @@ class StunServerTransaction private StunStack stackCallback = null; /** - * The source of the transaction request. + * The address that we are sending responses to. */ private TransportAddress responseDestination = null; + /** + * The address that we are receiving requests from. + */ + private TransportAddress requestSource = null; + /** * The response sent in response to the request. */ private Response response = null; + /** + * The TransportAddress that we received our request on. + */ + private TransportAddress localListeningAddress = null; + /** * The TransportAddress we use when sending responses */ - private TransportAddress localAddress = null; + private TransportAddress localSendingAddress = null; /** * The id of the transaction. @@ -93,13 +103,21 @@ class StunServerTransaction * @param stackCallback the stack that created us. * @param tranID the transaction id contained by the request that was the * cause for this transaction. + * @param localListeningAddress the TransportAddress that this + * transaction is receiving requests on. + * @param requestSource the TransportAddress that this + * transaction is receiving requests from. */ - public StunServerTransaction(StunStack stackCallback, - TransactionID tranID) + public StunServerTransaction(StunStack stackCallback, + TransactionID tranID, + TransportAddress localListeningAddress, + TransportAddress requestSource) { this.stackCallback = stackCallback; - - this.transactionID = tranID; + this.transactionID = tranID; + this.requestSource = requestSource; + this.localListeningAddress = localListeningAddress; + this.requestSource = requestSource; runningThread = new Thread(this); } @@ -136,9 +154,9 @@ class StunServerTransaction * the transaction's state to retransmitting. * * @param response the response to send the transaction to. - * @param sendThrough the local address through which the response is to + * @param sendThrough the local address through which responses are to * be sent - * @param sendTo the destination of the response. + * @param sendTo the destination for responses of this transaction. * * @throws IOException if an error occurs while sending message bytes * through the network socket. @@ -146,7 +164,7 @@ class StunServerTransaction * access point that had not been installed, * @throws StunException if message encoding fails, */ - public void sendResponse(Response response, + public void sendResponse(Response response, TransportAddress sendThrough, TransportAddress sendTo) throws StunException, @@ -158,8 +176,8 @@ class StunServerTransaction //the transaction id might already have been set, but its our job //to make sure of that response.setTransactionID(this.transactionID.getTransactionID()); - this.localAddress = sendThrough; - this.responseDestination = sendTo; + this.localSendingAddress = sendThrough; + this.responseDestination = sendTo; } isRetransmitting = true; @@ -187,7 +205,7 @@ class StunServerTransaction return; stackCallback.getNetAccessManager().sendMessage(response, - localAddress, + localSendingAddress, responseDestination); } @@ -257,4 +275,52 @@ class StunServerTransaction { return isRetransmitting; } + + /** + * Returns the local TransportAddress that this transaction is + * sending responses from. + * + * @return the local TransportAddress that this transaction is + * sending responses from. + */ + public TransportAddress getSendingAddress() + { + return localSendingAddress; + } + + /** + * Returns the remote TransportAddress that this transaction is + * receiving requests from. + * + * @return the remote TransportAddress that this transaction is + * receiving requests from. + */ + public TransportAddress getResponseDestinationAddress() + { + return responseDestination; + } + + /** + * Returns the local TransportAddress that this transaction is + * receiving requests on. + * + * @return the local TransportAddress that this transaction is + * receiving requests on. + */ + public TransportAddress getLocalListeningAddress() + { + return localListeningAddress; + } + + /** + * Returns the remote TransportAddress that this transaction is + * receiving requests from. + * + * @return the remote TransportAddress that this transaction is + * receiving requests from. + */ + public TransportAddress getRequestSourceAddress() + { + return requestSource; + } } diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 0ae47f4..dc3885a 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -109,9 +109,66 @@ public class StunStack */ public void removeSocket(TransportAddress localAddr) { + //first cancel all transactions using this address. + cancelTransactionsForAddress(localAddr); + netAccessManager.removeSocket(localAddr); + } + /** + * Stops all transactions for the specified localAddr so that they + * won't send messages through any longer and so that we could remove the + * associated socket. + * + * @param localAddr the TransportAddress that we'd like to remove + * transactions for. + */ + private void cancelTransactionsForAddress(TransportAddress localAddr) + { + synchronized(clientTransactions) + { + Iterator> + clientTransactionsIter = clientTransactions.entrySet() + .iterator(); + while (clientTransactionsIter.hasNext()) + { + Map.Entry entry + = clientTransactionsIter.next(); + + StunClientTransaction tran = entry.getValue(); + if (tran.getLocalAddress().equals(localAddr)) + clientTransactionsIter.remove(); + + tran.cancel(); + } + } + + synchronized(serverTransactions) + { + Iterator> + serverTransactionsIter = serverTransactions.entrySet() + .iterator(); + + while (serverTransactionsIter.hasNext()) + { + Map.Entry entry + = serverTransactionsIter.next(); + + StunServerTransaction tran = entry.getValue(); + + TransportAddress listenAddr = tran.getLocalListeningAddress(); + TransportAddress sendingAddr = tran.getSendingAddress(); + + if ( listenAddr.equals(localAddr) + || (sendingAddr != null && sendingAddr.equals(localAddr)) ) + { + serverTransactionsIter.remove(); + } + + tran.expire(); + } + } } /** @@ -318,8 +375,8 @@ public class StunStack else { logger.finest("exising transaction not found"); - sTran = - new StunServerTransaction(this, serverTid); + sTran = new StunServerTransaction(this, serverTid, + event.getLocalAddress(), event.getRemoteAddress()); serverTransactions.put(serverTid, sTran); sTran.start();