diff --git a/src/org/ice4j/ice/harvest/AbstractStunCandidateHarvester.java b/src/org/ice4j/ice/harvest/AbstractStunCandidateHarvester.java deleted file mode 100644 index 3cde7fc..0000000 --- a/src/org/ice4j/ice/harvest/AbstractStunCandidateHarvester.java +++ /dev/null @@ -1,376 +0,0 @@ -/* - * 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.ice.harvest; - -import java.net.*; -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.socket.*; -import org.ice4j.stack.*; - -/** - * Implements a CandidateHarvester which gathers Candidates - * for a specified {@link Component} using STUN and/or its extensions. - * - * @author Emil Ivov - * @author Lubomir Marinov - */ -public abstract class AbstractStunCandidateHarvester - implements CandidateHarvester, - ResponseCollector -{ - - /** - * The Logger used by the AbstractStunCandidateHarvester - * class and its instances for logging output. - */ - private static final Logger logger - = Logger.getLogger(AbstractStunCandidateHarvester.class.getName()); - - /** - * The list of candidates that we are currently resolving. - */ - private final Map resolveMap - = new Hashtable(); - - /** - * The address of the STUN server that we will be sending our requests to. - */ - protected final TransportAddress stunServer; - - /** - * The stack to use for STUN communication. - */ - protected final StunStack stunStack = StunStack.getInstance(); - - /** - * 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 AbstractStunCandidateHarvester(TransportAddress stunServer) - { - this.stunServer = stunServer; - - //these should be configurable. - System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "400"); - System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); - } - - /** - * Creates a new Candidate determined by a specific STUN response - * and with a specific HostCandidate base. - * - * @param responseEvent a StunMessageEvent which represents the - * STUN response which has been received - * @param localCand the HostCandidate for which harvesting is - * executing, the STUN response is associated with and which is to be the - * base of the newly created Candidate - */ - protected void createCandidate( - StunMessageEvent responseEvent, - HostCandidate localCand) - { - createServerReflexiveCandidate(responseEvent.getMessage(), localCand); - } - - /** - * Creates a new Request which is to be sent to {@link #stunServer} - * in order to start resolving a specific HostCandidate. - * - * @param hostCand the HostCandidate for which a Request - * to start resolving is to be created - * @return a new Request which is to be sent to {@link #stunServer} - * in order to start resolving the specified HostCandidate - */ - protected abstract Request createRequestToStartResolvingCandidate( - HostCandidate hostCand); - - /** - * 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. - */ - protected void createServerReflexiveCandidate(Message response, - HostCandidate base) - { - Attribute attribute - = response.getAttribute(Attribute.XOR_MAPPED_ADDRESS); - - if(attribute instanceof XorMappedAddressAttribute) - { - TransportAddress addr - = ((XorMappedAddressAttribute) attribute) - .getAddress(response.getTransactionID()); - ServerReflexiveCandidate srvrRflxCand - = createServerReflexiveCandidate(addr, base); - - if (srvrRflxCand != null) - base.getParentComponent().addLocalCandidate(srvrRflxCand); - } - } - - /** - * Creates a new ServerReflexiveCandidate instance which is to - * represent a specific TransportAddress harvested through a - * specific HostCandidate and the STUN server associated with this - * instance. - * - * @param transportAddress the TransportAddress to be represented - * by the new ServerReflexiveCandidate instance - * @param base the HostCandidate through which the specified - * TransportAddress has been harvested - * @return a new ServerReflexiveCandidate instance which represents - * the specified TransportAddress harvested through the specified - * HostCandidate and the STUN server associated with this instance - */ - protected ServerReflexiveCandidate createServerReflexiveCandidate( - TransportAddress transportAddress, - HostCandidate base) - { - return new ServerReflexiveCandidate(transportAddress, base, stunServer); - } - - /** - * Gets the DatagramPacketFilter which is to be associated with the - * DatagramSocket to be used for communication with - * {@link #stunServer} when gathering Candidates for a specific - * HostCandidate. - * - * @param hostCand the HostCandidate for which STUN - * Candidates are to be harvested by this CandidateHarvester - * @return the DatagramPacketFilter which is to be associated with - * the DatagramSocket to be used for communication with - * {@link #stunServer} when gathering Candidates for the specified - * HostCandidate - */ - protected abstract DatagramPacketFilter getStunDatagramPacketFilter( - HostCandidate hostCand); - - /** - * Gets the DatagramSocket to be used by this - * CandidateHarvester for STUN communication for the purposes of - * harvesting STUN Candidates for a specific - * HostCandidate. - * - * @param hostCand the HostCandidate for which STUN - * Candidates are to be harvested by this - * CandidateHarvester - * @return the DatagramSocket to be used by this - * CandidateHarvester for STUN communication for the purposes of - * harvesting STUN Candidates for the specified - * HostCandidate - */ - private DatagramSocket getStunSocket(HostCandidate hostCand) - { - DatagramSocket hostSocket = hostCand.getSocket(); - DatagramSocket stunSocket = null; - Throwable exception = null; - - if (hostSocket instanceof MultiplexingDatagramSocket) - try - { - stunSocket - = ((MultiplexingDatagramSocket) hostSocket) - .getSocket(getStunDatagramPacketFilter(hostCand)); - } - catch (SocketException sex) - { - logger.log( - Level.SEVERE, - "Failed to acquire DatagramSocket" - + " specific to STUN communication.", - sex); - exception = sex; - } - if (stunSocket == null) - throw new IllegalArgumentException("hostCand", exception); - else - return stunSocket; - } - - /** - * Gathers STUN candidates for all host Candidates that are already - * 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) - { - for (Candidate cand : component.getLocalCandidates()) - if (cand instanceof HostCandidate) - { - HostCandidate hostCand = (HostCandidate) cand; - - startResolvingCandidate(hostCand); - } - - waitForResolutionEnd(); - } - - /** - * 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); - } - } - - /** - * Matches the response to one of the local candidates and creates a - * ServerReflexiveCandidate using it as a base. - * - * @param response the response that we've just received from a StunServer. - */ - public void processResponse(StunMessageEvent response) - { - synchronized (resolveMap) - { - TransactionID tranID = response.getTransactionID(); - HostCandidate localCand = resolveMap.remove(tranID); - - if (localCand != null) - createCandidate(response, 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(); - - logger.finest("received a message tranid=" + tranID); - logger.finest("localCand=" + localCand); - } - } - - /** - * 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(StunTimeoutEvent event) - { - processFailure(event.getTransactionID()); - } - - /** - * 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 event the StunFailureEvent containing the - * PortUnreachableException which signaled that the destination of - * the request was found to be unreachable. - */ - public void processUnreachable(StunFailureEvent event) - { - processFailure(event.getTransactionID()); - } - - /** - * 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) - { - //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 socket = getStunSocket(hostCand); - - stunStack.addSocket(socket); - - synchronized(resolveMap) - { - Request request = createRequestToStartResolvingCandidate(hostCand); - TransactionID tran; - - try - { - tran = stunStack.sendRequest(request, stunServer, socket, this); - } - catch (Exception exception) - { - logger.log( - Level.INFO, - "Failed to send " - + request - + " through " + socket.getLocalSocketAddress(), - exception); - return; - } - - resolveMap.put(tran, hostCand); - } - } - - /** - * Blocks the current thread until all resolutions in this harvester - * have terminated one way or another. - */ - private void waitForResolutionEnd() - { - synchronized(resolveMap) - { - boolean interrupted = false; - - // Handle spurious wakeups. - while (!resolveMap.isEmpty()) - try - { - resolveMap.wait(); - } - catch (InterruptedException iex) - { - interrupted = true; - } - // Restore the interrupted status. - if (interrupted) - Thread.currentThread().interrupt(); - } - } -} diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index bb14710..840f3d4 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -12,9 +12,11 @@ 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.socket.*; +import org.ice4j.stack.*; /** * Implements a CandidateHarvester which gathers Candidates @@ -25,22 +27,15 @@ import org.ice4j.socket.*; * @author Lubomir Marinov */ public class StunCandidateHarvester - extends AbstractStunCandidateHarvester + implements CandidateHarvester, + ResponseCollector { /** * The Logger used by the StunCandidateHarvester * class and its instances for logging output. */ private static final Logger logger - = Logger.getLogger(AbstractStunCandidateHarvester.class.getName()); - - /** - * The DatagramPacketFilter which accepts only STUN messages - * defined in RFC 5389 "Session Traversal Utilities for NAT (STUN)" i.e. the - * STUN messages of interest to StunCandidateHarvester. - */ - private static final StunDatagramPacketFilter stunDatagramPacketFilter - = new StunDatagramPacketFilter(); + = Logger.getLogger(StunCandidateHarvester.class.getName()); /** * The list of candidates that this harvester has allocated and that it @@ -49,6 +44,28 @@ public class StunCandidateHarvester private List allocatedCandidates = new LinkedList(); + /** + * The list of candidates that we are currently resolving. + */ + private final Map resolveMap + = new Hashtable(); + + /** + * The DatagramPacketFilter which accepts only STUN messages and + * which are part of the communication with {@link #stunServer}. + */ + private DatagramPacketFilter stunDatagramPacketFilter; + + /** + * The address of the STUN server that we will be sending our requests to. + */ + protected final TransportAddress stunServer; + + /** + * The stack to use for STUN communication. + */ + protected final StunStack stunStack = StunStack.getInstance(); + /** * Creates a new STUN harvester that will be running against the specified * stunServer. @@ -58,7 +75,28 @@ public class StunCandidateHarvester */ public StunCandidateHarvester(TransportAddress stunServer) { - super(stunServer); + this.stunServer = stunServer; + + //these should be configurable. + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "400"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); + } + + /** + * Creates a new Candidate determined by a specific STUN response + * and with a specific HostCandidate base. + * + * @param responseEvent a StunMessageEvent which represents the + * STUN response which has been received + * @param localCand the HostCandidate for which harvesting is + * executing, the STUN response is associated with and which is to be the + * base of the newly created Candidate + */ + protected void createCandidate( + StunMessageEvent responseEvent, + HostCandidate localCand) + { + createServerReflexiveCandidate(responseEvent.getMessage(), localCand); } /** @@ -69,8 +107,6 @@ public class StunCandidateHarvester * to start resolving is to be created * @return a new Request which is to be sent to {@link #stunServer} * in order to start resolving the specified HostCandidate - * @see AbstractStunCandidateHarvester - * #createRequestToStartResolvingCandidate(HostCandidate) */ protected Request createRequestToStartResolvingCandidate( HostCandidate hostCand) @@ -78,6 +114,74 @@ public class StunCandidateHarvester return MessageFactory.createBindingRequest(); } + /** + * 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. + */ + protected void createServerReflexiveCandidate(Message response, + HostCandidate base) + { + Attribute attribute + = response.getAttribute(Attribute.XOR_MAPPED_ADDRESS); + + if(attribute instanceof XorMappedAddressAttribute) + { + TransportAddress addr + = ((XorMappedAddressAttribute) attribute) + .getAddress(response.getTransactionID()); + ServerReflexiveCandidate srvrRflxCand + = createServerReflexiveCandidate(addr, base); + + if (srvrRflxCand != null) + base.getParentComponent().addLocalCandidate(srvrRflxCand); + } + } + + /** + * Creates a new ServerReflexiveCandidate instance which is to + * represent a specific TransportAddress harvested through a + * specific HostCandidate and the STUN server associated with this + * instance. + * + * @param transportAddress the TransportAddress to be represented + * by the new ServerReflexiveCandidate instance + * @param base the HostCandidate through which the specified + * TransportAddress has been harvested + * @return a new ServerReflexiveCandidate instance which represents + * the specified TransportAddress harvested through the specified + * HostCandidate and the STUN server associated with this instance + */ + protected ServerReflexiveCandidate createServerReflexiveCandidate( + TransportAddress transportAddress, + HostCandidate base) + { + return new ServerReflexiveCandidate(transportAddress, base, stunServer); + } + + /** + * Creates the DatagramPacketFilter which is to be associated with + * the DatagramSocket to be used for communication with + * {@link #stunServer} when gathering Candidates for a specific + * HostCandidate. + * + * @return the DatagramPacketFilter which is to be associated with + * the DatagramSocket to be used for communication with + * {@link #stunServer} when gathering Candidates for a specific + * HostCandidate + */ + protected DatagramPacketFilter createStunDatagramPacketFilter() + { + return new StunDatagramPacketFilter(stunServer); + } + /** * Gets the DatagramPacketFilter which is to be associated with the * DatagramSocket to be used for communication with @@ -90,17 +194,157 @@ public class StunCandidateHarvester * the DatagramSocket to be used for communication with * {@link #stunServer} when gathering Candidates for the specified * HostCandidate - * @see AbstractStunCandidateHarvester#getStunDatagramPacketFilter( - * HostCandidate) */ protected DatagramPacketFilter getStunDatagramPacketFilter( HostCandidate hostCand) { + if (stunDatagramPacketFilter == null) + stunDatagramPacketFilter = createStunDatagramPacketFilter(); return stunDatagramPacketFilter; } /** - * reSends a binding request to our stun server through the specified + * Gets the DatagramSocket to be used by this + * CandidateHarvester for STUN communication for the purposes of + * harvesting STUN Candidates for a specific + * HostCandidate. + * + * @param hostCand the HostCandidate for which STUN + * Candidates are to be harvested by this + * CandidateHarvester + * @return the DatagramSocket to be used by this + * CandidateHarvester for STUN communication for the purposes of + * harvesting STUN Candidates for the specified + * HostCandidate + */ + private DatagramSocket getStunSocket(HostCandidate hostCand) + { + DatagramSocket hostSocket = hostCand.getSocket(); + DatagramSocket stunSocket = null; + Throwable exception = null; + + if (hostSocket instanceof MultiplexingDatagramSocket) + try + { + stunSocket + = ((MultiplexingDatagramSocket) hostSocket) + .getSocket(getStunDatagramPacketFilter(hostCand)); + } + catch (SocketException sex) + { + logger.log( + Level.SEVERE, + "Failed to acquire DatagramSocket" + + " specific to STUN communication.", + sex); + exception = sex; + } + if (stunSocket == null) + throw new IllegalArgumentException("hostCand", exception); + else + return stunSocket; + } + + /** + * Gathers STUN candidates for all host Candidates that are already + * 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) + { + for (Candidate cand : component.getLocalCandidates()) + if (cand instanceof HostCandidate) + { + HostCandidate hostCand = (HostCandidate) cand; + + startResolvingCandidate(hostCand); + } + + waitForResolutionEnd(); + } + + /** + * 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); + } + } + + /** + * Matches the response to one of the local candidates and creates a + * ServerReflexiveCandidate using it as a base. + * + * @param response the response that we've just received from a StunServer. + */ + public void processResponse(StunMessageEvent response) + { + synchronized (resolveMap) + { + TransactionID tranID = response.getTransactionID(); + HostCandidate localCand = resolveMap.remove(tranID); + + if (localCand != null) + createCandidate(response, 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(); + + logger.finest("received a message tranid=" + tranID); + logger.finest("localCand=" + localCand); + } + } + + /** + * 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(StunTimeoutEvent event) + { + processFailure(event.getTransactionID()); + } + + /** + * 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 event the StunFailureEvent containing the + * PortUnreachableException which signaled that the destination of + * the request was found to be unreachable. + */ + public void processUnreachable(StunFailureEvent event) + { + processFailure(event.getTransactionID()); + } + + /** + * Resends a binding request to our stun server through the specified * srflxCand candidate so that it would keep the potential NAT * mapping valid. * @@ -124,4 +368,72 @@ public class StunCandidateHarvester exception); } } + + /** + * 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) + { + //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 socket = getStunSocket(hostCand); + + stunStack.addSocket(socket); + + synchronized(resolveMap) + { + Request request = createRequestToStartResolvingCandidate(hostCand); + TransactionID tran; + + try + { + tran = stunStack.sendRequest(request, stunServer, socket, this); + } + catch (Exception exception) + { + logger.log( + Level.INFO, + "Failed to send " + + request + + " through " + socket.getLocalSocketAddress(), + exception); + return; + } + + resolveMap.put(tran, hostCand); + } + } + + /** + * Blocks the current thread until all resolutions in this harvester + * have terminated one way or another. + */ + private void waitForResolutionEnd() + { + synchronized(resolveMap) + { + boolean interrupted = false; + + // Handle spurious wakeups. + while (!resolveMap.isEmpty()) + try + { + resolveMap.wait(); + } + catch (InterruptedException iex) + { + interrupted = true; + } + // Restore the interrupted status. + if (interrupted) + Thread.currentThread().interrupt(); + } + } } diff --git a/src/org/ice4j/socket/AbstractStunDatagramPacketFilter.java b/src/org/ice4j/socket/AbstractStunDatagramPacketFilter.java deleted file mode 100644 index 30b9403..0000000 --- a/src/org/ice4j/socket/AbstractStunDatagramPacketFilter.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.socket; - -import java.net.*; - -/** - * Implements a DatagramPacketFilter which only accepts - * DatagramPackets which represent STUN messages. - * - * @author Lubomir Marinov - */ -public abstract class AbstractStunDatagramPacketFilter - implements DatagramPacketFilter -{ - - /** - * Determines whether a specific DatagramPacket represents a STUN - * message. - * - * @param p the DatagramPacket which is to be checked whether it is - * a STUN message - * @return true if p represents a STUN message; otherwise, - * false - */ - public boolean accept(DatagramPacket p) - { - /* - * All STUN messages MUST start with a 20-byte header followed by zero - * or more Attributes. - */ - if (p.getLength() >= 20) - { - /* - * The most significant 2 bits of every STUN message MUST be zeroes. - * This can be used to differentiate STUN packets from other - * protocols when STUN is multiplexed with other protocols on the - * same port. - */ - byte[] data = p.getData(); - int offset = p.getOffset(); - byte b0 = data[offset]; - - if ((b0 & 0xC0) == 0) - { - byte b1 = data[offset + 1]; - char method = (char) ((b0 & 0xFE) | (b1 & 0xEF)); - - return acceptMethod(method); - } - } - return false; - } - - /** - * Determines whether this DatagramPacketFilter accepts a - * DatagramPacket which represents a STUN message with a specific - * STUN method. - * - * @param method the STUN method of a STUN message represented by a - * DatagramPacket to be checked whether it is accepted by this - * DatagramPacketFilter - * @return true if this DatagramPacketFilter accepts the - * DatagramPacket which represents a STUN message with the - * specified STUN method; otherwise, false - */ - protected abstract boolean acceptMethod(char method); -} diff --git a/src/org/ice4j/socket/StunDatagramPacketFilter.java b/src/org/ice4j/socket/StunDatagramPacketFilter.java index 0de7eeb..8aa0406 100644 --- a/src/org/ice4j/socket/StunDatagramPacketFilter.java +++ b/src/org/ice4j/socket/StunDatagramPacketFilter.java @@ -7,6 +7,9 @@ */ package org.ice4j.socket; +import java.net.*; + +import org.ice4j.*; import org.ice4j.message.*; /** @@ -18,9 +21,74 @@ import org.ice4j.message.*; * @author Lubomir Marinov */ public class StunDatagramPacketFilter - extends AbstractStunDatagramPacketFilter + implements DatagramPacketFilter { + /** + * The TransportAddress of the STUN server DatagramPackets + * representing STUN messages from and to which are accepted by this + * instance. + */ + protected final TransportAddress stunServer; + + /** + * Initializes a new StunDatagramPacketFilter which will accept + * DatagramPackets which represent STUN messages and which are part + * of the communication with a specific STUN server. + * + * @param turnServer the TransportAddress of the STUN server + * DatagramPackets representing STUN messages from and to which + * will be accepted by the new instance + */ + public StunDatagramPacketFilter(TransportAddress stunServer) + { + this.stunServer = stunServer; + } + + /** + * Determines whether a specific DatagramPacket represents a STUN + * message which is part of the communication with the STUN server + * associated with this instance. + * + * @param p the DatagramPacket which is to be checked whether it is + * a STUN message which is part of the communicator with the STUN server + * associated with this instance + * @return true if the specified DatagramPacket represents + * a STUN message which is part of the communication with the STUN server + * associated with this instance; otherwise, false + */ + public boolean accept(DatagramPacket p) + { + if (stunServer.equals(p.getSocketAddress())) + { + /* + * All STUN messages MUST start with a 20-byte header followed by + * zero or more Attributes. + */ + if (p.getLength() >= 20) + { + /* + * The most significant 2 bits of every STUN message MUST be + * zeroes. This can be used to differentiate STUN packets from + * other protocols when STUN is multiplexed with other protocols + * on the same port. + */ + byte[] data = p.getData(); + int offset = p.getOffset(); + byte b0 = data[offset]; + + if ((b0 & 0xC0) == 0) + { + byte b1 = data[offset + 1]; + char method = (char) ((b0 & 0xFE) | (b1 & 0xEF)); + + return acceptMethod(method); + } + } + } + return false; + } + /** * Determines whether this DatagramPacketFilter accepts a * DatagramPacket which represents a STUN message with a specific @@ -33,9 +101,8 @@ public class StunDatagramPacketFilter * @return true if this DatagramPacketFilter accepts the * DatagramPacket which represents a STUN message with the * specified STUN method; otherwise, false - * @see AbstractStunDatagramPacketFilter#acceptMethod(char) */ - public boolean acceptMethod(char method) + protected boolean acceptMethod(char method) { switch (method) {