2
* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
27
import java.io.IOException;
28
import java.io.FileDescriptor;
29
import sun.net.ResourceManager;
32
* This class defines the plain SocketImpl that is used for all
33
* Windows version lower than Vista. It adds support for IPv6 on
34
* these platforms where available.
36
* For backward compatibility Windows platforms that do not have IPv6
37
* support also use this implementation, and fd1 gets set to null
38
* during socket creation.
40
* @author Chris Hegarty
43
class TwoStacksPlainSocketImpl extends AbstractPlainSocketImpl
45
/* second fd, used for ipv6 on windows only.
46
* fd1 is used for listeners and for client sockets at initialization
47
* until the socket is connected. Up to this point fd always refers
48
* to the ipv4 socket and fd1 to the ipv6 socket. After the socket
49
* becomes connected, fd always refers to the connected socket
50
* (either v4 or v6) and fd1 is closed.
52
* For ServerSockets, fd always refers to the v4 listener and
53
* fd1 the v6 listener.
58
* Needed for ipv6 on windows because we need to know
59
* if the socket is bound to ::0 or 0.0.0.0, when a caller
60
* asks for it. Otherwise we don't know which socket to ask.
62
private InetAddress anyLocalBoundAddr = null;
64
/* to prevent starvation when listening on two sockets, this is
65
* is used to hold the id of the last socket we accepted on.
67
cli.System.Net.Sockets.Socket lastfd = null;
69
public TwoStacksPlainSocketImpl() {}
71
public TwoStacksPlainSocketImpl(FileDescriptor fd) {
76
* Creates a socket with a boolean that specifies whether this
77
* is a stream socket (true) or an unconnected UDP socket (false).
79
protected synchronized void create(boolean stream) throws IOException {
80
fd1 = new FileDescriptor();
83
} catch (IOException e) {
90
* Binds the socket to the specified address of the specified local port.
91
* @param address the address
92
* @param port the port
94
protected synchronized void bind(InetAddress address, int lport)
97
super.bind(address, lport);
98
if (address.isAnyLocalAddress()) {
99
anyLocalBoundAddr = address;
103
public Object getOption(int opt) throws SocketException {
104
if (isClosedOrPending()) {
105
throw new SocketException("Socket Closed");
107
if (opt == SO_BINDADDR) {
108
if (fd != null && fd1 != null ) {
109
/* must be unbound or else bound to anyLocal */
110
return anyLocalBoundAddr;
112
InetAddressContainer in = new InetAddressContainer();
113
socketGetOption(opt, in);
116
return super.getOption(opt);
122
protected void close() throws IOException {
123
synchronized(fdLock) {
124
if (fd != null || fd1 != null) {
126
ResourceManager.afterUdpClose();
128
if (fdUseCount == 0) {
139
* If a thread has acquired the fd and a close
140
* isn't pending then use a deferred close.
141
* Also decrement fdUseCount to signal the last
142
* thread that releases the fd to close it.
154
void reset() throws IOException {
155
if (fd != null || fd1 != null) {
164
* Return true if already closed or close is pending
166
public boolean isClosedOrPending() {
168
* Lock on fdLock to ensure that we wait if a
169
* close is in progress.
171
synchronized (fdLock) {
172
if (closePending || (fd == null && fd1 == null)) {
182
void socketCreate(boolean stream) throws IOException {
183
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
184
TwoStacksPlainSocketImpl_c.socketCreate(env, this, stream);
185
env.ThrowPendingException();
188
void socketConnect(InetAddress address, int port, int timeout) throws IOException {
189
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
190
TwoStacksPlainSocketImpl_c.socketConnect(env, this, address, port, timeout);
191
env.ThrowPendingException();
194
void socketBind(InetAddress address, int localport) throws IOException {
195
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
196
TwoStacksPlainSocketImpl_c.socketBind(env, this, address, localport);
197
env.ThrowPendingException();
200
void socketListen(int count) throws IOException {
201
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
202
TwoStacksPlainSocketImpl_c.socketListen(env, this, count);
203
env.ThrowPendingException();
206
void socketAccept(SocketImpl socket) throws IOException {
207
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
208
TwoStacksPlainSocketImpl_c.socketAccept(env, this, socket);
209
env.ThrowPendingException();
212
int socketAvailable() throws IOException {
213
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
214
int ret = TwoStacksPlainSocketImpl_c.socketAvailable(env, this);
215
env.ThrowPendingException();
219
void socketClose0(boolean useDeferredClose) throws IOException {
220
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
221
TwoStacksPlainSocketImpl_c.socketClose0(env, this, useDeferredClose);
222
env.ThrowPendingException();
225
void socketShutdown(int howto) throws IOException {
226
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
227
TwoStacksPlainSocketImpl_c.socketShutdown(env, this, howto);
228
env.ThrowPendingException();
231
void socketSetOption(int cmd, boolean on, Object value) throws SocketException {
232
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
233
TwoStacksPlainSocketImpl_c.socketSetOption(env, this, cmd, on, value);
234
env.ThrowPendingException();
237
int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
238
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
239
int ret = TwoStacksPlainSocketImpl_c.socketGetOption(env, this, opt, iaContainerObj);
240
env.ThrowPendingException();
244
int socketGetOption1(int opt, Object iaContainerObj, FileDescriptor fd) throws SocketException {
245
throw new UnsatisfiedLinkError();
248
void socketSendUrgentData(int data) throws IOException {
249
ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
250
TwoStacksPlainSocketImpl_c.socketSendUrgentData(env, this, data);
251
env.ThrowPendingException();