net.tomp2p.connection
Class NATUtils

java.lang.Object
  extended by net.tomp2p.connection.NATUtils

public class NATUtils
extends Object

This class is used to do automatic port forwarding. It maps with PMP und UPNP and also unmaps them. It creates a shutdown hook in case the user exits the application without a proper shutdown.

Author:
Thomas Bocek

Constructor Summary
NATUtils()
          Constructor.
 
Method Summary
 boolean mapPMP(int internalPortUDP, int internalPortTCP, int externalPortUDP, int externalPortTCP)
          Maps with the PMP protocol (http://en.wikipedia.org/wiki/NAT_Port_Mapping_Protocol).
 boolean mapUPNP(String internalHost, int internalPortUDP, int internalPortTCP, int externalPortUDP, int externalPortTCP)
          Maps with UPNP protocol (http://en.wikipedia.org/wiki/Internet_Gateway_Device_Protocol).
 void shutdown()
          Since shutdown is also called from the shutdown hook, it might get called twice.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NATUtils

public NATUtils()
Constructor.

Method Detail

mapPMP

public boolean mapPMP(int internalPortUDP,
                      int internalPortTCP,
                      int externalPortUDP,
                      int externalPortTCP)
               throws NatPmpException
Maps with the PMP protocol (http://en.wikipedia.org/wiki/NAT_Port_Mapping_Protocol). One of the drawbacks of this protocol is that it needs to know the IP of the router. To get the IP of the router in Java, we need to use netstat and parse the output.

Parameters:
internalPortUDP - The UDP internal port
internalPortTCP - The TCP internal port
externalPortUDP - The UDP external port
externalPortTCP - The TCP external port
Returns:
True, if the mapping was successful (i.e. the router supports PMP)
Throws:
NatPmpException - the router does not supports PMP

mapUPNP

public boolean mapUPNP(String internalHost,
                       int internalPortUDP,
                       int internalPortTCP,
                       int externalPortUDP,
                       int externalPortTCP)
                throws IOException
Maps with UPNP protocol (http://en.wikipedia.org/wiki/Internet_Gateway_Device_Protocol). Since this uses broadcasting to discover routers, no calling the external program netstat is necessary.

Parameters:
internalHost - The internal host to map the ports to
internalPortUDP - The UDP internal port
internalPortTCP - The TCP internal port
externalPortUDP - The UDP external port
externalPortTCP - The TCP external port
Returns:
True, if at least one mapping was successful (i.e. the router supports UPNP)
Throws:
IOException - Exception

shutdown

public void shutdown()
Since shutdown is also called from the shutdown hook, it might get called twice. Thus, this method deregister NAT mappings only once. If it already has been called, this method does nothing.



Copyright © 2013. All Rights Reserved.