Makes TransportAddress a descendant of InetSocketAddress.
This commit is contained in:
parent
e9d364d650
commit
30ae0a9047
@ -76,6 +76,21 @@ public class TransportAddress
|
|||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an address instance from an <tt>InetSocketAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param address the address and port.
|
||||||
|
* @param transport the transport to use with this address.
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException if the port parameter is outside the
|
||||||
|
* range of valid port values, or if the host name parameter is
|
||||||
|
* <tt>null</tt>.
|
||||||
|
*/
|
||||||
|
public TransportAddress(InetSocketAddress address, Transport transport)
|
||||||
|
{
|
||||||
|
super(address.getAddress(), address.getPort());
|
||||||
|
this.transport = transport;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an address instance from a host name and a port number.
|
* Creates an address instance from a host name and a port number.
|
||||||
@ -152,4 +167,27 @@ public class TransportAddress
|
|||||||
{
|
{
|
||||||
return transport;
|
return transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares this object against the specified object. The result is
|
||||||
|
* <tt>true</tt> if and only if the argument is not <tt>null</tt> and it
|
||||||
|
* represents the same address.
|
||||||
|
* <p>
|
||||||
|
* Two instances of <tt>TransportAddress</tt> represent the same
|
||||||
|
* address if both the InetAddresses (or hostnames if it is unresolved),
|
||||||
|
* port numbers, and <tt>Transport</tt>s are equal.
|
||||||
|
*
|
||||||
|
* If both addresses are unresolved, then the hostname, the port & and
|
||||||
|
* the <tt>Transport</tt> are compared.
|
||||||
|
*
|
||||||
|
* @param obj the object to compare against.
|
||||||
|
* @return <tt>true</tt> if the objects are the same and
|
||||||
|
* <tt>false</tt> otherwise.
|
||||||
|
* @see java.net.InetAddress#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
|
public boolean equalsTransportAddress(Object obj)
|
||||||
|
{
|
||||||
|
return super.equals(obj)
|
||||||
|
&&( ((TransportAddress)obj).getTransport() == getTransport() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||||
|
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||||
*
|
*
|
||||||
* Distributable under LGPL license.
|
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||||
* See terms of license at gnu.org.
|
|
||||||
*/
|
*/
|
||||||
package org.ice4j.attribute;
|
package org.ice4j.attribute;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
@ -14,7 +13,7 @@ import org.ice4j.*;
|
|||||||
/**
|
/**
|
||||||
* This class is used to represent Stun attributes that contain an address. Such
|
* This class is used to represent Stun attributes that contain an address. Such
|
||||||
* attributes are:
|
* attributes are:
|
||||||
*
|
*<p>
|
||||||
* MAPPED-ADDRESS <br/>
|
* MAPPED-ADDRESS <br/>
|
||||||
* RESPONSE-ADDRESS <br/>
|
* RESPONSE-ADDRESS <br/>
|
||||||
* SOURCE-ADDRESS <br/>
|
* SOURCE-ADDRESS <br/>
|
||||||
@ -23,14 +22,16 @@ import org.ice4j.*;
|
|||||||
* ALTERNATE-SERVER <br/>
|
* ALTERNATE-SERVER <br/>
|
||||||
* XOR-PEER-ADDRESS <br/>
|
* XOR-PEER-ADDRESS <br/>
|
||||||
* XOR-RELAYED-ADDRESS <br/>
|
* XOR-RELAYED-ADDRESS <br/>
|
||||||
*
|
*</p>
|
||||||
|
*<p>
|
||||||
* The different attributes are distinguished by the attributeType of
|
* The different attributes are distinguished by the attributeType of
|
||||||
* org.ice4j.attribute.Attribute.
|
* org.ice4j.attribute.Attribute.
|
||||||
*
|
*</p>
|
||||||
|
*<p>
|
||||||
* Address attributes indicate the mapped IP address and
|
* Address attributes indicate the mapped IP address and
|
||||||
* port. They consist of an eight bit address family, and a sixteen bit
|
* port. They consist of an eight bit address family, and a sixteen bit
|
||||||
* port, followed by a fixed length value representing the IP address.
|
* port, followed by a fixed length value representing the IP address.
|
||||||
*
|
*<code>
|
||||||
* 0 1 2 3 <br/>
|
* 0 1 2 3 <br/>
|
||||||
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 <br/>
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 <br/>
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ <br/>
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ <br/>
|
||||||
@ -39,21 +40,26 @@ import org.ice4j.*;
|
|||||||
* | Address | <br/>
|
* | Address | <br/>
|
||||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ <br/>
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ <br/>
|
||||||
* <br/>
|
* <br/>
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
* The port is a network byte ordered representation of the mapped port.
|
* The port is a network byte ordered representation of the mapped port.
|
||||||
* The address family is always 0x01, corresponding to IPv4. The first
|
* The address family is always 0x01, corresponding to IPv4. The first
|
||||||
* 8 bits of the MAPPED-ADDRESS are ignored, for the purposes of
|
* 8 bits of the MAPPED-ADDRESS are ignored, for the purposes of
|
||||||
* aligning parameters on natural boundaries. The IPv4 address is 32
|
* aligning parameters on natural boundaries. The IPv4 address is 32
|
||||||
* bits.
|
* bits.
|
||||||
*
|
* </p>
|
||||||
*
|
|
||||||
* <p>Organisation: Louis Pasteur University, Strasbourg, France</p>
|
|
||||||
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
|
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
* @version 0.1
|
|
||||||
*/
|
*/
|
||||||
abstract class AddressAttribute extends Attribute
|
abstract class AddressAttribute extends Attribute
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Indicates that this attribute is transporting an IPv4 address
|
||||||
|
*/
|
||||||
static final byte ADDRESS_FAMILY_IPV4 = 0x01;
|
static final byte ADDRESS_FAMILY_IPV4 = 0x01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that this attribute is transporting an IPv6 address
|
||||||
|
*/
|
||||||
static final byte ADDRESS_FAMILY_IPV6 = 0x02;
|
static final byte ADDRESS_FAMILY_IPV6 = 0x02;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -105,7 +111,8 @@ abstract class AddressAttribute extends Attribute
|
|||||||
protected void setAttributeType(char type)
|
protected void setAttributeType(char type)
|
||||||
{
|
{
|
||||||
if (!isTypeValid(type))
|
if (!isTypeValid(type))
|
||||||
throw new IllegalArgumentException(((int)type) + "is not a valid address attribute!");
|
throw new IllegalArgumentException(((int)type)
|
||||||
|
+ "is not a valid address attribute!");
|
||||||
|
|
||||||
super.setAttributeType(type);
|
super.setAttributeType(type);
|
||||||
}
|
}
|
||||||
@ -187,7 +194,8 @@ abstract class AddressAttribute extends Attribute
|
|||||||
{
|
{
|
||||||
char type = getAttributeType();
|
char type = getAttributeType();
|
||||||
if (!isTypeValid(type))
|
if (!isTypeValid(type))
|
||||||
throw new IllegalStateException(((int)type) + "is not a valid address attribute!");
|
throw new IllegalStateException(((int)type)
|
||||||
|
+ "is not a valid address attribute!");
|
||||||
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
|
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
|
||||||
|
|
||||||
//Type
|
//Type
|
||||||
@ -250,7 +258,7 @@ abstract class AddressAttribute extends Attribute
|
|||||||
*/
|
*/
|
||||||
public byte getFamily()
|
public byte getFamily()
|
||||||
{
|
{
|
||||||
if ( address.getSocketAddress().getAddress() instanceof Inet6Address )
|
if ( address.getAddress() instanceof Inet6Address )
|
||||||
return ADDRESS_FAMILY_IPV6;
|
return ADDRESS_FAMILY_IPV6;
|
||||||
else
|
else
|
||||||
return ADDRESS_FAMILY_IPV4;
|
return ADDRESS_FAMILY_IPV4;
|
||||||
@ -287,7 +295,8 @@ abstract class AddressAttribute extends Attribute
|
|||||||
byte family = attributeValue[offset++];
|
byte family = attributeValue[offset++];
|
||||||
|
|
||||||
//port
|
//port
|
||||||
char port = ((char)((attributeValue[offset++] << 8 ) | (attributeValue[offset++]&0xFF) ));
|
char port = ((char)((attributeValue[offset++] << 8 )
|
||||||
|
| (attributeValue[offset++]&0xFF) ));
|
||||||
|
|
||||||
//address
|
//address
|
||||||
byte address[] = null;
|
byte address[] = null;
|
||||||
@ -300,7 +309,14 @@ abstract class AddressAttribute extends Attribute
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.arraycopy(attributeValue, offset, address, 0, address.length);
|
System.arraycopy(attributeValue, offset, address, 0, address.length);
|
||||||
setAddress(new TransportAddress(address, port));
|
try
|
||||||
|
{
|
||||||
|
setAddress(new TransportAddress(address, port, Transport.UDP));
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e)
|
||||||
|
{
|
||||||
|
throw new StunException(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
* Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||||
|
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||||
*
|
*
|
||||||
* Distributable under LGPL license.
|
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||||
* See terms of license at gnu.org.
|
|
||||||
*/
|
*/
|
||||||
package org.ice4j.attribute;
|
package org.ice4j.attribute;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,8 +70,10 @@ public class XorMappedAddressAttribute
|
|||||||
/**
|
/**
|
||||||
* Returns the result of applying XOR on the specified attribute's address.
|
* Returns the result of applying XOR on the specified attribute's address.
|
||||||
* The method may be used for both encoding and decoding XorMappedAddresses.
|
* The method may be used for both encoding and decoding XorMappedAddresses.
|
||||||
|
*
|
||||||
* @param address the address on which XOR should be applied
|
* @param address the address on which XOR should be applied
|
||||||
* @param transactionID the transaction id to use for the XOR
|
* @param transactionID the transaction id to use for the XOR
|
||||||
|
*
|
||||||
* @return the XOR-ed address.
|
* @return the XOR-ed address.
|
||||||
*/
|
*/
|
||||||
public static TransportAddress applyXor(TransportAddress address,
|
public static TransportAddress applyXor(TransportAddress address,
|
||||||
@ -86,7 +90,16 @@ public class XorMappedAddressAttribute
|
|||||||
for(int i = 0; i < addressBytes.length; i++)
|
for(int i = 0; i < addressBytes.length; i++)
|
||||||
addressBytes[i] ^= transactionID[i];
|
addressBytes[i] ^= transactionID[i];
|
||||||
|
|
||||||
TransportAddress xoredAdd = new TransportAddress(addressBytes, port);
|
TransportAddress xoredAdd;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
xoredAdd = new TransportAddress(addressBytes, port, Transport.UDP);
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e)
|
||||||
|
{
|
||||||
|
//shouldn't happen so just throw an illegal arg
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
}
|
||||||
|
|
||||||
return xoredAdd;
|
return xoredAdd;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -397,7 +397,7 @@ public class Candidate
|
|||||||
return MIN_LOCAL_PREFERENCE;
|
return MIN_LOCAL_PREFERENCE;
|
||||||
|
|
||||||
|
|
||||||
InetAddress addr = getTransportAddress().getInetAddress();
|
InetAddress addr = getTransportAddress().getAddress();
|
||||||
|
|
||||||
//the following tries to reusse precedence from RFC 3484 but that's a
|
//the following tries to reusse precedence from RFC 3484 but that's a
|
||||||
//bit tricky since it is not meant to be used exactly the way that
|
//bit tricky since it is not meant to be used exactly the way that
|
||||||
|
|||||||
@ -46,7 +46,7 @@ public class HostCandidate extends Candidate
|
|||||||
Component parentComponent)
|
Component parentComponent)
|
||||||
{
|
{
|
||||||
super(new TransportAddress(socket.getLocalAddress(),
|
super(new TransportAddress(socket.getLocalAddress(),
|
||||||
socket.getLocalPort()),
|
socket.getLocalPort(), Transport.UDP),
|
||||||
parentComponent,
|
parentComponent,
|
||||||
CandidateType.HOST_CANDIDATE);
|
CandidateType.HOST_CANDIDATE);
|
||||||
|
|
||||||
|
|||||||
@ -8,8 +8,6 @@
|
|||||||
package org.ice4j.ice.harvest;
|
package org.ice4j.ice.harvest;
|
||||||
|
|
||||||
import org.ice4j.ice.*;
|
import org.ice4j.ice.*;
|
||||||
import org.ice4j.stack.*;
|
|
||||||
import org.ice4j.stunclient.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A <tt>StunCandidateHarvester</tt> gathers STUN <tt>Candidate</tt>s for a
|
* A <tt>StunCandidateHarvester</tt> gathers STUN <tt>Candidate</tt>s for a
|
||||||
@ -30,7 +28,6 @@ public class StunCandidateHarvester
|
|||||||
public void harvest(Component component)
|
public void harvest(Component component)
|
||||||
{
|
{
|
||||||
//todo implement
|
//todo implement
|
||||||
StunClient client = new StunClient(stack);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -194,7 +194,7 @@ class NetAccessPoint
|
|||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
DatagramPacket datagramPacket = new DatagramPacket(
|
DatagramPacket datagramPacket = new DatagramPacket(
|
||||||
message, 0, message.length, address.getSocketAddress());
|
message, 0, message.length, address);
|
||||||
|
|
||||||
synchronized(sock)
|
synchronized(sock)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -176,7 +176,7 @@ class StunServerTransaction
|
|||||||
* access point that had not been installed,
|
* access point that had not been installed,
|
||||||
* @throws StunException if message encoding fails,
|
* @throws StunException if message encoding fails,
|
||||||
*/
|
*/
|
||||||
private void retransmitResponse()
|
protected void retransmitResponse()
|
||||||
throws StunException,
|
throws StunException,
|
||||||
IOException,
|
IOException,
|
||||||
IllegalArgumentException
|
IllegalArgumentException
|
||||||
|
|||||||
@ -167,8 +167,7 @@ public class NetworkConfigurationDiscoveryProcess
|
|||||||
{
|
{
|
||||||
stunStack = StunStack.getInstance();
|
stunStack = StunStack.getInstance();
|
||||||
|
|
||||||
DatagramSocket sock
|
DatagramSocket sock = new DatagramSocket(localAddress);
|
||||||
= new DatagramSocket(localAddress.getSocketAddress());
|
|
||||||
|
|
||||||
stunStack.installNetAccessPoint(sock);
|
stunStack.installNetAccessPoint(sock);
|
||||||
|
|
||||||
|
|||||||
@ -97,141 +97,65 @@ public class SimpleAddressDetector
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a listening point from the following address and attempts to
|
* Creates a listening point for the specified socket and attempts to
|
||||||
* discover how it is mapped so that using inside the application is
|
* discover how its local address is NAT mapped.
|
||||||
* possible.
|
* @param socket the socket whose address needs to be resolved.
|
||||||
*
|
|
||||||
* @param address the [address]:[port] pair where ther request should be
|
|
||||||
* sent from.
|
|
||||||
*
|
|
||||||
* @return a StunAddress object containing the mapped address or null if
|
* @return a StunAddress object containing the mapped address or null if
|
||||||
* discovery failed.
|
* discovery failed.
|
||||||
* @throws IOException if <tt>address</tt> is already bound.
|
*
|
||||||
|
* @throws IOException if something fails along the way.
|
||||||
|
* @throws BindException if we cannot bind the socket.
|
||||||
*/
|
*/
|
||||||
public TransportAddress getMappingFor(TransportAddress address)
|
public TransportAddress getMappingFor(DatagramSocket socket)
|
||||||
throws IOException
|
throws IOException, BindException
|
||||||
{
|
{
|
||||||
NetAccessPointDescriptor apDesc = new NetAccessPointDescriptor(address);
|
TransportAddress localAddress = new TransportAddress(
|
||||||
|
(InetSocketAddress)socket.getLocalSocketAddress(), Transport.UDP);
|
||||||
|
|
||||||
stunStack.installNetAccessPoint(apDesc);
|
stunStack.installNetAccessPoint(socket);
|
||||||
|
|
||||||
requestSender = new BlockingRequestSender(stunProvider, apDesc);
|
requestSender = new BlockingRequestSender(stunProvider, localAddress);
|
||||||
StunMessageEvent evt = null;
|
StunMessageEvent evt = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
evt = requestSender.sendRequestAndWaitForResponse(
|
||||||
{
|
MessageFactory.createBindingRequest(), serverAddress);
|
||||||
evt = requestSender.sendRequestAndWaitForResponse(
|
}
|
||||||
MessageFactory.createBindingRequest(), serverAddress);
|
catch(StunException exc)
|
||||||
}
|
{
|
||||||
catch (StunException ex)
|
//this shouldn't be happening since we are the one that constructed
|
||||||
{
|
//the request, so let's catch it here and not oblige users to
|
||||||
//a stun exception is thrown if there is a problem with
|
//handle exception they are not responsible for.
|
||||||
//the message. The message here is constructed by ourselves
|
logger.log(Level.SEVERE, "Internal Error. We apparently "
|
||||||
//so we don't need to tell the user ... just log and throw
|
+"constructed a faulty request.", exc);
|
||||||
//a runtime exception
|
return null;
|
||||||
logger.log( Level.SEVERE,
|
}
|
||||||
"Internal Error. Apparently we did not properly "
|
finally
|
||||||
+"construct a message",
|
{
|
||||||
ex);
|
stunStack.removeNetAccessPoint(localAddress);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
//free the port to allow the application to use it.
|
|
||||||
stunStack.removeNetAccessPoint(apDesc);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(evt != null)
|
if(evt != null)
|
||||||
{
|
{
|
||||||
Response res = (Response)evt.getMessage();
|
Response res = (Response)evt.getMessage();
|
||||||
|
|
||||||
/* in classic STUN, the response contains a MAPPED-ADDRESS */
|
/* in classic STUN, the response contains a MAPPED-ADDRESS */
|
||||||
MappedAddressAttribute maAtt =
|
MappedAddressAttribute maAtt = (MappedAddressAttribute)
|
||||||
(MappedAddressAttribute)
|
res.getAttribute(Attribute.MAPPED_ADDRESS);
|
||||||
res.getAttribute(Attribute.MAPPED_ADDRESS);
|
if(maAtt != null)
|
||||||
if(maAtt != null)
|
{
|
||||||
{
|
return maAtt.getAddress();
|
||||||
return maAtt.getAddress();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* in STUN bis, the response contains a XOR-MAPPED-ADDRESS */
|
/* in STUN bis, the response contains a XOR-MAPPED-ADDRESS */
|
||||||
XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res
|
XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res
|
||||||
.getAttribute(Attribute.XOR_MAPPED_ADDRESS);
|
.getAttribute(Attribute.XOR_MAPPED_ADDRESS);
|
||||||
|
if(xorAtt != null)
|
||||||
|
{
|
||||||
|
byte xoring[] = new byte[16];
|
||||||
|
|
||||||
if(xorAtt != null)
|
System.arraycopy(Message.MAGIC_COOKIE, 0, xoring, 0, 4);
|
||||||
{
|
System.arraycopy(res.getTransactionID(), 0, xoring, 4, 12);
|
||||||
byte xoring[] = new byte[16];
|
|
||||||
|
|
||||||
System.arraycopy(Message.MAGIC_COOKIE, 0, xoring, 0, 4);
|
|
||||||
System.arraycopy(res.getTransactionID(), 0, xoring, 4, 12);
|
|
||||||
|
|
||||||
return xorAtt.applyXor(xoring);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a listening point for the specified socket and attempts to
|
|
||||||
* discover how its local address is NAT mapped.
|
|
||||||
* @param socket the socket whose address needs to be resolved.
|
|
||||||
* @return a StunAddress object containing the mapped address or null if
|
|
||||||
* discovery failed.
|
|
||||||
*
|
|
||||||
* @throws IOException if something fails along the way.
|
|
||||||
* @throws BindException if we cannot bind the socket.
|
|
||||||
*/
|
|
||||||
public TransportAddress getMappingFor(DatagramSocket socket)
|
|
||||||
throws IOException, BindException
|
|
||||||
{
|
|
||||||
NetAccessPointDescriptor apDesc
|
|
||||||
= stunStack.installNetAccessPoint(socket);
|
|
||||||
|
|
||||||
requestSender = new BlockingRequestSender(stunProvider, apDesc);
|
|
||||||
StunMessageEvent evt = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
evt = requestSender.sendRequestAndWaitForResponse(
|
|
||||||
MessageFactory.createBindingRequest(), serverAddress);
|
|
||||||
}
|
|
||||||
catch(StunException exc)
|
|
||||||
{
|
|
||||||
//this shouldn't be happening since we are the one that constructed
|
|
||||||
//the request, so let's catch it here and not oblige users to handle
|
|
||||||
//exception they are not responsible for.
|
|
||||||
logger.log(Level.SEVERE, "Internal Error. We apparently "
|
|
||||||
+"constructed a faulty request.", exc);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
stunStack.removeNetAccessPoint(apDesc);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(evt != null)
|
|
||||||
{
|
|
||||||
Response res = (Response)evt.getMessage();
|
|
||||||
|
|
||||||
/* in classic STUN, the response contains a MAPPED-ADDRESS */
|
|
||||||
MappedAddressAttribute maAtt =
|
|
||||||
(MappedAddressAttribute)
|
|
||||||
res.getAttribute(Attribute.MAPPED_ADDRESS);
|
|
||||||
if(maAtt != null)
|
|
||||||
{
|
|
||||||
return maAtt.getAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* in STUN bis, the response contains a XOR-MAPPED-ADDRESS */
|
|
||||||
XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res
|
|
||||||
.getAttribute(Attribute.XOR_MAPPED_ADDRESS);
|
|
||||||
if(xorAtt != null)
|
|
||||||
{
|
|
||||||
byte xoring[] = new byte[16];
|
|
||||||
|
|
||||||
System.arraycopy(Message.MAGIC_COOKIE, 0, xoring, 0, 4);
|
|
||||||
System.arraycopy(res.getTransactionID(), 0, xoring, 4, 12);
|
|
||||||
|
|
||||||
return xorAtt.applyXor(xoring);
|
return xorAtt.applyXor(xoring);
|
||||||
}
|
}
|
||||||
@ -239,21 +163,4 @@ public class SimpleAddressDetector
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a listening point from the specified port and attempts to
|
|
||||||
* discover how it is being mapped.
|
|
||||||
*
|
|
||||||
* @param port the local port where to send the request from.
|
|
||||||
* @return a StunAddress object containing the mapped address or null if
|
|
||||||
* discovery failed.
|
|
||||||
*
|
|
||||||
* @throws IOException if the <tt>port</tt> is already bound.
|
|
||||||
*/
|
|
||||||
public TransportAddress getMappingFor(int port)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
return getMappingFor(new TransportAddress(port));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user