From 627097b986779a007479b55012921a70635ddbbe Mon Sep 17 00:00:00 2001 From: emcho Date: Mon, 15 Feb 2010 22:16:10 +0000 Subject: [PATCH] Adds basic support for StunHarvester-s to the ICE candidate gathering operations --- src/org/ice4j/ResponseCollector.java | 11 ++- .../attribute/XorMappedAddressAttribute.java | 12 ++- .../ice/harvest/StunCandidateHarvester.java | 95 ++++++++++++++----- .../stunclient/BlockingRequestSender.java | 9 +- 4 files changed, 91 insertions(+), 36 deletions(-) diff --git a/src/org/ice4j/ResponseCollector.java b/src/org/ice4j/ResponseCollector.java index 681679e..2cdf716 100644 --- a/src/org/ice4j/ResponseCollector.java +++ b/src/org/ice4j/ResponseCollector.java @@ -6,8 +6,6 @@ */ package org.ice4j; -import java.net.*; - /** * The interface is used as a callback when sending a request. The response * collector is then used as a means of dispatching the response. @@ -27,6 +25,9 @@ public interface ResponseCollector * Notify the collector that no response had been received * after repeated retransmissions of the original request (as described * by rfc3489) and that the request should be considered unanswered. + * + * @param event the StunTimeoutEvent containing a reference to + * the transaction that has just failed. */ public void processTimeout(StunTimeoutEvent event); @@ -35,8 +36,8 @@ public interface ResponseCollector * determined to be unreachable and that the request should be considered * unanswered. * - * @param exception the PortUnreachableException which signaled - * that the destination of the request was found to be unreachable + * @param event the StunFailureEvent containing the + * PortUnreachableException that has just occurred. */ - public void processUnreachable(PortUnreachableException exception); + public void processUnreachable(StunFailureEvent event); } diff --git a/src/org/ice4j/attribute/XorMappedAddressAttribute.java b/src/org/ice4j/attribute/XorMappedAddressAttribute.java index 3acb9d8..bdc69f6 100644 --- a/src/org/ice4j/attribute/XorMappedAddressAttribute.java +++ b/src/org/ice4j/attribute/XorMappedAddressAttribute.java @@ -106,13 +106,15 @@ public class XorMappedAddressAttribute /** * Returns the result of applying XOR on this attribute's address, using the - * specified transaction identifier. The method may be used for both - * encoding and decoding XorMappedAddresses. - * @param transactionID the transaction id to use for the XOR + * specified operand. The method may be used for both + * encoding and decoding XorMappedAddresses. + * + * @param operand the transaction id to use for the XOR + * * @return the XOR-ed address. */ - public TransportAddress applyXor(byte[] transactionID) + public TransportAddress applyXor(byte[] operand) { - return applyXor(getAddress(), transactionID); + return applyXor(getAddress(), operand); } } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 0bcf444..4b3f8a0 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -12,10 +12,13 @@ import java.util.*; import java.util.logging.*; import org.ice4j.*; +import org.ice4j.attribute.*; import org.ice4j.ice.*; import org.ice4j.message.*; import org.ice4j.stack.*; +import com.sun.org.apache.bcel.internal.generic.*; + /** * A StunCandidateHarvester gathers STUN Candidates for a * specified {@link org.ice4j.ice.Component}. @@ -149,9 +152,10 @@ public class StunCandidateHarvester } /** - * Try to figure out . + * Matches the response to one of the local candidates and creates a + * ServerReflexiveCandidate using it as a base. * - * @param response the response to dispatch. + * @param response the response that we've just received from a StunServer. */ public void processResponse(StunMessageEvent response) { @@ -159,15 +163,21 @@ public class StunCandidateHarvester { TransactionID tranID = response.getTransactionID(); - Candidate localCand = resolveMap.remove(tranID); + HostCandidate localCand = resolveMap.remove(tranID); + + if ( localCand != null) + { + createServerReflexiveCandidate(response.getMessage(), + localCand); + } //if this was the last candidate, we are done with the STUN //resolution and need to notify the waiters. if(resolveMap.isEmpty()) resolveMap.notify(); - System.out.println("received a message tranid=" + tranID); - System.out.println("response=" + response); + logger.finest("received a message tranid=" + tranID); + logger.finest("localCand=" + localCand); } } @@ -182,20 +192,7 @@ public class StunCandidateHarvester */ public void processTimeout(StunTimeoutEvent event) { - synchronized (resolveMap) - { - TransactionID tranID = event.getTransactionID(); - - Candidate localCand = resolveMap.remove(tranID); - - //if this was the last candidate, we are done with the STUN - //resolution and need to notify the waiters. - if(resolveMap.isEmpty()) - resolveMap.notify(); - - System.out.println("a tran expired tranid=" + tranID); - System.out.println("event=" + event); - } + processFailure(event.getTransactionID()); } /** @@ -204,11 +201,65 @@ public class StunCandidateHarvester * because of the oddities of the BSD and Java socket implementations, which * is why we currently ignore this method. * - * @param exception the PortUnreachableException which signaled - * that the destination of the request was found to be unreachable + * @param event the StunFailureEvent + * PortUnreachableException which signaled that the destination of + * the request was found to be unreachable. */ - public void processUnreachable(PortUnreachableException exception) + public void processUnreachable(StunFailureEvent event) { + processFailure(event.getTransactionID()); + } + /** + * Removes the corresponding local candidate from the list of candidates + * that we are waiting on in order to complete the harvest. + * + * @param transactionID the ID of the transaction that has just ended. + */ + private void processFailure(TransactionID transactionID) + { + synchronized (resolveMap) + { + Candidate localCand = resolveMap.remove(transactionID); + + //if this was the last candidate, we are done with the STUN + //resolution and need to notify the waiters. + if(resolveMap.isEmpty()) + resolveMap.notify(); + + logger.finest("a tran expired tranid=" + transactionID); + logger.finest("localAddr=" + localCand); + } + } + + /** + * Creates a ServerReflexiveCandidate using base as its + * and the XOR-MAPPED-ADDRESS attributes in response for + * the actual TransportAddress of the new candidate. If the message + * is somehow malformed and does not contain the corresponding attribute + * this method simply has no effect. + * + * @param response the Message that is supposed to contain the + * address we should use for this candidate. + * @param base the HostCandidate that we should use as a base + * for the new ServerReflexiveCandidate. + */ + private void createServerReflexiveCandidate(Message response, + HostCandidate base) + { + Attribute attribute + = response.getAttribute(Attribute.XOR_MAPPED_ADDRESS); + + if(attribute == null + || !(attribute instanceof XorMappedAddressAttribute)) + return; + + TransportAddress addr = ((XorMappedAddressAttribute)attribute) + .applyXor(Message.MAGIC_COOKIE); + + ServerReflexiveCandidate srvrRflxCand + = new ServerReflexiveCandidate(addr, base, stunServer); + + base.getParentComponent().addLocalCandidate(srvrRflxCand); } } diff --git a/src/org/ice4j/stunclient/BlockingRequestSender.java b/src/org/ice4j/stunclient/BlockingRequestSender.java index 919824f..f5a1234 100644 --- a/src/org/ice4j/stunclient/BlockingRequestSender.java +++ b/src/org/ice4j/stunclient/BlockingRequestSender.java @@ -116,12 +116,13 @@ class BlockingRequestSender * determined to be unreachable and that the request should be considered * unanswered. * - * @param exception the PortUnreachableException which signaled - * that the destination of the request was found to be unreachable - * @see ResponseCollector#processUnreachable(PortUnreachableException) + * @param event the StunFailureEvent that contains the + * PortUnreachableException which signaled that the destination of + * the request was found to be unreachable + * @see ResponseCollector#processUnreachable(StunFailureEvent) */ public synchronized void processUnreachable( - PortUnreachableException exception) + StunFailureEvent event) { synchronized(sendLock) {