1 /* |
|
2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
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 * |
|
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. |
|
24 */ |
|
25 package sun.nio.ch; |
|
26 |
|
27 import java.io.FileDescriptor; |
|
28 import java.io.IOException; |
|
29 import java.net.InetAddress; |
|
30 import java.net.InetSocketAddress; |
|
31 import java.net.SocketAddress; |
|
32 import java.nio.channels.AlreadyBoundException; |
|
33 import java.util.Set; |
|
34 import java.util.HashSet; |
|
35 import java.security.AccessController; |
|
36 import sun.security.action.GetPropertyAction; |
|
37 import com.sun.nio.sctp.SctpSocketOption; |
|
38 import static com.sun.nio.sctp.SctpStandardSocketOptions.*; |
|
39 |
|
40 public class SctpNet { |
|
41 static final String osName = AccessController.doPrivileged( |
|
42 new GetPropertyAction("os.name")); |
|
43 |
|
44 /* -- Miscellaneous SCTP utilities -- */ |
|
45 |
|
46 private static boolean IPv4MappedAddresses() { |
|
47 if ("SunOS".equals(osName)) { |
|
48 /* Solaris supports IPv4Mapped Addresses with bindx */ |
|
49 return true; |
|
50 } /* else { //other OS/implementations */ |
|
51 |
|
52 /* lksctp/linux requires Ipv4 addresses */ |
|
53 return false; |
|
54 } |
|
55 |
|
56 static boolean throwAlreadyBoundException() throws IOException { |
|
57 throw new AlreadyBoundException(); |
|
58 } |
|
59 |
|
60 static void listen(int fd, int backlog) throws IOException { |
|
61 listen0(fd, backlog); |
|
62 } |
|
63 |
|
64 static int connect(int fd, InetAddress remote, int remotePort) |
|
65 throws IOException { |
|
66 return connect0(fd, remote, remotePort); |
|
67 } |
|
68 |
|
69 static void close(int fd) throws IOException { |
|
70 close0(fd); |
|
71 } |
|
72 |
|
73 static void preClose(int fd) throws IOException { |
|
74 preClose0(fd); |
|
75 } |
|
76 |
|
77 /** |
|
78 * @param oneToOne |
|
79 * if {@code true} returns a one-to-one sctp socket, otherwise |
|
80 * returns a one-to-many sctp socket |
|
81 */ |
|
82 static FileDescriptor socket(boolean oneToOne) throws IOException { |
|
83 int nativefd = socket0(oneToOne); |
|
84 return IOUtil.newFD(nativefd); |
|
85 } |
|
86 |
|
87 static void bindx(int fd, InetAddress[] addrs, int port, boolean add) |
|
88 throws IOException { |
|
89 bindx(fd, addrs, port, addrs.length, add, |
|
90 IPv4MappedAddresses()); |
|
91 } |
|
92 |
|
93 static Set<SocketAddress> getLocalAddresses(int fd) |
|
94 throws IOException { |
|
95 HashSet<SocketAddress> set = null; |
|
96 SocketAddress[] saa = getLocalAddresses0(fd); |
|
97 |
|
98 if (saa != null) { |
|
99 set = new HashSet<SocketAddress>(saa.length); |
|
100 for (SocketAddress sa : saa) |
|
101 set.add(sa); |
|
102 } |
|
103 |
|
104 return set; |
|
105 } |
|
106 |
|
107 static Set<SocketAddress> getRemoteAddresses(int fd, int assocId) |
|
108 throws IOException { |
|
109 HashSet<SocketAddress> set = null; |
|
110 SocketAddress[] saa = getRemoteAddresses0(fd, assocId); |
|
111 |
|
112 if (saa != null) { |
|
113 set = new HashSet<SocketAddress>(saa.length); |
|
114 for (SocketAddress sa : saa) |
|
115 set.add(sa); |
|
116 } |
|
117 |
|
118 return set; |
|
119 } |
|
120 |
|
121 static <T> void setSocketOption(int fd, |
|
122 SctpSocketOption<T> name, |
|
123 T value, |
|
124 int assocId) |
|
125 throws IOException { |
|
126 if (value == null) |
|
127 throw new IllegalArgumentException("Invalid option value"); |
|
128 |
|
129 if (name.equals(SCTP_INIT_MAXSTREAMS)) { |
|
130 InitMaxStreams maxStreamValue = (InitMaxStreams)value; |
|
131 SctpNet.setInitMsgOption0(fd, |
|
132 maxStreamValue.maxInStreams(), maxStreamValue.maxOutStreams()); |
|
133 } else if (name.equals(SCTP_PRIMARY_ADDR) || |
|
134 name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) { |
|
135 |
|
136 SocketAddress addr = (SocketAddress) value; |
|
137 if (addr == null) |
|
138 throw new IllegalArgumentException("Invalid option value"); |
|
139 |
|
140 Net.checkAddress(addr); |
|
141 InetSocketAddress netAddr = (InetSocketAddress)addr; |
|
142 |
|
143 if (name.equals(SCTP_PRIMARY_ADDR)) { |
|
144 setPrimAddrOption0(fd, |
|
145 assocId, |
|
146 netAddr.getAddress(), |
|
147 netAddr.getPort()); |
|
148 } else { |
|
149 setPeerPrimAddrOption0(fd, |
|
150 assocId, |
|
151 netAddr.getAddress(), |
|
152 netAddr.getPort(), |
|
153 IPv4MappedAddresses()); |
|
154 } |
|
155 } else if (name.equals(SCTP_DISABLE_FRAGMENTS) || |
|
156 name.equals(SCTP_EXPLICIT_COMPLETE) || |
|
157 name.equals(SCTP_FRAGMENT_INTERLEAVE) || |
|
158 name.equals(SCTP_NODELAY) || |
|
159 name.equals(SO_SNDBUF) || |
|
160 name.equals(SO_RCVBUF) || |
|
161 name.equals(SO_LINGER)) { |
|
162 setIntOption(fd, name, value); |
|
163 } else { |
|
164 throw new AssertionError("Unknown socket option"); |
|
165 } |
|
166 } |
|
167 |
|
168 static Object getSocketOption(int fd, SctpSocketOption<?> name, int assocId) |
|
169 throws IOException { |
|
170 if (name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) { |
|
171 throw new IllegalArgumentException( |
|
172 "SCTP_SET_PEER_PRIMARY_ADDR cannot be retrieved"); |
|
173 } else if (name.equals(SCTP_INIT_MAXSTREAMS)) { |
|
174 /* container for holding maxIn/Out streams */ |
|
175 int[] values = new int[2]; |
|
176 SctpNet.getInitMsgOption0(fd, values); |
|
177 return InitMaxStreams.create(values[0], values[1]); |
|
178 } else if (name.equals(SCTP_PRIMARY_ADDR)) { |
|
179 return getPrimAddrOption0(fd, assocId); |
|
180 } else if (name.equals(SCTP_DISABLE_FRAGMENTS) || |
|
181 name.equals(SCTP_EXPLICIT_COMPLETE) || |
|
182 name.equals(SCTP_FRAGMENT_INTERLEAVE) || |
|
183 name.equals(SCTP_NODELAY) || |
|
184 name.equals(SO_SNDBUF) || |
|
185 name.equals(SO_RCVBUF) || |
|
186 name.equals(SO_LINGER)) { |
|
187 return getIntOption(fd, name); |
|
188 } else { |
|
189 throw new AssertionError("Unknown socket option"); |
|
190 } |
|
191 } |
|
192 |
|
193 static void setIntOption(int fd, SctpSocketOption<?> name, Object value) |
|
194 throws IOException { |
|
195 if (value == null) |
|
196 throw new IllegalArgumentException("Invalid option value"); |
|
197 |
|
198 Class<?> type = name.type(); |
|
199 if (type != Integer.class && type != Boolean.class) |
|
200 throw new AssertionError("Should not reach here"); |
|
201 |
|
202 if (name == SO_RCVBUF || |
|
203 name == SO_SNDBUF) |
|
204 { |
|
205 int i = ((Integer)value).intValue(); |
|
206 if (i < 0) |
|
207 throw new IllegalArgumentException( |
|
208 "Invalid send/receive buffer size"); |
|
209 } else if (name == SO_LINGER) { |
|
210 int i = ((Integer)value).intValue(); |
|
211 if (i < 0) |
|
212 value = Integer.valueOf(-1); |
|
213 if (i > 65535) |
|
214 value = Integer.valueOf(65535); |
|
215 } else if (name.equals(SCTP_FRAGMENT_INTERLEAVE)) { |
|
216 int i = ((Integer)value).intValue(); |
|
217 if (i < 0 || i > 2) |
|
218 throw new IllegalArgumentException( |
|
219 "Invalid value for SCTP_FRAGMENT_INTERLEAVE"); |
|
220 } |
|
221 |
|
222 int arg; |
|
223 if (type == Integer.class) { |
|
224 arg = ((Integer)value).intValue(); |
|
225 } else { |
|
226 boolean b = ((Boolean)value).booleanValue(); |
|
227 arg = (b) ? 1 : 0; |
|
228 } |
|
229 |
|
230 setIntOption0(fd, ((SctpStdSocketOption)name).constValue(), arg); |
|
231 } |
|
232 |
|
233 static Object getIntOption(int fd, SctpSocketOption<?> name) |
|
234 throws IOException { |
|
235 Class<?> type = name.type(); |
|
236 |
|
237 if (type != Integer.class && type != Boolean.class) |
|
238 throw new AssertionError("Should not reach here"); |
|
239 |
|
240 if (!(name instanceof SctpStdSocketOption)) |
|
241 throw new AssertionError("Should not reach here"); |
|
242 |
|
243 int value = getIntOption0(fd, |
|
244 ((SctpStdSocketOption)name).constValue()); |
|
245 |
|
246 if (type == Integer.class) { |
|
247 return Integer.valueOf(value); |
|
248 } else { |
|
249 return (value == 0) ? Boolean.FALSE : Boolean.TRUE; |
|
250 } |
|
251 } |
|
252 |
|
253 static void shutdown(int fd, int assocId) |
|
254 throws IOException { |
|
255 shutdown0(fd, assocId); |
|
256 } |
|
257 |
|
258 static FileDescriptor branch(int fd, int assocId) throws IOException { |
|
259 int nativefd = branch0(fd, assocId); |
|
260 return IOUtil.newFD(nativefd); |
|
261 } |
|
262 |
|
263 /* Native Methods */ |
|
264 static native int socket0(boolean oneToOne) throws IOException; |
|
265 |
|
266 static native void listen0(int fd, int backlog) throws IOException; |
|
267 |
|
268 static native int connect0(int fd, InetAddress remote, int remotePort) |
|
269 throws IOException; |
|
270 |
|
271 static native void close0(int fd) throws IOException; |
|
272 |
|
273 static native void preClose0(int fd) throws IOException; |
|
274 |
|
275 static native void bindx(int fd, InetAddress[] addrs, int port, int length, |
|
276 boolean add, boolean preferIPv6) throws IOException; |
|
277 |
|
278 static native int getIntOption0(int fd, int opt) throws IOException; |
|
279 |
|
280 static native void setIntOption0(int fd, int opt, int arg) |
|
281 throws IOException; |
|
282 |
|
283 static native SocketAddress[] getLocalAddresses0(int fd) throws IOException; |
|
284 |
|
285 static native SocketAddress[] getRemoteAddresses0(int fd, int assocId) |
|
286 throws IOException; |
|
287 |
|
288 static native int branch0(int fd, int assocId) throws IOException; |
|
289 |
|
290 static native void setPrimAddrOption0(int fd, int assocId, InetAddress ia, |
|
291 int port) throws IOException; |
|
292 |
|
293 static native void setPeerPrimAddrOption0(int fd, int assocId, |
|
294 InetAddress ia, int port, boolean preferIPv6) throws IOException; |
|
295 |
|
296 static native SocketAddress getPrimAddrOption0(int fd, int assocId) |
|
297 throws IOException; |
|
298 |
|
299 /* retVals [0] maxInStreams, [1] maxOutStreams */ |
|
300 static native void getInitMsgOption0(int fd, int[] retVals) throws IOException; |
|
301 |
|
302 static native void setInitMsgOption0(int fd, int arg1, int arg2) |
|
303 throws IOException; |
|
304 |
|
305 static native void shutdown0(int fd, int assocId); |
|
306 |
|
307 static native void init(); |
|
308 |
|
309 static { |
|
310 init(); |
|
311 } |
|
312 } |
|
313 |
|