1 /*
2 * Copyright (c) 2001, 2012, 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
145 Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
146 jobject fdo, jlong address,
147 jint len, jboolean connected)
148 {
149 jint fd = fdval(env, fdo);
150 void *buf = (void *)jlong_to_ptr(address);
151 SOCKADDR sa;
152 socklen_t sa_len = SOCKADDR_LEN;
153 jboolean retry = JNI_FALSE;
154 jint n = 0;
155 jobject senderAddr;
156
157 if (len > MAX_PACKET_LEN) {
158 len = MAX_PACKET_LEN;
159 }
160
161 do {
162 retry = JNI_FALSE;
163 n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len);
164 if (n < 0) {
165 if (errno == EWOULDBLOCK) {
166 return IOS_UNAVAILABLE;
167 }
168 if (errno == EINTR) {
169 return IOS_INTERRUPTED;
170 }
171 if (errno == ECONNREFUSED) {
172 if (connected == JNI_FALSE) {
173 retry = JNI_TRUE;
174 } else {
175 JNU_ThrowByName(env, JNU_JAVANETPKG
176 "PortUnreachableException", 0);
177 return IOS_THROWN;
178 }
179 } else {
180 return handleSocketError(env, errno);
181 }
182 }
183 } while (retry == JNI_TRUE);
184
185 /*
222 jint len, jobject destAddress, jint destPort)
223 {
224 jint fd = fdval(env, fdo);
225 void *buf = (void *)jlong_to_ptr(address);
226 SOCKADDR sa;
227 int sa_len = SOCKADDR_LEN;
228 jint n = 0;
229
230 if (len > MAX_PACKET_LEN) {
231 len = MAX_PACKET_LEN;
232 }
233
234 if (NET_InetAddressToSockaddr(env, destAddress, destPort,
235 (struct sockaddr *)&sa,
236 &sa_len, preferIPv6) != 0) {
237 return IOS_THROWN;
238 }
239
240 n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len);
241 if (n < 0) {
242 if (errno == EAGAIN) {
243 return IOS_UNAVAILABLE;
244 }
245 if (errno == EINTR) {
246 return IOS_INTERRUPTED;
247 }
248 if (errno == ECONNREFUSED) {
249 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
250 return IOS_THROWN;
251 }
252 return handleSocketError(env, errno);
253 }
254 return n;
255 }
|
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
145 Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
146 jobject fdo, jlong address,
147 jint len, jboolean connected)
148 {
149 jint fd = fdval(env, fdo);
150 void *buf = (void *)jlong_to_ptr(address);
151 SOCKADDR sa;
152 socklen_t sa_len = SOCKADDR_LEN;
153 jboolean retry = JNI_FALSE;
154 jint n = 0;
155 jobject senderAddr;
156
157 if (len > MAX_PACKET_LEN) {
158 len = MAX_PACKET_LEN;
159 }
160
161 do {
162 retry = JNI_FALSE;
163 n = recvfrom(fd, buf, len, 0, (struct sockaddr *)&sa, &sa_len);
164 if (n < 0) {
165 if (errno == EAGAIN || errno == EWOULDBLOCK) {
166 return IOS_UNAVAILABLE;
167 }
168 if (errno == EINTR) {
169 return IOS_INTERRUPTED;
170 }
171 if (errno == ECONNREFUSED) {
172 if (connected == JNI_FALSE) {
173 retry = JNI_TRUE;
174 } else {
175 JNU_ThrowByName(env, JNU_JAVANETPKG
176 "PortUnreachableException", 0);
177 return IOS_THROWN;
178 }
179 } else {
180 return handleSocketError(env, errno);
181 }
182 }
183 } while (retry == JNI_TRUE);
184
185 /*
222 jint len, jobject destAddress, jint destPort)
223 {
224 jint fd = fdval(env, fdo);
225 void *buf = (void *)jlong_to_ptr(address);
226 SOCKADDR sa;
227 int sa_len = SOCKADDR_LEN;
228 jint n = 0;
229
230 if (len > MAX_PACKET_LEN) {
231 len = MAX_PACKET_LEN;
232 }
233
234 if (NET_InetAddressToSockaddr(env, destAddress, destPort,
235 (struct sockaddr *)&sa,
236 &sa_len, preferIPv6) != 0) {
237 return IOS_THROWN;
238 }
239
240 n = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sa_len);
241 if (n < 0) {
242 if (errno == EAGAIN || errno == EWOULDBLOCK) {
243 return IOS_UNAVAILABLE;
244 }
245 if (errno == EINTR) {
246 return IOS_INTERRUPTED;
247 }
248 if (errno == ECONNREFUSED) {
249 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
250 return IOS_THROWN;
251 }
252 return handleSocketError(env, errno);
253 }
254 return n;
255 }
|