Implements DatagramSocket multiplexing for the purposes of ICE.

This commit is contained in:
lubomir.marinov 2010-02-01 19:34:56 +00:00
parent 3d1538865a
commit ef3acb3028
4 changed files with 1122 additions and 0 deletions

View File

@ -0,0 +1,32 @@
/*
* 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.net.*;
/**
* Represents a filter which selects or deselects <tt>DatagramPacket</tt>s.
*
* @author Lubomir Marinov
*/
public interface DatagramPacketFilter
{
/**
* Determines whether a specific <tt>DatagramPacket</tt> is accepted by this
* filter i.e. whether the caller should include the specified
* <tt>DatagramPacket</tt> in a selection based on this filter.
*
* @param p the <tt>DatagramPacket</tt> which is to be checked whether it is
* accepted by this filter
* @return <tt>true</tt> if this filter accepts the specified
* <tt>DatagramPacket</tt> i.e. if the caller should include the specified
* <tt>DatagramPacket</tt> in a selection based on this filter; otherwise,
* <tt>false</tt>
*/
public boolean accept(DatagramPacket p);
}

View File

@ -0,0 +1,626 @@
/*
* 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.*;
/**
* Represents a <tt>DatagramSocket</tt> which receives <tt>DatagramPacket</tt>s
* selected by a <tt>DatagramPacketFilter</tt> from a
* <tt>MultiplexingDatagramSocket</tt>. The associated
* <tt>MultiplexingDatagramSocket</tt> is the actual <tt>DatagramSocket</tt>
* which reads the <tt>DatagramPacket</tt>s from the network. The
* <tt>DatagramPacket</tt>s received through the
* <tt>MultiplexedDatagramSocket</tt> will not be received through the
* associated <tt>MultiplexingDatagramSocket</tt>.
*
* @author Lubomir Marinov
*/
public class MultiplexedDatagramSocket
extends DatagramSocket
{
/**
* The <tt>DatagramPacketFilter</tt> which determines which
* <tt>DatagramPacket</tt>s read from the network by {@link #multiplexing}
* are to be received through this instance.
*/
private final DatagramPacketFilter filter;
/**
* The <tt>MultiplexingDatagramSocket</tt> which does the actual reading
* from the network and which forwards <tt>DatagramPacket</tt>s accepted by
* {@link #filter} for receipt to this instance.
*/
private final MultiplexingDatagramSocket multiplexing;
/**
* The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}.
*/
final List<DatagramPacket> received
= new LinkedList<DatagramPacket>();
/**
* Initializes a new <tt>MultiplexedDatagramSocket</tt> which is unbound and
* filters <tt>DatagramPacket</tt>s away from a specific
* <tt>MultiplexingDatagramSocket</tt> using a specific
* <tt>DatagramPacketFilter</tt>.
*
* @param multiplexing the <tt>MultiplexingDatagramSocket</tt> which does
* the actual reading from the network and which forwards
* <tt>DatagramPacket</tt>s accepted by the specified <tt>filter</tt> to the
* new instance
* @param filter the <tt>DatagramPacketFilter</tt> which determines which
* <tt>DatagramPacket</tt>s read from the network by the specified
* <tt>multiplexing</tt> are to be received through the new instance
* @throws SocketException if the socket could not be opened
*/
MultiplexedDatagramSocket(
MultiplexingDatagramSocket multiplexing,
DatagramPacketFilter filter)
throws SocketException
{
/*
* Even if MultiplexingDatagramSocket allows MultiplexedDatagramSocket
* to perform bind, binding in the super will not execute correctly this
* early in the construction because the multiplexing field is not set
* yet. That is why MultiplexedDatagramSocket does not currently support
* bind at construction time.
*/
super((SocketAddress) null);
if (multiplexing == null)
throw new NullPointerException("multiplexing");
this.multiplexing = multiplexing;
this.filter = filter;
}
/**
* Binds this <tt>DatagramSocket</tt> to a specific address and port.
* <p>
* If the address is <tt>null</tt>, then the system will pick up an
* ephemeral port and a valid local address to bind the socket.
*</p>
*
* @param addr the address and port to bind to
* @throws SocketException if any error happens during the bind, or if the
* socket is already bound
* @throws SecurityException if a security manager exists and its
* <tt>checkListen</tt> method doesn't allow the operation
* @throws IllegalArgumentException if <tt>addr</tt> is a
* <tt>SocketAddress</tt> subclass not supported by this socket
* @see DatagramSocket#bind(SocketAddress)
*/
@Override
public void bind(SocketAddress addr)
throws SocketException
{
multiplexing.bind(addr);
}
/**
* Closes this datagram socket.
* <p>
* Any thread currently blocked in {@link #receive(DatagramPacket)} upon
* this socket will throw a {@link SocketException}.
* </p>
*
* @see DatagramSocket#close()
*/
@Override
public void close()
{
multiplexing.close(this);
}
/**
* Connects the socket to a remote address for this socket. When a socket is
* connected to a remote address, packets may only be sent to or received
* from that address. By default a datagram socket is not connected.
* <p>
* If the remote destination to which the socket is connected does not
* exist, or is otherwise unreachable, and if an ICMP destination
* unreachable packet has been received for that address, then a subsequent
* call to {@link #send(DatagramPacket)} or {@link #receive(DatagramPacket)}
* may throw a <tt>PortUnreachableException</tt>. Note, there is no
* guarantee that the exception will be thrown.
* </p>
*
* @param address the remote address for the socket
* @param port the remote port for the socket
* @throws IllegalArgumentException if the address is <tt>null</tt>, or the
* port is out of range
* @throws SecurityException if the caller is not allowed to send datagrams
* to and receive datagrams from the address and port
* @see DatagramSocket#connect(InetAddress, int)
*/
@Override
public void connect(InetAddress address, int port)
{
multiplexing.connect(address, port);
}
/**
* Connects this socket to a remote socket address.
*
* @param addr the remote address
* @throws SocketException if the connect fails
* @throws IllegalArgumentException if <tt>addr</tt> is <tt>null</tt> or a
* <tt>SocketAddress</tt> subclass not supported by this socket
* @see DatagramSocket#connect(SocketAddress)
*/
@Override
public void connect(SocketAddress addr)
throws SocketException
{
multiplexing.connect(addr);
}
/**
* Disconnects the socket. This does nothing if the socket is not connected.
*
* @see DatagramSocket#disconnect()
*/
@Override
public void disconnect()
{
multiplexing.disconnect();
}
/**
* Tests if <tt>SO_BROADCAST</tt> is enabled.
*
* @return a <tt>boolean</tt> indicating whether or not
* <tt>SO_BROADCAST</tt> is enabled
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getBroadcast()
*/
@Override
public boolean getBroadcast()
throws SocketException
{
return multiplexing.getBroadcast();
}
/**
* Returns the unique {@link DatagramChannel} object associated with this
* datagram socket, if any.
* <p>
* A datagram socket will have a channel if, and only if, the channel itself
* was created via the {@link DatagramChannel#open()} method
* </p>
*
* @return the datagram channel associated with this datagram socket, or
* <tt>null</tt> if this socket was not created for a channel
* @see DatagramSocket#getChannel()
*/
@Override
public DatagramChannel getChannel()
{
return multiplexing.getChannel();
}
/**
* Gets the <tt>DatagramPacketFilter</tt> which determines which
* <tt>DatagramPacket</tt>s read from the network are to be received through
* this <tt>DatagramSocket</tt>.
*
* @return the <tt>DatagramPacketFilter</tt> which determines which
* <tt>DatagramPacket</tt>s read from the network are to be received through
* this <tt>DatagramSocket</tt>
*/
public DatagramPacketFilter getFilter()
{
return filter;
}
/**
* Returns the address to which this socket is connected. Returns
* <tt>null</tt> if the socket is not connected.
*
* @return the address to which this socket is connected
* @see DatagramSocket#getInetAddress()
*/
@Override
public InetAddress getInetAddress()
{
return multiplexing.getInetAddress();
}
/**
* Gets the local address to which the socket is bound.
* <p>
* If there is a security manager, its <tt>checkConnect</tt> method is first
* called with the host address and <tt>-1</tt> as its arguments to see if
* the operation is allowed.
*
* @return the local address to which the socket is bound, or an
* <tt>InetAddress</tt> representing any local address if either the socket
* is not bound, or the security manager <tt>checkConnect</tt> method does
* not allow the operation
* @see DatagramSocket#getLocalAddress()
*/
@Override
public InetAddress getLocalAddress()
{
return multiplexing.getLocalAddress();
}
/**
* Returns the port number on the local host to which this socket is bound.
*
* @return the port number on the local host to which this socket is bound
* @see DatagramSocket#getLocalPort()
*/
@Override
public int getLocalPort()
{
return multiplexing.getLocalPort();
}
/**
* Returns the address of the endpoint this socket is bound to, or
* <tt>null</tt> if it is not bound yet.
*
* @return a <tt>SocketAddress</tt> representing the local endpoint of this
* socket, or <tt>null</tt> if it is not bound yet
* @see DatagramSocket#getLocalSocketAddress()
*/
@Override
public SocketAddress getLocalSocketAddress()
{
return multiplexing.getLocalSocketAddress();
}
/**
* Returns the port for this socket. Returns <tt>-1</tt> if the socket is
* not connected.
*
* @return the port to which this socket is connected
* @see DatagramSocket#getPort()
*/
@Override
public int getPort()
{
return multiplexing.getPort();
}
/**
* Gets the value of the <tt>SO_RCVBUF</tt> option for this
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
* input on this <tt>DatagramSocket</tt>.
*
* @return the value of the <tt>SO_RCVBUF</tt> option for this
* <tt>DatagramSocket</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getReceiveBufferSize()
*/
@Override
public int getReceiveBufferSize()
throws SocketException
{
return multiplexing.getReceiveBufferSize();
}
/**
* Returns the address of the endpoint this socket is connected to, or
* <tt>null</tt> if it is unconnected.
*
* @return a <tt>SocketAddress</tt> representing the remote endpoint of this
* socket, or <tt>null</tt> if it is not connected yet
* @see DatagramSocket#getRemoteSocketAddress()
*/
@Override
public SocketAddress getRemoteSocketAddress()
{
return multiplexing.getRemoteSocketAddress();
}
/**
* Tests if <tt>SO_REUSEADDR</tt> is enabled.
*
* @return a <tt>boolean</tt> indicating whether or not
* <tt>SO_REUSEADDR</tt> is enabled
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getReuseAddress()
*/
@Override
public boolean getReuseAddress()
throws SocketException
{
return multiplexing.getReuseAddress();
}
/**
* Gets the value of the <tt>SO_SNDBUF</tt> option for this
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
* output on this <tt>DatagramSocket</tt>.
*
* @return the value of the <tt>SO_SNDBUF</tt> option for this
* <tt>DatagramSocket</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getSendBufferSize()
*/
@Override
public int getSendBufferSize()
throws SocketException
{
return multiplexing.getSendBufferSize();
}
/**
* Retrieves setting for <tt>SO_TIMEOUT</tt>. Zero returned implies that
* the option is disabled (i.e., timeout of infinity).
*
* @return the setting for <tt>SO_TIMEOUT</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getSoTimeout()
*/
@Override
public int getSoTimeout()
throws SocketException
{
return multiplexing.getSoTimeout();
}
/**
* Gets the traffic class or type-of-service in the IP datagram header for
* packets sent from this <tt>DatagramSocket</tt>.
* <p>
* As the underlying network implementation may ignore the traffic class or
* type-of-service set using {@link #setTrafficClass(int)} this method may
* return a different value than was previously set using the
* {@link #setTrafficClass(int)} method on this <tt>DatagramSocket</tt>.
* </p>
*
* @return the traffic class or type-of-service already set
* @throws SocketException if there is an error obtaining the traffic class
* or type-of-service value
* @see DatagramSocket#getTrafficClass()
*/
@Override
public int getTrafficClass()
throws SocketException
{
return multiplexing.getTrafficClass();
}
/**
* Returns the binding state of the socket.
*
* @return <tt>true</tt> if the socket successfully bound to an address;
* otherwise, <tt>false</tt>
* @see DatagramSocket#isBound()
*/
@Override
public boolean isBound()
{
return multiplexing.isBound();
}
/**
* Returns whether the socket is closed or not.
*
* @return <tt>true</tt> if the socket has been closed; otherwise,
* <tt>false</tt>
* @see DatagramSocket#isClosed()
*/
@Override
public boolean isClosed()
{
return multiplexing.isClosed();
}
/**
* Returns the connection state of the socket.
*
* @return <tt>true</tt> if the socket successfully connected to a server;
* otherwise, <tt>false</tt>
* @see DatagramSocket#isConnected()
*/
@Override
public boolean isConnected()
{
return multiplexing.isConnected();
}
/**
* Receives a datagram packet from this socket. When this method returns,
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
* The datagram packet also contains the sender's IP address, and the port
* number on the sender's machine.
* <p>
* This method blocks until a datagram is received. The <tt>length</tt>
* field of the datagram packet object contains the length of the received
* message. If the message is longer than the packet's length, the message
* is truncated.
* </p>
* <p>
* If there is a security manager, a packet cannot be received if the
* security manager's <tt>checkAccept</tt> method does not allow it.
* </p>
*
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data
* @throws IOException if an I/O error occurs
* @see DatagramSocket#receive(DatagramPacket)
*/
@Override
public void receive(DatagramPacket p)
throws IOException
{
multiplexing.receive(this, p);
}
/**
* Sends a datagram packet from this socket. The <tt>DatagramPacket</tt>
* includes information indicating the data to be sent, its length, the IP
* address of the remote host, and the port number on the remote host.
* <p>
* If there is a security manager, and the socket is not currently connected
* to a remote address, this method first performs some security checks.
* First, if <tt>p.getAddress().isMulticastAddress()</tt> is true, this
* method calls the security manager's <tt>checkMulticast</tt> method with
* <tt>p.getAddress()</tt> as its argument. If the evaluation of that
* expression is <tt>false</tt>, this method instead calls the security
* manager's <tt>checkConnect</tt> method with arguments
* <tt>p.getAddress().getHostAddress()</tt> and <tt>p.getPort()</tt>. Each
* call to a security manager method could result in a
* <tt>SecurityException</tt> if the operation is not allowed.
* </p>
*
* @param p the <tt>DatagramPacket</tt> to be sent
* @throws IOException if an I/O error occurs
* @see DatagramSocket#send(DatagramPacket)
*/
@Override
public void send(DatagramPacket p)
throws IOException
{
multiplexing.send(p);
}
/**
* Enables/disables <tt>SO_BROADCAST</tt>.
*
* @param on whether or not to have broadcast turned on
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#setBroadcast(boolean)
*/
@Override
public void setBroadcast(boolean on)
throws SocketException
{
multiplexing.setBroadcast(on);
}
/**
* Sets the <tt>SO_RCVBUF</tt> option to the specified value for this
* <tt>DatagramSocket</tt>. The <tt>SO_RCVBUF</tt> option is used by the
* network implementation as a hint to size the underlying network I/O
* buffers. The <tt>SO_RCVBUF</tt> setting may also be used by the network
* implementation to determine the maximum size of the packet that can be
* received on this socket.
* <p>
* Because <tt>SO_RCVBUF</tt> is a hint, applications that want to verify
* what size the buffers were set to should call
* {@link #getReceiveBufferSize()}.
* </p>
*
* @param size the size to which to set the receive buffer size. The value
* must be greater than zero
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @throws IllegalArgumentException if the value is zero or is negative
* @see DatagramSocket#setReceiveBufferSize(int)
*/
@Override
public void setReceiveBufferSize(int size)
throws SocketException
{
multiplexing.setReceiveBufferSize(size);
}
/**
* Enables/disables the <tt>SO_REUSEADDR</tt> socket option.
*
* @param on whether to enable or disable the <tt>SO_REUSEADDR</tt> socket
* option
* @throws SocketException if an error occurs enabling or disabling the
* <tt>SO_RESUEADDR</tt> socket option, or the socket is closed
* @see DatagramSocket#setReuseAddress(boolean)
*/
@Override
public void setReuseAddress(boolean on)
throws SocketException
{
multiplexing.setReuseAddress(on);
}
/**
* Sets the <tt>SO_SNDBUF</tt> option to the specified value for this
* <tt>DatagramSocket</tt>. The <tt>SO_SNDBUF</tt> option is used by the
* network implementation as a hint to size the underlying network I/O
* buffers. The <tt>SO_SNDBUF</tt> setting may also be used by the network
* implementation to determine the maximum size of the packet that can be
* sent on this socket.
* <p>
* As <tt>SO_SNDBUF</tt> is a hint, applications that want to verify what
* size the buffer is should call {@link #getSendBufferSize()}.
* </p>
* <p>
* Increasing the buffer size may allow multiple outgoing packets to be
* queued by the network implementation when the send rate is high.
* </p>
*
* @param size the size to which to set the send buffer size. The value must
* be greater than zero
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @throws IllegalArgumentException if the value is zero or is negative
* @see DatagramSocket#setSendBufferSize(int)
*/
@Override
public void setSendBufferSize(int size)
throws SocketException
{
multiplexing.setSendBufferSize(size);
}
/**
* Enables/disables <tt>SO_TIMEOUT</tt> with the specified timeout, in
* milliseconds. With this option set to a non-zero timeout, a call to
* {@link #receive(DatagramPacket)} for this <tt>DatagramSocket</tt> will
* block for only this amount of time. If the timeout expires, a
* <tt>SocketTimeoutException</tt> is raised, though the
* <tt>DatagramSocket</tt> is still valid. The option must be enabled prior
* to entering the blocking operation to have effect. The timeout must be
* greater than zero. A timeout of zero is interpreted as an infinite
* timeout.
*
* @param timeout the specified timeout in milliseconds
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#setSoTimeout(int)
*/
@Override
public void setSoTimeout(int timeout)
throws SocketException
{
multiplexing.setSoTimeout(timeout);
}
/**
* Sets traffic class or type-of-service octet in the IP datagram header for
* datagrams sent from this <tt>DatagramSocket</tt>. As the underlying
* network implementation may ignore this value applications should consider
* it a hint.
*
* @param tc an <tt>int</tt> value for the bitset
* @throws SocketException if there is an error setting the traffic class or
* type-of-service
* @see DatagramSocket#setTrafficClass(int)
*/
@Override
public void setTrafficClass(int tc)
throws SocketException
{
multiplexing.setTrafficClass(tc);
}
}

View File

@ -0,0 +1,414 @@
/*
* 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.util.*;
/**
* Represents a <tt>DatagramSocket</tt> which allows filtering
* <tt>DatagramPacket</tt>s it reads from the network using
* <tt>DatagramPacketFilter</tt>s so that the <tt>DatagramPacket</tt>s do not
* get received through it but through associated
* <tt>MultiplexedDatagramSocket</tt>s.
*
* @author Lubomir Marinov
*/
public class MultiplexingDatagramSocket
extends DatagramSocket
{
/**
* The constant which represents an empty array with
* <tt>MultiplexedDatagramSocket</tt> element type. Explicitly defined in
* order to reduce allocations.
*/
private static final MultiplexedDatagramSocket[] NO_SOCKETS
= new MultiplexedDatagramSocket[0];
/**
* The indicator which determines whether this <tt>DatagramSocket</tt> is
* currently reading from the network using
* {@link DatagramSocket#receive(DatagramPacket)}. When <tt>true</tt>,
* subsequent requests to read from the network will be blocked until the
* current read is finished.
*/
private boolean inReceive = false;
/**
* The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s
* of {@link #sockets} at the time of the reading from the network.
*/
private final List<DatagramPacket> received
= new LinkedList<DatagramPacket>();
/**
* The <tt>Object</tt> which synchronizes the access to {@link #inReceive}.
*/
private final Object receiveSyncRoot = new Object();
/**
* The <tt>MultiplexedDatagramSocket</tt>s filtering
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
*/
private MultiplexedDatagramSocket[] sockets = NO_SOCKETS;
/**
* The <tt>Object</tt> which synchronizes the access to the {@link #sockets}
* field of this instance.
*/
private final Object socketsSyncRoot = new Object();
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket()
*/
public MultiplexingDatagramSocket()
throws SocketException
{
super();
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* socket address.
* <p>
* If the specified local socket address is <tt>null</tt>, creates an
* unbound socket.
* </p>
*
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
* unbound socket
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(SocketAddress)
*/
public MultiplexingDatagramSocket(SocketAddress bindaddr)
throws SocketException
{
super(bindaddr);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to the specified
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @param port the port to bind the new socket to
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int)
*/
public MultiplexingDatagramSocket(int port)
throws SocketException
{
super(port);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* address. The local port must be between 0 and 65535 inclusive. If the IP
* address is 0.0.0.0, the socket will be bound to the wildcard address, an
* IP address chosen by the kernel.
*
* @param port the local port to bind the new socket to
* @param laddr the local address to bind the new socket to
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int, InetAddress)
*/
public MultiplexingDatagramSocket(int port, InetAddress laddr)
throws SocketException
{
super(port, laddr);
}
/**
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
* <tt>DatagramPacket</tt> are clones of the specified
* <tt>DatagramPacket</tt>.
*
* @param p the <tt>DatagramPacket</tt> to clone
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
* specified <tt>DatagramPacket</tt>
*/
static DatagramPacket clone(DatagramPacket p)
{
synchronized (p)
{
byte[] pData = p.getData();
byte[] cData = (pData == null) ? pData : pData.clone();
return
new DatagramPacket(
cData,
p.getOffset(),
p.getLength(),
p.getAddress(),
p.getPort());
}
}
/**
* Closes a specific <tt>MultiplexedDatagramSocket</tt> which filters
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
*
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> to close
*/
void close(MultiplexedDatagramSocket multiplexed)
{
synchronized (socketsSyncRoot)
{
int socketCount = sockets.length;
for (int i = 0; i < socketCount; i++)
if (sockets[i].equals(multiplexed))
{
if (socketCount == 1)
sockets = NO_SOCKETS;
else
{
MultiplexedDatagramSocket[] newSockets
= new MultiplexedDatagramSocket[socketCount - 1];
System.arraycopy(sockets, 0, newSockets, 0, i);
System
.arraycopy(
sockets,
i + 1,
newSockets,
i,
newSockets.length - i);
sockets = newSockets;
}
break;
}
}
}
/**
* Copies the properties of a specific <tt>DatagramPacket</tt> to another
* <tt>DatagramPacket</tt>. The property values are not cloned.
*
* @param src the <tt>DatagramPacket</tt> which is to have its properties
* copied to <tt>dest</tt>
* @param dest the <tt>DatagramPacket</tt> which is to have its properties
* set to the value of the respective properties of <tt>src</tt>
*/
static void copy(DatagramPacket src, DatagramPacket dest)
{
synchronized (dest)
{
dest.setAddress(src.getAddress());
dest.setData(src.getData(), src.getOffset(), src.getLength());
dest.setPort(src.getPort());
}
}
/**
* Gets a <tt>MultiplexedDatagramSocket</tt> which filters
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt> using a
* specific <tt>DatagramPacketFilter</tt>. If such a
* <tt>MultiplexedDatagramSocket</tt> does not exist in this instance, it is
* created.
*
* @param filter the <tt>DatagramPacketFilter</tt> to get a
* <tt>MultiplexedDatagramSocket</tt> for
* @return a <tt>MultiplexedDatagramSocket</tt> which filters
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt> using the
* specified <tt>filter</tt>
* @throws SocketException if creating the
* <tt>MultiplexedDatagramSocket</tt> for the specified <tt>filter</tt>
* fails
*/
public MultiplexedDatagramSocket getSocket(DatagramPacketFilter filter)
throws SocketException
{
if (filter == null)
throw new NullPointerException("filter");
synchronized (socketsSyncRoot)
{
/*
* If a socket for the specified filter exists already, do not
* create a new one and return the existing.
*/
for (MultiplexedDatagramSocket socket : sockets)
if (filter.equals(socket.getFilter()))
return socket;
// Create a new socket for the specified filter.
MultiplexedDatagramSocket socket
= new MultiplexedDatagramSocket(this, filter);
// Remember the new socket.
int socketCount = sockets.length;
if (socketCount == 0)
sockets = new MultiplexedDatagramSocket[] { socket };
else
{
MultiplexedDatagramSocket[] newSockets
= new MultiplexedDatagramSocket[socketCount + 1];
System.arraycopy(sockets, 0, newSockets, 0, socketCount);
newSockets[socketCount] = socket;
sockets = newSockets;
}
return socket;
}
}
/**
* Receives a datagram packet from this socket. The <tt>DatagramPacket</tt>s
* returned by this method do not match any of the
* <tt>DatagramPacketFilter</tt>s of the <tt>MultiplexedDatagramSocket</tt>s
* associated with this instance at the time of their receipt. When this
* method returns, the <tt>DatagramPacket</tt>'s buffer is filled with the
* data received. The datagram packet also contains the sender's IP address,
* and the port number on the sender's machine.
* <p>
* This method blocks until a datagram is received. The <tt>length</tt>
* field of the datagram packet object contains the length of the received
* message. If the message is longer than the packet's length, the message
* is truncated.
* </p>
*
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data
* @throws IOException if an I/O error occurs
* @see DatagramSocket#receive(DatagramPacket)
*/
@Override
public void receive(DatagramPacket p)
throws IOException
{
receive(received, p);
}
/**
* Receives a <tt>DatagramPacket</tt> from a specific list of
* <tt>DatagramPacket</tt>s if it is not empty or from the network if the
* specified list is empty. When this method returns, the
* <tt>DatagramPacket</tt>'s buffer is filled with the data received. The
* datagram packet also contains the sender's IP address, and the port
* number on the sender's machine.
*
* @param received the list of previously received <tt>DatagramPacket</tt>
* from which the first is to be removed and returned if available
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data
* @throws IOException if an I/O error occurs
*/
private void receive(List<DatagramPacket> received, DatagramPacket p)
throws IOException
{
DatagramPacket r = null;
do
{
boolean doReceive;
synchronized (receiveSyncRoot)
{
if (received.isEmpty())
{
if (inReceive)
{
doReceive = false;
try
{
receiveSyncRoot.wait();
}
catch (InterruptedException iex)
{
continue;
}
}
else
{
doReceive = true;
inReceive = true;
}
}
else
{
doReceive = false;
r = received.remove(0);
}
}
if (doReceive)
{
try
{
super.receive(p);
synchronized (receiveSyncRoot)
{
synchronized (socketsSyncRoot)
{
boolean accepted = false;
for (MultiplexedDatagramSocket socket : sockets)
if (socket.getFilter().accept(p))
{
socket.received.add(clone(p));
accepted = true;
break;
}
if (!accepted)
this.received.add(clone(p));
}
}
}
finally
{
synchronized (receiveSyncRoot)
{
inReceive = false;
receiveSyncRoot.notifyAll();
}
}
}
}
while (r == null);
copy(r, p);
}
/**
* Receives a <tt>DatagramPacket</tt> from this <tt>DatagramSocket</tt> upon
* request from a specific <tt>MultiplexedDatagramSocket</tt>.
*
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> which requests
* the receipt of a <tt>DatagramPacket</tt> from the network
* @param p the <tt>DatagramPacket</tt> to receive the data from the network
* @throws IOException if an I/O error occurs
*/
void receive(
MultiplexedDatagramSocket multiplexed,
DatagramPacket p)
throws IOException
{
receive(multiplexed.received, p);
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.net.*;
/**
* Implements a <tt>DatagramPacketFilter</tt> which only accepts
* <tt>DatagramPacket</tt>s which represent STUN messages.
*
* @author Lubomir Marinov
*/
public class StunDatagramPacketFilter
implements DatagramPacketFilter
{
/**
* Determines whether a specific <tt>DatagramPacket</tt> represents a STUN
* message.
*
* @param p the <tt>DatagramPacket</tt> which is to be checked whether it is
* a STUN message
* @return <tt>true</tt> if <tt>p</tt> represents a STUN message
*/
public boolean accept(DatagramPacket p)
{
/*
* All STUN messages MUST start with a 20-byte header followed by zero
* or more Attributes.
*/
if (p.getLength() >= 20)
{
/*
* The most significant 2 bits of every STUN message MUST be zeroes.
* This can be used to differentiate STUN packets from other
* protocols when STUN is multiplexed with other protocols on the
* same port.
*/
byte b0 = p.getData()[p.getOffset()];
return ((b0 & 0xC0) == 0);
}
return false;
}
}