Moves connectivity check client and server to a package of their own. Adds mechanism that allows connectivity check servers to report peer reflexive addresses

This commit is contained in:
emcho 2010-04-28 22:30:42 +00:00
parent 21cd44045f
commit 045429921b
3 changed files with 397 additions and 0 deletions

View File

@ -0,0 +1,143 @@
/*
* 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.checks;
import java.net.*;
import java.util.logging.*;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.ice.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* @author Emil Ivov
*/
public class ConnectivityCheckClient
implements ResponseCollector
{
/**
* The <tt>Logger</tt> used by the <tt>ConnectivityCheckClient</tt>
* class and its instances for logging output.
*/
private static final Logger logger = Logger
.getLogger(ConnectivityCheckClient.class.getName());
/**
* The agent that created us.
*/
private final Agent parentAgent;
/**
* The stun stack that we will use for connectivity checks.
*/
StunStack stunStack = StunStack.getInstance();
/**
* Creates a new <tt>ConnectivityCheckHandler</tt> setting
* <tt>parentAgent</tt> as the agent that will be used for retrieving
* information such as user fragments for example.
*
* @param parentAgent the <tt>Agent</tt> that is creating this instance.
*/
public ConnectivityCheckClient(Agent parentAgent)
{
this.parentAgent = parentAgent;
}
public void startChecks(CheckList checkList)
{
for(CandidatePair pair : checkList)
{
startCheckForPair(pair);
}
}
private void startCheckForPair(CandidatePair pair)
{
//we don't need to do a canReach() verification here as it has been
//already verified during the gathering process.
DatagramSocket stunSocket = ((HostCandidate)pair.getLocalCandidate())
.getStunSocket(null);
Request request = MessageFactory.createBindingRequest();
//priority
PriorityAttribute priority = AttributeFactory.createPriorityAttribute(
pair.getLocalCandidate().computePriorityForType(
CandidateType.PEER_REFLEXIVE_CANDIDATE));
request.addAttribute(priority);
//controlling controlled
if (parentAgent.isControlling())
{
request.addAttribute(AttributeFactory
.createIceControllingAttribute(parentAgent
.getTieBreaker()));
}
else
{
request.addAttribute(AttributeFactory
.createIceControlledAttribute(parentAgent
.getTieBreaker()));
}
//credentials
String localUserName = parentAgent.generateLocalUserName();
UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute(
localUserName);
request.addAttribute(unameAttr);
//todo: do this in the stun stack so that we could do the
//calculation once the request is ready (we'd need the transaction id
//for example.
//todo: also implement SASL prepare
MessageIntegrityAttribute msgIntegrity = AttributeFactory
.createMessageIntegrityAttribute(localUserName);
request.addAttribute(msgIntegrity);
TransactionID tran;
try
{
tran = stunStack.sendRequest(request,
pair.getRemoteCandidate().getTransportAddress(), stunSocket,
this);
System.out.println("checking pair " + pair + " with tran=" + tran.toString());
}
catch (Exception exception)
{
logger.log( Level.INFO,
"Failed to send " + request + " through "
+ stunSocket.getLocalSocketAddress(),
exception);
return;
}
}
public void processResponse(StunMessageEvent response)
{
}
public void processTimeout(StunTimeoutEvent event)
{
System.out.println("timeout event=" + event);
}
public void processUnreachable(StunFailureEvent event)
{
System.out.println("failure event=" + event);
}
}

View File

@ -0,0 +1,215 @@
/*
* 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.checks;
import java.util.logging.*;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.ice.*;
import org.ice4j.message.*;
import org.ice4j.security.*;
import org.ice4j.stack.*;
/**
* @author Emil Ivov
*/
public class ConnectivityCheckServer
implements RequestListener,
CredentialsAuthority
{
/**
* The <tt>Logger</tt> used by the <tt>ConnectivityCheckServer</tt>
* class and its instances for logging output.
*/
private static final Logger logger = Logger
.getLogger(ConnectivityCheckServer.class.getName());
/**
* The agent that created us.
*/
private final Agent parentAgent;
/**
* The {@link RemoteAddressHandler} that we should notify when we notice
* new remote transport addresses.
*/
private final RemoteAddressHandler addressHandler;
/**
* The stun stack that we will use for connectivity checks.
*/
private StunStack stunStack = StunStack.getInstance();
/**
* Creates a new <tt>ConnectivityCheckServer</tt> setting
* <tt>parentAgent</tt> as the agent that will be used for retrieving
* information such as user fragments for example.
*
* @param parentAgent the <tt>Agent</tt> that is creating this instance.
* @param addressHandler the handler that
*/
public ConnectivityCheckServer(Agent parentAgent,
RemoteAddressHandler addressHandler)
{
this.parentAgent = parentAgent;
this.addressHandler = addressHandler;
stunStack.addRequestListener(this);
stunStack.getCredentialsManager().registerAuthority(this);
}
public void requestReceived(StunMessageEvent evt)
{
Request request = (Request)evt.getMessage();
//ignore incoming requests that are not meant for the local user.
//normally the stack will get rid of faulty user names but we could
//still see messages not meant for this server if both peers or running
//on this same instance of the stack.
UsernameAttribute uname = (UsernameAttribute)request
.getAttribute(Attribute.USERNAME);
if(uname == null
|| !checkLocalUserName(new String(uname.getUsername())))
{
return;
}
String username = new String(uname.getUsername());
int colon = username.indexOf(":");
//caller gave us the entire username.
String remoteUfrag = username.substring(0, colon);
//make sure we have a priority attribute and ignore otherwise.
PriorityAttribute priorityAttr
= (PriorityAttribute)request.getAttribute(Attribute.PRIORITY);
//extract priority
if(priorityAttr == null)
{
if(logger.isLoggable(Level.FINE))
{
logger.log(Level.FINE, "Received a connectivity ckeck with"
+"no PRIORITY attribute. Discarting.");
}
return;
}
long priority = priorityAttr.getPriority();
//tell our address handler we saw a new remote address;
addressHandler.handleRemoteAddress(evt.getRemoteAddress(),
evt.getLocalAddress(),
priority, remoteUfrag);
Response response = MessageFactory.createBindingResponse(
request, evt.getRemoteAddress());
try
{
stunStack.sendResponse(evt.getTransactionID().getTransactionID(),
response, evt.getLocalAddress(), evt.getRemoteAddress());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Verifies whether <tt>username</tt> is currently known to this server
* and returns <tt>true</tt> if so. Returns <tt>false</tt> otherwise.
*
* @param username the user name whose validity we'd like to check.
*
* @return <tt>true</tt> if <tt>username</tt> is known to this
* <tt>ConnectivityCheckServer</tt> and <tt>false</tt> otherwise.
*/
public boolean checkLocalUserName(String username)
{
boolean accept = false;
int colon = username.indexOf(":");
if( colon < 0)
{
//caller gave us a ufrag
if(username.equals(parentAgent.getLocalUfrag()))
accept = true;
}
else
{
//caller gave us the entire username.
if(username.substring(0, colon).equals(parentAgent.getLocalUfrag()))
accept = true;
}
return accept;
}
/**
* Implements the {@link CredentialsAuthority#getLocalKey(String)} method in a
* way that would return this handler's parent agent password if
* <tt>username</tt> is either the local ufrag or the username that the
* agent's remote peer was expected to use.
*
* @param username the local ufrag that we should return a password for.
*
* @return this handler's parent agent local password if <tt>username</tt>
* equals the local ufrag and <tt>null</tt> otherwise.
*/
public byte[] getLocalKey(String username)
{
//support both the case where username is the local fragment or the
//entire user name.
int colon = username.indexOf(":");
if( colon < 0)
{
//caller gave us a ufrag
if (username.equals(parentAgent.getLocalUfrag()))
return parentAgent.getLocalPassword().getBytes();
}
else
{
//caller gave us the entire username.
if(username.substring(0, colon).equals(parentAgent.getLocalUfrag()))
return parentAgent.getLocalPassword().getBytes();
}
return null;
}
/**
* Implements the {@link CredentialsAuthority#getRemoteKey(String)} method
* in a way that would return this handler's parent agent remote password if
* <tt>username</tt> is either the remote ufrag or the username that we
* are expected to use when querying the remote peer.
*
* @param username the remote ufrag that we should return a password for.
*
* @return this handler's parent agent remote password if <tt>username</tt>
* equals the remote ufrag and <tt>null</tt> otherwise.
*/
public byte[] getRemoteKey(String username)
{
//support both the case where username is the local fragment or the
//entire user name.
int colon = username.indexOf(":");
if( colon < 0)
{
//caller gave us a ufrag
if (username.equals(parentAgent.getRemoteUfrag()))
return parentAgent.getRemotePassword().getBytes();
}
else
{
//caller gave us the entire username.
if (username.equals(parentAgent.generateLocalUserName()))
return parentAgent.getRemotePassword().getBytes();
}
return null;
}
}

View File

@ -0,0 +1,39 @@
/*
* 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.checks;
import org.ice4j.*;
/**
* This interface is used by {@link ConnectivityCheckServer}s to deliver remote
* addresses that they see while handling incoming connectivity checks.
* Implementations of this interface should determine whether these addresses
* were (or will be) advertised by the remote peer and, if not, consider them
* as new, peer reflexive candidates.
*
* @author Emil Ivov
*/
public interface RemoteAddressHandler
{
/**
* Notifies the implementation that the {@link ConnectivityCheckServer} has
* just received a message on <tt>localAddress</tt> originating at
* <tt>remoteAddress</tt> carrying the specified <tt>priority</tt>.
*
* @param remoteAddress the address that we've just seen, and that is
* potentially a peer-reflexive address.
* @param localAddress the address that we were contacted on.
* @param priority the priority that the remote party assigned to
* @param remoteUFrag the user fragment that we should be using when and if
* we decide to send a check to <tt>remoteAddress</tt>.
*
*/
public void handleRemoteAddress(TransportAddress remoteAddress,
TransportAddress localAddress,
long priority,
String remoteUFrag);
}