1 /*
   2  * Copyright (c) 2001, 2008, 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 <windows.h>
  27 #include <winsock2.h>
  28 #include "jni.h"
  29 #include "jni_util.h"
  30 #include "jvm.h"
  31 #include "jlong.h"
  32 
  33 #include "nio.h"
  34 #include "nio_util.h"
  35 #include "net_util.h"
  36 
  37 #include "sun_nio_ch_Net.h"
  38 
  39 /**
  40  * Definitions to allow for building with older SDK include files.
  41  */
  42 
  43 #ifndef MCAST_BLOCK_SOURCE
  44 
  45 #define MCAST_BLOCK_SOURCE          43
  46 #define MCAST_UNBLOCK_SOURCE        44
  47 #define MCAST_JOIN_SOURCE_GROUP     45
  48 #define MCAST_LEAVE_SOURCE_GROUP    46
  49 
  50 #endif  /* MCAST_BLOCK_SOURCE */
  51 
  52 typedef struct my_ip_mreq_source {
  53     IN_ADDR imr_multiaddr;
  54     IN_ADDR imr_sourceaddr;
  55     IN_ADDR imr_interface;
  56 };
  57 
  58 typedef struct my_group_source_req {
  59     ULONG gsr_interface;
  60     SOCKADDR_STORAGE gsr_group;
  61     SOCKADDR_STORAGE gsr_source;
  62 };
  63 
  64 /**
  65  * Copy IPv6 address as jbytearray to target
  66  */
  67 #define COPY_INET6_ADDRESS(env, source, target) \
  68     (*env)->GetByteArrayRegion(env, source, 0, 16, target)
  69 
  70 /**
  71  * Enable or disable receipt of WSAECONNRESET errors.
  72  */
  73 static void setConnectionReset(SOCKET s, BOOL enable) {
  74     DWORD bytesReturned = 0;
  75     WSAIoctl(s, SIO_UDP_CONNRESET, &enable, sizeof(enable),
  76              NULL, 0, &bytesReturned, NULL, NULL);
  77 }
  78 
  79 
  80 JNIEXPORT void JNICALL
  81 Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz)
  82 {
  83     /* nothing to do */
  84 }
  85 
  86 JNIEXPORT jboolean JNICALL
  87 Java_sun_nio_ch_Net_isIPv6Available0(JNIEnv* env, jclass cl)
  88 {
  89     /*
  90      * Return true if Windows Vista or newer, and IPv6 is configured
  91      */
  92     OSVERSIONINFO ver;
  93     ver.dwOSVersionInfoSize = sizeof(ver);
  94     GetVersionEx(&ver);
  95     if ((ver.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  96         (ver.dwMajorVersion >= 6)  && ipv6_available())
  97     {
  98         return JNI_TRUE;
  99     }
 100     return JNI_FALSE;
 101 }
 102 
 103 JNIEXPORT jint JNICALL
 104 Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
 105                             jboolean stream, jboolean reuse)
 106 {
 107     SOCKET s;
 108     int domain = (preferIPv6) ? AF_INET6 : AF_INET;
 109 
 110     s = socket(domain, (stream ? SOCK_STREAM : SOCK_DGRAM), 0);
 111     if (s != INVALID_SOCKET) {
 112         SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
 113 
 114         /* IPV6_V6ONLY is true by default */
 115         if (domain == AF_INET6) {
 116             int opt = 0;
 117             setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
 118                        (const char *)&opt, sizeof(opt));
 119         }
 120 
 121         /* Disable WSAECONNRESET errors for initially unconnected UDP sockets */
 122         if (!stream) {
 123             setConnectionReset(s, FALSE);
 124         }
 125 
 126     } else {
 127         NET_ThrowNew(env, WSAGetLastError(), "socket");
 128     }
 129 
 130     return (jint)s;
 131 }
 132 
 133 JNIEXPORT void JNICALL
 134 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
 135                           jobject fdo, jobject iao, jint port)
 136 {
 137     SOCKETADDRESS sa;
 138     int rv;
 139     int sa_len;
 140 
 141     if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
 142       return;
 143     }
 144 
 145     rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
 146     if (rv == SOCKET_ERROR)
 147         NET_ThrowNew(env, WSAGetLastError(), "bind");
 148 }
 149 
 150 JNIEXPORT void JNICALL
 151 Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
 152 {
 153     if (listen(fdval(env,fdo), backlog) == SOCKET_ERROR) {
 154         NET_ThrowNew(env, WSAGetLastError(), "listen");
 155     }
 156 }
 157 
 158 
 159 JNIEXPORT jint JNICALL
 160 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6, jobject fdo,
 161                              jobject iao, jint port)
 162 {
 163     SOCKETADDRESS sa;
 164     int rv;
 165     int sa_len;
 166     SOCKET s = (SOCKET)fdval(env, fdo);
 167 
 168     if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
 169         return IOS_THROWN;
 170     }
 171 
 172     rv = connect(s, (struct sockaddr *)&sa, sa_len);
 173     if (rv != 0) {
 174         int err = WSAGetLastError();
 175         if (err == WSAEINPROGRESS || err == WSAEWOULDBLOCK) {
 176             return IOS_UNAVAILABLE;
 177         }
 178         NET_ThrowNew(env, err, "connect");
 179         return IOS_THROWN;
 180     } else {
 181         /* Enable WSAECONNRESET errors when a UDP socket is connected */
 182         int type = 0, optlen = sizeof(type);
 183         rv = getsockopt(s, SOL_SOCKET, SO_TYPE, (char*)&type, &optlen);
 184         if (rv == 0 && type == SOCK_DGRAM) {
 185             setConnectionReset(s, TRUE);
 186         }
 187     }
 188     return 1;
 189 }
 190 
 191 JNIEXPORT jint JNICALL
 192 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
 193 {
 194     SOCKETADDRESS sa;
 195     int sa_len = sizeof(sa);
 196 
 197     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
 198         int error = WSAGetLastError();
 199         if (error == WSAEINVAL) {
 200             return 0;
 201         }
 202         NET_ThrowNew(env, error, "getsockname");
 203         return IOS_THROWN;
 204     }
 205     return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
 206 }
 207 
 208 JNIEXPORT jobject JNICALL
 209 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 210 {
 211     SOCKETADDRESS sa;
 212     int sa_len = sizeof(sa);
 213     int port;
 214 
 215     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
 216         NET_ThrowNew(env, WSAGetLastError(), "getsockname");
 217         return NULL;
 218     }
 219     return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
 220 }
 221 
 222 JNIEXPORT jint JNICALL
 223 Java_sun_nio_ch_Net_remotePort(JNIEnv *env, jclass clazz, jobject fdo)
 224 {
 225     SOCKETADDRESS sa;
 226     int sa_len = sizeof(sa);
 227 
 228     if (getpeername(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
 229         int error = WSAGetLastError();
 230         if (error == WSAEINVAL) {
 231             return 0;
 232         }
 233         NET_ThrowNew(env, error, "getsockname");
 234         return IOS_THROWN;
 235     }
 236     return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
 237 }
 238 
 239 JNIEXPORT jobject JNICALL
 240 Java_sun_nio_ch_Net_remoteInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 241 {
 242     SOCKETADDRESS sa;
 243     int sa_len = sizeof(sa);
 244     int port;
 245 
 246     if (getpeername(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
 247         NET_ThrowNew(env, WSAGetLastError(), "getsockname");
 248         return NULL;
 249     }
 250     return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
 251 }
 252 
 253 JNIEXPORT jint JNICALL
 254 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 255                                   jboolean mayNeedConversion, jint level, jint opt)
 256 {
 257     int result = 0;
 258     struct linger linger;
 259     char *arg;
 260     int arglen, n;
 261 
 262     if (level == SOL_SOCKET && opt == SO_LINGER) {
 263         arg = (char *)&linger;
 264         arglen = sizeof(linger);
 265     } else {
 266         arg = (char *)&result;
 267         arglen = sizeof(result);
 268     }
 269 
 270     /**
 271      * HACK: IP_TOS is deprecated on Windows and querying the option
 272      * returns a protocol error. NET_GetSockOpt handles this and uses
 273      * a fallback mechanism.
 274      */
 275     if (level == IPPROTO_IP && opt == IP_TOS) {
 276         mayNeedConversion = JNI_TRUE;
 277     }
 278 
 279     if (mayNeedConversion) {
 280         n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, &arglen);
 281     } else {
 282         n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
 283     }
 284     if (n < 0) {
 285         handleSocketError(env, WSAGetLastError());
 286         return IOS_THROWN;
 287     }
 288 
 289     if (level == SOL_SOCKET && opt == SO_LINGER)
 290         return linger.l_onoff ? linger.l_linger : -1;
 291     else
 292         return result;
 293 }
 294 
 295 JNIEXPORT void JNICALL
 296 Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
 297                                   jboolean mayNeedConversion, jint level, jint opt, jint arg)
 298 {
 299     struct linger linger;
 300     char *parg;
 301     int arglen, n;
 302 
 303     if (level == SOL_SOCKET && opt == SO_LINGER) {
 304         parg = (char *)&linger;
 305         arglen = sizeof(linger);
 306         if (arg >= 0) {
 307             linger.l_onoff = 1;
 308             linger.l_linger = (unsigned short)arg;
 309         } else {
 310             linger.l_onoff = 0;
 311             linger.l_linger = 0;
 312         }
 313     } else {
 314         parg = (char *)&arg;
 315         arglen = sizeof(arg);
 316     }
 317 
 318     if (mayNeedConversion) {
 319         n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
 320     } else {
 321         n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
 322     }
 323     if (n < 0)
 324         handleSocketError(env, WSAGetLastError());
 325 }
 326 
 327 JNIEXPORT jint JNICALL
 328 Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 329                                 jint group, jint interf, jint source)
 330 {
 331     struct ip_mreq mreq;
 332     struct my_ip_mreq_source mreq_source;
 333     int opt, n, optlen;
 334     void* optval;
 335 
 336     if (source == 0) {
 337         mreq.imr_multiaddr.s_addr = htonl(group);
 338         mreq.imr_interface.s_addr = htonl(interf);
 339         opt = (join) ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
 340         optval = (void*)&mreq;
 341         optlen = sizeof(mreq);
 342     } else {
 343         mreq_source.imr_multiaddr.s_addr = htonl(group);
 344         mreq_source.imr_sourceaddr.s_addr = htonl(source);
 345         mreq_source.imr_interface.s_addr = htonl(interf);
 346         opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
 347         optval = (void*)&mreq_source;
 348         optlen = sizeof(mreq_source);
 349     }
 350 
 351     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
 352     if (n < 0) {
 353         if (join && (WSAGetLastError() == WSAENOPROTOOPT))
 354             return IOS_UNAVAILABLE;
 355         handleSocketError(env, WSAGetLastError());
 356     }
 357     return 0;
 358 }
 359 
 360 JNIEXPORT jint JNICALL
 361 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
 362                                    jint group, jint interf, jint source)
 363 {
 364     struct my_ip_mreq_source mreq_source;
 365     int n;
 366     int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
 367 
 368     mreq_source.imr_multiaddr.s_addr = htonl(group);
 369     mreq_source.imr_sourceaddr.s_addr = htonl(source);
 370     mreq_source.imr_interface.s_addr = htonl(interf);
 371 
 372     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt,
 373                    (void*)&mreq_source, sizeof(mreq_source));
 374     if (n < 0) {
 375         if (block && (WSAGetLastError() == WSAENOPROTOOPT))
 376             return IOS_UNAVAILABLE;
 377         handleSocketError(env, WSAGetLastError());
 378     }
 379     return 0;
 380 }
 381 
 382 /**
 383  * Call setsockopt with a IPPROTO_IPV6 level socket option
 384  * and a group_source_req structure as the option value. The
 385  * given IPv6 group, interface index, and IPv6 source address
 386  * are copied into the structure.
 387  */
 388 static int setGroupSourceReqOption(JNIEnv* env,
 389                                    jobject fdo,
 390                                    int opt,
 391                                    jbyteArray group,
 392                                    jint index,
 393                                    jbyteArray source)
 394 {
 395     struct my_group_source_req req;
 396     struct sockaddr_in6* sin6;
 397 
 398     req.gsr_interface = (ULONG)index;
 399 
 400     sin6 = (struct sockaddr_in6*)&(req.gsr_group);
 401     sin6->sin6_family = AF_INET6;
 402     COPY_INET6_ADDRESS(env, group, (jbyte*)&(sin6->sin6_addr));
 403 
 404     sin6 = (struct sockaddr_in6*)&(req.gsr_source);
 405     sin6->sin6_family = AF_INET6;
 406     COPY_INET6_ADDRESS(env, source, (jbyte*)&(sin6->sin6_addr));
 407 
 408     return setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, (void*)&req, sizeof(req));
 409 }
 410 
 411 JNIEXPORT jint JNICALL
 412 Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
 413                                 jbyteArray group, jint index, jbyteArray source)
 414 {
 415     struct ipv6_mreq mreq6;
 416     int n;
 417 
 418     if (source == NULL) {
 419         int opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
 420         COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
 421         mreq6.ipv6mr_interface = (int)index;
 422         n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt,
 423                        (void*)&mreq6, sizeof(mreq6));
 424     } else {
 425         int opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
 426         n = setGroupSourceReqOption(env, fdo, opt, group, index, source);
 427     }
 428 
 429     if (n < 0) {
 430         handleSocketError(env, errno);
 431     }
 432     return 0;
 433 }
 434 
 435 JNIEXPORT jint JNICALL
 436 Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
 437                                     jbyteArray group, jint index, jbyteArray source)
 438 {
 439     int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
 440     int n = setGroupSourceReqOption(env, fdo, opt, group, index, source);
 441     if (n < 0) {
 442         handleSocketError(env, errno);
 443     }
 444     return 0;
 445 }
 446 
 447 JNIEXPORT void JNICALL
 448 Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint interf)
 449 {
 450     struct in_addr in;
 451     int arglen = sizeof(struct in_addr);
 452     int n;
 453 
 454     in.s_addr = htonl(interf);
 455 
 456     n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
 457                    (void*)&(in.s_addr), arglen);
 458     if (n < 0) {
 459         handleSocketError(env, WSAGetLastError());
 460     }
 461 }
 462 
 463 JNIEXPORT jint JNICALL
 464 Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
 465 {
 466     struct in_addr in;
 467     int arglen = sizeof(struct in_addr);
 468     int n;
 469 
 470     n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
 471     if (n < 0) {
 472         handleSocketError(env, WSAGetLastError());
 473         return IOS_THROWN;
 474     }
 475     return ntohl(in.s_addr);
 476 }
 477 
 478 JNIEXPORT void JNICALL
 479 Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint index)
 480 {
 481     int value = (jint)index;
 482     int arglen = sizeof(value);
 483     int n;
 484 
 485     n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
 486                    (void*)&(index), arglen);
 487     if (n < 0) {
 488         handleSocketError(env, errno);
 489     }
 490 }
 491 
 492 JNIEXPORT jint JNICALL
 493 Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
 494 {
 495     int index;
 496     int arglen = sizeof(index);
 497     int n;
 498 
 499     n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
 500     if (n < 0) {
 501         handleSocketError(env, errno);
 502         return -1;
 503     }
 504     return (jint)index;
 505 }
 506 
 507 JNIEXPORT void JNICALL
 508 Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow) {
 509     int how = (jhow == sun_nio_ch_Net_SHUT_RD) ? SD_RECEIVE :
 510         (jhow == sun_nio_ch_Net_SHUT_WR) ? SD_SEND : SD_BOTH;
 511     if (shutdown(fdval(env, fdo), how) == SOCKET_ERROR) {
 512         NET_ThrowNew(env, WSAGetLastError(), "shutdown");
 513     }
 514 }