1 /*
2 * Copyright (c) 2001, 2019, 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
570 optval = (void*)&mreq;
571 optlen = sizeof(mreq);
572 } else {
573
574 #ifdef _AIX
575 /* check AIX for support of source filtering */
576 if (isSourceFilterSupported() != JNI_TRUE){
577 return IOS_UNAVAILABLE;
578 }
579 #endif
580
581 mreq_source.imr_multiaddr.s_addr = htonl(group);
582 mreq_source.imr_sourceaddr.s_addr = htonl(source);
583 mreq_source.imr_interface.s_addr = htonl(interf);
584 opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
585 optval = (void*)&mreq_source;
586 optlen = sizeof(mreq_source);
587 }
588
589 n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
590 if (n < 0) {
591 if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
592 return IOS_UNAVAILABLE;
593 handleSocketError(env, errno);
594 }
595 return 0;
596 }
597
598 JNIEXPORT jint JNICALL
599 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
600 jint group, jint interf, jint source)
601 {
602 #ifdef __APPLE__
603 /* no IPv4 exclude-mode filtering for now */
604 return IOS_UNAVAILABLE;
605 #else
606 struct ip_mreq_source mreq_source;
607 int n;
608 int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
609
640
641 if (source == NULL) {
642 COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
643 mreq6.ipv6mr_interface = (int)index;
644 opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
645 optval = (void*)&mreq6;
646 optlen = sizeof(mreq6);
647 } else {
648 #ifdef __APPLE__
649 /* no IPv6 include-mode filtering for now */
650 return IOS_UNAVAILABLE;
651 #else
652 initGroupSourceReq(env, group, index, source, &req);
653 opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
654 optval = (void*)&req;
655 optlen = sizeof(req);
656 #endif
657 }
658
659 n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
660 if (n < 0) {
661 if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
662 return IOS_UNAVAILABLE;
663 handleSocketError(env, errno);
664 }
665 return 0;
666 }
667
668 JNIEXPORT jint JNICALL
669 Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
670 jbyteArray group, jint index, jbyteArray source)
671 {
672 #ifdef __APPLE__
673 /* no IPv6 exclude-mode filtering for now */
674 return IOS_UNAVAILABLE;
675 #else
676 struct group_source_req req;
677 int n;
678 int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
679
|
1 /*
2 * Copyright (c) 2001, 2020, 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
570 optval = (void*)&mreq;
571 optlen = sizeof(mreq);
572 } else {
573
574 #ifdef _AIX
575 /* check AIX for support of source filtering */
576 if (isSourceFilterSupported() != JNI_TRUE){
577 return IOS_UNAVAILABLE;
578 }
579 #endif
580
581 mreq_source.imr_multiaddr.s_addr = htonl(group);
582 mreq_source.imr_sourceaddr.s_addr = htonl(source);
583 mreq_source.imr_interface.s_addr = htonl(interf);
584 opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
585 optval = (void*)&mreq_source;
586 optlen = sizeof(mreq_source);
587 }
588
589 n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
590 #ifdef __APPLE__
591 // workaround macOS bug where IP_ADD_MEMBERSHIP fails intermittently
592 if (n < 0 && errno == ENOMEM) {
593 n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
594 }
595 #endif
596
597 if (n < 0) {
598 if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
599 return IOS_UNAVAILABLE;
600 handleSocketError(env, errno);
601 }
602 return 0;
603 }
604
605 JNIEXPORT jint JNICALL
606 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
607 jint group, jint interf, jint source)
608 {
609 #ifdef __APPLE__
610 /* no IPv4 exclude-mode filtering for now */
611 return IOS_UNAVAILABLE;
612 #else
613 struct ip_mreq_source mreq_source;
614 int n;
615 int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
616
647
648 if (source == NULL) {
649 COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
650 mreq6.ipv6mr_interface = (int)index;
651 opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
652 optval = (void*)&mreq6;
653 optlen = sizeof(mreq6);
654 } else {
655 #ifdef __APPLE__
656 /* no IPv6 include-mode filtering for now */
657 return IOS_UNAVAILABLE;
658 #else
659 initGroupSourceReq(env, group, index, source, &req);
660 opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
661 optval = (void*)&req;
662 optlen = sizeof(req);
663 #endif
664 }
665
666 n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
667 #ifdef __APPLE__
668 // workaround macOS bug where IPV6_ADD_MEMBERSHIP fails intermittently
669 if (n < 0 && errno == ENOMEM) {
670 n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
671 }
672 #endif
673
674 if (n < 0) {
675 if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
676 return IOS_UNAVAILABLE;
677 handleSocketError(env, errno);
678 }
679 return 0;
680 }
681
682 JNIEXPORT jint JNICALL
683 Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
684 jbyteArray group, jint index, jbyteArray source)
685 {
686 #ifdef __APPLE__
687 /* no IPv6 exclude-mode filtering for now */
688 return IOS_UNAVAILABLE;
689 #else
690 struct group_source_req req;
691 int n;
692 int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
693
|