Class NatPMPDeviceImpl

java.lang.Object
com.biglybt.net.natpmp.impl.NatPMPDeviceImpl
All Implemented Interfaces:
NatPMPDevice

public class NatPMPDeviceImpl extends Object implements NatPMPDevice
Main class
  • Field Details

  • Constructor Details

  • Method Details

    • 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)
    • convertHost2RouterAddress

      private String convertHost2RouterAddress(InetAddress inet)
      Parameters:
      init - takes the host address
      Returns:
      String the address as (xxx.xxx.xxx.1)