1 /*
2 * Copyright (c) 2001, 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
125 Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
126 jobject fdo, jlong address,
127 jint len, jboolean connected)
128 {
129 jint fd = fdval(env, fdo);
130 void *buf = (void *)jlong_to_ptr(address);
131 SOCKETADDRESS sa;
132 socklen_t sa_len = sizeof(SOCKETADDRESS);
133 jboolean retry = JNI_FALSE;
134 jint n = 0;
135 jobject senderAddr;
136
137 if (len > MAX_PACKET_LEN) {
138 len = MAX_PACKET_LEN;
139 }
140
141 do {
142 retry = JNI_FALSE;
143 n = recvfrom(fd, buf, len, 0, &sa.sa, &sa_len);
144 if (n < 0) {
145 if (errno == EWOULDBLOCK) {
146 return IOS_UNAVAILABLE;
147 }
148 if (errno == EINTR) {
149 return IOS_INTERRUPTED;
150 }
151 if (errno == ECONNREFUSED) {
152 if (connected == JNI_FALSE) {
153 retry = JNI_TRUE;
154 } else {
155 JNU_ThrowByName(env, JNU_JAVANETPKG
156 "PortUnreachableException", 0);
157 return IOS_THROWN;
158 }
159 } else {
160 return handleSocketError(env, errno);
161 }
162 }
163 } while (retry == JNI_TRUE);
164
165 /*
200 jboolean preferIPv6, jobject fdo, jlong address,
201 jint len, jobject destAddress, jint destPort)
202 {
203 jint fd = fdval(env, fdo);
204 void *buf = (void *)jlong_to_ptr(address);
205 SOCKETADDRESS sa;
206 int sa_len = 0;
207 jint n = 0;
208
209 if (len > MAX_PACKET_LEN) {
210 len = MAX_PACKET_LEN;
211 }
212
213 if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
214 &sa_len, preferIPv6) != 0) {
215 return IOS_THROWN;
216 }
217
218 n = sendto(fd, buf, len, 0, &sa.sa, sa_len);
219 if (n < 0) {
220 if (errno == EAGAIN) {
221 return IOS_UNAVAILABLE;
222 }
223 if (errno == EINTR) {
224 return IOS_INTERRUPTED;
225 }
226 if (errno == ECONNREFUSED) {
227 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
228 return IOS_THROWN;
229 }
230 return handleSocketError(env, errno);
231 }
232 return n;
233 }
|
1 /*
2 * Copyright (c) 2001, 2018, 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
125 Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
126 jobject fdo, jlong address,
127 jint len, jboolean connected)
128 {
129 jint fd = fdval(env, fdo);
130 void *buf = (void *)jlong_to_ptr(address);
131 SOCKETADDRESS sa;
132 socklen_t sa_len = sizeof(SOCKETADDRESS);
133 jboolean retry = JNI_FALSE;
134 jint n = 0;
135 jobject senderAddr;
136
137 if (len > MAX_PACKET_LEN) {
138 len = MAX_PACKET_LEN;
139 }
140
141 do {
142 retry = JNI_FALSE;
143 n = recvfrom(fd, buf, len, 0, &sa.sa, &sa_len);
144 if (n < 0) {
145 if (errno == EAGAIN || errno == EWOULDBLOCK) {
146 return IOS_UNAVAILABLE;
147 }
148 if (errno == EINTR) {
149 return IOS_INTERRUPTED;
150 }
151 if (errno == ECONNREFUSED) {
152 if (connected == JNI_FALSE) {
153 retry = JNI_TRUE;
154 } else {
155 JNU_ThrowByName(env, JNU_JAVANETPKG
156 "PortUnreachableException", 0);
157 return IOS_THROWN;
158 }
159 } else {
160 return handleSocketError(env, errno);
161 }
162 }
163 } while (retry == JNI_TRUE);
164
165 /*
200 jboolean preferIPv6, jobject fdo, jlong address,
201 jint len, jobject destAddress, jint destPort)
202 {
203 jint fd = fdval(env, fdo);
204 void *buf = (void *)jlong_to_ptr(address);
205 SOCKETADDRESS sa;
206 int sa_len = 0;
207 jint n = 0;
208
209 if (len > MAX_PACKET_LEN) {
210 len = MAX_PACKET_LEN;
211 }
212
213 if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa,
214 &sa_len, preferIPv6) != 0) {
215 return IOS_THROWN;
216 }
217
218 n = sendto(fd, buf, len, 0, &sa.sa, sa_len);
219 if (n < 0) {
220 if (errno == EAGAIN || errno == EWOULDBLOCK) {
221 return IOS_UNAVAILABLE;
222 }
223 if (errno == EINTR) {
224 return IOS_INTERRUPTED;
225 }
226 if (errno == ECONNREFUSED) {
227 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
228 return IOS_THROWN;
229 }
230 return handleSocketError(env, errno);
231 }
232 return n;
233 }
|