From 6c08af84a4ccebf095a517b943cc6afe7b48c457 Mon Sep 17 00:00:00 2001 From: emcho Date: Mon, 15 Feb 2010 19:35:23 +0000 Subject: [PATCH] Adds a StunTimeoutEvent to the processTimeout() method. Adds a reachability check in the stun candidate retriever. --- src/org/ice4j/ResponseCollector.java | 2 +- src/org/ice4j/StunTimeoutEvent.java | 105 ++++++++++++++++++ src/org/ice4j/TransportAddress.java | 37 ++++++ .../ice/harvest/HostCandidateHarvester.java | 5 +- .../ice/harvest/StunCandidateHarvester.java | 27 ++++- src/org/ice4j/message/Message.java | 6 +- .../ice4j/socket/SafeCloseDatagramSocket.java | 5 +- .../ice4j/stack/StunClientTransaction.java | 3 +- .../stunclient/BlockingRequestSender.java | 5 +- 9 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 src/org/ice4j/StunTimeoutEvent.java diff --git a/src/org/ice4j/ResponseCollector.java b/src/org/ice4j/ResponseCollector.java index b8bf1b0..681679e 100644 --- a/src/org/ice4j/ResponseCollector.java +++ b/src/org/ice4j/ResponseCollector.java @@ -28,7 +28,7 @@ public interface ResponseCollector * after repeated retransmissions of the original request (as described * by rfc3489) and that the request should be considered unanswered. */ - public void processTimeout(); + public void processTimeout(StunTimeoutEvent event); /** * Notifies this collector that the destination of the request has been diff --git a/src/org/ice4j/StunTimeoutEvent.java b/src/org/ice4j/StunTimeoutEvent.java new file mode 100644 index 0000000..fe25bd4 --- /dev/null +++ b/src/org/ice4j/StunTimeoutEvent.java @@ -0,0 +1,105 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. See terms of license at gnu.org. + */ +package org.ice4j; + +import java.util.*; + +import org.ice4j.message.*; +import org.ice4j.stack.*; + +/** + * The class is used to dispatch events that occur when a STUN transaction + * expires. + * + * @author Emil Ivov + */ +public class StunTimeoutEvent + extends EventObject +{ + /** + * Serial version UID for this Serializable class. + */ + private static final long serialVersionUID = 41267841L; + + /** + * The message that the corresponding transaction was about. + */ + private Message message = null; + + /** + * The TransactionID of the transaction that the source message + * is related to. + */ + private final TransactionID transactionID; + + /** + * Constructs a StunTimeoutEvent according to the specified + * message. + * + * @param message the message itself + * @param localAddress the local address that the message was sent from. + */ + public StunTimeoutEvent(Message message, + TransportAddress localAddress) + { + super(localAddress); + this.message = message; + this.transactionID = TransactionID.createTransactionID(message + .getTransactionID()); + } + + /** + * Returns the TransportAddress that the message was supposed to + * leave from. + * + * @return the TransportAddress that the message was supposed to + * leave from. + */ + public TransportAddress getLocalAddress() + { + return (TransportAddress)getSource(); + } + + /** + * Returns the Message whose transaction has just expired. + * + * @return the Message whose transaction has just expired. + */ + public Message getMessage() + { + return message; + } + + /** + * Returns the id of the transaction that has just expired. + * + * @return the TransactionID of the transaction that has just + * expired. + */ + public TransactionID getTransactionID() + { + return transactionID; + } + + /** + * Returns a String representation of this event, containing the + * corresponding message, and local address. + * + * @return a String representation of this event, containing the + * corresponding message, and local address. + */ + public String toString() + { + StringBuffer buff = new StringBuffer("StunTimeoutEvent:\n\tMessage="); + + buff.append(getMessage()); + buff.append(" localAddr=").append(getLocalAddress()); + + return buff.toString(); + } + +} diff --git a/src/org/ice4j/TransportAddress.java b/src/org/ice4j/TransportAddress.java index 4e92b77..fadaf49 100644 --- a/src/org/ice4j/TransportAddress.java +++ b/src/org/ice4j/TransportAddress.java @@ -205,4 +205,41 @@ public class TransportAddress return super.equals(obj) &&( ((TransportAddress)obj).getTransport() == getTransport() ); } + + /** + * Returns true if this is an IPv6 address and false + * otherwise. + * + * @return true if this is an IPv6 address and false + * otherwise. + */ + public boolean isIPv6() + { + return getAddress() instanceof Inet6Address; + } + + /** + * Determines whether this TransportAddress is theoretically + * capable of communicating with dst. An address is certain not + * to be able to communicate with another if they do not have the same + * Transport or family. + * + * @param dst the TransportAddress that we'd like to check for + * reachability from this one. + * + * @return true if this {@link TransportAddress} shares the same + * Transport and family as dst or false + * otherwise. + * + */ + public boolean canReach(TransportAddress dst) + { + if (dst.getTransport() != getTransport()) + return false; + + if( isIPv6() != dst.isIPv6()) + return false; + + return true; + } } diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 2ebf44f..bad4aeb 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -206,8 +206,9 @@ public class HostCandidateHarvester { logger.log(Level.INFO, "Retrying a bind because of a failure to bind to address " - + laddr + " and port " + port); - logger.log(Level.FINEST, "", se); + + laddr + " and port " + port + + " (" + se.getMessage() +")"); + logger.log(Level.INFO, "", se); } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 5411f75..0bcf444 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -100,6 +100,11 @@ public class StunCandidateHarvester */ private void startResolvingCandidate(HostCandidate hostCand) { + //first of all, make sure that the STUN server and the Candidate + //address are of the same type and that they can communicate. + if(!hostCand.getTransportAddress().canReach(stunServer)) + return; + DatagramSocket sock = hostCand.getSocket(); stunStack.addSocket(sock); @@ -122,8 +127,6 @@ public class StunCandidateHarvester resolveMap.put(tran, hostCand); } - - waitForResolutionEnd(); } /** @@ -170,13 +173,29 @@ public class StunCandidateHarvester } /** - * Notify the collector that no response had been received + * Notifies 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 that contains the transaction + * which has just expired. */ - public void processTimeout() + 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); + } } /** diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index 5229070..134d4eb 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -1,8 +1,8 @@ /* - * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). * - * Distributable under LGPL license. - * See terms of license at gnu.org. + * Distributable under LGPL license. See terms of license at gnu.org. */ package org.ice4j.message; diff --git a/src/org/ice4j/socket/SafeCloseDatagramSocket.java b/src/org/ice4j/socket/SafeCloseDatagramSocket.java index 520c62c..628bf70 100644 --- a/src/org/ice4j/socket/SafeCloseDatagramSocket.java +++ b/src/org/ice4j/socket/SafeCloseDatagramSocket.java @@ -14,8 +14,9 @@ import java.net.*; * Represents a DatagramSocket which fixes the following problem: when * {@link DatagramSocket#close()} is called while another thread is blocked in * its {@link DatagramSocket#receive(DatagramPacket)}, calling - * {@link DatagramSocket#bind(SocketAddress) throws an "Already bound" exception - * until (the native counterpart of) the receive method returns. + * {@link DatagramSocket#bind(SocketAddress)} throws an "Already bound" + * exception until (the native counterpart of) the receive method + * returns. * * @author Lubomir Marinov */ diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index a740d24..28d4b36 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -222,7 +222,8 @@ class StunClientTransaction waitFor(nextWaitInterval); - responseCollector.processTimeout(); + responseCollector.processTimeout( + new StunTimeoutEvent(this.request, getLocalAddress())); stackCallback.removeClientTransaction(this); } diff --git a/src/org/ice4j/stunclient/BlockingRequestSender.java b/src/org/ice4j/stunclient/BlockingRequestSender.java index 63b0ce9..919824f 100644 --- a/src/org/ice4j/stunclient/BlockingRequestSender.java +++ b/src/org/ice4j/stunclient/BlockingRequestSender.java @@ -99,8 +99,11 @@ class BlockingRequestSender /** * Notifies the discoverer thread when a message has timeout-ed so that * it may resume and consider it as unanswered. + * + * @param evt the StunTimeoutEvent containing the transaction that + * has just expired. */ - public synchronized void processTimeout() + public synchronized void processTimeout(StunTimeoutEvent evt) { synchronized(sendLock){ ended = true;