2542
|
1 |
/*
|
|
2 |
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
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).
|
|
16 |
*
|
|
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.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package sun.nio.ch;
|
|
26 |
|
|
27 |
import java.net.SocketAddress;
|
|
28 |
import java.net.InetSocketAddress;
|
|
29 |
import java.net.InetAddress;
|
|
30 |
import java.io.FileDescriptor;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.util.Collections;
|
|
33 |
import java.util.Set;
|
|
34 |
import java.util.HashSet;
|
|
35 |
import java.nio.channels.SelectionKey;
|
|
36 |
import java.nio.channels.AlreadyBoundException;
|
|
37 |
import java.nio.channels.ClosedChannelException;
|
|
38 |
import java.nio.channels.NotYetBoundException;
|
|
39 |
import java.nio.channels.spi.SelectorProvider;
|
|
40 |
import com.sun.nio.sctp.IllegalUnbindException;
|
|
41 |
import com.sun.nio.sctp.SctpChannel;
|
|
42 |
import com.sun.nio.sctp.SctpServerChannel;
|
|
43 |
import com.sun.nio.sctp.SctpSocketOption;
|
|
44 |
import com.sun.nio.sctp.SctpStandardSocketOption;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* An implementation of SctpServerChannel
|
|
48 |
*/
|
|
49 |
public class SctpServerChannelImpl extends SctpServerChannel
|
|
50 |
implements SelChImpl
|
|
51 |
{
|
|
52 |
/* Used to make native close and preClose calls */
|
|
53 |
private static NativeDispatcher nd;
|
|
54 |
|
|
55 |
private final FileDescriptor fd;
|
|
56 |
|
|
57 |
private final int fdVal;
|
|
58 |
|
|
59 |
/* IDs of native thread doing accept, for signalling */
|
|
60 |
private volatile long thread = 0;
|
|
61 |
|
|
62 |
/* Lock held by thread currently blocked in this channel */
|
|
63 |
private final Object lock = new Object();
|
|
64 |
|
|
65 |
/* Lock held by any thread that modifies the state fields declared below
|
|
66 |
* DO NOT invoke a blocking I/O operation while holding this lock! */
|
|
67 |
private final Object stateLock = new Object();
|
|
68 |
|
|
69 |
private enum ChannelState {
|
|
70 |
UNINITIALIZED,
|
|
71 |
INUSE,
|
|
72 |
KILLPENDING,
|
|
73 |
KILLED,
|
|
74 |
}
|
|
75 |
/* -- The following fields are protected by stateLock -- */
|
|
76 |
private ChannelState state = ChannelState.UNINITIALIZED;
|
|
77 |
|
|
78 |
/* Binding: Once bound the port will remain constant. */
|
|
79 |
int port = -1;
|
|
80 |
private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
|
|
81 |
/* Has the channel been bound to the wildcard address */
|
|
82 |
private boolean wildcard; /* false */
|
|
83 |
|
|
84 |
/* -- End of fields protected by stateLock -- */
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Initializes a new instance of this class.
|
|
88 |
*/
|
|
89 |
public SctpServerChannelImpl(SelectorProvider provider)
|
|
90 |
throws IOException {
|
|
91 |
//TODO: update provider remove public modifier
|
|
92 |
super(provider);
|
|
93 |
this.fd = SctpNet.socket(true);
|
|
94 |
this.fdVal = IOUtil.fdVal(fd);
|
|
95 |
this.state = ChannelState.INUSE;
|
|
96 |
}
|
|
97 |
|
|
98 |
@Override
|
|
99 |
public SctpServerChannel bind(SocketAddress local, int backlog)
|
|
100 |
throws IOException {
|
|
101 |
synchronized (lock) {
|
|
102 |
synchronized (stateLock) {
|
|
103 |
if (!isOpen())
|
|
104 |
throw new ClosedChannelException();
|
|
105 |
if (isBound())
|
|
106 |
throw new AlreadyBoundException();
|
|
107 |
|
|
108 |
InetSocketAddress isa = (local == null) ?
|
|
109 |
new InetSocketAddress(0) : Net.checkAddress(local);
|
|
110 |
SecurityManager sm = System.getSecurityManager();
|
|
111 |
if (sm != null)
|
|
112 |
sm.checkListen(isa.getPort());
|
|
113 |
Net.bind(fd, isa.getAddress(), isa.getPort());
|
|
114 |
|
|
115 |
InetSocketAddress boundIsa = Net.localAddress(fd);
|
|
116 |
port = boundIsa.getPort();
|
|
117 |
localAddresses.add(isa);
|
|
118 |
if (isa.getAddress().isAnyLocalAddress())
|
|
119 |
wildcard = true;
|
|
120 |
|
|
121 |
Net.listen(fd, backlog < 1 ? 50 : backlog);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
return this;
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public SctpServerChannel bindAddress(InetAddress address)
|
|
129 |
throws IOException {
|
|
130 |
return bindUnbindAddress(address, true);
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
public SctpServerChannel unbindAddress(InetAddress address)
|
|
135 |
throws IOException {
|
|
136 |
return bindUnbindAddress(address, false);
|
|
137 |
}
|
|
138 |
|
|
139 |
private SctpServerChannel bindUnbindAddress(InetAddress address, boolean add)
|
|
140 |
throws IOException {
|
|
141 |
if (address == null)
|
|
142 |
throw new IllegalArgumentException();
|
|
143 |
|
|
144 |
synchronized (lock) {
|
|
145 |
synchronized (stateLock) {
|
|
146 |
if (!isOpen())
|
|
147 |
throw new ClosedChannelException();
|
|
148 |
if (!isBound())
|
|
149 |
throw new NotYetBoundException();
|
|
150 |
if (wildcard)
|
|
151 |
throw new IllegalStateException(
|
|
152 |
"Cannot add or remove addresses from a channel that is bound to the wildcard address");
|
|
153 |
if (address.isAnyLocalAddress())
|
|
154 |
throw new IllegalArgumentException(
|
|
155 |
"Cannot add or remove the wildcard address");
|
|
156 |
if (add) {
|
|
157 |
for (InetSocketAddress addr : localAddresses) {
|
|
158 |
if (addr.getAddress().equals(address)) {
|
|
159 |
throw new AlreadyBoundException();
|
|
160 |
}
|
|
161 |
}
|
|
162 |
} else { /*removing */
|
|
163 |
/* Verify that there is more than one address
|
|
164 |
* and that address is already bound */
|
|
165 |
if (localAddresses.size() <= 1)
|
|
166 |
throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound");
|
|
167 |
boolean foundAddress = false;
|
|
168 |
for (InetSocketAddress addr : localAddresses) {
|
|
169 |
if (addr.getAddress().equals(address)) {
|
|
170 |
foundAddress = true;
|
|
171 |
break;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
if (!foundAddress )
|
|
175 |
throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address");
|
|
176 |
}
|
|
177 |
|
|
178 |
SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
|
|
179 |
|
|
180 |
/* Update our internal Set to reflect the addition/removal */
|
|
181 |
if (add)
|
|
182 |
localAddresses.add(new InetSocketAddress(address, port));
|
|
183 |
else {
|
|
184 |
for (InetSocketAddress addr : localAddresses) {
|
|
185 |
if (addr.getAddress().equals(address)) {
|
|
186 |
localAddresses.remove(addr);
|
|
187 |
break;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
}
|
|
193 |
return this;
|
|
194 |
}
|
|
195 |
|
|
196 |
private boolean isBound() {
|
|
197 |
synchronized (stateLock) {
|
|
198 |
return port == -1 ? false : true;
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
private void acceptCleanup() throws IOException {
|
|
203 |
synchronized (stateLock) {
|
|
204 |
thread = 0;
|
|
205 |
if (state == ChannelState.KILLPENDING)
|
|
206 |
kill();
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
@Override
|
|
211 |
public SctpChannel accept() throws IOException {
|
|
212 |
synchronized (lock) {
|
|
213 |
if (!isOpen())
|
|
214 |
throw new ClosedChannelException();
|
|
215 |
if (!isBound())
|
|
216 |
throw new NotYetBoundException();
|
|
217 |
SctpChannel sc = null;
|
|
218 |
|
|
219 |
int n = 0;
|
|
220 |
FileDescriptor newfd = new FileDescriptor();
|
|
221 |
InetSocketAddress[] isaa = new InetSocketAddress[1];
|
|
222 |
|
|
223 |
try {
|
|
224 |
begin();
|
|
225 |
if (!isOpen())
|
|
226 |
return null;
|
|
227 |
thread = NativeThread.current();
|
|
228 |
for (;;) {
|
|
229 |
n = accept0(fd, newfd, isaa);
|
|
230 |
if ((n == IOStatus.INTERRUPTED) && isOpen())
|
|
231 |
continue;
|
|
232 |
break;
|
|
233 |
}
|
|
234 |
} finally {
|
|
235 |
acceptCleanup();
|
|
236 |
end(n > 0);
|
|
237 |
assert IOStatus.check(n);
|
|
238 |
}
|
|
239 |
|
|
240 |
if (n < 1)
|
|
241 |
return null;
|
|
242 |
|
|
243 |
IOUtil.configureBlocking(newfd, true);
|
|
244 |
InetSocketAddress isa = isaa[0];
|
|
245 |
sc = new SctpChannelImpl(provider(), newfd);
|
|
246 |
|
|
247 |
SecurityManager sm = System.getSecurityManager();
|
|
248 |
if (sm != null)
|
|
249 |
sm.checkAccept(isa.getAddress().getHostAddress(),
|
|
250 |
isa.getPort());
|
|
251 |
|
|
252 |
return sc;
|
|
253 |
}
|
|
254 |
}
|
|
255 |
|
|
256 |
@Override
|
|
257 |
protected void implConfigureBlocking(boolean block) throws IOException {
|
|
258 |
IOUtil.configureBlocking(fd, block);
|
|
259 |
}
|
|
260 |
|
|
261 |
@Override
|
|
262 |
public void implCloseSelectableChannel() throws IOException {
|
|
263 |
synchronized (stateLock) {
|
|
264 |
nd.preClose(fd);
|
|
265 |
if (thread != 0)
|
|
266 |
NativeThread.signal(thread);
|
|
267 |
if (!isRegistered())
|
|
268 |
kill();
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
@Override
|
|
273 |
public void kill() throws IOException {
|
|
274 |
synchronized (stateLock) {
|
|
275 |
if (state == ChannelState.KILLED)
|
|
276 |
return;
|
|
277 |
if (state == ChannelState.UNINITIALIZED) {
|
|
278 |
state = ChannelState.KILLED;
|
|
279 |
return;
|
|
280 |
}
|
|
281 |
assert !isOpen() && !isRegistered();
|
|
282 |
|
|
283 |
// Postpone the kill if there is a thread in accept
|
|
284 |
if (thread == 0) {
|
|
285 |
nd.close(fd);
|
|
286 |
state = ChannelState.KILLED;
|
|
287 |
} else {
|
|
288 |
state = ChannelState.KILLPENDING;
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
@Override
|
|
294 |
public FileDescriptor getFD() {
|
|
295 |
return fd;
|
|
296 |
}
|
|
297 |
|
|
298 |
@Override
|
|
299 |
public int getFDVal() {
|
|
300 |
return fdVal;
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* Translates native poll revent ops into a ready operation ops
|
|
305 |
*/
|
|
306 |
private boolean translateReadyOps(int ops, int initialOps,
|
|
307 |
SelectionKeyImpl sk) {
|
|
308 |
int intOps = sk.nioInterestOps();
|
|
309 |
int oldOps = sk.nioReadyOps();
|
|
310 |
int newOps = initialOps;
|
|
311 |
|
|
312 |
if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
|
|
313 |
/* This should only happen if this channel is pre-closed while a
|
|
314 |
* selection operation is in progress
|
|
315 |
* ## Throw an error if this channel has not been pre-closed */
|
|
316 |
return false;
|
|
317 |
}
|
|
318 |
|
|
319 |
if ((ops & (PollArrayWrapper.POLLERR
|
|
320 |
| PollArrayWrapper.POLLHUP)) != 0) {
|
|
321 |
newOps = intOps;
|
|
322 |
sk.nioReadyOps(newOps);
|
|
323 |
return (newOps & ~oldOps) != 0;
|
|
324 |
}
|
|
325 |
|
|
326 |
if (((ops & PollArrayWrapper.POLLIN) != 0) &&
|
|
327 |
((intOps & SelectionKey.OP_ACCEPT) != 0))
|
|
328 |
newOps |= SelectionKey.OP_ACCEPT;
|
|
329 |
|
|
330 |
sk.nioReadyOps(newOps);
|
|
331 |
return (newOps & ~oldOps) != 0;
|
|
332 |
}
|
|
333 |
|
|
334 |
@Override
|
|
335 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
|
|
336 |
return translateReadyOps(ops, sk.nioReadyOps(), sk);
|
|
337 |
}
|
|
338 |
|
|
339 |
@Override
|
|
340 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
|
|
341 |
return translateReadyOps(ops, 0, sk);
|
|
342 |
}
|
|
343 |
|
|
344 |
@Override
|
|
345 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
|
|
346 |
int newOps = 0;
|
|
347 |
|
|
348 |
/* Translate ops */
|
|
349 |
if ((ops & SelectionKey.OP_ACCEPT) != 0)
|
|
350 |
newOps |= PollArrayWrapper.POLLIN;
|
|
351 |
/* Place ops into pollfd array */
|
|
352 |
sk.selector.putEventOps(sk, newOps);
|
|
353 |
|
|
354 |
}
|
|
355 |
|
|
356 |
@Override
|
|
357 |
public <T> SctpServerChannel setOption(SctpSocketOption<T> name, T value)
|
|
358 |
throws IOException {
|
|
359 |
if (name == null)
|
|
360 |
throw new NullPointerException();
|
|
361 |
if (!supportedOptions().contains(name))
|
|
362 |
throw new UnsupportedOperationException("'" + name + "' not supported");
|
|
363 |
|
|
364 |
synchronized (stateLock) {
|
|
365 |
if (!isOpen())
|
|
366 |
throw new ClosedChannelException();
|
|
367 |
|
|
368 |
SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
|
|
369 |
return this;
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
@Override
|
|
374 |
public <T> T getOption(SctpSocketOption<T> name) throws IOException {
|
|
375 |
if (name == null)
|
|
376 |
throw new NullPointerException();
|
|
377 |
if (!supportedOptions().contains(name))
|
|
378 |
throw new UnsupportedOperationException("'" + name + "' not supported");
|
|
379 |
|
|
380 |
synchronized (stateLock) {
|
|
381 |
if (!isOpen())
|
|
382 |
throw new ClosedChannelException();
|
|
383 |
|
|
384 |
return (T) SctpNet.getSocketOption(fdVal, name, 0 /*oneToOne*/);
|
|
385 |
}
|
|
386 |
}
|
|
387 |
|
|
388 |
private static class DefaultOptionsHolder {
|
|
389 |
static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
|
|
390 |
|
|
391 |
private static Set<SctpSocketOption<?>> defaultOptions() {
|
|
392 |
HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(1);
|
|
393 |
set.add(SctpStandardSocketOption.SCTP_INIT_MAXSTREAMS);
|
|
394 |
return Collections.unmodifiableSet(set);
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
@Override
|
|
399 |
public final Set<SctpSocketOption<?>> supportedOptions() {
|
|
400 |
return DefaultOptionsHolder.defaultOptions;
|
|
401 |
}
|
|
402 |
|
|
403 |
@Override
|
|
404 |
public Set<SocketAddress> getAllLocalAddresses()
|
|
405 |
throws IOException {
|
|
406 |
synchronized (stateLock) {
|
|
407 |
if (!isOpen())
|
|
408 |
throw new ClosedChannelException();
|
|
409 |
if (!isBound())
|
|
410 |
return null;
|
|
411 |
|
|
412 |
return SctpNet.getLocalAddresses(fdVal);
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
/* Native */
|
|
417 |
private static native void initIDs();
|
|
418 |
|
|
419 |
private static native int accept0(FileDescriptor ssfd,
|
|
420 |
FileDescriptor newfd, InetSocketAddress[] isaa) throws IOException;
|
|
421 |
|
|
422 |
static {
|
|
423 |
Util.load(); // loads nio & net native libraries
|
|
424 |
java.security.AccessController.doPrivileged(
|
|
425 |
new sun.security.action.LoadLibraryAction("sctp"));
|
|
426 |
nd = new SctpSocketDispatcher();
|
|
427 |
initIDs();
|
|
428 |
}
|
|
429 |
}
|