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 java.lang.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.
 java.lang.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()
           
 java.lang.String getExternalIPAddress()
           
 java.net.InetAddress getLocalAddress()
           
 java.net.NetworkInterface getNetworkInterface()
           
static NatPMPDeviceImpl getSingletonObject(NATPMPDeviceAdapter adapter)
           
 java.lang.String intArrayString(int[] buf)
           
 byte[] intToByteArray(int v)
          Convert a 32-bit int into a 4 byte array
protected  void log(java.lang.String str)
           
 int portMappingProtocol(boolean tcp, int publicPort, int privatePort, int lifetime)
          General port mapping protocol
 java.net.DatagramPacket sendNATMsg(java.net.InetAddress dstInet, java.net.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 java.lang.Exception
Throws:
java.lang.Exception

checkRouterAddress

protected void checkRouterAddress()
                           throws java.lang.Exception
Throws:
java.lang.Exception

sendNATMsg

public java.net.DatagramPacket sendNATMsg(java.net.InetAddress dstInet,
                                          java.net.DatagramPacket dstPkt,
                                          byte[] recBuf)
                                   throws java.lang.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:
java.lang.Exception

connect

public boolean connect()
                throws java.lang.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:
java.lang.Exception

addPortMapping

public int addPortMapping(boolean tcp,
                          int publicPort,
                          int privatePort)
                   throws java.lang.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:
java.lang.Exception

deletePortMapping

public void deletePortMapping(boolean tcp,
                              int publicPort,
                              int privatePort)
                       throws java.lang.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:
java.lang.Exception

portMappingProtocol

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

Throws:
java.lang.Exception

getLocalAddress

public java.net.InetAddress getLocalAddress()
Specified by:
getLocalAddress in interface NatPMPDevice

getNetworkInterface

public java.net.NetworkInterface getNetworkInterface()
Specified by:
getNetworkInterface in interface NatPMPDevice

getExternalIPAddress

public java.lang.String getExternalIPAddress()
Specified by:
getExternalIPAddress in interface NatPMPDevice

getEpoch

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

log

protected void log(java.lang.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 java.lang.String intArrayString(int[] buf)

byteArrayString

public java.lang.String byteArrayString(byte[] buf)