2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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(struct sockaddr *sa) {
|
|
41 |
int family = sa->sa_family;
|
|
42 |
#ifdef AF_INET6
|
|
43 |
if (ipv6_available()) {
|
|
44 |
return (family == AF_INET6);
|
|
45 |
}
|
|
46 |
#endif
|
|
47 |
return (family == AF_INET);
|
|
48 |
}
|
|
49 |
|
|
50 |
JNIEXPORT jobject JNICALL
|
|
51 |
Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
|
|
52 |
{
|
|
53 |
struct sockaddr *sa;
|
|
54 |
socklen_t sa_len;
|
|
55 |
jobject remote_ia = NULL;
|
|
56 |
jint remote_port;
|
|
57 |
|
|
58 |
NET_AllocSockaddr(&sa, (int *)&sa_len);
|
|
59 |
if (getpeername(fd, sa, &sa_len) == 0) {
|
|
60 |
if (matchFamily(sa)) {
|
|
61 |
remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
|
|
62 |
}
|
|
63 |
}
|
|
64 |
free((void *)sa);
|
|
65 |
|
|
66 |
return remote_ia;
|
|
67 |
}
|
|
68 |
|
|
69 |
|
|
70 |
JNIEXPORT jint JNICALL
|
|
71 |
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
|
|
72 |
{
|
|
73 |
struct sockaddr *sa;
|
|
74 |
socklen_t sa_len;
|
|
75 |
jint remote_port = -1;
|
|
76 |
|
|
77 |
NET_AllocSockaddr(&sa, (int *)&sa_len);
|
|
78 |
if (getpeername(fd, sa, &sa_len) == 0) {
|
|
79 |
if (matchFamily(sa)) {
|
|
80 |
NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
free((void *)sa);
|
|
84 |
|
|
85 |
return remote_port;
|
|
86 |
}
|
|
87 |
|
|
88 |
JNIEXPORT jint JNICALL
|
|
89 |
Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
|
|
90 |
{
|
895
|
91 |
int sotype;
|
|
92 |
socklen_t arglen=sizeof(sotype);
|
2
|
93 |
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
|
|
94 |
if (sotype == SOCK_STREAM)
|
|
95 |
return sun_nio_ch_InheritedChannel_SOCK_STREAM;
|
|
96 |
if (sotype == SOCK_DGRAM)
|
|
97 |
return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
|
|
98 |
}
|
|
99 |
return sun_nio_ch_InheritedChannel_UNKNOWN;
|
|
100 |
}
|
|
101 |
|
|
102 |
JNIEXPORT jint JNICALL
|
|
103 |
Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
|
|
104 |
{
|
|
105 |
int newfd = dup(fd);
|
|
106 |
if (newfd < 0) {
|
|
107 |
JNU_ThrowIOExceptionWithLastError(env, "dup failed");
|
|
108 |
}
|
|
109 |
return (jint)newfd;
|
|
110 |
}
|
|
111 |
|
|
112 |
JNIEXPORT void JNICALL
|
|
113 |
Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
|
|
114 |
{
|
|
115 |
if (dup2(fd, fd2) < 0) {
|
|
116 |
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
JNIEXPORT jint JNICALL
|
|
121 |
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
|
|
122 |
{
|
|
123 |
const char* str;
|
|
124 |
int oflag_actual;
|
|
125 |
|
|
126 |
/* convert to OS specific value */
|
|
127 |
switch (oflag) {
|
|
128 |
case sun_nio_ch_InheritedChannel_O_RDWR :
|
|
129 |
oflag_actual = O_RDWR;
|
|
130 |
break;
|
|
131 |
case sun_nio_ch_InheritedChannel_O_RDONLY :
|
|
132 |
oflag_actual = O_RDONLY;
|
|
133 |
break;
|
|
134 |
case sun_nio_ch_InheritedChannel_O_WRONLY :
|
|
135 |
oflag_actual = O_WRONLY;
|
|
136 |
break;
|
|
137 |
default :
|
|
138 |
JNU_ThrowInternalError(env, "Unrecognized file mode");
|
|
139 |
return -1;
|
|
140 |
}
|
|
141 |
|
|
142 |
str = JNU_GetStringPlatformChars(env, path, NULL);
|
|
143 |
if (str == NULL) {
|
|
144 |
return (jint)-1;
|
|
145 |
} else {
|
|
146 |
int fd = open(str, oflag_actual);
|
|
147 |
if (fd < 0) {
|
|
148 |
JNU_ThrowIOExceptionWithLastError(env, str);
|
|
149 |
}
|
|
150 |
JNU_ReleaseStringPlatformChars(env, path, str);
|
|
151 |
return (jint)fd;
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
JNIEXPORT void JNICALL
|
|
156 |
Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
|
|
157 |
{
|
|
158 |
if (close(fd) < 0) {
|
|
159 |
JNU_ThrowIOExceptionWithLastError(env, "close failed");
|
|
160 |
}
|
|
161 |
}
|