diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java new file mode 100644 index 0000000..2a78b86 --- /dev/null +++ b/src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java @@ -0,0 +1,1152 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * + * Copyright @ 2015 Atlassian Pty Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ice4j.ice.harvest; + +import org.ice4j.*; +import org.ice4j.ice.*; +import org.ice4j.message.*; +import org.ice4j.socket.*; + +import java.io.*; +import java.net.*; +import java.nio.*; +import java.nio.channels.*; +import java.util.*; +import java.util.logging.*; + +/** + * An abstract class that binds on a set of sockets and accepts sessions that + * start with a STUN Binding Request (preceded by an optional fake SSL + * handshake). The handling of the accepted sessions (e.g. handling in ICE) is + * left to the implementations. + * + * This instance runs two threads: {@link #acceptThread} and + * {@link #readThread}. The 'accept' thread just accepts new Sockets + * and passes them over to the 'read' thread. The 'read' thread reads a STUN + * message from an accepted socket and, based on the STUN username, passes it + * to the appropriate session. + * + * @author Boris Grozev + * @author Lyubomir Marinov + */ +public abstract class AbstractTcpListener +{ + /** + * Our class logger. + */ + private static final Logger logger + = Logger.getLogger(AbstractTcpListener.class.getName()); + + /** + * Closes a {@code Channel} and swallows any {@link IOException}. + * + * @param channel the {@code Channel} to close + */ + static void closeNoExceptions(Channel channel) + { + MuxServerSocketChannelFactory.closeNoExceptions(channel); + } + + /** + * Returns a list of all addresses on the interfaces in interfaces + * which are found suitable for candidate allocations (are not loopback, are + * up, and are allowed by the configuration). + * + * @param port the port to use. + * @param interfaces the list of interfaces to use. + */ + private static List getLocalAddresses( + int port, + List interfaces) + throws IOException + { + List addresses = new LinkedList<>(); + + for (NetworkInterface iface : interfaces) + { + if (NetworkUtils.isInterfaceLoopback(iface) + || !NetworkUtils.isInterfaceUp(iface) + || !HostCandidateHarvester.isInterfaceAllowed(iface)) + { + //this one is obviously not going to do + continue; + } + + Enumeration ifaceAddresses = iface.getInetAddresses(); + + while(ifaceAddresses.hasMoreElements()) + { + InetAddress addr = ifaceAddresses.nextElement(); + + addresses.add(new TransportAddress(addr, port, Transport.TCP)); + } + } + return addresses; + } + + /** + * Determines whether a specific {@link DatagramPacket} is the first + * expected (i.e. supported) to be received from an accepted + * {@link SocketChannel} by this {@link AbstractTcpListener}. This is true + * if it is contains the hard-coded SSL client handshake ( + * {@link GoogleTurnSSLCandidateHarvester#SSL_CLIENT_HANDSHAKE}), or + * a STUN Binding Request. + * + * @param p the {@code DatagramPacket} to examine + * @return {@code true} if {@code p} looks like the first + * {@code DatagramPacket} expected to be received from an accepted + * {@code SocketChannel} by this {@code TcpHarvester}; + * otherwise, {@code false} + */ + private static boolean isFirstDatagramPacket(DatagramPacket p) + { + int len = p.getLength(); + boolean b = false; + + if (len > 0) + { + byte[] buf = p.getData(); + int off = p.getOffset(); + + // Check for Google TURN SSLTCP + final byte[] googleTurnSslTcp + = GoogleTurnSSLCandidateHarvester.SSL_CLIENT_HANDSHAKE; + + if (len >= googleTurnSslTcp.length) + { + b = true; + for (int i = 0, iEnd = googleTurnSslTcp.length, j = off; + i < iEnd; + i++, j++) + { + if (googleTurnSslTcp[i] != buf[j]) + { + b = false; + break; + } + } + } + + // nothing found, lets check for stun binding requests + if (!b) + { + // 2 bytes uint16 length + // STUN Binding request: + // 2 bits 00 + // 14 bits STUN Messsage Type + // 2 bytes Message Length + // 4 bytes Magic Cookie + + // RFC 5389: For example, a Binding request has class=0b00 + // (request) and method=0b000000000001 (Binding) and is encoded + // into the first 16 bits as 0x0001. + if (len >= 10 && buf[off + 2] == 0 && buf[off + 3] == 1) + { + final byte[] magicCookie = Message.MAGIC_COOKIE; + + b = true; + for (int i = 0, iEnd = magicCookie.length, j = off + 6; + i < iEnd; + i++, j++) + { + if (magicCookie[i] != buf[j]) + { + b = false; + break; + } + } + } + } + } + return b; + } + + /** + * The thread which accepts TCP connections from the sockets in + * {@link #serverSocketChannels}. + */ + private AcceptThread acceptThread; + + /** + * Triggers the termination of the threads of this instance. + */ + private boolean close = false; + + /** + * The list of transport addresses which we have found to be listening on, + * and which may be, for example, advertises as ICE candidates. + */ + protected final List localAddresses = new LinkedList<>(); + + /** + * Channels pending to be added to the list that {@link #readThread} reads + * from. + */ + private final List newChannels = new LinkedList<>(); + + /** + * The Selector used by {@link #readThread}. + */ + private final Selector readSelector = Selector.open(); + + /** + * The thread which reads from the already accepted sockets. + */ + private ReadThread readThread; + + /** + * The list of ServerSocketChannels that we will accept + * on. + */ + private final List serverSocketChannels + = new LinkedList<>(); + + /** + * The object used to synchronize access to the collection of sessions that + * the implementation of this class uses. + */ + protected final Object sessionsSyncRoot = new Object(); + + /** + * Initializes a new TcpHarvester, which is to + * listen on port number port on all IP addresses on all available + * interfaces. + * + * @param port the port to listen on. + * @throws IOException when {@link StackProperties#ALLOWED_ADDRESSES} or + * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values, or + * if an I/O error occurs. + */ + public AbstractTcpListener(int port) + throws IOException + { + this(port, Collections.list(NetworkInterface.getNetworkInterfaces())); + } + + /** + * Initializes a new TcpHarvester, which is to listen on port + * number port on all the IP addresses on the specified + * NetworkInterfaces. + * + * @param port the port to listen on. + * @param interfaces the interfaces to listen on. + * @throws IOException when {@link StackProperties#ALLOWED_ADDRESSES} or + * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values, or + * if an I/O error occurs. + */ + public AbstractTcpListener(int port, List interfaces) + throws IOException + { + this(getLocalAddresses(port, interfaces)); + } + + /** + * Initializes a new TcpHarvester, which is to listen on the + * specified list of TransportAddresses. + * + * @param transportAddresses the transport addresses to listen on. + * @throws IOException when {@link StackProperties#ALLOWED_ADDRESSES} or + * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values, or + * if an I/O error occurs. + */ + public AbstractTcpListener(List transportAddresses) + throws IOException + { + addLocalAddresses(transportAddresses); + init(); + } + + /** + * Adds to {@link #localAddresses} those addresses from + * transportAddresses which are found suitable for candidate + * allocation. + * + * @param transportAddresses the list of addresses to add. + * @throws IOException when {@link StackProperties#ALLOWED_ADDRESSES} or + * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values. + */ + protected void addLocalAddresses(List transportAddresses) + throws IOException + { + boolean useIPv6 = !StackProperties.getBoolean( + StackProperties.DISABLE_IPv6, + false); + boolean useIPv6LinkLocal = !StackProperties.getBoolean( + StackProperties.DISABLE_LINK_LOCAL_ADDRESSES, + false); + + // White list from the configuration + String[] allowedAddressesStr + = StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES, + ";"); + InetAddress[] allowedAddresses = null; + + if (allowedAddressesStr != null) + { + allowedAddresses = new InetAddress[allowedAddressesStr.length]; + for (int i = 0; i < allowedAddressesStr.length; i++) + { + allowedAddresses[i] + = InetAddress.getByName(allowedAddressesStr[i]); + } + } + + // Black list from the configuration + String[] blockedAddressesStr + = StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES, + ";"); + InetAddress[] blockedAddresses = null; + + if (blockedAddressesStr != null) + { + blockedAddresses = new InetAddress[blockedAddressesStr.length]; + for (int i = 0; i < blockedAddressesStr.length; i++) + { + blockedAddresses[i] + = InetAddress.getByName(blockedAddressesStr[i]); + } + } + + for (TransportAddress transportAddress : transportAddresses) + { + InetAddress address = transportAddress.getAddress(); + + if (address.isLoopbackAddress()) + { + //loopback again + continue; + } + + if (!useIPv6 && (address instanceof Inet6Address)) + continue; + + if (!useIPv6LinkLocal + && (address instanceof Inet6Address) + && address.isLinkLocalAddress()) + { + logger.info("Not using link-local address " + address +" for" + + " TCP candidates."); + continue; + } + + if (allowedAddresses != null) + { + boolean found = false; + + for (InetAddress allowedAddress : allowedAddresses) + { + if (allowedAddress.equals(address)) + { + found = true; + break; + } + } + if (!found) + { + logger.info("Not using " + address +" for TCP candidates, " + + "because it is not in the allowed list."); + continue; + } + } + + if (blockedAddresses != null) + { + boolean found = false; + + for (InetAddress blockedAddress : blockedAddresses) + { + if (blockedAddress.equals(address)) + { + found = true; + break; + } + } + if (found) + { + logger.info("Not using " + address + " for TCP candidates, " + + "because it is in the blocked list."); + continue; + } + } + + // Passed all checks + localAddresses.add(transportAddress); + } + } + + /** + * Triggers the termination of the threads of this + * MultiplexingTcpHarvester. + */ + public void close() + { + close = true; + } + + /** + * Initializes {@link #serverSocketChannels}, creates and starts the threads + * used by this instance. + * @throws IOException if an I/O error occurs + */ + protected void init() + throws IOException + { + for (TransportAddress transportAddress : localAddresses) + { + ServerSocketChannel channel + = MuxServerSocketChannelFactory + .openAndBindMuxServerSocketChannel( + /* properties */ null, + new InetSocketAddress( + transportAddress.getAddress(), + transportAddress.getPort()), + /* backlog */ 0, + new DatagramPacketFilter() + { + /** + * {@inheritDoc} + */ + @Override + public boolean accept(DatagramPacket p) + { + return isFirstDatagramPacket(p); + } + }); + + serverSocketChannels.add(channel); + } + + acceptThread = new AcceptThread(); + acceptThread.start(); + + readThread = new ReadThread(); + readThread.start(); + } + + /** + * Accepts a session. + * @param socket the {@link Socket} for the session. + * @param ufrag the local username fragment for the session. + * @param pushback the first "datagram" (RFC4571-framed), already read from + * the socket's stream. + * @throws IllegalStateException + * @throws IOException + */ + protected abstract void acceptSession( + Socket socket, String ufrag, DatagramPacket pushback) + throws IOException, IllegalStateException; + + /** + * A Thread which will accept new SocketChannels from all + * ServerSocketChannels in {@link #serverSocketChannels}. + */ + private class AcceptThread + extends Thread + { + /** + * The Selector used to select a specific + * ServerSocketChannel which is ready to accept. + */ + private final Selector selector; + + /** + * Initializes a new AcceptThread. + */ + public AcceptThread() + throws IOException + { + setName("TcpHarvester AcceptThread"); + setDaemon(true); + + selector = Selector.open(); + for (ServerSocketChannel channel : serverSocketChannels) + { + channel.configureBlocking(false); + channel.register(selector, SelectionKey.OP_ACCEPT); + } + } + + /** + * Notifies {@link #readThread} that new channels have been added. + */ + private void notifyReadThread() + { + readSelector.wakeup(); + } + + /** + * {@inheritDoc} + */ + @Override + public void run() + { + do + { + if (close) + { + break; + } + + IOException exception = null; + List channelsToAdd = new LinkedList<>(); + // Allow to go on, so we can quit if closed. + long selectTimeout = 3000; + + for (SelectionKey key : selector.keys()) + { + if (key.isValid()) + { + SocketChannel channel; + boolean acceptable = key.isAcceptable(); + + try + { + channel + = ((ServerSocketChannel) key.channel()) + .accept(); + } + catch (IOException ioe) + { + exception = ioe; + break; + } + + // Add the accepted channel to newChannels to allow the + // 'read' thread to it up. + if (channel != null) + { + channelsToAdd.add(channel); + } + else if (acceptable) + { + // The SelectionKey reported the channel as + // acceptable but channel#accept() did not accept a + // non-null SocketChannel. Give the channel a little + // time to get its act together. + selectTimeout = 100; + } + } + } + // We accepted from all serverSocketChannels. + selector.selectedKeys().clear(); + + if (!channelsToAdd.isEmpty()) + { + synchronized (newChannels) + { + newChannels.addAll(channelsToAdd); + } + notifyReadThread(); + } + + if (exception != null) + { + logger.info( + "Failed to accept a socket, which should have been" + + " ready to accept: " + exception); + break; + } + + try + { + // Allow to go on, so we can quit if closed. + selector.select(selectTimeout); + } + catch (IOException ioe) + { + logger.info( + "Failed to select an accept-ready socket: " + ioe); + break; + } + } + while (true); + + //now clean up and exit + for (ServerSocketChannel serverSocketChannel : serverSocketChannels) + closeNoExceptions(serverSocketChannel); + + try + { + selector.close(); + } + catch (IOException ioe) + {} + } + } + + /** + * Contains a SocketChannel that ReadThread is reading + * from. + */ + private static class ChannelDesc + { + /** + * The actual SocketChannel. + */ + public final SocketChannel channel; + + /** + * The time the channel was last found to be active. + */ + long lastActive = System.currentTimeMillis(); + + /** + * The buffer which stores the data so far read from the channel. + */ + ByteBuffer buffer = null; + + /** + * Whether we had checked for initial "pseudo" SSL handshake. + */ + boolean checkedForSSLHandshake = false; + + /** + * Buffer to use if we had read some data in advance and want to process + * it after next read, used when we are checking for "pseudo" SSL and + * we haven't found some, but had read data to check for it. + */ + byte[] preBuffered = null; + + /** + * The value of the RFC4571 "length" field read from the channel, or + * -1 if it hasn't been read (yet). + */ + int length = -1; + + /** + * Initializes a new ChannelDesc with the given channel. + * @param channel the channel. + */ + public ChannelDesc(SocketChannel channel) + { + this.channel = channel; + } + } + + /** + * An IceSocketWrapper implementation which allows a + * DatagramPacket to be pushed back and received on the first call + * to {@link #receive(DatagramPacket)}. + */ + protected static class PushBackIceSocketWrapper + extends IceSocketWrapper + { + /** + * The DatagramPacket which will be used on the first call to + * {@link #receive(DatagramPacket)}. + */ + private DatagramPacket datagramPacket; + + /** + * The IceSocketWrapper that this instance wraps around. + */ + private final IceSocketWrapper wrapped; + + /** + * Initializes a new PushBackIceSocketWrapper instance that + * wraps around wrappedWrapper and reads from + * datagramSocket on the first call to + * {@link #receive(DatagramPacket)} + * + * @param wrappedWrapper the IceSocketWrapper instance that we + * wrap around. + * @param datagramPacket the DatagramPacket which will be used + * on the first call to {@link #receive(DatagramPacket)} + */ + public PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper, + DatagramPacket datagramPacket) + { + this.wrapped = wrappedWrapper; + this.datagramPacket = datagramPacket; + } + + /** + * {@inheritDoc} + */ + @Override + public void close() + { + wrapped.close(); + } + + /** + * {@inheritDoc} + */ + @Override + public InetAddress getLocalAddress() + { + return wrapped.getLocalAddress(); + } + + /** + * {@inheritDoc} + */ + @Override + public int getLocalPort() + { + return wrapped.getLocalPort(); + } + + /** + * {@inheritDoc} + */ + @Override + public SocketAddress getLocalSocketAddress() + { + return wrapped.getLocalSocketAddress(); + } + + /** + * {@inheritDoc} + */ + @Override + public Socket getTCPSocket() + { + return wrapped.getTCPSocket(); + } + + /** + * {@inheritDoc} + */ + @Override + public DatagramSocket getUDPSocket() + { + return wrapped.getUDPSocket(); + } + + /** + * {@inheritDoc} + * + * On the first call to this instance reads from + * {@link #datagramPacket}, on subsequent calls delegates to + * {@link #wrapped}. + */ + @Override + public void receive(DatagramPacket p) throws IOException + { + if (datagramPacket != null) + { + int len = Math.min(p.getLength(), datagramPacket.getLength()); + System.arraycopy(datagramPacket.getData(), 0, + p.getData(), 0, + len); + p.setAddress(datagramPacket.getAddress()); + p.setPort(datagramPacket.getPort()); + datagramPacket = null; + } + else + { + wrapped.receive(p); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void send(DatagramPacket p) throws IOException + { + wrapped.send(p); + } + } + + private class ReadThread + extends Thread + { + /** + * Initializes a new ReadThread. + * + * @throws IOException if the selector to be used fails to open. + */ + public ReadThread() + throws IOException + { + setName("TcpHarvester ReadThread"); + setDaemon(true); + } + + /** + * Registers the channels from {@link #newChannels} in + * {@link #readSelector}. + */ + private void checkForNewChannels() + { + synchronized (newChannels) + { + for (SocketChannel channel : newChannels) + { + try + { + channel.configureBlocking(false); + channel.register( + readSelector, + SelectionKey.OP_READ, + new ChannelDesc(channel)); + } + catch (IOException ioe) + { + logger.info("Failed to register channel: " + ioe); + closeNoExceptions(channel); + } + } + newChannels.clear(); + } + } + + /** + * Closes any inactive channels registered with {@link #readSelector}. + * A channel is considered inactive if it hasn't been available for + * reading for + * {@link MuxServerSocketChannelFactory#SOCKET_CHANNEL_READ_TIMEOUT} + * milliseconds. + */ + private void cleanup() + { + long now = System.currentTimeMillis(); + + for (SelectionKey key : readSelector.keys()) + { + // An invalid key specifies that either the channel was closed + // (in which case we do not have to do anything else to it) or + // that we no longer control the channel (i.e. we do not want to + // do anything else to it). + if (!key.isValid()) + continue; + + ChannelDesc channelDesc = (ChannelDesc) key.attachment(); + + if (channelDesc == null) + continue; + + long lastActive = channelDesc.lastActive; + + if (lastActive != -1 + && now - lastActive + > MuxServerSocketChannelFactory + .SOCKET_CHANNEL_READ_TIMEOUT) + { + // De-register from the Selector. + key.cancel(); + + SocketChannel channel = channelDesc.channel; + + logger.info("Read timeout for socket: " + channel.socket()); + + closeNoExceptions(channel); + } + } + } + + /** + * Tries to read, without blocking, from channel to its + * buffer. If after reading the buffer is filled, handles the data in + * the buffer. + * + * This works in three stages: + * 1 (optional): Read a fixed-size message. If it matches the + * hard-coded pseudo SSL ClientHello, sends the hard-coded ServerHello. + * 2: Read two bytes as an unsigned int and interpret it as the length + * to read in the next stage. + * 3: Read number of bytes indicated in stage2 and try to interpret + * them as a STUN message. + * + * If a datagram is successfully read it is passed on to + * {@link #processFirstDatagram(byte[], ChannelDesc, SelectionKey)} + * + * @param channel the SocketChannel to read from. + * @param key the SelectionKey associated with + * channel, which is to be canceled in case no further + * reading is required from the channel. + */ + private void readFromChannel(ChannelDesc channel, SelectionKey key) + { + if (channel.buffer == null) + { + // Set up a buffer with a pre-determined size + + if (!channel.checkedForSSLHandshake && channel.length == -1) + { + channel.buffer + = ByteBuffer.allocate( + GoogleTurnSSLCandidateHarvester + .SSL_CLIENT_HANDSHAKE.length); + } + else if (channel.length == -1) + { + channel.buffer = ByteBuffer.allocate(2); + } + else + { + channel.buffer = ByteBuffer.allocate(channel.length); + } + } + + try + { + int read = channel.channel.read(channel.buffer); + + if (read == -1) + throw new IOException("End of stream!"); + else if (read > 0) + channel.lastActive = System.currentTimeMillis(); + + if (!channel.buffer.hasRemaining()) + { + // We've filled in the buffer. + if (!channel.checkedForSSLHandshake) + { + byte[] bytesRead + = new byte[GoogleTurnSSLCandidateHarvester + .SSL_CLIENT_HANDSHAKE.length]; + + channel.buffer.flip(); + channel.buffer.get(bytesRead); + + // Set to null, so that we re-allocate it for the next + // stage + channel.buffer = null; + channel.checkedForSSLHandshake = true; + + if (Arrays.equals(bytesRead, + GoogleTurnSSLCandidateHarvester + .SSL_CLIENT_HANDSHAKE)) + { + ByteBuffer byteBuffer = ByteBuffer.wrap( + GoogleTurnSSLCandidateHarvester + .SSL_SERVER_HANDSHAKE); + channel.channel.write(byteBuffer); + } + else + { + int fb = bytesRead[0]; + int sb = bytesRead[1]; + + channel.length = (((fb & 0xff) << 8) | (sb & 0xff)); + + byte[] preBuffered + = Arrays.copyOfRange( + bytesRead, 2, bytesRead.length); + + // if we had read enough data + if(channel.length <= bytesRead.length - 2) + { + processFirstDatagram( + preBuffered, channel, key); + } + else + { + // not enough data, store what was read + // and continue + channel.preBuffered = preBuffered; + + channel.length -= channel.preBuffered.length; + } + } + } + else if (channel.length == -1) + { + channel.buffer.flip(); + + int fb = channel.buffer.get(); + int sb = channel.buffer.get(); + + channel.length = (((fb & 0xff) << 8) | (sb & 0xff)); + + // Set to null, so that we re-allocate it for the next + // stage + channel.buffer = null; + } + else + { + byte[] bytesRead = new byte[channel.length]; + + channel.buffer.flip(); + channel.buffer.get(bytesRead); + + if(channel.preBuffered != null) + { + // will store preBuffered and currently read data + byte[] newBytesRead = new byte[ + channel.preBuffered.length + bytesRead.length]; + + // copy old data + System.arraycopy( + channel.preBuffered, 0, + newBytesRead, 0, + channel.preBuffered.length); + // and new data + System.arraycopy( + bytesRead, 0, + newBytesRead, channel.preBuffered.length, + bytesRead.length); + + // use that data for processing + bytesRead = newBytesRead; + + channel.preBuffered = null; + } + + processFirstDatagram(bytesRead, channel, key); + } + } + } + catch (IOException | IllegalStateException e) + { + logger.info( + "Failed to handle TCP socket " + + channel.channel.socket() + ": " + e.getMessage()); + key.cancel(); + closeNoExceptions(channel.channel); + } + } + + /** + * Process the first RFC4571-framed datagram read from a socket. + * + * If the datagram contains a STUN Binding Request, and it has a + * USERNAME attribute, the local "ufrag" is extracted from the + * attribute value, and the socket is passed to + * {@link #acceptSession(Socket, String, DatagramPacket)}. + * + * @param bytesRead bytes to be processed + * @param channel the SocketChannel to read from. + * @param key the SelectionKey associated with + * channel, which is to be canceled in case no further + * reading is required from the channel. + * @throws IOException if the datagram does not contain s STUN Binding + * Request with a USERNAME attribute. + * @throws IllegalStateException if the session for the extracted + * username fragment cannot be accepted for implementation reasons + * (e.g. no ICE Agent with the given local ufrag is found). + */ + private void processFirstDatagram( + byte[] bytesRead, + ChannelDesc channel, SelectionKey key) + throws IOException, IllegalStateException + { + // Does this look like a STUN binding request? + // What's the username? + String ufrag + = AbstractUdpListener.getUfrag(bytesRead, + (char) 0, + (char) bytesRead.length); + + if (ufrag == null) + { + throw new IOException("Cannot extract ufrag"); + } + + // The rest of the stack will read from the socket's + // InputStream. We cannot change the blocking mode + // before the channel is removed from the selector (by + // cancelling the key) + key.cancel(); + channel.channel.configureBlocking(true); + + // Construct a DatagramPacket from the just-read packet + // which is to be pushed back + DatagramPacket p + = new DatagramPacket(bytesRead, bytesRead.length); + Socket socket = channel.channel.socket(); + + p.setAddress(socket.getInetAddress()); + p.setPort(socket.getPort()); + + acceptSession(socket, ufrag, p); + } + + /** + * {@inheritDoc} + */ + @Override + public void run() + { + do + { + synchronized (AbstractTcpListener.this) + { + if (close) + break; + } + + // clean up stale channels + cleanup(); + + checkForNewChannels(); + + for (SelectionKey key : readSelector.keys()) + { + if (key.isValid()) + { + ChannelDesc channelDesc + = (ChannelDesc) key.attachment(); + + readFromChannel(channelDesc, key); + } + } + // We read from all SocketChannels. + readSelector.selectedKeys().clear(); + + try + { + readSelector.select( + MuxServerSocketChannelFactory + .SOCKET_CHANNEL_READ_TIMEOUT + / 2); + } + catch (IOException ioe) + { + logger.info("Failed to select a read-ready channel."); + } + } + while (true); + + //we are all done, clean up. + synchronized (newChannels) + { + for (SocketChannel channel : newChannels) + { + closeNoExceptions(channel); + } + newChannels.clear(); + } + + for (SelectionKey key : readSelector.keys()) + { + // An invalid key specifies that either the channel was closed + // (in which case we do not have to do anything else to it) or + // that we no longer control the channel (i.e. we do not want to + // do anything else to it). + if (key.isValid()) + { + Channel channel = key.channel(); + + if (channel.isOpen()) + closeNoExceptions(channel); + } + } + + try + { + readSelector.close(); + } + catch (IOException ioe) + {} + } + } +} diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java index 8145fe1..fa1afac 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java @@ -125,6 +125,80 @@ public abstract class AbstractUdpListener return addresses; } + /** + * Tries to parse the bytes in buf at offset off (and + * length len) as a STUN Binding Request message. If successful, + * looks for a USERNAME attribute and returns the local username fragment + * part (see RFC5245 Section 7.1.2.3). + * In case of any failure returns null. + * + * @param buf the bytes. + * @param off the offset. + * @param len the length. + * @return the local ufrag from the USERNAME attribute of the STUN message + * contained in buf, or null. + */ + static String getUfrag(byte[] buf, int off, int len) + { + // RFC5389, Section 6: + // All STUN messages MUST start with a 20-byte header followed by zero + // or more Attributes. + if (buf == null || buf.length < off + len || len < 20) + { + return null; + } + + // RFC5389, Section 6: + // The magic cookie field MUST contain the fixed value 0x2112A442 in + // network byte order. + if ( !( (buf[off + 4] & 0xFF) == 0x21 && + (buf[off + 5] & 0xFF) == 0x12 && + (buf[off + 6] & 0xFF) == 0xA4 && + (buf[off + 7] & 0xFF) == 0x42)) + { + if (logger.isLoggable(Level.FINE)) + { + logger.fine("Not a STUN packet, magic cookie not found."); + } + return null; + } + + try + { + Message stunMessage + = Message.decode(buf, + (char) off, + (char) len); + + if (stunMessage.getMessageType() + != Message.BINDING_REQUEST) + { + return null; + } + + UsernameAttribute usernameAttribute + = (UsernameAttribute) + stunMessage.getAttribute(Attribute.USERNAME); + if (usernameAttribute == null) + return null; + + String usernameString + = new String(usernameAttribute.getUsername()); + return usernameString.split(":")[0]; + } + catch (Exception e) + { + // Catch everything. We are going to log, and then drop the packet + // anyway. + if (logger.isLoggable(Level.FINE)) + { + logger.fine("Failed to extract local ufrag: " + e); + } + } + + return null; + } + /** * The map which keeps the known remote addresses and their associated * candidateSockets. @@ -286,80 +360,6 @@ public abstract class AbstractUdpListener return buf; } - /** - * Tries to parse the bytes in buf at offset off (and - * length len) as a STUN Binding Request message. If successful, - * looks for a USERNAME attribute and returns the local username fragment - * part (see RFC5245 Section 7.1.2.3). - * In case of any failure returns null. - * - * @param buf the bytes. - * @param off the offset. - * @param len the length. - * @return the local ufrag from the USERNAME attribute of the STUN message - * contained in buf, or null. - */ - private String getUfrag(byte[] buf, int off, int len) - { - // RFC5389, Section 6: - // All STUN messages MUST start with a 20-byte header followed by zero - // or more Attributes. - if (buf == null || buf.length < off + len || len < 20) - { - return null; - } - - // RFC5389, Section 6: - // The magic cookie field MUST contain the fixed value 0x2112A442 in - // network byte order. - if ( !( (buf[off + 4] & 0xFF) == 0x21 && - (buf[off + 5] & 0xFF) == 0x12 && - (buf[off + 6] & 0xFF) == 0xA4 && - (buf[off + 7] & 0xFF) == 0x42)) - { - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Not a STUN packet, magic cookie not found."); - } - return null; - } - - try - { - Message stunMessage - = Message.decode(buf, - (char) off, - (char) len); - - if (stunMessage.getMessageType() - != Message.BINDING_REQUEST) - { - return null; - } - - UsernameAttribute usernameAttribute - = (UsernameAttribute) - stunMessage.getAttribute(Attribute.USERNAME); - if (usernameAttribute == null) - return null; - - String usernameString - = new String(usernameAttribute.getUsername()); - return usernameString.split(":")[0]; - } - catch (Exception e) - { - // Catch everything. We are going to log, and then drop the packet - // anyway. - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Failed to extract local ufrag: " + e); - } - } - - return null; - } - /** * Creates a new {@link MySocket} instance and associates it with the given * remote address. Returns the created instance. diff --git a/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java b/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java index 4c6cf2d..94d288d 100644 --- a/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java @@ -20,33 +20,26 @@ package org.ice4j.ice.harvest; import java.io.*; import java.lang.ref.*; import java.net.*; -import java.nio.*; -import java.nio.channels.*; import java.util.*; import java.util.logging.*; import org.ice4j.*; -import org.ice4j.attribute.*; import org.ice4j.ice.*; -import org.ice4j.message.*; import org.ice4j.socket.*; /** - * A CandidateHarvester implementation, which listens on a specified - * list of TCP server sockets. On {@link #harvest(org.ice4j.ice.Component)}, a - * TCP candidate with type "passive" is added for each of the server sockets. - * - * This instance runs two threads: {@link #acceptThread} and - * {@link #readThread}. The 'accept' thread just accepts new Sockets - * and passes them over to the 'read' thread. The 'read' thread reads a STUN - * message from an accepted socket and, based on the STUN username, passes it - * to the appropriate Component. + * An implementation of {@link AbstractTcpListener} which acts as a + * {@link CandidateHarvester}. Sessions are accepted if their ufrag matches + * a {@link Component} registered with this harvester, and the known addresses + * of {@link AbstractTcpListener} are added as local host candidates (with type + * "tcp" and tcptype "passive") when harvesting. * * @author Boris Grozev * @author Lyubomir Marinov */ public class TcpHarvester - extends AbstractCandidateHarvester + extends AbstractTcpListener + implements CandidateHarvester { /** * Our class logger. @@ -60,64 +53,6 @@ public class TcpHarvester */ private static final int PURGE_INTERVAL = 20; - /** - * Closes a {@code Channel} and swallows any {@link IOException}. - * - * @param channel the {@code Channel} to close - */ - static void closeNoExceptions(Channel channel) - { - MuxServerSocketChannelFactory.closeNoExceptions(channel); - } - - /** - * Returns a list of all addresses on the interfaces in interfaces - * which are found suitable for candidate allocations (are not loopback, are - * up, and are allowed by the configuration). - * - * @param port the port to use. - * @param interfaces the list of interfaces to use. - */ - private static List getLocalAddresses( - int port, - List interfaces) - throws IOException - { - List addresses = new LinkedList<>(); - - for (NetworkInterface iface : interfaces) - { - if (NetworkUtils.isInterfaceLoopback(iface) - || !NetworkUtils.isInterfaceUp(iface) - || !HostCandidateHarvester.isInterfaceAllowed(iface)) - { - //this one is obviously not going to do - continue; - } - - Enumeration ifaceAddresses = iface.getInetAddresses(); - - while(ifaceAddresses.hasMoreElements()) - { - InetAddress addr = ifaceAddresses.nextElement(); - - addresses.add(new TransportAddress(addr, port, Transport.TCP)); - } - } - return addresses; - } - - /** - * The thread which accepts TCP connections from the sockets in - * {@link #serverSocketChannels}. - */ - private AcceptThread acceptThread; - - /** - * Triggers the termination of the threads of this instance. - */ - private boolean close = false; - /** * Maps a local "ufrag" to the single Component instance with that * "ufrag". @@ -129,14 +64,8 @@ public class TcpHarvester = new HashMap<>(); /** - * The list of transport addresses which we have found to be listening on, - * and which we will advertise as candidates in - * {@link #harvest(org.ice4j.ice.Component)} - */ - private final List localAddresses = new LinkedList<>(); - - /** - * Maps a public address to a local address. + * Maps a public address to a local address. Used to add additional + * candidates with type "srflx" when harvesting. */ private final Map mappedAddresses = new HashMap<>(); @@ -147,39 +76,22 @@ public class TcpHarvester */ private final Set mappedPorts = new HashSet<>(); - /** - * Channels pending to be added to the list that {@link #readThread} reads - * from. - */ - private final List newChannels = new LinkedList<>(); - /** * A counter used to decide when to purge {@link #components}. */ private int purgeCounter = 0; /** - * The Selector used by {@link #readThread}. - */ - private final Selector readSelector = Selector.open(); - - /** - * The thread which reads from the already accepted sockets. - */ - private ReadThread readThread; - - /** - * The list of ServerSocketChannels that we will accept - * on. - */ - private final List serverSocketChannels - = new LinkedList<>(); - - /** - * Whether or not to use ssltcp. + * Whether to advertise candidates with type "ssltcp" (if true) or "tcp" + * (if false). */ private final boolean ssltcp; + /** + * Manages statistics about harvesting time. + */ + private HarvestStatistics harvestStatistics = new HarvestStatistics(); + /** * Initializes a new TcpHarvester, which is to * listen on port number port on all IP addresses on all available @@ -193,7 +105,8 @@ public class TcpHarvester public TcpHarvester(int port) throws IOException { - this(port, /* ssltcp */ false); + super(port); + this.ssltcp = false; } /** @@ -210,9 +123,8 @@ public class TcpHarvester public TcpHarvester(int port, boolean ssltcp) throws IOException { - this(port, - Collections.list(NetworkInterface.getNetworkInterfaces()), - ssltcp); + super(port, Collections.list(NetworkInterface.getNetworkInterfaces())); + this.ssltcp = ssltcp; } /** @@ -227,12 +139,14 @@ public class TcpHarvester * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values, or * if an I/O error occurs. */ - public TcpHarvester(int port, - List interfaces, - boolean ssltcp) + public TcpHarvester( + int port, + List interfaces, + boolean ssltcp) throws IOException { - this(getLocalAddresses(port, interfaces), ssltcp); + super(port, interfaces); + this.ssltcp = ssltcp; } /** @@ -244,11 +158,11 @@ public class TcpHarvester * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values, or * if an I/O error occurs. */ - public TcpHarvester( - List transportAddresses) + public TcpHarvester(List transportAddresses) throws IOException { - this(transportAddresses, /* ssltcp */ false); + super(transportAddresses); + this.ssltcp = false; } /** @@ -266,127 +180,8 @@ public class TcpHarvester boolean ssltcp) throws IOException { + super(transportAddresses); this.ssltcp = ssltcp; - addLocalAddresses(transportAddresses); - init(); - } - - /** - * Adds to {@link #localAddresses} those addresses from - * transportAddresses which are found suitable for candidate - * allocation. - * - * @param transportAddresses the list of addresses to add. - * @throws IOException when {@link StackProperties#ALLOWED_ADDRESSES} or - * {@link StackProperties#BLOCKED_ADDRESSES} contains invalid values. - */ - private void addLocalAddresses(List transportAddresses) - throws IOException - { - boolean useIPv6 = !StackProperties.getBoolean( - StackProperties.DISABLE_IPv6, - false); - boolean useIPv6LinkLocal = !StackProperties.getBoolean( - StackProperties.DISABLE_LINK_LOCAL_ADDRESSES, - false); - - // White list from the configuration - String[] allowedAddressesStr - = StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES, - ";"); - InetAddress[] allowedAddresses = null; - - if (allowedAddressesStr != null) - { - allowedAddresses = new InetAddress[allowedAddressesStr.length]; - for (int i = 0; i < allowedAddressesStr.length; i++) - { - allowedAddresses[i] - = InetAddress.getByName(allowedAddressesStr[i]); - } - } - - // Black list from the configuration - String[] blockedAddressesStr - = StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES, - ";"); - InetAddress[] blockedAddresses = null; - - if (blockedAddressesStr != null) - { - blockedAddresses = new InetAddress[blockedAddressesStr.length]; - for (int i = 0; i < blockedAddressesStr.length; i++) - { - blockedAddresses[i] - = InetAddress.getByName(blockedAddressesStr[i]); - } - } - - for (TransportAddress transportAddress : transportAddresses) - { - InetAddress address = transportAddress.getAddress(); - - if (address.isLoopbackAddress()) - { - //loopback again - continue; - } - - if (!useIPv6 && (address instanceof Inet6Address)) - continue; - - if (!useIPv6LinkLocal - && (address instanceof Inet6Address) - && address.isLinkLocalAddress()) - { - logger.info("Not using link-local address " + address +" for" - + " TCP candidates."); - continue; - } - - if (allowedAddresses != null) - { - boolean found = false; - - for (InetAddress allowedAddress : allowedAddresses) - { - if (allowedAddress.equals(address)) - { - found = true; - break; - } - } - if (!found) - { - logger.info("Not using " + address +" for TCP candidates, " - + "because it is not in the allowed list."); - continue; - } - } - - if (blockedAddresses != null) - { - boolean found = false; - - for (InetAddress blockedAddress : blockedAddresses) - { - if (blockedAddress.equals(address)) - { - found = true; - break; - } - } - if (found) - { - logger.info("Not using " + address + " for TCP candidates, " - + "because it is in the blocked list."); - continue; - } - } - - // Passed all checks - localAddresses.add(transportAddress); - } } /** @@ -415,15 +210,6 @@ public class TcpHarvester mappedPorts.add(port); } - /** - * Triggers the termination of the threads of this - * MultiplexingTcpHarvester. - */ - public void close() - { - close = true; - } - /** * Creates and returns the list of LocalCandidates which are to be * added by this TcpHarvester to a specific @@ -619,124 +405,6 @@ public class TcpHarvester return candidates; } - /** - * Initializes {@link #serverSocketChannels}, creates and starts the threads - * used by this instance. - * @throws IOException if an I/O error occurs - */ - private void init() - throws IOException - { - for (TransportAddress transportAddress : localAddresses) - { - ServerSocketChannel channel - = MuxServerSocketChannelFactory - .openAndBindMuxServerSocketChannel( - /* properties */ null, - new InetSocketAddress( - transportAddress.getAddress(), - transportAddress.getPort()), - /* backlog */ 0, - new DatagramPacketFilter() - { - /** - * {@inheritDoc} - */ - @Override - public boolean accept(DatagramPacket p) - { - return isFirstDatagramPacket(p); - } - }); - - serverSocketChannels.add(channel); - } - - acceptThread = new AcceptThread(); - acceptThread.start(); - - readThread = new ReadThread(); - readThread.start(); - } - - /** - * Determines whether a specific {@link DatagramPacket} is the first - * expected (i.e. supported) to be received from an accepted - * {@link SocketChannel} by this {@code TcpHarvester}. If - * {@link #ssltcp} signals that Google TURN SSLTCP is to be expected, then - * {@code p} must be - * {@link GoogleTurnSSLCandidateHarvester#SSL_CLIENT_HANDSHAKE}. Otherwise, - * it must be a STUN Binding request packetized for TCP. - * - * @param p the {@code DatagramPacket} to examine - * @return {@code true} if {@code p} looks like the first - * {@code DatagramPacket} expected to be received from an accepted - * {@code SocketChannel} by this {@code TcpHarvester}; - * otherwise, {@code false} - */ - private boolean isFirstDatagramPacket(DatagramPacket p) - { - int len = p.getLength(); - boolean b = false; - - if (len > 0) - { - byte[] buf = p.getData(); - int off = p.getOffset(); - - // Check for Google TURN SSLTCP - final byte[] googleTurnSslTcp - = GoogleTurnSSLCandidateHarvester.SSL_CLIENT_HANDSHAKE; - - if (len >= googleTurnSslTcp.length) - { - b = true; - for (int i = 0, iEnd = googleTurnSslTcp.length, j = off; - i < iEnd; - i++, j++) - { - if (googleTurnSslTcp[i] != buf[j]) - { - b = false; - break; - } - } - } - - // nothing found, lets check for stun binding requests - if (!b) - { - // 2 bytes uint16 length - // STUN Binding request: - // 2 bits 00 - // 14 bits STUN Messsage Type - // 2 bytes Message Length - // 4 bytes Magic Cookie - - // RFC 5389: For example, a Binding request has class=0b00 - // (request) and method=0b000000000001 (Binding) and is encoded - // into the first 16 bits as 0x0001. - if (len >= 10 && buf[off + 2] == 0 && buf[off + 3] == 1) - { - final byte[] magicCookie = Message.MAGIC_COOKIE; - - b = true; - for (int i = 0, iEnd = magicCookie.length, j = off + 6; - i < iEnd; - i++, j++) - { - if (magicCookie[i] != buf[j]) - { - b = false; - break; - } - } - } - } - } - return b; - } - /** * Removes entries from {@link #components} for which the * WeakReference has been cleared. @@ -759,6 +427,116 @@ public class TcpHarvester } } + /** + * {@inheritDoc} + */ + @Override + protected void acceptSession(Socket socket, String ufrag, + DatagramPacket pushback) + throws IOException, IllegalStateException + { + Component component = getComponent(ufrag); + if (component == null) + { + throw new IllegalStateException( + "No component found for ufrag " + ufrag); + } + + addSocketToComponent(socket, component, pushback); + } + + /** + * Makes socket available to component and pushes back + * datagramPacket into the STUN socket. + * + * @param socket the Socket. + * @param component the Component. + * @param datagramPacket the DatagramPacket to push back. + * @throws IllegalStateException if the ICE state is incorrect, or an + * appropriate candidate could not be found. + * @throws IOException if creation of some of the required socket instances + * failed. + */ + private void addSocketToComponent( + Socket socket, Component component, DatagramPacket datagramPacket) + throws IOException, IllegalStateException + { + IceProcessingState state + = component.getParentStream().getParentAgent().getState(); + + if (!IceProcessingState.WAITING.equals(state) + && !IceProcessingState.RUNNING.equals(state)) + { + throw new IllegalStateException( + "The associated Agent is in state " + state); + } + + // Socket to add to the candidate + IceSocketWrapper candidateSocket = null; + // STUN-only filtered socket to add to the StunStack + IceSocketWrapper stunSocket = null; + + MultiplexingSocket multiplexing = new MultiplexingSocket(socket); + candidateSocket = new IceTcpSocketWrapper(multiplexing); + + stunSocket + = new IceTcpSocketWrapper( + multiplexing.getSocket(new StunDatagramPacketFilter())); + stunSocket = new PushBackIceSocketWrapper(stunSocket, datagramPacket); + + TcpHostCandidate candidate = findCandidate(component, socket); + if (candidate == null) + { + throw new IOException( + "Failed to find the local candidate for socket: " + socket); + } + + component.getParentStream().getParentAgent().getStunStack() + .addSocket(stunSocket); + candidate.addSocket(candidateSocket); + // the socket is not our responsibility anymore. It is up to + // the candidate/component to close/free it. + } + + /** + * Searches among the local candidates of Component for a + * TcpHostCandidate with the same transport address as the + * local transport address of socket. + * + * We expect to find such a candidate, which has been added by this + * TcpHarvester while harvesting. + * + * @param component the Component to search. + * @param socket the Socket to match the local transport + * address of. + * @return a TcpHostCandidate among the local candidates of + * Component with the same transport address as the local + * address of Socket, or null if no such candidate + * exists. + */ + private TcpHostCandidate findCandidate( + Component component, + Socket socket) + { + InetAddress localAddress = socket.getLocalAddress(); + int localPort = socket.getLocalPort(); + + for (LocalCandidate candidate : component.getLocalCandidates()) + { + TransportAddress transportAddress + = candidate.getTransportAddress(); + + if (candidate instanceof TcpHostCandidate + && Transport.TCP.equals(transportAddress.getTransport()) + && localPort == transportAddress.getPort() + && localAddress.equals(transportAddress.getAddress())) + { + return (TcpHostCandidate) candidate; + } + } + return null; + } + /** * {@inheritDoc} */ @@ -769,828 +547,11 @@ public class TcpHarvester } /** - * A Thread which will accept new SocketChannels from all - * ServerSocketChannels in {@link #serverSocketChannels}. + * {@inheritDoc} */ - private class AcceptThread - extends Thread + public HarvestStatistics getHarvestStatistics() { - /** - * The Selector used to select a specific - * ServerSocketChannel which is ready to accept. - */ - private final Selector selector; - - /** - * Initializes a new AcceptThread. - */ - public AcceptThread() - throws IOException - { - setName("TcpHarvester AcceptThread"); - setDaemon(true); - - selector = Selector.open(); - for (ServerSocketChannel channel : serverSocketChannels) - { - channel.configureBlocking(false); - channel.register(selector, SelectionKey.OP_ACCEPT); - } - } - - /** - * Notifies {@link #readThread} that new channels have been added to - */ - private void notifyReadThread() - { - readSelector.wakeup(); - } - - /** - * {@inheritDoc} - */ - @Override - public void run() - { - do - { - if (close) - { - break; - } - - IOException exception = null; - List channelsToAdd = new LinkedList<>(); - // Allow to go on, so we can quit if closed. - long selectTimeout = 3000; - - for (SelectionKey key : selector.keys()) - { - if (key.isValid()) - { - SocketChannel channel; - boolean acceptable = key.isAcceptable(); - - try - { - channel - = ((ServerSocketChannel) key.channel()) - .accept(); - } - catch (IOException ioe) - { - exception = ioe; - break; - } - - // Add the accepted channel to newChannels to allow the - // 'read' thread to it up. - if (channel != null) - { - channelsToAdd.add(channel); - } - else if (acceptable) - { - // The SelectionKey reported the channel as - // acceptable but channel#accept() did not accept a - // non-null SocketChannel. Give the channel a little - // time to get its act together. - selectTimeout = 100; - } - } - } - // We accepted from all serverSocketChannels. - selector.selectedKeys().clear(); - - if (!channelsToAdd.isEmpty()) - { - synchronized (newChannels) - { - newChannels.addAll(channelsToAdd); - } - notifyReadThread(); - } - - if (exception != null) - { - logger.info( - "Failed to accept a socket, which should have been" - + " ready to accept: " + exception); - break; - } - - try - { - // Allow to go on, so we can quit if closed. - selector.select(selectTimeout); - } - catch (IOException ioe) - { - logger.info( - "Failed to select an accept-ready socket: " + ioe); - break; - } - } - while (true); - - //now clean up and exit - for (ServerSocketChannel serverSocketChannel : serverSocketChannels) - closeNoExceptions(serverSocketChannel); - - try - { - selector.close(); - } - catch (IOException ioe) - {} - } + return harvestStatistics; } - /** - * Contains a SocketChannel that ReadThread is reading - * from. - */ - private static class ChannelDesc - { - /** - * The actual SocketChannel. - */ - public final SocketChannel channel; - - /** - * The time the channel was last found to be active. - */ - long lastActive = System.currentTimeMillis(); - - /** - * The buffer which stores the data so far read from the channel. - */ - ByteBuffer buffer = null; - - /** - * Whether we had checked for initial "pseudo" SSL handshake. - */ - boolean checkedForSSLHandshake = false; - - /** - * Buffer to use if we had read some data in advance and want to process - * it after next read, used when we are checking for "pseudo" SSL and - * we haven't found some, but had read data to check for it. - */ - byte[] preBuffered = null; - - /** - * The value of the RFC4571 "length" field read from the channel, or - * -1 if it hasn't been read (yet). - */ - int length = -1; - - /** - * Initializes a new ChannelDesc with the given channel. - * @param channel the channel. - */ - public ChannelDesc(SocketChannel channel) - { - this.channel = channel; - } - } - - /** - * An IceSocketWrapper implementation which allows a - * DatagramPacket to be pushed back and received on the first call - * to {@link #receive(java.net.DatagramPacket)}. - */ - private static class PushBackIceSocketWrapper - extends IceSocketWrapper - { - /** - * The DatagramPacket which will be used on the first call to - * {@link #receive(java.net.DatagramPacket)}. - */ - private DatagramPacket datagramPacket; - - /** - * The IceSocketWrapper that this instance wraps around. - */ - private final IceSocketWrapper wrapped; - - /** - * Initializes a new PushBackIceSocketWrapper instance that - * wraps around wrappedWrapper and reads from - * datagramSocket on the first call to - * {@link #receive(java.net.DatagramPacket)} - * - * @param wrappedWrapper the IceSocketWrapper instance that we - * wrap around. - * @param datagramPacket the DatagramPacket which will be used - * on the first call to {@link #receive(java.net.DatagramPacket)} - */ - public PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper, - DatagramPacket datagramPacket) - { - this.wrapped = wrappedWrapper; - this.datagramPacket = datagramPacket; - } - - /** - * {@inheritDoc} - */ - @Override - public void close() - { - wrapped.close(); - } - - /** - * {@inheritDoc} - */ - @Override - public InetAddress getLocalAddress() - { - return wrapped.getLocalAddress(); - } - - /** - * {@inheritDoc} - */ - @Override - public int getLocalPort() - { - return wrapped.getLocalPort(); - } - - /** - * {@inheritDoc} - */ - @Override - public SocketAddress getLocalSocketAddress() - { - return wrapped.getLocalSocketAddress(); - } - - /** - * {@inheritDoc} - */ - @Override - public Socket getTCPSocket() - { - return wrapped.getTCPSocket(); - } - - /** - * {@inheritDoc} - */ - @Override - public DatagramSocket getUDPSocket() - { - return wrapped.getUDPSocket(); - } - - /** - * {@inheritDoc} - * - * On the first call to this instance reads from - * {@link #datagramPacket}, on subsequent calls delegates to - * {@link #wrapped}. - */ - @Override - public void receive(DatagramPacket p) throws IOException - { - if (datagramPacket != null) - { - int len = Math.min(p.getLength(), datagramPacket.getLength()); - System.arraycopy(datagramPacket.getData(), 0, - p.getData(), 0, - len); - p.setAddress(datagramPacket.getAddress()); - p.setPort(datagramPacket.getPort()); - datagramPacket = null; - } - else - { - wrapped.receive(p); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void send(DatagramPacket p) throws IOException - { - wrapped.send(p); - } - } - - private class ReadThread - extends Thread - { - /** - * Initializes a new ReadThread. - * - * @throws IOException if the selector to be used fails to open. - */ - public ReadThread() - throws IOException - { - setName("TcpHarvester ReadThread"); - setDaemon(true); - } - - /** - * Registers the channels from {@link #newChannels} in - * {@link #readSelector}. - */ - private void checkForNewChannels() - { - synchronized (newChannels) - { - for (SocketChannel channel : newChannels) - { - try - { - channel.configureBlocking(false); - channel.register( - readSelector, - SelectionKey.OP_READ, - new ChannelDesc(channel)); - } - catch (IOException ioe) - { - logger.info("Failed to register channel: " + ioe); - closeNoExceptions(channel); - } - } - newChannels.clear(); - } - } - - /** - * Closes any inactive channels registered with {@link #readSelector}. - * A channel is considered inactive if it hasn't been available for - * reading for - * {@link MuxServerSocketChannelFactory#SOCKET_CHANNEL_READ_TIMEOUT} - * milliseconds. - */ - private void cleanup() - { - long now = System.currentTimeMillis(); - - for (SelectionKey key : readSelector.keys()) - { - // An invalid key specifies that either the channel was closed - // (in which case we do not have to do anything else to it) or - // that we no longer control the channel (i.e. we do not want to - // do anything else to it). - if (!key.isValid()) - continue; - - ChannelDesc channelDesc = (ChannelDesc) key.attachment(); - - if (channelDesc == null) - continue; - - long lastActive = channelDesc.lastActive; - - if (lastActive != -1 - && now - lastActive - > MuxServerSocketChannelFactory - .SOCKET_CHANNEL_READ_TIMEOUT) - { - // De-register from the Selector. - key.cancel(); - - SocketChannel channel = channelDesc.channel; - - logger.info("Read timeout for socket: " + channel.socket()); - - closeNoExceptions(channel); - } - } - } - - /** - * Searches among the local candidates of Component for a - * TcpHostCandidate with the same transport address as the - * local transport address of socket. - * - * We expect to find such a candidate, which has been added by this - * TcpHarvester while harvesting. - * - * @param component the Component to search. - * @param socket the Socket to match the local transport - * address of. - * @return a TcpHostCandidate among the local candidates of - * Component with the same transport address as the local - * address of Socket, or null if no such candidate - * exists. - */ - private TcpHostCandidate findCandidate( - Component component, - Socket socket) - { - InetAddress localAddress = socket.getLocalAddress(); - int localPort = socket.getLocalPort(); - - for (LocalCandidate candidate : component.getLocalCandidates()) - { - TransportAddress transportAddress - = candidate.getTransportAddress(); - - if (candidate instanceof TcpHostCandidate - && Transport.TCP.equals(transportAddress.getTransport()) - && localPort == transportAddress.getPort() - && localAddress.equals(transportAddress.getAddress())) - { - return (TcpHostCandidate) candidate; - } - } - return null; - } - - /** - * Makes socket available to component and pushes back - * datagramPacket into the STUN socket. - * - * @param socket the Socket. - * @param component the Component. - * @param datagramPacket the DatagramPacket to push back. - */ - private void handSocketToComponent(Socket socket, - Component component, - DatagramPacket datagramPacket) - { - IceProcessingState state - = component.getParentStream().getParentAgent().getState(); - - if (!IceProcessingState.WAITING.equals(state) - && !IceProcessingState.RUNNING.equals(state)) - { - throw new IllegalStateException( - "The associated Agent is in state " + state); - } - - // Socket to add to the candidate - IceSocketWrapper candidateSocket = null; - // STUN-only filtered socket to add to the StunStack - IceSocketWrapper stunSocket = null; - - try - { - MultiplexingSocket multiplexing = new MultiplexingSocket(socket); - candidateSocket = new IceTcpSocketWrapper(multiplexing); - - stunSocket - = new IceTcpSocketWrapper( - multiplexing.getSocket(new StunDatagramPacketFilter())); - stunSocket - = new PushBackIceSocketWrapper(stunSocket, datagramPacket); - } - catch (IOException ioe) - { - logger.info("Failed to create sockets: " + ioe); - } - - TcpHostCandidate candidate = findCandidate(component, socket); - if (candidate != null) - { - component.getParentStream().getParentAgent().getStunStack() - .addSocket(stunSocket); - candidate.addSocket(candidateSocket); - - // the socket is not our responsibility anymore. It is up to - // the candidate/component to close/free it. - } - else - { - logger.info("Failed to find the local candidate for socket: " - + socket); - try - { - socket.close(); - } - catch (IOException ioe) - {} - } - } - - /** - * Tries to read, without blocking, from channel to its - * buffer. If after reading the buffer is filled, handles the data in - * the buffer. - * - * This works in three stages: - * 1 (optional, only if ssltcp is enabled): Read a fixed-size message. - * If it matches the hard-coded pseudo SSL ClientHello, sends the - * hard-coded ServerHello. - * 2: Read two bytes as an unsigned int and interpret it as the length - * to read in the next stage. - * 3: Read number of bytes indicated in stage2 and try to interpret - * them as a STUN message. - * - * If a STUN message is successfully read, and it contains a USERNAME - * attribute, the local "ufrag" is extracted from the - * attribute value and the socket is passed on to the Component - * that this TcpHarvester has associated with - * that "ufrag". - * - * @param channel the SocketChannel to read from. - * @param key the SelectionKey associated with - * channel, which is to be canceled in case no further - * reading is required from the channel. - */ - private void readFromChannel(ChannelDesc channel, SelectionKey key) - { - if (channel.buffer == null) - { - // Set up a buffer with a pre-determined size - - if (!channel.checkedForSSLHandshake && channel.length == -1) - { - channel.buffer - = ByteBuffer.allocate( - GoogleTurnSSLCandidateHarvester - .SSL_CLIENT_HANDSHAKE.length); - } - else if (channel.length == -1) - { - channel.buffer = ByteBuffer.allocate(2); - } - else - { - channel.buffer = ByteBuffer.allocate(channel.length); - } - } - - try - { - int read = channel.channel.read(channel.buffer); - - if (read == -1) - throw new IOException("End of stream!"); - else if (read > 0) - channel.lastActive = System.currentTimeMillis(); - - if (!channel.buffer.hasRemaining()) - { - // We've filled in the buffer. - if (!channel.checkedForSSLHandshake) - { - byte[] bytesRead - = new byte[GoogleTurnSSLCandidateHarvester - .SSL_CLIENT_HANDSHAKE.length]; - - channel.buffer.flip(); - channel.buffer.get(bytesRead); - - // Set to null, so that we re-allocate it for the next - // stage - channel.buffer = null; - channel.checkedForSSLHandshake = true; - - if (Arrays.equals(bytesRead, - GoogleTurnSSLCandidateHarvester - .SSL_CLIENT_HANDSHAKE)) - { - ByteBuffer byteBuffer = ByteBuffer.wrap( - GoogleTurnSSLCandidateHarvester - .SSL_SERVER_HANDSHAKE); - channel.channel.write(byteBuffer); - } - else - { - int fb = bytesRead[0]; - int sb = bytesRead[1]; - - channel.length = (((fb & 0xff) << 8) | (sb & 0xff)); - - byte[] preBuffered - = Arrays.copyOfRange( - bytesRead, 2, bytesRead.length); - - // if we had read enough data - if(channel.length <= bytesRead.length - 2) - { - processStunBindingRequest( - preBuffered, channel, key); - } - else - { - // not enough data, store what was read - // and continue - channel.preBuffered = preBuffered; - - channel.length -= channel.preBuffered.length; - } - } - } - else if (channel.length == -1) - { - channel.buffer.flip(); - - int fb = channel.buffer.get(); - int sb = channel.buffer.get(); - - channel.length = (((fb & 0xff) << 8) | (sb & 0xff)); - - // Set to null, so that we re-allocate it for the next - // stage - channel.buffer = null; - } - else - { - byte[] bytesRead = new byte[channel.length]; - - channel.buffer.flip(); - channel.buffer.get(bytesRead); - - if(channel.preBuffered != null) - { - // will store preBuffered and currently read data - byte[] newBytesRead = new byte[ - channel.preBuffered.length + bytesRead.length]; - - // copy old data - System.arraycopy( - channel.preBuffered, 0, - newBytesRead, 0, - channel.preBuffered.length); - // and new data - System.arraycopy( - bytesRead, 0, - newBytesRead, channel.preBuffered.length, - bytesRead.length); - - // use that data for processing - bytesRead = newBytesRead; - - channel.preBuffered = null; - } - - processStunBindingRequest(bytesRead, channel, key); - } - } - } - catch (IOException | StunException | IllegalStateException e) - { - logger.info( - "Failed to handle TCP socket " - + channel.channel.socket() + ": " + e.getMessage()); - key.cancel(); - closeNoExceptions(channel.channel); - } - } - - /** - * Process the readed bytes as stun binding request. - * - * If a STUN message is successfully read, and it contains a USERNAME - * attribute, the local "ufrag" is extracted from the - * attribute value and the socket is passed on to the Component - * that this TcpHarvester has associated with - * that "ufrag". - * - * @param bytesRead bytes to be processed - * @param channel the SocketChannel to read from. - * @param key the SelectionKey associated with - * channel, which is to be canceled in case no further - * reading is required from the channel. - * @throws StunException error decoding package - * @throws IOException missing attributes (username, component or - * wrong type) - */ - private void processStunBindingRequest( - byte[] bytesRead, - ChannelDesc channel, SelectionKey key) - throws StunException, IOException - { - // Does this look like a STUN binding request? - // What's the username? - Message stunMessage - = Message.decode(bytesRead, - (char) 0, - (char) bytesRead.length); - - if (stunMessage.getMessageType() - != Message.BINDING_REQUEST) - { - throw new IOException("Not a binding request"); - } - - UsernameAttribute usernameAttribute - = (UsernameAttribute) - stunMessage.getAttribute(Attribute.USERNAME); - - if (usernameAttribute == null) - { - throw new IOException( - "No USERNAME attribute present."); - } - - String usernameString - = new String(usernameAttribute.getUsername()); - String localUfrag = usernameString.split(":")[0]; - Component component = getComponent(localUfrag); - - if (component == null) - throw new IOException("No component found."); - - // The rest of the stack will read from the socket's - // InputStream. We cannot change the blocking mode - // before the channel is removed from the selector (by - // cancelling the key) - key.cancel(); - channel.channel.configureBlocking(true); - - // Construct a DatagramPacket from the just-read packet - // which is to be pushed back - DatagramPacket p - = new DatagramPacket(bytesRead, bytesRead.length); - Socket socket = channel.channel.socket(); - - p.setAddress(socket.getInetAddress()); - p.setPort(socket.getPort()); - - handSocketToComponent(socket, component, p); - } - - /** - * {@inheritDoc} - */ - @Override - public void run() - { - do - { - synchronized (TcpHarvester.this) - { - if (close) - break; - } - - // clean up stale channels - cleanup(); - - checkForNewChannels(); - - for (SelectionKey key : readSelector.keys()) - { - if (key.isValid()) - { - ChannelDesc channelDesc - = (ChannelDesc) key.attachment(); - - readFromChannel(channelDesc, key); - } - } - // We read from all SocketChannels. - readSelector.selectedKeys().clear(); - - try - { - readSelector.select( - MuxServerSocketChannelFactory - .SOCKET_CHANNEL_READ_TIMEOUT - / 2); - } - catch (IOException ioe) - { - logger.info("Failed to select a read-ready channel."); - } - } - while (true); - - //we are all done, clean up. - synchronized (newChannels) - { - for (SocketChannel channel : newChannels) - { - closeNoExceptions(channel); - } - newChannels.clear(); - } - - for (SelectionKey key : readSelector.keys()) - { - // An invalid key specifies that either the channel was closed - // (in which case we do not have to do anything else to it) or - // that we no longer control the channel (i.e. we do not want to - // do anything else to it). - if (key.isValid()) - { - Channel channel = key.channel(); - - if (channel.isOpen()) - closeNoExceptions(channel); - } - } - - try - { - readSelector.close(); - } - catch (IOException ioe) - {} - } - } }