upnp first cut

This commit is contained in:
zzz
2009-02-19 21:42:59 +00:00
parent fd32d77976
commit 312e6071d7
123 changed files with 17920 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
/* This code is part of Freenet. It is distributed under the GNU General
* Public License, version 2 (or at your option any later version). See
* http://www.gnu.org/ for further details of the GPL. */
package org.freenetproject;
import java.net.InetAddress;
/**
* Class returned by a FredPluginIPDetector.
*
* Indicates:
* - Whether there is no UDP connectivity at all.
* - Whether there is full inbound IP connectivity.
* - A list of detected public IPs.
*/
public class DetectedIP {
public final InetAddress publicAddress;
public final short natType;
/** The MTU as advertized by the JVM */
public int mtu;
// Constants
/** The plugin does not support detecting the NAT type. */
public static final short NOT_SUPPORTED = 1;
/** Full internet access! */
public static final short FULL_INTERNET = 2;
/** Full cone NAT. Once we have sent a packet out on a port, any node anywhere can send us
* a packet on that port. The nicest option, but very rare unfortunately. */
public static final short FULL_CONE_NAT = 3;
/** Restricted cone NAT. Once we have sent a packet out to a specific IP, it can send us
* packets on the port we just used. */
public static final short RESTRICTED_CONE_NAT = 4;
/** Port restricted cone NAT. Once we have sent a packet to a specific IP+Port, that IP+Port
* can send us packets on the port we just used. */
public static final short PORT_RESTRICTED_NAT = 5;
/** Symmetric NAT. Uses a separate port number for each IP+port ! Not much hope for symmetric
* to symmetric... */
public static final short SYMMETRIC_NAT = 6;
/** Symmetric UDP firewall. We are not NATed, but the firewall behaves as if we were. */
public static final short SYMMETRIC_UDP_FIREWALL = 7;
/** No UDP connectivity at all */
public static final short NO_UDP = 8;
public DetectedIP(InetAddress addr, short type) {
this.publicAddress = addr;
this.natType = type;
this.mtu = 1500;
}
@Override
public boolean equals(Object o) {
if(!(o instanceof DetectedIP)) {
return false;
}
DetectedIP d = (DetectedIP)o;
return ((d.natType == natType) && d.publicAddress.equals(publicAddress));
}
@Override
public int hashCode() {
return publicAddress.hashCode() ^ natType;
}
@Override
public String toString() {
return publicAddress.toString()+":"+natType+":"+mtu;
}
}

View File

@@ -0,0 +1,45 @@
package org.freenetproject;
/**
* A public Internet Protocol port on the node which needs to be forwarded if the
* node is NATed.
* @author toad
*/
public class ForwardPort {
/** Name of the interface e.g. "opennet" */
public final String name;
/** IPv4 vs IPv6? */
public final boolean isIP6;
/** Protocol number. See constants. */
public final int protocol;
public static final int PROTOCOL_UDP_IPV4 = 17;
public static final int PROTOCOL_TCP_IPV4 = 6;
/** Port number to forward */
public final int portNumber;
// We don't currently support binding to a specific internal interface.
// It would be complicated: Different interfaces may be on different LANs,
// and an IGD is normally on only one LAN.
private final int hashCode;
public ForwardPort(String name, boolean isIP6, int protocol, int portNumber) {
this.name = name;
this.isIP6 = isIP6;
this.protocol = protocol;
this.portNumber = portNumber;
hashCode = name.hashCode() | (isIP6 ? 1 : 0) | protocol | portNumber;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object o) {
if(o == this) return true;
if(!(o instanceof ForwardPort)) return false;
ForwardPort f = (ForwardPort) o;
return (f.name.equals(name)) && f.isIP6 == isIP6 && f.protocol == protocol && f.portNumber == portNumber;
}
}

View File

@@ -0,0 +1,14 @@
package org.freenetproject;
import java.util.Map;
/**
* Callback called by port forwarding plugins to indicate success or failure.
* @author toad
*/
public interface ForwardPortCallback {
/** Called to indicate status on one or more forwarded ports. */
public void portForwardStatus(Map<ForwardPort,ForwardPortStatus> statuses);
}

View File

@@ -0,0 +1,33 @@
package org.freenetproject;
public class ForwardPortStatus {
public final int status;
/** The port forward definitely succeeded. */
public static final int DEFINITE_SUCCESS = 3;
/** The port forward probably succeeded. I.e. it succeeded unless there was
* for example hostile action on the part of the router. */
public static final int PROBABLE_SUCCESS = 2;
/** The port forward may have succeeded. Or it may not have. We should
* definitely try to check out of band. See UP&P: Many routers say they've
* forwarded the port when they haven't. */
public static final int MAYBE_SUCCESS = 1;
/** The port forward is in progress */
public static final int IN_PROGRESS = 0;
/** The port forward probably failed */
public static final int PROBABLE_FAILURE = -1;
/** The port forward definitely failed. */
public static final int DEFINITE_FAILURE = -2;
public final String reasonString;
/** Some plugins may need to change the external port. They can return it
* to the node here. */
public final int externalPort;
public ForwardPortStatus(int status, String reason, int externalPort) {
this.status = status;
this.reasonString = reason;
this.externalPort = externalPort;
}
}