1 /*
2 * Copyright (c) 2009, 2021, 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
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_sctp_SctpNet.h"
38 #include "sun_nio_ch_sctp_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 #ifndef __FreeBSD__
47 sctp_getladdrs_func* nio_sctp_getladdrs;
48 sctp_freeladdrs_func* nio_sctp_freeladdrs;
49 sctp_getpaddrs_func* nio_sctp_getpaddrs;
50 sctp_freepaddrs_func* nio_sctp_freepaddrs;
51 sctp_bindx_func* nio_sctp_bindx;
52 sctp_peeloff_func* nio_sctp_peeloff;
53 #endif
54
DEF_JNI_OnLoad(JavaVM * vm,void * reserved)55 JNIEXPORT jint JNICALL DEF_JNI_OnLoad
56 (JavaVM *vm, void *reserved) {
57 return JNI_VERSION_1_2;
58 }
59
60 static int preCloseFD = -1; /* File descriptor to which we dup other fd's
61 before closing them for real */
62
63 /**
64 * Loads the native sctp library that contains the socket extension
65 * functions, as well as locating the individual functions.
66 * There will be a pending exception if this method returns false.
67 */
loadSocketExtensionFuncs(JNIEnv * env)68 jboolean loadSocketExtensionFuncs
69 (JNIEnv* env) {
70 #ifndef __FreeBSD__
71 if (dlopen(nativeSctpLib, RTLD_GLOBAL | RTLD_LAZY) == NULL) {
72 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
73 dlerror());
74 return JNI_FALSE;
75 }
76
77 if ((nio_sctp_getladdrs = (sctp_getladdrs_func*)
78 dlsym(RTLD_DEFAULT, "sctp_getladdrs")) == NULL) {
79 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
80 dlerror());
81 return JNI_FALSE;
82 }
83
84 if ((nio_sctp_freeladdrs = (sctp_freeladdrs_func*)
85 dlsym(RTLD_DEFAULT, "sctp_freeladdrs")) == NULL) {
86 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
87 dlerror());
88 return JNI_FALSE;
89 }
90
91 if ((nio_sctp_getpaddrs = (sctp_getpaddrs_func*)
92 dlsym(RTLD_DEFAULT, "sctp_getpaddrs")) == NULL) {
93 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
94 dlerror());
95 return JNI_FALSE;
96 }
97
98 if ((nio_sctp_freepaddrs = (sctp_freepaddrs_func*)
99 dlsym(RTLD_DEFAULT, "sctp_freepaddrs")) == NULL) {
100 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
101 dlerror());
102 return JNI_FALSE;
103 }
104
105 if ((nio_sctp_bindx = (sctp_bindx_func*)
106 dlsym(RTLD_DEFAULT, "sctp_bindx")) == NULL) {
107 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
108 dlerror());
109 return JNI_FALSE;
110 }
111
112 if ((nio_sctp_peeloff = (sctp_peeloff_func*)
113 dlsym(RTLD_DEFAULT, "sctp_peeloff")) == NULL) {
114 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
115 dlerror());
116 return JNI_FALSE;
117 }
118 #endif
119
120 funcsLoaded = JNI_TRUE;
121 return JNI_TRUE;
122 }
123
124 jint
handleSocketError(JNIEnv * env,jint errorValue)125 handleSocketError(JNIEnv *env, jint errorValue)
126 {
127 char *xn;
128 switch (errorValue) {
129 case EINPROGRESS: /* Non-blocking connect */
130 return 0;
131 case EPROTO:
132 xn= JNU_JAVANETPKG "ProtocolException";
133 break;
134 case ECONNREFUSED:
135 xn = JNU_JAVANETPKG "ConnectException";
136 break;
137 case ETIMEDOUT:
138 xn = JNU_JAVANETPKG "ConnectException";
139 break;
140 case EHOSTUNREACH:
141 xn = JNU_JAVANETPKG "NoRouteToHostException";
142 break;
143 case EADDRINUSE: /* Fall through */
144 case EADDRNOTAVAIL:
145 xn = JNU_JAVANETPKG "BindException";
146 break;
147 default:
148 xn = JNU_JAVANETPKG "SocketException";
149 break;
150 }
151 errno = errorValue;
152 JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
153 return IOS_THROWN;
154 }
155
156 /*
157 * Class: sun_nio_ch_sctp_SctpNet
158 * Method: init
159 * Signature: ()V
160 */
161 JNIEXPORT void JNICALL
Java_sun_nio_ch_sctp_SctpNet_init(JNIEnv * env,jclass cl)162 Java_sun_nio_ch_sctp_SctpNet_init
163 (JNIEnv *env, jclass cl) {
164 int sp[2];
165 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
166 JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
167 return;
168 }
169 preCloseFD = sp[0];
170 close(sp[1]);
171 initInetAddressIDs(env);
172 }
173
174 /*
175 * Class: sun_nio_ch_sctp_SctpNet
176 * Method: socket0
177 * Signature: (Z)I
178 */
Java_sun_nio_ch_sctp_SctpNet_socket0(JNIEnv * env,jclass klass,jboolean oneToOne)179 JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpNet_socket0
180 (JNIEnv *env, jclass klass, jboolean oneToOne) {
181 int fd;
182 struct sctp_event_subscribe event;
183 #ifdef AF_INET6
184 int domain = ipv6_available() ? AF_INET6 : AF_INET;
185 #else
186 int domain = AF_INET;
187 #endif
188
189 /* Try to load the socket API extension functions */
190 if (!funcsLoaded && !loadSocketExtensionFuncs(env)) {
191 return 0;
192 }
193
194 fd = socket(domain, (oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP);
195
196 if (fd < 0) {
197 if (errno == EPROTONOSUPPORT || errno == ESOCKTNOSUPPORT) {
198 JNU_ThrowByNameWithLastError(env, "java/lang/UnsupportedOperationException",
199 "Protocol not supported");
200 return IOS_THROWN;
201 } else {
202 return handleSocketError(env, errno);
203 }
204 }
205
206 /* Enable events */
207 memset(&event, 0, sizeof(event));
208 event.sctp_data_io_event = 1;
209 event.sctp_association_event = 1;
210 event.sctp_address_event = 1;
211 event.sctp_send_failure_event = 1;
212 //event.sctp_peer_error_event = 1;
213 event.sctp_shutdown_event = 1;
214 //event.sctp_partial_delivery_event = 1;
215 //event.sctp_adaptation_layer_event = 1;
216 if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(event)) != 0) {
217 handleSocketError(env, errno);
218 }
219 return fd;
220 }
221
222 /*
223 * Class: sun_nio_ch_sctp_SctpNet
224 * Method: bindx
225 * Signature: (I[Ljava/net/InetAddress;IIZ)V
226 */
Java_sun_nio_ch_sctp_SctpNet_bindx(JNIEnv * env,jclass klass,jint fd,jobjectArray addrs,jint port,jint addrsLength,jboolean add,jboolean preferIPv6)227 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_bindx
228 (JNIEnv *env, jclass klass, jint fd, jobjectArray addrs, jint port,
229 jint addrsLength, jboolean add, jboolean preferIPv6) {
230 SOCKETADDRESS *sap, *tmpSap;
231 int i;
232 jobject ia;
233
234 if (addrsLength < 1)
235 return;
236
237 if ((sap = calloc(addrsLength, sizeof(SOCKETADDRESS))) == NULL) {
238 JNU_ThrowOutOfMemoryError(env, "heap allocation failure");
239 return;
240 }
241
242 tmpSap = sap;
243 for (i = 0; i < addrsLength; i++) {
244 ia = (*env)->GetObjectArrayElement(env, addrs, i);
245 if (NET_InetAddressToSockaddr(env, ia, port, tmpSap, NULL,
246 preferIPv6) != 0) {
247 free(sap);
248 return;
249 }
250 tmpSap++;
251 }
252
253 if (nio_sctp_bindx(fd, (void *)sap, addrsLength, add ? SCTP_BINDX_ADD_ADDR :
254 SCTP_BINDX_REM_ADDR) != 0) {
255 handleSocketError(env, errno);
256 }
257
258 free(sap);
259 }
260
261 /*
262 * Class: sun_nio_ch_sctp_SctpNet
263 * Method: listen0
264 * Signature: (II)V
265 */
266 JNIEXPORT void JNICALL
Java_sun_nio_ch_sctp_SctpNet_listen0(JNIEnv * env,jclass cl,jint fd,jint backlog)267 Java_sun_nio_ch_sctp_SctpNet_listen0
268 (JNIEnv *env, jclass cl, jint fd, jint backlog) {
269 if (listen(fd, backlog) < 0)
270 handleSocketError(env, errno);
271 }
272
273 /*
274 * Class: sun_nio_ch_sctp_SctpNet
275 * Method: connect0
276 * Signature: (ILjava/net/InetAddress;I)I
277 */
278 JNIEXPORT jint JNICALL
Java_sun_nio_ch_sctp_SctpNet_connect0(JNIEnv * env,jclass clazz,int fd,jobject iao,jint port)279 Java_sun_nio_ch_sctp_SctpNet_connect0
280 (JNIEnv *env, jclass clazz, int fd, jobject iao, jint port) {
281 SOCKETADDRESS sa;
282 int sa_len = 0;
283 int rv;
284
285 if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len,
286 JNI_TRUE) != 0) {
287 return IOS_THROWN;
288 }
289
290 rv = connect(fd, &sa.sa, sa_len);
291 if (rv != 0) {
292 if (errno == EINPROGRESS) {
293 return IOS_UNAVAILABLE;
294 } else if (errno == EINTR) {
295 return IOS_INTERRUPTED;
296 }
297 return handleSocketError(env, errno);
298 }
299 return 1;
300 }
301
302 /*
303 * Class: sun_nio_ch_sctp_SctpNet
304 * Method: close0
305 * Signature: (I)V
306 */
307 JNIEXPORT void JNICALL
Java_sun_nio_ch_sctp_SctpNet_close0(JNIEnv * env,jclass clazz,jint fd)308 Java_sun_nio_ch_sctp_SctpNet_close0
309 (JNIEnv *env, jclass clazz, jint fd) {
310 if (fd != -1) {
311 int rv = close(fd);
312 if (rv < 0)
313 JNU_ThrowIOExceptionWithLastError(env, "Close failed");
314 }
315 }
316
317 /*
318 * Class: sun_nio_ch_sctp_SctpNet
319 * Method: preClose0
320 * Signature: (I)V
321 */
322 JNIEXPORT void JNICALL
Java_sun_nio_ch_sctp_SctpNet_preClose0(JNIEnv * env,jclass clazz,jint fd)323 Java_sun_nio_ch_sctp_SctpNet_preClose0
324 (JNIEnv *env, jclass clazz, jint fd) {
325 if (preCloseFD >= 0) {
326 if (dup2(preCloseFD, fd) < 0)
327 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
328 }
329 }
330
initializeISA(JNIEnv * env)331 void initializeISA(JNIEnv* env) {
332 if (isaCls == 0) {
333 jclass c = (*env)->FindClass(env, "java/net/InetSocketAddress");
334 CHECK_NULL(c);
335 isaCtrID = (*env)->GetMethodID(env, c, "<init>",
336 "(Ljava/net/InetAddress;I)V");
337 CHECK_NULL(isaCtrID);
338 isaCls = (*env)->NewGlobalRef(env, c);
339 CHECK_NULL(isaCls);
340 (*env)->DeleteLocalRef(env, c);
341 }
342 }
343
SockAddrToInetSocketAddress(JNIEnv * env,SOCKETADDRESS * sap)344 jobject SockAddrToInetSocketAddress(JNIEnv *env, SOCKETADDRESS *sap) {
345 int port = 0;
346
347 jobject ia = NET_SockaddrToInetAddress(env, sap, &port);
348 if (ia == NULL)
349 return NULL;
350
351 if (isaCls == 0) {
352 initializeISA(env);
353 CHECK_NULL_RETURN(isaCls, NULL);
354 }
355
356 return (*env)->NewObject(env, isaCls, isaCtrID, ia, port);
357 }
358
359 /*
360 * Class: sun_nio_ch_sctp_SctpNet
361 * Method: getLocalAddresses0
362 * Signature: (I)[Ljava/net/SocketAddress;
363 */
Java_sun_nio_ch_sctp_SctpNet_getLocalAddresses0(JNIEnv * env,jclass klass,jint fd)364 JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_sctp_SctpNet_getLocalAddresses0
365 (JNIEnv *env, jclass klass, jint fd)
366 {
367 void *addr_buf, *laddr;
368 int i, addrCount;
369 jobjectArray isaa;
370
371 if ((addrCount = nio_sctp_getladdrs(fd, 0, (struct sockaddr **)&addr_buf)) == -1) {
372 handleSocketError(env, errno);
373 return NULL;
374 }
375
376 if (addrCount < 1)
377 return NULL;
378
379 if (isaCls == 0) {
380 initializeISA(env);
381 CHECK_NULL_RETURN(isaCls, NULL);
382 }
383
384 isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL);
385 if (isaa == NULL) {
386 nio_sctp_freeladdrs(addr_buf);
387 return NULL;
388 }
389
390 laddr = addr_buf;
391 for (i = 0; i < addrCount; i++) {
392 int port = 0;
393 jobject ia, isa = NULL;
394 ia = NET_SockaddrToInetAddress(env, (SOCKETADDRESS *)addr_buf, &port);
395 if (ia != NULL)
396 isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port);
397 if (isa == NULL)
398 break;
399 (*env)->SetObjectArrayElement(env, isaa, i, isa);
400
401 if (((struct sockaddr *)addr_buf)->sa_family == AF_INET)
402 addr_buf = ((struct sockaddr_in *)addr_buf) + 1;
403 else
404 addr_buf = ((struct sockaddr_in6 *)addr_buf) + 1;
405 }
406
407 nio_sctp_freeladdrs(laddr);
408 return isaa;
409 }
410
getRemoteAddresses(JNIEnv * env,jint fd,sctp_assoc_t id)411 jobjectArray getRemoteAddresses(JNIEnv *env, jint fd, sctp_assoc_t id) {
412 void *addr_buf, *paddr;
413 int i, addrCount;
414 jobjectArray isaa;
415
416 if ((addrCount = nio_sctp_getpaddrs(fd, id, (struct sockaddr **)&addr_buf)) == -1) {
417 handleSocketError(env, errno);
418 return NULL;
419 }
420
421 if (addrCount < 1)
422 return NULL;
423
424 if (isaCls == 0) {
425 initializeISA(env);
426 CHECK_NULL_RETURN(isaCls, NULL);
427 }
428
429 isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL);
430 if (isaa == NULL) {
431 nio_sctp_freepaddrs(addr_buf);
432 return NULL;
433 }
434
435 paddr = addr_buf;
436 for (i = 0; i < addrCount; i++) {
437 int port = 0;
438 jobject ia, isa = NULL;
439 ia = NET_SockaddrToInetAddress(env, (SOCKETADDRESS *)addr_buf, &port);
440 if (ia != NULL)
441 isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port);
442 if (isa == NULL)
443 break;
444 (*env)->SetObjectArrayElement(env, isaa, i, isa);
445
446 if (((struct sockaddr *)addr_buf)->sa_family == AF_INET)
447 addr_buf = ((struct sockaddr_in *)addr_buf) + 1;
448 else
449 addr_buf = ((struct sockaddr_in6 *)addr_buf) + 1;
450 }
451
452 nio_sctp_freepaddrs(paddr);
453 return isaa;
454 }
455
456 /*
457 * Class: sun_nio_ch_sctp_SctpNet
458 * Method: getRemoteAddresses0
459 * Signature: (II)[Ljava/net/SocketAddress;
460 */
Java_sun_nio_ch_sctp_SctpNet_getRemoteAddresses0(JNIEnv * env,jclass klass,jint fd,jint assocId)461 JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_sctp_SctpNet_getRemoteAddresses0
462 (JNIEnv *env, jclass klass, jint fd, jint assocId) {
463 return getRemoteAddresses(env, fd, assocId);
464 }
465
466 /* Map the Java level option to the native level */
mapSocketOption(jint cmd,int * level,int * optname)467 int mapSocketOption
468 (jint cmd, int *level, int *optname) {
469 static struct {
470 jint cmd;
471 int level;
472 int optname;
473 } const opts[] = {
474 { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_DISABLE_FRAGMENTS, IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS },
475 { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_EXPLICIT_COMPLETE, IPPROTO_SCTP, SCTP_EXPLICIT_EOR },
476 { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_FRAGMENT_INTERLEAVE, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE },
477 { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_NODELAY, IPPROTO_SCTP, SCTP_NODELAY },
478 { sun_nio_ch_sctp_SctpStdSocketOption_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF },
479 { sun_nio_ch_sctp_SctpStdSocketOption_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF },
480 { sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER, SOL_SOCKET, SO_LINGER } };
481
482 int i;
483 for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
484 if (cmd == opts[i].cmd) {
485 *level = opts[i].level;
486 *optname = opts[i].optname;
487 return 0;
488 }
489 }
490
491 /* not found */
492 return -1;
493 }
494
495 /*
496 * Class: sun_nio_ch_sctp_SctpNet
497 * Method: setIntOption0
498 * Signature: (III)V
499 */
Java_sun_nio_ch_sctp_SctpNet_setIntOption0(JNIEnv * env,jclass klass,jint fd,jint opt,int arg)500 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_setIntOption0
501 (JNIEnv *env, jclass klass, jint fd, jint opt, int arg) {
502 int klevel, kopt;
503 int result;
504 struct linger linger;
505 void *parg;
506 int arglen;
507
508 if (mapSocketOption(opt, &klevel, &kopt) < 0) {
509 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
510 "Unsupported socket option");
511 return;
512 }
513
514 if (opt == sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER) {
515 parg = (void *)&linger;
516 arglen = sizeof(linger);
517 if (arg >= 0) {
518 linger.l_onoff = 1;
519 linger.l_linger = arg;
520 } else {
521 linger.l_onoff = 0;
522 linger.l_linger = 0;
523 }
524 } else {
525 parg = (void *)&arg;
526 arglen = sizeof(arg);
527 }
528
529 if (NET_SetSockOpt(fd, klevel, kopt, parg, arglen) < 0) {
530 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
531 "sun_nio_ch_sctp_SctpNet.setIntOption0");
532 }
533 }
534
535 /*
536 * Class: sun_nio_ch_sctp_SctpNet
537 * Method: getIntOption0
538 * Signature: (II)I
539 */
Java_sun_nio_ch_sctp_SctpNet_getIntOption0(JNIEnv * env,jclass klass,jint fd,jint opt)540 JNIEXPORT int JNICALL Java_sun_nio_ch_sctp_SctpNet_getIntOption0
541 (JNIEnv *env, jclass klass, jint fd, jint opt) {
542 int klevel, kopt;
543 int result;
544 struct linger linger;
545 void *arg;
546 int arglen;
547
548 memset((char *) &linger, 0, sizeof(linger));
549 if (mapSocketOption(opt, &klevel, &kopt) < 0) {
550 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
551 "Unsupported socket option");
552 return -1;
553 }
554
555 if (opt == sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER) {
556 arg = (void *)&linger;
557 arglen = sizeof(linger);
558 } else {
559 arg = (void *)&result;
560 arglen = sizeof(result);
561 }
562
563 if (NET_GetSockOpt(fd, klevel, kopt, arg, &arglen) < 0) {
564 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
565 "sun.nio.ch.Net.getIntOption");
566 return -1;
567 }
568
569 if (opt == sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER)
570 return linger.l_onoff ? linger.l_linger : -1;
571 else
572 return result;
573 }
574
575 /*
576 * Class: sun_nio_ch_sctp_SctpNet
577 * Method: getPrimAddrOption0
578 * Signature: (II)Ljava/net/SocketAddress;
579 */
Java_sun_nio_ch_sctp_SctpNet_getPrimAddrOption0(JNIEnv * env,jclass klass,jint fd,jint assocId)580 JNIEXPORT jobject JNICALL Java_sun_nio_ch_sctp_SctpNet_getPrimAddrOption0
581 (JNIEnv *env, jclass klass, jint fd, jint assocId) {
582 struct sctp_setprim prim;
583 unsigned int prim_len = sizeof(prim);
584
585 prim.ssp_assoc_id = assocId;
586
587 if (getsockopt(fd, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &prim, &prim_len) < 0) {
588 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
589 "sun.nio.ch.SctpNet.getPrimAddrOption0");
590 return NULL;
591 }
592
593 return SockAddrToInetSocketAddress(env, (SOCKETADDRESS *)&prim.ssp_addr);
594 }
595
596 /*
597 * Class: sun_nio_ch_sctp_SctpNet
598 * Method: setPrimAddrOption0
599 * Signature: (IILjava/net/InetAddress;I)V
600 */
Java_sun_nio_ch_sctp_SctpNet_setPrimAddrOption0(JNIEnv * env,jclass klass,jint fd,jint assocId,jobject iaObj,jint port)601 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_setPrimAddrOption0
602 (JNIEnv *env, jclass klass, jint fd, jint assocId, jobject iaObj, jint port) {
603 struct sctp_setprim prim;
604
605 if (NET_InetAddressToSockaddr(env, iaObj, port,
606 (SOCKETADDRESS *)&prim.ssp_addr,
607 NULL, JNI_TRUE) != 0) {
608 return;
609 }
610
611 prim.ssp_assoc_id = assocId;
612
613 if (setsockopt(fd, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &prim, sizeof(prim)) < 0) {
614 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
615 "sun.nio.ch.SctpNet.setPrimAddrOption0");
616 }
617 }
618
619 /*
620 * Class: sun_nio_ch_sctp_SctpNet
621 * Method: setPeerPrimAddrOption0
622 * Signature: (IILjava/net/InetAddress;I)V
623 */
Java_sun_nio_ch_sctp_SctpNet_setPeerPrimAddrOption0(JNIEnv * env,jclass klass,jint fd,jint assocId,jobject iaObj,jint port,jboolean preferIPv6)624 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_setPeerPrimAddrOption0
625 (JNIEnv *env, jclass klass, jint fd, jint assocId,
626 jobject iaObj, jint port, jboolean preferIPv6) {
627 struct sctp_setpeerprim prim;
628
629 if (NET_InetAddressToSockaddr(env, iaObj, port,
630 (SOCKETADDRESS *)&prim.sspp_addr,
631 NULL, preferIPv6) != 0) {
632 return;
633 }
634
635 prim.sspp_assoc_id = assocId;
636
637 if (setsockopt(fd, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR, &prim,
638 sizeof(prim)) < 0) {
639 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
640 "sun.nio.ch.SctpNet.setPeerPrimAddrOption0");
641 }
642 }
643
644 /*
645 * Class: sun_nio_ch_sctp_SctpNet
646 * Method: getInitMsgOption0
647 * Signature: (I[I)V
648 */
Java_sun_nio_ch_sctp_SctpNet_getInitMsgOption0(JNIEnv * env,jclass klass,jint fd,jintArray retVal)649 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_getInitMsgOption0
650 (JNIEnv *env, jclass klass, jint fd, jintArray retVal) {
651 struct sctp_initmsg sctp_initmsg;
652 unsigned int sim_len = sizeof(sctp_initmsg);
653 int vals[2];
654
655 if (getsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_initmsg,
656 &sim_len) < 0) {
657 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
658 "sun.nio.ch.SctpNet.getInitMsgOption0");
659 return;
660 }
661
662 vals[0] = sctp_initmsg.sinit_max_instreams;
663 vals[1] = sctp_initmsg.sinit_num_ostreams;
664 (*env)->SetIntArrayRegion(env, retVal, 0, 2, vals);
665 }
666
667 /*
668 * Class: sun_nio_ch_sctp_SctpNet
669 * Method: setInitMsgOption0
670 * Signature: (III)V
671 */
Java_sun_nio_ch_sctp_SctpNet_setInitMsgOption0(JNIEnv * env,jclass klass,jint fd,jint inArg,jint outArg)672 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_setInitMsgOption0
673 (JNIEnv *env, jclass klass, jint fd, jint inArg, jint outArg) {
674 struct sctp_initmsg sctp_initmsg;
675
676 sctp_initmsg.sinit_max_instreams = (unsigned int)inArg;
677 sctp_initmsg.sinit_num_ostreams = (unsigned int)outArg;
678 sctp_initmsg.sinit_max_attempts = 0; // default
679 sctp_initmsg.sinit_max_init_timeo = 0; // default
680
681 if (setsockopt(fd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_initmsg,
682 sizeof(sctp_initmsg)) < 0) {
683 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
684 "sun.nio.ch.SctpNet.setInitMsgOption0");
685 }
686 }
687
688 /*
689 * Class: sun_nio_ch_sctp_SctpNet
690 * Method: shutdown0
691 * Signature: (II)V
692 */
Java_sun_nio_ch_sctp_SctpNet_shutdown0(JNIEnv * env,jclass klass,jint fd,jint assocId)693 JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_shutdown0
694 (JNIEnv *env, jclass klass, jint fd, jint assocId) {
695 int rv;
696 struct msghdr msg[1];
697 struct iovec iov[1];
698 int cbuf_size = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo));
699 char cbuf[CMSG_SPACE(sizeof (struct sctp_sndrcvinfo))];
700 struct cmsghdr* cmsg;
701 struct sctp_sndrcvinfo *sri;
702
703 /* SctpSocketChannel */
704 if (assocId < 0) {
705 shutdown(fd, SHUT_WR);
706 return;
707 }
708
709 memset(msg, 0, sizeof (*msg));
710 memset(cbuf, 0, cbuf_size);
711 msg->msg_name = NULL;
712 msg->msg_namelen = 0;
713 iov->iov_base = NULL;
714 iov->iov_len = 0;
715 msg->msg_iov = iov;
716 msg->msg_iovlen = 1;
717 msg->msg_control = cbuf;
718 msg->msg_controllen = cbuf_size;
719 msg->msg_flags = 0;
720
721 cmsg = CMSG_FIRSTHDR(msg);
722 cmsg->cmsg_level = IPPROTO_SCTP;
723 cmsg->cmsg_type = SCTP_SNDRCV;
724 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
725
726 /* Initialize the payload: */
727 sri = (struct sctp_sndrcvinfo*) CMSG_DATA(cmsg);
728 memset(sri, 0, sizeof (*sri));
729
730 if (assocId > 0) {
731 sri->sinfo_assoc_id = assocId;
732 }
733
734 sri->sinfo_flags = sri->sinfo_flags | SCTP_EOF;
735
736 /* Sum of the length of all control messages in the buffer. */
737 msg->msg_controllen = cmsg->cmsg_len;
738
739 if ((rv = sendmsg(fd, msg, 0)) < 0) {
740 handleSocketError(env, errno);
741 }
742 }
743
744 /*
745 * Class: sun_nio_ch_sctp_SctpNet
746 * Method: branch
747 * Signature: (II)I
748 */
Java_sun_nio_ch_sctp_SctpNet_branch0(JNIEnv * env,jclass klass,jint fd,jint assocId)749 JNIEXPORT int JNICALL Java_sun_nio_ch_sctp_SctpNet_branch0
750 (JNIEnv *env, jclass klass, jint fd, jint assocId) {
751 int newfd = 0;
752 if ((newfd = nio_sctp_peeloff(fd, assocId)) < 0) {
753 handleSocketError(env, errno);
754 }
755
756 return newfd;
757 }
758