author | chegar |
Fri, 18 Oct 2019 21:25:01 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58697 | e3ff12d14d43 |
parent 58680 | 7e9e2f10a050 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, 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 |
package java.net; |
|
26 |
||
27 |
import java.io.FileDescriptor; |
|
28 |
import java.io.IOException; |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
29 |
import java.util.Collections; |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
30 |
import java.util.HashSet; |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
31 |
import java.util.Objects; |
36115 | 32 |
import java.util.Set; |
58680 | 33 |
import sun.net.PlatformDatagramSocketImpl; |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
34 |
import sun.net.ResourceManager; |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
35 |
import sun.net.ext.ExtendedSocketOptions; |
55375 | 36 |
import sun.net.util.IPAddressUtil; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36115
diff
changeset
|
37 |
import sun.security.action.GetPropertyAction; |
2 | 38 |
|
39 |
/** |
|
40 |
* Abstract datagram and multicast socket implementation base class. |
|
41 |
* Note: This is not a public class, so that applets cannot call |
|
42 |
* into the implementation directly and hence cannot bypass the |
|
43 |
* security checks present in the DatagramSocket and MulticastSocket |
|
44 |
* classes. |
|
45 |
* |
|
46 |
* @author Pavani Diwanji |
|
47 |
*/ |
|
48 |
||
58697 | 49 |
abstract class AbstractPlainDatagramSocketImpl |
50 |
extends DatagramSocketImpl |
|
51 |
implements PlatformDatagramSocketImpl |
|
2 | 52 |
{ |
53 |
/* timeout value for receive() */ |
|
58680 | 54 |
int timeout; |
55 |
boolean connected; |
|
56 |
private int trafficClass; |
|
57 |
protected InetAddress connectedAddress; |
|
2 | 58 |
private int connectedPort = -1; |
59 |
||
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
60 |
private static final String os = |
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
61 |
GetPropertyAction.privilegedGetProperty("os.name"); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
62 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
63 |
/** |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
64 |
* flag set if the native connect() call not to be used |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
65 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32503
diff
changeset
|
66 |
private static final boolean connectDisabled = os.contains("OS X"); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
67 |
|
2 | 68 |
/** |
69 |
* Load net library into runtime. |
|
70 |
*/ |
|
71 |
static { |
|
55693
9a97b1393e72
8227587: Add internal privileged System.loadLibrary
redestad
parents:
55375
diff
changeset
|
72 |
jdk.internal.loader.BootLoader.loadLibrary("net"); |
2 | 73 |
} |
74 |
||
36115 | 75 |
private static volatile boolean checkedReusePort; |
76 |
private static volatile boolean isReusePortAvailable; |
|
77 |
||
78 |
/** |
|
79 |
* Tells whether SO_REUSEPORT is supported. |
|
80 |
*/ |
|
81 |
static boolean isReusePortAvailable() { |
|
82 |
if (!checkedReusePort) { |
|
83 |
isReusePortAvailable = isReusePortAvailable0(); |
|
84 |
checkedReusePort = true; |
|
85 |
} |
|
86 |
return isReusePortAvailable; |
|
87 |
} |
|
88 |
||
58680 | 89 |
private final boolean isMulticast; |
90 |
||
91 |
AbstractPlainDatagramSocketImpl(boolean isMulticast) { |
|
92 |
this.isMulticast = isMulticast; |
|
93 |
} |
|
94 |
||
36115 | 95 |
/** |
2 | 96 |
* Creates a datagram socket |
97 |
*/ |
|
98 |
protected synchronized void create() throws SocketException { |
|
10594
d6723ee39ebf
7073491: -Dsun.net.maxDatagramSockets=1 does not work correctly with system.gc()
michaelm
parents:
9035
diff
changeset
|
99 |
ResourceManager.beforeUdpCreate(); |
2 | 100 |
fd = new FileDescriptor(); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
101 |
try { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
102 |
datagramSocketCreate(); |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
103 |
SocketCleanable.register(fd); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
104 |
} catch (SocketException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
105 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
106 |
fd = null; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
107 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
108 |
} |
2 | 109 |
} |
110 |
||
111 |
/** |
|
112 |
* Binds a datagram socket to a local port. |
|
113 |
*/ |
|
114 |
protected synchronized void bind(int lport, InetAddress laddr) |
|
115 |
throws SocketException { |
|
55375 | 116 |
if (laddr.isLinkLocalAddress()) { |
117 |
laddr = IPAddressUtil.toScopedAddress(laddr); |
|
118 |
} |
|
2 | 119 |
bind0(lport, laddr); |
120 |
} |
|
121 |
||
122 |
protected abstract void bind0(int lport, InetAddress laddr) |
|
123 |
throws SocketException; |
|
124 |
||
125 |
/** |
|
126 |
* Sends a datagram packet. The packet contains the data and the |
|
127 |
* destination address to send the packet to. |
|
14014 | 128 |
* @param p the packet to be sent. |
2 | 129 |
*/ |
55375 | 130 |
protected void send(DatagramPacket p) throws IOException { |
131 |
InetAddress orig = p.getAddress(); |
|
132 |
if (orig.isLinkLocalAddress()) { |
|
133 |
InetAddress scoped = IPAddressUtil.toScopedAddress(orig); |
|
134 |
if (orig != scoped) { |
|
135 |
p = new DatagramPacket(p.getData(), p.getOffset(), |
|
136 |
p.getLength(), scoped, p.getPort()); |
|
137 |
} |
|
138 |
} |
|
139 |
send0(p); |
|
140 |
} |
|
141 |
||
142 |
protected abstract void send0(DatagramPacket p) throws IOException; |
|
2 | 143 |
|
144 |
/** |
|
145 |
* Connects a datagram socket to a remote destination. This associates the remote |
|
146 |
* address with the local socket so that datagrams may only be sent to this destination |
|
147 |
* and received from this destination. |
|
148 |
* @param address the remote InetAddress to connect to |
|
149 |
* @param port the remote port number |
|
150 |
*/ |
|
151 |
protected void connect(InetAddress address, int port) throws SocketException { |
|
55375 | 152 |
if (address.isLinkLocalAddress()) { |
153 |
address = IPAddressUtil.toScopedAddress(address); |
|
154 |
} |
|
2 | 155 |
connect0(address, port); |
156 |
connectedAddress = address; |
|
157 |
connectedPort = port; |
|
158 |
connected = true; |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Disconnects a previously connected socket. Does nothing if the socket was |
|
163 |
* not connected already. |
|
164 |
*/ |
|
165 |
protected void disconnect() { |
|
16870 | 166 |
disconnect0(connectedAddress.holder().getFamily()); |
2 | 167 |
connected = false; |
168 |
connectedAddress = null; |
|
169 |
connectedPort = -1; |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Peek at the packet to see who it is from. |
|
14014 | 174 |
* @param i the address to populate with the sender address |
2 | 175 |
*/ |
176 |
protected abstract int peek(InetAddress i) throws IOException; |
|
177 |
protected abstract int peekData(DatagramPacket p) throws IOException; |
|
178 |
/** |
|
179 |
* Receive the datagram packet. |
|
14014 | 180 |
* @param p the packet to receive into |
2 | 181 |
*/ |
182 |
protected synchronized void receive(DatagramPacket p) |
|
183 |
throws IOException { |
|
184 |
receive0(p); |
|
185 |
} |
|
186 |
||
187 |
protected abstract void receive0(DatagramPacket p) |
|
188 |
throws IOException; |
|
189 |
||
190 |
/** |
|
191 |
* Set the TTL (time-to-live) option. |
|
14014 | 192 |
* @param ttl TTL to be set. |
2 | 193 |
*/ |
194 |
protected abstract void setTimeToLive(int ttl) throws IOException; |
|
195 |
||
196 |
/** |
|
197 |
* Get the TTL (time-to-live) option. |
|
198 |
*/ |
|
199 |
protected abstract int getTimeToLive() throws IOException; |
|
200 |
||
201 |
/** |
|
202 |
* Set the TTL (time-to-live) option. |
|
14014 | 203 |
* @param ttl TTL to be set. |
2 | 204 |
*/ |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10594
diff
changeset
|
205 |
@Deprecated |
2 | 206 |
protected abstract void setTTL(byte ttl) throws IOException; |
207 |
||
208 |
/** |
|
209 |
* Get the TTL (time-to-live) option. |
|
210 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10594
diff
changeset
|
211 |
@Deprecated |
2 | 212 |
protected abstract byte getTTL() throws IOException; |
213 |
||
214 |
/** |
|
215 |
* Join the multicast group. |
|
14014 | 216 |
* @param inetaddr multicast address to join. |
2 | 217 |
*/ |
218 |
protected void join(InetAddress inetaddr) throws IOException { |
|
219 |
join(inetaddr, null); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Leave the multicast group. |
|
14014 | 224 |
* @param inetaddr multicast address to leave. |
2 | 225 |
*/ |
226 |
protected void leave(InetAddress inetaddr) throws IOException { |
|
227 |
leave(inetaddr, null); |
|
228 |
} |
|
229 |
/** |
|
230 |
* Join the multicast group. |
|
14014 | 231 |
* @param mcastaddr multicast address to join. |
2 | 232 |
* @param netIf specifies the local interface to receive multicast |
233 |
* datagram packets |
|
234 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
235 |
* SocketAddress subclass not supported by this socket |
|
236 |
* @since 1.4 |
|
237 |
*/ |
|
238 |
||
239 |
protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
240 |
throws IOException { |
|
241 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
242 |
throw new IllegalArgumentException("Unsupported address type"); |
|
243 |
join(((InetSocketAddress)mcastaddr).getAddress(), netIf); |
|
244 |
} |
|
245 |
||
246 |
protected abstract void join(InetAddress inetaddr, NetworkInterface netIf) |
|
247 |
throws IOException; |
|
248 |
||
249 |
/** |
|
250 |
* Leave the multicast group. |
|
14014 | 251 |
* @param mcastaddr multicast address to leave. |
2 | 252 |
* @param netIf specified the local interface to leave the group at |
253 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
254 |
* SocketAddress subclass not supported by this socket |
|
255 |
* @since 1.4 |
|
256 |
*/ |
|
257 |
protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
258 |
throws IOException { |
|
259 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
260 |
throw new IllegalArgumentException("Unsupported address type"); |
|
261 |
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf); |
|
262 |
} |
|
263 |
||
264 |
protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf) |
|
265 |
throws IOException; |
|
266 |
||
267 |
/** |
|
268 |
* Close the socket. |
|
269 |
*/ |
|
270 |
protected void close() { |
|
271 |
if (fd != null) { |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
272 |
SocketCleanable.unregister(fd); |
2 | 273 |
datagramSocketClose(); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
274 |
ResourceManager.afterUdpClose(); |
2 | 275 |
fd = null; |
276 |
} |
|
277 |
} |
|
278 |
||
279 |
protected boolean isClosed() { |
|
280 |
return (fd == null) ? true : false; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* set a value - since we only support (setting) binary options |
|
285 |
* here, o must be a Boolean |
|
286 |
*/ |
|
287 |
||
288 |
public void setOption(int optID, Object o) throws SocketException { |
|
289 |
if (isClosed()) { |
|
290 |
throw new SocketException("Socket Closed"); |
|
291 |
} |
|
292 |
switch (optID) { |
|
293 |
/* check type safety b4 going native. These should never |
|
294 |
* fail, since only java.Socket* has access to |
|
295 |
* PlainSocketImpl.setOption(). |
|
296 |
*/ |
|
297 |
case SO_TIMEOUT: |
|
298 |
if (o == null || !(o instanceof Integer)) { |
|
299 |
throw new SocketException("bad argument for SO_TIMEOUT"); |
|
300 |
} |
|
301 |
int tmp = ((Integer) o).intValue(); |
|
302 |
if (tmp < 0) |
|
303 |
throw new IllegalArgumentException("timeout < 0"); |
|
304 |
timeout = tmp; |
|
305 |
return; |
|
306 |
case IP_TOS: |
|
307 |
if (o == null || !(o instanceof Integer)) { |
|
308 |
throw new SocketException("bad argument for IP_TOS"); |
|
309 |
} |
|
310 |
trafficClass = ((Integer)o).intValue(); |
|
311 |
break; |
|
312 |
case SO_REUSEADDR: |
|
313 |
if (o == null || !(o instanceof Boolean)) { |
|
314 |
throw new SocketException("bad argument for SO_REUSEADDR"); |
|
315 |
} |
|
316 |
break; |
|
317 |
case SO_BROADCAST: |
|
318 |
if (o == null || !(o instanceof Boolean)) { |
|
319 |
throw new SocketException("bad argument for SO_BROADCAST"); |
|
320 |
} |
|
321 |
break; |
|
322 |
case SO_BINDADDR: |
|
323 |
throw new SocketException("Cannot re-bind Socket"); |
|
324 |
case SO_RCVBUF: |
|
325 |
case SO_SNDBUF: |
|
326 |
if (o == null || !(o instanceof Integer) || |
|
327 |
((Integer)o).intValue() < 0) { |
|
328 |
throw new SocketException("bad argument for SO_SNDBUF or " + |
|
329 |
"SO_RCVBUF"); |
|
330 |
} |
|
331 |
break; |
|
332 |
case IP_MULTICAST_IF: |
|
333 |
if (o == null || !(o instanceof InetAddress)) |
|
334 |
throw new SocketException("bad argument for IP_MULTICAST_IF"); |
|
335 |
break; |
|
336 |
case IP_MULTICAST_IF2: |
|
337 |
if (o == null || !(o instanceof NetworkInterface)) |
|
338 |
throw new SocketException("bad argument for IP_MULTICAST_IF2"); |
|
339 |
break; |
|
340 |
case IP_MULTICAST_LOOP: |
|
341 |
if (o == null || !(o instanceof Boolean)) |
|
342 |
throw new SocketException("bad argument for IP_MULTICAST_LOOP"); |
|
343 |
break; |
|
36115 | 344 |
case SO_REUSEPORT: |
345 |
if (o == null || !(o instanceof Boolean)) { |
|
346 |
throw new SocketException("bad argument for SO_REUSEPORT"); |
|
347 |
} |
|
348 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { |
|
349 |
throw new UnsupportedOperationException("unsupported option"); |
|
350 |
} |
|
351 |
break; |
|
2 | 352 |
default: |
353 |
throw new SocketException("invalid option: " + optID); |
|
354 |
} |
|
355 |
socketSetOption(optID, o); |
|
356 |
} |
|
357 |
||
358 |
/* |
|
359 |
* get option's state - set or not |
|
360 |
*/ |
|
361 |
||
362 |
public Object getOption(int optID) throws SocketException { |
|
363 |
if (isClosed()) { |
|
364 |
throw new SocketException("Socket Closed"); |
|
365 |
} |
|
366 |
||
367 |
Object result; |
|
368 |
||
369 |
switch (optID) { |
|
370 |
case SO_TIMEOUT: |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
371 |
result = timeout; |
2 | 372 |
break; |
373 |
||
374 |
case IP_TOS: |
|
375 |
result = socketGetOption(optID); |
|
376 |
if ( ((Integer)result).intValue() == -1) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
377 |
result = trafficClass; |
2 | 378 |
} |
379 |
break; |
|
380 |
||
381 |
case SO_BINDADDR: |
|
382 |
case IP_MULTICAST_IF: |
|
383 |
case IP_MULTICAST_IF2: |
|
384 |
case SO_RCVBUF: |
|
385 |
case SO_SNDBUF: |
|
386 |
case IP_MULTICAST_LOOP: |
|
387 |
case SO_REUSEADDR: |
|
388 |
case SO_BROADCAST: |
|
389 |
result = socketGetOption(optID); |
|
390 |
break; |
|
391 |
||
36115 | 392 |
case SO_REUSEPORT: |
393 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { |
|
394 |
throw new UnsupportedOperationException("unsupported option"); |
|
395 |
} |
|
396 |
result = socketGetOption(optID); |
|
397 |
break; |
|
398 |
||
2 | 399 |
default: |
400 |
throw new SocketException("invalid option: " + optID); |
|
401 |
} |
|
402 |
||
403 |
return result; |
|
404 |
} |
|
405 |
||
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
406 |
static final ExtendedSocketOptions extendedOptions = |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
407 |
ExtendedSocketOptions.getInstance(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
408 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
409 |
private static final Set<SocketOption<?>> datagramSocketOptions = datagramSocketOptions(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
410 |
private static final Set<SocketOption<?>> multicastSocketOptions = multicastSocketOptions(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
411 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
412 |
private static Set<SocketOption<?>> datagramSocketOptions() { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
413 |
HashSet<SocketOption<?>> options = new HashSet<>(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
414 |
options.add(StandardSocketOptions.SO_SNDBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
415 |
options.add(StandardSocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
416 |
options.add(StandardSocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
417 |
options.add(StandardSocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
418 |
if (isReusePortAvailable()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
419 |
options.add(StandardSocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
420 |
options.addAll(ExtendedSocketOptions.datagramSocketOptions()); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
421 |
return Collections.unmodifiableSet(options); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
422 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
423 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
424 |
private static Set<SocketOption<?>> multicastSocketOptions() { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
425 |
HashSet<SocketOption<?>> options = new HashSet<>(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
426 |
options.add(StandardSocketOptions.SO_SNDBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
427 |
options.add(StandardSocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
428 |
options.add(StandardSocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
429 |
options.add(StandardSocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
430 |
options.add(StandardSocketOptions.IP_MULTICAST_IF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
431 |
options.add(StandardSocketOptions.IP_MULTICAST_TTL); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
432 |
options.add(StandardSocketOptions.IP_MULTICAST_LOOP); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
433 |
if (isReusePortAvailable()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
434 |
options.add(StandardSocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
435 |
options.addAll(ExtendedSocketOptions.datagramSocketOptions()); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
436 |
return Collections.unmodifiableSet(options); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
437 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
438 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
439 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
440 |
protected Set<SocketOption<?>> supportedOptions() { |
58680 | 441 |
if (isMulticast) |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
442 |
return multicastSocketOptions; |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
443 |
else |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
444 |
return datagramSocketOptions; |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
445 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
446 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
447 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
448 |
protected <T> void setOption(SocketOption<T> name, T value) throws IOException { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
449 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
450 |
if (!supportedOptions().contains(name)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
451 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
452 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
453 |
if (!name.type().isInstance(value)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
454 |
throw new IllegalArgumentException("Invalid value '" + value + "'"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
455 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
456 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
457 |
throw new SocketException("Socket closed"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
458 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
459 |
if (name == StandardSocketOptions.SO_SNDBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
460 |
if (((Integer)value).intValue() < 0) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
461 |
throw new IllegalArgumentException("Invalid send buffer size:" + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
462 |
setOption(SocketOptions.SO_SNDBUF, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
463 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
464 |
if (((Integer)value).intValue() < 0) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
465 |
throw new IllegalArgumentException("Invalid recv buffer size:" + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
466 |
setOption(SocketOptions.SO_RCVBUF, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
467 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
468 |
setOption(SocketOptions.SO_REUSEADDR, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
469 |
} else if (name == StandardSocketOptions.SO_REUSEPORT) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
470 |
setOption(SocketOptions.SO_REUSEPORT, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
471 |
} else if (name == StandardSocketOptions.IP_TOS) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
472 |
int i = ((Integer)value).intValue(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
473 |
if (i < 0 || i > 255) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
474 |
throw new IllegalArgumentException("Invalid IP_TOS value: " + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
475 |
setOption(SocketOptions.IP_TOS, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
476 |
} else if (name == StandardSocketOptions.IP_MULTICAST_IF ) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
477 |
setOption(SocketOptions.IP_MULTICAST_IF2, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
478 |
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
479 |
int i = ((Integer)value).intValue(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
480 |
if (i < 0 || i > 255) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
481 |
throw new IllegalArgumentException("Invalid TTL/hop value: " + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
482 |
setTimeToLive((Integer)value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
483 |
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
484 |
setOption(SocketOptions.IP_MULTICAST_LOOP, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
485 |
} else if (extendedOptions.isOptionSupported(name)) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
486 |
extendedOptions.setOption(fd, name, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
487 |
} else { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
488 |
throw new AssertionError("unknown option :" + name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
489 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
490 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
491 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
492 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
493 |
@SuppressWarnings("unchecked") |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
494 |
protected <T> T getOption(SocketOption<T> name) throws IOException { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
495 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
496 |
if (!supportedOptions().contains(name)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
497 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
498 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
499 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
500 |
throw new SocketException("Socket closed"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
501 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
502 |
if (name == StandardSocketOptions.SO_SNDBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
503 |
return (T) getOption(SocketOptions.SO_SNDBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
504 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
505 |
return (T) getOption(SocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
506 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
507 |
return (T) getOption(SocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
508 |
} else if (name == StandardSocketOptions.SO_REUSEPORT) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
509 |
return (T) getOption(SocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
510 |
} else if (name == StandardSocketOptions.IP_TOS) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
511 |
return (T) getOption(SocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
512 |
} else if (name == StandardSocketOptions.IP_MULTICAST_IF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
513 |
return (T) getOption(SocketOptions.IP_MULTICAST_IF2); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
514 |
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
515 |
return (T) ((Integer) getTimeToLive()); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
516 |
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
517 |
return (T) getOption(SocketOptions.IP_MULTICAST_LOOP); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
518 |
} else if (extendedOptions.isOptionSupported(name)) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
519 |
return (T) extendedOptions.getOption(fd, name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
520 |
} else { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
521 |
throw new AssertionError("unknown option: " + name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
522 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
523 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
48737
diff
changeset
|
524 |
|
2 | 525 |
protected abstract void datagramSocketCreate() throws SocketException; |
526 |
protected abstract void datagramSocketClose(); |
|
527 |
protected abstract void socketSetOption(int opt, Object val) |
|
528 |
throws SocketException; |
|
529 |
protected abstract Object socketGetOption(int opt) throws SocketException; |
|
530 |
||
531 |
protected abstract void connect0(InetAddress address, int port) throws SocketException; |
|
532 |
protected abstract void disconnect0(int family); |
|
533 |
||
58680 | 534 |
@Override |
535 |
public boolean nativeConnectDisabled() { |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
536 |
return connectDisabled; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
537 |
} |
27078 | 538 |
|
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
29986
diff
changeset
|
539 |
abstract int dataAvailable(); |
36115 | 540 |
private static native boolean isReusePortAvailable0(); |
2 | 541 |
} |