< prev index next >

src/java.base/windows/native/libnet/DualStackPlainDatagramSocketImpl.c

Print this page
rev 15720 : 8166850: No runtime error expected after calling NET_MapSocketOption
   1 /*
   2  * Copyright (c) 2007, 2015, 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


 458 
 459     (*env)->GetByteArrayRegion(env, data, offset, length,
 460                                (jbyte *)fullPacket);
 461     rv = sendto(fd, fullPacket, length, 0, (struct sockaddr *)sap, sa_len);
 462     if (rv == SOCKET_ERROR) {
 463         if (rv == -1) {
 464             NET_ThrowNew(env, WSAGetLastError(), "Datagram send failed");
 465         }
 466     }
 467 
 468     if (length > MAX_BUFFER_LEN) {
 469         free(fullPacket);
 470     }
 471 }
 472 
 473 /*
 474  * Class:     java_net_DualStackPlainDatagramSocketImpl
 475  * Method:    socketSetIntOption
 476  * Signature: (III)V
 477  */
 478 JNIEXPORT void JNICALL Java_java_net_DualStackPlainDatagramSocketImpl_socketSetIntOption
 479   (JNIEnv *env, jclass clazz, jint fd , jint cmd, jint value) {


 480     int level = 0, opt = 0;
 481 
 482     if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
 483         JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 484                                      "Invalid option");
 485         return;
 486     }
 487 
 488     if (NET_SetSockOpt(fd, level, opt, (char *)&value, sizeof(value)) < 0) {
 489         NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
 490     }
 491 }
 492 
 493 /*
 494  * Class:     java_net_DualStackPlainDatagramSocketImpl
 495  * Method:    socketGetIntOption
 496  * Signature: (II)I
 497  */
 498 JNIEXPORT jint JNICALL Java_java_net_DualStackPlainDatagramSocketImpl_socketGetIntOption
 499   (JNIEnv *env, jclass clazz, jint fd, jint cmd) {
 500     int level = 0, opt = 0, result=0;


 501     int result_len = sizeof(result);
 502 
 503     if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
 504         JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 505                                      "Invalid option");
 506         return -1;
 507     }
 508 
 509     if (NET_GetSockOpt(fd, level, opt, (void *)&result, &result_len) < 0) {
 510         NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
 511         return -1;
 512     }
 513 
 514     return result;
 515 }
 516 
 517 /*
 518  * Class:     java_net_DualStackPlainDatagramSocketImpl
 519  * Method:    dataAvailable
 520  * Signature: ()I
 521  */
 522 JNIEXPORT jint JNICALL Java_java_net_DualStackPlainDatagramSocketImpl_dataAvailable
 523 (JNIEnv *env, jobject this) {


 524     SOCKET fd;
 525     int  rv = -1;
 526     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 527 
 528     if (!IS_NULL(fdObj)) {
 529         int retval = 0;
 530         fd = (SOCKET)(*env)->GetIntField(env, fdObj, IO_fd_fdID);
 531         rv = ioctlsocket(fd, FIONREAD, &retval);
 532         if (retval > 0) {
 533             return retval;
 534         }
 535     }
 536 
 537     if (rv < 0) {
 538         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 539                         "Socket closed");
 540         return -1;
 541     }
 542 
 543     return 0;
   1 /*
   2  * Copyright (c) 2007, 2016, 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


 458 
 459     (*env)->GetByteArrayRegion(env, data, offset, length,
 460                                (jbyte *)fullPacket);
 461     rv = sendto(fd, fullPacket, length, 0, (struct sockaddr *)sap, sa_len);
 462     if (rv == SOCKET_ERROR) {
 463         if (rv == -1) {
 464             NET_ThrowNew(env, WSAGetLastError(), "Datagram send failed");
 465         }
 466     }
 467 
 468     if (length > MAX_BUFFER_LEN) {
 469         free(fullPacket);
 470     }
 471 }
 472 
 473 /*
 474  * Class:     java_net_DualStackPlainDatagramSocketImpl
 475  * Method:    socketSetIntOption
 476  * Signature: (III)V
 477  */
 478 JNIEXPORT void JNICALL
 479 Java_java_net_DualStackPlainDatagramSocketImpl_socketSetIntOption
 480   (JNIEnv *env, jclass clazz, jint fd, jint cmd, jint value)
 481 {
 482     int level = 0, opt = 0;
 483 
 484     if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
 485         JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");

 486         return;
 487     }
 488 
 489     if (NET_SetSockOpt(fd, level, opt, (char *)&value, sizeof(value)) < 0) {
 490         NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
 491     }
 492 }
 493 
 494 /*
 495  * Class:     java_net_DualStackPlainDatagramSocketImpl
 496  * Method:    socketGetIntOption
 497  * Signature: (II)I
 498  */
 499 JNIEXPORT jint JNICALL
 500 Java_java_net_DualStackPlainDatagramSocketImpl_socketGetIntOption
 501   (JNIEnv *env, jclass clazz, jint fd, jint cmd)
 502 {
 503     int level = 0, opt = 0, result = 0;
 504     int result_len = sizeof(result);
 505 
 506     if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
 507         JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");

 508         return -1;
 509     }
 510 
 511     if (NET_GetSockOpt(fd, level, opt, (void *)&result, &result_len) < 0) {
 512         NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
 513         return -1;
 514     }
 515 
 516     return result;
 517 }
 518 
 519 /*
 520  * Class:     java_net_DualStackPlainDatagramSocketImpl
 521  * Method:    dataAvailable
 522  * Signature: ()I
 523  */
 524 JNIEXPORT jint JNICALL
 525 Java_java_net_DualStackPlainDatagramSocketImpl_dataAvailable
 526   (JNIEnv *env, jobject this)
 527 {
 528     SOCKET fd;
 529     int  rv = -1;
 530     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 531 
 532     if (!IS_NULL(fdObj)) {
 533         int retval = 0;
 534         fd = (SOCKET)(*env)->GetIntField(env, fdObj, IO_fd_fdID);
 535         rv = ioctlsocket(fd, FIONREAD, &retval);
 536         if (retval > 0) {
 537             return retval;
 538         }
 539     }
 540 
 541     if (rv < 0) {
 542         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 543                         "Socket closed");
 544         return -1;
 545     }
 546 
 547     return 0;
< prev index next >