58680
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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_NioDatagramSocketImpl.h"
|
|
48 |
|
|
49 |
static jfieldID dsi_senderID; /* sender in sun.nio.ch.NioDatagramSocketImpl */
|
|
50 |
static jclass isa_class; /* java.net.InetSocketAddress */
|
|
51 |
static jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */
|
|
52 |
|
|
53 |
JNIEXPORT void JNICALL
|
|
54 |
Java_sun_nio_ch_NioDatagramSocketImpl_initIDs(JNIEnv *env, jclass clazz)
|
|
55 |
{
|
|
56 |
clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
|
|
57 |
CHECK_NULL(clazz);
|
|
58 |
isa_class = (*env)->NewGlobalRef(env, clazz);
|
|
59 |
if (isa_class == NULL) {
|
|
60 |
JNU_ThrowOutOfMemoryError(env, NULL);
|
|
61 |
return;
|
|
62 |
}
|
|
63 |
isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>", "(Ljava/net/InetAddress;I)V");
|
|
64 |
CHECK_NULL(isa_ctorID);
|
|
65 |
|
|
66 |
clazz = (*env)->FindClass(env, "sun/nio/ch/NioDatagramSocketImpl");
|
|
67 |
CHECK_NULL(clazz);
|
|
68 |
dsi_senderID = (*env)->GetFieldID(env, clazz, "sender", "Ljava/net/InetSocketAddress;");
|
|
69 |
CHECK_NULL(dsi_senderID);
|
|
70 |
}
|
|
71 |
|
|
72 |
JNIEXPORT jint JNICALL
|
|
73 |
Java_sun_nio_ch_NioDatagramSocketImpl_send0(JNIEnv *env,
|
|
74 |
jclass clazz,
|
|
75 |
jboolean preferIPv6,
|
|
76 |
jobject fdo,
|
|
77 |
jlong address,
|
|
78 |
jint len,
|
|
79 |
jobject destAddress,
|
|
80 |
jint destPort)
|
|
81 |
{
|
|
82 |
jint fd = fdval(env, fdo);
|
|
83 |
void *buf = (void *)jlong_to_ptr(address);
|
|
84 |
SOCKETADDRESS sa;
|
|
85 |
struct sockaddr *saP = NULL;
|
|
86 |
int sa_len = 0;
|
|
87 |
jint n = 0;
|
|
88 |
|
|
89 |
if (destAddress != NULL) { // not connected
|
|
90 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
|
|
91 |
&sa_len, preferIPv6) != 0) {
|
|
92 |
return IOS_THROWN;
|
|
93 |
}
|
|
94 |
saP = &sa.sa;
|
|
95 |
}
|
|
96 |
|
|
97 |
n = sendto(fd, buf, len, 0, saP, sa_len);
|
|
98 |
if (n < 0) {
|
|
99 |
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
100 |
return IOS_UNAVAILABLE;
|
|
101 |
}
|
|
102 |
if (errno == EINTR) {
|
|
103 |
return IOS_INTERRUPTED;
|
|
104 |
}
|
|
105 |
if (errno == ECONNREFUSED) {
|
|
106 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
|
|
107 |
return IOS_THROWN;
|
|
108 |
}
|
|
109 |
return handleSocketError(env, errno);
|
|
110 |
}
|
|
111 |
return n;
|
|
112 |
}
|
|
113 |
|
|
114 |
JNIEXPORT jint JNICALL
|
|
115 |
Java_sun_nio_ch_NioDatagramSocketImpl_receive0(JNIEnv *env,
|
|
116 |
jobject this,
|
|
117 |
jobject fdo,
|
|
118 |
jlong address,
|
|
119 |
jint len,
|
|
120 |
jboolean isPeek,
|
|
121 |
jobject cachedSenderAddress,
|
|
122 |
jint cachedSenderPort)
|
|
123 |
{
|
|
124 |
jint fd = fdval(env, fdo);
|
|
125 |
void *buf = (void *)jlong_to_ptr(address);
|
|
126 |
SOCKETADDRESS sa;
|
|
127 |
socklen_t sa_len = sizeof(SOCKETADDRESS);
|
|
128 |
jboolean retry = JNI_FALSE;
|
|
129 |
int flags = 0;
|
|
130 |
jint n = 0;
|
|
131 |
jobject senderAddr;
|
|
132 |
|
|
133 |
if (isPeek == JNI_TRUE) {
|
|
134 |
flags = MSG_PEEK;
|
|
135 |
}
|
|
136 |
|
|
137 |
n = recvfrom(fd, buf, len, flags, &sa.sa, &sa_len);
|
|
138 |
if (n < 0) {
|
|
139 |
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
140 |
return IOS_UNAVAILABLE;
|
|
141 |
} else if (errno == EINTR) {
|
|
142 |
return IOS_INTERRUPTED;
|
|
143 |
} else if (errno == ECONNREFUSED) {
|
|
144 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
|
|
145 |
"ICMP Port Unreachable");
|
|
146 |
return IOS_THROWN;
|
|
147 |
} else {
|
|
148 |
return handleSocketError(env, errno);
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
// If the cached address does not match, then create a new one.
|
|
153 |
if (cachedSenderAddress == NULL ||
|
|
154 |
!NET_SockaddrEqualsInetAddress(env, &sa, cachedSenderAddress) ||
|
|
155 |
cachedSenderPort != NET_GetPortFromSockaddr(&sa)) {
|
|
156 |
|
|
157 |
jobject isa = NULL;
|
|
158 |
int port = 0;
|
|
159 |
jobject ia = NET_SockaddrToInetAddress(env, &sa, &port);
|
|
160 |
if (ia != NULL) {
|
|
161 |
isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
|
|
162 |
}
|
|
163 |
CHECK_NULL_RETURN(isa, IOS_THROWN);
|
|
164 |
|
|
165 |
(*env)->SetObjectField(env, this, dsi_senderID, isa);
|
|
166 |
}
|
|
167 |
|
|
168 |
return n;
|
|
169 |
}
|
|
170 |
|
|
171 |
JNIEXPORT void JNICALL
|
|
172 |
Java_sun_nio_ch_NioDatagramSocketImpl_disconnect0(JNIEnv *env,
|
|
173 |
jobject clazz,
|
|
174 |
jobject fdo,
|
|
175 |
jboolean isIPv6)
|
|
176 |
{
|
|
177 |
jint fd = fdval(env, fdo);
|
|
178 |
int rv;
|
|
179 |
|
|
180 |
#if defined(__solaris__)
|
|
181 |
rv = connect(fd, 0, 0);
|
|
182 |
#else
|
|
183 |
SOCKETADDRESS sa;
|
|
184 |
socklen_t len = isIPv6 ? sizeof(struct sockaddr_in6) :
|
|
185 |
sizeof(struct sockaddr_in);
|
|
186 |
|
|
187 |
memset(&sa, 0, sizeof(sa));
|
|
188 |
#if defined(_ALLBSD_SOURCE)
|
|
189 |
sa.sa.sa_family = isIPv6 ? AF_INET6 : AF_INET;
|
|
190 |
#else
|
|
191 |
sa.sa.sa_family = AF_UNSPEC;
|
|
192 |
#endif
|
|
193 |
|
|
194 |
rv = connect(fd, &sa.sa, len);
|
|
195 |
|
|
196 |
#if defined(_ALLBSD_SOURCE)
|
|
197 |
if (rv < 0 && errno == EADDRNOTAVAIL)
|
|
198 |
rv = errno = 0;
|
|
199 |
#elif defined(_AIX)
|
|
200 |
/* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254:
|
|
201 |
* 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT
|
|
202 |
* but that is acceptable.
|
|
203 |
*/
|
|
204 |
if (rv < 0 && errno == EAFNOSUPPORT)
|
|
205 |
rv = errno = 0;
|
|
206 |
#endif // defined(_ALLBSD_SOURCE) || defined(_AIX)
|
|
207 |
|
|
208 |
#endif // defined(__solaris__)
|
|
209 |
|
|
210 |
if (rv < 0)
|
|
211 |
handleSocketError(env, errno);
|
|
212 |
}
|