From a8c22244fba848cfe4b4842bbb0cf7d70f136274 Mon Sep 17 00:00:00 2001 From: emcho Date: Mon, 15 Feb 2010 15:53:00 +0000 Subject: [PATCH] Resolves several conflicts --- src/org/ice4j/ResponseCollector.java | 10 +- .../ice/harvest/StunCandidateHarvester.java | 144 +++++++++++++++++- src/org/ice4j/message/Message.java | 45 +++--- src/org/ice4j/stack/Connector.java | 3 - src/org/ice4j/stack/NetAccessManager.java | 6 +- .../ice4j/stack/StunClientTransaction.java | 10 +- src/org/ice4j/stack/StunStack.java | 48 +++++- src/org/ice4j/stack/TransactionID.java | 15 +- 8 files changed, 229 insertions(+), 52 deletions(-) diff --git a/src/org/ice4j/ResponseCollector.java b/src/org/ice4j/ResponseCollector.java index e3c31fd..b8bf1b0 100644 --- a/src/org/ice4j/ResponseCollector.java +++ b/src/org/ice4j/ResponseCollector.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; @@ -12,15 +12,13 @@ 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. * - *

Organization: Louis Pasteur University, Strasbourg, France

- *

Network Research Team (http://www-r2.u-strasbg.fr)

* @author Emil Ivov - * @version 0.1 */ public interface ResponseCollector { /** * Dispatch the specified response. + * * @param response the response to dispatch. */ public void processResponse(StunMessageEvent response); diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 27128a9..997cec7 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -7,7 +7,14 @@ */ package org.ice4j.ice.harvest; +import java.io.*; +import java.net.*; +import java.util.*; + +import org.ice4j.*; import org.ice4j.ice.*; +import org.ice4j.message.*; +import org.ice4j.stack.*; /** * A StunCandidateHarvester gathers STUN Candidates for a @@ -16,19 +23,150 @@ import org.ice4j.ice.*; * @author Emil Ivov */ public class StunCandidateHarvester - implements CandidateHarvester + implements CandidateHarvester, + ResponseCollector { + + /** + * The stack to use for STUN communication. + */ + private final StunStack stunStack; + + /** + * The address of the STUN server that we will be sending our requests to. + */ + private final TransportAddress stunServer; + + /** + * The list of candidates that we are currently resolving. + */ + private final List resolveList + = new LinkedList(); + + /** + * Creates a new STUN harvester that will be running against the specified + * stunServer. + * + * @param stunServer the address of the STUN server that we will be querying + * for our public bindings + */ + public StunCandidateHarvester(TransportAddress stunServer) + { + stunStack = StunStack.getInstance(); + this.stunServer = stunServer; + + //these should be configurable. + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "400"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); + } + /** * Gathers STUN candidates for all host Candidates that are already - * present in the specified component. + * present in the specified component. This method relies on the + * specified component to already contain all its host candidates + * so that it would resolve them. * * @param component the {@link Component} that we'd like to gather candidate * STUN Candidates for. */ public void harvest(Component component) { - //todo implement + List localCandidates = component.getLocalCandidates(); + for(Candidate cand : localCandidates) + { + if ( !(cand instanceof HostCandidate) ) + continue; + + HostCandidate hostCand = (HostCandidate)cand; + + startResolvingCandidate(hostCand); + + waitForResolutionEnd(); + } + } + + /** + * Sends a binding request to our stun server through the specified + * hostCand candidate and adds it to the list of addresses still + * waiting for resolution. + * + * @param hostCand the HostCandidate that we'd like to resolve. + */ + private void startResolvingCandidate(HostCandidate hostCand) + { + DatagramSocket sock = hostCand.getSocket(); + stunStack.addSocket(sock); + + synchronized(resolveList) + { + resolveList.add(hostCand); + } + + try + { + stunStack.sendRequest(MessageFactory.createBindingRequest(), + stunServer, sock, this); + } + catch (Exception e) + { + synchronized(resolveList) + { + resolveList.remove(hostCand); + } + } + } + + /** + * Blocks the current thread until all resolutions in this harverster + * have terminated one way or another. + */ + private void waitForResolutionEnd() + { + synchronized(resolveList) + { + if(resolveList.isEmpty()) + return; + + try + { + resolveList.wait(); + } + catch (InterruptedException e){} + } + } + + /** + * Try to figure out . + * + * @param response the response to dispatch. + */ + public void processResponse(StunMessageEvent response) + { + response.getMessage().getTransactionID(); + } + + /** + * 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. + */ + public void processTimeout() + { + + } + + /** + * Called when one of our requests results in a + * PortUnreachableException ... which is actually quite rare + * 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 + */ + public void processUnreachable(PortUnreachableException exception) + { } } diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index dd2b09a..7911cc8 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -18,11 +18,8 @@ import org.ice4j.attribute.*; * STUN attributes, the set of which depends on the message type. The STUN * header contains a STUN message type, transaction ID, and length. * - *

Organisation: Louis Pasteur University, Strasbourg, France

- *

Network Research Team (http://www-r2.u-strasbg.fr)

* @author Emil Ivov * @author Sebastien Vincent - * @version 0.1 */ public abstract class Message { @@ -97,7 +94,8 @@ public abstract class Message * so we'll be using a LinkedHashMap */ //not sure this is the best solution but I'm trying to keep entry order - protected LinkedHashMap attributes = new LinkedHashMap(); + protected LinkedHashMap attributes + = new LinkedHashMap(); /** * Desribes which attributes are present in which messages. An @@ -187,7 +185,7 @@ public abstract class Message protected static final byte PRIORITY_PRESENTITY_INDEX = 28; protected static final byte ICE_CONTROLLING_PRESENTITY_INDEX = 29; protected static final byte ICE_CONTROLLED_PRESENTITY_INDEX = 30; - protected static final byte USE_CANDIDATE_PRESENTITY_INDEX = 31; + protected static final byte USE_CANDIDATE_PRESENTITY_INDEX = 31; protected final static byte attributePresentities[][] = new byte[][]{ // Binding Shared Shared Shared Alloc Alloc Rfrsh Rfrsh ChnlBnd ChnlBnd Send Data @@ -213,7 +211,7 @@ public abstract class Message /*ALTERNATE_SERVER*/ { O, O, O, O, O, O, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A}, /*REALM*/ { O, N_A, N_A, N_A, M, N_A, O, O, O, O, O, O, N_A, N_A}, /*NONCE*/ { O, N_A, N_A, N_A, M, N_A, O, O, O, O, O, O, N_A, N_A}, - /*FINGERPRINT*/ { O, O, O, O, O, O, O, O, O, O, O, O, N_A, N_A}, + /*FINGERPRINT*/ { O, O, O, O, O, O, O, O, O, O, O, O, N_A, N_A}, /*CHANNEL-NUMBER*/ { N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, M, N_A, N_A, N_A}, /*LIFETIME*/ { N_A, N_A, N_A, N_A, N_A, N_A, O, N_A, M, N_A, N_A, N_A, N_A, N_A}, /*XOR-PEER-ADDRESS*/ { N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, N_A, M, N_A, M, M}, @@ -493,12 +491,13 @@ public abstract class Message /** * Returns a binary representation of this message. + * * @return a binary representation of this message. - * @throws StunException if the message does not have all required - * attributes. + * @throws IllegalStateException if the message does not have all + * required attributes. */ public byte[] encode() - throws StunException + throws IllegalStateException { //make sure we have everything necessary to encode a proper message validateAttributePresentity(); @@ -514,11 +513,18 @@ public abstract class Message System.arraycopy(MAGIC_COOKIE, 0, binMsg, offset, 4); offset+=4; - System.arraycopy(getTransactionID(), 0, binMsg, offset, TRANSACTION_ID_LENGTH); + System.arraycopy(getTransactionID(), 0, binMsg, offset, + TRANSACTION_ID_LENGTH); offset+=TRANSACTION_ID_LENGTH; - for (Attribute attribute : attributes.values()) + Iterator> iter + = attributes.entrySet().iterator(); + while (iter.hasNext()) { + + Attribute attribute + = ((Map.Entry)iter.next()).getValue(); + byte[] attBinValue = attribute.encode(); System.arraycopy(attBinValue, 0, binMsg, offset, attBinValue.length); offset += attBinValue.length; @@ -567,7 +573,7 @@ public abstract class Message /* copy the cookie */ byte cookie[] = new byte[4]; System.arraycopy(binMessage, offset, cookie, 0, 4); - offset+=4; + offset+=4; if(arrayLen - offset - TRANSACTION_ID_LENGTH < length) throw new StunException(StunException.ILLEGAL_ARGUMENT, @@ -596,18 +602,16 @@ public abstract class Message * Verify that the message has all obligatory attributes and throw an * exception if this is not the case. * - * @throws StunException (ILLEGAL_STATE)if the message does not have all + * @throws IllegalStateException if the message does not have all * required attributes. */ protected void validateAttributePresentity() - throws StunException + throws IllegalStateException { for(char i = Attribute.MAPPED_ADDRESS; i < Attribute.REFLECTED_FROM; i++) if(getAttributePresentity(i) == M && getAttribute(i) == null) - throw new StunException(StunException.ILLEGAL_STATE, - "A mandatory attribute (type=" - +(int)i - + ") is missing!"); + throw new IllegalStateException( + "A mandatory attribute (type=" +(int)i + ") is missing!"); } @@ -665,6 +669,11 @@ public abstract class Message return ((type & 0x0110) == STUN_REQUEST); } + /** + * Returns a String representation of this message. + * + * @return a String representation of this message. + */ public String toString() { return getName()+"("+getMessageType() diff --git a/src/org/ice4j/stack/Connector.java b/src/org/ice4j/stack/Connector.java index 55e0331..6b4804d 100644 --- a/src/org/ice4j/stack/Connector.java +++ b/src/org/ice4j/stack/Connector.java @@ -83,11 +83,8 @@ class Connector /** * Start the network listening thread. - * - * @throws IOException if we fail to setup the socket. */ void start() - throws IOException { this.isRunning = true; Thread thread = new Thread(this); diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index 85b93ad..988b0d4 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -138,11 +138,8 @@ class NetAccessManager * has no effect. * * @param socket the socket that the access point should use. - * - * @throws IOException if we fail to setup the socket. */ protected void addSocket(DatagramSocket socket) - throws IOException { //no null check - let it through a null pointer exception TransportAddress localAddr = new TransportAddress( @@ -261,12 +258,11 @@ class NetAccessManager * through the network socket. * @throws IllegalArgumentException if the apDescriptor references an * access point that had not been installed, - * @throws StunException if message encoding fails, */ void sendMessage(Message stunMessage, TransportAddress srcAddr, TransportAddress remoteAddr) - throws IOException, IllegalArgumentException, StunException + throws IOException, IllegalArgumentException { byte[] bytes = stunMessage.encode(); Connector ap = netAccessPoints.get(srcAddr); diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index 8f58a6b..cff9416 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -231,11 +231,16 @@ class StunClientTransaction * @throws IOException if an error occurs while sending message bytes * through the network socket. * @throws IllegalArgumentException if the apDescriptor references an +<<<<<<< HEAD * access point that had not been installed * @throws StunException if message encoding fails +======= + * access point that had not been installed, + * +>>>>>>> Makes stun transactions more visible to users of the stun stack */ void sendRequest() - throws StunException, IllegalArgumentException, IOException + throws IllegalArgumentException, IOException { sendRequest0(); @@ -249,10 +254,9 @@ class StunClientTransaction * through the network socket. * @throws IllegalArgumentException if the apDescriptor references an * access point that had not been installed, - * @throws StunException if message encoding fails, */ private void sendRequest0() - throws StunException, IllegalArgumentException, IOException + throws IllegalArgumentException, IOException { if(cancelled) { diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 9d5a988..b40df39 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -92,10 +92,8 @@ public class StunStack * specified socket and returns a relevant descriptor. * * @param sock The socket that the new access point should represent. - * @throws IOException if we fail to setup the socket. */ public void addSocket(DatagramSocket sock) - throws IOException { netAccessManager.addSocket(sock); } @@ -195,17 +193,19 @@ public class StunStack * @param collector the instance to notify when a response arrives or the * the transaction timeouts * + * @return the TransactionID of the StunClientTransaction + * that we used in order to send the request. + * * @throws IOException if an error occurs while sending message bytes * through the network socket. * @throws IllegalArgumentException if the apDescriptor references an * access point that had not been installed, - * @throws StunException if message encoding fails */ - public void sendRequest( Request request, - TransportAddress sendTo, - TransportAddress sendThrough, - ResponseCollector collector ) - throws StunException, IOException, IllegalArgumentException + public TransactionID sendRequest( Request request, + TransportAddress sendTo, + TransportAddress sendThrough, + ResponseCollector collector ) + throws IOException, IllegalArgumentException { StunClientTransaction clientTransaction = new StunClientTransaction(this, request, sendTo, sendThrough, @@ -215,6 +215,38 @@ public class StunStack clientTransaction); clientTransaction.sendRequest(); + + return clientTransaction.getTransactionID(); + } + + /** + * Sends the specified request through the specified access point, and + * registers the specified ResponseCollector for later notification. + * @param request the request to send + * @param sendTo the destination address of the request. + * @param sendThrough the socket that we should send the request through. + * @param collector the instance to notify when a response arrives or the + * the transaction timeouts + * + * @return the TransactionID of the StunClientTransaction + * that we used in order to send the request. + * + * @throws IOException if an error occurs while sending message bytes + * through the network socket. + * @throws IllegalArgumentException if the apDescriptor references an + * access point that had not been installed, + */ + public TransactionID sendRequest( Request request, + TransportAddress sendTo, + DatagramSocket sendThrough, + ResponseCollector collector ) + throws IOException, IllegalArgumentException + { + TransportAddress sendThroughAddr = new TransportAddress( + sendThrough.getLocalAddress(), sendThrough.getLocalPort(), + Transport.UDP); + + return sendRequest(request, sendTo, sendThroughAddr, collector); } /** diff --git a/src/org/ice4j/stack/TransactionID.java b/src/org/ice4j/stack/TransactionID.java index a85e2a3..9a62915 100644 --- a/src/org/ice4j/stack/TransactionID.java +++ b/src/org/ice4j/stack/TransactionID.java @@ -37,19 +37,22 @@ class TransactionID */ private int hashCode = 0; + /** + * Limits access to TransactionID instantiation. + */ private TransactionID() { } /** - * Creates a transaction id object.The transaction id itself is genereated - * using the folloing algorithm: + * Creates a transaction id object.The transaction id itself is generated + * using the following algorithm: * - * The first 6 bytes of the id are given the value of System.currentTimeMillis() - * Putting the right most bits first so that we get a more optimized equals() - * method. + * The first 6 bytes of the id are given the value of + * System.currentTimeMillis(). Putting the right most bits first + * so that we get a more optimized equals() method. * - * @return A TransactionID object with a unique transaction id. + * @return A TransactionID object with a unique transaction id. */ static TransactionID createTransactionID() {