Add UPnP harvester support.

This commit is contained in:
seb 2010-12-21 10:29:04 +00:00
parent 71c4c28298
commit ad79151b1f
6 changed files with 261 additions and 1 deletions

View File

@ -14,6 +14,7 @@
<pathelement location="${dest}"/>
<pathelement location="${JUnit.home}/junit.jar"/>
<pathelement location="${lib}/jain-sdp.jar"/>
<pathelement location="${lib}/weupnp-0.1.2-SNAPSHOT.jar"/>
</path>
<!--Patternset to exclude files from the output directory:-->

Binary file not shown.

View File

@ -1007,6 +1007,14 @@ public class Agent
boolean useCandidate)
{
LocalCandidate localCandidate = findLocalCandidate(localAddress);
if(localCandidate == null)
{
logger.info("No localAddress for this incoming checks: " +
localAddress);
return;
}
Component parentComponent = localCandidate.getParentComponent();
RemoteCandidate remoteCandidate = new RemoteCandidate(

View File

@ -386,7 +386,7 @@ class ConnectivityCheckClient
//generated from it momentarily
validLocalCandidate = peerReflexiveCandidate;
logger.fine("Receive a peer-reflexive candidate: " +
logger.info("Receive a peer-reflexive candidate: " +
peerReflexiveCandidate.getTransportAddress());
}

View File

@ -0,0 +1,90 @@
/*
* 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;
import java.net.*;
import org.ice4j.*;
import org.bitlet.weupnp.*;
/**
* Represents a <tt>Candidate</tt> obtained via UPnP.
*
* @author Sebastien Vincent
*/
public class UPNPCandidate
extends LocalCandidate
{
/**
* The UPnP gateway device.
*/
private GatewayDevice device = null;
/**
* Creates a <tt>UPNPCandidate</tt> for the specified transport, address,
* and base.
*
* @param transportAddress the transport address that this candidate is
* encapsulating.
* @param base the base candidate
* @param parentComponent the <tt>Component</tt> that this candidate
* belongs to.
* @param device the UPnP gateway device
*/
public UPNPCandidate(TransportAddress transportAddress,
LocalCandidate base, Component parentComponent,
GatewayDevice device)
{
super(transportAddress, parentComponent,
CandidateType.SERVER_REFLEXIVE_CANDIDATE);
this.setBase(base);
this.device = device;
setStunServerAddress(transportAddress);
}
/**
* Frees resources allocated by this candidate such as its
* <tt>DatagramSocket</tt>, for example. The <tt>socket</tt> of this
* <tt>LocalCandidate</tt> is closed only if it is not the <tt>socket</tt>
* of the <tt>base</tt> of this <tt>LocalCandidate</tt>.
*/
@Override
protected void free()
{
// delete the port mapping
try
{
device.deletePortMapping(getTransportAddress().getPort(), "UDP");
}
catch(Exception e)
{
}
DatagramSocket socket = getSocket();
if(socket != null)
{
socket.close();
}
device = null;
}
/**
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>.
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
*/
@Override
public DatagramSocket getSocket()
{
return getBase().getSocket();
}
}

View File

@ -0,0 +1,161 @@
/*
* 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.ice.*;
import org.ice4j.socket.*;
import org.bitlet.weupnp.*;
/**
* Implements a <tt>CandidateHarvester</tt> which gathers <tt>Candidate</tt>s
* for a specified {@link Component} using UPnP.
*
* @author Sebastien Vincent
*/
public class UPNPHarvester
implements CandidateHarvester
{
/**
* The logger.
*/
private static final Logger logger =
Logger.getLogger(UPNPHarvester.class.getName());
/**
* Maximum port to try to allocate.
*/
private static final int MAX_RETRIES = 5;
/**
* The UPnP discover.
*/
private GatewayDiscover discover = new GatewayDiscover();
/**
* Constructor.
*/
public UPNPHarvester()
{
try
{
discover.discover();
}
catch(Exception e)
{
logger.info("UPnP discovery failed: " + e);
}
}
/**
* Gathers UPnP 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
* UPnP <tt>Candidate</tt>s for
* @return the <tt>LocalCandidate</tt>s gathered by this
* <tt>CandidateHarvester</tt>
*/
public synchronized Collection<LocalCandidate> harvest(Component component)
{
Collection<LocalCandidate> candidates = new HashSet<LocalCandidate>();
int retries = 0;
try
{
GatewayDevice device = discover.getValidGateway();
if(device == null)
{
return candidates;
}
InetAddress localAddress = device.getLocalAddress();
String externalIPAddress = device.getExternalIPAddress();
PortMappingEntry portMapping = new PortMappingEntry();
MultiplexingDatagramSocket socket =
new MultiplexingDatagramSocket(0, localAddress);
int port = socket.getLocalPort();
int externalPort = socket.getLocalPort();
while(retries < MAX_RETRIES)
{
if(!device.getSpecificPortMappingEntry(port, "UDP",
portMapping))
{
if(device.addPortMapping(
externalPort,
port,
localAddress.getHostAddress(),
"UDP",
"Ice4J: " + port))
{
LocalCandidate cand = createUPNPCandidate(socket,
localAddress, externalIPAddress, externalPort,
component, device);
component.addLocalCandidate(cand);
candidates.add(cand);
break;
}
else
{
port++;
}
}
else
{
port++;
}
}
}
catch(Throwable e)
{
logger.info("Exception while gathering UPnP candidates: " + e);
}
return candidates;
}
/**
* Create a UPnP candidate.
*
* @param socket local socket
* @param localAddr local host address
* @param externalIP external IP address
* @param port local port
* @param cmp parent component
* @param device the UPnP gateway device
* @return a new <tt>UPNPCandidate</tt> instance which
* represents the specified <tt>TransportAddress</tt>
* @throws Exception if something goes wrong during candidate creation
*/
public UPNPCandidate createUPNPCandidate(DatagramSocket socket,
InetAddress localAddr, String externalIP, int port, Component cmp,
GatewayDevice device)
throws Exception
{
TransportAddress addr = new TransportAddress(externalIP, port,
Transport.UDP);
HostCandidate base = new HostCandidate(socket, cmp);
UPNPCandidate candidate = new UPNPCandidate(addr, base, cmp, device);
DatagramSocket stunSocket = candidate.getStunSocket(null);
candidate.getStunStack().addSocket(stunSocket);
return candidate;
}
}