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