From 026bfff9959f36688f4e5dfcc3b3c400f13139ad Mon Sep 17 00:00:00 2001 From: boris Date: Fri, 29 Aug 2014 08:27:56 +0000 Subject: [PATCH] Saves TCP sockets in NetAccessManager on the basis of both the local and remote address, in order to allow multiple Sockets/Connectors for a single local address. --- src/org/ice4j/stack/Connector.java | 37 +++- src/org/ice4j/stack/NetAccessManager.java | 200 ++++++++++++++++------ src/org/ice4j/stack/StunStack.java | 52 ++++-- 3 files changed, 219 insertions(+), 70 deletions(-) diff --git a/src/org/ice4j/stack/Connector.java b/src/org/ice4j/stack/Connector.java index 6ccd97b..4cef317 100644 --- a/src/org/ice4j/stack/Connector.java +++ b/src/org/ice4j/stack/Connector.java @@ -62,6 +62,12 @@ class Connector */ private final TransportAddress listenAddress; + /** + * The remote address of the socket of this Connector if it is + * a TCP socket, or null if it is UDP. + */ + private final TransportAddress remoteAddress; + /** * Creates a network access point. * @param socket the socket that this access point is supposed to use for @@ -76,9 +82,19 @@ class Connector this.sock = socket; this.messageQueue = messageQueue; this.errorHandler = errorHandler; - this.listenAddress = new TransportAddress(socket.getLocalAddress(), - socket.getLocalPort(), socket.getUDPSocket() != null ? - Transport.UDP : Transport.TCP); + + Transport transport + = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP; + this.listenAddress + = new TransportAddress(socket.getLocalAddress(), + socket.getLocalPort(), + transport); + this.remoteAddress + = transport == Transport.UDP + ? null + : new TransportAddress(socket.getTCPSocket().getInetAddress(), + socket.getTCPSocket().getPort(), + transport); } /** @@ -198,7 +214,8 @@ class Connector { logger.log( Level.WARNING, - "Connector died: " + listenAddress, + "Connector died: " + listenAddress + " -> " + + remoteAddress, ex); stop(); @@ -292,4 +309,16 @@ class Connector { return listenAddress; } + + /** + * Returns the remote TransportAddress in case of TCP, or + * null in case of UDP. + * + * @return the remote TransportAddress in case of TCP, or + * null in case of UDP. + */ + TransportAddress getRemoteAddress() + { + return remoteAddress; + } } diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index ea87282..dec0177 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -7,6 +7,7 @@ package org.ice4j.stack; import java.io.*; +import java.net.*; import java.util.*; import java.util.logging.*; @@ -23,6 +24,7 @@ import org.ice4j.socket.*; * * @author Emil Ivov * @author Aakash Garg + * @author Boris Grozev */ class NetAccessManager implements ErrorHandler @@ -34,7 +36,14 @@ class NetAccessManager = Logger.getLogger(NetAccessManager.class.getName()); /** - * All access points currently in use with UDP. The table maps + * Synchronization root for access to {@link #netUDPAccessPoints} and + * {@link #netTCPAccessPoints} and the lists contained in + * {@link #netTCPAccessPoints}. + */ + private final Object connectorsSyncRoot = new Object(); + + /** + * All access points currently in use with UDP. The table maps local * TransportAddresses to Connectors. * * Due to the final hashCode() method of InetSocketAddress, TransportAddress @@ -46,16 +55,18 @@ class NetAccessManager = new Hashtable(); /** - * All access points currently in use with TCP. The table maps - * TransportAddresses to Connectors. + * All access points currently in use with TCP. The table maps local + * TransportAddresses to a list of Connectors with that + * local address. To find the required Connector in the list, the + * remote address needs to be used. * * Due to the final hashCode() method of InetSocketAddress, TransportAddress * cannot override and it causes problem to store both UDP and TCP address * (i.e. 192.168.0.3:5000/tcp has the same hashcode as 192.168.0.3:5000/udp * because InetSocketAddress does not take into account transport). */ - private Map netTCPAccessPoints - = new Hashtable(); + private Map> netTCPAccessPoints + = new Hashtable>(); /** * A synchronized FIFO where incoming messages are stocked for processing. @@ -235,7 +246,8 @@ class NetAccessManager Connector ap = (Connector)callingThread; //make sure nothing's left and notify user - removeSocket(ap.getListenAddress()); + removeSocket(ap.getListenAddress(), + ap.getRemoteAddress()); logger.log(Level.WARNING, "Removing connector:" + ap, error); } else if( callingThread instanceof MessageProcessor ) @@ -265,56 +277,119 @@ class NetAccessManager protected void addSocket(IceSocketWrapper socket) { //no null check - let it through as a NullPointerException + Transport transport + = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP; TransportAddress localAddr = new TransportAddress( socket.getLocalAddress(), socket.getLocalPort(), - socket.getUDPSocket() != null ? Transport.UDP : - Transport.TCP); + transport); - if (socket.getUDPSocket() != null && - !netUDPAccessPoints.containsKey(localAddr)) + synchronized (connectorsSyncRoot) { - Connector ap = new Connector(socket, messageQueue, this); + switch (transport) + { + case UDP: + if (!netUDPAccessPoints.containsKey(localAddr)) + { + Connector connector + = new Connector(socket, messageQueue, this); - netUDPAccessPoints.put(localAddr, ap); - ap.start(); + netUDPAccessPoints.put(localAddr, connector); + connector.start(); + } + break; + case TCP: + List connectors = netTCPAccessPoints.get(localAddr); + if (connectors == null) + { + connectors = new LinkedList(); + netTCPAccessPoints.put(localAddr, connectors); + } + + Connector connector + = findTCPConnectorByRemoteAddress( + connectors, + new TransportAddress( + socket.getTCPSocket().getInetAddress(), + socket.getTCPSocket().getPort(), + Transport.TCP)); + if (connector == null) + { + connector = new Connector(socket, messageQueue, this); + + connectors.add(connector); + connector.start(); + } + } + } + } + + /** + * From a list of Connectors with TCP sockets finds the one with + * remote address equal to dst. + * @param list the list of Connectors to search. + * @param dst the remote address of the connector to search for. + * @return the first Connector in list with a remote + * address equal to dst. + */ + private Connector findTCPConnectorByRemoteAddress( + List list, + TransportAddress dst) + { + if (dst == null) + return null; + + for (Connector connector : list) + { + if (dst.equals(connector.getRemoteAddress())) + { + return connector; + } } - if (socket.getTCPSocket() != null && - !netTCPAccessPoints.containsKey(localAddr)) - { - Connector ap = new Connector(socket, messageQueue, this); - - netTCPAccessPoints.put(localAddr, ap); - ap.start(); - } + return null; } /** * Stops and deletes the specified access point. * - * @param address the address of the connector to remove. + * @param src the local address of the connector to remove. + * @param dst the remote address of the connector to remote. For TCP only, + * use null for UDP. */ - protected void removeSocket(TransportAddress address) + protected void removeSocket(TransportAddress src, + TransportAddress dst) { - Connector ap; + Connector connector = null; - switch (address.getTransport()) + synchronized (connectorsSyncRoot) { - case TCP: - ap = netTCPAccessPoints.remove(address); - break; - case UDP: - ap = netUDPAccessPoints.remove(address); - break; - default: - ap = null; - break; + switch (src.getTransport()) + { + case UDP: + connector = netUDPAccessPoints.remove(src); + break; + case TCP: + List connectors = netTCPAccessPoints.remove(src); + if (connectors != null) + { + connector + = findTCPConnectorByRemoteAddress(connectors, dst); + + if (connector != null) + { + connectors.remove(connector); + if (connectors.isEmpty()) + netTCPAccessPoints.remove(src); + } + } + break; + } } - if(ap != null) - ap.stop(); + if(connector != null) + connector.stop(); } /** @@ -404,6 +479,37 @@ class NetAccessManager } } + /** + * Returns the Connector responsible for a particular source + * address and a particular destination address. + * + * @param src the source address. + * @param dst the destination address. + * Returns the Connector responsible for a particular source + * address and a particular destination address, or null if there's + * none. + */ + private Connector getConnector(TransportAddress src, + TransportAddress dst) + { + synchronized (connectorsSyncRoot) + { + switch (src.getTransport()) + { + case UDP: + return netUDPAccessPoints.get(src); + case TCP: + List connectors = netTCPAccessPoints.get(src); + if (connectors != null) + { + return findTCPConnectorByRemoteAddress(connectors, dst); + } + } + } + + return null; + } + //--------------- SENDING MESSAGES ----------------------------------------- /** * Sends the specified stun message through the specified access point. @@ -426,16 +532,11 @@ class NetAccessManager { byte[] bytes = stunMessage.encode(stunStack); - Connector ap = null; - - if (srcAddr.getTransport() == Transport.UDP) - ap = netUDPAccessPoints.get(srcAddr); - else if (srcAddr.getTransport() == Transport.TCP) - ap = netTCPAccessPoints.get(srcAddr); + Connector ap = getConnector(srcAddr, remoteAddr); if (ap == null) { throw new IllegalArgumentException( - "No socket has been added for source address: " + srcAddr); + "No socket has been added for: " + srcAddr + " <-> " + remoteAddr); } ap.sendMessage(bytes, remoteAddr); @@ -463,12 +564,7 @@ class NetAccessManager { byte[] bytes = channelData.encode(); - Connector ap = null; - - if (srcAddr.getTransport() == Transport.UDP) - ap = netUDPAccessPoints.get(srcAddr); - else if (srcAddr.getTransport() == Transport.TCP) - ap = netTCPAccessPoints.get(srcAddr); + Connector ap = getConnector(srcAddr, remoteAddr); if (ap == null) { throw new IllegalArgumentException( @@ -498,11 +594,7 @@ class NetAccessManager throws IllegalArgumentException, IOException, StunException { - Connector ap = null; - if (srcAddr.getTransport() == Transport.UDP) - ap = netUDPAccessPoints.get(srcAddr); - else if (srcAddr.getTransport() == Transport.TCP) - ap = netTCPAccessPoints.get(srcAddr); + Connector ap = getConnector(srcAddr, remoteAddr); if (ap == null) { throw new IllegalArgumentException( diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 8f2a17a..d995a10 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -122,15 +122,32 @@ public class StunStack /** * Stops and deletes the connector listening on the specified local address. + * Note this removes connectors with UDP sockets only, use + * {@link #removeSocket(org.ice4j.TransportAddress, org.ice4j.TransportAddress)} + * with the appropriate remote address for TCP. * - * @param localAddr the access point to remove + * @param localAddr the local address of the socket to remove. */ public void removeSocket(TransportAddress localAddr) { - //first cancel all transactions using this address. - cancelTransactionsForAddress(localAddr); + removeSocket(localAddr, null); + } - netAccessManager.removeSocket(localAddr); + /** + * Stops and deletes the connector listening on the specified local address + * and remote address. + * + * @param localAddr the local address of the socket to remove. + * @param remoteAddr the remote address of the socket to remove. Use + * null for UDP. + */ + public void removeSocket(TransportAddress localAddr, + TransportAddress remoteAddr) + { + //first cancel all transactions using this address. + cancelTransactionsForAddress(localAddr, remoteAddr); + + netAccessManager.removeSocket(localAddr, remoteAddr); } /** @@ -239,8 +256,13 @@ public class StunStack * * @param localAddr the TransportAddress that we'd like to remove * transactions for. + * @param remoteAddr the remote TransportAddress that we'd like to + * remove transactions for. If null, then it will not be taken + * into account (that is, all transactions with for localAddr will + * be cancelled). */ - private void cancelTransactionsForAddress(TransportAddress localAddr) + private void cancelTransactionsForAddress(TransportAddress localAddr, + TransportAddress remoteAddr) { List clientTransactionsToCancel = null; @@ -253,7 +275,9 @@ public class StunStack { StunClientTransaction tran = clientTransactionsIter.next(); - if (tran.getLocalAddress().equals(localAddr)) + if (tran.getLocalAddress().equals(localAddr) + && (remoteAddr == null + || remoteAddr.equals(tran.getRemoteAddress()))) { clientTransactionsIter.remove(); @@ -299,14 +323,18 @@ public class StunStack || (sendingAddr != null && sendingAddr.equals(localAddr))) { - serverTransactionsIter.remove(); - - if (serverTransactionsToExpire == null) + if (remoteAddr == null + || remoteAddr.equals(tran.getRequestSourceAddress())) { - serverTransactionsToExpire - = new LinkedList(); + serverTransactionsIter.remove(); + + if (serverTransactionsToExpire == null) + { + serverTransactionsToExpire + = new LinkedList(); + } + serverTransactionsToExpire.add(tran); } - serverTransactionsToExpire.add(tran); } } }