From 30ae0a90471a26534b65bdb2126a5c823b129e2a Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 31 Jan 2010 13:01:07 +0000 Subject: [PATCH] Makes TransportAddress a descendant of InetSocketAddress. --- src/org/ice4j/TransportAddress.java | 38 ++++ src/org/ice4j/attribute/AddressAttribute.java | 52 +++-- .../attribute/XorMappedAddressAttribute.java | 21 +- src/org/ice4j/ice/Candidate.java | 2 +- src/org/ice4j/ice/HostCandidate.java | 2 +- .../ice/harvest/StunCandidateHarvester.java | 3 - src/org/ice4j/stack/NetAccessPoint.java | 2 +- .../ice4j/stack/StunServerTransaction.java | 2 +- .../NetworkConfigurationDiscoveryProcess.java | 3 +- .../stunclient/SimpleAddressDetector.java | 185 +++++------------- 10 files changed, 140 insertions(+), 170 deletions(-) diff --git a/src/org/ice4j/TransportAddress.java b/src/org/ice4j/TransportAddress.java index 86bbc8f..5c0523b 100644 --- a/src/org/ice4j/TransportAddress.java +++ b/src/org/ice4j/TransportAddress.java @@ -76,6 +76,21 @@ public class TransportAddress this.transport = transport; } + /** + * Creates an address instance from an InetSocketAddress. + * + * @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 + * null. + */ + 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. @@ -152,4 +167,27 @@ public class TransportAddress { return transport; } + + /** + * Compares this object against the specified object. The result is + * true if and only if the argument is not null and it + * represents the same address. + *

+ * Two instances of TransportAddress represent the same + * address if both the InetAddresses (or hostnames if it is unresolved), + * port numbers, and Transports are equal. + * + * If both addresses are unresolved, then the hostname, the port & and + * the Transport are compared. + * + * @param obj the object to compare against. + * @return true if the objects are the same and + * false otherwise. + * @see java.net.InetAddress#equals(java.lang.Object) + */ + public boolean equalsTransportAddress(Object obj) + { + return super.equals(obj) + &&( ((TransportAddress)obj).getTransport() == getTransport() ); + } } diff --git a/src/org/ice4j/attribute/AddressAttribute.java b/src/org/ice4j/attribute/AddressAttribute.java index d77f75f..00ce3e1 100644 --- a/src/org/ice4j/attribute/AddressAttribute.java +++ b/src/org/ice4j/attribute/AddressAttribute.java @@ -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. - * See terms of license at gnu.org. + * Distributable under LGPL license. See terms of license at gnu.org. */ package org.ice4j.attribute; -import java.util.Arrays; import java.net.*; import org.ice4j.*; @@ -14,7 +13,7 @@ import org.ice4j.*; /** * This class is used to represent Stun attributes that contain an address. Such * attributes are: - * + *

* MAPPED-ADDRESS
* RESPONSE-ADDRESS
* SOURCE-ADDRESS
@@ -23,14 +22,16 @@ import org.ice4j.*; * ALTERNATE-SERVER
* XOR-PEER-ADDRESS
* XOR-RELAYED-ADDRESS
- * + *

+ *

* The different attributes are distinguished by the attributeType of * org.ice4j.attribute.Attribute. - * + *

+ *

* Address attributes indicate the mapped IP address and * port. They consist of an eight bit address family, and a sixteen bit * port, followed by a fixed length value representing the IP address. - * + * * 0 1 2 3
* 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -39,21 +40,26 @@ import org.ice4j.*; * | Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
+ *

+ *

* The port is a network byte ordered representation of the mapped port. * The address family is always 0x01, corresponding to IPv4. The first * 8 bits of the MAPPED-ADDRESS are ignored, for the purposes of * aligning parameters on natural boundaries. The IPv4 address is 32 * bits. - * - * - *

Organisation: Louis Pasteur University, Strasbourg, France

- *

Network Research Team (http://www-r2.u-strasbg.fr)

+ *

* @author Emil Ivov - * @version 0.1 */ abstract class AddressAttribute extends Attribute { + /** + * Indicates that this attribute is transporting an IPv4 address + */ static final byte ADDRESS_FAMILY_IPV4 = 0x01; + + /** + * Indicates that this attribute is transporting an IPv6 address + */ static final byte ADDRESS_FAMILY_IPV6 = 0x02; /** @@ -105,7 +111,8 @@ abstract class AddressAttribute extends Attribute protected void setAttributeType(char 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); } @@ -187,7 +194,8 @@ abstract class AddressAttribute extends Attribute { char type = getAttributeType(); 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()]; //Type @@ -250,7 +258,7 @@ abstract class AddressAttribute extends Attribute */ public byte getFamily() { - if ( address.getSocketAddress().getAddress() instanceof Inet6Address ) + if ( address.getAddress() instanceof Inet6Address ) return ADDRESS_FAMILY_IPV6; else return ADDRESS_FAMILY_IPV4; @@ -287,7 +295,8 @@ abstract class AddressAttribute extends Attribute byte family = attributeValue[offset++]; //port - char port = ((char)((attributeValue[offset++] << 8 ) | (attributeValue[offset++]&0xFF) )); + char port = ((char)((attributeValue[offset++] << 8 ) + | (attributeValue[offset++]&0xFF) )); //address byte address[] = null; @@ -300,7 +309,14 @@ abstract class AddressAttribute extends Attribute } 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); + } } diff --git a/src/org/ice4j/attribute/XorMappedAddressAttribute.java b/src/org/ice4j/attribute/XorMappedAddressAttribute.java index d73995f..3acb9d8 100644 --- a/src/org/ice4j/attribute/XorMappedAddressAttribute.java +++ b/src/org/ice4j/attribute/XorMappedAddressAttribute.java @@ -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. - * See terms of license at gnu.org. + * Distributable under LGPL license. See terms of license at gnu.org. */ package org.ice4j.attribute; +import java.net.*; + import org.ice4j.*; /** @@ -68,8 +70,10 @@ public class XorMappedAddressAttribute /** * Returns the result of applying XOR on the specified attribute's address. * The method may be used for both encoding and decoding XorMappedAddresses. + * * @param address the address on which XOR should be applied * @param transactionID the transaction id to use for the XOR + * * @return the XOR-ed address. */ public static TransportAddress applyXor(TransportAddress address, @@ -86,7 +90,16 @@ public class XorMappedAddressAttribute for(int i = 0; i < addressBytes.length; 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; } diff --git a/src/org/ice4j/ice/Candidate.java b/src/org/ice4j/ice/Candidate.java index dc1afaf..ecca46e 100644 --- a/src/org/ice4j/ice/Candidate.java +++ b/src/org/ice4j/ice/Candidate.java @@ -397,7 +397,7 @@ public class Candidate 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 //bit tricky since it is not meant to be used exactly the way that diff --git a/src/org/ice4j/ice/HostCandidate.java b/src/org/ice4j/ice/HostCandidate.java index 4601cd7..99c914a 100644 --- a/src/org/ice4j/ice/HostCandidate.java +++ b/src/org/ice4j/ice/HostCandidate.java @@ -46,7 +46,7 @@ public class HostCandidate extends Candidate Component parentComponent) { super(new TransportAddress(socket.getLocalAddress(), - socket.getLocalPort()), + socket.getLocalPort(), Transport.UDP), parentComponent, CandidateType.HOST_CANDIDATE); diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index f1c994e..27128a9 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -8,8 +8,6 @@ package org.ice4j.ice.harvest; import org.ice4j.ice.*; -import org.ice4j.stack.*; -import org.ice4j.stunclient.*; /** * A StunCandidateHarvester gathers STUN Candidates for a @@ -30,7 +28,6 @@ public class StunCandidateHarvester public void harvest(Component component) { //todo implement - StunClient client = new StunClient(stack); } diff --git a/src/org/ice4j/stack/NetAccessPoint.java b/src/org/ice4j/stack/NetAccessPoint.java index 45c1fa2..822c12f 100644 --- a/src/org/ice4j/stack/NetAccessPoint.java +++ b/src/org/ice4j/stack/NetAccessPoint.java @@ -194,7 +194,7 @@ class NetAccessPoint throws IOException { DatagramPacket datagramPacket = new DatagramPacket( - message, 0, message.length, address.getSocketAddress()); + message, 0, message.length, address); synchronized(sock) { diff --git a/src/org/ice4j/stack/StunServerTransaction.java b/src/org/ice4j/stack/StunServerTransaction.java index 22c4c40..1967586 100644 --- a/src/org/ice4j/stack/StunServerTransaction.java +++ b/src/org/ice4j/stack/StunServerTransaction.java @@ -176,7 +176,7 @@ class StunServerTransaction * access point that had not been installed, * @throws StunException if message encoding fails, */ - private void retransmitResponse() + protected void retransmitResponse() throws StunException, IOException, IllegalArgumentException diff --git a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java index afce620..ecdd94b 100644 --- a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java +++ b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java @@ -167,8 +167,7 @@ public class NetworkConfigurationDiscoveryProcess { stunStack = StunStack.getInstance(); - DatagramSocket sock - = new DatagramSocket(localAddress.getSocketAddress()); + DatagramSocket sock = new DatagramSocket(localAddress); stunStack.installNetAccessPoint(sock); diff --git a/src/org/ice4j/stunclient/SimpleAddressDetector.java b/src/org/ice4j/stunclient/SimpleAddressDetector.java index 0b3cc83..a4d7e98 100644 --- a/src/org/ice4j/stunclient/SimpleAddressDetector.java +++ b/src/org/ice4j/stunclient/SimpleAddressDetector.java @@ -97,141 +97,65 @@ public class SimpleAddressDetector /** - * Creates a listening point from the following address and attempts to - * discover how it is mapped so that using inside the application is - * possible. - * - * @param address the [address]:[port] pair where ther request should be - * sent from. - * + * 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 address is already bound. + * + * @throws IOException if something fails along the way. + * @throws BindException if we cannot bind the socket. */ - public TransportAddress getMappingFor(TransportAddress address) - throws IOException + public TransportAddress getMappingFor(DatagramSocket socket) + 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); - StunMessageEvent evt = null; - try - { - try - { - evt = requestSender.sendRequestAndWaitForResponse( - MessageFactory.createBindingRequest(), serverAddress); - } - catch (StunException ex) - { - //a stun exception is thrown if there is a problem with - //the message. The message here is constructed by ourselves - //so we don't need to tell the user ... just log and throw - //a runtime exception - logger.log( Level.SEVERE, - "Internal Error. Apparently we did not properly " - +"construct a message", - ex); - } - } - finally - { - //free the port to allow the application to use it. - stunStack.removeNetAccessPoint(apDesc); - } + requestSender = new BlockingRequestSender(stunProvider, localAddress); + 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(localAddress); + } 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 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); + /* 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]; - 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 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); + System.arraycopy(Message.MAGIC_COOKIE, 0, xoring, 0, 4); + System.arraycopy(res.getTransactionID(), 0, xoring, 4, 12); return xorAtt.applyXor(xoring); } @@ -239,21 +163,4 @@ public class SimpleAddressDetector 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 port is already bound. - */ - public TransportAddress getMappingFor(int port) - throws IOException - { - return getMappingFor(new TransportAddress(port)); - } }