1 /*
2 * Copyright (c) 2003, 2017, 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 <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include "jni.h"
33
34 #include "jni.h"
35 #include "jni_util.h"
36 #include "net_util.h"
37
38 #include "sun_nio_ch_InheritedChannel.h"
39
40 static int matchFamily(SOCKETADDRESS *sa) {
41 return (sa->sa.sa_family == (ipv6_available() ? AF_INET6 : AF_INET));
42 }
43
44 JNIEXPORT void JNICALL
45 Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla)
46 {
47 /* Initialize InetAddress IDs before later use of NET_XXX functions */
48 initInetAddressIDs(env);
49 }
50
51 JNIEXPORT jobject JNICALL
52 Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
53 {
54 SOCKETADDRESS sa;
55 socklen_t len = sizeof(SOCKETADDRESS);
56 jobject remote_ia = NULL;
57 jint remote_port;
58
59 if (getpeername(fd, &sa.sa, &len) == 0) {
60 // TODO: rename remote_ia variable and matchFamily method
61 if (matchFamily(&sa)) {
62 // TODO: move to some common function
63 jobject inetAddress = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
64 jclass isa_class = (*env)->FindClass(env, "java/net/InetSocketAddress");
65 jmethodID isa_ctor = (*env)->GetMethodID(env, isa_class, "<init>", "(Ljava/net/InetAddress;I)V");
66 remote_ia = (*env)->NewObject(env, isa_class, isa_ctor, inetAddress, remote_port);
67 } else if (sa.sa.sa_family == AF_UNIX) {
68 // TODO: move to some common function
69 jclass usa_class = (*env)->FindClass(env, "java/net/UnixSocketAddress");
70 jmethodID usa_ctor = (*env)->GetMethodID(env, usa_class, "<init>", "()V");
71 remote_ia = (*env)->NewObject(env, usa_class, usa_ctor);
72 }
73 }
74
75 return remote_ia;
76 }
77
78 JNIEXPORT jint JNICALL
79 Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
80 {
81 int sotype;
82 socklen_t arglen = sizeof(sotype);
83 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
84 if (sotype == SOCK_STREAM)
85 return sun_nio_ch_InheritedChannel_SOCK_STREAM;
86 if (sotype == SOCK_DGRAM)
87 return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
88 }
89 return sun_nio_ch_InheritedChannel_UNKNOWN;
90 }
91
92 JNIEXPORT jint JNICALL
93 Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
94 {
95 int newfd = dup(fd);
96 if (newfd < 0) {
97 JNU_ThrowIOExceptionWithLastError(env, "dup failed");
98 }
99 return (jint)newfd;
100 }
101
102 JNIEXPORT void JNICALL
103 Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
104 {
105 if (dup2(fd, fd2) < 0) {
106 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
107 }
108 }
109
110 JNIEXPORT jint JNICALL
111 Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
112 {
113 const char *str;
114 int oflag_actual;
115
116 /* convert to OS specific value */
117 switch (oflag) {
118 case sun_nio_ch_InheritedChannel_O_RDWR :
119 oflag_actual = O_RDWR;
120 break;
121 case sun_nio_ch_InheritedChannel_O_RDONLY :
122 oflag_actual = O_RDONLY;
123 break;
124 case sun_nio_ch_InheritedChannel_O_WRONLY :
125 oflag_actual = O_WRONLY;
126 break;
127 default :
128 JNU_ThrowInternalError(env, "Unrecognized file mode");
129 return -1;
130 }
131
132 str = JNU_GetStringPlatformChars(env, path, NULL);
133 if (str == NULL) {
134 return (jint)-1;
135 } else {
136 int fd = open(str, oflag_actual);
137 if (fd < 0) {
138 JNU_ThrowIOExceptionWithLastError(env, str);
139 }
140 JNU_ReleaseStringPlatformChars(env, path, str);
141 return (jint)fd;
142 }
143 }
144
145 JNIEXPORT void JNICALL
146 Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
147 {
148 if (close(fd) < 0) {
149 JNU_ThrowIOExceptionWithLastError(env, "close failed");
150 }
151 }