/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* An implementation of DatagramChannels.
*/
class DatagramChannelImpl
extends DatagramChannel
implements SelChImpl
{
// Used to make native read and write calls
// Our file descriptor
// even after the value in the file descriptor object has been set to -1
private final int fdVal;
// The protocol family of the socket
// IDs of native threads doing reads and writes, for signalling
// Cached InetAddress and port for unconnected DatagramChannels
// used by receive0
private int cachedSenderPort;
// Lock held by current reading or connecting thread
// Lock held by current writing or connecting thread
// Lock held by any thread that modifies the state fields declared below
// DO NOT invoke a blocking I/O operation while holding this lock!
// -- The following fields are protected by stateLock
// State (does not necessarily increase monotonically)
// Binding
// Our socket adaptor, if any
// Multicast support
// set true when socket is bound and SO_REUSEADDRESS is emulated
private boolean reuseAddressEmulated;
private boolean isReuseAddress;
// -- End of fields protected by stateLock
throws IOException
{
super(sp);
try {
this.state = ST_UNCONNECTED;
} catch (IOException ioe) {
throw ioe;
}
}
throws IOException
{
super(sp);
{
throw new NullPointerException("'family' is null");
else
throw new UnsupportedOperationException("Protocol family not supported");
}
if (!Net.isIPv6Available()) {
throw new UnsupportedOperationException("IPv6 not available");
}
}
this.state = ST_UNCONNECTED;
}
throws IOException
{
super(sp);
this.state = ST_UNCONNECTED;
}
synchronized (stateLock) {
return socket;
}
}
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
}
}
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
return remoteAddress;
}
}
throws IOException
{
throw new NullPointerException();
synchronized (stateLock) {
ensureOpen();
// IPv4 only; no-op for IPv6
}
return this;
}
{
// options are protocol dependent
return this;
}
throw new IllegalArgumentException("Cannot set IP_MULTICAST_IF to 'null'");
if (index == -1)
throw new IOException("Network interface cannot be identified");
} else {
// need IPv4 address to identify interface
throw new IOException("Network interface not configured for IPv4");
}
return this;
}
{
reuseAddressEmulated = true;
}
// remaining options don't need any special handling
return this;
}
}
@SuppressWarnings("unchecked")
throws IOException
{
throw new NullPointerException();
synchronized (stateLock) {
ensureOpen();
// IPv4 only; always return 0 on IPv6
} else {
}
}
{
}
if (address == 0)
return null; // default interface
throw new IOException("Unable to map address to interface");
return (T) ni;
} else {
if (index == 0)
return null; // default interface
throw new IOException("Unable to map index to interface");
return (T) ni;
}
}
{
}
// no special handling
}
}
private static class DefaultOptionsHolder {
}
}
return DefaultOptionsHolder.defaultOptions;
}
if (!isOpen())
throw new ClosedChannelException();
}
if (dst.isReadOnly())
throw new IllegalArgumentException("Read-only buffer");
throw new NullPointerException();
synchronized (readLock) {
ensureOpen();
// Socket was not bound before attempting receive
if (localAddress() == null)
int n = 0;
try {
begin();
if (!isOpen())
return null;
do {
if (n == IOStatus.UNAVAILABLE)
return null;
} else {
for (;;) {
do {
if (n == IOStatus.UNAVAILABLE)
return null;
try {
} catch (SecurityException se) {
// Ignore packet
n = 0;
continue;
}
break;
}
}
return sender;
} finally {
readerThread = 0;
}
}
}
throws IOException
{
// Substitute a native buffer. If the supplied buffer is empty
// we must instead use a nonempty buffer, otherwise the call
// will not block waiting for a datagram on some platforms.
try {
return n;
} finally {
}
}
throws IOException
{
isConnected());
if (n > 0)
return n;
}
throws IOException
{
throw new NullPointerException();
synchronized (writeLock) {
ensureOpen();
throw new IOException("Target address not resolved");
synchronized (stateLock) {
if (!isConnected()) {
throw new NullPointerException();
if (ia.isMulticastAddress()) {
} else {
}
}
} else { // Connected case; Check address then write
throw new IllegalArgumentException(
"Connected address not equal to target address");
}
}
}
int n = 0;
try {
begin();
if (!isOpen())
return 0;
do {
synchronized (stateLock) {
}
}
} finally {
writerThread = 0;
}
}
}
throws IOException
{
if (src instanceof DirectBuffer)
// Substitute a native buffer
try {
// Do not update src until we see how many bytes were written
if (n > 0) {
// now update src
}
return n;
} finally {
}
}
throws IOException
{
int written;
try {
} catch (PortUnreachableException pue) {
if (isConnected())
throw pue;
}
if (written > 0)
return written;
}
throw new NullPointerException();
synchronized (readLock) {
synchronized (stateLock) {
ensureOpen();
if (!isConnected())
throw new NotYetConnectedException();
}
int n = 0;
try {
begin();
if (!isOpen())
return 0;
do {
} finally {
readerThread = 0;
}
}
}
throws IOException
{
throw new IndexOutOfBoundsException();
synchronized (readLock) {
synchronized (stateLock) {
ensureOpen();
if (!isConnected())
throw new NotYetConnectedException();
}
long n = 0;
try {
begin();
if (!isOpen())
return 0;
do {
} finally {
readerThread = 0;
}
}
}
throw new NullPointerException();
synchronized (writeLock) {
synchronized (stateLock) {
ensureOpen();
if (!isConnected())
throw new NotYetConnectedException();
}
int n = 0;
try {
begin();
if (!isOpen())
return 0;
do {
} finally {
writerThread = 0;
}
}
}
throws IOException
{
throw new IndexOutOfBoundsException();
synchronized (writeLock) {
synchronized (stateLock) {
ensureOpen();
if (!isConnected())
throw new NotYetConnectedException();
}
long n = 0;
try {
begin();
if (!isOpen())
return 0;
do {
} finally {
writerThread = 0;
}
}
}
}
synchronized (stateLock) {
return localAddress;
}
}
synchronized (stateLock) {
return remoteAddress;
}
}
synchronized (readLock) {
synchronized (writeLock) {
synchronized (stateLock) {
ensureOpen();
if (localAddress != null)
throw new AlreadyBoundException();
// only Inet4Address allowed with IPv4 socket
} else {
}
} else {
// only Inet4Address allowed with IPv4 socket
if (!(addr instanceof Inet4Address))
throw new UnsupportedAddressTypeException();
}
}
}
}
}
}
return this;
}
public boolean isConnected() {
synchronized (stateLock) {
return (state == ST_CONNECTED);
}
}
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
if (state != ST_UNCONNECTED)
throw new IllegalStateException("Connect already invoked");
}
}
int localPort = 0;
synchronized(readLock) {
synchronized(writeLock) {
synchronized (stateLock) {
fd,
isa.getAddress(),
if (n <= 0)
throw new Error(); // Can't happen
// Connection succeeded; disallow further invocation
remoteAddress = isa;
// set or refresh local address
}
}
}
return this;
}
synchronized(readLock) {
synchronized(writeLock) {
synchronized (stateLock) {
if (!isConnected() || !isOpen())
return this;
// refresh local address
}
}
}
return this;
}
/**
* optional source address.
*/
throws IOException
{
if (!group.isMulticastAddress())
throw new IllegalArgumentException("Group not a multicast address");
// check multicast address is compatible with this socket
if (group instanceof Inet4Address) {
throw new IllegalArgumentException("IPv6 socket cannot join IPv4 multicast group");
} else if (group instanceof Inet6Address) {
throw new IllegalArgumentException("Only IPv6 sockets can join IPv6 multicast group");
} else {
throw new IllegalArgumentException("Address type not supported");
}
// check source address
if (source.isAnyLocalAddress())
throw new IllegalArgumentException("Source address is a wildcard address");
if (source.isMulticastAddress())
throw new IllegalArgumentException("Source address is multicast address");
throw new IllegalArgumentException("Source address is different type to group");
}
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
// check the registry to see if we are already a member of the group
registry = new MembershipRegistry();
} else {
// return existing membership key
return key;
}
{
if (index == -1)
throw new IOException("Network interface cannot be identified");
// need multicast and source address as byte arrays
// join the group
if (n == IOStatus.UNAVAILABLE)
throw new UnsupportedOperationException();
} else {
// need IPv4 address to identify interface
throw new IOException("Network interface not configured for IPv4");
// join the group
if (n == IOStatus.UNAVAILABLE)
throw new UnsupportedOperationException();
}
return key;
}
}
throws IOException
{
}
throws IOException
{
throw new NullPointerException("source address is null");
}
// package-private
synchronized (stateLock) {
return;
try {
} else {
}
} catch (IOException ioe) {
// should not happen
throw new AssertionError(ioe);
}
key.invalidate();
}
}
/**
* Block datagrams from given source if a memory to receive all
* datagrams.
*/
throws IOException
{
synchronized (stateLock) {
throw new IllegalStateException("key is no longer valid");
if (source.isAnyLocalAddress())
throw new IllegalArgumentException("Source address is a wildcard address");
if (source.isMulticastAddress())
throw new IllegalArgumentException("Source address is multicast address");
throw new IllegalArgumentException("Source address is different type to group");
int n;
} else {
}
if (n == IOStatus.UNAVAILABLE) {
// ancient kernel
throw new UnsupportedOperationException();
}
}
}
/**
* Unblock given source.
*/
synchronized (stateLock) {
throw new IllegalStateException("key is no longer valid");
try {
} else {
}
} catch (IOException ioe) {
// should not happen
throw new AssertionError(ioe);
}
}
}
synchronized (stateLock) {
// if member of mulitcast group then invalidate all keys
long th;
if (!isRegistered())
kill();
}
}
synchronized (stateLock) {
return;
if (state == ST_UNINITIALIZED) {
return;
}
assert !isOpen() && !isRegistered();
}
}
// fd is null if constructor threw exception
close();
}
/**
* Translates native poll revent set into a ready operation set
*/
int newOps = initialOps;
// This should only happen if this channel is pre-closed while a
// selection operation is in progress
// ## Throw an error if this channel has not been pre-closed
return false;
}
}
}
}
}
/**
* Translates an interest operation set into a native poll event set
*/
int newOps = 0;
}
return fd;
}
public int getFDVal() {
return fdVal;
}
// -- Native methods --
private static native void initIDs();
throws IOException;
boolean connected)
throws IOException;
throws IOException;
static {
initIDs();
}
}