Package com.biglybt.net.natpmp.impl
Class NatPMPDeviceImpl
- java.lang.Object
-
- com.biglybt.net.natpmp.impl.NatPMPDeviceImpl
-
- All Implemented Interfaces:
NatPMPDevice
public class NatPMPDeviceImpl extends java.lang.Object implements NatPMPDevice
Main class
-
-
Field Summary
Fields Modifier and Type Field Description private NATPMPDeviceAdapter
adapter
private java.lang.String
current_router_address
private java.net.InetAddress
hostInet
private int
nat_epoch
(package private) static int
NATAddrReplyLen
(package private) static int
NATAddrRequest
(package private) static int
NATMAP_DEFAULT_LEASE
(package private) static int
NATMAP_INIT_RETRY
(package private) static java.lang.String
NATMAP_LLM
(package private) static int
NATMAP_MAX_RETRY
(package private) static int
NATMAP_PORT
(package private) static int
NATMAP_RESPONSE_MASK
(package private) static int
NATMAP_VER
(package private) static byte
NATOp_AddrRequest
(package private) static byte
NATOp_MapTCP
(package private) static byte
NATOp_MapUDP
private static NatPMPDeviceImpl
NatPMPDeviceSingletonRef
Singleton creation(package private) static int
NATPortMapReplyLen
(package private) static int
NATPortMapRequestLen
private java.net.InetAddress
natPriInet
private java.net.InetAddress
natPubInet
(package private) static int
NATResultNetFailure
Network Failure (e.g.(package private) static int
NATResultNoResc
(package private) static int
NATResultNotAuth
Not Authorized/Refused (e.g.(package private) static int
NATResultSuccess
(package private) static int
NATResultUnsupportedOp
(package private) static int
NATResultUnsupportedVer
private java.net.NetworkInterface
networkInterface
-
Constructor Summary
Constructors Modifier Constructor Description private
NatPMPDeviceImpl(NATPMPDeviceAdapter _adapter)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.private java.lang.String
convertHost2RouterAddress(java.net.InetAddress inet)
void
deletePortMapping(boolean tcp, int publicPort, int privatePort)
Delete a mapped public portint
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 arrayprotected void
log(java.lang.String str)
int
portMappingProtocol(boolean tcp, int publicPort, int privatePort, int lifetime)
General port mapping protocoljava.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 arraystatic 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)
-
-
-
Field Detail
-
NATMAP_VER
static final int NATMAP_VER
- See Also:
- Constant Field Values
-
NATMAP_PORT
static final int NATMAP_PORT
- See Also:
- Constant Field Values
-
NATMAP_RESPONSE_MASK
static final int NATMAP_RESPONSE_MASK
- See Also:
- Constant Field Values
-
NATMAP_INIT_RETRY
static final int NATMAP_INIT_RETRY
- See Also:
- Constant Field Values
-
NATMAP_MAX_RETRY
static final int NATMAP_MAX_RETRY
- See Also:
- Constant Field Values
-
NATMAP_DEFAULT_LEASE
static final int NATMAP_DEFAULT_LEASE
- See Also:
- Constant Field Values
-
NATMAP_LLM
static final java.lang.String NATMAP_LLM
- See Also:
- Constant Field Values
-
NATOp_AddrRequest
static final byte NATOp_AddrRequest
- See Also:
- Constant Field Values
-
NATOp_MapUDP
static final byte NATOp_MapUDP
- See Also:
- Constant Field Values
-
NATOp_MapTCP
static final byte NATOp_MapTCP
- See Also:
- Constant Field Values
-
NATAddrRequest
static final int NATAddrRequest
- See Also:
- Constant Field Values
-
NATPortMapRequestLen
static final int NATPortMapRequestLen
- See Also:
- Constant Field Values
-
NATAddrReplyLen
static final int NATAddrReplyLen
- See Also:
- Constant Field Values
-
NATPortMapReplyLen
static final int NATPortMapReplyLen
- See Also:
- Constant Field Values
-
NATResultSuccess
static final int NATResultSuccess
- See Also:
- Constant Field Values
-
NATResultUnsupportedVer
static final int NATResultUnsupportedVer
- See Also:
- Constant Field Values
-
NATResultNotAuth
static final int NATResultNotAuth
Not Authorized/Refused (e.g. box supports mapping, but user has turned feature off)- See Also:
- Constant Field Values
-
NATResultNetFailure
static final int NATResultNetFailure
Network Failure (e.g. NAT box itself has not obtained a DHCP lease)- See Also:
- Constant Field Values
-
NATResultNoResc
static final int NATResultNoResc
- See Also:
- Constant Field Values
-
NATResultUnsupportedOp
static final int NATResultUnsupportedOp
- See Also:
- Constant Field Values
-
current_router_address
private java.lang.String current_router_address
-
hostInet
private java.net.InetAddress hostInet
-
natPriInet
private java.net.InetAddress natPriInet
-
natPubInet
private java.net.InetAddress natPubInet
-
networkInterface
private java.net.NetworkInterface networkInterface
-
nat_epoch
private int nat_epoch
-
adapter
private NATPMPDeviceAdapter adapter
-
NatPMPDeviceSingletonRef
private static NatPMPDeviceImpl NatPMPDeviceSingletonRef
Singleton creation
-
-
Constructor Detail
-
NatPMPDeviceImpl
private NatPMPDeviceImpl(NATPMPDeviceAdapter _adapter) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-
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 sendrecBuf
- 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 interfaceNatPMPDevice
- 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 interfaceNatPMPDevice
- 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 interfaceNatPMPDevice
- Parameters:
tcp
- true TCP, false UDP portpublicPort
- the public port to closeprivatePort
- 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 interfaceNatPMPDevice
-
getNetworkInterface
public java.net.NetworkInterface getNetworkInterface()
- Specified by:
getNetworkInterface
in interfaceNatPMPDevice
-
getExternalIPAddress
public java.lang.String getExternalIPAddress()
- Specified by:
getExternalIPAddress
in interfaceNatPMPDevice
-
getEpoch
public int getEpoch()
- Specified by:
getEpoch
in interfaceNatPMPDevice
-
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 arrayoffset
- 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 arrayoffset
- 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 arrayoffset
- 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)
-
-