author | chegar |
Mon, 29 Jun 2009 14:53:10 +0100 | |
changeset 3072 | a801b122142f |
parent 2542 | d859108aea12 |
child 4669 | 11d1dbd3598d |
permissions | -rw-r--r-- |
2542 | 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 |
||
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include <dlfcn.h> |
|
29 |
||
30 |
#include "Sctp.h" |
|
31 |
#include "jni.h" |
|
32 |
#include "jni_util.h" |
|
33 |
#include "nio_util.h" |
|
34 |
#include "nio.h" |
|
35 |
#include "net_util.h" |
|
36 |
#include "net_util_md.h" |
|
37 |
#include "sun_nio_ch_SctpNet.h" |
|
38 |
#include "sun_nio_ch_SctpStdSocketOption.h" |
|
39 |
||
40 |
static jclass isaCls = 0; |
|
41 |
static jmethodID isaCtrID = 0; |
|
42 |
||
43 |
static const char* nativeSctpLib = "libsctp.so.1"; |
|
44 |
static jboolean funcsLoaded = JNI_FALSE; |
|
45 |
||
46 |
JNIEXPORT jint JNICALL JNI_OnLoad |
|
47 |
(JavaVM *vm, void *reserved) { |
|
48 |
return JNI_VERSION_1_2; |
|
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Loads the native sctp library that contains the socket extension |
|
53 |
* functions, as well as locating the individual functions. |
|
54 |
* There will be a pending exception if this method returns false. |
|
55 |
*/ |
|
56 |
jboolean loadSocketExtensionFuncs |
|
57 |
(JNIEnv* env) { |
|
58 |
if (dlopen(nativeSctpLib, RTLD_GLOBAL | RTLD_LAZY) == NULL) { |
|
59 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
60 |
dlerror()); |
|
61 |
return JNI_FALSE; |
|
62 |
} |
|
63 |
||
64 |
if ((nio_sctp_getladdrs = (sctp_getladdrs_func*) |
|
65 |
dlsym(RTLD_DEFAULT, "sctp_getladdrs")) == NULL) { |
|
66 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
67 |
dlerror()); |
|
68 |
return JNI_FALSE; |
|
69 |
} |
|
70 |
||
71 |
if ((nio_sctp_freeladdrs = (sctp_freeladdrs_func*) |
|
72 |
dlsym(RTLD_DEFAULT, "sctp_freeladdrs")) == NULL) { |
|
73 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
74 |
dlerror()); |
|
75 |
return JNI_FALSE; |
|
76 |
} |
|
77 |
||
78 |
if ((nio_sctp_getpaddrs = (sctp_getpaddrs_func*) |
|
79 |
dlsym(RTLD_DEFAULT, "sctp_getpaddrs")) == NULL) { |
|
80 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
81 |
dlerror()); |
|
82 |
return JNI_FALSE; |
|
83 |
} |
|
84 |
||
85 |
if ((nio_sctp_freepaddrs = (sctp_freepaddrs_func*) |
|
86 |
dlsym(RTLD_DEFAULT, "sctp_freepaddrs")) == NULL) { |
|
87 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
88 |
dlerror()); |
|
89 |
return JNI_FALSE; |
|
90 |
} |
|
91 |
||
92 |
if ((nio_sctp_bindx = (sctp_bindx_func*) |
|
93 |
dlsym(RTLD_DEFAULT, "sctp_bindx")) == NULL) { |
|
94 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
|
95 |
dlerror()); |
|
96 |
return JNI_FALSE; |
|
97 |
} |
|
98 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
99 |
if ((nio_sctp_peeloff = (sctp_peeloff_func*) |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
100 |
dlsym(RTLD_DEFAULT, "sctp_peeloff")) == NULL) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
101 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
102 |
dlerror()); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
103 |
return JNI_FALSE; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
104 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
105 |
|
2542 | 106 |
funcsLoaded = JNI_TRUE; |
107 |
return JNI_TRUE; |
|
108 |
} |
|
109 |
||
110 |
/* |
|
111 |
* Class: sun_nio_ch_SctpNet |
|
112 |
* Method: socket0 |
|
113 |
* Signature: (Z)I |
|
114 |
*/ |
|
115 |
JNIEXPORT jint JNICALL Java_sun_nio_ch_SctpNet_socket0 |
|
116 |
(JNIEnv *env, jclass klass, jboolean oneToOne) { |
|
117 |
int fd; |
|
118 |
struct sctp_event_subscribe event; |
|
119 |
||
120 |
/* Try to load the socket API extension functions */ |
|
121 |
if (!funcsLoaded && !loadSocketExtensionFuncs(env)) { |
|
122 |
return 0; |
|
123 |
} |
|
124 |
||
125 |
fd = socket(ipv6_available() ? AF_INET6 : AF_INET, |
|
126 |
(oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP); |
|
127 |
||
128 |
if (fd < 0) { |
|
129 |
return handleSocketError(env, errno); |
|
130 |
} |
|
131 |
||
132 |
/* Enable events */ |
|
133 |
memset(&event, 0, sizeof(event)); |
|
134 |
event.sctp_data_io_event = 1; |
|
135 |
event.sctp_association_event = 1; |
|
136 |
event.sctp_address_event = 1; |
|
137 |
event.sctp_send_failure_event = 1; |
|
138 |
//event.sctp_peer_error_event = 1; |
|
139 |
event.sctp_shutdown_event = 1; |
|
140 |
//event.sctp_partial_delivery_event = 1; |
|
141 |
//event.sctp_adaptation_layer_event = 1; |
|
142 |
if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(event)) != 0) { |
|
143 |
handleSocketError(env, errno); |
|
144 |
} |
|
145 |
return fd; |
|
146 |
} |
|
147 |
||
148 |
/* |
|
149 |
* Class: sun_nio_ch_SctpNet |
|
150 |
* Method: bindx |
|
151 |
* Signature: (I[Ljava/net/InetAddress;IIZ)V |
|
152 |
*/ |
|
153 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_bindx |
|
154 |
(JNIEnv *env, jclass klass, jint fd, jobjectArray addrs, jint port, |
|
155 |
jint addrsLength, jboolean add, jboolean preferIPv6) { |
|
156 |
SOCKADDR *sap, *tmpSap; |
|
157 |
int i, sa_len = sizeof(SOCKADDR); |
|
158 |
jobject ia; |
|
159 |
||
160 |
if (addrsLength < 1) |
|
161 |
return; |
|
162 |
||
163 |
if ((sap = calloc(addrsLength, sa_len)) == NULL) { |
|
164 |
JNU_ThrowOutOfMemoryError(env, "heap allocation failure"); |
|
165 |
return; |
|
166 |
} |
|
167 |
||
168 |
tmpSap = sap; |
|
169 |
for (i=0; i<addrsLength; i++) { |
|
170 |
ia = (*env)->GetObjectArrayElement(env, addrs, i); |
|
171 |
if (NET_InetAddressToSockaddr(env, ia, port, (struct sockaddr*)tmpSap, |
|
172 |
&sa_len, preferIPv6) != 0) { |
|
173 |
free(sap); |
|
174 |
return; |
|
175 |
} |
|
176 |
tmpSap++; |
|
177 |
} |
|
178 |
||
179 |
if (nio_sctp_bindx(fd, (void*)sap, addrsLength, add ? SCTP_BINDX_ADD_ADDR : |
|
180 |
SCTP_BINDX_REM_ADDR) != 0) { |
|
181 |
handleSocketError(env, errno); |
|
182 |
} |
|
183 |
||
184 |
free(sap); |
|
185 |
} |
|
186 |
||
187 |
void initializeISA |
|
188 |
(JNIEnv* env) { |
|
189 |
if (isaCls == 0) { |
|
190 |
jclass c = (*env)->FindClass(env, "java/net/InetSocketAddress"); |
|
191 |
CHECK_NULL(c); |
|
192 |
isaCls = (*env)->NewGlobalRef(env, c); |
|
193 |
CHECK_NULL(isaCls); |
|
194 |
(*env)->DeleteLocalRef(env, c); |
|
195 |
isaCtrID = (*env)->GetMethodID(env, isaCls, "<init>", |
|
196 |
"(Ljava/net/InetAddress;I)V"); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
jobject SockAddrToInetSocketAddress |
|
201 |
(JNIEnv *env, struct sockaddr* sap) { |
|
202 |
int port = 0; |
|
203 |
||
204 |
jobject ia = NET_SockaddrToInetAddress(env, sap, &port); |
|
205 |
if (ia == NULL) |
|
206 |
return NULL; |
|
207 |
||
208 |
if (isaCls == 0) { |
|
209 |
initializeISA(env); |
|
210 |
CHECK_NULL_RETURN(isaCls, NULL); |
|
211 |
} |
|
212 |
||
213 |
return (*env)->NewObject(env, isaCls, isaCtrID, ia, port); |
|
214 |
} |
|
215 |
||
216 |
/* |
|
217 |
* Class: sun_nio_ch_SctpNet |
|
218 |
* Method: getLocalAddresses0 |
|
219 |
* Signature: (I)[Ljava/net/SocketAddress; |
|
220 |
*/ |
|
221 |
JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_SctpNet_getLocalAddresses0 |
|
222 |
(JNIEnv *env, jclass klass, jint fd) { |
|
223 |
void *addr_buf, *laddr; |
|
224 |
struct sockaddr* sap; |
|
225 |
int i, addrCount; |
|
226 |
jobjectArray isaa; |
|
227 |
||
228 |
#ifdef __solaris__ |
|
229 |
if ((addrCount = nio_sctp_getladdrs(fd, 0, (void **)&addr_buf)) == -1) { |
|
230 |
#else /* __linux__ */ |
|
231 |
if ((addrCount = nio_sctp_getladdrs(fd, 0, (struct sockaddr **)&addr_buf)) == -1) { |
|
232 |
#endif |
|
233 |
handleSocketError(env, errno); |
|
234 |
return NULL; |
|
235 |
} |
|
236 |
||
237 |
if (addrCount < 1) |
|
238 |
return NULL; |
|
239 |
||
240 |
if (isaCls == 0) { |
|
241 |
initializeISA(env); |
|
242 |
CHECK_NULL_RETURN(isaCls, NULL); |
|
243 |
} |
|
244 |
||
245 |
isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL); |
|
246 |
if (isaa == NULL) { |
|
247 |
nio_sctp_freeladdrs(addr_buf); |
|
248 |
return NULL; |
|
249 |
} |
|
250 |
||
251 |
laddr = addr_buf; |
|
252 |
for (i=0; i<addrCount; i++) { |
|
253 |
int port = 0; |
|
254 |
jobject isa = NULL, ia; |
|
255 |
sap = (struct sockaddr*)addr_buf; |
|
256 |
ia = NET_SockaddrToInetAddress(env, sap, &port); |
|
257 |
if (ia != NULL) |
|
258 |
isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port); |
|
259 |
if (isa != NULL) |
|
260 |
(*env)->SetObjectArrayElement(env, isaa, i, isa); |
|
261 |
||
262 |
if (sap->sa_family == AF_INET) |
|
263 |
addr_buf = ((struct sockaddr_in*)addr_buf) + 1; |
|
264 |
else |
|
265 |
addr_buf = ((struct sockaddr_in6*)addr_buf) + 1; |
|
266 |
} |
|
267 |
||
268 |
nio_sctp_freeladdrs(laddr); |
|
269 |
return isaa; |
|
270 |
} |
|
271 |
||
272 |
jobjectArray getRemoteAddresses |
|
273 |
(JNIEnv *env, jint fd, sctp_assoc_t id) { |
|
274 |
void *addr_buf, *paddr; |
|
275 |
struct sockaddr* sap; |
|
276 |
int i, addrCount; |
|
277 |
jobjectArray isaa; |
|
278 |
||
279 |
#if __solaris__ |
|
280 |
if ((addrCount = nio_sctp_getpaddrs(fd, id, (void **)&addr_buf)) == -1) { |
|
281 |
#else /* __linux__ */ |
|
282 |
if ((addrCount = nio_sctp_getpaddrs(fd, id, (struct sockaddr**)&addr_buf)) == -1) { |
|
283 |
#endif |
|
284 |
handleSocketError(env, errno); |
|
285 |
return NULL; |
|
286 |
} |
|
287 |
||
288 |
if (addrCount < 1) |
|
289 |
return NULL; |
|
290 |
||
291 |
if (isaCls == 0) { |
|
292 |
initializeISA(env); |
|
293 |
CHECK_NULL_RETURN(isaCls, NULL); |
|
294 |
} |
|
295 |
||
296 |
isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL); |
|
297 |
if (isaa == NULL) { |
|
298 |
nio_sctp_freepaddrs(addr_buf); |
|
299 |
return NULL; |
|
300 |
} |
|
301 |
||
302 |
paddr = addr_buf; |
|
303 |
for (i=0; i<addrCount; i++) { |
|
304 |
jobject ia, isa = NULL; |
|
305 |
int port; |
|
306 |
sap = (struct sockaddr*)addr_buf; |
|
307 |
ia = NET_SockaddrToInetAddress(env, sap, &port); |
|
308 |
if (ia != NULL) |
|
309 |
isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port); |
|
310 |
if (isa != NULL) |
|
311 |
(*env)->SetObjectArrayElement(env, isaa, i, isa); |
|
312 |
||
313 |
if (sap->sa_family == AF_INET) |
|
314 |
addr_buf = ((struct sockaddr_in*)addr_buf) + 1; |
|
315 |
else |
|
316 |
addr_buf = ((struct sockaddr_in6*)addr_buf) + 1; |
|
317 |
} |
|
318 |
||
319 |
nio_sctp_freepaddrs(paddr); |
|
320 |
||
321 |
return isaa; |
|
322 |
} |
|
323 |
||
324 |
/* |
|
325 |
* Class: sun_nio_ch_SctpNet |
|
326 |
* Method: getRemoteAddresses0 |
|
327 |
* Signature: (II)[Ljava/net/SocketAddress; |
|
328 |
*/ |
|
329 |
JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_SctpNet_getRemoteAddresses0 |
|
330 |
(JNIEnv *env, jclass klass, jint fd, jint assocId) { |
|
331 |
return getRemoteAddresses(env, fd, assocId); |
|
332 |
} |
|
333 |
||
334 |
/* Map the Java level option to the native level */ |
|
335 |
int mapSocketOption |
|
336 |
(jint cmd, int *level, int *optname) { |
|
337 |
static struct { |
|
338 |
jint cmd; |
|
339 |
int level; |
|
340 |
int optname; |
|
341 |
} const opts[] = { |
|
342 |
{ sun_nio_ch_SctpStdSocketOption_SCTP_DISABLE_FRAGMENTS, IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS }, |
|
343 |
{ sun_nio_ch_SctpStdSocketOption_SCTP_EXPLICIT_COMPLETE, IPPROTO_SCTP, SCTP_EXPLICIT_EOR }, |
|
344 |
{ sun_nio_ch_SctpStdSocketOption_SCTP_FRAGMENT_INTERLEAVE, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE }, |
|
345 |
{ sun_nio_ch_SctpStdSocketOption_SCTP_NODELAY, IPPROTO_SCTP, SCTP_NODELAY }, |
|
346 |
{ sun_nio_ch_SctpStdSocketOption_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF }, |
|
347 |
{ sun_nio_ch_SctpStdSocketOption_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF }, |
|
348 |
{ sun_nio_ch_SctpStdSocketOption_SO_LINGER, SOL_SOCKET, SO_LINGER } }; |
|
349 |
||
350 |
int i; |
|
351 |
for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) { |
|
352 |
if (cmd == opts[i].cmd) { |
|
353 |
*level = opts[i].level; |
|
354 |
*optname = opts[i].optname; |
|
355 |
return 0; |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
/* not found */ |
|
360 |
return -1; |
|
361 |
} |
|
362 |
||
363 |
/* |
|
364 |
* Class: sun_nio_ch_SctpNet |
|
365 |
* Method: setIntOption0 |
|
366 |
* Signature: (III)V |
|
367 |
*/ |
|
368 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_setIntOption0 |
|
369 |
(JNIEnv *env, jclass klass, jint fd, jint opt, int arg) { |
|
370 |
int klevel, kopt; |
|
371 |
int result; |
|
372 |
struct linger linger; |
|
373 |
void *parg; |
|
374 |
int arglen; |
|
375 |
||
376 |
if (mapSocketOption(opt, &klevel, &kopt) < 0) { |
|
377 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
378 |
"Unsupported socket option"); |
|
379 |
return; |
|
380 |
} |
|
381 |
||
382 |
if (opt == sun_nio_ch_SctpStdSocketOption_SO_LINGER) { |
|
383 |
parg = (void *)&linger; |
|
384 |
arglen = sizeof(linger); |
|
385 |
if (arg >= 0) { |
|
386 |
linger.l_onoff = 1; |
|
387 |
linger.l_linger = arg; |
|
388 |
} else { |
|
389 |
linger.l_onoff = 0; |
|
390 |
linger.l_linger = 0; |
|
391 |
} |
|
392 |
} else { |
|
393 |
parg = (void *)&arg; |
|
394 |
arglen = sizeof(arg); |
|
395 |
} |
|
396 |
||
397 |
if (setsockopt(fd, klevel, kopt, parg, arglen) < 0) { |
|
398 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
399 |
"sun_nio_ch_SctpNet.setIntOption0"); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
/* |
|
404 |
* Class: sun_nio_ch_SctpNet |
|
405 |
* Method: getIntOption0 |
|
406 |
* Signature: (II)I |
|
407 |
*/ |
|
408 |
JNIEXPORT int JNICALL Java_sun_nio_ch_SctpNet_getIntOption0 |
|
409 |
(JNIEnv *env, jclass klass, jint fd, jint opt) { |
|
410 |
int klevel, kopt; |
|
411 |
int result; |
|
412 |
struct linger linger; |
|
413 |
void *arg; |
|
414 |
unsigned int arglen; |
|
415 |
||
416 |
if (mapSocketOption(opt, &klevel, &kopt) < 0) { |
|
417 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
418 |
"Unsupported socket option"); |
|
419 |
return -1; |
|
420 |
} |
|
421 |
||
422 |
if (opt == sun_nio_ch_SctpStdSocketOption_SO_LINGER) { |
|
423 |
arg = (void *)&linger; |
|
424 |
arglen = sizeof(linger); |
|
425 |
} else { |
|
426 |
arg = (void *)&result; |
|
427 |
arglen = sizeof(result); |
|
428 |
} |
|
429 |
||
430 |
if (getsockopt(fd, klevel, kopt, arg, &arglen) < 0) { |
|
431 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
432 |
"sun.nio.ch.Net.getIntOption"); |
|
433 |
return -1; |
|
434 |
} |
|
435 |
||
436 |
if (opt == sun_nio_ch_SctpStdSocketOption_SO_LINGER) |
|
437 |
return linger.l_onoff ? linger.l_linger : -1; |
|
438 |
else |
|
439 |
return result; |
|
440 |
} |
|
441 |
||
442 |
/* |
|
443 |
* Class: sun_nio_ch_SctpNet |
|
444 |
* Method: getPrimAddrOption0 |
|
445 |
* Signature: (II)Ljava/net/SocketAddress; |
|
446 |
*/ |
|
447 |
JNIEXPORT jobject JNICALL Java_sun_nio_ch_SctpNet_getPrimAddrOption0 |
|
448 |
(JNIEnv *env, jclass klass, jint fd, jint assocId) { |
|
449 |
struct sctp_setprim prim; |
|
450 |
unsigned int prim_len = sizeof(prim); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
451 |
struct sockaddr* sap = (struct sockaddr*)&prim.ssp_addr; |
2542 | 452 |
|
453 |
prim.ssp_assoc_id = assocId; |
|
454 |
||
455 |
if (getsockopt(fd, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &prim, &prim_len) < 0) { |
|
456 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
457 |
"sun.nio.ch.SctpNet.getPrimAddrOption0"); |
|
458 |
return NULL; |
|
459 |
} |
|
460 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
461 |
return SockAddrToInetSocketAddress(env, sap); |
2542 | 462 |
} |
463 |
||
464 |
/* |
|
465 |
* Class: sun_nio_ch_SctpNet |
|
466 |
* Method: setPrimAddrOption0 |
|
467 |
* Signature: (IILjava/net/InetAddress;I)V |
|
468 |
*/ |
|
469 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_setPrimAddrOption0 |
|
470 |
(JNIEnv *env, jclass klass, jint fd, jint assocId, jobject iaObj, jint port) { |
|
471 |
struct sctp_setprim prim; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
472 |
struct sockaddr* sap = (struct sockaddr*)&prim.ssp_addr; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
473 |
int sap_len; |
2542 | 474 |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
475 |
if (NET_InetAddressToSockaddr(env, iaObj, port, sap, |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
476 |
&sap_len, JNI_TRUE) != 0) { |
2542 | 477 |
return; |
478 |
} |
|
479 |
||
480 |
prim.ssp_assoc_id = assocId; |
|
481 |
||
482 |
if (setsockopt(fd, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &prim, sizeof(prim)) < 0) { |
|
483 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
484 |
"sun.nio.ch.SctpNet.setPrimAddrOption0"); |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
/* |
|
489 |
* Class: sun_nio_ch_SctpNet |
|
490 |
* Method: setPeerPrimAddrOption0 |
|
491 |
* Signature: (IILjava/net/InetAddress;I)V |
|
492 |
*/ |
|
493 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_setPeerPrimAddrOption0 |
|
494 |
(JNIEnv *env, jclass klass, jint fd, jint assocId, jobject iaObj, jint port) { |
|
495 |
struct sctp_setpeerprim prim; |
|
496 |
struct sockaddr_storage ss; |
|
497 |
int ss_len = sizeof(ss); |
|
498 |
||
499 |
if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&ss, |
|
500 |
&ss_len, JNI_TRUE) != 0) { |
|
501 |
return; |
|
502 |
} |
|
503 |
||
504 |
prim.sspp_assoc_id = assocId; |
|
505 |
prim.sspp_addr = ss; |
|
506 |
||
507 |
if (setsockopt(fd, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR, &prim, |
|
508 |
sizeof(prim)) < 0) { |
|
509 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
510 |
"sun.nio.ch.SctpNet.setPeerPrimAddrOption0"); |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
/* |
|
515 |
* Class: sun_nio_ch_SctpNet |
|
516 |
* Method: getInitMsgOption0 |
|
517 |
* Signature: (I[I)V |
|
518 |
*/ |
|
519 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_getInitMsgOption0 |
|
520 |
(JNIEnv *env, jclass klass, jint fd, jintArray retVal) { |
|
521 |
struct sctp_initmsg sctp_initmsg; |
|
522 |
unsigned int sim_len = sizeof(sctp_initmsg); |
|
523 |
int vals[2]; |
|
524 |
||
525 |
if (getsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_initmsg, |
|
526 |
&sim_len) < 0) { |
|
527 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
528 |
"sun.nio.ch.SctpNet.getInitMsgOption0"); |
|
529 |
return; |
|
530 |
} |
|
531 |
||
532 |
vals[0] = sctp_initmsg.sinit_max_instreams; |
|
533 |
vals[1] = sctp_initmsg.sinit_num_ostreams; |
|
534 |
(*env)->SetIntArrayRegion(env, retVal, 0, 2, vals); |
|
535 |
} |
|
536 |
||
537 |
/* |
|
538 |
* Class: sun_nio_ch_SctpNet |
|
539 |
* Method: setInitMsgOption0 |
|
540 |
* Signature: (III)V |
|
541 |
*/ |
|
542 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_setInitMsgOption0 |
|
543 |
(JNIEnv *env, jclass klass, jint fd, jint inArg, jint outArg) { |
|
544 |
struct sctp_initmsg sctp_initmsg; |
|
545 |
||
546 |
sctp_initmsg.sinit_max_instreams = (unsigned int)inArg; |
|
547 |
sctp_initmsg.sinit_num_ostreams = (unsigned int)outArg; |
|
548 |
sctp_initmsg.sinit_max_attempts = 0; // default |
|
549 |
sctp_initmsg.sinit_max_init_timeo = 0; // default |
|
550 |
||
551 |
if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_initmsg, |
|
552 |
sizeof(sctp_initmsg)) < 0) { |
|
553 |
JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
554 |
"sun.nio.ch.SctpNet.setInitMsgOption0"); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/* |
|
559 |
* Class: sun_nio_ch_SctpNet |
|
560 |
* Method: shutdown0 |
|
561 |
* Signature: (II)V |
|
562 |
*/ |
|
563 |
JNIEXPORT void JNICALL Java_sun_nio_ch_SctpNet_shutdown0 |
|
564 |
(JNIEnv *env, jclass klass, jint fd, jint assocId) { |
|
565 |
int rv; |
|
566 |
struct msghdr msg[1]; |
|
567 |
struct iovec iov[1]; |
|
568 |
int cbuf_size = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo)); |
|
569 |
char cbuf[CMSG_SPACE(sizeof (struct sctp_sndrcvinfo))]; |
|
570 |
struct cmsghdr* cmsg; |
|
571 |
struct sctp_sndrcvinfo *sri; |
|
572 |
||
573 |
/* SctpSocketChannel */ |
|
574 |
if (assocId < 0) { |
|
575 |
shutdown(fd, SHUT_WR); |
|
576 |
return; |
|
577 |
} |
|
578 |
||
579 |
memset(msg, 0, sizeof (*msg)); |
|
580 |
memset(cbuf, 0, cbuf_size); |
|
581 |
msg->msg_name = NULL; |
|
582 |
msg->msg_namelen = 0; |
|
583 |
iov->iov_base = NULL; |
|
584 |
iov->iov_len = 0; |
|
585 |
msg->msg_iov = iov; |
|
586 |
msg->msg_iovlen = 1; |
|
587 |
msg->msg_control = cbuf; |
|
588 |
msg->msg_controllen = cbuf_size; |
|
589 |
msg->msg_flags = 0; |
|
590 |
||
591 |
cmsg = CMSG_FIRSTHDR(msg); |
|
592 |
cmsg->cmsg_level = IPPROTO_SCTP; |
|
593 |
cmsg->cmsg_type = SCTP_SNDRCV; |
|
594 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
|
595 |
||
596 |
/* Initialize the payload: */ |
|
597 |
sri = (struct sctp_sndrcvinfo*) CMSG_DATA(cmsg); |
|
598 |
memset(sri, 0, sizeof (*sri)); |
|
599 |
||
600 |
if (assocId > 0) { |
|
601 |
sri->sinfo_assoc_id = assocId; |
|
602 |
} |
|
603 |
||
604 |
sri->sinfo_flags = sri->sinfo_flags | SCTP_EOF; |
|
605 |
||
606 |
/* Sum of the length of all control messages in the buffer. */ |
|
607 |
msg->msg_controllen = cmsg->cmsg_len; |
|
608 |
||
609 |
if ((rv = sendmsg(fd, msg, 0)) < 0) { |
|
610 |
handleSocketError(env, errno); |
|
611 |
} |
|
612 |
} |
|
613 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
614 |
/* |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
615 |
* Class: sun_nio_ch_SctpNet |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
616 |
* Method: branch |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
617 |
* Signature: (II)I |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
618 |
*/ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
619 |
JNIEXPORT int JNICALL Java_sun_nio_ch_SctpNet_branch0 |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
620 |
(JNIEnv *env, jclass klass, jint fd, jint assocId) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
621 |
int newfd = 0; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
622 |
if ((newfd = nio_sctp_peeloff(fd, assocId)) < 0) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
623 |
handleSocketError(env, errno); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
624 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
625 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
626 |
return newfd; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
627 |
} |