DatagramChannelImpl.c revision 240109a2df3fb0699cdaf919b490d34668814a4e
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 "jni.h" 27#include "jni_util.h" 28#include "jvm.h" 29#include "jlong.h" 30 31#include <netdb.h> 32#include <sys/types.h> 33#include <sys/socket.h> 34#include <stdlib.h> 35#include <string.h> 36#include <errno.h> 37 38#if defined(__linux__) || defined(_ALLBSD_SOURCE) 39#include <netinet/in.h> 40#endif 41 42#include "net_util.h" 43#include "net_util_md.h" 44#include "nio.h" 45#include "nio_util.h" 46 47 48#include "JNIHelp.h" 49 50#define NATIVE_METHOD(className, functionName, signature) \ 51{ #functionName, signature, (void*)(Java_sun_nio_ch_ ## className ## _ ## functionName) } 52 53 54static jfieldID dci_senderID; /* sender in sun.nio.ch.DatagramChannelImpl */ 55static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */ 56static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */ 57static jclass isa_class; /* java.net.InetSocketAddress */ 58static jmethodID isa_ctorID; /* .InetSocketAddress(InetAddress, int) */ 59 60JNIEXPORT void JNICALL 61Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz) 62{ 63 clazz = (*env)->FindClass(env, "java/net/InetSocketAddress"); 64 isa_class = (*env)->NewGlobalRef(env, clazz); 65 isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>", 66 "(Ljava/net/InetAddress;I)V"); 67 68 clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl"); 69 dci_senderID = (*env)->GetFieldID(env, clazz, "sender", 70 "Ljava/net/SocketAddress;"); 71 dci_senderAddrID = (*env)->GetFieldID(env, clazz, 72 "cachedSenderInetAddress", 73 "Ljava/net/InetAddress;"); 74 dci_senderPortID = (*env)->GetFieldID(env, clazz, 75 "cachedSenderPort", "I"); 76} 77 78JNIEXPORT void JNICALL 79Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this, 80 jobject fdo, jboolean isIPv6) 81{ 82 jint fd = fdval(env, fdo); 83 int rv; 84 85#ifdef __solaris__ 86 rv = connect(fd, 0, 0); 87#endif 88 89#if defined(__linux__) || defined(_ALLBSD_SOURCE) 90 { 91 int len; 92 SOCKADDR sa; 93 94 memset(&sa, 0, sizeof(sa)); 95 96#ifdef AF_INET6 97 if (isIPv6) { 98 struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&sa; 99#if defined(_ALLBSD_SOURCE) 100 him6->sin6_family = AF_INET6; 101#else 102 him6->sin6_family = AF_UNSPEC; 103#endif 104 len = sizeof(struct sockaddr_in6); 105 } else 106#endif 107 { 108 struct sockaddr_in *him4 = (struct sockaddr_in*)&sa; 109#if defined(_ALLBSD_SOURCE) 110 him4->sin_family = AF_INET; 111#else 112 him4->sin_family = AF_UNSPEC; 113#endif 114 len = sizeof(struct sockaddr_in); 115 } 116 117 rv = connect(fd, (struct sockaddr *)&sa, len); 118 119#if defined(_ALLBSD_SOURCE) 120 if (rv < 0 && errno == EADDRNOTAVAIL) 121 rv = errno = 0; 122#endif 123 } 124#endif 125 126 if (rv < 0) 127 handleSocketError(env, errno); 128 129} 130 131JNIEXPORT jint JNICALL 132Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this, 133 jobject fdo, jlong address, 134 jint len, jboolean connected) 135{ 136 jint fd = fdval(env, fdo); 137 void *buf = (void *)jlong_to_ptr(address); 138 SOCKADDR sa; 139 socklen_t sa_len = SOCKADDR_LEN; 140 jboolean retry = JNI_FALSE; 141 jint n = 0; 142 jobject senderAddr; 143 144 if (len > MAX_PACKET_LEN) { 145 len = MAX_PACKET_LEN; 146 } 147 148 do { 149 retry = JNI_FALSE; 150 n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len); 151 if (n < 0) { 152 if (errno == EWOULDBLOCK) { 153 return IOS_UNAVAILABLE; 154 } 155 if (errno == EINTR) { 156 return IOS_INTERRUPTED; 157 } 158 if (errno == ECONNREFUSED) { 159 if (connected == JNI_FALSE) { 160 retry = JNI_TRUE; 161 } else { 162 JNU_ThrowByName(env, JNU_JAVANETPKG 163 "PortUnreachableException", 0); 164 return IOS_THROWN; 165 } 166 } else { 167 return handleSocketError(env, errno); 168 } 169 } 170 } while (retry == JNI_TRUE); 171 172 /* 173 * If the source address and port match the cached address 174 * and port in DatagramChannelImpl then we don't need to 175 * create InetAddress and InetSocketAddress objects. 176 */ 177 senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID); 178 if (senderAddr != NULL) { 179 if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa, 180 senderAddr)) { 181 senderAddr = NULL; 182 } else { 183 jint port = (*env)->GetIntField(env, this, dci_senderPortID); 184 if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) { 185 senderAddr = NULL; 186 } 187 } 188 } 189 if (senderAddr == NULL) { 190 jobject isa = NULL; 191 int port; 192 jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, 193 &port); 194 195 if (ia != NULL) { 196 isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port); 197 } 198 199 if (isa == NULL) { 200 JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); 201 return IOS_THROWN; 202 } 203 204 (*env)->SetObjectField(env, this, dci_senderAddrID, ia); 205 (*env)->SetIntField(env, this, dci_senderPortID, 206 NET_GetPortFromSockaddr((struct sockaddr *)&sa)); 207 (*env)->SetObjectField(env, this, dci_senderID, isa); 208 } 209 return n; 210} 211 212JNIEXPORT jint JNICALL 213Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this, 214 jboolean preferIPv6, jobject fdo, jlong address, 215 jint len, jobject destAddress, jint destPort) 216{ 217 jint fd = fdval(env, fdo); 218 void *buf = (void *)jlong_to_ptr(address); 219 SOCKADDR sa; 220 int sa_len = SOCKADDR_LEN; 221 jint n = 0; 222 223 if (len > MAX_PACKET_LEN) { 224 len = MAX_PACKET_LEN; 225 } 226 227 if (NET_InetAddressToSockaddr(env, destAddress, destPort, 228 (struct sockaddr *)&sa, 229 &sa_len, preferIPv6) != 0) { 230 return IOS_THROWN; 231 } 232 233 n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len); 234 if (n < 0) { 235 if (errno == EAGAIN) { 236 return IOS_UNAVAILABLE; 237 } 238 if (errno == EINTR) { 239 return IOS_INTERRUPTED; 240 } 241 if (errno == ECONNREFUSED) { 242 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); 243 return IOS_THROWN; 244 } 245 return handleSocketError(env, errno); 246 } 247 return n; 248} 249 250static JNINativeMethod gMethods[] = { 251 NATIVE_METHOD(DatagramChannelImpl, initIDs, "()V"), 252 NATIVE_METHOD(DatagramChannelImpl, disconnect0, "(Ljava/io/FileDescriptor;Z)V"), 253 NATIVE_METHOD(DatagramChannelImpl, receive0, "(Ljava/io/FileDescriptor;JIZ)I"), 254 NATIVE_METHOD(DatagramChannelImpl, send0, "(ZLjava/io/FileDescriptor;JILjava/net/InetAddress;I)I"), 255}; 256 257void register_sun_nio_ch_DatagramChannelImpl(JNIEnv* env) { 258 jniRegisterNativeMethods(env, "sun/nio/ch/DatagramChannelImpl", gMethods, NELEM(gMethods)); 259} 260