Path: blob/master/src/java.base/unix/native/libnio/ch/InheritedChannel.c
41133 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <sys/types.h>27#include <sys/socket.h>28#include <unistd.h>29#include <fcntl.h>3031#include "jni.h"3233#include "jni.h"34#include "jni_util.h"35#include "net_util.h"36#include "nio_util.h"3738#include "sun_nio_ch_InheritedChannel.h"3940static int toInetFamily(SOCKETADDRESS *sa) {41return (sa->sa.sa_family == (ipv6_available() ? AF_INET6 : AF_INET));42}4344JNIEXPORT void JNICALL45Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla)46{47/* Initialize InetAddress IDs before later use of NET_XXX functions */48initInetAddressIDs(env);49}5051JNIEXPORT jobject JNICALL52Java_sun_nio_ch_InheritedChannel_inetPeerAddress0(JNIEnv *env, jclass cla, jint fd)53{54SOCKETADDRESS sa;55socklen_t len = sizeof(SOCKETADDRESS);56jobject remote_ia = NULL;57jint remote_port;5859if (getpeername(fd, &sa.sa, &len) == 0) {60if (toInetFamily(&sa)) {61remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);62}63}6465return remote_ia;66}6768JNIEXPORT jbyteArray JNICALL69Java_sun_nio_ch_InheritedChannel_unixPeerAddress0(JNIEnv *env, jclass cla, jint fd)70{71struct sockaddr_un sa;72socklen_t len = sizeof(struct sockaddr_un);73jobject remote_sa = NULL;7475if (getpeername(fd, (struct sockaddr *)&sa, &len) == 0) {76if (sa.sun_family == AF_UNIX) {77remote_sa = sockaddrToUnixAddressBytes(env, &sa, len);78}79}80return remote_sa;81}8283JNIEXPORT jint JNICALL84Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)85{86SOCKETADDRESS sa;87socklen_t len = sizeof(SOCKETADDRESS);88jint remote_port = -1;8990if (getpeername(fd, (struct sockaddr *)&sa.sa, &len) == 0) {91if (toInetFamily(&sa)) {92NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);93}94}9596return remote_port;97}9899JNIEXPORT jint JNICALL100Java_sun_nio_ch_InheritedChannel_addressFamily(JNIEnv *env, jclass cla, jint fd)101{102SOCKETADDRESS addr;103socklen_t addrlen = sizeof(addr);104105if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0) {106return sun_nio_ch_InheritedChannel_AF_UNKNOWN;107}108if (addr.sa.sa_family == AF_INET) {109return sun_nio_ch_InheritedChannel_AF_INET;110}111if (addr.sa.sa_family == AF_INET6) {112return sun_nio_ch_InheritedChannel_AF_INET6;113}114if (addr.sa.sa_family == AF_UNIX) {115return sun_nio_ch_InheritedChannel_AF_UNIX;116}117return sun_nio_ch_InheritedChannel_AF_UNKNOWN;118}119120JNIEXPORT jboolean JNICALL121Java_sun_nio_ch_InheritedChannel_isConnected(JNIEnv *env, jclass cla, jint fd)122{123SOCKETADDRESS addr;124socklen_t addrlen = sizeof(addr);125126if (getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0) {127return JNI_FALSE;128}129return JNI_TRUE;130}131132JNIEXPORT jint JNICALL133Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)134{135int sotype;136socklen_t arglen = sizeof(sotype);137if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {138if (sotype == SOCK_STREAM)139return sun_nio_ch_InheritedChannel_SOCK_STREAM;140if (sotype == SOCK_DGRAM)141return sun_nio_ch_InheritedChannel_SOCK_DGRAM;142}143return sun_nio_ch_InheritedChannel_UNKNOWN;144}145146JNIEXPORT jint JNICALL147Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)148{149int newfd = dup(fd);150if (newfd < 0) {151JNU_ThrowIOExceptionWithLastError(env, "dup failed");152}153return (jint)newfd;154}155156JNIEXPORT void JNICALL157Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)158{159if (dup2(fd, fd2) < 0) {160JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");161}162}163164JNIEXPORT jint JNICALL165Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)166{167const char *str;168int oflag_actual;169170/* convert to OS specific value */171switch (oflag) {172case sun_nio_ch_InheritedChannel_O_RDWR :173oflag_actual = O_RDWR;174break;175case sun_nio_ch_InheritedChannel_O_RDONLY :176oflag_actual = O_RDONLY;177break;178case sun_nio_ch_InheritedChannel_O_WRONLY :179oflag_actual = O_WRONLY;180break;181default :182JNU_ThrowInternalError(env, "Unrecognized file mode");183return -1;184}185186str = JNU_GetStringPlatformChars(env, path, NULL);187if (str == NULL) {188return (jint)-1;189} else {190int fd = open(str, oflag_actual);191if (fd < 0) {192JNU_ThrowIOExceptionWithLastError(env, str);193}194JNU_ReleaseStringPlatformChars(env, path, str);195return (jint)fd;196}197}198199JNIEXPORT void JNICALL200Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)201{202if (close(fd) < 0) {203JNU_ThrowIOExceptionWithLastError(env, "close failed");204}205}206207208