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 JNIEXPORT jint JNICALL
 207 Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
 208                             jboolean stream, jboolean reuse, jboolean ignored)
 209 {
 210     int fd;
 211     int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
 212     int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
 213 
 214     fd = socket(domain, type, 0);
 215     if (fd < 0) {
 216         return handleSocketError(env, errno);
 217     }
 218 
 219     /*
 220      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 221      */
 222     if (domain == AF_INET6 && ipv4_available()) {
 223         int arg = 0;
 224         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 225                        sizeof(int)) < 0) {
 226             JNU_ThrowByNameWithLastError(env,
 227                                          JNU_JAVANETPKG "SocketException",
 228                                          "Unable to set IPV6_V6ONLY");
 229             close(fd);
 230             return -1;
 231         }
 232     }
 233 
 234     if (reuse) {
 235         int arg = 1;
 236         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
 237                        sizeof(arg)) < 0) {
 238             JNU_ThrowByNameWithLastError(env,
 239                                          JNU_JAVANETPKG "SocketException",
 240                                          "Unable to set SO_REUSEADDR");
 241             close(fd);
 242             return -1;
 243         }
 244     }
 245 
 246 #if defined(__linux__)
 247     if (type == SOCK_DGRAM) {
 248         int arg = 0;
 249         int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
 250         if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
 251             (errno != ENOPROTOOPT)) {
 252             JNU_ThrowByNameWithLastError(env,
 253                                          JNU_JAVANETPKG "SocketException",
 254                                          "Unable to set IP_MULTICAST_ALL");
 255             close(fd);
 256             return -1;
 257         }
 258     }
 259 
 260     /* By default, Linux uses the route default */
 261     if (domain == AF_INET6 && type == SOCK_DGRAM) {
 262         int arg = 1;
 263         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &arg,
 264                        sizeof(arg)) < 0) {
 265             JNU_ThrowByNameWithLastError(env,
 266                                          JNU_JAVANETPKG "SocketException",
 267                                          "Unable to set IPV6_MULTICAST_HOPS");
 268             close(fd);
 269             return -1;
 270         }
 271     }
 272 #endif
 273     return fd;
 274 }
 275 
 276 JNIEXPORT void JNICALL
 277 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6,
 278                           jboolean useExclBind, jobject iao, int port, int scopeId)
 279 {
 280     SOCKETADDRESS sa;
 281     int sa_len = 0;
 282     int rv = 0;
 283 
 284     if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len,
 285                                   preferIPv6) != 0) {
 286         return;
 287     }
 288     if (scopeId != -1) { // use the explicit scopeId, if there is one
 289         ((struct sockaddr_in6 *)&sa.sa)->sin6_scope_id = scopeId;
 290     }
 291 
 292     rv = NET_Bind(fdval(env, fdo), &sa, sa_len);
 293     if (rv != 0) {
 294         handleSocketError(env, errno);
 295     }
 296 }
 297 
 298 JNIEXPORT void JNICALL
 299 Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
 300 {
 301     if (listen(fdval(env, fdo), backlog) < 0)
 302         handleSocketError(env, errno);
 303 }
 304 
 305 JNIEXPORT jint JNICALL
 306 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
 307                              jobject fdo, jobject iao, jint port)
 308 {
 309     SOCKETADDRESS sa;
 310     int sa_len = 0;
 311     int rv;
 312 
 313     if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len, preferIPv6) != 0) {
 314         return IOS_THROWN;
 315     }
 316 
 317     rv = connect(fdval(env, fdo), &sa.sa, sa_len);
 318     if (rv != 0) {
 319         if (errno == EINPROGRESS) {
 320             return IOS_UNAVAILABLE;
 321         } else if (errno == EINTR) {
 322             return IOS_INTERRUPTED;
 323         }
 324         return handleSocketError(env, errno);
 325     }
 326     return 1;
 327 }
 328 
 329 JNIEXPORT jint JNICALL
 330 Java_sun_nio_ch_Net_accept(JNIEnv *env, jclass clazz, jobject fdo, jobject newfdo,
 331                            jobjectArray isaa)
 332 {
 333     jint fd = fdval(env, fdo);
 334     jint newfd;
 335     SOCKETADDRESS sa;
 336     socklen_t sa_len = sizeof(SOCKETADDRESS);
 337     jobject remote_ia;
 338     jint remote_port = 0;
 339     jobject isa;
 340 
 341     /* accept connection but ignore ECONNABORTED */
 342     for (;;) {
 343         newfd = accept(fd, &sa.sa, &sa_len);
 344         if (newfd >= 0) {
 345             break;
 346         }
 347         if (errno != ECONNABORTED) {
 348             break;
 349         }
 350         /* ECONNABORTED => restart accept */
 351     }
 352 
 353     if (newfd < 0) {
 354         if (errno == EAGAIN || errno == EWOULDBLOCK)
 355             return IOS_UNAVAILABLE;
 356         if (errno == EINTR)
 357             return IOS_INTERRUPTED;
 358         JNU_ThrowIOExceptionWithLastError(env, "Accept failed");
 359         return IOS_THROWN;
 360     }
 361 
 362     setfdval(env, newfdo, newfd);
 363 
 364     remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
 365     CHECK_NULL_RETURN(remote_ia, IOS_THROWN);
 366 
 367     isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port);
 368     CHECK_NULL_RETURN(isa, IOS_THROWN);
 369     (*env)->SetObjectArrayElement(env, isaa, 0, isa);
 370 
 371     return 1;
 372 }
 373 
 374 JNIEXPORT jint JNICALL
 375 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
 376 {
 377     SOCKETADDRESS sa;
 378     socklen_t sa_len = sizeof(SOCKETADDRESS);
 379     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 380 #ifdef _ALLBSD_SOURCE
 381         /*
 382          * XXXBSD:
 383          * ECONNRESET is specific to the BSDs. We can not return an error,
 384          * as the calling Java code with raise a java.lang.Error given the expectation
 385          * that getsockname() will never fail. According to the Single UNIX Specification,
 386          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 387          */
 388         if (errno == ECONNRESET) {
 389             bzero(&sa.sa4, sizeof(sa));
 390             sa.sa4.sin_len = sizeof(struct sockaddr_in);
 391             sa.sa4.sin_family = AF_INET;
 392             sa.sa4.sin_port = htonl(0);
 393             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 394         } else {
 395             handleSocketError(env, errno);
 396             return -1;
 397         }
 398 #else /* _ALLBSD_SOURCE */
 399         handleSocketError(env, errno);
 400         return -1;
 401 #endif /* _ALLBSD_SOURCE */
 402     }
 403     return NET_GetPortFromSockaddr(&sa);
 404 }
 405 
 406 JNIEXPORT jobject JNICALL
 407 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 408 {
 409     SOCKETADDRESS sa;
 410     socklen_t sa_len = sizeof(SOCKETADDRESS);
 411     int port;
 412     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 413 #ifdef _ALLBSD_SOURCE
 414         /*
 415          * XXXBSD:
 416          * ECONNRESET is specific to the BSDs. We can not return an error,
 417          * as the calling Java code with raise a java.lang.Error with the expectation
 418          * that getsockname() will never fail. According to the Single UNIX Specification,
 419          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 420          */
 421         if (errno == ECONNRESET) {
 422             bzero(&sa.sa4, sizeof(sa));
 423             sa.sa4.sin_len  = sizeof(struct sockaddr_in);
 424             sa.sa4.sin_family = AF_INET;
 425             sa.sa4.sin_port = htonl(0);
 426             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 427         } else {
 428             handleSocketError(env, errno);
 429             return NULL;
 430         }
 431 #else /* _ALLBSD_SOURCE */
 432         handleSocketError(env, errno);
 433         return NULL;
 434 #endif /* _ALLBSD_SOURCE */
 435     }
 436     return NET_SockaddrToInetAddress(env, &sa, &port);
 437 }
 438 
 439 JNIEXPORT jint JNICALL
 440 Java_sun_nio_ch_Net_remotePort(JNIEnv *env, jclass clazz, jobject fdo)
 441 {
 442     SOCKETADDRESS sa;
 443     socklen_t sa_len = sizeof(sa);
 444 
 445     if (getpeername(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 446         handleSocketError(env, errno);
 447         return IOS_THROWN;
 448     }
 449     return NET_GetPortFromSockaddr(&sa);
 450 }
 451 
 452 JNIEXPORT jobject JNICALL
 453 Java_sun_nio_ch_Net_remoteInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 454 {
 455     SOCKETADDRESS sa;
 456     socklen_t sa_len = sizeof(sa);
 457     int port;
 458 
 459     if (getpeername(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 460         handleSocketError(env, errno);
 461         return NULL;
 462     }
 463     return NET_SockaddrToInetAddress(env, &sa, &port);
 464 }
 465 
 466 JNIEXPORT jint JNICALL
 467 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 468                                   jboolean mayNeedConversion, jint level, jint opt)
 469 {
 470     int result;
 471     struct linger linger;
 472     u_char carg;
 473     void *arg;
 474     socklen_t arglen;
 475     int n;
 476 
 477     /* Option value is an int except for a few specific cases */
 478 
 479     arg = (void *)&result;
 480     arglen = sizeof(result);
 481 
 482     if (level == IPPROTO_IP &&
 483         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
 484         arg = (void*)&carg;
 485         arglen = sizeof(carg);
 486     }
 487 
 488     if (level == SOL_SOCKET && opt == SO_LINGER) {
 489         arg = (void *)&linger;
 490         arglen = sizeof(linger);
 491     }
 492 
 493     if (mayNeedConversion) {
 494         n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, (int*)&arglen);
 495     } else {
 496         n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
 497     }
 498     if (n < 0) {
 499         JNU_ThrowByNameWithLastError(env,
 500                                      JNU_JAVANETPKG "SocketException",
 501                                      "sun.nio.ch.Net.getIntOption");
 502         return -1;
 503     }
 504 
 505     if (level == IPPROTO_IP &&
 506         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP))
 507     {
 508         return (jint)carg;
 509     }
 510 
 511     if (level == SOL_SOCKET && opt == SO_LINGER)
 512         return linger.l_onoff ? (jint)linger.l_linger : (jint)-1;
 513 
 514     return (jint)result;
 515 }
 516 
 517 JNIEXPORT void JNICALL
 518 Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 519                                   jboolean mayNeedConversion, jint level,
 520                                   jint opt, jint arg, jboolean isIPv6)
 521 {
 522     int result;
 523     struct linger linger;
 524     u_char carg;
 525     void *parg;
 526     socklen_t arglen;
 527     int n;
 528 
 529     /* Option value is an int except for a few specific cases */
 530 
 531     parg = (void*)&arg;
 532     arglen = sizeof(arg);
 533 
 534     if (level == IPPROTO_IP &&
 535         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
 536         parg = (void*)&carg;
 537         arglen = sizeof(carg);
 538         carg = (u_char)arg;
 539     }
 540 
 541     if (level == SOL_SOCKET && opt == SO_LINGER) {
 542         parg = (void *)&linger;
 543         arglen = sizeof(linger);
 544         if (arg >= 0) {
 545             linger.l_onoff = 1;
 546             linger.l_linger = arg;
 547         } else {
 548             linger.l_onoff = 0;
 549             linger.l_linger = 0;
 550         }
 551     }
 552 
 553     if (mayNeedConversion) {
 554         n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
 555     } else {
 556         n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
 557     }
 558     if (n < 0) {
 559         JNU_ThrowByNameWithLastError(env,
 560                                      JNU_JAVANETPKG "SocketException",
 561                                      "sun.nio.ch.Net.setIntOption");
 562     }
 563 #ifdef __linux__
 564     if (level == IPPROTO_IPV6 && opt == IPV6_TCLASS && isIPv6) {
 565         // set the V4 option also
 566         setsockopt(fdval(env, fdo), IPPROTO_IP, IP_TOS, parg, arglen);
 567     }
 568 #endif
 569 }
 570 
 571 JNIEXPORT jint JNICALL
 572 Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 573                                 jint group, jint interf, jint source)
 574 {
 575     struct ip_mreq mreq;
 576     struct ip_mreq_source mreq_source;
 577     int opt, n, optlen;
 578     void* optval;
 579 
 580     if (source == 0) {
 581         mreq.imr_multiaddr.s_addr = htonl(group);
 582         mreq.imr_interface.s_addr = htonl(interf);
 583         opt = (join) ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
 584         optval = (void*)&mreq;
 585         optlen = sizeof(mreq);
 586     } else {
 587 
 588 #ifdef _AIX
 589         /* check AIX for support of source filtering */
 590         if (isSourceFilterSupported() != JNI_TRUE){
 591             return IOS_UNAVAILABLE;
 592         }
 593 #endif
 594 
 595         mreq_source.imr_multiaddr.s_addr = htonl(group);
 596         mreq_source.imr_sourceaddr.s_addr = htonl(source);
 597         mreq_source.imr_interface.s_addr = htonl(interf);
 598         opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
 599         optval = (void*)&mreq_source;
 600         optlen = sizeof(mreq_source);
 601     }
 602 
 603     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
 604     if (n < 0) {
 605         if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 606             return IOS_UNAVAILABLE;
 607         handleSocketError(env, errno);
 608     }
 609     return 0;
 610 }
 611 
 612 JNIEXPORT jint JNICALL
 613 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
 614                                     jint group, jint interf, jint source)
 615 {
 616 #ifdef __APPLE__
 617     /* no IPv4 exclude-mode filtering for now */
 618     return IOS_UNAVAILABLE;
 619 #else
 620     struct ip_mreq_source mreq_source;
 621     int n;
 622     int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
 623 
 624 #ifdef _AIX
 625     /* check AIX for support of source filtering */
 626     if (isSourceFilterSupported() != JNI_TRUE){
 627         return IOS_UNAVAILABLE;
 628     }
 629 #endif
 630 
 631     mreq_source.imr_multiaddr.s_addr = htonl(group);
 632     mreq_source.imr_sourceaddr.s_addr = htonl(source);
 633     mreq_source.imr_interface.s_addr = htonl(interf);
 634 
 635     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt,
 636                    (void*)&mreq_source, sizeof(mreq_source));
 637     if (n < 0) {
 638         if (block && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 639             return IOS_UNAVAILABLE;
 640         handleSocketError(env, errno);
 641     }
 642     return 0;
 643 #endif
 644 }
 645 
 646 JNIEXPORT jint JNICALL
 647 Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 648                                 jbyteArray group, jint index, jbyteArray source)
 649 {
 650     struct ipv6_mreq mreq6;
 651     struct group_source_req req;
 652     int opt, n, optlen;
 653     void* optval;
 654 
 655     if (source == NULL) {
 656         COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
 657         mreq6.ipv6mr_interface = (int)index;
 658         opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
 659         optval = (void*)&mreq6;
 660         optlen = sizeof(mreq6);
 661     } else {
 662 #ifdef __APPLE__
 663         /* no IPv6 include-mode filtering for now */
 664         return IOS_UNAVAILABLE;
 665 #else
 666         initGroupSourceReq(env, group, index, source, &req);
 667         opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
 668         optval = (void*)&req;
 669         optlen = sizeof(req);
 670 #endif
 671     }
 672 
 673     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
 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 
 694     initGroupSourceReq(env, group, index, source, &req);
 695 
 696     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt,
 697         (void*)&req, sizeof(req));
 698     if (n < 0) {
 699         if (block && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
 700             return IOS_UNAVAILABLE;
 701         handleSocketError(env, errno);
 702     }
 703     return 0;
 704 #endif
 705 }
 706 
 707 JNIEXPORT void JNICALL
 708 Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint interf)
 709 {
 710     struct in_addr in;
 711     socklen_t arglen = sizeof(struct in_addr);
 712     int n;
 713 
 714     in.s_addr = htonl(interf);
 715 
 716     n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
 717                    (void*)&(in.s_addr), arglen);
 718     if (n < 0) {
 719         handleSocketError(env, errno);
 720     }
 721 }
 722 
 723 JNIEXPORT jint JNICALL
 724 Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
 725 {
 726     struct in_addr in;
 727     socklen_t arglen = sizeof(struct in_addr);
 728     int n;
 729 
 730     n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
 731     if (n < 0) {
 732         handleSocketError(env, errno);
 733         return -1;
 734     }
 735     return ntohl(in.s_addr);
 736 }
 737 
 738 JNIEXPORT void JNICALL
 739 Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint index)
 740 {
 741     int value = (jint)index;
 742     socklen_t arglen = sizeof(value);
 743     int n;
 744 
 745     n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
 746                    (void*)&(index), arglen);
 747     if (n < 0) {
 748         handleSocketError(env, errno);
 749     }
 750 }
 751 
 752 JNIEXPORT jint JNICALL
 753 Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
 754 {
 755     int index;
 756     socklen_t arglen = sizeof(index);
 757     int n;
 758 
 759     n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
 760     if (n < 0) {
 761         handleSocketError(env, errno);
 762         return -1;
 763     }
 764     return (jint)index;
 765 }
 766 
 767 JNIEXPORT void JNICALL
 768 Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow)
 769 {
 770     int how = (jhow == sun_nio_ch_Net_SHUT_RD) ? SHUT_RD :
 771         (jhow == sun_nio_ch_Net_SHUT_WR) ? SHUT_WR : SHUT_RDWR;
 772     if ((shutdown(fdval(env, fdo), how) < 0) && (errno != ENOTCONN))
 773         handleSocketError(env, errno);
 774 }
 775 
 776 JNIEXPORT jint JNICALL
 777 Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
 778 {
 779     int count = 0;
 780     if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
 781         handleSocketError(env, errno);
 782         return IOS_THROWN;
 783     }
 784     return (jint) count;
 785 }
 786 
 787 JNIEXPORT jint JNICALL
 788 Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlong timeout)
 789 {
 790     struct pollfd pfd;
 791     int rv;
 792     pfd.fd = fdval(env, fdo);
 793     pfd.events = events;
 794     if (timeout < -1) {
 795         timeout = -1;
 796     } else if (timeout > INT_MAX) {
 797         timeout = INT_MAX;
 798     }
 799     rv = poll(&pfd, 1, (int)timeout);
 800 
 801     if (rv >= 0) {
 802         return pfd.revents;
 803     } else if (errno == EINTR) {
 804         // interrupted, no events to return
 805         return 0;
 806     } else {
 807         handleSocketError(env, errno);
 808         return IOS_THROWN;
 809     }
 810 }
 811 
 812 JNIEXPORT jboolean JNICALL
 813 Java_sun_nio_ch_Net_pollConnect(JNIEnv *env, jobject this, jobject fdo, jlong timeout)
 814 {
 815     jint fd = fdval(env, fdo);
 816     struct pollfd poller;
 817     int result;
 818 
 819     poller.fd = fd;
 820     poller.events = POLLOUT;
 821     poller.revents = 0;
 822     if (timeout < -1) {
 823         timeout = -1;
 824     } else if (timeout > INT_MAX) {
 825         timeout = INT_MAX;
 826     }
 827 
 828     result = poll(&poller, 1, (int)timeout);
 829 
 830     if (result > 0) {
 831         int error = 0;
 832         socklen_t n = sizeof(int);
 833         errno = 0;
 834         result = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n);
 835         if (result < 0) {
 836             handleSocketError(env, errno);
 837             return JNI_FALSE;
 838         } else if (error) {
 839             handleSocketError(env, error);
 840             return JNI_FALSE;
 841         } else if ((poller.revents & POLLHUP) != 0) {
 842             handleSocketError(env, ENOTCONN);
 843             return JNI_FALSE;
 844         }
 845         // connected
 846         return JNI_TRUE;
 847     } else if (result == 0 || errno == EINTR) {
 848         return JNI_FALSE;
 849     } else {
 850         JNU_ThrowIOExceptionWithLastError(env, "poll failed");
 851         return JNI_FALSE;
 852     }
 853 }
 854 
 855 JNIEXPORT jshort JNICALL
 856 Java_sun_nio_ch_Net_pollinValue(JNIEnv *env, jclass this)
 857 {
 858     return (jshort)POLLIN;
 859 }
 860 
 861 JNIEXPORT jshort JNICALL
 862 Java_sun_nio_ch_Net_polloutValue(JNIEnv *env, jclass this)
 863 {
 864     return (jshort)POLLOUT;
 865 }
 866 
 867 JNIEXPORT jshort JNICALL
 868 Java_sun_nio_ch_Net_pollerrValue(JNIEnv *env, jclass this)
 869 {
 870     return (jshort)POLLERR;
 871 }
 872 
 873 JNIEXPORT jshort JNICALL
 874 Java_sun_nio_ch_Net_pollhupValue(JNIEnv *env, jclass this)
 875 {
 876     return (jshort)POLLHUP;
 877 }
 878 
 879 JNIEXPORT jshort JNICALL
 880 Java_sun_nio_ch_Net_pollnvalValue(JNIEnv *env, jclass this)
 881 {
 882     return (jshort)POLLNVAL;
 883 }
 884 
 885 JNIEXPORT jshort JNICALL
 886 Java_sun_nio_ch_Net_pollconnValue(JNIEnv *env, jclass this)
 887 {
 888     return (jshort)POLLOUT;
 889 }
 890 
 891 JNIEXPORT jint JNICALL
 892 Java_sun_nio_ch_Net_sendOOB(JNIEnv* env, jclass this, jobject fdo, jbyte b)
 893 {
 894     int n = send(fdval(env, fdo), (const void*)&b, 1, MSG_OOB);
 895     return convertReturnVal(env, n, JNI_FALSE);
 896 }
 897 
 898 /* Declared in nio_util.h */
 899 
 900 jint handleSocketError(JNIEnv *env, jint errorValue)
 901 {
 902     char *xn;
 903     switch (errorValue) {
 904         case EINPROGRESS:       /* Non-blocking connect */
 905             return 0;
 906 #ifdef EPROTO
 907         case EPROTO:
 908             xn = JNU_JAVANETPKG "ProtocolException";
 909             break;
 910 #endif
 911         case ECONNREFUSED:
 912         case ETIMEDOUT:
 913         case ENOTCONN:
 914             xn = JNU_JAVANETPKG "ConnectException";
 915             break;
 916 
 917         case EHOSTUNREACH:
 918             xn = JNU_JAVANETPKG "NoRouteToHostException";
 919             break;
 920         case EADDRINUSE:  /* Fall through */
 921         case EADDRNOTAVAIL:
 922         case EACCES:
 923             xn = JNU_JAVANETPKG "BindException";
 924             break;
 925         default:
 926             xn = JNU_JAVANETPKG "SocketException";
 927             break;
 928     }
 929     errno = errorValue;
 930     JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
 931     return IOS_THROWN;
 932 }