author | alanb |
Sat, 30 Nov 2019 16:21:19 +0000 | |
changeset 59329 | 289000934908 |
parent 50275 | 69204b98dc3d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50275
69204b98dc3d
8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, 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 <netdb.h> |
|
27 |
#include <sys/types.h> |
|
28 |
#include <sys/socket.h> |
|
29 |
#include <stdlib.h> |
|
30 |
#include <string.h> |
|
31 |
#include <errno.h> |
|
32 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
33 |
#if defined(__linux__) || defined(_ALLBSD_SOURCE) |
2 | 34 |
#include <netinet/in.h> |
35 |
#endif |
|
36 |
||
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
37 |
#include "jni.h" |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
38 |
#include "jni_util.h" |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
39 |
#include "jlong.h" |
2 | 40 |
#include "net_util.h" |
41 |
#include "nio.h" |
|
42 |
#include "nio_util.h" |
|
43 |
||
44 |
#include "sun_nio_ch_DatagramChannelImpl.h" |
|
45 |
||
46 |
JNIEXPORT void JNICALL |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
47 |
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jclass clazz, |
12547
ae1b2051db5d
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
12047
diff
changeset
|
48 |
jobject fdo, jboolean isIPv6) |
2 | 49 |
{ |
50 |
jint fd = fdval(env, fdo); |
|
51 |
int rv; |
|
52 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
53 |
#if defined(__solaris__) |
2 | 54 |
rv = connect(fd, 0, 0); |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
55 |
#else |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
56 |
SOCKETADDRESS sa; |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
57 |
socklen_t len = isIPv6 ? sizeof(struct sockaddr_in6) : |
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
58 |
sizeof(struct sockaddr_in); |
2 | 59 |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
60 |
memset(&sa, 0, sizeof(sa)); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
61 |
#if defined(_ALLBSD_SOURCE) |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
62 |
sa.sa.sa_family = isIPv6 ? AF_INET6 : AF_INET; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
63 |
#else |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
64 |
sa.sa.sa_family = AF_UNSPEC; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
65 |
#endif |
2 | 66 |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
67 |
rv = connect(fd, &sa.sa, len); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
68 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
69 |
#if defined(_ALLBSD_SOURCE) |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
70 |
if (rv < 0 && errno == EADDRNOTAVAIL) |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
71 |
rv = errno = 0; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
72 |
#elif defined(_AIX) |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
73 |
/* See W. Richard Stevens, "UNIX Network Programming, Volume 1", p. 254: |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
74 |
* 'Setting the address family to AF_UNSPEC might return EAFNOSUPPORT |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
75 |
* but that is acceptable. |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
76 |
*/ |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
77 |
if (rv < 0 && errno == EAFNOSUPPORT) |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
78 |
rv = errno = 0; |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
79 |
#endif // defined(_ALLBSD_SOURCE) || defined(_AIX) |
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
80 |
|
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
81 |
#endif // defined(__solaris__) |
2 | 82 |
|
83 |
if (rv < 0) |
|
84 |
handleSocketError(env, errno); |
|
85 |
} |
|
86 |
||
87 |
JNIEXPORT jint JNICALL |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
88 |
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jclass clazz, |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
89 |
jobject fdo, jlong bufAddress, |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
90 |
jint len, jlong senderAddress, |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
91 |
jboolean connected) |
2 | 92 |
{ |
93 |
jint fd = fdval(env, fdo); |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
94 |
void *buf = (void *)jlong_to_ptr(bufAddress); |
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
95 |
SOCKETADDRESS *sa = (SOCKETADDRESS *)jlong_to_ptr(senderAddress); |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
96 |
socklen_t sa_len = sizeof(SOCKETADDRESS); |
2 | 97 |
jboolean retry = JNI_FALSE; |
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
98 |
jint n; |
2 | 99 |
|
100 |
if (len > MAX_PACKET_LEN) { |
|
101 |
len = MAX_PACKET_LEN; |
|
102 |
} |
|
103 |
||
104 |
do { |
|
105 |
retry = JNI_FALSE; |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
106 |
n = recvfrom(fd, buf, len, 0, (struct sockaddr *)sa, &sa_len); |
2 | 107 |
if (n < 0) { |
50275
69204b98dc3d
8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents:
47216
diff
changeset
|
108 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
2 | 109 |
return IOS_UNAVAILABLE; |
110 |
} |
|
111 |
if (errno == EINTR) { |
|
112 |
return IOS_INTERRUPTED; |
|
113 |
} |
|
114 |
if (errno == ECONNREFUSED) { |
|
115 |
if (connected == JNI_FALSE) { |
|
116 |
retry = JNI_TRUE; |
|
117 |
} else { |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
118 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
2 | 119 |
return IOS_THROWN; |
120 |
} |
|
121 |
} else { |
|
122 |
return handleSocketError(env, errno); |
|
123 |
} |
|
124 |
} |
|
125 |
} while (retry == JNI_TRUE); |
|
126 |
||
127 |
return n; |
|
128 |
} |
|
129 |
||
130 |
JNIEXPORT jint JNICALL |
|
59329
289000934908
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation
alanb
parents:
50275
diff
changeset
|
131 |
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jclass clazz, |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
895
diff
changeset
|
132 |
jboolean preferIPv6, jobject fdo, jlong address, |
16089 | 133 |
jint len, jobject destAddress, jint destPort) |
2 | 134 |
{ |
135 |
jint fd = fdval(env, fdo); |
|
136 |
void *buf = (void *)jlong_to_ptr(address); |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
137 |
SOCKETADDRESS sa; |
43100 | 138 |
int sa_len = 0; |
2 | 139 |
jint n = 0; |
140 |
||
141 |
if (len > MAX_PACKET_LEN) { |
|
142 |
len = MAX_PACKET_LEN; |
|
143 |
} |
|
144 |
||
43100 | 145 |
if (NET_InetAddressToSockaddr(env, destAddress, destPort, &sa, |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
895
diff
changeset
|
146 |
&sa_len, preferIPv6) != 0) { |
2 | 147 |
return IOS_THROWN; |
148 |
} |
|
149 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
29118
diff
changeset
|
150 |
n = sendto(fd, buf, len, 0, &sa.sa, sa_len); |
2 | 151 |
if (n < 0) { |
50275
69204b98dc3d
8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents:
47216
diff
changeset
|
152 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
2 | 153 |
return IOS_UNAVAILABLE; |
154 |
} |
|
155 |
if (errno == EINTR) { |
|
156 |
return IOS_INTERRUPTED; |
|
157 |
} |
|
158 |
if (errno == ECONNREFUSED) { |
|
159 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0); |
|
160 |
return IOS_THROWN; |
|
161 |
} |
|
162 |
return handleSocketError(env, errno); |
|
163 |
} |
|
164 |
return n; |
|
165 |
} |