Class NatPMPDeviceImpl

  • All Implemented Interfaces:
    NatPMPDevice

    public class NatPMPDeviceImpl
    extends java.lang.Object
    implements NatPMPDevice
    Main class
    • Constructor Detail

      • NatPMPDeviceImpl

        private NatPMPDeviceImpl​(NATPMPDeviceAdapter _adapter)
                          throws java.lang.Exception
        Throws:
        java.lang.Exception
    • Method Detail

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

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