diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java new file mode 100644 index 0000000..467380d --- /dev/null +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -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 Logger used by the ConnectivityCheckClient + * 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 ConnectivityCheckHandler setting + * parentAgent as the agent that will be used for retrieving + * information such as user fragments for example. + * + * @param parentAgent the Agent 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); + } + + +} diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckServer.java b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java new file mode 100644 index 0000000..41c9c77 --- /dev/null +++ b/src/org/ice4j/ice/checks/ConnectivityCheckServer.java @@ -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 Logger used by the ConnectivityCheckServer + * 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 ConnectivityCheckServer setting + * parentAgent as the agent that will be used for retrieving + * information such as user fragments for example. + * + * @param parentAgent the Agent 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 username is currently known to this server + * and returns true if so. Returns false otherwise. + * + * @param username the user name whose validity we'd like to check. + * + * @return true if username is known to this + * ConnectivityCheckServer and false 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 + * username 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 username + * equals the local ufrag and null 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 + * username 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 username + * equals the remote ufrag and null 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; + } +} diff --git a/src/org/ice4j/ice/checks/RemoteAddressHandler.java b/src/org/ice4j/ice/checks/RemoteAddressHandler.java new file mode 100644 index 0000000..b394d23 --- /dev/null +++ b/src/org/ice4j/ice/checks/RemoteAddressHandler.java @@ -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 localAddress originating at + * remoteAddress carrying the specified priority. + * + * @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 remoteAddress. + * + */ + public void handleRemoteAddress(TransportAddress remoteAddress, + TransportAddress localAddress, + long priority, + String remoteUFrag); +}