diff --git a/build.xml b/build.xml
index 04e848a..a9b0b5b 100644
--- a/build.xml
+++ b/build.xml
@@ -14,6 +14,7 @@
+
diff --git a/lib/weupnp-0.1.2-SNAPSHOT.jar b/lib/weupnp-0.1.2-SNAPSHOT.jar
new file mode 100644
index 0000000..ba5de0d
Binary files /dev/null and b/lib/weupnp-0.1.2-SNAPSHOT.jar differ
diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java
index 228af80..81604a2 100644
--- a/src/org/ice4j/ice/Agent.java
+++ b/src/org/ice4j/ice/Agent.java
@@ -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(
diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java
index 2b076bc..e3119ea 100644
--- a/src/org/ice4j/ice/ConnectivityCheckClient.java
+++ b/src/org/ice4j/ice/ConnectivityCheckClient.java
@@ -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());
}
diff --git a/src/org/ice4j/ice/UPNPCandidate.java b/src/org/ice4j/ice/UPNPCandidate.java
new file mode 100644
index 0000000..9ce429f
--- /dev/null
+++ b/src/org/ice4j/ice/UPNPCandidate.java
@@ -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 Candidate obtained via UPnP.
+ *
+ * @author Sebastien Vincent
+ */
+public class UPNPCandidate
+ extends LocalCandidate
+{
+ /**
+ * The UPnP gateway device.
+ */
+ private GatewayDevice device = null;
+
+ /**
+ * Creates a UPNPCandidate 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 Component 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
+ * DatagramSocket, for example. The socket of this
+ * LocalCandidate is closed only if it is not the socket
+ * of the base of this LocalCandidate.
+ */
+ @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 DatagramSocket associated with this Candidate.
+ *
+ * @return the DatagramSocket associated with this
+ * Candidate
+ */
+ @Override
+ public DatagramSocket getSocket()
+ {
+ return getBase().getSocket();
+ }
+}
diff --git a/src/org/ice4j/ice/harvest/UPNPHarvester.java b/src/org/ice4j/ice/harvest/UPNPHarvester.java
new file mode 100644
index 0000000..908c517
--- /dev/null
+++ b/src/org/ice4j/ice/harvest/UPNPHarvester.java
@@ -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 CandidateHarvester which gathers Candidates
+ * 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 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
+ * UPnP Candidates for
+ * @return the LocalCandidates gathered by this
+ * CandidateHarvester
+ */
+ public synchronized Collection harvest(Component component)
+ {
+ Collection candidates = new HashSet();
+ 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 UPNPCandidate instance which
+ * represents the specified TransportAddress
+ * @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;
+ }
+}