1 /*
   2  * Copyright (c) 2001, 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 #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) || defined(_AIX)
  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 #if defined(_AIX)
 119         /* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254:
 120          * 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT
 121          * but that is acceptable.
 122          */
 123         if (rv < 0 && errno == EAFNOSUPPORT)
 124                 rv = errno = 0;
 125 #endif
 126     }
 127 #endif
 128 
 129     if (rv < 0)
 130         handleSocketError(env, errno);
 131 
 132 }
 133 
 134 JNIEXPORT jint JNICALL
 135 Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
 136                                              jobject fdo, jlong address,
 137                                              jint len, jboolean connected)
 138 {
 139     jint fd = fdval(env, fdo);
 140     void *buf = (void *)jlong_to_ptr(address);
 141     SOCKADDR sa;
 142     socklen_t sa_len = SOCKADDR_LEN;
 143     jboolean retry = JNI_FALSE;
 144     jint n = 0;
 145     jobject senderAddr;
 146 
 147     if (len > MAX_PACKET_LEN) {
 148         len = MAX_PACKET_LEN;
 149     }
 150 
 151     do {
 152         retry = JNI_FALSE;
 153         n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len);
 154         if (n < 0) {
 155             if (errno == EWOULDBLOCK) {
 156                 return IOS_UNAVAILABLE;
 157             }
 158             if (errno == EINTR) {
 159                 return IOS_INTERRUPTED;
 160             }
 161             if (errno == ECONNREFUSED) {
 162                 if (connected == JNI_FALSE) {
 163                     retry = JNI_TRUE;
 164                 } else {
 165                     JNU_ThrowByName(env, JNU_JAVANETPKG
 166                                     "PortUnreachableException", 0);
 167                     return IOS_THROWN;
 168                 }
 169             } else {
 170                 return handleSocketError(env, errno);
 171             }
 172         }
 173     } while (retry == JNI_TRUE);
 174 
 175     /*
 176      * If the source address and port match the cached address
 177      * and port in DatagramChannelImpl then we don't need to
 178      * create InetAddress and InetSocketAddress objects.
 179      */
 180     senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
 181     if (senderAddr != NULL) {
 182         if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa,
 183                                            senderAddr)) {
 184             senderAddr = NULL;
 185         } else {
 186             jint port = (*env)->GetIntField(env, this, dci_senderPortID);
 187             if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) {
 188                 senderAddr = NULL;
 189             }
 190         }
 191     }
 192     if (senderAddr == NULL) {
 193         jobject isa = NULL;
 194         int port;
 195         jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa,
 196                                                &port);
 197 
 198         if (ia != NULL) {
 199             isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
 200         }
 201 
 202         if (isa == NULL) {
 203             JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
 204             return IOS_THROWN;
 205         }
 206 
 207         (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
 208         (*env)->SetIntField(env, this, dci_senderPortID,
 209                             NET_GetPortFromSockaddr((struct sockaddr *)&sa));
 210         (*env)->SetObjectField(env, this, dci_senderID, isa);
 211     }
 212     return n;
 213 }
 214 
 215 JNIEXPORT jint JNICALL
 216 Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
 217                                           jboolean preferIPv6, jobject fdo, jlong address,
 218                                           jint len, jobject destAddress, jint destPort)
 219 {
 220     jint fd = fdval(env, fdo);
 221     void *buf = (void *)jlong_to_ptr(address);
 222     SOCKADDR sa;
 223     int sa_len = SOCKADDR_LEN;
 224     jint n = 0;
 225 
 226     if (len > MAX_PACKET_LEN) {
 227         len = MAX_PACKET_LEN;
 228     }
 229 
 230     if (NET_InetAddressToSockaddr(env, destAddress, destPort,
 231                                   (struct sockaddr *)&sa,
 232                                   &sa_len, preferIPv6) != 0) {
 233       return IOS_THROWN;
 234     }
 235 
 236     n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len);
 237     if (n < 0) {
 238         if (errno == EAGAIN) {
 239             return IOS_UNAVAILABLE;
 240         }
 241         if (errno == EINTR) {
 242             return IOS_INTERRUPTED;
 243         }
 244         if (errno == ECONNREFUSED) {
 245             JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
 246             return IOS_THROWN;
 247         }
 248         return handleSocketError(env, errno);
 249     }
 250     return n;
 251 }