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 isa_addrID;     /* address in java.net.InetSocketAddress */
  50 static jfieldID isa_portID;     /* port in java.net.InetSocketAddress */
  51 static jfieldID dci_senderID;   /* sender in sun.nio.ch.DatagramChannelImpl */
  52 static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
  53 static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
  54 static jclass isa_class;        /* java.net.InetSocketAddress */
  55 static jmethodID isa_ctorID;    /*   .InetSocketAddress(InetAddress, int) */
  56 
  57 JNIEXPORT void JNICALL
  58 Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
  59 {
  60     clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
  61     isa_class = (*env)->NewGlobalRef(env, clazz);
  62     isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
  63                                      "(Ljava/net/InetAddress;I)V");
  64     isa_addrID = (*env)->GetFieldID(env, clazz, "addr",
  65                                     "Ljava/net/InetAddress;");
  66     isa_portID = (*env)->GetFieldID(env, clazz, "port", "I");
  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 
  78 JNIEXPORT void JNICALL
  79 Java_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 
 131 JNIEXPORT jint JNICALL
 132 Java_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 
 212 JNIEXPORT jint JNICALL
 213 Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
 214                                           jboolean preferIPv6, jobject fdo, jlong address,
 215                                             jint len, jobject dest)
 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     jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID);
 223     jint destPort = (*env)->GetIntField(env, dest, isa_portID);
 224 
 225     if (len > MAX_PACKET_LEN) {
 226         len = MAX_PACKET_LEN;
 227     }
 228 
 229     if (NET_InetAddressToSockaddr(env, destAddress, destPort,
 230                                   (struct sockaddr *)&sa,
 231                                   &sa_len, preferIPv6) != 0) {
 232       return IOS_THROWN;
 233     }
 234 
 235     n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len);
 236     if (n < 0) {
 237         if (errno == EAGAIN) {
 238             return IOS_UNAVAILABLE;
 239         }
 240         if (errno == EINTR) {
 241             return IOS_INTERRUPTED;
 242         }
 243         if (errno == ECONNREFUSED) {
 244             JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
 245             return IOS_THROWN;
 246         }
 247         return handleSocketError(env, errno);
 248     }
 249     return n;
 250 }