author | redestad |
Tue, 03 May 2016 15:50:54 +0200 | |
changeset 37781 | 71ed5645f17c |
parent 37593 | 824750ada3d6 |
child 38477 | f462865d453d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 1996, 2016, 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; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
29 |
import java.security.AccessController; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
30 |
import sun.net.ResourceManager; |
36115 | 31 |
import java.util.Set; |
32 |
import java.util.HashSet; |
|
33 |
import java.util.Collections; |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36115
diff
changeset
|
34 |
import sun.security.action.GetPropertyAction; |
2 | 35 |
|
36 |
/** |
|
37 |
* Abstract datagram and multicast socket implementation base class. |
|
38 |
* Note: This is not a public class, so that applets cannot call |
|
39 |
* into the implementation directly and hence cannot bypass the |
|
40 |
* security checks present in the DatagramSocket and MulticastSocket |
|
41 |
* classes. |
|
42 |
* |
|
43 |
* @author Pavani Diwanji |
|
44 |
*/ |
|
45 |
||
46 |
abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl |
|
47 |
{ |
|
48 |
/* timeout value for receive() */ |
|
49 |
int timeout = 0; |
|
50 |
boolean connected = false; |
|
51 |
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
|
52 |
protected InetAddress connectedAddress = null; |
2 | 53 |
private int connectedPort = -1; |
54 |
||
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
55 |
private static final String os = |
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
56 |
GetPropertyAction.privilegedGetProperty("os.name"); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
57 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
58 |
/** |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
59 |
* 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
|
60 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32503
diff
changeset
|
61 |
private static final boolean connectDisabled = os.contains("OS X"); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
62 |
|
2 | 63 |
/** |
64 |
* Load net library into runtime. |
|
65 |
*/ |
|
66 |
static { |
|
67 |
java.security.AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27078
diff
changeset
|
68 |
new java.security.PrivilegedAction<>() { |
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12538
diff
changeset
|
69 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12538
diff
changeset
|
70 |
System.loadLibrary("net"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12538
diff
changeset
|
71 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12538
diff
changeset
|
72 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
12538
diff
changeset
|
73 |
}); |
2 | 74 |
} |
75 |
||
36115 | 76 |
private static volatile boolean checkedReusePort; |
77 |
private static volatile boolean isReusePortAvailable; |
|
78 |
||
79 |
/** |
|
80 |
* Tells whether SO_REUSEPORT is supported. |
|
81 |
*/ |
|
82 |
static boolean isReusePortAvailable() { |
|
83 |
if (!checkedReusePort) { |
|
84 |
isReusePortAvailable = isReusePortAvailable0(); |
|
85 |
checkedReusePort = true; |
|
86 |
} |
|
87 |
return isReusePortAvailable; |
|
88 |
} |
|
89 |
||
90 |
private static volatile Set<SocketOption<?>> socketOptions; |
|
91 |
||
92 |
/** |
|
93 |
* Returns a set of SocketOptions supported by this impl |
|
94 |
* and by this impl's socket (Socket or ServerSocket) |
|
95 |
* |
|
96 |
* @return a Set of SocketOptions |
|
97 |
*/ |
|
98 |
@Override |
|
99 |
protected Set<SocketOption<?>> supportedOptions() { |
|
100 |
Set<SocketOption<?>> options = socketOptions; |
|
101 |
if (options == null) { |
|
102 |
if (isReusePortAvailable()) { |
|
103 |
options = new HashSet<>(); |
|
104 |
options.addAll(super.supportedOptions()); |
|
105 |
options.add(StandardSocketOptions.SO_REUSEPORT); |
|
106 |
options = Collections.unmodifiableSet(options); |
|
107 |
} else { |
|
108 |
options = super.supportedOptions(); |
|
109 |
} |
|
110 |
socketOptions = options; |
|
111 |
} |
|
112 |
return options; |
|
113 |
} |
|
114 |
||
2 | 115 |
/** |
116 |
* Creates a datagram socket |
|
117 |
*/ |
|
118 |
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
|
119 |
ResourceManager.beforeUdpCreate(); |
2 | 120 |
fd = new FileDescriptor(); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
121 |
try { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
122 |
datagramSocketCreate(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
123 |
} catch (SocketException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
124 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
125 |
fd = null; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
126 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
5506
diff
changeset
|
127 |
} |
2 | 128 |
} |
129 |
||
130 |
/** |
|
131 |
* Binds a datagram socket to a local port. |
|
132 |
*/ |
|
133 |
protected synchronized void bind(int lport, InetAddress laddr) |
|
134 |
throws SocketException { |
|
135 |
bind0(lport, laddr); |
|
136 |
} |
|
137 |
||
138 |
protected abstract void bind0(int lport, InetAddress laddr) |
|
139 |
throws SocketException; |
|
140 |
||
141 |
/** |
|
142 |
* Sends a datagram packet. The packet contains the data and the |
|
143 |
* destination address to send the packet to. |
|
14014 | 144 |
* @param p the packet to be sent. |
2 | 145 |
*/ |
146 |
protected abstract void send(DatagramPacket p) throws IOException; |
|
147 |
||
148 |
/** |
|
149 |
* Connects a datagram socket to a remote destination. This associates the remote |
|
150 |
* address with the local socket so that datagrams may only be sent to this destination |
|
151 |
* and received from this destination. |
|
152 |
* @param address the remote InetAddress to connect to |
|
153 |
* @param port the remote port number |
|
154 |
*/ |
|
155 |
protected void connect(InetAddress address, int port) throws SocketException { |
|
156 |
connect0(address, port); |
|
157 |
connectedAddress = address; |
|
158 |
connectedPort = port; |
|
159 |
connected = true; |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Disconnects a previously connected socket. Does nothing if the socket was |
|
164 |
* not connected already. |
|
165 |
*/ |
|
166 |
protected void disconnect() { |
|
16870 | 167 |
disconnect0(connectedAddress.holder().getFamily()); |
2 | 168 |
connected = false; |
169 |
connectedAddress = null; |
|
170 |
connectedPort = -1; |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Peek at the packet to see who it is from. |
|
14014 | 175 |
* @param i the address to populate with the sender address |
2 | 176 |
*/ |
177 |
protected abstract int peek(InetAddress i) throws IOException; |
|
178 |
protected abstract int peekData(DatagramPacket p) throws IOException; |
|
179 |
/** |
|
180 |
* Receive the datagram packet. |
|
14014 | 181 |
* @param p the packet to receive into |
2 | 182 |
*/ |
183 |
protected synchronized void receive(DatagramPacket p) |
|
184 |
throws IOException { |
|
185 |
receive0(p); |
|
186 |
} |
|
187 |
||
188 |
protected abstract void receive0(DatagramPacket p) |
|
189 |
throws IOException; |
|
190 |
||
191 |
/** |
|
192 |
* Set the TTL (time-to-live) option. |
|
14014 | 193 |
* @param ttl TTL to be set. |
2 | 194 |
*/ |
195 |
protected abstract void setTimeToLive(int ttl) throws IOException; |
|
196 |
||
197 |
/** |
|
198 |
* Get the TTL (time-to-live) option. |
|
199 |
*/ |
|
200 |
protected abstract int getTimeToLive() throws IOException; |
|
201 |
||
202 |
/** |
|
203 |
* Set the TTL (time-to-live) option. |
|
14014 | 204 |
* @param ttl TTL to be set. |
2 | 205 |
*/ |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10594
diff
changeset
|
206 |
@Deprecated |
2 | 207 |
protected abstract void setTTL(byte ttl) throws IOException; |
208 |
||
209 |
/** |
|
210 |
* Get the TTL (time-to-live) option. |
|
211 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10594
diff
changeset
|
212 |
@Deprecated |
2 | 213 |
protected abstract byte getTTL() throws IOException; |
214 |
||
215 |
/** |
|
216 |
* Join the multicast group. |
|
14014 | 217 |
* @param inetaddr multicast address to join. |
2 | 218 |
*/ |
219 |
protected void join(InetAddress inetaddr) throws IOException { |
|
220 |
join(inetaddr, null); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Leave the multicast group. |
|
14014 | 225 |
* @param inetaddr multicast address to leave. |
2 | 226 |
*/ |
227 |
protected void leave(InetAddress inetaddr) throws IOException { |
|
228 |
leave(inetaddr, null); |
|
229 |
} |
|
230 |
/** |
|
231 |
* Join the multicast group. |
|
14014 | 232 |
* @param mcastaddr multicast address to join. |
2 | 233 |
* @param netIf specifies the local interface to receive multicast |
234 |
* datagram packets |
|
235 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
236 |
* SocketAddress subclass not supported by this socket |
|
237 |
* @since 1.4 |
|
238 |
*/ |
|
239 |
||
240 |
protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
241 |
throws IOException { |
|
242 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
243 |
throw new IllegalArgumentException("Unsupported address type"); |
|
244 |
join(((InetSocketAddress)mcastaddr).getAddress(), netIf); |
|
245 |
} |
|
246 |
||
247 |
protected abstract void join(InetAddress inetaddr, NetworkInterface netIf) |
|
248 |
throws IOException; |
|
249 |
||
250 |
/** |
|
251 |
* Leave the multicast group. |
|
14014 | 252 |
* @param mcastaddr multicast address to leave. |
2 | 253 |
* @param netIf specified the local interface to leave the group at |
254 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
255 |
* SocketAddress subclass not supported by this socket |
|
256 |
* @since 1.4 |
|
257 |
*/ |
|
258 |
protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
259 |
throws IOException { |
|
260 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
261 |
throw new IllegalArgumentException("Unsupported address type"); |
|
262 |
leave(((InetSocketAddress)mcastaddr).getAddress(), netIf); |
|
263 |
} |
|
264 |
||
265 |
protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf) |
|
266 |
throws IOException; |
|
267 |
||
268 |
/** |
|
269 |
* Close the socket. |
|
270 |
*/ |
|
271 |
protected void close() { |
|
272 |
if (fd != null) { |
|
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 |
protected void finalize() { |
|
284 |
close(); |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* set a value - since we only support (setting) binary options |
|
289 |
* here, o must be a Boolean |
|
290 |
*/ |
|
291 |
||
292 |
public void setOption(int optID, Object o) throws SocketException { |
|
293 |
if (isClosed()) { |
|
294 |
throw new SocketException("Socket Closed"); |
|
295 |
} |
|
296 |
switch (optID) { |
|
297 |
/* check type safety b4 going native. These should never |
|
298 |
* fail, since only java.Socket* has access to |
|
299 |
* PlainSocketImpl.setOption(). |
|
300 |
*/ |
|
301 |
case SO_TIMEOUT: |
|
302 |
if (o == null || !(o instanceof Integer)) { |
|
303 |
throw new SocketException("bad argument for SO_TIMEOUT"); |
|
304 |
} |
|
305 |
int tmp = ((Integer) o).intValue(); |
|
306 |
if (tmp < 0) |
|
307 |
throw new IllegalArgumentException("timeout < 0"); |
|
308 |
timeout = tmp; |
|
309 |
return; |
|
310 |
case IP_TOS: |
|
311 |
if (o == null || !(o instanceof Integer)) { |
|
312 |
throw new SocketException("bad argument for IP_TOS"); |
|
313 |
} |
|
314 |
trafficClass = ((Integer)o).intValue(); |
|
315 |
break; |
|
316 |
case SO_REUSEADDR: |
|
317 |
if (o == null || !(o instanceof Boolean)) { |
|
318 |
throw new SocketException("bad argument for SO_REUSEADDR"); |
|
319 |
} |
|
320 |
break; |
|
321 |
case SO_BROADCAST: |
|
322 |
if (o == null || !(o instanceof Boolean)) { |
|
323 |
throw new SocketException("bad argument for SO_BROADCAST"); |
|
324 |
} |
|
325 |
break; |
|
326 |
case SO_BINDADDR: |
|
327 |
throw new SocketException("Cannot re-bind Socket"); |
|
328 |
case SO_RCVBUF: |
|
329 |
case SO_SNDBUF: |
|
330 |
if (o == null || !(o instanceof Integer) || |
|
331 |
((Integer)o).intValue() < 0) { |
|
332 |
throw new SocketException("bad argument for SO_SNDBUF or " + |
|
333 |
"SO_RCVBUF"); |
|
334 |
} |
|
335 |
break; |
|
336 |
case IP_MULTICAST_IF: |
|
337 |
if (o == null || !(o instanceof InetAddress)) |
|
338 |
throw new SocketException("bad argument for IP_MULTICAST_IF"); |
|
339 |
break; |
|
340 |
case IP_MULTICAST_IF2: |
|
341 |
if (o == null || !(o instanceof NetworkInterface)) |
|
342 |
throw new SocketException("bad argument for IP_MULTICAST_IF2"); |
|
343 |
break; |
|
344 |
case IP_MULTICAST_LOOP: |
|
345 |
if (o == null || !(o instanceof Boolean)) |
|
346 |
throw new SocketException("bad argument for IP_MULTICAST_LOOP"); |
|
347 |
break; |
|
36115 | 348 |
case SO_REUSEPORT: |
349 |
if (o == null || !(o instanceof Boolean)) { |
|
350 |
throw new SocketException("bad argument for SO_REUSEPORT"); |
|
351 |
} |
|
352 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { |
|
353 |
throw new UnsupportedOperationException("unsupported option"); |
|
354 |
} |
|
355 |
break; |
|
2 | 356 |
default: |
357 |
throw new SocketException("invalid option: " + optID); |
|
358 |
} |
|
359 |
socketSetOption(optID, o); |
|
360 |
} |
|
361 |
||
362 |
/* |
|
363 |
* get option's state - set or not |
|
364 |
*/ |
|
365 |
||
366 |
public Object getOption(int optID) throws SocketException { |
|
367 |
if (isClosed()) { |
|
368 |
throw new SocketException("Socket Closed"); |
|
369 |
} |
|
370 |
||
371 |
Object result; |
|
372 |
||
373 |
switch (optID) { |
|
374 |
case SO_TIMEOUT: |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
375 |
result = timeout; |
2 | 376 |
break; |
377 |
||
378 |
case IP_TOS: |
|
379 |
result = socketGetOption(optID); |
|
380 |
if ( ((Integer)result).intValue() == -1) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
381 |
result = trafficClass; |
2 | 382 |
} |
383 |
break; |
|
384 |
||
385 |
case SO_BINDADDR: |
|
386 |
case IP_MULTICAST_IF: |
|
387 |
case IP_MULTICAST_IF2: |
|
388 |
case SO_RCVBUF: |
|
389 |
case SO_SNDBUF: |
|
390 |
case IP_MULTICAST_LOOP: |
|
391 |
case SO_REUSEADDR: |
|
392 |
case SO_BROADCAST: |
|
393 |
result = socketGetOption(optID); |
|
394 |
break; |
|
395 |
||
36115 | 396 |
case SO_REUSEPORT: |
397 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { |
|
398 |
throw new UnsupportedOperationException("unsupported option"); |
|
399 |
} |
|
400 |
result = socketGetOption(optID); |
|
401 |
break; |
|
402 |
||
2 | 403 |
default: |
404 |
throw new SocketException("invalid option: " + optID); |
|
405 |
} |
|
406 |
||
407 |
return result; |
|
408 |
} |
|
409 |
||
410 |
protected abstract void datagramSocketCreate() throws SocketException; |
|
411 |
protected abstract void datagramSocketClose(); |
|
412 |
protected abstract void socketSetOption(int opt, Object val) |
|
413 |
throws SocketException; |
|
414 |
protected abstract Object socketGetOption(int opt) throws SocketException; |
|
415 |
||
416 |
protected abstract void connect0(InetAddress address, int port) throws SocketException; |
|
417 |
protected abstract void disconnect0(int family); |
|
418 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
419 |
protected boolean nativeConnectDisabled() { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
420 |
return connectDisabled; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10693
diff
changeset
|
421 |
} |
27078 | 422 |
|
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
29986
diff
changeset
|
423 |
abstract int dataAvailable(); |
36115 | 424 |
private static native boolean isReusePortAvailable0(); |
2 | 425 |
} |