author | chegar |
Fri, 16 Sep 2011 12:09:04 -0700 | |
changeset 10596 | 39b3a979e600 |
parent 5506 | 202f599c92aa |
child 14856 | 92a1bcf46888 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2007,2011 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; |
|
28 |
import sun.misc.SharedSecrets; |
|
29 |
import sun.misc.JavaIOFileDescriptorAccess; |
|
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 |
||
48 |
protected void datagramSocketCreate() throws SocketException { |
|
49 |
if (fd == null) |
|
50 |
throw new SocketException("Socket closed"); |
|
51 |
||
52 |
int newfd = socketCreate(false /* v6Only */); |
|
53 |
||
54 |
fdAccess.set(fd, newfd); |
|
55 |
} |
|
56 |
||
57 |
protected synchronized void bind0(int lport, InetAddress laddr) |
|
58 |
throws SocketException { |
|
59 |
int nativefd = checkAndReturnNativeFD(); |
|
60 |
||
61 |
if (laddr == null) |
|
62 |
throw new NullPointerException("argument address"); |
|
63 |
||
64 |
socketBind(nativefd, laddr, lport); |
|
65 |
if (lport == 0) { |
|
66 |
localPort = socketLocalPort(nativefd); |
|
67 |
} else { |
|
68 |
localPort = lport; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
protected synchronized int peek(InetAddress address) throws IOException { |
|
73 |
int nativefd = checkAndReturnNativeFD(); |
|
74 |
||
75 |
if (address == null) |
|
76 |
throw new NullPointerException("Null address in peek()"); |
|
77 |
||
78 |
// Use peekData() |
|
79 |
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1); |
|
80 |
int peekPort = peekData(peekPacket); |
|
81 |
address = peekPacket.getAddress(); |
|
82 |
return peekPort; |
|
83 |
} |
|
84 |
||
85 |
protected synchronized int peekData(DatagramPacket p) throws IOException { |
|
86 |
int nativefd = checkAndReturnNativeFD(); |
|
87 |
||
88 |
if (p == null) |
|
89 |
throw new NullPointerException("packet"); |
|
90 |
if (p.getData() == null) |
|
91 |
throw new NullPointerException("packet buffer"); |
|
92 |
||
93 |
return socketReceiveOrPeekData(nativefd, p, timeout, connected, true /*peek*/); |
|
94 |
} |
|
95 |
||
96 |
protected synchronized void receive0(DatagramPacket p) throws IOException { |
|
97 |
int nativefd = checkAndReturnNativeFD(); |
|
98 |
||
99 |
if (p == null) |
|
100 |
throw new NullPointerException("packet"); |
|
101 |
if (p.getData() == null) |
|
102 |
throw new NullPointerException("packet buffer"); |
|
103 |
||
104 |
socketReceiveOrPeekData(nativefd, p, timeout, connected, false /*receive*/); |
|
105 |
} |
|
106 |
||
107 |
protected void send(DatagramPacket p) throws IOException { |
|
108 |
int nativefd = checkAndReturnNativeFD(); |
|
109 |
||
110 |
if (p == null) |
|
111 |
throw new NullPointerException("null packet"); |
|
112 |
||
113 |
if (p.getAddress() == null ||p.getData() ==null) |
|
114 |
throw new NullPointerException("null address || null buffer"); |
|
115 |
||
116 |
socketSend(nativefd, p.getData(), p.getOffset(), p.getLength(), |
|
117 |
p.getAddress(), p.getPort(), connected); |
|
118 |
} |
|
119 |
||
120 |
protected void connect0(InetAddress address, int port) throws SocketException { |
|
121 |
int nativefd = checkAndReturnNativeFD(); |
|
122 |
||
123 |
if (address == null) |
|
124 |
throw new NullPointerException("address"); |
|
125 |
||
126 |
socketConnect(nativefd, address, port); |
|
127 |
} |
|
128 |
||
129 |
protected void disconnect0(int family /*unused*/) { |
|
130 |
if (fd == null || !fd.valid()) |
|
131 |
return; // disconnect doesn't throw any exceptions |
|
132 |
||
133 |
socketDisconnect(fdAccess.get(fd)); |
|
134 |
} |
|
135 |
||
136 |
protected void datagramSocketClose() { |
|
137 |
if (fd == null || !fd.valid()) |
|
138 |
return; // close doesn't throw any exceptions |
|
139 |
||
140 |
socketClose(fdAccess.get(fd)); |
|
141 |
fdAccess.set(fd, -1); |
|
142 |
} |
|
143 |
||
144 |
protected void socketSetOption(int opt, Object val) throws SocketException { |
|
145 |
int nativefd = checkAndReturnNativeFD(); |
|
146 |
||
147 |
int optionValue = 0; |
|
148 |
||
149 |
switch(opt) { |
|
150 |
case IP_TOS : |
|
151 |
case SO_RCVBUF : |
|
152 |
case SO_SNDBUF : |
|
153 |
optionValue = ((Integer)val).intValue(); |
|
154 |
break; |
|
155 |
case SO_REUSEADDR : |
|
156 |
case SO_BROADCAST : |
|
157 |
optionValue = ((Boolean)val).booleanValue() ? 1 : 0; |
|
158 |
break; |
|
159 |
default: /* shouldn't get here */ |
|
160 |
throw new SocketException("Option not supported"); |
|
161 |
} |
|
162 |
||
163 |
socketSetIntOption(nativefd, opt, optionValue); |
|
164 |
} |
|
165 |
||
166 |
protected Object socketGetOption(int opt) throws SocketException { |
|
167 |
int nativefd = checkAndReturnNativeFD(); |
|
168 |
||
169 |
// SO_BINDADDR is not a socket option. |
|
170 |
if (opt == SO_BINDADDR) { |
|
171 |
return socketLocalAddress(nativefd); |
|
172 |
} |
|
173 |
||
174 |
int value = socketGetIntOption(nativefd, opt); |
|
175 |
Object returnValue = null; |
|
176 |
||
177 |
switch (opt) { |
|
178 |
case SO_REUSEADDR : |
|
179 |
case SO_BROADCAST : |
|
180 |
returnValue = (value == 0) ? Boolean.FALSE : Boolean.TRUE; |
|
181 |
break; |
|
182 |
case IP_TOS : |
|
183 |
case SO_RCVBUF : |
|
184 |
case SO_SNDBUF : |
|
185 |
returnValue = new Integer(value); |
|
186 |
break; |
|
187 |
default: /* shouldn't get here */ |
|
188 |
throw new SocketException("Option not supported"); |
|
189 |
} |
|
190 |
||
191 |
return returnValue; |
|
192 |
} |
|
193 |
||
194 |
/* Multicast specific methods. |
|
195 |
* Multicasting on a dual layer TCP/IP stack is always done with |
|
196 |
* TwoStacksPlainDatagramSocketImpl. This is to overcome the lack |
|
197 |
* of behavior defined for multicasting over a dual layer socket by the RFC. |
|
198 |
*/ |
|
199 |
protected void join(InetAddress inetaddr, NetworkInterface netIf) |
|
200 |
throws IOException { |
|
201 |
throw new IOException("Method not implemented!"); |
|
202 |
} |
|
203 |
||
204 |
protected void leave(InetAddress inetaddr, NetworkInterface netIf) |
|
205 |
throws IOException { |
|
206 |
throw new IOException("Method not implemented!"); |
|
207 |
} |
|
208 |
||
209 |
protected void setTimeToLive(int ttl) throws IOException { |
|
210 |
throw new IOException("Method not implemented!"); |
|
211 |
} |
|
212 |
||
213 |
protected int getTimeToLive() throws IOException { |
|
214 |
throw new IOException("Method not implemented!"); |
|
215 |
} |
|
216 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
217 |
@Deprecated |
2 | 218 |
protected void setTTL(byte ttl) throws IOException { |
219 |
throw new IOException("Method not implemented!"); |
|
220 |
} |
|
221 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
222 |
@Deprecated |
2 | 223 |
protected byte getTTL() throws IOException { |
224 |
throw new IOException("Method not implemented!"); |
|
225 |
} |
|
226 |
/* END Multicast specific methods */ |
|
227 |
||
228 |
private int checkAndReturnNativeFD() throws SocketException { |
|
229 |
if (fd == null || !fd.valid()) |
|
230 |
throw new SocketException("Socket closed"); |
|
231 |
||
232 |
return fdAccess.get(fd); |
|
233 |
} |
|
234 |
||
235 |
/* Native methods */ |
|
236 |
||
237 |
private static native void initIDs(); |
|
238 |
||
239 |
private static native int socketCreate(boolean v6Only); |
|
240 |
||
241 |
private static native void socketBind(int fd, InetAddress localAddress, int localport) |
|
242 |
throws SocketException; |
|
243 |
||
244 |
private static native void socketConnect(int fd, InetAddress address, int port) |
|
245 |
throws SocketException; |
|
246 |
||
247 |
private static native void socketDisconnect(int fd); |
|
248 |
||
249 |
private static native void socketClose(int fd); |
|
250 |
||
251 |
private static native int socketLocalPort(int fd) throws SocketException; |
|
252 |
||
253 |
private static native Object socketLocalAddress(int fd) throws SocketException; |
|
254 |
||
255 |
private static native int socketReceiveOrPeekData(int fd, DatagramPacket packet, |
|
256 |
int timeout, boolean connected, boolean peek) throws IOException; |
|
257 |
||
258 |
private static native void socketSend(int fd, byte[] data, int offset, int length, |
|
259 |
InetAddress address, int port, boolean connected) throws IOException; |
|
260 |
||
261 |
private static native void socketSetIntOption(int fd, int cmd, |
|
262 |
int optionValue) throws SocketException; |
|
263 |
||
264 |
private static native int socketGetIntOption(int fd, int cmd) throws SocketException; |
|
265 |
} |