0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.FileDescriptor;
0N/Aimport sun.misc.SharedSecrets;
0N/Aimport sun.misc.JavaIOFileDescriptorAccess;
0N/A
0N/A/**
0N/A * This class defines the plain DatagramSocketImpl that is used on
0N/A * Windows platforms greater than or equal to Windows Vista. These
0N/A * platforms have a dual layer TCP/IP stack and can handle both IPv4
0N/A * and IPV6 through a single file descriptor.
0N/A * <p>
0N/A * Note: Multicasting on a dual layer TCP/IP stack is always done with
0N/A * TwoStacksPlainDatagramSocketImpl. This is to overcome the lack
0N/A * of behavior defined for multicasting over a dual layer socket by the RFC.
0N/A *
0N/A * @author Chris Hegarty
0N/A */
0N/A
0N/Aclass DualStackPlainDatagramSocketImpl extends AbstractPlainDatagramSocketImpl
0N/A{
0N/A static JavaIOFileDescriptorAccess fdAccess = SharedSecrets.getJavaIOFileDescriptorAccess();
0N/A
6272N/A // true if this socket is exclusively bound
6272N/A private final boolean exclusiveBind;
6272N/A
6272N/A /*
6272N/A * Set to true if SO_REUSEADDR is set after the socket is bound to
6272N/A * indicate SO_REUSEADDR is being emulated
6272N/A */
6272N/A private boolean reuseAddressEmulated;
6272N/A
6272N/A // emulates SO_REUSEADDR when exclusiveBind is true and socket is bound
6272N/A private boolean isReuseAddress;
6272N/A
6272N/A DualStackPlainDatagramSocketImpl(boolean exclBind) {
6272N/A exclusiveBind = exclBind;
6272N/A }
6272N/A
0N/A protected void datagramSocketCreate() throws SocketException {
0N/A if (fd == null)
0N/A throw new SocketException("Socket closed");
0N/A
0N/A int newfd = socketCreate(false /* v6Only */);
0N/A
0N/A fdAccess.set(fd, newfd);
0N/A }
0N/A
0N/A protected synchronized void bind0(int lport, InetAddress laddr)
0N/A throws SocketException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (laddr == null)
0N/A throw new NullPointerException("argument address");
0N/A
6272N/A socketBind(nativefd, laddr, lport, exclusiveBind);
0N/A if (lport == 0) {
0N/A localPort = socketLocalPort(nativefd);
0N/A } else {
0N/A localPort = lport;
0N/A }
0N/A }
0N/A
0N/A protected synchronized int peek(InetAddress address) throws IOException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (address == null)
0N/A throw new NullPointerException("Null address in peek()");
0N/A
0N/A // Use peekData()
0N/A DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
0N/A int peekPort = peekData(peekPacket);
0N/A address = peekPacket.getAddress();
0N/A return peekPort;
0N/A }
0N/A
0N/A protected synchronized int peekData(DatagramPacket p) throws IOException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (p == null)
0N/A throw new NullPointerException("packet");
0N/A if (p.getData() == null)
0N/A throw new NullPointerException("packet buffer");
0N/A
0N/A return socketReceiveOrPeekData(nativefd, p, timeout, connected, true /*peek*/);
0N/A }
0N/A
0N/A protected synchronized void receive0(DatagramPacket p) throws IOException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (p == null)
0N/A throw new NullPointerException("packet");
0N/A if (p.getData() == null)
0N/A throw new NullPointerException("packet buffer");
0N/A
0N/A socketReceiveOrPeekData(nativefd, p, timeout, connected, false /*receive*/);
0N/A }
0N/A
0N/A protected void send(DatagramPacket p) throws IOException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (p == null)
0N/A throw new NullPointerException("null packet");
0N/A
0N/A if (p.getAddress() == null ||p.getData() ==null)
0N/A throw new NullPointerException("null address || null buffer");
0N/A
0N/A socketSend(nativefd, p.getData(), p.getOffset(), p.getLength(),
0N/A p.getAddress(), p.getPort(), connected);
0N/A }
0N/A
0N/A protected void connect0(InetAddress address, int port) throws SocketException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A if (address == null)
0N/A throw new NullPointerException("address");
0N/A
0N/A socketConnect(nativefd, address, port);
0N/A }
0N/A
0N/A protected void disconnect0(int family /*unused*/) {
0N/A if (fd == null || !fd.valid())
0N/A return; // disconnect doesn't throw any exceptions
0N/A
0N/A socketDisconnect(fdAccess.get(fd));
0N/A }
0N/A
0N/A protected void datagramSocketClose() {
0N/A if (fd == null || !fd.valid())
0N/A return; // close doesn't throw any exceptions
0N/A
0N/A socketClose(fdAccess.get(fd));
0N/A fdAccess.set(fd, -1);
0N/A }
0N/A
6272N/A @SuppressWarnings("fallthrough")
0N/A protected void socketSetOption(int opt, Object val) throws SocketException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A int optionValue = 0;
0N/A
0N/A switch(opt) {
0N/A case IP_TOS :
0N/A case SO_RCVBUF :
0N/A case SO_SNDBUF :
0N/A optionValue = ((Integer)val).intValue();
0N/A break;
0N/A case SO_REUSEADDR :
6272N/A if (exclusiveBind && localPort != 0) {
6272N/A // socket already bound, emulate SO_REUSEADDR
6272N/A reuseAddressEmulated = true;
6272N/A isReuseAddress = (Boolean)val;
6272N/A return;
6272N/A }
6272N/A //Intentional fallthrough
0N/A case SO_BROADCAST :
0N/A optionValue = ((Boolean)val).booleanValue() ? 1 : 0;
0N/A break;
0N/A default: /* shouldn't get here */
0N/A throw new SocketException("Option not supported");
0N/A }
0N/A
0N/A socketSetIntOption(nativefd, opt, optionValue);
0N/A }
0N/A
0N/A protected Object socketGetOption(int opt) throws SocketException {
0N/A int nativefd = checkAndReturnNativeFD();
0N/A
0N/A // SO_BINDADDR is not a socket option.
0N/A if (opt == SO_BINDADDR) {
0N/A return socketLocalAddress(nativefd);
0N/A }
6272N/A if (opt == SO_REUSEADDR && reuseAddressEmulated)
6272N/A return isReuseAddress;
0N/A
0N/A int value = socketGetIntOption(nativefd, opt);
0N/A Object returnValue = null;
0N/A
0N/A switch (opt) {
0N/A case SO_REUSEADDR :
0N/A case SO_BROADCAST :
0N/A returnValue = (value == 0) ? Boolean.FALSE : Boolean.TRUE;
0N/A break;
0N/A case IP_TOS :
0N/A case SO_RCVBUF :
0N/A case SO_SNDBUF :
0N/A returnValue = new Integer(value);
0N/A break;
0N/A default: /* shouldn't get here */
0N/A throw new SocketException("Option not supported");
0N/A }
0N/A
0N/A return returnValue;
0N/A }
0N/A
0N/A /* Multicast specific methods.
0N/A * Multicasting on a dual layer TCP/IP stack is always done with
0N/A * TwoStacksPlainDatagramSocketImpl. This is to overcome the lack
0N/A * of behavior defined for multicasting over a dual layer socket by the RFC.
0N/A */
0N/A protected void join(InetAddress inetaddr, NetworkInterface netIf)
0N/A throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A protected void leave(InetAddress inetaddr, NetworkInterface netIf)
0N/A throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A protected void setTimeToLive(int ttl) throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A protected int getTimeToLive() throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A
0N/A protected void setTTL(byte ttl) throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A protected byte getTTL() throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A /* END Multicast specific methods */
0N/A
0N/A private int checkAndReturnNativeFD() throws SocketException {
0N/A if (fd == null || !fd.valid())
0N/A throw new SocketException("Socket closed");
0N/A
0N/A return fdAccess.get(fd);
0N/A }
0N/A
0N/A /* Native methods */
0N/A
0N/A private static native void initIDs();
0N/A
0N/A private static native int socketCreate(boolean v6Only);
0N/A
6272N/A private static native void socketBind(int fd, InetAddress localAddress,
6272N/A int localport, boolean exclBind) throws SocketException;
0N/A
0N/A private static native void socketConnect(int fd, InetAddress address, int port)
0N/A throws SocketException;
0N/A
0N/A private static native void socketDisconnect(int fd);
0N/A
0N/A private static native void socketClose(int fd);
0N/A
0N/A private static native int socketLocalPort(int fd) throws SocketException;
0N/A
0N/A private static native Object socketLocalAddress(int fd) throws SocketException;
0N/A
0N/A private static native int socketReceiveOrPeekData(int fd, DatagramPacket packet,
0N/A int timeout, boolean connected, boolean peek) throws IOException;
0N/A
0N/A private static native void socketSend(int fd, byte[] data, int offset, int length,
0N/A InetAddress address, int port, boolean connected) throws IOException;
0N/A
0N/A private static native void socketSetIntOption(int fd, int cmd,
0N/A int optionValue) throws SocketException;
0N/A
0N/A private static native int socketGetIntOption(int fd, int cmd) throws SocketException;
0N/A}