|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any 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.util.Set; |
|
33 import java.util.HashSet; |
|
34 import java.security.AccessController; |
|
35 import sun.security.action.GetPropertyAction; |
|
36 import com.sun.nio.sctp.SctpSocketOption; |
|
37 import static com.sun.nio.sctp.SctpStandardSocketOption.*; |
|
38 |
|
39 public class SctpNet { |
|
40 static final String osName = AccessController.doPrivileged( |
|
41 new GetPropertyAction("os.name")); |
|
42 |
|
43 /* -- Miscellaneous SCTP utilities -- */ |
|
44 |
|
45 static boolean bindxIPv4MappedAddresses() { |
|
46 if ("SunOS".equals(osName)) { |
|
47 /* Solaris supports IPv4Mapped Addresses with bindx */ |
|
48 return true; |
|
49 } /* else { //other OS/implementations */ |
|
50 |
|
51 /* lksctp/linux requires Ipv4 addresses */ |
|
52 return false; |
|
53 } |
|
54 |
|
55 /** |
|
56 * @param oneToone |
|
57 * if {@code true} returns a one-to-one sctp socket, otherwise |
|
58 * returns a one-to-many sctp socket |
|
59 */ |
|
60 static FileDescriptor socket(boolean oneToOne) throws IOException { |
|
61 int nativefd = socket0(oneToOne); |
|
62 return IOUtil.newFD(nativefd); |
|
63 } |
|
64 |
|
65 static void bindx(int fd, InetAddress[] addrs, int port, boolean add) |
|
66 throws IOException { |
|
67 bindx(fd, addrs, port, addrs.length, add, |
|
68 bindxIPv4MappedAddresses()); |
|
69 } |
|
70 |
|
71 static Set<SocketAddress> getLocalAddresses(int fd) |
|
72 throws IOException { |
|
73 HashSet<SocketAddress> set = null; |
|
74 SocketAddress[] saa = getLocalAddresses0(fd); |
|
75 |
|
76 if (saa != null) { |
|
77 set = new HashSet<SocketAddress>(saa.length); |
|
78 for (SocketAddress sa : saa) |
|
79 set.add(sa); |
|
80 } |
|
81 |
|
82 return set; |
|
83 } |
|
84 |
|
85 static Set<SocketAddress> getRemoteAddresses(int fd, int assocId) |
|
86 throws IOException { |
|
87 HashSet<SocketAddress> set = null; |
|
88 SocketAddress[] saa = getRemoteAddresses0(fd, assocId); |
|
89 |
|
90 if (saa != null) { |
|
91 set = new HashSet<SocketAddress>(saa.length); |
|
92 for (SocketAddress sa : saa) |
|
93 set.add(sa); |
|
94 } |
|
95 |
|
96 return set; |
|
97 } |
|
98 |
|
99 static void setSocketOption(int fd, |
|
100 SctpSocketOption name, |
|
101 Object value, |
|
102 int assocId) |
|
103 throws IOException { |
|
104 if (value == null) |
|
105 throw new IllegalArgumentException("Invalid option value"); |
|
106 |
|
107 Class<?> type = name.type(); |
|
108 if (!type.isInstance(value)) |
|
109 throw new IllegalArgumentException("Invalid option value"); |
|
110 |
|
111 if (name.equals(SCTP_INIT_MAXSTREAMS)) { |
|
112 InitMaxStreams maxStreamValue = (InitMaxStreams)value; |
|
113 SctpNet.setInitMsgOption0(fd, |
|
114 maxStreamValue.maxInStreams(), maxStreamValue.maxOutStreams()); |
|
115 } else if (name.equals(SCTP_PRIMARY_ADDR) || |
|
116 name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) { |
|
117 |
|
118 SocketAddress addr = (SocketAddress) value; |
|
119 if (addr == null) |
|
120 throw new IllegalArgumentException("Invalid option value"); |
|
121 |
|
122 Net.checkAddress(addr); |
|
123 InetSocketAddress netAddr = (InetSocketAddress)addr; |
|
124 |
|
125 if (name.equals(SCTP_PRIMARY_ADDR)) { |
|
126 setPrimAddrOption0(fd, assocId, |
|
127 netAddr.getAddress(), netAddr.getPort()); |
|
128 } else { |
|
129 setPeerPrimAddrOption0(fd, assocId, |
|
130 netAddr.getAddress(), netAddr.getPort()); |
|
131 } |
|
132 } else if (name.equals(SCTP_DISABLE_FRAGMENTS) || |
|
133 name.equals(SCTP_EXPLICIT_COMPLETE) || |
|
134 name.equals(SCTP_FRAGMENT_INTERLEAVE) || |
|
135 name.equals(SCTP_NODELAY) || |
|
136 name.equals(SO_SNDBUF) || |
|
137 name.equals(SO_RCVBUF) || |
|
138 name.equals(SO_LINGER)) { |
|
139 setIntOption(fd, name, value); |
|
140 } else { |
|
141 throw new AssertionError("Unknown socket option"); |
|
142 } |
|
143 } |
|
144 |
|
145 static Object getSocketOption(int fd, SctpSocketOption name, int assocId) |
|
146 throws IOException { |
|
147 if (name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) { |
|
148 throw new IllegalArgumentException( |
|
149 "SCTP_SET_PEER_PRIMARY_ADDR cannot be retrieved"); |
|
150 } else if (name.equals(SCTP_INIT_MAXSTREAMS)) { |
|
151 /* container for holding maxIn/Out streams */ |
|
152 int[] values = new int[2]; |
|
153 SctpNet.getInitMsgOption0(fd, values); |
|
154 return InitMaxStreams.create(values[0], values[1]); |
|
155 } else if (name.equals(SCTP_PRIMARY_ADDR)) { |
|
156 return getPrimAddrOption0(fd, assocId); |
|
157 } else if (name.equals(SCTP_DISABLE_FRAGMENTS) || |
|
158 name.equals(SCTP_EXPLICIT_COMPLETE) || |
|
159 name.equals(SCTP_FRAGMENT_INTERLEAVE) || |
|
160 name.equals(SCTP_NODELAY) || |
|
161 name.equals(SO_SNDBUF) || |
|
162 name.equals(SO_RCVBUF) || |
|
163 name.equals(SO_LINGER)) { |
|
164 return getIntOption(fd, name); |
|
165 } else { |
|
166 throw new AssertionError("Unknown socket option"); |
|
167 } |
|
168 } |
|
169 |
|
170 static void setIntOption(int fd, SctpSocketOption name, Object value) |
|
171 throws IOException { |
|
172 if (value == null) |
|
173 throw new IllegalArgumentException("Invalid option value"); |
|
174 |
|
175 Class<?> type = name.type(); |
|
176 if (type != Integer.class && type != Boolean.class) |
|
177 throw new AssertionError("Should not reach here"); |
|
178 |
|
179 if (name == SO_RCVBUF || |
|
180 name == SO_SNDBUF) |
|
181 { |
|
182 int i = ((Integer)value).intValue(); |
|
183 if (i < 0) |
|
184 throw new IllegalArgumentException( |
|
185 "Invalid send/receive buffer size"); |
|
186 } else if (name == SO_LINGER) { |
|
187 int i = ((Integer)value).intValue(); |
|
188 if (i < 0) |
|
189 value = Integer.valueOf(-1); |
|
190 if (i > 65535) |
|
191 value = Integer.valueOf(65535); |
|
192 } else if (name.equals(SCTP_FRAGMENT_INTERLEAVE)) { |
|
193 int i = ((Integer)value).intValue(); |
|
194 if (i < 0 || i > 2) |
|
195 throw new IllegalArgumentException( |
|
196 "Invalid value for SCTP_FRAGMENT_INTERLEAVE"); |
|
197 } |
|
198 |
|
199 int arg; |
|
200 if (type == Integer.class) { |
|
201 arg = ((Integer)value).intValue(); |
|
202 } else { |
|
203 boolean b = ((Boolean)value).booleanValue(); |
|
204 arg = (b) ? 1 : 0; |
|
205 } |
|
206 |
|
207 setIntOption0(fd, ((SctpStdSocketOption)name).constValue(), arg); |
|
208 } |
|
209 |
|
210 static Object getIntOption(int fd, SctpSocketOption name) |
|
211 throws IOException { |
|
212 Class<?> type = name.type(); |
|
213 |
|
214 if (type != Integer.class && type != Boolean.class) |
|
215 throw new AssertionError("Should not reach here"); |
|
216 |
|
217 if (!(name instanceof SctpStdSocketOption)) |
|
218 throw new AssertionError("Should not reach here"); |
|
219 |
|
220 int value = getIntOption0(fd, |
|
221 ((SctpStdSocketOption)name).constValue()); |
|
222 |
|
223 if (type == Integer.class) { |
|
224 return Integer.valueOf(value); |
|
225 } else { |
|
226 return (value == 0) ? Boolean.FALSE : Boolean.TRUE; |
|
227 } |
|
228 } |
|
229 |
|
230 static void shutdown(int fd, int assocId) |
|
231 throws IOException { |
|
232 shutdown0(fd, assocId); |
|
233 } |
|
234 |
|
235 /* Native Methods */ |
|
236 static native int socket0(boolean oneToOne) throws IOException; |
|
237 |
|
238 static native void bindx(int fd, InetAddress[] addrs, int port, int length, |
|
239 boolean add, boolean preferIPv6) throws IOException; |
|
240 |
|
241 static native int getIntOption0(int fd, int opt) throws IOException; |
|
242 |
|
243 static native void setIntOption0(int fd, int opt, int arg) |
|
244 throws IOException; |
|
245 |
|
246 static native SocketAddress[] getLocalAddresses0(int fd) throws IOException; |
|
247 |
|
248 static native SocketAddress[] getRemoteAddresses0(int fd, int assocId) |
|
249 throws IOException; |
|
250 |
|
251 static native void setPrimAddrOption0(int fd, int assocId, InetAddress ia, |
|
252 int port) throws IOException; |
|
253 |
|
254 static native void setPeerPrimAddrOption0(int fd, int assocId, |
|
255 InetAddress ia, int port) throws IOException; |
|
256 |
|
257 static native SocketAddress getPrimAddrOption0(int fd, int assocId) |
|
258 throws IOException; |
|
259 |
|
260 /* retVals [0] maxInStreams, [1] maxOutStreams */ |
|
261 static native void getInitMsgOption0(int fd, int[] retVals) throws IOException; |
|
262 |
|
263 static native void setInitMsgOption0(int fd, int arg1, int arg2) |
|
264 throws IOException; |
|
265 |
|
266 static native void shutdown0(int fd, int assocId); |
|
267 } |
|
268 |