com.aelitis.net.natpmp.impl
Class NatPMPDeviceImpl

java.lang.Object
  extended by com.aelitis.net.natpmp.impl.NatPMPDeviceImpl
All Implemented Interfaces:
NatPMPDevice

public class NatPMPDeviceImpl
extends Object
implements NatPMPDevice

Main class


Method Summary
 int addPortMapping(boolean tcp, int publicPort, int privatePort)
          Asks for a public port to be mapped to a private port from this host.
 String byteArrayString(byte[] buf)
           
protected  void checkRouterAddress()
           
 boolean connect()
          Try to connect with a NAT-PMP device.
 void deletePortMapping(boolean tcp, int publicPort, int privatePort)
          Delete a mapped public port
 int getEpoch()
           
 String getExternalIPAddress()
           
 InetAddress getLocalAddress()
           
 NetworkInterface getNetworkInterface()
           
static NatPMPDeviceImpl getSingletonObject(NATPMPDeviceAdapter adapter)
           
 String intArrayString(int[] buf)
           
 byte[] intToByteArray(int v)
          Convert a 32-bit int into a 4 byte array
protected  void log(String str)
           
 int portMappingProtocol(boolean tcp, int publicPort, int privatePort, int lifetime)
          General port mapping protocol
 DatagramPacket sendNATMsg(InetAddress dstInet, DatagramPacket dstPkt, byte[] recBuf)
          Send a request and wait for reply This class should be threaded!!! This sends to the default NATPMP_PORT.
 byte[] shortToByteArray(short v)
          Convert a 16-bit short into a 2 byte array
static int unsigned16ByteArrayToInt(byte[] b, int offset)
          Convert the byte array containing 16-bits to an int starting from the given offset.
static int unsigned32ByteArrayToInt(byte[] b, int offset)
          Convert the byte array containing 32-bit to an int starting from the given offset.
static int unsigned8ByteArrayToInt(byte[] b, int offset)
          Convert the byte array containing 8-bits to an int starting from the given offset.
 short unsignedByteArrayToShort(byte[] buf)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getSingletonObject

public static NatPMPDeviceImpl getSingletonObject(NATPMPDeviceAdapter adapter)
                                           throws Exception
Throws:
Exception

checkRouterAddress

protected void checkRouterAddress()
                           throws Exception
Throws:
Exception

sendNATMsg

public DatagramPacket sendNATMsg(InetAddress dstInet,
                                 DatagramPacket dstPkt,
                                 byte[] recBuf)
                          throws Exception
Send a request and wait for reply This class should be threaded!!! This sends to the default NATPMP_PORT.

Parameters:
dstInet - destination address (should be the private NAT address)
dstPkt - packet to send
recBuf - byte buffer big enough to hold received
Throws:
Exception

connect

public boolean connect()
                throws Exception
Try to connect with a NAT-PMP device. This could take sometime.

Specified by:
connect in interface NatPMPDevice
Returns:
true if it found one
Throws:
Exception

addPortMapping

public int addPortMapping(boolean tcp,
                          int publicPort,
                          int privatePort)
                   throws Exception
Asks for a public port to be mapped to a private port from this host. NAP-PMP allows the device to assign another public port if the requested one is taken. So, you should check the returned port.

Specified by:
addPortMapping in interface NatPMPDevice
Parameters:
tcp - true TCP, false UDP
Returns:
the returned publicPort. -1 if error occured
Throws:
Exception

deletePortMapping

public void deletePortMapping(boolean tcp,
                              int publicPort,
                              int privatePort)
                       throws Exception
Delete a mapped public port

Specified by:
deletePortMapping in interface NatPMPDevice
Parameters:
tcp - true TCP, false UDP port
publicPort - the public port to close
privatePort - the private port that it is mapped to
Throws:
Exception

portMappingProtocol

public int portMappingProtocol(boolean tcp,
                               int publicPort,
                               int privatePort,
                               int lifetime)
                        throws Exception
General port mapping protocol

Throws:
Exception

getLocalAddress

public InetAddress getLocalAddress()
Specified by:
getLocalAddress in interface NatPMPDevice

getNetworkInterface

public NetworkInterface getNetworkInterface()
Specified by:
getNetworkInterface in interface NatPMPDevice

getExternalIPAddress

public String getExternalIPAddress()
Specified by:
getExternalIPAddress in interface NatPMPDevice

getEpoch

public int getEpoch()
Specified by:
getEpoch in interface NatPMPDevice

log

protected void log(String str)

unsigned32ByteArrayToInt

public static int unsigned32ByteArrayToInt(byte[] b,
                                           int offset)
Convert the byte array containing 32-bit to an int starting from the given offset.

Parameters:
b - The byte array
offset - The array offset
Returns:
The integer

unsigned16ByteArrayToInt

public static int unsigned16ByteArrayToInt(byte[] b,
                                           int offset)
Convert the byte array containing 16-bits to an int starting from the given offset.

Parameters:
b - The byte array
offset - The array offset
Returns:
The integer

unsigned8ByteArrayToInt

public static int unsigned8ByteArrayToInt(byte[] b,
                                          int offset)
Convert the byte array containing 8-bits to an int starting from the given offset.

Parameters:
b - The byte array
offset - The array offset
Returns:
The integer

unsignedByteArrayToShort

public short unsignedByteArrayToShort(byte[] buf)

shortToByteArray

public byte[] shortToByteArray(short v)
Convert a 16-bit short into a 2 byte array

Returns:
unsigned byte array

intToByteArray

public byte[] intToByteArray(int v)
Convert a 32-bit int into a 4 byte array

Returns:
unsigned byte array

intArrayString

public String intArrayString(int[] buf)

byteArrayString

public String byteArrayString(byte[] buf)