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