Allows a listening endpoint to be shared among multiple protocols (e.g. ICE-TCP, HTTP).
This commit is contained in:
parent
c7b8fd2e2b
commit
446607e5d7
@ -32,6 +32,7 @@ import org.ice4j.socket.*;
|
||||
* to the appropriate <tt>Component</tt>.
|
||||
*
|
||||
* @author Boris Grozev
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class MultiplexingTcpHostHarvester
|
||||
extends CandidateHarvester
|
||||
@ -49,11 +50,14 @@ public class MultiplexingTcpHostHarvester
|
||||
private static final int PURGE_INTERVAL = 20;
|
||||
|
||||
/**
|
||||
* Channels which we have failed to read from after at least
|
||||
* <tt>READ_TIMEOUT</tt> milliseconds will be considered failed and will
|
||||
* be closed.
|
||||
* Closes a {@code Channel} and swallows any {@link IOException}.
|
||||
*
|
||||
* @param channel the {@code Channel} to close
|
||||
*/
|
||||
private static final int READ_TIMEOUT = 10000;
|
||||
static void closeNoExceptions(Channel channel)
|
||||
{
|
||||
MuxServerSocketChannel.closeNoExceptions(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all addresses on the interfaces in <tt>interfaces</tt>
|
||||
@ -67,7 +71,6 @@ public class MultiplexingTcpHostHarvester
|
||||
int port,
|
||||
List<NetworkInterface> interfaces)
|
||||
throws IOException
|
||||
|
||||
{
|
||||
List<TransportAddress> addresses = new LinkedList<TransportAddress>();
|
||||
|
||||
@ -193,8 +196,8 @@ public class MultiplexingTcpHostHarvester
|
||||
throws IOException
|
||||
{
|
||||
this(port,
|
||||
Collections.list(NetworkInterface.getNetworkInterfaces()),
|
||||
ssltcp);
|
||||
Collections.list(NetworkInterface.getNetworkInterfaces()),
|
||||
ssltcp);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -257,7 +260,6 @@ public class MultiplexingTcpHostHarvester
|
||||
boolean useIPv6 = !StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_IPv6,
|
||||
false);
|
||||
|
||||
boolean useIPv6LinkLocal = !StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
|
||||
false);
|
||||
@ -267,6 +269,7 @@ public class MultiplexingTcpHostHarvester
|
||||
= StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES,
|
||||
";");
|
||||
InetAddress[] allowedAddresses = null;
|
||||
|
||||
if (allowedAddressesStr != null)
|
||||
{
|
||||
allowedAddresses = new InetAddress[allowedAddressesStr.length];
|
||||
@ -282,6 +285,7 @@ public class MultiplexingTcpHostHarvester
|
||||
= StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES,
|
||||
";");
|
||||
InetAddress[] blockedAddresses = null;
|
||||
|
||||
if (blockedAddressesStr != null)
|
||||
{
|
||||
blockedAddresses = new InetAddress[blockedAddressesStr.length];
|
||||
@ -317,6 +321,7 @@ public class MultiplexingTcpHostHarvester
|
||||
if (allowedAddresses != null)
|
||||
{
|
||||
boolean found = false;
|
||||
|
||||
for (InetAddress allowedAddress : allowedAddresses)
|
||||
{
|
||||
if (allowedAddress.equals(address))
|
||||
@ -325,7 +330,6 @@ public class MultiplexingTcpHostHarvester
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
logger.info("Not using " + address +" for TCP candidates, "
|
||||
@ -337,6 +341,7 @@ public class MultiplexingTcpHostHarvester
|
||||
if (blockedAddresses != null)
|
||||
{
|
||||
boolean found = false;
|
||||
|
||||
for (InetAddress blockedAddress : blockedAddresses)
|
||||
{
|
||||
if (blockedAddress.equals(address))
|
||||
@ -345,7 +350,6 @@ public class MultiplexingTcpHostHarvester
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
logger.info("Not using " + address + " for TCP candidates, "
|
||||
@ -601,11 +605,25 @@ public class MultiplexingTcpHostHarvester
|
||||
{
|
||||
for (TransportAddress transportAddress : localAddresses)
|
||||
{
|
||||
ServerSocketChannel channel = ServerSocketChannel.open();
|
||||
ServerSocket socket = channel.socket();
|
||||
socket.bind(
|
||||
new InetSocketAddress(transportAddress.getAddress(),
|
||||
transportAddress.getPort()));
|
||||
ServerSocketChannel channel
|
||||
= MuxServerSocketChannel.openAndBind(
|
||||
/* 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);
|
||||
}
|
||||
|
||||
@ -616,6 +634,85 @@ public class MultiplexingTcpHostHarvester
|
||||
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 MultiplexingTcpHostHarvester}. 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 MultiplexingTcpHostHarvester};
|
||||
* 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();
|
||||
|
||||
if (ssltcp)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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
|
||||
* <tt>WeakReference</tt> has been cleared.
|
||||
@ -663,7 +760,7 @@ public class MultiplexingTcpHostHarvester
|
||||
/**
|
||||
* Initializes a new <tt>AcceptThread</tt>.
|
||||
*/
|
||||
private AcceptThread()
|
||||
public AcceptThread()
|
||||
throws IOException
|
||||
{
|
||||
setName("MultiplexingTcpHostHarvester AcceptThread");
|
||||
@ -691,85 +788,91 @@ public class MultiplexingTcpHostHarvester
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (true)
|
||||
do
|
||||
{
|
||||
int readyChannels;
|
||||
if (close)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
IOException exception = null;
|
||||
List<SocketChannel> channelsToAdd
|
||||
= new LinkedList<SocketChannel>();
|
||||
// Allow to go on, so we can quit if closed.
|
||||
long selectTimeout = 3000;
|
||||
|
||||
for (SelectionKey key : selector.keys())
|
||||
{
|
||||
// Allow to go on, so we can quit if closed
|
||||
readyChannels = selector.select(3000);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
// We accepted from all serverSocketChannels.
|
||||
selector.selectedKeys().clear();
|
||||
|
||||
if (!channelsToAdd.isEmpty())
|
||||
{
|
||||
logger.info("Failed to select an accept-ready socket: "
|
||||
+ ioe);
|
||||
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;
|
||||
}
|
||||
|
||||
if (readyChannels > 0)
|
||||
{
|
||||
IOException exception = null;
|
||||
List<SocketChannel> channelsToAdd
|
||||
= new LinkedList<SocketChannel>();
|
||||
|
||||
for (SelectionKey key : selector.selectedKeys())
|
||||
{
|
||||
SocketChannel channel;
|
||||
if (key.isAcceptable())
|
||||
{
|
||||
try
|
||||
{
|
||||
channel = ((ServerSocketChannel)
|
||||
key.channel()).accept();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
exception = ioe;
|
||||
break;
|
||||
}
|
||||
|
||||
// Add the accepted socket to newChannels, so
|
||||
// the 'read' thread can pick it up.
|
||||
channelsToAdd.add(channel);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
} // while(true)
|
||||
|
||||
//now clean up and exit
|
||||
for (ServerSocketChannel serverSocketChannel : serverSocketChannels)
|
||||
{
|
||||
try
|
||||
{
|
||||
serverSocketChannel.close();
|
||||
// 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
|
||||
{
|
||||
@ -780,6 +883,48 @@ public class MultiplexingTcpHostHarvester
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a <tt>SocketChannel</tt> that <tt>ReadThread</tt> is reading
|
||||
* from.
|
||||
*/
|
||||
private static class ChannelDesc
|
||||
{
|
||||
/**
|
||||
* The actual <tt>SocketChannel</tt>.
|
||||
*/
|
||||
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 or not the initial "pseudo" SSL handshake has been read.
|
||||
*/
|
||||
boolean sslHandshakeRead = false;
|
||||
|
||||
/**
|
||||
* 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 <tt>ChannelDesc</tt> with the given channel.
|
||||
* @param channel the channel.
|
||||
*/
|
||||
public ChannelDesc(SocketChannel channel)
|
||||
{
|
||||
this.channel = channel;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An <tt>IceSocketWrapper</tt> implementation which allows a
|
||||
* <tt>DatagramPacket</tt> to be pushed back and received on the first call
|
||||
@ -810,8 +955,8 @@ public class MultiplexingTcpHostHarvester
|
||||
* @param datagramPacket the <tt>DatagramPacket</tt> which will be used
|
||||
* on the first call to {@link #receive(java.net.DatagramPacket)}
|
||||
*/
|
||||
private PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper,
|
||||
DatagramPacket datagramPacket)
|
||||
public PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper,
|
||||
DatagramPacket datagramPacket)
|
||||
{
|
||||
this.wrapped = wrappedWrapper;
|
||||
this.datagramPacket = datagramPacket;
|
||||
@ -910,19 +1055,12 @@ public class MultiplexingTcpHostHarvester
|
||||
private class ReadThread
|
||||
extends Thread
|
||||
{
|
||||
/**
|
||||
* Contains the <tt>SocketChanel</tt>s that we are currently reading
|
||||
* from.
|
||||
*/
|
||||
private final List<ChannelDesc> channels
|
||||
= new LinkedList<ChannelDesc>();
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>ReadThread</tt>.
|
||||
*
|
||||
* @throws IOException if the selector to be used fails to open.
|
||||
*/
|
||||
private ReadThread()
|
||||
public ReadThread()
|
||||
throws IOException
|
||||
{
|
||||
setName("MultiplexingTcpHostHarvester ReadThread");
|
||||
@ -942,20 +1080,16 @@ public class MultiplexingTcpHostHarvester
|
||||
try
|
||||
{
|
||||
channel.configureBlocking(false);
|
||||
channel.register(readSelector, SelectionKey.OP_READ);
|
||||
channel.register(
|
||||
readSelector,
|
||||
SelectionKey.OP_READ,
|
||||
new ChannelDesc(channel));
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to register channel: " + ioe);
|
||||
try
|
||||
{
|
||||
channel.close();
|
||||
}
|
||||
catch (IOException ioe2)
|
||||
{}
|
||||
closeNoExceptions(channel);
|
||||
}
|
||||
|
||||
channels.add(new ChannelDesc(channel));
|
||||
}
|
||||
newChannels.clear();
|
||||
}
|
||||
@ -969,35 +1103,35 @@ public class MultiplexingTcpHostHarvester
|
||||
{
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
for (Iterator<ChannelDesc> i = channels.iterator();
|
||||
i.hasNext();)
|
||||
for (SelectionKey key : readSelector.keys())
|
||||
{
|
||||
ChannelDesc channelDesc = i.next();
|
||||
// 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;
|
||||
|
||||
if (channelDesc.lastActive != -1
|
||||
&& now - channelDesc.lastActive > READ_TIMEOUT)
|
||||
ChannelDesc channelDesc = (ChannelDesc) key.attachment();
|
||||
|
||||
if (channelDesc == null)
|
||||
continue;
|
||||
|
||||
long lastActive = channelDesc.lastActive;
|
||||
|
||||
if (lastActive != -1
|
||||
&& now - lastActive
|
||||
> MuxServerSocketChannel
|
||||
.SOCKET_CHANNEL_READ_TIMEOUT)
|
||||
{
|
||||
i.remove();
|
||||
logger.info("Read timeout for socket: "
|
||||
+ channelDesc.channel.socket());
|
||||
// De-register from the Selector.
|
||||
key.cancel();
|
||||
|
||||
// De-register from the selector
|
||||
for (SelectionKey key : readSelector.keys())
|
||||
{
|
||||
if (key.channel().equals(channelDesc.channel))
|
||||
{
|
||||
key.cancel();
|
||||
}
|
||||
}
|
||||
SocketChannel channel = channelDesc.channel;
|
||||
|
||||
try
|
||||
{
|
||||
channelDesc.channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to close channel: " + ioe);
|
||||
}
|
||||
logger.info("Read timeout for socket: " + channel.socket());
|
||||
|
||||
closeNoExceptions(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1029,6 +1163,7 @@ public class MultiplexingTcpHostHarvester
|
||||
{
|
||||
TransportAddress transportAddress
|
||||
= candidate.getTransportAddress();
|
||||
|
||||
if (candidate instanceof TcpHostCandidate
|
||||
&& Transport.TCP.equals(transportAddress.getTransport())
|
||||
&& localPort == transportAddress.getPort()
|
||||
@ -1040,25 +1175,6 @@ public class MultiplexingTcpHostHarvester
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the <tt>ChannelDesc</tt> in {@link #channels} whose
|
||||
* channel is <tt>channel</tt>.
|
||||
*
|
||||
* @param channel the channel to search for.
|
||||
* @return the <tt>ChannelDesc</tt> in {@link #channels} whose
|
||||
* channel is <tt>channel</tt>
|
||||
*/
|
||||
private ChannelDesc findChannelDesc(SelectableChannel channel)
|
||||
{
|
||||
for (ChannelDesc channelDesc : channels)
|
||||
{
|
||||
if (channelDesc.channel.equals(channel))
|
||||
return channelDesc;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes <tt>socket</tt> available to <tt>component</tt> and pushes back
|
||||
* <tt>datagramPacket</tt> into the STUN socket.
|
||||
@ -1073,17 +1189,18 @@ public class MultiplexingTcpHostHarvester
|
||||
{
|
||||
IceProcessingState state
|
||||
= component.getParentStream().getParentAgent().getState();
|
||||
|
||||
if (!IceProcessingState.WAITING.equals(state)
|
||||
&& !IceProcessingState.RUNNING.equals(state))
|
||||
{
|
||||
logger.info("Not adding a socket to an ICE agent with state "
|
||||
+ state);
|
||||
logger.info(
|
||||
"Not adding a socket to an ICE agent with state "
|
||||
+ state);
|
||||
return;
|
||||
}
|
||||
|
||||
// Socket to add to the candidate
|
||||
IceSocketWrapper candidateSocket = null;
|
||||
|
||||
// STUN-only filtered socket to add to the StunStack
|
||||
IceSocketWrapper stunSocket = null;
|
||||
|
||||
@ -1124,7 +1241,6 @@ public class MultiplexingTcpHostHarvester
|
||||
catch (IOException ioe)
|
||||
{}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1152,8 +1268,7 @@ public class MultiplexingTcpHostHarvester
|
||||
* <tt>channel</tt>, which is to be canceled in case no further
|
||||
* reading is required from the channel.
|
||||
*/
|
||||
private void readFromChannel(ChannelDesc channel,
|
||||
SelectionKey key)
|
||||
private void readFromChannel(ChannelDesc channel, SelectionKey key)
|
||||
{
|
||||
if (channel.buffer == null)
|
||||
{
|
||||
@ -1162,8 +1277,8 @@ public class MultiplexingTcpHostHarvester
|
||||
if (ssltcp && !channel.sslHandshakeRead)
|
||||
{
|
||||
channel.buffer
|
||||
= ByteBuffer.allocate(
|
||||
GoogleTurnSSLCandidateHarvester
|
||||
= ByteBuffer.allocate(
|
||||
GoogleTurnSSLCandidateHarvester
|
||||
.SSL_CLIENT_HANDSHAKE.length);
|
||||
}
|
||||
else if (channel.length == -1)
|
||||
@ -1178,10 +1293,12 @@ public class MultiplexingTcpHostHarvester
|
||||
|
||||
try
|
||||
{
|
||||
if (channel.channel.read(channel.buffer) == -1)
|
||||
{
|
||||
throw new IOException("Socket closed.");
|
||||
}
|
||||
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())
|
||||
{
|
||||
@ -1189,7 +1306,7 @@ public class MultiplexingTcpHostHarvester
|
||||
if (ssltcp && !channel.sslHandshakeRead)
|
||||
{
|
||||
byte[] bytesRead
|
||||
= new byte[GoogleTurnSSLCandidateHarvester
|
||||
= new byte[GoogleTurnSSLCandidateHarvester
|
||||
.SSL_CLIENT_HANDSHAKE.length];
|
||||
|
||||
channel.buffer.flip();
|
||||
@ -1214,11 +1331,11 @@ public class MultiplexingTcpHostHarvester
|
||||
throw new IOException("Expected a pseudo ssl"
|
||||
+ " handshake, but received something else.");
|
||||
}
|
||||
|
||||
}
|
||||
else if (channel.length == -1)
|
||||
{
|
||||
channel.buffer.flip();
|
||||
|
||||
int fb = channel.buffer.get();
|
||||
int sb = channel.buffer.get();
|
||||
|
||||
@ -1231,6 +1348,7 @@ public class MultiplexingTcpHostHarvester
|
||||
else
|
||||
{
|
||||
byte[] bytesRead = new byte[channel.length];
|
||||
|
||||
channel.buffer.flip();
|
||||
channel.buffer.get(bytesRead);
|
||||
|
||||
@ -1248,16 +1366,20 @@ public class MultiplexingTcpHostHarvester
|
||||
}
|
||||
|
||||
UsernameAttribute usernameAttribute
|
||||
= (UsernameAttribute)
|
||||
= (UsernameAttribute)
|
||||
stunMessage.getAttribute(Attribute.USERNAME);
|
||||
|
||||
if (usernameAttribute == null)
|
||||
{
|
||||
throw new IOException(
|
||||
"No USERNAME attribute present.");
|
||||
}
|
||||
|
||||
String usernameString
|
||||
= new String(usernameAttribute.getUsername());
|
||||
= new String(usernameAttribute.getUsername());
|
||||
String localUfrag = usernameString.split(":")[0];
|
||||
Component component = getComponent(localUfrag);
|
||||
|
||||
if (component == null)
|
||||
throw new IOException("No component found.");
|
||||
|
||||
@ -1271,44 +1393,31 @@ public class MultiplexingTcpHostHarvester
|
||||
// Construct a DatagramPacket from the just-read packet
|
||||
// which is to be pushed back
|
||||
DatagramPacket p
|
||||
= new DatagramPacket(bytesRead,
|
||||
bytesRead.length);
|
||||
= new DatagramPacket(bytesRead, bytesRead.length);
|
||||
Socket socket = channel.channel.socket();
|
||||
|
||||
p.setAddress(socket.getInetAddress());
|
||||
p.setPort(socket.getPort());
|
||||
|
||||
handSocketToComponent(socket, component, p);
|
||||
|
||||
// Successfully accepted, we don't need it anymore.
|
||||
channels.remove(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to handle TCP socket "
|
||||
+ channel.channel.socket() + ": " + ioe);
|
||||
channels.remove(channel);
|
||||
logger.info(
|
||||
"Failed to handle TCP socket "
|
||||
+ channel.channel.socket() + ": " + ioe);
|
||||
key.cancel();
|
||||
try
|
||||
{
|
||||
channel.channel.close();
|
||||
}
|
||||
catch (IOException ioe2)
|
||||
{}
|
||||
closeNoExceptions(channel.channel);
|
||||
}
|
||||
catch (StunException se)
|
||||
{
|
||||
logger.info("Failed to handle TCP socket "
|
||||
+ channel.channel.socket() + ": " + se);
|
||||
channels.remove(channel);
|
||||
logger.info(
|
||||
"Failed to handle TCP socket "
|
||||
+ channel.channel.socket() + ": " + se);
|
||||
key.cancel();
|
||||
try
|
||||
{
|
||||
channel.channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{}
|
||||
closeNoExceptions(channel.channel);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1318,11 +1427,7 @@ public class MultiplexingTcpHostHarvester
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
Set<SelectionKey> selectedKeys;
|
||||
int readyChannels = 0;
|
||||
SelectableChannel selectedChannel;
|
||||
|
||||
while (true)
|
||||
do
|
||||
{
|
||||
synchronized (MultiplexingTcpHostHarvester.this)
|
||||
{
|
||||
@ -1335,63 +1440,57 @@ public class MultiplexingTcpHostHarvester
|
||||
|
||||
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
|
||||
{
|
||||
readyChannels = readSelector.select(READ_TIMEOUT / 2);
|
||||
readSelector.select(
|
||||
MuxServerSocketChannel.SOCKET_CHANNEL_READ_TIMEOUT
|
||||
/ 2);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to select a read-ready channel.");
|
||||
}
|
||||
|
||||
if (readyChannels > 0)
|
||||
{
|
||||
selectedKeys = readSelector.selectedKeys();
|
||||
for (SelectionKey key : selectedKeys)
|
||||
{
|
||||
if (key.isReadable())
|
||||
{
|
||||
selectedChannel = key.channel();
|
||||
|
||||
ChannelDesc channelDesc
|
||||
= findChannelDesc(selectedChannel);
|
||||
channelDesc.lastActive = System.currentTimeMillis();
|
||||
|
||||
readFromChannel(channelDesc, key);
|
||||
}
|
||||
}
|
||||
selectedKeys.clear();
|
||||
}
|
||||
} //while(true)
|
||||
}
|
||||
while (true);
|
||||
|
||||
//we are all done, clean up.
|
||||
synchronized (newChannels)
|
||||
{
|
||||
for (SocketChannel channel : newChannels)
|
||||
{
|
||||
try
|
||||
{
|
||||
channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{}
|
||||
closeNoExceptions(channel);
|
||||
}
|
||||
newChannels.clear();
|
||||
}
|
||||
|
||||
for (ChannelDesc channelDesc : channels)
|
||||
for (SelectionKey key : readSelector.keys())
|
||||
{
|
||||
try
|
||||
{
|
||||
channelDesc.channel.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
channels.clear();
|
||||
|
||||
try
|
||||
{
|
||||
readSelector.close();
|
||||
@ -1399,47 +1498,5 @@ public class MultiplexingTcpHostHarvester
|
||||
catch (IOException ioe)
|
||||
{}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a <tt>SocketChannel</tt> that <tt>ReadThread</tt> is
|
||||
* reading from.
|
||||
*/
|
||||
private class ChannelDesc
|
||||
{
|
||||
/**
|
||||
* The actual <tt>SocketChannel</tt>.
|
||||
*/
|
||||
private final SocketChannel channel;
|
||||
|
||||
/**
|
||||
* The time the channel was last found to be active.
|
||||
*/
|
||||
private long lastActive = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* The buffer which stores the data so far read from the channel.
|
||||
*/
|
||||
ByteBuffer buffer = null;
|
||||
|
||||
/**
|
||||
* Whether or not the initial "pseudo" SSL handshake has been read.
|
||||
*/
|
||||
boolean sslHandshakeRead = false;
|
||||
|
||||
/**
|
||||
* The value of the RFC4571 "length" field read from the channel, or
|
||||
* -1 if it hasn't (yet) been read.
|
||||
*/
|
||||
int length = -1;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>ChannelDesc</tt> with the given channel.
|
||||
* @param channel the channel.
|
||||
*/
|
||||
private ChannelDesc(SocketChannel channel)
|
||||
{
|
||||
this.channel = channel;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
279
src/org/ice4j/socket/DelegatingServerSocket.java
Normal file
279
src/org/ice4j/socket/DelegatingServerSocket.java
Normal file
@ -0,0 +1,279 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code ServerSocket} which delegates (its method calls) to
|
||||
* another {@code ServerSocket}. In other words, the former wraps the latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class DelegatingServerSocket
|
||||
extends ServerSocket
|
||||
{
|
||||
/**
|
||||
* The {@link ServerSocketChannel} of this instance. If {@code null}, the
|
||||
* one of the {@link #delegate} is used.
|
||||
*/
|
||||
protected final ServerSocketChannel channel;
|
||||
|
||||
/**
|
||||
* The {@link ServerSocket} this instance delegates (its method calls) to.
|
||||
*/
|
||||
protected final ServerSocket delegate;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingServerSocket} instance which is to
|
||||
* delegate (its method calls) to a specific {@link ServerSocket}.
|
||||
*
|
||||
* @param delegate the {@code ServerSocket} the new instance is to delegate
|
||||
* (it method calls) to
|
||||
* @throws IOException
|
||||
*/
|
||||
public DelegatingServerSocket(ServerSocket delegate)
|
||||
throws IOException
|
||||
{
|
||||
this(delegate, delegate.getChannel());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingServerSocket} instance which is to
|
||||
* delegate (its method calls) to a specific {@link ServerSocket} and is to
|
||||
* report a specific {@link ServerSocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code ServerSocket} the new instance is to delegate
|
||||
* (it method calls) to
|
||||
* @param channel the {@code ServerSocketChannel} to be reported by the new
|
||||
* instance or {@code null} to report the one of {@code delegate}
|
||||
* @throws IOException
|
||||
*/
|
||||
public DelegatingServerSocket(
|
||||
ServerSocket delegate,
|
||||
ServerSocketChannel channel)
|
||||
throws IOException
|
||||
{
|
||||
this.delegate = delegate;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public Socket accept()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.accept();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void bind(SocketAddress endpoint)
|
||||
throws IOException
|
||||
{
|
||||
delegate.bind(endpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void bind(SocketAddress endpoint, int backlog)
|
||||
throws IOException
|
||||
{
|
||||
delegate.bind(endpoint, backlog);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
throws IOException
|
||||
{
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* If {@link #channel} is not {@code null}, returns it. Otherwise, forwards
|
||||
* to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public ServerSocketChannel getChannel()
|
||||
{
|
||||
ServerSocketChannel channel = this.channel;
|
||||
|
||||
return (channel == null) ? delegate.getChannel() : channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public InetAddress getInetAddress()
|
||||
{
|
||||
return delegate.getInetAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getLocalPort()
|
||||
{
|
||||
return delegate.getLocalPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalSocketAddress()
|
||||
{
|
||||
return delegate.getLocalSocketAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getReceiveBufferSize()
|
||||
throws SocketException
|
||||
{
|
||||
return delegate.getReceiveBufferSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean getReuseAddress()
|
||||
throws SocketException
|
||||
{
|
||||
return delegate.getReuseAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getSoTimeout()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getSoTimeout();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isBound()
|
||||
{
|
||||
return delegate.isBound();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isClosed()
|
||||
{
|
||||
return delegate.isClosed();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void setPerformancePreferences(int connectionTime,
|
||||
int latency,
|
||||
int bandwidth)
|
||||
{
|
||||
delegate.setPerformancePreferences(connectionTime, latency, bandwidth);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void setReceiveBufferSize(int size)
|
||||
throws SocketException
|
||||
{
|
||||
delegate.setReceiveBufferSize(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void setReuseAddress(boolean on)
|
||||
throws SocketException
|
||||
{
|
||||
delegate.setReuseAddress(on);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void setSoTimeout(int timeout)
|
||||
throws SocketException
|
||||
{
|
||||
delegate.setSoTimeout(timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return delegate.toString();
|
||||
}
|
||||
}
|
||||
321
src/org/ice4j/socket/DelegatingServerSocketChannel.java
Normal file
321
src/org/ice4j/socket/DelegatingServerSocketChannel.java
Normal file
@ -0,0 +1,321 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code ServerSocketChannel} which delegates (its method calls)
|
||||
* to another {@code ServerSocketChannel}. In other words, the former wraps the
|
||||
* latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||
extends ServerSocketChannel
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The {@link ServerSocketChannel} this instance delegates (its method
|
||||
* calls) to.
|
||||
*/
|
||||
protected final T delegate;
|
||||
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* The {@code ServerSocket} to be reported by this instance.
|
||||
*/
|
||||
private ServerSocket socket;
|
||||
|
||||
/**
|
||||
* The <tt>Object</tt> which synchronizes the access to {@link #socket}.
|
||||
*/
|
||||
private final Object socketSyncRoot = new Object();
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingServerSocketChannel} instance which is
|
||||
* to delegate (its method calls) to a specific {@code ServerSocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code ServerSocketChannel} the new instance is to
|
||||
* delegate (its method calls) to
|
||||
*/
|
||||
public DelegatingServerSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate.provider());
|
||||
|
||||
this.delegate = delegate;
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public SocketChannel accept()
|
||||
throws IOException
|
||||
{
|
||||
SocketChannel channel = delegate.accept();
|
||||
|
||||
return (channel == null) ? null : implAccept(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public ServerSocketChannel bind(SocketAddress local, int backlog)
|
||||
throws IOException
|
||||
{
|
||||
delegate.bind(local, backlog);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalAddress()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getLocalAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public <U> U getOption(SocketOption<U> name)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getOption(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows extenders to optionally configure (e.g. wrap) a
|
||||
* <tt>SocketChannel</tt> which has been accepted by {@link #delegate} and
|
||||
* before it is returned by {@link #accept()}.
|
||||
*
|
||||
* @param accepted the <tt>SocketChannel</tt> accepted by <tt>delegate</tt>
|
||||
* @return the <tt>SocketChannel</tt> to be returned by {@link #accept()}
|
||||
* (in place of <tt>accepted</tt>)
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected SocketChannel implAccept(SocketChannel accepted)
|
||||
throws IOException
|
||||
{
|
||||
return accepted;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
protected void implCloseSelectableChannel()
|
||||
throws IOException
|
||||
{
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
protected void implConfigureBlocking(boolean block)
|
||||
throws IOException
|
||||
{
|
||||
delegate.configureBlocking(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows extenders to optionally configure (e.g. wrap) the
|
||||
* {@code ServerSocket} of {@link #delegate} and before it is returned by
|
||||
* {@link #socket()}.
|
||||
*
|
||||
* @param socket the {@code ServerSocket} of {@code delegate}
|
||||
* @return the {@code ServerSocket} to be returned by {@link #socket()} (in
|
||||
* place of {@code socket})
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected ServerSocket implSocket(ServerSocket socket)
|
||||
throws IOException
|
||||
{
|
||||
return new DelegatingServerSocket(socket, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this {@code DelegatingServerSocketChannel} is bound.
|
||||
*
|
||||
* @return {@code true} if this instancei bound; otherwise, {@code false}
|
||||
*/
|
||||
public boolean isBound()
|
||||
{
|
||||
try
|
||||
{
|
||||
return getLocalAddress() != null;
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public <U> ServerSocketChannel setOption(SocketOption<U> name, U value)
|
||||
throws IOException
|
||||
{
|
||||
delegate.setOption(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Allows wrapping the {@code socket} of {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public ServerSocket socket()
|
||||
{
|
||||
ServerSocket socket = delegate.socket();
|
||||
|
||||
synchronized (socketSyncRoot)
|
||||
{
|
||||
if (this.socket == null)
|
||||
{
|
||||
if (socket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.socket = implSocket(socket);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new RuntimeException(ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (socket == null)
|
||||
{
|
||||
this.socket = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO For the sake of completeness, maybe check that the
|
||||
// value of this.socket still delegates to the latest value of
|
||||
// socket. However, the value of socket is very likely final so
|
||||
// do not bother with it at the time of this writing.
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public Set<SocketOption<?>> supportedOptions()
|
||||
{
|
||||
return delegate.supportedOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
}
|
||||
@ -208,10 +208,25 @@ public class DelegatingSocket
|
||||
this.delegate = delegate;
|
||||
this.channel = channel;
|
||||
|
||||
delegateAsDelegatingSocket
|
||||
= (delegate instanceof DelegatingSocket)
|
||||
? (DelegatingSocket) delegate
|
||||
: null;
|
||||
if (delegate instanceof DelegatingSocket)
|
||||
{
|
||||
// FIXME DelegatingSocketChannel was in need of a Socket which wraps
|
||||
// another Socket and reports a specific SocketChannel. Since
|
||||
// DelegatingSocket does that, DelegatingSocketChannel used it.
|
||||
// Unfortunately, it turned out after much debugging that
|
||||
// DelegatingSocket has the additional function of flagging where
|
||||
// UDP packets sent over TCP are to be framed. As a temporary fix,
|
||||
// do not treat the sockets of DelegatingSocketChannel as
|
||||
// DelegatingSockets.
|
||||
if (channel instanceof DelegatingSocketChannel)
|
||||
delegateAsDelegatingSocket = null;
|
||||
else
|
||||
delegateAsDelegatingSocket = (DelegatingSocket) delegate;
|
||||
}
|
||||
else
|
||||
{
|
||||
delegateAsDelegatingSocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -708,11 +723,11 @@ public class DelegatingSocket
|
||||
// Else, sends the packet to the final socket (outputStream).
|
||||
outputStream.write(p.getData(), p.getOffset(), p.getLength());
|
||||
|
||||
InetSocketAddress localAddress
|
||||
= (InetSocketAddress) super.getLocalSocketAddress();
|
||||
|
||||
if (DelegatingDatagramSocket.logNonStun(++nbSentPackets))
|
||||
{
|
||||
InetSocketAddress localAddress
|
||||
= (InetSocketAddress) super.getLocalSocketAddress();
|
||||
|
||||
StunStack.logPacketToPcap(
|
||||
p,
|
||||
true,
|
||||
|
||||
404
src/org/ice4j/socket/DelegatingSocketChannel.java
Normal file
404
src/org/ice4j/socket/DelegatingSocketChannel.java
Normal file
@ -0,0 +1,404 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
|
||||
import sun.nio.ch.*;
|
||||
|
||||
/**
|
||||
* Implements a {@code SocketChannel} which delegates (its method calls) to
|
||||
* another {@code SocketChannel}. In other words, the former wraps the latter.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class DelegatingSocketChannel<T extends SocketChannel>
|
||||
extends SocketChannel
|
||||
implements SelChImpl
|
||||
{
|
||||
/**
|
||||
* The {@link SocketChannel} this instance delegates (its method calls) to.
|
||||
*/
|
||||
protected final T delegate;
|
||||
|
||||
/**
|
||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||
* required by {@link Selector} and related functionality.
|
||||
*/
|
||||
protected final SelChImpl delegateAsSelChImpl;
|
||||
|
||||
/**
|
||||
* The {@code Socket} to be reported by this instance.
|
||||
*/
|
||||
private Socket socket;
|
||||
|
||||
/**
|
||||
* The <tt>Object</tt> which synchronizes the access to {@link #socket}.
|
||||
*/
|
||||
private final Object socketSyncRoot = new Object();
|
||||
|
||||
/**
|
||||
* Initializes a new {@code DelegatingSocketChannel} instance which is to
|
||||
* delegate (its method calls) to a specific {@code SocketChannel}.
|
||||
*
|
||||
* @param delegate the {@code SocketChannel} the new instance is to delegate
|
||||
* (its method calls) to
|
||||
*/
|
||||
public DelegatingSocketChannel(T delegate)
|
||||
{
|
||||
super(delegate.provider());
|
||||
|
||||
this.delegate = delegate;
|
||||
|
||||
delegateAsSelChImpl
|
||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public SocketChannel bind(SocketAddress local)
|
||||
throws IOException
|
||||
{
|
||||
delegate.bind(local);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean connect(SocketAddress remote)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.connect(remote);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean finishConnect()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.finishConnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public FileDescriptor getFD()
|
||||
{
|
||||
return delegateAsSelChImpl.getFD();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int getFDVal()
|
||||
{
|
||||
return delegateAsSelChImpl.getFDVal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalAddress()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getLocalAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public <U> U getOption(SocketOption<U> name)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getOption(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getRemoteAddress()
|
||||
throws IOException
|
||||
{
|
||||
return delegate.getRemoteAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
protected void implCloseSelectableChannel()
|
||||
throws IOException
|
||||
{
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
protected void implConfigureBlocking(boolean block)
|
||||
throws IOException
|
||||
{
|
||||
delegate.configureBlocking(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows extenders to optionally configure (e.g. wrap) the {@code Socket}
|
||||
* of {@link #delegate} and before it is returned by {@link #socket()}.
|
||||
*
|
||||
* @param socket the {@code Socket} of {@code delegate}
|
||||
* @return the {@code Socket} to be returned by {@link #socket()} (in place
|
||||
* of {@code socket})
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected Socket implSocket(Socket socket)
|
||||
throws IOException
|
||||
{
|
||||
return new DelegatingSocket(socket, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isConnected()
|
||||
{
|
||||
return delegate.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isConnectionPending()
|
||||
{
|
||||
return delegate.isConnectionPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void kill()
|
||||
throws IOException
|
||||
{
|
||||
delegateAsSelChImpl.kill();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int read(ByteBuffer dst)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.read(dst);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public long read(ByteBuffer[] dsts, int offset, int length)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.read(dsts, offset, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public <U> SocketChannel setOption(SocketOption<U> name, U value)
|
||||
throws IOException
|
||||
{
|
||||
delegate.setOption(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public SocketChannel shutdownInput()
|
||||
throws IOException
|
||||
{
|
||||
delegate.shutdownInput();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate} and returns {@code this}.
|
||||
*/
|
||||
@Override
|
||||
public SocketChannel shutdownOutput()
|
||||
throws IOException
|
||||
{
|
||||
delegate.shutdownOutput();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Allows wrapping the {@code socket} of {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public Socket socket()
|
||||
{
|
||||
Socket socket = delegate.socket();
|
||||
|
||||
synchronized (socketSyncRoot)
|
||||
{
|
||||
if (this.socket == null)
|
||||
{
|
||||
if (socket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.socket = implSocket(socket);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new RuntimeException(ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (socket == null)
|
||||
{
|
||||
this.socket = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO For the sake of completeness, maybe check that the
|
||||
// value of this.socket still delegates to the latest value of
|
||||
// socket. However, the value of socket is very likely final so
|
||||
// do not bother with it at the time of this writing.
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public Set<SocketOption<?>> supportedOptions()
|
||||
{
|
||||
return delegate.supportedOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||
{
|
||||
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public int write(ByteBuffer src)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.write(src);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Forwards to {@link #delegate}.
|
||||
*/
|
||||
@Override
|
||||
public long write(ByteBuffer[] srcs, int offset, int length)
|
||||
throws IOException
|
||||
{
|
||||
return delegate.write(srcs, offset, length);
|
||||
}
|
||||
}
|
||||
267
src/org/ice4j/socket/HttpDemuxFilter.java
Normal file
267
src/org/ice4j/socket/HttpDemuxFilter.java
Normal file
@ -0,0 +1,267 @@
|
||||
/*
|
||||
* Jitsi Videobridge, OpenSource video conferencing.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.net.*;
|
||||
import java.nio.charset.*;
|
||||
|
||||
import org.ice4j.ice.harvest.*;
|
||||
|
||||
/**
|
||||
* Implements a {@link DatagramPacketFilter} which allows demultiplexing HTTP(S)
|
||||
* out of {@link MuxServerSocketChannel}. Accepts HTTP, SSL v2, and TLS. Rejects
|
||||
* Google TURN SSLTCP.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class HttpDemuxFilter
|
||||
implements DatagramPacketFilter
|
||||
{
|
||||
/**
|
||||
* The US-ASCII {@code byte}s of {@link #REQUEST_METHOD_STRINGS}. Explicitly
|
||||
* defined for the purposes of performance.
|
||||
*/
|
||||
private static byte[][] REQUEST_METHOD_BYTES;
|
||||
|
||||
/**
|
||||
* The maximum US-ASCII character with which an element of
|
||||
* {@link #REQUEST_METHOD_STRINGS} starts.
|
||||
*/
|
||||
private static final char REQUEST_METHOD_MAX_CHAR;
|
||||
|
||||
/**
|
||||
* The maximum length in {@code byte}s of a US-ASCII representation of an
|
||||
* HTTP request method supported by the class {@code HttpDemuxFilter}.
|
||||
*/
|
||||
public static final int REQUEST_METHOD_MAX_LENGTH;
|
||||
|
||||
/**
|
||||
* The minimum US-ASCII character with which an element of
|
||||
* {@link #REQUEST_METHOD_STRINGS} starts.
|
||||
*/
|
||||
private static final char REQUEST_METHOD_MIN_CHAR;
|
||||
|
||||
/**
|
||||
* The HTTP request methods recognized by the class {@code HttpDemuxFilter}.
|
||||
*/
|
||||
private static String[] REQUEST_METHOD_STRINGS
|
||||
= {
|
||||
"CONNECT",
|
||||
"DELETE",
|
||||
"GET",
|
||||
"HEAD",
|
||||
"MOVE",
|
||||
"OPTIONS",
|
||||
"POST",
|
||||
"PRI",
|
||||
"PROXY",
|
||||
"PUT",
|
||||
"TRACE"
|
||||
};
|
||||
|
||||
/**
|
||||
* The minimum number of bytes required by the class {@code HttpDemuxFilter}
|
||||
* in order to declare that (the data of) a specific {@link DatagramPacket}
|
||||
* represents TLS.
|
||||
*/
|
||||
public static final int TLS_MIN_LENGTH = 11;
|
||||
|
||||
static
|
||||
{
|
||||
// Gather statistics about the supported HTTP request methods in order
|
||||
// to speed up the analysis of DatagramPackets later on.
|
||||
char maxChar = 'A';
|
||||
int maxLength = Integer.MAX_VALUE;
|
||||
char minChar = 'Z';
|
||||
Charset ascii = Charset.forName("US-ASCII");
|
||||
|
||||
REQUEST_METHOD_BYTES = new byte[REQUEST_METHOD_STRINGS.length][];
|
||||
for (int i = 0; i < REQUEST_METHOD_STRINGS.length; i++)
|
||||
{
|
||||
String s = REQUEST_METHOD_STRINGS[i];
|
||||
|
||||
if (s != null && s.length() != 0)
|
||||
{
|
||||
char ch = s.charAt(0);
|
||||
byte[] bytes = s.getBytes(ascii);
|
||||
int length = bytes.length;
|
||||
|
||||
if (maxChar < ch)
|
||||
maxChar = ch;
|
||||
if (maxLength < length)
|
||||
maxLength = length;
|
||||
if (minChar > ch)
|
||||
minChar = ch;
|
||||
|
||||
REQUEST_METHOD_BYTES[i] = bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
REQUEST_METHOD_BYTES[i] = new byte[0];
|
||||
}
|
||||
}
|
||||
REQUEST_METHOD_MAX_CHAR = maxChar;
|
||||
REQUEST_METHOD_MAX_LENGTH = maxLength;
|
||||
REQUEST_METHOD_MIN_CHAR = minChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a specific {@link DatagramPacket} looks like the
|
||||
* beginning of HTTP(S) client communication. Accepts HTTP, SSL v2, and
|
||||
* TLS. Rejects Google TURN SSLTCP.
|
||||
*
|
||||
* @param p the {@code DatagramPacket} to analyze
|
||||
* @return {@code true} if {@code p} looks like the beginning of HTTP(S)
|
||||
* client communication; otherwise, {@code false}
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(DatagramPacket p)
|
||||
{
|
||||
int len = p.getLength();
|
||||
boolean accept = false;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
byte[] buf = p.getData();
|
||||
int off = p.getOffset();
|
||||
// The first bytes of HTTP, SSL v2, and TCP are different so quickly
|
||||
// determine which one of the three is possible and, respectively,
|
||||
// which two of the three are impossible.
|
||||
int b0 = 0xFF & buf[off];
|
||||
boolean http, sslv2, tls;
|
||||
|
||||
if (b0 == 22 /* TLS handshake */)
|
||||
{
|
||||
http = false;
|
||||
sslv2 = false;
|
||||
tls = true;
|
||||
}
|
||||
else if (b0 > 0x80 /* SSL v2 client hello */)
|
||||
{
|
||||
http = false;
|
||||
sslv2 = true;
|
||||
tls = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
http = true;
|
||||
sslv2 = false;
|
||||
tls = false;
|
||||
}
|
||||
|
||||
// HTTP
|
||||
if (http)
|
||||
{
|
||||
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
|
||||
// HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
|
||||
if (b0 >= REQUEST_METHOD_MIN_CHAR
|
||||
&& b0 <= REQUEST_METHOD_MAX_CHAR
|
||||
&& len >= REQUEST_METHOD_MAX_LENGTH + 1 /* SP */)
|
||||
{
|
||||
// Match a supported HTTP request method.
|
||||
for (byte[] bytes : REQUEST_METHOD_BYTES)
|
||||
{
|
||||
int length = bytes.length;
|
||||
|
||||
if (buf[off + length] == ' ' /* SP */)
|
||||
{
|
||||
boolean equals = true;
|
||||
|
||||
for (int i = 1, j = off + 1; i < length; i++, j++)
|
||||
{
|
||||
if (bytes[i] != buf[j])
|
||||
{
|
||||
equals = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (equals)
|
||||
{
|
||||
accept = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||
// looking at the first byte. If p didn't look like HTTP here,
|
||||
// then it will not look like SSL v2 or TLS.
|
||||
return accept;
|
||||
}
|
||||
|
||||
// HTTPS
|
||||
if (tls)
|
||||
{
|
||||
// 1 byte ContentType type = handshake(22)
|
||||
// 2 bytes ProtocolVersion version = { major(3) , minor<1..3> }
|
||||
// 2 bytes uint16 length
|
||||
// 1 byte HandshakeType msg_type = client_hello(1)
|
||||
// 3 bytes uint24 length
|
||||
// 2 bytes ProtocolVersion client_version
|
||||
if (len >= TLS_MIN_LENGTH
|
||||
&& /* major */ (0xFF & buf[off + 1]) == 3)
|
||||
{
|
||||
int minor = 0xFF & buf[off + 2];
|
||||
|
||||
if (1 <= minor
|
||||
&& minor <= 3
|
||||
&& /* msg_type */ (0xFF & buf[off + 5])
|
||||
== /* client_hello */ 1
|
||||
&& /* major */ (0xFF & buf[off + 9]) == 3)
|
||||
{
|
||||
minor = 0xFF & buf[off + 10];
|
||||
if (1 <= minor && minor <= 3)
|
||||
accept = true;
|
||||
}
|
||||
}
|
||||
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||
// looking at the first byte. If p didn't look like TLS here,
|
||||
// then it will not look like HTTP or SSL v2.
|
||||
return accept;
|
||||
}
|
||||
if (sslv2)
|
||||
{
|
||||
final byte[] googleTurnSslTcp
|
||||
= GoogleTurnSSLCandidateHarvester.SSL_CLIENT_HANDSHAKE;
|
||||
|
||||
// 2 bytes uint15 length
|
||||
// 1 byte uint8 msg_type = 1
|
||||
// 2 bytes Version version
|
||||
if (len > 5
|
||||
&& len >= googleTurnSslTcp.length
|
||||
&& /* msg_type */ (0xFF & buf[off + 2]) == 1
|
||||
&& /* major */ (0xFF & buf[off + 3]) == 3)
|
||||
{
|
||||
int minor = 0xFF & buf[off + 4];
|
||||
|
||||
if (1 <= minor && minor <= 3)
|
||||
{
|
||||
// Reject Google TURN SSLTCP.
|
||||
boolean equals = true;
|
||||
|
||||
for (int i = 0, iEnd = googleTurnSslTcp.length, j = off;
|
||||
i < iEnd;
|
||||
i++, j++)
|
||||
{
|
||||
if (googleTurnSslTcp[i] != buf[j])
|
||||
{
|
||||
equals = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
accept = !equals;
|
||||
}
|
||||
}
|
||||
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||
// looking at the first byte. If p didn't look like SSL v2 here,
|
||||
// then it will not look like HTTP or TLS.
|
||||
return accept;
|
||||
}
|
||||
}
|
||||
return accept;
|
||||
}
|
||||
}
|
||||
@ -50,7 +50,7 @@ public class IceTcpSocketWrapper
|
||||
{
|
||||
socket = delegate;
|
||||
|
||||
if(delegate instanceof DelegatingSocket)
|
||||
if (delegate instanceof DelegatingSocket)
|
||||
{
|
||||
inputStream = null;
|
||||
outputStream = null;
|
||||
|
||||
1323
src/org/ice4j/socket/MuxServerSocketChannel.java
Normal file
1323
src/org/ice4j/socket/MuxServerSocketChannel.java
Normal file
File diff suppressed because it is too large
Load Diff
97
src/org/ice4j/socket/PreReadSocketChannel.java
Normal file
97
src/org/ice4j/socket/PreReadSocketChannel.java
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.socket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
/**
|
||||
* Prepends a {@link DatagramPacket} to a {@link SocketChannel}. In other words,
|
||||
* enables looking ahead at the {@code byte}s of a {@code SocketChannel}.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class PreReadSocketChannel
|
||||
extends DelegatingSocketChannel<SocketChannel>
|
||||
{
|
||||
/**
|
||||
* The {@link DatagramPacket} which is to be prepended to the
|
||||
* {@link SocketChannel} which is the {@link #delegate} of this
|
||||
* {@link DelegatingSocketChannel}.
|
||||
*/
|
||||
private final DatagramPacket preRead;
|
||||
|
||||
/**
|
||||
* Initializes a new {@code PreReadSocketChannel} instance which is to
|
||||
* prepend a specific {@link DatagrapPacket} to a specific
|
||||
* {@link SocketChannel}.
|
||||
*
|
||||
* @param preRead the {@code DatagramPacket} to prepend to {@code delegate}
|
||||
* @param delegate the {@code SocketChannel} to prepend {@code preRead} to
|
||||
*/
|
||||
public PreReadSocketChannel(DatagramPacket preRead, SocketChannel delegate)
|
||||
{
|
||||
super(delegate);
|
||||
|
||||
this.preRead = preRead;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Reads from {@link #preRead} first (until it is fully read) and then
|
||||
* continues with the {@code super} implementation.
|
||||
*/
|
||||
@Override
|
||||
public int read(ByteBuffer dst)
|
||||
throws IOException
|
||||
{
|
||||
synchronized (preRead)
|
||||
{
|
||||
int len = preRead.getLength();
|
||||
int read;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
int toRead = Math.min(len, dst.remaining());
|
||||
|
||||
if (toRead > 0)
|
||||
{
|
||||
byte[] buf = preRead.getData();
|
||||
int off = preRead.getOffset();
|
||||
|
||||
dst.put(buf, off, toRead);
|
||||
read = toRead;
|
||||
preRead.setData(buf, off + read, len - read);
|
||||
}
|
||||
else
|
||||
{
|
||||
read = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
read = 0;
|
||||
}
|
||||
|
||||
if (read == 0)
|
||||
{
|
||||
read = super.read(dst);
|
||||
}
|
||||
else if (dst.hasRemaining())
|
||||
{
|
||||
int superRead = super.read(dst);
|
||||
|
||||
if (superRead > 0)
|
||||
read += superRead;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user