author | pconcannon |
Wed, 20 Nov 2019 10:13:10 +0000 | |
changeset 59200 | a686b67a59d9 |
parent 55375 | 96c7427456f9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
59200
a686b67a59d9
8234103: DatagramSocketImpl::socket is not needed
pconcannon
parents:
55375
diff
changeset
|
2 |
* Copyright (c) 2007, 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.IOException; |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49892
diff
changeset
|
28 |
import jdk.internal.access.SharedSecrets; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49892
diff
changeset
|
29 |
import jdk.internal.access.JavaIOFileDescriptorAccess; |
2 | 30 |
|
31 |
/** |
|
32 |
* This class defines the plain DatagramSocketImpl that is used on |
|
33 |
* Windows platforms greater than or equal to Windows Vista. These |
|
34 |
* platforms have a dual layer TCP/IP stack and can handle both IPv4 |
|
35 |
* and IPV6 through a single file descriptor. |
|
36 |
* <p> |
|
37 |
* Note: Multicasting on a dual layer TCP/IP stack is always done with |
|
38 |
* TwoStacksPlainDatagramSocketImpl. This is to overcome the lack |
|
39 |
* of behavior defined for multicasting over a dual layer socket by the RFC. |
|
40 |
* |
|
41 |
* @author Chris Hegarty |
|
42 |
*/ |
|
43 |
||
44 |
class DualStackPlainDatagramSocketImpl extends AbstractPlainDatagramSocketImpl |
|
45 |
{ |
|
46 |
static JavaIOFileDescriptorAccess fdAccess = SharedSecrets.getJavaIOFileDescriptorAccess(); |
|
47 |
||
23023 | 48 |
static { |
49 |
initIDs(); |
|
50 |
} |
|
51 |
||
18192 | 52 |
// true if this socket is exclusively bound |
53 |
private final boolean exclusiveBind; |
|
54 |
||
55 |
/* |
|
56 |
* Set to true if SO_REUSEADDR is set after the socket is bound to |
|
57 |
* indicate SO_REUSEADDR is being emulated |
|
58 |
*/ |
|
59 |
private boolean reuseAddressEmulated; |
|
60 |
||
61 |
// emulates SO_REUSEADDR when exclusiveBind is true and socket is bound |
|
62 |
private boolean isReuseAddress; |
|
63 |
||
64 |
DualStackPlainDatagramSocketImpl(boolean exclBind) { |
|
59200
a686b67a59d9
8234103: DatagramSocketImpl::socket is not needed
pconcannon
parents:
55375
diff
changeset
|
65 |
super(false); |
18192 | 66 |
exclusiveBind = exclBind; |
67 |
} |
|
68 |
||
2 | 69 |
protected void datagramSocketCreate() throws SocketException { |
70 |
if (fd == null) |
|
71 |
throw new SocketException("Socket closed"); |
|
72 |
||
49892
8bed781a8d9c
8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents:
47216
diff
changeset
|
73 |
int newfd = socketCreate(); |
2 | 74 |
|
75 |
fdAccess.set(fd, newfd); |
|
76 |
} |
|
77 |
||
78 |
protected synchronized void bind0(int lport, InetAddress laddr) |
|
79 |
throws SocketException { |
|
80 |
int nativefd = checkAndReturnNativeFD(); |
|
81 |
||
82 |
if (laddr == null) |
|
83 |
throw new NullPointerException("argument address"); |
|
84 |
||
18192 | 85 |
socketBind(nativefd, laddr, lport, exclusiveBind); |
2 | 86 |
if (lport == 0) { |
87 |
localPort = socketLocalPort(nativefd); |
|
88 |
} else { |
|
89 |
localPort = lport; |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
protected synchronized int peek(InetAddress address) throws IOException { |
|
94 |
int nativefd = checkAndReturnNativeFD(); |
|
95 |
||
96 |
if (address == null) |
|
97 |
throw new NullPointerException("Null address in peek()"); |
|
98 |
||
99 |
// Use peekData() |
|
100 |
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1); |
|
101 |
int peekPort = peekData(peekPacket); |
|
102 |
address = peekPacket.getAddress(); |
|
103 |
return peekPort; |
|
104 |
} |
|
105 |
||
106 |
protected synchronized int peekData(DatagramPacket p) throws IOException { |
|
107 |
int nativefd = checkAndReturnNativeFD(); |
|
108 |
||
109 |
if (p == null) |
|
110 |
throw new NullPointerException("packet"); |
|
111 |
if (p.getData() == null) |
|
112 |
throw new NullPointerException("packet buffer"); |
|
113 |
||
114 |
return socketReceiveOrPeekData(nativefd, p, timeout, connected, true /*peek*/); |
|
115 |
} |
|
116 |
||
117 |
protected synchronized void receive0(DatagramPacket p) throws IOException { |
|
118 |
int nativefd = checkAndReturnNativeFD(); |
|
119 |
||
120 |
if (p == null) |
|
121 |
throw new NullPointerException("packet"); |
|
122 |
if (p.getData() == null) |
|
123 |
throw new NullPointerException("packet buffer"); |
|
124 |
||
125 |
socketReceiveOrPeekData(nativefd, p, timeout, connected, false /*receive*/); |
|
126 |
} |
|
127 |
||
55375 | 128 |
protected void send0(DatagramPacket p) throws IOException { |
2 | 129 |
int nativefd = checkAndReturnNativeFD(); |
130 |
||
131 |
if (p == null) |
|
132 |
throw new NullPointerException("null packet"); |
|
133 |
||
134 |
if (p.getAddress() == null ||p.getData() ==null) |
|
135 |
throw new NullPointerException("null address || null buffer"); |
|
136 |
||
137 |
socketSend(nativefd, p.getData(), p.getOffset(), p.getLength(), |
|
138 |
p.getAddress(), p.getPort(), connected); |
|
139 |
} |
|
140 |
||
141 |
protected void connect0(InetAddress address, int port) throws SocketException { |
|
142 |
int nativefd = checkAndReturnNativeFD(); |
|
143 |
||
144 |
if (address == null) |
|
145 |
throw new NullPointerException("address"); |
|
146 |
||
147 |
socketConnect(nativefd, address, port); |
|
148 |
} |
|
149 |
||
150 |
protected void disconnect0(int family /*unused*/) { |
|
151 |
if (fd == null || !fd.valid()) |
|
152 |
return; // disconnect doesn't throw any exceptions |
|
153 |
||
154 |
socketDisconnect(fdAccess.get(fd)); |
|
155 |
} |
|
156 |
||
157 |
protected void datagramSocketClose() { |
|
158 |
if (fd == null || !fd.valid()) |
|
159 |
return; // close doesn't throw any exceptions |
|
160 |
||
161 |
socketClose(fdAccess.get(fd)); |
|
162 |
fdAccess.set(fd, -1); |
|
163 |
} |
|
164 |
||
18192 | 165 |
@SuppressWarnings("fallthrough") |
2 | 166 |
protected void socketSetOption(int opt, Object val) throws SocketException { |
167 |
int nativefd = checkAndReturnNativeFD(); |
|
168 |
||
169 |
int optionValue = 0; |
|
170 |
||
36115 | 171 |
// SO_REUSEPORT is not supported on Windows. |
172 |
if (opt == SO_REUSEPORT) { |
|
173 |
throw new UnsupportedOperationException("unsupported option"); |
|
174 |
} |
|
175 |
||
2 | 176 |
switch(opt) { |
177 |
case IP_TOS : |
|
178 |
case SO_RCVBUF : |
|
179 |
case SO_SNDBUF : |
|
180 |
optionValue = ((Integer)val).intValue(); |
|
181 |
break; |
|
182 |
case SO_REUSEADDR : |
|
18192 | 183 |
if (exclusiveBind && localPort != 0) { |
184 |
// socket already bound, emulate SO_REUSEADDR |
|
185 |
reuseAddressEmulated = true; |
|
186 |
isReuseAddress = (Boolean)val; |
|
187 |
return; |
|
188 |
} |
|
189 |
//Intentional fallthrough |
|
2 | 190 |
case SO_BROADCAST : |
191 |
optionValue = ((Boolean)val).booleanValue() ? 1 : 0; |
|
192 |
break; |
|
193 |
default: /* shouldn't get here */ |
|
194 |
throw new SocketException("Option not supported"); |
|
195 |
} |
|
196 |
||
197 |
socketSetIntOption(nativefd, opt, optionValue); |
|
198 |
} |
|
199 |
||
200 |
protected Object socketGetOption(int opt) throws SocketException { |
|
201 |
int nativefd = checkAndReturnNativeFD(); |
|
202 |
||
203 |
// SO_BINDADDR is not a socket option. |
|
204 |
if (opt == SO_BINDADDR) { |
|
205 |
return socketLocalAddress(nativefd); |
|
206 |
} |
|
18192 | 207 |
if (opt == SO_REUSEADDR && reuseAddressEmulated) |
208 |
return isReuseAddress; |
|
36115 | 209 |
// SO_REUSEPORT is not supported on Windows. |
210 |
if (opt == SO_REUSEPORT) |
|
211 |
throw new UnsupportedOperationException("unsupported option"); |
|
2 | 212 |
|
213 |
int value = socketGetIntOption(nativefd, opt); |
|
214 |
Object returnValue = null; |
|
215 |
||
216 |
switch (opt) { |
|
217 |
case SO_REUSEADDR : |
|
218 |
case SO_BROADCAST : |
|
219 |
returnValue = (value == 0) ? Boolean.FALSE : Boolean.TRUE; |
|
220 |
break; |
|
221 |
case IP_TOS : |
|
222 |
case SO_RCVBUF : |
|
223 |
case SO_SNDBUF : |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
36115
diff
changeset
|
224 |
returnValue = Integer.valueOf(value); |
2 | 225 |
break; |
226 |
default: /* shouldn't get here */ |
|
227 |
throw new SocketException("Option not supported"); |
|
228 |
} |
|
229 |
||
230 |
return returnValue; |
|
231 |
} |
|
232 |
||
233 |
/* Multicast specific methods. |
|
234 |
* Multicasting on a dual layer TCP/IP stack is always done with |
|
235 |
* TwoStacksPlainDatagramSocketImpl. This is to overcome the lack |
|
236 |
* of behavior defined for multicasting over a dual layer socket by the RFC. |
|
237 |
*/ |
|
238 |
protected void join(InetAddress inetaddr, NetworkInterface netIf) |
|
239 |
throws IOException { |
|
240 |
throw new IOException("Method not implemented!"); |
|
241 |
} |
|
242 |
||
243 |
protected void leave(InetAddress inetaddr, NetworkInterface netIf) |
|
244 |
throws IOException { |
|
245 |
throw new IOException("Method not implemented!"); |
|
246 |
} |
|
247 |
||
248 |
protected void setTimeToLive(int ttl) throws IOException { |
|
249 |
throw new IOException("Method not implemented!"); |
|
250 |
} |
|
251 |
||
252 |
protected int getTimeToLive() throws IOException { |
|
253 |
throw new IOException("Method not implemented!"); |
|
254 |
} |
|
255 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
256 |
@Deprecated |
2 | 257 |
protected void setTTL(byte ttl) throws IOException { |
258 |
throw new IOException("Method not implemented!"); |
|
259 |
} |
|
260 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
261 |
@Deprecated |
2 | 262 |
protected byte getTTL() throws IOException { |
263 |
throw new IOException("Method not implemented!"); |
|
264 |
} |
|
265 |
/* END Multicast specific methods */ |
|
266 |
||
267 |
private int checkAndReturnNativeFD() throws SocketException { |
|
268 |
if (fd == null || !fd.valid()) |
|
269 |
throw new SocketException("Socket closed"); |
|
270 |
||
271 |
return fdAccess.get(fd); |
|
272 |
} |
|
273 |
||
274 |
/* Native methods */ |
|
275 |
||
276 |
private static native void initIDs(); |
|
277 |
||
49892
8bed781a8d9c
8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents:
47216
diff
changeset
|
278 |
private static native int socketCreate(); |
2 | 279 |
|
18192 | 280 |
private static native void socketBind(int fd, InetAddress localAddress, |
281 |
int localport, boolean exclBind) throws SocketException; |
|
2 | 282 |
|
283 |
private static native void socketConnect(int fd, InetAddress address, int port) |
|
284 |
throws SocketException; |
|
285 |
||
286 |
private static native void socketDisconnect(int fd); |
|
287 |
||
288 |
private static native void socketClose(int fd); |
|
289 |
||
290 |
private static native int socketLocalPort(int fd) throws SocketException; |
|
291 |
||
292 |
private static native Object socketLocalAddress(int fd) throws SocketException; |
|
293 |
||
294 |
private static native int socketReceiveOrPeekData(int fd, DatagramPacket packet, |
|
295 |
int timeout, boolean connected, boolean peek) throws IOException; |
|
296 |
||
297 |
private static native void socketSend(int fd, byte[] data, int offset, int length, |
|
298 |
InetAddress address, int port, boolean connected) throws IOException; |
|
299 |
||
300 |
private static native void socketSetIntOption(int fd, int cmd, |
|
301 |
int optionValue) throws SocketException; |
|
302 |
||
303 |
private static native int socketGetIntOption(int fd, int cmd) throws SocketException; |
|
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
25859
diff
changeset
|
304 |
|
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
25859
diff
changeset
|
305 |
native int dataAvailable(); |
2 | 306 |
} |