1 /* 2 * Copyright (c) 2002, 2012, 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 #include <string.h> 38 39 #include "nio_util.h" 40 #include <limits.h> 41 42 JNIEXPORT jint JNICALL 43 Java_sun_nio_ch_DatagramDispatcher_read0(JNIEnv *env, jclass clazz, 44 jobject fdo, jlong address, jint len) 45 { 46 jint fd = fdval(env, fdo); 47 void *buf = (void *)jlong_to_ptr(address); 48 int result = recv(fd, buf, len, 0); 49 if (result < 0 && errno == ECONNREFUSED) { 50 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); 51 return -2; 52 } 53 54 if (result > 0) { 55 JVM_callNetworkReadBytes(env, result); 56 } 57 58 return convertReturnVal(env, result, JNI_TRUE); 59 } 60 61 62 JNIEXPORT jlong JNICALL 63 Java_sun_nio_ch_DatagramDispatcher_readv0(JNIEnv *env, jclass clazz, 64 jobject fdo, jlong address, jint len) 65 { 66 jint fd = fdval(env, fdo); 67 ssize_t result = 0; 68 struct iovec *iov = (struct iovec *)jlong_to_ptr(address); 69 struct msghdr m; 70 if (len > IOV_MAX) { 71 len = IOV_MAX; 72 } 73 74 // initialize the message 75 memset(&m, 0, sizeof(m)); 76 m.msg_iov = iov; 77 m.msg_iovlen = len; 78 79 result = recvmsg(fd, &m, 0); 80 if (result < 0 && errno == ECONNREFUSED) { 81 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); 82 return -2; 83 } 84 85 if (result > 0) { 86 JVM_callNetworkReadBytes(env, result); 87 } 88 89 return convertLongReturnVal(env, (jlong)result, JNI_TRUE); 90 } 91 92 JNIEXPORT jint JNICALL 93 Java_sun_nio_ch_DatagramDispatcher_write0(JNIEnv *env, jclass clazz, 94 jobject fdo, jlong address, jint len) 95 { 96 jint fd = fdval(env, fdo); 97 void *buf = (void *)jlong_to_ptr(address); 98 int result = send(fd, buf, len, 0); 99 if (result < 0 && errno == ECONNREFUSED) { 100 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); 101 return -2; 102 } 103 104 if (result > 0) { 105 JVM_callNetworkWriteBytes(env, result); 106 } 107 108 return convertReturnVal(env, result, JNI_FALSE); 109 } 110 111 JNIEXPORT jlong JNICALL 112 Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz, 113 jobject fdo, jlong address, jint len) 114 { 115 jint fd = fdval(env, fdo); 116 struct iovec *iov = (struct iovec *)jlong_to_ptr(address); 117 struct msghdr m; 118 ssize_t result = 0; 119 if (len > IOV_MAX) { 120 len = IOV_MAX; 121 } 122 123 // initialize the message 124 memset(&m, 0, sizeof(m)); 125 m.msg_iov = iov; 126 m.msg_iovlen = len; 127 128 result = sendmsg(fd, &m, 0); 129 if (result < 0 && errno == ECONNREFUSED) { 130 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); 131 return -2; 132 } 133 134 if (result > 0) { 135 JVM_callNetworkWriteBytes(env, result); 136 } 137 138 return convertLongReturnVal(env, (jlong)result, JNI_FALSE); 139 }