refactor: Extracts an abstract base class from SinglePortUdpHarvester.

This commit is contained in:
Boris Grozev 2016-09-08 13:02:43 -05:00
parent 0abaef19e8
commit 35e603d346
2 changed files with 673 additions and 532 deletions

View File

@ -0,0 +1,625 @@
/*
* 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.attribute.*;
import org.ice4j.ice.*;
import org.ice4j.message.*;
import org.ice4j.socket.*;
import org.ice4j.stack.*;
import org.ice4j.util.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import java.util.logging.Logger;
/**
* A class which holds a {@link DatagramSocket} and runs a thread
* ({@link #thread}) which perpetually reads from it.
*
* When a datagram from an unknown source is received, it is parsed as a STUN
* Binding Request, and if it has a USERNAME attribute, its ufrag is extracted.
* At this point, an implementing class may choose to create a mapping for
* the remote address of the datagram, which will be used for further packets
* from this address.
*
* @author Boris Grozev
*/
public abstract class AbstractUdpListener
{
/**
* Our class logger.
*/
private static final Logger logger
= Logger.getLogger(AbstractUdpListener.class.getName());
/**
* The size for newly allocated <tt>Buffer</tt> instances. This limits the
* maximum size of datagrams we can receive.
*
* XXX should we increase this in case of other MTUs, or set it dynamically
* according to the available network interfaces?
*/
private static final int BUFFER_SIZE
= /* assumed MTU */ 1500 - /* IPv4 header */ 20 - /* UDP header */ 8;
/**
* The number of <tt>Buffer</tt> instances to keep in {@link #pool}.
*/
private static final int POOL_SIZE = 256;
/**
* Returns the list of {@link TransportAddress}es, one for each allowed IP
* address found on each allowed network interface, with the given port.
*
* @param port the UDP port number.
* @return the list of allowed transport addresses.
*/
public static List<TransportAddress> getAllowedAddresses(int port)
{
List<TransportAddress> addresses = new LinkedList<>();
boolean isIPv6Disabled = StackProperties.getBoolean(
StackProperties.DISABLE_IPv6,
false);
boolean isIPv6LinkLocalDisabled = StackProperties.getBoolean(
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
false);
try
{
for (NetworkInterface iface
: Collections.list(NetworkInterface.getNetworkInterfaces()))
{
if (NetworkUtils.isInterfaceLoopback(iface)
|| !NetworkUtils.isInterfaceUp(iface)
|| !HostCandidateHarvester.isInterfaceAllowed(iface))
{
continue;
}
Enumeration<InetAddress> ifaceAddresses
= iface.getInetAddresses();
while (ifaceAddresses.hasMoreElements())
{
InetAddress address = ifaceAddresses.nextElement();
if (!HostCandidateHarvester.isAddressAllowed(address))
continue;
if (isIPv6Disabled && address instanceof Inet6Address)
continue;
if (isIPv6LinkLocalDisabled
&& address instanceof Inet6Address
&& address.isLinkLocalAddress())
continue;
addresses.add(
new TransportAddress(address, port, Transport.UDP));
}
}
}
catch (SocketException se)
{
logger.info("Failed to get network interfaces: " + se);
}
return addresses;
}
/**
* The map which keeps the known remote addresses and their associated
* candidateSockets.
* {@link #thread} is the only thread which adds new entries, while
* other threads remove entries when candidates are freed.
*/
private final Map<SocketAddress, MySocket> sockets
= new ConcurrentHashMap<>();
/**
* A pool of <tt>Buffer</tt> instances used to avoid creating of new java
* objects.
*/
private final ArrayBlockingQueue<Buffer> pool
= new ArrayBlockingQueue<>(POOL_SIZE);
/**
* The local address that this harvester is bound to.
*/
protected final TransportAddress localAddress;
/**
* The "main" socket that this harvester reads from.
*/
private final DatagramSocket socket;
/**
* The thread reading from {@link #socket}.
*/
private final Thread thread;
/**
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
* bind on the specified local address.
* @param localAddress the address to bind to.
* @throws IOException if initialization fails.
*/
protected AbstractUdpListener(TransportAddress localAddress)
throws IOException
{
this.localAddress = localAddress;
this.socket = new DatagramSocket(localAddress);
thread = new Thread()
{
@Override
public void run()
{
AbstractUdpListener.this.runInHarvesterThread();
}
};
thread.setName(AbstractUdpListener.class.getName() + " thread for "
+ localAddress);
thread.setDaemon(true);
thread.start();
}
/**
* Perpetually reads datagrams from {@link #socket} and handles them
* accordingly.
*
* It is important that this blocks are little as possible (except on
* socket.receive(), of course), because it could potentially delay the
* reception of both ICE and media packets for the whole application.
*/
private void runInHarvesterThread()
{
Buffer buf;
DatagramPacket pkt = null;
MySocket destinationSocket;
InetSocketAddress remoteAddress;
do
{
// TODO: implement stopping the thread with a switch?
buf = getFreeBuffer();
if (pkt == null)
pkt = new DatagramPacket(buf.buffer, 0, buf.buffer.length);
else
pkt.setData(buf.buffer, 0, buf.buffer.length);
try
{
socket.receive(pkt);
}
catch (IOException ioe)
{
logger.severe("Failed to receive from socket: " + ioe);
break;
}
buf.len = pkt.getLength();
remoteAddress = (InetSocketAddress) pkt.getSocketAddress();
destinationSocket = sockets.get(remoteAddress);
if (destinationSocket != null)
{
//make 'pkt' available for reading through destinationSocket
destinationSocket.addBuffer(buf);
}
else
{
// Packet from an unknown source. Is it a STUN Binding Request?
String ufrag = getUfrag(buf.buffer, 0, buf.len);
if (ufrag == null)
{
// Not a STUN Binding Request or doesn't have a valid
// USERNAME attribute. Drop it.
continue;
}
maybeAcceptNewSession(buf, remoteAddress, ufrag);
// Maybe add to #sockets here in the base class?
}
}
while (true);
// TODO we are all done, clean up.
}
/**
* Handles the reception of a STUN Binding Request with a valid USERNAME
* attribute, from a "new" remote address (one which is not in
* {@link #sockets}).
* Implementations may choose to e.g. create a socket and pass it to their
* ICE stack.
*
* Note that this is meant to only be executed by
* {@link AbstractUdpListener}'s read thread, and should not be called from
* implementing classes.
*
* @param buf the UDP payload of the first datagram received on the newly
* accepted socket.
* @param remoteAddress the remote address from which the datagram was
* received.
* @param ufrag the local ICE username fragment of the received STUN Binding
* Request.
*/
protected abstract void maybeAcceptNewSession(
Buffer buf,
InetSocketAddress remoteAddress,
String ufrag);
/**
* Gets an unused <tt>Buffer</tt> instance, creating it if necessary.
* @return an unused <tt>Buffer</tt> instance, creating it if necessary.
*/
private Buffer getFreeBuffer()
{
Buffer buf = pool.poll();
if (buf == null)
{
buf = new Buffer(new byte[BUFFER_SIZE], 0);
}
return buf;
}
/**
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
* length <tt>len</tt>) 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 <tt>null</tt>.
*
* @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 <tt>buf</tt>, or <tt>null</tt>.
*/
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.
*
* Note that this is meant to only execute in {@link AbstractUdpListener}'s
* read thread.
*
* @param remoteAddress the remote address with which to associate the new
* socket instance.
* @return the created socket instance.
*/
protected MySocket addSocket(InetSocketAddress remoteAddress)
throws SocketException
{
MySocket newSocket = new MySocket(remoteAddress);
sockets.put(remoteAddress, newSocket);
return newSocket;
}
/**
* Implements a <tt>DatagramSocket</tt> for the purposes of a specific
* <tt>MyCandidate</tt>.
*
* It is not bound to a specific port, but shares the same local address
* as the bound socket held by the harvester.
*/
protected class MySocket
extends DatagramSocket
{
/**
* The size of {@link #queue}.
*/
private static final int QUEUE_SIZE = 128;
/**
* The FIFO which acts as a buffer for this socket.
*/
private final ArrayBlockingQueue<Buffer> queue
= new ArrayBlockingQueue<>(QUEUE_SIZE);
/**
* The {@link QueueStatistics} instance optionally used to collect and
* print detailed statistics about {@link #queue}.
*/
private final QueueStatistics queueStatistics;
/**
* The remote address that is associated with this socket.
*/
private SocketAddress remoteAddress;
/**
* The flag which indicates that this <tt>DatagramSocket</tt> has been
* closed.
*/
private boolean closed = false;
/**
* Initializes a new <tt>MySocket</tt> instance with the given
* remote address.
* @param remoteAddress the remote address to be associated with the
* new instance.
* @throws SocketException
*/
public MySocket(SocketAddress remoteAddress)
throws SocketException
{
// unbound
super((SocketAddress)null);
this.remoteAddress = remoteAddress;
if (logger.isLoggable(Level.FINEST))
{
queueStatistics = new QueueStatistics(
"SinglePort" + remoteAddress.toString().replace('/', '-'));
}
else
{
queueStatistics = null;
}
}
/**
* Adds pkt to this socket. If the queue is full, drops a packet. Does
* not block.
*/
protected void addBuffer(Buffer buf)
{
synchronized (queue)
{
// Drop the first rather than the current packet, so that
// receivers can notice the loss earlier.
if (queue.size() == QUEUE_SIZE)
{
logger.info("Dropping a packet because the queue is full.");
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
queue.poll();
}
queue.offer(buf);
if (queueStatistics != null)
{
queueStatistics.add(System.currentTimeMillis());
}
queue.notifyAll();
}
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public InetAddress getLocalAddress()
{
return localAddress.getAddress();
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public int getLocalPort()
{
return localAddress.getPort();
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public SocketAddress getLocalSocketAddress()
{
return localAddress;
}
/**
* {@inheritDoc}
*
* Removes the association of the remote address with this socket from
* the harvester's map.
*/
@Override
public void close()
{
closed = true;
synchronized (queue)
{
// Wake up any threads still in receive()
queue.notifyAll();
}
// We could be called by the super-class constructor, in which
// case this.removeAddress is not initialized yet.
if (remoteAddress != null)
AbstractUdpListener.this.sockets.remove(remoteAddress);
super.close();
}
/**
* Reads the data from the first element of {@link #queue} into
* <tt>p</tt>. Blocks until {@link #queue} has an element.
* @param p
* @throws IOException
*/
@Override
public void receive(DatagramPacket p)
throws IOException
{
Buffer buf = null;
while (buf == null)
{
if (closed)
throw new SocketException("Socket closed");
synchronized (queue)
{
if (queue.isEmpty())
{
try
{
queue.wait();
}
catch (InterruptedException ie)
{}
}
buf = queue.poll();
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
}
}
byte[] pData = p.getData();
// XXX Should we use p.setData() here with a buffer of our own?
if (pData == null || pData.length < buf.len)
throw new IOException("packet buffer not available");
System.arraycopy(buf.buffer, 0, pData, 0, buf.len);
p.setLength(buf.len);
p.setSocketAddress(remoteAddress);
pool.offer(buf);
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public void send(DatagramPacket p)
throws IOException
{
socket.send(p);
}
}
/**
* Represents a buffer for the purposes of <tt>SinglePortUdpHarvester</tt>.
* Wraps a byte[] and adds a length field specifying the number of elements
* actually used.
*/
protected class Buffer
{
/**
* The actual data.
*/
byte[] buffer;
/**
* The number of elements of {@link #buffer} actually used.
*/
int len;
/**
* Initializes a new <tt>Buffer</tt> instance.
* @param buffer the data.
* @param len the length.
*/
private Buffer(byte[] buffer, int len)
{
this.buffer = buffer;
this.len = len;
}
}
}

View File

@ -43,17 +43,16 @@ import java.util.logging.Logger; // Disambiguation
* and associates the component's local username fragment (ufrag) with this * and associates the component's local username fragment (ufrag) with this
* candidate. * candidate.
* *
* When a datagram from an unknown source is received, it is parsed as a STUN * When a STUN Binding Request with a given ufrag is received, if the ufrag
* Binding Request, and if it has a USERNAME attribute, its ufrag is extracted. * matches one of the registered candidates, then a new socket is created, which
* If this ufrag is associated with a candidate of this harvester, a new socket * is to receive further packets from the remote address, and the socket is
* is created and added to the candidate, and the remote address of the datagram * added to the candidate.
* is associated with this socket. This mapping is then used to de-multiplex
* received datagrams based on their remote address.
* *
* @author Boris Grozev * @author Boris Grozev
*/ */
public class SinglePortUdpHarvester public class SinglePortUdpHarvester
extends AbstractCandidateHarvester extends AbstractUdpListener
implements CandidateHarvester
{ {
/** /**
* Our class logger. * Our class logger.
@ -61,21 +60,6 @@ public class SinglePortUdpHarvester
private static final Logger logger private static final Logger logger
= Logger.getLogger(SinglePortUdpHarvester.class.getName()); = Logger.getLogger(SinglePortUdpHarvester.class.getName());
/**
* The size for newly allocated <tt>Buffer</tt> instances. This limits the
* maximum size of datagrams we can receive.
*
* XXX should we increase this in case of other MTUs, or set it dynamically
* according to the available network interfaces?
*/
private static final int BUFFER_SIZE
= /* assumed MTU */ 1500 - /* IPv4 header */ 20 - /* UDP header */ 8;
/**
* The number of <tt>Buffer</tt> instances to keep in {@link #pool}.
*/
private static final int POOL_SIZE = 256;
/** /**
* Creates a new <tt>SinglePortUdpHarvester</tt> instance for each allowed * Creates a new <tt>SinglePortUdpHarvester</tt> instance for each allowed
* IP address found on each allowed network interface, with the given port. * IP address found on each allowed network interface, with the given port.
@ -83,58 +67,12 @@ public class SinglePortUdpHarvester
* @param port the UDP port number to use. * @param port the UDP port number to use.
* @return the list of created <tt>SinglePortUdpHarvester</tt>s. * @return the list of created <tt>SinglePortUdpHarvester</tt>s.
*/ */
public static List<SinglePortUdpHarvester> public static List<SinglePortUdpHarvester> createHarvesters(int port)
createHarvesters(int port)
{ {
List<SinglePortUdpHarvester> harvesters = new LinkedList<>(); List<SinglePortUdpHarvester> harvesters = new LinkedList<>();
List<TransportAddress> addresses = new LinkedList<>(); for (TransportAddress address
boolean isIPv6Disabled = StackProperties.getBoolean( : AbstractUdpListener.getAllowedAddresses(port))
StackProperties.DISABLE_IPv6,
false);
boolean isIPv6LinkLocalDisabled = StackProperties.getBoolean(
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
false);
try
{
for (NetworkInterface iface
: Collections.list(NetworkInterface.getNetworkInterfaces()))
{
if (NetworkUtils.isInterfaceLoopback(iface)
|| !NetworkUtils.isInterfaceUp(iface)
|| !HostCandidateHarvester.isInterfaceAllowed(iface))
{
continue;
}
Enumeration<InetAddress> ifaceAddresses
= iface.getInetAddresses();
while (ifaceAddresses.hasMoreElements())
{
InetAddress address = ifaceAddresses.nextElement();
if (!HostCandidateHarvester.isAddressAllowed(address))
continue;
if (isIPv6Disabled && address instanceof Inet6Address)
continue;
if (isIPv6LinkLocalDisabled
&& address instanceof Inet6Address
&& address.isLinkLocalAddress())
continue;
addresses.add(
new TransportAddress(address, port, Transport.UDP));
}
}
}
catch (SocketException se)
{
logger.info("Failed to get network interfaces: " + se);
}
for (TransportAddress address : addresses)
{ {
try try
{ {
@ -151,15 +89,6 @@ public class SinglePortUdpHarvester
return harvesters; return harvesters;
} }
/**
* The map which keeps the known remote addresses and their associated
* candidateSockets.
* {@link #thread} is the only thread which adds new entries, while
* other threads remove entries when candidates are freed.
*/
private final Map<SocketAddress, MySocket> sockets
= new ConcurrentHashMap<>();
/** /**
* The map which keeps all currently active <tt>Candidate</tt>s created by * The map which keeps all currently active <tt>Candidate</tt>s created by
* this harvester. The keys are the local username fragments (ufrags) of * this harvester. The keys are the local username fragments (ufrags) of
@ -169,26 +98,9 @@ public class SinglePortUdpHarvester
= new ConcurrentHashMap<>(); = new ConcurrentHashMap<>();
/** /**
* A pool of <tt>Buffer</tt> instances used to avoid creating of new java * Manages statistics about harvesting time.
* objects.
*/ */
private final ArrayBlockingQueue<Buffer> pool private HarvestStatistics harvestStatistics = new HarvestStatistics();
= new ArrayBlockingQueue<>(POOL_SIZE);
/**
* The local address that this harvester is bound to.
*/
private final TransportAddress localAddress;
/**
* The "main" socket that this harvester reads from.
*/
private final DatagramSocket socket;
/**
* The thread reading from {@link #socket}.
*/
private final Thread thread;
/** /**
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to * Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
@ -199,214 +111,61 @@ public class SinglePortUdpHarvester
public SinglePortUdpHarvester(TransportAddress localAddress) public SinglePortUdpHarvester(TransportAddress localAddress)
throws IOException throws IOException
{ {
this.localAddress = localAddress; super(localAddress);
this.socket = new DatagramSocket(localAddress);
logger.info("Initialized SinglePortUdpHarvester with address " logger.info("Initialized SinglePortUdpHarvester with address "
+ localAddress); + localAddress);
thread = new Thread()
{
@Override
public void run()
{
SinglePortUdpHarvester.this.runInHarvesterThread();
}
};
thread.setName(SinglePortUdpHarvester.class.getName() + " thread");
thread.setDaemon(true);
thread.start();
} }
/** /**
* Perpetually reads datagrams from {@link #socket} and handles them * {@inheritDoc}
* accordingly. */
public HarvestStatistics getHarvestStatistics()
{
return harvestStatistics;
}
/**
* {@inheritDoc}
* *
* It is important that this blocks are little as possible (except on * Looks for an ICE candidate registered with this harvester, which has a
* socket.receive(), of course), because it could potentially delay the * local ufrag of {@code ufrag}, and if one is found it accepts the new
* reception of both ICE and media packets for the whole application. * socket and adds it to the candidate.
*/ */
private void runInHarvesterThread() protected void maybeAcceptNewSession(Buffer buf,
InetSocketAddress remoteAddress,
String ufrag)
{ {
Buffer buf; MyCandidate candidate = candidates.get(ufrag);
DatagramPacket pkt = null; if (candidate == null)
Component component;
MyCandidate candidate;
MySocket destinationSocket;
InetSocketAddress remoteAddress;
do
{ {
// TODO: implement stopping the thread with a switch? // A STUN Binding Request with an unknown USERNAME. Drop it.
return;
buf = getFreeBuffer();
if (pkt == null)
pkt = new DatagramPacket(buf.buffer, 0, buf.buffer.length);
else
pkt.setData(buf.buffer, 0, buf.buffer.length);
try
{
socket.receive(pkt);
}
catch (IOException ioe)
{
logger.severe("Failed to receive from socket: " + ioe);
break;
}
buf.len = pkt.getLength();
remoteAddress = (InetSocketAddress) pkt.getSocketAddress();
destinationSocket = sockets.get(remoteAddress);
if (destinationSocket != null)
{
//make 'pkt' available for reading through destinationSocket
destinationSocket.addBuffer(buf);
}
else
{
// Packet from an unknown source. Is it a STUN Binding Request?
String ufrag = getUfrag(buf.buffer, 0, buf.len);
if (ufrag == null)
{
// Not a STUN Binding Request or doesn't have a valid
// USERNAME attribute. Drop it.
continue;
}
candidate = candidates.get(ufrag);
component
= candidate == null ? null : candidate.getParentComponent();
if (component != null)
{
// This is a STUN Binding Request destined for this
// specific Candidate/Component/Agent.
try
{
// 1. Create a socket for this remote address
MySocket newSocket = new MySocket(remoteAddress);
// 2. Set-up de-multiplexing for future datagrams
// with this address to this socket.
sockets.put(remoteAddress, newSocket);
// 3. Let the candidate and its STUN stack no about the
// new socket.
candidate.addSocket(newSocket, remoteAddress);
// 4. Add the original datagram to the new socket.
newSocket.addBuffer(buf);
}
catch (SocketException se)
{
logger.info("Could not create a socket: " + se);
}
catch (IOException ioe)
{
logger.info("Failed to handle new socket: " + ioe);
}
}
else
{
// A STUN Binding Request with an unknown USERNAME. Drop it.
}
}
}
while (true);
// TODO we are all done, clean up.
}
/**
* Gets an unused <tt>Buffer</tt> instance, creating it if necessary.
* @return an unused <tt>Buffer</tt> instance, creating it if necessary.
*/
private Buffer getFreeBuffer()
{
Buffer buf = pool.poll();
if (buf == null)
{
buf = new Buffer(new byte[BUFFER_SIZE], 0);
}
return buf;
}
/**
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
* length <tt>len</tt>) 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 <tt>null</tt>.
*
* @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 <tt>buf</tt>, or <tt>null</tt>.
*/
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;
} }
// This is a STUN Binding Request destined for this
// specific Candidate/Component/Agent.
try try
{ {
Message stunMessage // 1. Create a socket for this remote address
= Message.decode(buf, // 2. Set-up de-multiplexing for future datagrams
(char) off, // with this address to this socket.
(char) len); MySocket newSocket = addSocket(remoteAddress);
if (stunMessage.getMessageType() // 3. Let the candidate and its STUN stack no about the
!= Message.BINDING_REQUEST) // new socket.
{ candidate.addSocket(newSocket, remoteAddress);
return null;
}
UsernameAttribute usernameAttribute // 4. Add the original datagram to the new socket.
= (UsernameAttribute) newSocket.addBuffer(buf);
stunMessage.getAttribute(Attribute.USERNAME);
if (usernameAttribute == null)
return null;
String usernameString
= new String(usernameAttribute.getUsername());
return usernameString.split(":")[0];
} }
catch (Exception e) catch (SocketException se)
{ {
// Catch everything. We are going to log, and then drop the packet logger.info("Could not create a socket: " + se);
// anyway. }
if (logger.isLoggable(Level.FINE)) catch (IOException ioe)
{ {
logger.fine("Failed to extract local ufrag: " + e); logger.info("Failed to handle new socket: " + ioe);
}
} }
return null;
} }
/** /**
@ -449,249 +208,6 @@ public class SinglePortUdpHarvester
return true; return true;
} }
/**
* Implements a <tt>DatagramSocket</tt> for the purposes of a specific
* <tt>MyCandidate</tt>.
*
* It is not bound to a specific port, but shares the same local address
* as the bound socket held by the harvester.
*/
private class MySocket
extends DatagramSocket
{
/**
* The size of {@link #queue}.
*/
private static final int QUEUE_SIZE = 128;
/**
* The FIFO which acts as a buffer for this socket.
*/
private final ArrayBlockingQueue<Buffer> queue
= new ArrayBlockingQueue<>(QUEUE_SIZE);
/**
* The {@link QueueStatistics} instance optionally used to collect and
* print detailed statistics about {@link #queue}.
*/
private final QueueStatistics queueStatistics;
/**
* The remote address that is associated with this socket.
*/
private SocketAddress remoteAddress;
/**
* The flag which indicates that this <tt>DatagramSocket</tt> has been
* closed.
*/
private boolean closed = false;
/**
* Initializes a new <tt>MySocket</tt> instance with the given
* remote address.
* @param remoteAddress the remote address to be associated with the
* new instance.
* @throws SocketException
*/
public MySocket(SocketAddress remoteAddress)
throws SocketException
{
// unbound
super((SocketAddress)null);
this.remoteAddress = remoteAddress;
if (logger.isLoggable(Level.FINEST))
{
queueStatistics = new QueueStatistics(
"SinglePort" + remoteAddress.toString().replace('/', '-'));
}
else
{
queueStatistics = null;
}
}
/**
* Adds pkt to this socket. If the queue is full, drops a packet. Does
* not block.
*/
private void addBuffer(Buffer buf)
{
synchronized (queue)
{
// Drop the first rather than the current packet, so that
// receivers can notice the loss earlier.
if (queue.size() == QUEUE_SIZE)
{
logger.info("Dropping a packet because the queue is full.");
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
queue.poll();
}
queue.offer(buf);
if (queueStatistics != null)
{
queueStatistics.add(System.currentTimeMillis());
}
queue.notifyAll();
}
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public InetAddress getLocalAddress()
{
return localAddress.getAddress();
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public int getLocalPort()
{
return localAddress.getPort();
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public SocketAddress getLocalSocketAddress()
{
return localAddress;
}
/**
* {@inheritDoc}
*
* Removes the association of the remote address with this socket from
* the harvester's map.
*/
@Override
public void close()
{
closed = true;
synchronized (queue)
{
// Wake up any threads still in receive()
queue.notifyAll();
}
// We could be called by the super-class constructor, in which
// case this.removeAddress is not initialized yet.
if (remoteAddress != null)
SinglePortUdpHarvester.this.sockets.remove(remoteAddress);
super.close();
}
/**
* Reads the data from the first element of {@link #queue} into
* <tt>p</tt>. Blocks until {@link #queue} has an element.
* @param p
* @throws IOException
*/
@Override
public void receive(DatagramPacket p)
throws IOException
{
Buffer buf = null;
while (buf == null)
{
if (closed)
throw new SocketException("Socket closed");
synchronized (queue)
{
if (queue.isEmpty())
{
try
{
queue.wait();
}
catch (InterruptedException ie)
{}
}
buf = queue.poll();
if (queueStatistics != null)
{
queueStatistics.remove(System.currentTimeMillis());
}
}
}
byte[] pData = p.getData();
// XXX Should we use p.setData() here with a buffer of our own?
if (pData == null || pData.length < buf.len)
throw new IOException("packet buffer not available");
System.arraycopy(buf.buffer, 0, pData, 0, buf.len);
p.setLength(buf.len);
p.setSocketAddress(remoteAddress);
pool.offer(buf);
}
/**
* {@inheritDoc}
*
* Delegates to the actual socket of the harvester.
*/
@Override
public void send(DatagramPacket p)
throws IOException
{
socket.send(p);
}
}
/**
* Represents a buffer for the purposes of <tt>SinglePortUdpHarvester</tt>.
* Wraps a byte[] and adds a length field specifying the number of elements
* actually used.
*/
private class Buffer
{
/**
* The actual data.
*/
byte[] buffer;
/**
* The number of elements of {@link #buffer} actually used.
*/
int len;
/**
* Initializes a new <tt>Buffer</tt> instance.
* @param buffer the data.
* @param len the length.
*/
private Buffer(byte[] buffer, int len)
{
this.buffer = buffer;
this.len = len;
}
}
/** /**
* Implements a <tt>Candidate</tt> for the purposes of this * Implements a <tt>Candidate</tt> for the purposes of this
* <tt>SinglePortUdpHarvester</tt>. * <tt>SinglePortUdpHarvester</tt>.