author | bpb |
Wed, 14 Feb 2018 08:15:07 -0800 | |
changeset 48896 | 1e358cc5af98 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48896
1e358cc5af98
8144672: (ch) PipeImpl should use localhost instead of loopback address
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
*/ |
|
28 |
||
29 |
package sun.nio.ch; |
|
30 |
||
31 |
import java.io.IOException; |
|
32 |
import java.net.InetAddress; |
|
33 |
import java.net.InetSocketAddress; |
|
34 |
import java.nio.*; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.nio.channels.spi.*; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.security.PrivilegedExceptionAction; |
|
39 |
import java.security.PrivilegedActionException; |
|
39748 | 40 |
import java.security.SecureRandom; |
2 | 41 |
import java.util.Random; |
42 |
||
43 |
||
44 |
/** |
|
45 |
* A simple Pipe implementation based on a socket connection. |
|
46 |
*/ |
|
47 |
||
48 |
class PipeImpl |
|
49 |
extends Pipe |
|
50 |
{ |
|
39748 | 51 |
// Number of bytes in the secret handshake. |
52 |
private static final int NUM_SECRET_BYTES = 16; |
|
53 |
||
54 |
// Random object for handshake values |
|
55 |
private static final Random RANDOM_NUMBER_GENERATOR = new SecureRandom(); |
|
2 | 56 |
|
57 |
// Source and sink channels |
|
58 |
private SourceChannel source; |
|
59 |
private SinkChannel sink; |
|
60 |
||
61 |
private class Initializer |
|
895 | 62 |
implements PrivilegedExceptionAction<Void> |
2 | 63 |
{ |
64 |
||
65 |
private final SelectorProvider sp; |
|
66 |
||
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
67 |
private IOException ioe = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
68 |
|
2 | 69 |
private Initializer(SelectorProvider sp) { |
70 |
this.sp = sp; |
|
71 |
} |
|
72 |
||
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
73 |
@Override |
895 | 74 |
public Void run() throws IOException { |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
75 |
LoopbackConnector connector = new LoopbackConnector(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
76 |
connector.run(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
77 |
if (ioe instanceof ClosedByInterruptException) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
78 |
ioe = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
79 |
Thread connThread = new Thread(connector) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
80 |
@Override |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
81 |
public void interrupt() {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
82 |
}; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
83 |
connThread.start(); |
2 | 84 |
for (;;) { |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
85 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
86 |
connThread.join(); |
2 | 87 |
break; |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
88 |
} catch (InterruptedException ex) {} |
2 | 89 |
} |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
90 |
Thread.currentThread().interrupt(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
91 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
92 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
93 |
if (ioe != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
94 |
throw new IOException("Unable to establish loopback connection", ioe); |
2 | 95 |
|
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
96 |
return null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
97 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
98 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
99 |
private class LoopbackConnector implements Runnable { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
100 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
101 |
@Override |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
102 |
public void run() { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
103 |
ServerSocketChannel ssc = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
104 |
SocketChannel sc1 = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
105 |
SocketChannel sc2 = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
106 |
|
2 | 107 |
try { |
39748 | 108 |
// Create secret with a backing array. |
109 |
ByteBuffer secret = ByteBuffer.allocate(NUM_SECRET_BYTES); |
|
110 |
ByteBuffer bb = ByteBuffer.allocate(NUM_SECRET_BYTES); |
|
111 |
||
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
112 |
// Loopback address |
48896
1e358cc5af98
8144672: (ch) PipeImpl should use localhost instead of loopback address
bpb
parents:
47216
diff
changeset
|
113 |
InetAddress lb = InetAddress.getLoopbackAddress(); |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
114 |
assert(lb.isLoopbackAddress()); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
115 |
InetSocketAddress sa = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
116 |
for(;;) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
117 |
// Bind ServerSocketChannel to a port on the loopback |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
118 |
// address |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
119 |
if (ssc == null || !ssc.isOpen()) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
120 |
ssc = ServerSocketChannel.open(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
121 |
ssc.socket().bind(new InetSocketAddress(lb, 0)); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
122 |
sa = new InetSocketAddress(lb, ssc.socket().getLocalPort()); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
123 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
124 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
125 |
// Establish connection (assume connections are eagerly |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
126 |
// accepted) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
127 |
sc1 = SocketChannel.open(sa); |
39748 | 128 |
RANDOM_NUMBER_GENERATOR.nextBytes(secret.array()); |
129 |
do { |
|
130 |
sc1.write(secret); |
|
131 |
} while (secret.hasRemaining()); |
|
132 |
secret.rewind(); |
|
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
133 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
134 |
// Get a connection and verify it is legitimate |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
135 |
sc2 = ssc.accept(); |
39748 | 136 |
do { |
137 |
sc2.read(bb); |
|
138 |
} while (bb.hasRemaining()); |
|
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
139 |
bb.rewind(); |
39748 | 140 |
|
141 |
if (bb.equals(secret)) |
|
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
142 |
break; |
39748 | 143 |
|
2 | 144 |
sc2.close(); |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
145 |
sc1.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
146 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
147 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
148 |
// Create source and sink channels |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
149 |
source = new SourceChannelImpl(sp, sc1); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
150 |
sink = new SinkChannelImpl(sp, sc2); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
151 |
} catch (IOException e) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
152 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
153 |
if (sc1 != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
154 |
sc1.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
155 |
if (sc2 != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
156 |
sc2.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
157 |
} catch (IOException e2) {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
158 |
ioe = e; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
159 |
} finally { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
160 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
161 |
if (ssc != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
162 |
ssc.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
163 |
} catch (IOException e2) {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
164 |
} |
2 | 165 |
} |
166 |
} |
|
167 |
} |
|
168 |
||
169 |
PipeImpl(final SelectorProvider sp) throws IOException { |
|
170 |
try { |
|
171 |
AccessController.doPrivileged(new Initializer(sp)); |
|
172 |
} catch (PrivilegedActionException x) { |
|
173 |
throw (IOException)x.getCause(); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
public SourceChannel source() { |
|
178 |
return source; |
|
179 |
} |
|
180 |
||
181 |
public SinkChannel sink() { |
|
182 |
return sink; |
|
183 |
} |
|
184 |
||
185 |
} |