|
1 /* |
|
2 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. 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. 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. |
|
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 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. |
|
24 */ |
|
25 |
|
26 package sun.nio.ch; |
|
27 |
|
28 import java.io.FileDescriptor; |
|
29 import java.io.IOException; |
|
30 import java.net.InetAddress; |
|
31 import java.net.InetSocketAddress; |
|
32 import java.net.ProtocolFamily; |
|
33 import java.net.Socket; |
|
34 import java.net.SocketAddress; |
|
35 import java.net.SocketException; |
|
36 import java.net.SocketOption; |
|
37 import java.net.SocketTimeoutException; |
|
38 import java.net.StandardProtocolFamily; |
|
39 import java.net.StandardSocketOptions; |
|
40 import java.nio.ByteBuffer; |
|
41 import java.nio.channels.AlreadyBoundException; |
|
42 import java.nio.channels.AlreadyConnectedException; |
|
43 import java.nio.channels.AsynchronousCloseException; |
|
44 import java.nio.channels.ClosedChannelException; |
|
45 import java.nio.channels.ConnectionPendingException; |
|
46 import java.nio.channels.IllegalBlockingModeException; |
|
47 import java.nio.channels.NoConnectionPendingException; |
|
48 import java.nio.channels.NotYetConnectedException; |
|
49 import java.nio.channels.SelectionKey; |
|
50 import java.nio.channels.SocketChannel; |
|
51 import java.nio.channels.spi.SelectorProvider; |
|
52 import java.util.Collections; |
|
53 import java.util.HashSet; |
|
54 import java.util.Objects; |
|
55 import java.util.Set; |
|
56 import java.util.concurrent.locks.ReentrantLock; |
|
57 |
|
58 import sun.net.ConnectionResetException; |
|
59 import sun.net.NetHooks; |
|
60 import sun.net.ext.ExtendedSocketOptions; |
|
61 import sun.net.util.SocketExceptions; |
|
62 |
|
63 /** |
|
64 * An implementation of SocketChannels |
|
65 */ |
|
66 |
|
67 class InetSocketChannelImpl extends SocketChannelImpl |
|
68 { |
|
69 // set true when exclusive binding is on and SO_REUSEADDR is emulated |
|
70 private boolean isReuseAddress; |
|
71 |
|
72 // Constructor for normal connecting sockets |
|
73 // |
|
74 InetSocketChannelImpl(SelectorProvider sp) throws IOException { |
|
75 super(sp); |
|
76 } |
|
77 |
|
78 InetSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) |
|
79 throws IOException |
|
80 { |
|
81 super(sp, fd); |
|
82 if (bound) { |
|
83 synchronized (stateLock) { |
|
84 this.localAddress = Net.localAddress(fd); |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 // Constructor for sockets obtained from server sockets |
|
90 // |
|
91 InetSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa) |
|
92 throws IOException |
|
93 { |
|
94 super(sp, fd); |
|
95 synchronized (stateLock) { |
|
96 this.localAddress = Net.localAddress(fd); |
|
97 this.remoteAddress = isa; |
|
98 this.state = ST_CONNECTED; |
|
99 } |
|
100 } |
|
101 |
|
102 @Override |
|
103 public SocketAddress getLocalAddress() throws IOException { |
|
104 synchronized (stateLock) { |
|
105 ensureOpen(); |
|
106 return Net.getRevealedLocalAddress((InetSocketAddress)localAddress); |
|
107 } |
|
108 } |
|
109 |
|
110 @Override |
|
111 public SocketAddress getRemoteAddress() throws IOException { |
|
112 synchronized (stateLock) { |
|
113 ensureOpen(); |
|
114 return remoteAddress; |
|
115 } |
|
116 } |
|
117 |
|
118 @Override |
|
119 public <T> SocketChannel setOption(SocketOption<T> name, T value) |
|
120 throws IOException |
|
121 { |
|
122 Objects.requireNonNull(name); |
|
123 if (!supportedOptions().contains(name)) |
|
124 throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
125 if (!name.type().isInstance(value)) |
|
126 throw new IllegalArgumentException("Invalid value '" + value + "'"); |
|
127 |
|
128 synchronized (stateLock) { |
|
129 ensureOpen(); |
|
130 |
|
131 if (name == StandardSocketOptions.IP_TOS) { |
|
132 ProtocolFamily family = Net.isIPv6Available() ? |
|
133 StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
|
134 Net.setSocketOption(fd, family, name, value); |
|
135 return this; |
|
136 } |
|
137 |
|
138 if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
|
139 // SO_REUSEADDR emulated when using exclusive bind |
|
140 isReuseAddress = (Boolean)value; |
|
141 return this; |
|
142 } |
|
143 |
|
144 // no options that require special handling |
|
145 Net.setSocketOption(fd, name, value); |
|
146 return this; |
|
147 } |
|
148 } |
|
149 |
|
150 @Override |
|
151 @SuppressWarnings("unchecked") |
|
152 public <T> T getOption(SocketOption<T> name) |
|
153 throws IOException |
|
154 { |
|
155 Objects.requireNonNull(name); |
|
156 if (!supportedOptions().contains(name)) |
|
157 throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
158 |
|
159 synchronized (stateLock) { |
|
160 ensureOpen(); |
|
161 |
|
162 if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
|
163 // SO_REUSEADDR emulated when using exclusive bind |
|
164 return (T)Boolean.valueOf(isReuseAddress); |
|
165 } |
|
166 |
|
167 // special handling for IP_TOS: always return 0 when IPv6 |
|
168 if (name == StandardSocketOptions.IP_TOS) { |
|
169 ProtocolFamily family = Net.isIPv6Available() ? |
|
170 StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
|
171 return (T) Net.getSocketOption(fd, family, name); |
|
172 } |
|
173 |
|
174 // no options that require special handling |
|
175 return (T) Net.getSocketOption(fd, name); |
|
176 } |
|
177 } |
|
178 |
|
179 private static class DefaultOptionsHolder { |
|
180 static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
|
181 |
|
182 private static Set<SocketOption<?>> defaultOptions() { |
|
183 HashSet<SocketOption<?>> set = new HashSet<>(); |
|
184 set.add(StandardSocketOptions.SO_SNDBUF); |
|
185 set.add(StandardSocketOptions.SO_RCVBUF); |
|
186 set.add(StandardSocketOptions.SO_KEEPALIVE); |
|
187 set.add(StandardSocketOptions.SO_REUSEADDR); |
|
188 if (Net.isReusePortAvailable()) { |
|
189 set.add(StandardSocketOptions.SO_REUSEPORT); |
|
190 } |
|
191 set.add(StandardSocketOptions.SO_LINGER); |
|
192 set.add(StandardSocketOptions.TCP_NODELAY); |
|
193 // additional options required by socket adaptor |
|
194 set.add(StandardSocketOptions.IP_TOS); |
|
195 set.add(ExtendedSocketOption.SO_OOBINLINE); |
|
196 set.addAll(ExtendedSocketOptions.clientSocketOptions()); |
|
197 return Collections.unmodifiableSet(set); |
|
198 } |
|
199 } |
|
200 |
|
201 @Override |
|
202 public final Set<SocketOption<?>> supportedOptions() { |
|
203 return DefaultOptionsHolder.defaultOptions; |
|
204 } |
|
205 |
|
206 @Override |
|
207 public Socket socket() { |
|
208 synchronized (stateLock) { |
|
209 if (socket == null) |
|
210 socket = SocketAdaptor.create(this); |
|
211 return socket; |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * Writes a byte of out of band data. |
|
217 */ |
|
218 int sendOutOfBandData(byte b) throws IOException { |
|
219 writeLock.lock(); |
|
220 try { |
|
221 boolean blocking = isBlocking(); |
|
222 int n = 0; |
|
223 try { |
|
224 beginWrite(blocking); |
|
225 if (blocking) { |
|
226 do { |
|
227 n = Net.sendOOB(fd, b); |
|
228 } while (n == IOStatus.INTERRUPTED && isOpen()); |
|
229 } else { |
|
230 n = Net.sendOOB(fd, b); |
|
231 } |
|
232 } finally { |
|
233 endWrite(blocking, n > 0); |
|
234 if (n <= 0 && isOutputClosed) |
|
235 throw new AsynchronousCloseException(); |
|
236 } |
|
237 return IOStatus.normalize(n); |
|
238 } finally { |
|
239 writeLock.unlock(); |
|
240 } |
|
241 } |
|
242 |
|
243 @Override |
|
244 public SocketChannel bind(SocketAddress local) throws IOException { |
|
245 readLock.lock(); |
|
246 try { |
|
247 writeLock.lock(); |
|
248 try { |
|
249 synchronized (stateLock) { |
|
250 ensureOpen(); |
|
251 if (state == ST_CONNECTIONPENDING) |
|
252 throw new ConnectionPendingException(); |
|
253 if (localAddress != null) |
|
254 throw new AlreadyBoundException(); |
|
255 InetSocketAddress isa = (local == null) ? |
|
256 new InetSocketAddress(0) : Net.checkAddress(local); |
|
257 SecurityManager sm = System.getSecurityManager(); |
|
258 if (sm != null) { |
|
259 sm.checkListen(isa.getPort()); |
|
260 } |
|
261 NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
|
262 Net.bind(fd, isa.getAddress(), isa.getPort()); |
|
263 localAddress = Net.localAddress(fd); |
|
264 } |
|
265 } finally { |
|
266 writeLock.unlock(); |
|
267 } |
|
268 } finally { |
|
269 readLock.unlock(); |
|
270 } |
|
271 return this; |
|
272 } |
|
273 |
|
274 |
|
275 /** |
|
276 * Checks the remote address to which this channel is to be connected. |
|
277 */ |
|
278 @Override |
|
279 protected SocketAddress checkRemote(SocketAddress sa) throws IOException { |
|
280 InetSocketAddress isa = Net.checkAddress(sa); |
|
281 SecurityManager sm = System.getSecurityManager(); |
|
282 if (sm != null) { |
|
283 sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort()); |
|
284 } |
|
285 if (isa.getAddress().isAnyLocalAddress()) { |
|
286 return new InetSocketAddress(InetAddress.getLocalHost(), isa.getPort()); |
|
287 } else { |
|
288 return isa; |
|
289 } |
|
290 } |
|
291 |
|
292 @Override |
|
293 protected int connectImpl(FileDescriptor fd, SocketAddress sa) throws IOException { |
|
294 InetSocketAddress isa = (InetSocketAddress)sa; |
|
295 return Net.connect(fd, isa.getAddress(), isa.getPort()); |
|
296 } |
|
297 |
|
298 @Override |
|
299 protected SocketAddress localAddressImpl(FileDescriptor fd) throws IOException { |
|
300 return Net.localAddress(fd); |
|
301 } |
|
302 |
|
303 @Override |
|
304 protected String getRevealedLocalAddressAsString(SocketAddress sa) { |
|
305 InetSocketAddress isa = (InetSocketAddress)sa; |
|
306 return Net.getRevealedLocalAddressAsString(isa); |
|
307 } |
|
308 } |