1 /*
   2  * Copyright (c) 2002, 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 /*
  27  */
  28 
  29 #include "jni.h"
  30 #include "jni_util.h"
  31 #include "jvm.h"
  32 #include "jlong.h"
  33 #include "sun_nio_ch_DatagramDispatcher.h"
  34 #include <sys/types.h>
  35 #include <sys/uio.h>
  36 #include <sys/socket.h>
  37 
  38 #include "nio_util.h"
  39 #include <limits.h>
  40 
  41 JNIEXPORT jint JNICALL
  42 Java_sun_nio_ch_DatagramDispatcher_read0(JNIEnv *env, jclass clazz,
  43                          jobject fdo, jlong address, jint len)
  44 {
  45     jint fd = fdval(env, fdo);
  46     void *buf = (void *)jlong_to_ptr(address);
  47     int result = recv(fd, buf, len, 0);
  48     if (result < 0 && errno == ECONNREFUSED) {
  49         JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
  50         return -2;
  51     }
  52     return convertReturnVal(env, result, JNI_TRUE);
  53 }
  54 
  55 
  56 JNIEXPORT jlong JNICALL
  57 Java_sun_nio_ch_DatagramDispatcher_readv0(JNIEnv *env, jclass clazz,
  58                               jobject fdo, jlong address, jint len)
  59 {
  60     jint fd = fdval(env, fdo);
  61     ssize_t result = 0;
  62     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
  63     struct msghdr m;
  64     if (len > IOV_MAX) {
  65         len = IOV_MAX;
  66     }
  67 
  68     // initialize the message
  69     memset(&m, 0, sizeof(m));
  70     m.msg_iov = iov;
  71     m.msg_iovlen = len;
  72 
  73     result = recvmsg(fd, &m, 0);
  74     if (result < 0 && errno == ECONNREFUSED) {
  75         JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
  76         return -2;
  77     }
  78     return convertLongReturnVal(env, (jlong)result, JNI_TRUE);
  79 }
  80 
  81 JNIEXPORT jint JNICALL
  82 Java_sun_nio_ch_DatagramDispatcher_write0(JNIEnv *env, jclass clazz,
  83                               jobject fdo, jlong address, jint len)
  84 {
  85     jint fd = fdval(env, fdo);
  86     void *buf = (void *)jlong_to_ptr(address);
  87     int result = send(fd, buf, len, 0);
  88     if (result < 0 && errno == ECONNREFUSED) {
  89         JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
  90         return -2;
  91     }
  92     return convertReturnVal(env, result, JNI_FALSE);
  93 }
  94 
  95 JNIEXPORT jlong JNICALL
  96 Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
  97                                        jobject fdo, jlong address, jint len)
  98 {
  99     jint fd = fdval(env, fdo);
 100     struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
 101     struct msghdr m;
 102     ssize_t result = 0;
 103     if (len > IOV_MAX) {
 104         len = IOV_MAX;
 105     }
 106 
 107     // initialize the message
 108     memset(&m, 0, sizeof(m));
 109     m.msg_iov = iov;
 110     m.msg_iovlen = len;
 111 
 112     result = sendmsg(fd, &m, 0);
 113     if (result < 0 && errno == ECONNREFUSED) {
 114         JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
 115         return -2;
 116     }
 117     return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
 118 }