jdk/src/windows/classes/java/net/TwoStacksPlainSocketImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 82 216899854ed5
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class defines the plain SocketImpl that is used for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Windows version lower than Vista. It adds support for IPv6 on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * these platforms where available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * For backward compatibility Windows platforms that do not have IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * support also use this implementation, and fd1 gets set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * during socket creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class TwoStacksPlainSocketImpl extends AbstractPlainSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /* second fd, used for ipv6 on windows only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * fd1 is used for listeners and for client sockets at initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * until the socket is connected. Up to this point fd always refers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * to the ipv4 socket and fd1 to the ipv6 socket. After the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * becomes connected, fd always refers to the connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * (either v4 or v6) and fd1 is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * For ServerSockets, fd always refers to the v4 listener and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * fd1 the v6 listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private FileDescriptor fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Needed for ipv6 on windows because we need to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * if the socket is bound to ::0 or 0.0.0.0, when a caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * asks for it. Otherwise we don't know which socket to ask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private InetAddress anyLocalBoundAddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* to prevent starvation when listening on two sockets, this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * is used to hold the id of the last socket we accepted on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int lastfd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        initProto();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public TwoStacksPlainSocketImpl() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public TwoStacksPlainSocketImpl(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a socket with a boolean that specifies whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * is a stream socket (true) or an unconnected UDP socket (false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected synchronized void create(boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        fd1 = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super.create(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Binds the socket to the specified address of the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param port the port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected synchronized void bind(InetAddress address, int lport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        super.bind(address, lport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (address.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            anyLocalBoundAddr = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Object getOption(int opt) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (opt == SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (fd != null && fd1 != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                /* must be unbound or else bound to anyLocal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                return anyLocalBoundAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            InetAddressContainer in = new InetAddressContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            socketGetOption(opt, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return in.addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return super.getOption(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Closes the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized(fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (fd != null || fd1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                if (fdUseCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    if (closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    fd1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                     * If a thread has acquired the fd and a close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                     * isn't pending then use a deferred close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                     * Also decrement fdUseCount to signal the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                     * thread that releases the fd to close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    if (!closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        fdUseCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (fd != null || fd1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        fd1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Return true if already closed or close is pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public boolean isClosedOrPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * Lock on fdLock to ensure that we wait if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * close is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (closePending || (fd == null && fd1 == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /* Native methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static native void initProto();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    native void socketCreate(boolean isServer) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    native void socketConnect(InetAddress address, int port, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    native void socketBind(InetAddress address, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    native void socketListen(int count) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    native void socketAccept(SocketImpl s) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    native int socketAvailable() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    native void socketClose0(boolean useDeferredClose) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    native void socketShutdown(int howto) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    native void socketSetOption(int cmd, boolean on, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    native int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    native int socketGetOption1(int opt, Object iaContainerObj, FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    native void socketSendUrgentData(int data) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}