Removes the abstract STUN CandidateHarvester and DatagramPacketFilter because the TURN implementation will extends the actual STUN implementation.

This commit is contained in:
lubomir.marinov 2010-03-01 01:34:31 +00:00
parent 273a579942
commit 41ed1c9e41
4 changed files with 398 additions and 468 deletions

View File

@ -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 <tt>CandidateHarvester</tt> which gathers <tt>Candidate</tt>s
* 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 <tt>Logger</tt> used by the <tt>AbstractStunCandidateHarvester</tt>
* 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<TransactionID, HostCandidate> resolveMap
= new Hashtable<TransactionID, HostCandidate>();
/**
* 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
* <tt>stunServer</tt>.
*
* @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 <tt>Candidate</tt> determined by a specific STUN response
* and with a specific <tt>HostCandidate</tt> base.
*
* @param responseEvent a <tt>StunMessageEvent</tt> which represents the
* STUN response which has been received
* @param localCand the <tt>HostCandidate</tt> for which harvesting is
* executing, the STUN response is associated with and which is to be the
* base of the newly created <tt>Candidate</tt>
*/
protected void createCandidate(
StunMessageEvent responseEvent,
HostCandidate localCand)
{
createServerReflexiveCandidate(responseEvent.getMessage(), localCand);
}
/**
* Creates a new <tt>Request</tt> which is to be sent to {@link #stunServer}
* in order to start resolving a specific <tt>HostCandidate</tt>.
*
* @param hostCand the <tt>HostCandidate</tt> for which a <tt>Request</tt>
* to start resolving is to be created
* @return a new <tt>Request</tt> which is to be sent to {@link #stunServer}
* in order to start resolving the specified <tt>HostCandidate</tt>
*/
protected abstract Request createRequestToStartResolvingCandidate(
HostCandidate hostCand);
/**
* Creates a <tt>ServerReflexiveCandidate</tt> using <tt>base</tt> as its
* and the <tt>XOR-MAPPED-ADDRESS</tt> attributes in <tt>response</tt> for
* the actual <tt>TransportAddress</tt> 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 <tt>Message</tt> that is supposed to contain the
* address we should use for this candidate.
* @param base the <tt>HostCandidate</tt> that we should use as a base
* for the new <tt>ServerReflexiveCandidate</tt>.
*/
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 <tt>ServerReflexiveCandidate</tt> instance which is to
* represent a specific <tt>TransportAddress</tt> harvested through a
* specific <tt>HostCandidate</tt> and the STUN server associated with this
* instance.
*
* @param transportAddress the <tt>TransportAddress</tt> to be represented
* by the new <tt>ServerReflexiveCandidate</tt> instance
* @param base the <tt>HostCandidate</tt> through which the specified
* <tt>TransportAddress</tt> has been harvested
* @return a new <tt>ServerReflexiveCandidate</tt> instance which represents
* the specified <tt>TransportAddress</tt> harvested through the specified
* <tt>HostCandidate</tt> and the STUN server associated with this instance
*/
protected ServerReflexiveCandidate createServerReflexiveCandidate(
TransportAddress transportAddress,
HostCandidate base)
{
return new ServerReflexiveCandidate(transportAddress, base, stunServer);
}
/**
* Gets the <tt>DatagramPacketFilter</tt> which is to be associated with the
* <tt>DatagramSocket</tt> to be used for communication with
* {@link #stunServer} when gathering <tt>Candidate</tt>s for a specific
* <tt>HostCandidate</tt>.
*
* @param hostCand the <tt>HostCandidate</tt> for which STUN
* <tt>Candidate</tt>s are to be harvested by this <tt>CandidateHarvester</tt>
* @return the <tt>DatagramPacketFilter</tt> which is to be associated with
* the <tt>DatagramSocket</tt> to be used for communication with
* {@link #stunServer} when gathering <tt>Candidate</tt>s for the specified
* <tt>HostCandidate</tt>
*/
protected abstract DatagramPacketFilter getStunDatagramPacketFilter(
HostCandidate hostCand);
/**
* Gets the <tt>DatagramSocket</tt> to be used by this
* <tt>CandidateHarvester</tt> for STUN communication for the purposes of
* harvesting STUN <tt>Candidate</tt>s for a specific
* <tt>HostCandidate</tt>.
*
* @param hostCand the <tt>HostCandidate</tt> for which STUN
* <tt>Candidate</tt>s are to be harvested by this
* <tt>CandidateHarvester</tt>
* @return the <tt>DatagramSocket</tt> to be used by this
* <tt>CandidateHarvester</tt> for STUN communication for the purposes of
* harvesting STUN <tt>Candidate</tt>s for the specified
* <tt>HostCandidate</tt>
*/
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 <tt>Candidate</tt>s that are already
* present in the specified <tt>component</tt>. This method relies on the
* specified <tt>component</tt> 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 <tt>Candidate</tt>s 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
* <tt>ServerReflexiveCandidate</tt> 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 <tt>StunTimeoutEvent</tt> 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
* <tt>PortUnreachableException</tt> ... 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 <tt>StunFailureEvent</tt> containing the
* <tt>PortUnreachableException</tt> 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
* <tt>hostCand</tt> candidate and adds it to the list of addresses still
* waiting for resolution.
*
* @param hostCand the <tt>HostCandidate</tt> 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();
}
}
}

View File

@ -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 <tt>CandidateHarvester</tt> which gathers <tt>Candidate</tt>s
@ -25,22 +27,15 @@ import org.ice4j.socket.*;
* @author Lubomir Marinov
*/
public class StunCandidateHarvester
extends AbstractStunCandidateHarvester
implements CandidateHarvester,
ResponseCollector
{
/**
* The <tt>Logger</tt> used by the <tt>StunCandidateHarvester</tt>
* class and its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(AbstractStunCandidateHarvester.class.getName());
/**
* The <tt>DatagramPacketFilter</tt> which accepts only STUN messages
* defined in RFC 5389 "Session Traversal Utilities for NAT (STUN)" i.e. the
* STUN messages of interest to <tt>StunCandidateHarvester</tt>.
*/
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<ServerReflexiveCandidate> allocatedCandidates
= new LinkedList<ServerReflexiveCandidate>();
/**
* The list of candidates that we are currently resolving.
*/
private final Map<TransactionID, HostCandidate> resolveMap
= new Hashtable<TransactionID, HostCandidate>();
/**
* The <tt>DatagramPacketFilter</tt> 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
* <tt>stunServer</tt>.
@ -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 <tt>Candidate</tt> determined by a specific STUN response
* and with a specific <tt>HostCandidate</tt> base.
*
* @param responseEvent a <tt>StunMessageEvent</tt> which represents the
* STUN response which has been received
* @param localCand the <tt>HostCandidate</tt> for which harvesting is
* executing, the STUN response is associated with and which is to be the
* base of the newly created <tt>Candidate</tt>
*/
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 <tt>Request</tt> which is to be sent to {@link #stunServer}
* in order to start resolving the specified <tt>HostCandidate</tt>
* @see AbstractStunCandidateHarvester
* #createRequestToStartResolvingCandidate(HostCandidate)
*/
protected Request createRequestToStartResolvingCandidate(
HostCandidate hostCand)
@ -78,6 +114,74 @@ public class StunCandidateHarvester
return MessageFactory.createBindingRequest();
}
/**
* Creates a <tt>ServerReflexiveCandidate</tt> using <tt>base</tt> as its
* and the <tt>XOR-MAPPED-ADDRESS</tt> attributes in <tt>response</tt> for
* the actual <tt>TransportAddress</tt> 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 <tt>Message</tt> that is supposed to contain the
* address we should use for this candidate.
* @param base the <tt>HostCandidate</tt> that we should use as a base
* for the new <tt>ServerReflexiveCandidate</tt>.
*/
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 <tt>ServerReflexiveCandidate</tt> instance which is to
* represent a specific <tt>TransportAddress</tt> harvested through a
* specific <tt>HostCandidate</tt> and the STUN server associated with this
* instance.
*
* @param transportAddress the <tt>TransportAddress</tt> to be represented
* by the new <tt>ServerReflexiveCandidate</tt> instance
* @param base the <tt>HostCandidate</tt> through which the specified
* <tt>TransportAddress</tt> has been harvested
* @return a new <tt>ServerReflexiveCandidate</tt> instance which represents
* the specified <tt>TransportAddress</tt> harvested through the specified
* <tt>HostCandidate</tt> and the STUN server associated with this instance
*/
protected ServerReflexiveCandidate createServerReflexiveCandidate(
TransportAddress transportAddress,
HostCandidate base)
{
return new ServerReflexiveCandidate(transportAddress, base, stunServer);
}
/**
* Creates the <tt>DatagramPacketFilter</tt> which is to be associated with
* the <tt>DatagramSocket</tt> to be used for communication with
* {@link #stunServer} when gathering <tt>Candidate</tt>s for a specific
* <tt>HostCandidate</tt>.
*
* @return the <tt>DatagramPacketFilter</tt> which is to be associated with
* the <tt>DatagramSocket</tt> to be used for communication with
* {@link #stunServer} when gathering <tt>Candidate</tt>s for a specific
* <tt>HostCandidate</tt>
*/
protected DatagramPacketFilter createStunDatagramPacketFilter()
{
return new StunDatagramPacketFilter(stunServer);
}
/**
* Gets the <tt>DatagramPacketFilter</tt> which is to be associated with the
* <tt>DatagramSocket</tt> to be used for communication with
@ -90,17 +194,157 @@ public class StunCandidateHarvester
* the <tt>DatagramSocket</tt> to be used for communication with
* {@link #stunServer} when gathering <tt>Candidate</tt>s for the specified
* <tt>HostCandidate</tt>
* @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 <tt>DatagramSocket</tt> to be used by this
* <tt>CandidateHarvester</tt> for STUN communication for the purposes of
* harvesting STUN <tt>Candidate</tt>s for a specific
* <tt>HostCandidate</tt>.
*
* @param hostCand the <tt>HostCandidate</tt> for which STUN
* <tt>Candidate</tt>s are to be harvested by this
* <tt>CandidateHarvester</tt>
* @return the <tt>DatagramSocket</tt> to be used by this
* <tt>CandidateHarvester</tt> for STUN communication for the purposes of
* harvesting STUN <tt>Candidate</tt>s for the specified
* <tt>HostCandidate</tt>
*/
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 <tt>Candidate</tt>s that are already
* present in the specified <tt>component</tt>. This method relies on the
* specified <tt>component</tt> 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 <tt>Candidate</tt>s 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
* <tt>ServerReflexiveCandidate</tt> 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 <tt>StunTimeoutEvent</tt> 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
* <tt>PortUnreachableException</tt> ... 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 <tt>StunFailureEvent</tt> containing the
* <tt>PortUnreachableException</tt> 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
* <tt>srflxCand</tt> 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
* <tt>hostCand</tt> candidate and adds it to the list of addresses still
* waiting for resolution.
*
* @param hostCand the <tt>HostCandidate</tt> 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();
}
}
}

View File

@ -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 <tt>DatagramPacketFilter</tt> which only accepts
* <tt>DatagramPacket</tt>s which represent STUN messages.
*
* @author Lubomir Marinov
*/
public abstract class AbstractStunDatagramPacketFilter
implements DatagramPacketFilter
{
/**
* Determines whether a specific <tt>DatagramPacket</tt> represents a STUN
* message.
*
* @param p the <tt>DatagramPacket</tt> which is to be checked whether it is
* a STUN message
* @return <tt>true</tt> if <tt>p</tt> represents a STUN message; otherwise,
* <tt>false</tt>
*/
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 <tt>DatagramPacketFilter</tt> accepts a
* <tt>DatagramPacket</tt> which represents a STUN message with a specific
* STUN method.
*
* @param method the STUN method of a STUN message represented by a
* <tt>DatagramPacket</tt> to be checked whether it is accepted by this
* <tt>DatagramPacketFilter</tt>
* @return <tt>true</tt> if this <tt>DatagramPacketFilter</tt> accepts the
* <tt>DatagramPacket</tt> which represents a STUN message with the
* specified STUN method; otherwise, <tt>false</tt>
*/
protected abstract boolean acceptMethod(char method);
}

View File

@ -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 <tt>TransportAddress</tt> of the STUN server <tt>DatagramPacket</tt>s
* representing STUN messages from and to which are accepted by this
* instance.
*/
protected final TransportAddress stunServer;
/**
* Initializes a new <tt>StunDatagramPacketFilter</tt> which will accept
* <tt>DatagramPacket</tt>s which represent STUN messages and which are part
* of the communication with a specific STUN server.
*
* @param turnServer the <tt>TransportAddress</tt> of the STUN server
* <tt>DatagramPacket</tt>s 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 <tt>DatagramPacket</tt> represents a STUN
* message which is part of the communication with the STUN server
* associated with this instance.
*
* @param p the <tt>DatagramPacket</tt> 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 <tt>true</tt> if the specified <tt>DatagramPacket</tt> represents
* a STUN message which is part of the communication with the STUN server
* associated with this instance; otherwise, <tt>false</tt>
*/
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 <tt>DatagramPacketFilter</tt> accepts a
* <tt>DatagramPacket</tt> which represents a STUN message with a specific
@ -33,9 +101,8 @@ public class StunDatagramPacketFilter
* @return <tt>true</tt> if this <tt>DatagramPacketFilter</tt> accepts the
* <tt>DatagramPacket</tt> which represents a STUN message with the
* specified STUN method; otherwise, <tt>false</tt>
* @see AbstractStunDatagramPacketFilter#acceptMethod(char)
*/
public boolean acceptMethod(char method)
protected boolean acceptMethod(char method)
{
switch (method)
{