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