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