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
  23  * questions.
  24  */
  25 
  26 #include <poll.h>
  27 #include <sys/types.h>
  28 #include <sys/socket.h>
  29 #include <string.h>
  30 #include <netinet/in.h>
  31 #include <netinet/tcp.h>
  32 #include <limits.h>
  33 
  34 #include "jni.h"
  35 #include "jni_util.h"
  36 #include "jvm.h"
  37 #include "jlong.h"
  38 #include "sun_nio_ch_Net.h"
  39 #include "net_util.h"
  40 #include "net_util_md.h"
  41 #include "nio_util.h"
  42 #include "nio.h"
  43 
  44 #ifdef _AIX
  45 #include <sys/utsname.h>
  46 #endif
  47 
  48 /**
  49  * IP_MULTICAST_ALL supported since 2.6.31 but may not be available at
  50  * build time.
  51  */
  52 #ifdef __linux__
  53   #ifndef IP_MULTICAST_ALL
  54     #define IP_MULTICAST_ALL    49
  55   #endif
  56 #endif
  57 
  58 /**
  59  * IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP may not be defined on OSX and AIX
  60  */
  61 #if defined(__APPLE__) || defined(_AIX)
  62   #ifndef IPV6_ADD_MEMBERSHIP
  63     #define IPV6_ADD_MEMBERSHIP     IPV6_JOIN_GROUP
  64     #define IPV6_DROP_MEMBERSHIP    IPV6_LEAVE_GROUP
  65   #endif
  66 #endif
  67 
  68 #if defined(_AIX)
  69   #ifndef IP_BLOCK_SOURCE
  70     #define IP_BLOCK_SOURCE                 58   /* Block data from a given source to a given group */
  71     #define IP_UNBLOCK_SOURCE               59   /* Unblock data from a given source to a given group */
  72     #define IP_ADD_SOURCE_MEMBERSHIP        60   /* Join a source-specific group */
  73     #define IP_DROP_SOURCE_MEMBERSHIP       61   /* Leave a source-specific group */
  74   #endif
  75 
  76   #ifndef MCAST_BLOCK_SOURCE
  77     #define MCAST_BLOCK_SOURCE              64
  78     #define MCAST_UNBLOCK_SOURCE            65
  79     #define MCAST_JOIN_SOURCE_GROUP         66
  80     #define MCAST_LEAVE_SOURCE_GROUP        67
  81 
  82     /* This means we're on AIX 5.3 and 'group_source_req' and 'ip_mreq_source' aren't defined as well */
  83     struct group_source_req {
  84         uint32_t gsr_interface;
  85         struct sockaddr_storage gsr_group;
  86         struct sockaddr_storage gsr_source;
  87     };
  88     struct ip_mreq_source {
  89         struct in_addr  imr_multiaddr;  /* IP multicast address of group */
  90         struct in_addr  imr_sourceaddr; /* IP address of source */
  91         struct in_addr  imr_interface;  /* local IP address of interface */
  92     };
  93   #endif
  94 #endif /* _AIX */
  95 
  96 #define COPY_INET6_ADDRESS(env, source, target) \
  97     (*env)->GetByteArrayRegion(env, source, 0, 16, target)
  98 
  99 /*
 100  * Copy IPv6 group, interface index, and IPv6 source address
 101  * into group_source_req structure.
 102  */
 103 static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index,
 104                                jbyteArray source, struct group_source_req *req)
 105 {
 106     struct sockaddr_in6* sin6;
 107 
 108     req->gsr_interface = (uint32_t)index;
 109 
 110     sin6 = (struct sockaddr_in6 *)&(req->gsr_group);
 111     sin6->sin6_family = AF_INET6;
 112     COPY_INET6_ADDRESS(env, group, (jbyte *)&(sin6->sin6_addr));
 113 
 114     sin6 = (struct sockaddr_in6 *)&(req->gsr_source);
 115     sin6->sin6_family = AF_INET6;
 116     COPY_INET6_ADDRESS(env, source, (jbyte *)&(sin6->sin6_addr));
 117 }
 118 
 119 #ifdef _AIX
 120 
 121 /*
 122  * Checks whether or not "socket extensions for multicast source filters" is supported.
 123  * Returns JNI_TRUE if it is supported, JNI_FALSE otherwise
 124  */
 125 static jboolean isSourceFilterSupported(){
 126     static jboolean alreadyChecked = JNI_FALSE;
 127     static jboolean result = JNI_TRUE;
 128     if (alreadyChecked != JNI_TRUE){
 129         struct utsname uts;
 130         memset(&uts, 0, sizeof(uts));
 131         strcpy(uts.sysname, "?");
 132         const int utsRes = uname(&uts);
 133         int major = -1;
 134         int minor = -1;
 135         major = atoi(uts.version);
 136         minor = atoi(uts.release);
 137         if (strcmp(uts.sysname, "AIX") == 0) {
 138             if (major < 6 || (major == 6 && minor < 1)) {// unsupported on aix < 6.1
 139                 result = JNI_FALSE;
 140             }
 141         }
 142         alreadyChecked = JNI_TRUE;
 143     }
 144     return result;
 145 }
 146 
 147 #endif  /* _AIX */
 148 
 149 static jclass isa_class;        /* java.net.InetSocketAddress */
 150 static jmethodID isa_ctorID;    /* InetSocketAddress(InetAddress, int) */
 151 
 152 JNIEXPORT void JNICALL
 153 Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz)
 154 {
 155      jclass cls = (*env)->FindClass(env, "java/net/InetSocketAddress");
 156      CHECK_NULL(cls);
 157      isa_class = (*env)->NewGlobalRef(env, cls);
 158      if (isa_class == NULL) {
 159          JNU_ThrowOutOfMemoryError(env, NULL);
 160          return;
 161      }
 162      isa_ctorID = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/net/InetAddress;I)V");
 163      CHECK_NULL(isa_ctorID);
 164 
 165      initInetAddressIDs(env);
 166 }
 167 
 168 JNIEXPORT jboolean JNICALL
 169 Java_sun_nio_ch_Net_isIPv6Available0(JNIEnv* env, jclass cl)
 170 {
 171     return (ipv6_available()) ? JNI_TRUE : JNI_FALSE;
 172 }
 173 
 174 JNIEXPORT jboolean JNICALL
 175 Java_sun_nio_ch_Net_isReusePortAvailable0(JNIEnv* env, jclass c1)
 176 {
 177     return (reuseport_available()) ? JNI_TRUE : JNI_FALSE;
 178 }
 179 
 180 JNIEXPORT jint JNICALL
 181 Java_sun_nio_ch_Net_isExclusiveBindAvailable(JNIEnv *env, jclass clazz) {
 182     return -1;
 183 }
 184 
 185 JNIEXPORT jboolean JNICALL
 186 Java_sun_nio_ch_Net_canIPv6SocketJoinIPv4Group0(JNIEnv* env, jclass cl)
 187 {
 188 #if defined(__APPLE__) || defined(_AIX)
 189     /* for now IPv6 sockets cannot join IPv4 multicast groups */
 190     return JNI_FALSE;
 191 #else
 192     return JNI_TRUE;
 193 #endif
 194 }
 195 
 196 JNIEXPORT jboolean JNICALL
 197 Java_sun_nio_ch_Net_canJoin6WithIPv4Group0(JNIEnv* env, jclass cl)
 198 {
 199 #ifdef __solaris__
 200     return JNI_TRUE;
 201 #else
 202     return JNI_FALSE;
 203 #endif
 204 }
 205 
 206 static int socket0(int domain, int type, int protocol) {
 207     if (domain == AF_INET) {
 208         errno = EAFNOSUPPORT;
 209         return -1;
 210     }
 211     return socket(domain, type, protocol);
 212 }
 213 
 214 JNIEXPORT jint JNICALL
 215 Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
 216                             jboolean stream, jboolean reuse, jboolean ignored)
 217 {
 218     int fd;
 219     int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
 220     int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
 221 
 222     fd = socket0(domain, type, 0);
 223     if (fd < 0) {
 224         return handleSocketError(env, errno);
 225     }
 226 
 227     /*
 228      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 229      */
 230     if (domain == AF_INET6 && ipv4_available()) {
 231         int arg = 0;
 232         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 233                        sizeof(int)) < 0) {
 234             JNU_ThrowByNameWithLastError(env,
 235                                          JNU_JAVANETPKG "SocketException",
 236                                          "Unable to set IPV6_V6ONLY");
 237             close(fd);
 238             return -1;
 239         }
 240     }
 241 
 242     if (reuse) {
 243         int arg = 1;
 244         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
 245                        sizeof(arg)) < 0) {
 246             JNU_ThrowByNameWithLastError(env,
 247                                          JNU_JAVANETPKG "SocketException",
 248                                          "Unable to set SO_REUSEADDR");
 249             close(fd);
 250             return -1;
 251         }
 252     }
 253 
 254 #if defined(__linux__)
 255     if (type == SOCK_DGRAM) {
 256         int arg = 0;
 257         int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
 258         if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
 259             (errno != ENOPROTOOPT)) {
 260             JNU_ThrowByNameWithLastError(env,
 261                                          JNU_JAVANETPKG "SocketException",
 262                                          "Unable to set IP_MULTICAST_ALL");
 263             close(fd);
 264             return -1;
 265         }
 266     }
 267 
 268     /* By default, Linux uses the route default */
 269     if (domain == AF_INET6 && type == SOCK_DGRAM) {
 270         int arg = 1;
 271         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &arg,
 272                        sizeof(arg)) < 0) {
 273             JNU_ThrowByNameWithLastError(env,
 274                                          JNU_JAVANETPKG "SocketException",
 275                                          "Unable to set IPV6_MULTICAST_HOPS");
 276             close(fd);
 277             return -1;
 278         }
 279     }
 280 #endif
 281     return fd;
 282 }
 283 
 284 JNIEXPORT void JNICALL
 285 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6,
 286                           jboolean useExclBind, jobject iao, int port)
 287 {
 288     SOCKETADDRESS sa;
 289     int sa_len = 0;
 290     int rv = 0;
 291 
 292     if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len,
 293                                   preferIPv6) != 0) {
 294         return;
 295     }
 296 
 297     rv = NET_Bind(fdval(env, fdo), &sa, sa_len);
 298     if (rv != 0) {
 299         handleSocketError(env, errno);
 300     }
 301 }
 302 
 303 JNIEXPORT void JNICALL
 304 Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
 305 {
 306     if (listen(fdval(env, fdo), backlog) < 0)
 307         handleSocketError(env, errno);
 308 }
 309 
 310 JNIEXPORT jint JNICALL
 311 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
 312                              jobject fdo, jobject iao, jint port)
 313 {
 314     SOCKETADDRESS sa;
 315     int sa_len = 0;
 316     int rv;
 317 
 318     if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len, preferIPv6) != 0) {
 319         return IOS_THROWN;
 320     }
 321 
 322     rv = connect(fdval(env, fdo), &sa.sa, sa_len);
 323     if (rv != 0) {
 324         if (errno == EINPROGRESS) {
 325             return IOS_UNAVAILABLE;
 326         } else if (errno == EINTR) {
 327             return IOS_INTERRUPTED;
 328         }
 329         return handleSocketError(env, errno);
 330     }
 331     return 1;
 332 }
 333 
 334 JNIEXPORT jint JNICALL
 335 Java_sun_nio_ch_Net_accept(JNIEnv *env, jclass clazz, jobject fdo, jobject newfdo,
 336                            jobjectArray isaa)
 337 {
 338     jint fd = fdval(env, fdo);
 339     jint newfd;
 340     SOCKETADDRESS sa;
 341     socklen_t sa_len = sizeof(SOCKETADDRESS);
 342     jobject remote_ia;
 343     jint remote_port = 0;
 344     jobject isa;
 345 
 346     /* accept connection but ignore ECONNABORTED */
 347     for (;;) {
 348         newfd = accept(fd, &sa.sa, &sa_len);
 349         if (newfd >= 0) {
 350             break;
 351         }
 352         if (errno != ECONNABORTED) {
 353             break;
 354         }
 355         /* ECONNABORTED => restart accept */
 356     }
 357 
 358     if (newfd < 0) {
 359         if (errno == EAGAIN || errno == EWOULDBLOCK)
 360             return IOS_UNAVAILABLE;
 361         if (errno == EINTR)
 362             return IOS_INTERRUPTED;
 363         JNU_ThrowIOExceptionWithLastError(env, "Accept failed");
 364         return IOS_THROWN;
 365     }
 366 
 367     setfdval(env, newfdo, newfd);
 368 
 369     remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
 370     CHECK_NULL_RETURN(remote_ia, IOS_THROWN);
 371 
 372     isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port);
 373     CHECK_NULL_RETURN(isa, IOS_THROWN);
 374     (*env)->SetObjectArrayElement(env, isaa, 0, isa);
 375 
 376     return 1;
 377 }
 378 
 379 JNIEXPORT jint JNICALL
 380 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
 381 {
 382     SOCKETADDRESS sa;
 383     socklen_t sa_len = sizeof(SOCKETADDRESS);
 384     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 385 #ifdef _ALLBSD_SOURCE
 386         /*
 387          * XXXBSD:
 388          * ECONNRESET is specific to the BSDs. We can not return an error,
 389          * as the calling Java code with raise a java.lang.Error given the expectation
 390          * that getsockname() will never fail. According to the Single UNIX Specification,
 391          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 392          */
 393         if (errno == ECONNRESET) {
 394             bzero(&sa.sa4, sizeof(sa));
 395             sa.sa4.sin_len = sizeof(struct sockaddr_in);
 396             sa.sa4.sin_family = AF_INET;
 397             sa.sa4.sin_port = htonl(0);
 398             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 399         } else {
 400             handleSocketError(env, errno);
 401             return -1;
 402         }
 403 #else /* _ALLBSD_SOURCE */
 404         handleSocketError(env, errno);
 405         return -1;
 406 #endif /* _ALLBSD_SOURCE */
 407     }
 408     return NET_GetPortFromSockaddr(&sa);
 409 }
 410 
 411 JNIEXPORT jobject JNICALL
 412 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 413 {
 414     SOCKETADDRESS sa;
 415     socklen_t sa_len = sizeof(SOCKETADDRESS);
 416     int port;
 417     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 418 #ifdef _ALLBSD_SOURCE
 419         /*
 420          * XXXBSD:
 421          * ECONNRESET is specific to the BSDs. We can not return an error,
 422          * as the calling Java code with raise a java.lang.Error with the expectation
 423          * that getsockname() will never fail. According to the Single UNIX Specification,
 424          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 425          */
 426         if (errno == ECONNRESET) {
 427             bzero(&sa.sa4, sizeof(sa));
 428             sa.sa4.sin_len  = sizeof(struct sockaddr_in);
 429             sa.sa4.sin_family = AF_INET;
 430             sa.sa4.sin_port = htonl(0);
 431             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 432         } else {
 433             handleSocketError(env, errno);
 434             return NULL;
 435         }
 436 #else /* _ALLBSD_SOURCE */
 437         handleSocketError(env, errno);
 438         return NULL;
 439 #endif /* _ALLBSD_SOURCE */
 440     }
 441     return NET_SockaddrToInetAddress(env, &sa, &port);
 442 }
 443 
 444 JNIEXPORT jint JNICALL
 445 Java_sun_nio_ch_Net_remotePort(JNIEnv *env, jclass clazz, jobject fdo)
 446 {
 447     SOCKETADDRESS sa;
 448     socklen_t sa_len = sizeof(sa);
 449 
 450     if (getpeername(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 451         handleSocketError(env, errno);
 452         return IOS_THROWN;
 453     }
 454     return NET_GetPortFromSockaddr(&sa);
 455 }
 456 
 457 JNIEXPORT jobject JNICALL
 458 Java_sun_nio_ch_Net_remoteInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 459 {
 460     SOCKETADDRESS sa;
 461     socklen_t sa_len = sizeof(sa);
 462     int port;
 463 
 464     if (getpeername(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 465         handleSocketError(env, errno);
 466         return NULL;
 467     }
 468     return NET_SockaddrToInetAddress(env, &sa, &port);
 469 }
 470 
 471 JNIEXPORT jint JNICALL
 472 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 473                                   jboolean mayNeedConversion, jint level, jint opt)
 474 {
 475     int result;
 476     struct linger linger;
 477     u_char carg;
 478     void *arg;
 479     socklen_t arglen;
 480     int n;
 481 
 482     /* Option value is an int except for a few specific cases */
 483 
 484     arg = (void *)&result;
 485     arglen = sizeof(result);
 486 
 487     if (level == IPPROTO_IP &&
 488         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
 489         arg = (void*)&carg;
 490         arglen = sizeof(carg);
 491     }
 492 
 493     if (level == SOL_SOCKET && opt == SO_LINGER) {
 494         arg = (void *)&linger;
 495         arglen = sizeof(linger);
 496     }
 497 
 498     if (mayNeedConversion) {
 499         n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, (int*)&arglen);
 500     } else {
 501         n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
 502     }
 503     if (n < 0) {
 504         JNU_ThrowByNameWithLastError(env,
 505                                      JNU_JAVANETPKG "SocketException",
 506                                      "sun.nio.ch.Net.getIntOption");
 507         return -1;
 508     }
 509 
 510     if (level == IPPROTO_IP &&
 511         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP))
 512     {
 513         return (jint)carg;
 514     }
 515 
 516     if (level == SOL_SOCKET && opt == SO_LINGER)
 517         return linger.l_onoff ? (jint)linger.l_linger : (jint)-1;
 518 
 519     return (jint)result;
 520 }
 521 
 522 JNIEXPORT void JNICALL
 523 Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 524                                   jboolean mayNeedConversion, jint level,
 525                                   jint opt, jint arg, jboolean isIPv6)
 526 {
 527     int result;
 528     struct linger linger;
 529     u_char carg;
 530     void *parg;
 531     socklen_t arglen;
 532     int n;
 533 
 534     /* Option value is an int except for a few specific cases */
 535 
 536     parg = (void*)&arg;
 537     arglen = sizeof(arg);
 538 
 539     if (level == IPPROTO_IP &&
 540         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
 541         parg = (void*)&carg;
 542         arglen = sizeof(carg);
 543         carg = (u_char)arg;
 544     }
 545 
 546     if (level == SOL_SOCKET && opt == SO_LINGER) {
 547         parg = (void *)&linger;
 548         arglen = sizeof(linger);
 549         if (arg >= 0) {
 550             linger.l_onoff = 1;
 551             linger.l_linger = arg;
 552         } else {
 553             linger.l_onoff = 0;
 554             linger.l_linger = 0;
 555         }
 556     }
 557 
 558     if (mayNeedConversion) {
 559         n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
 560     } else {
 561         n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
 562     }
 563     if (n < 0) {
 564         JNU_ThrowByNameWithLastError(env,
 565                                      JNU_JAVANETPKG "SocketException",
 566                                      "sun.nio.ch.Net.setIntOption");
 567     }
 568 #ifdef __linux__
 569     if (level == IPPROTO_IPV6 && opt == IPV6_TCLASS && isIPv6) {
 570         // set the V4 option also
 571         setsockopt(fdval(env, fdo), IPPROTO_IP, IP_TOS, parg, arglen);
 572     }
 573 #endif
 574 }
 575 
 576 JNIEXPORT jint JNICALL
 577 Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 578                                 jint group, jint interf, jint source)
 579 {
 580     struct ip_mreq mreq;
 581     struct ip_mreq_source mreq_source;
 582     int opt, n, optlen;
 583     void* optval;
 584 
 585     if (source == 0) {
 586         mreq.imr_multiaddr.s_addr = htonl(group);
 587         mreq.imr_interface.s_addr = htonl(interf);
 588         opt = (join) ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
 589         optval = (void*)&mreq;
 590         optlen = sizeof(mreq);
 591     } else {
 592 
 593 #ifdef _AIX
 594         /* check AIX for support of source filtering */
 595         if (isSourceFilterSupported() != JNI_TRUE){
 596             return IOS_UNAVAILABLE;
 597         }
 598 #endif
 599 
 600         mreq_source.imr_multiaddr.s_addr = htonl(group);
 601         mreq_source.imr_sourceaddr.s_addr = htonl(source);
 602         mreq_source.imr_interface.s_addr = htonl(interf);
 603         opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
 604         optval = (void*)&mreq_source;
 605         optlen = sizeof(mreq_source);
 606     }
 607 
 608     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
 609     if (n < 0) {
 610         if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 611             return IOS_UNAVAILABLE;
 612         handleSocketError(env, errno);
 613     }
 614     return 0;
 615 }
 616 
 617 JNIEXPORT jint JNICALL
 618 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
 619                                     jint group, jint interf, jint source)
 620 {
 621 #ifdef __APPLE__
 622     /* no IPv4 exclude-mode filtering for now */
 623     return IOS_UNAVAILABLE;
 624 #else
 625     struct ip_mreq_source mreq_source;
 626     int n;
 627     int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
 628 
 629 #ifdef _AIX
 630     /* check AIX for support of source filtering */
 631     if (isSourceFilterSupported() != JNI_TRUE){
 632         return IOS_UNAVAILABLE;
 633     }
 634 #endif
 635 
 636     mreq_source.imr_multiaddr.s_addr = htonl(group);
 637     mreq_source.imr_sourceaddr.s_addr = htonl(source);
 638     mreq_source.imr_interface.s_addr = htonl(interf);
 639 
 640     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt,
 641                    (void*)&mreq_source, sizeof(mreq_source));
 642     if (n < 0) {
 643         if (block && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 644             return IOS_UNAVAILABLE;
 645         handleSocketError(env, errno);
 646     }
 647     return 0;
 648 #endif
 649 }
 650 
 651 JNIEXPORT jint JNICALL
 652 Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 653                                 jbyteArray group, jint index, jbyteArray source)
 654 {
 655     struct ipv6_mreq mreq6;
 656     struct group_source_req req;
 657     int opt, n, optlen;
 658     void* optval;
 659 
 660     if (source == NULL) {
 661         COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
 662         mreq6.ipv6mr_interface = (int)index;
 663         opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
 664         optval = (void*)&mreq6;
 665         optlen = sizeof(mreq6);
 666     } else {
 667 #ifdef __APPLE__
 668         /* no IPv6 include-mode filtering for now */
 669         return IOS_UNAVAILABLE;
 670 #else
 671         initGroupSourceReq(env, group, index, source, &req);
 672         opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
 673         optval = (void*)&req;
 674         optlen = sizeof(req);
 675 #endif
 676     }
 677 
 678     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
 679     if (n < 0) {
 680         if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 681             return IOS_UNAVAILABLE;
 682         handleSocketError(env, errno);
 683     }
 684     return 0;
 685 }
 686 
 687 JNIEXPORT jint JNICALL
 688 Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
 689                                     jbyteArray group, jint index, jbyteArray source)
 690 {
 691 #ifdef __APPLE__
 692     /* no IPv6 exclude-mode filtering for now */
 693     return IOS_UNAVAILABLE;
 694 #else
 695     struct group_source_req req;
 696     int n;
 697     int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
 698 
 699     initGroupSourceReq(env, group, index, source, &req);
 700 
 701     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt,
 702         (void*)&req, sizeof(req));
 703     if (n < 0) {
 704         if (block && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 705             return IOS_UNAVAILABLE;
 706         handleSocketError(env, errno);
 707     }
 708     return 0;
 709 #endif
 710 }
 711 
 712 JNIEXPORT void JNICALL
 713 Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint interf)
 714 {
 715     struct in_addr in;
 716     socklen_t arglen = sizeof(struct in_addr);
 717     int n;
 718 
 719     in.s_addr = htonl(interf);
 720 
 721     n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
 722                    (void*)&(in.s_addr), arglen);
 723     if (n < 0) {
 724         handleSocketError(env, errno);
 725     }
 726 }
 727 
 728 JNIEXPORT jint JNICALL
 729 Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
 730 {
 731     struct in_addr in;
 732     socklen_t arglen = sizeof(struct in_addr);
 733     int n;
 734 
 735     n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
 736     if (n < 0) {
 737         handleSocketError(env, errno);
 738         return -1;
 739     }
 740     return ntohl(in.s_addr);
 741 }
 742 
 743 JNIEXPORT void JNICALL
 744 Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint index)
 745 {
 746     int value = (jint)index;
 747     socklen_t arglen = sizeof(value);
 748     int n;
 749 
 750     n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
 751                    (void*)&(index), arglen);
 752     if (n < 0) {
 753         handleSocketError(env, errno);
 754     }
 755 }
 756 
 757 JNIEXPORT jint JNICALL
 758 Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
 759 {
 760     int index;
 761     socklen_t arglen = sizeof(index);
 762     int n;
 763 
 764     n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
 765     if (n < 0) {
 766         handleSocketError(env, errno);
 767         return -1;
 768     }
 769     return (jint)index;
 770 }
 771 
 772 JNIEXPORT void JNICALL
 773 Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow)
 774 {
 775     int how = (jhow == sun_nio_ch_Net_SHUT_RD) ? SHUT_RD :
 776         (jhow == sun_nio_ch_Net_SHUT_WR) ? SHUT_WR : SHUT_RDWR;
 777     if ((shutdown(fdval(env, fdo), how) < 0) && (errno != ENOTCONN))
 778         handleSocketError(env, errno);
 779 }
 780 
 781 JNIEXPORT jint JNICALL
 782 Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
 783 {
 784     int count = 0;
 785     if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
 786         handleSocketError(env, errno);
 787         return IOS_THROWN;
 788     }
 789     return (jint) count;
 790 }
 791 
 792 JNIEXPORT jint JNICALL
 793 Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlong timeout)
 794 {
 795     struct pollfd pfd;
 796     int rv;
 797     pfd.fd = fdval(env, fdo);
 798     pfd.events = events;
 799     if (timeout < -1) {
 800         timeout = -1;
 801     } else if (timeout > INT_MAX) {
 802         timeout = INT_MAX;
 803     }
 804     rv = poll(&pfd, 1, (int)timeout);
 805 
 806     if (rv >= 0) {
 807         return pfd.revents;
 808     } else if (errno == EINTR) {
 809         // interrupted, no events to return
 810         return 0;
 811     } else {
 812         handleSocketError(env, errno);
 813         return IOS_THROWN;
 814     }
 815 }
 816 
 817 JNIEXPORT jboolean JNICALL
 818 Java_sun_nio_ch_Net_pollConnect(JNIEnv *env, jobject this, jobject fdo, jlong timeout)
 819 {
 820     jint fd = fdval(env, fdo);
 821     struct pollfd poller;
 822     int result;
 823 
 824     poller.fd = fd;
 825     poller.events = POLLOUT;
 826     poller.revents = 0;
 827     if (timeout < -1) {
 828         timeout = -1;
 829     } else if (timeout > INT_MAX) {
 830         timeout = INT_MAX;
 831     }
 832 
 833     result = poll(&poller, 1, (int)timeout);
 834 
 835     if (result > 0) {
 836         int error = 0;
 837         socklen_t n = sizeof(int);
 838         errno = 0;
 839         result = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n);
 840         if (result < 0) {
 841             handleSocketError(env, errno);
 842             return JNI_FALSE;
 843         } else if (error) {
 844             handleSocketError(env, error);
 845             return JNI_FALSE;
 846         } else if ((poller.revents & POLLHUP) != 0) {
 847             handleSocketError(env, ENOTCONN);
 848             return JNI_FALSE;
 849         }
 850         // connected
 851         return JNI_TRUE;
 852     } else if (result == 0 || errno == EINTR) {
 853         return JNI_FALSE;
 854     } else {
 855         JNU_ThrowIOExceptionWithLastError(env, "poll failed");
 856         return JNI_FALSE;
 857     }
 858 }
 859 
 860 JNIEXPORT jshort JNICALL
 861 Java_sun_nio_ch_Net_pollinValue(JNIEnv *env, jclass this)
 862 {
 863     return (jshort)POLLIN;
 864 }
 865 
 866 JNIEXPORT jshort JNICALL
 867 Java_sun_nio_ch_Net_polloutValue(JNIEnv *env, jclass this)
 868 {
 869     return (jshort)POLLOUT;
 870 }
 871 
 872 JNIEXPORT jshort JNICALL
 873 Java_sun_nio_ch_Net_pollerrValue(JNIEnv *env, jclass this)
 874 {
 875     return (jshort)POLLERR;
 876 }
 877 
 878 JNIEXPORT jshort JNICALL
 879 Java_sun_nio_ch_Net_pollhupValue(JNIEnv *env, jclass this)
 880 {
 881     return (jshort)POLLHUP;
 882 }
 883 
 884 JNIEXPORT jshort JNICALL
 885 Java_sun_nio_ch_Net_pollnvalValue(JNIEnv *env, jclass this)
 886 {
 887     return (jshort)POLLNVAL;
 888 }
 889 
 890 JNIEXPORT jshort JNICALL
 891 Java_sun_nio_ch_Net_pollconnValue(JNIEnv *env, jclass this)
 892 {
 893     return (jshort)POLLOUT;
 894 }
 895 
 896 JNIEXPORT jint JNICALL
 897 Java_sun_nio_ch_Net_sendOOB(JNIEnv* env, jclass this, jobject fdo, jbyte b)
 898 {
 899     int n = send(fdval(env, fdo), (const void*)&b, 1, MSG_OOB);
 900     return convertReturnVal(env, n, JNI_FALSE);
 901 }
 902 
 903 /* Declared in nio_util.h */
 904 
 905 jint handleSocketError(JNIEnv *env, jint errorValue)
 906 {
 907     char *xn;
 908     switch (errorValue) {
 909         case EINPROGRESS:       /* Non-blocking connect */
 910             return 0;
 911 #ifdef EPROTO
 912         case EPROTO:
 913             xn = JNU_JAVANETPKG "ProtocolException";
 914             break;
 915 #endif
 916         case ECONNREFUSED:
 917         case ETIMEDOUT:
 918         case ENOTCONN:
 919             xn = JNU_JAVANETPKG "ConnectException";
 920             break;
 921 
 922         case EHOSTUNREACH:
 923             xn = JNU_JAVANETPKG "NoRouteToHostException";
 924             break;
 925         case EADDRINUSE:  /* Fall through */
 926         case EADDRNOTAVAIL:
 927         case EACCES:
 928             xn = JNU_JAVANETPKG "BindException";
 929             break;
 930         default:
 931             xn = JNU_JAVANETPKG "SocketException";
 932             break;
 933     }
 934     errno = errorValue;
 935     JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
 936     return IOS_THROWN;
 937 }