author | mcberg |
Tue, 05 Apr 2016 11:37:41 -0700 | |
changeset 37293 | c010188d360f |
parent 28970 | aaec841a1a3c |
child 41380 | c27cf95dd7e6 |
permissions | -rw-r--r-- |
2542 | 1 |
/* |
28970
aaec841a1a3c
8067846: (sctp) InternalError when receiving SendFailedNotification
robm
parents:
28059
diff
changeset
|
2 |
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. |
2542 | 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 |
2542 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2542 | 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. |
|
2542 | 24 |
*/ |
25 |
||
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include "Sctp.h" |
|
29 |
||
30 |
#include "jni.h" |
|
31 |
#include "nio_util.h" |
|
32 |
#include "nio.h" |
|
33 |
#include "net_util.h" |
|
34 |
#include "net_util_md.h" |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
35 |
#include "sun_nio_ch_sctp_SctpNet.h" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
36 |
#include "sun_nio_ch_sctp_SctpChannelImpl.h" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
37 |
#include "sun_nio_ch_sctp_AssociationChange.h" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
38 |
#include "sun_nio_ch_sctp_ResultContainer.h" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
39 |
#include "sun_nio_ch_sctp_PeerAddrChange.h" |
2542 | 40 |
|
23575 | 41 |
static int SCTP_NOTIFICATION_SIZE = sizeof(union sctp_notification); |
2542 | 42 |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
43 |
#define MESSAGE_IMPL_CLASS "sun/nio/ch/sctp/MessageInfoImpl" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
44 |
#define RESULT_CONTAINER_CLASS "sun/nio/ch/sctp/ResultContainer" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
45 |
#define SEND_FAILED_CLASS "sun/nio/ch/sctp/SendFailed" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
46 |
#define ASSOC_CHANGE_CLASS "sun/nio/ch/sctp/AssociationChange" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
47 |
#define PEER_CHANGE_CLASS "sun/nio/ch/sctp/PeerAddrChange" |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
48 |
#define SHUTDOWN_CLASS "sun/nio/ch/sctp/Shutdown" |
2542 | 49 |
|
50 |
struct controlData { |
|
51 |
int assocId; |
|
52 |
unsigned short streamNumber; |
|
53 |
jboolean unordered; |
|
54 |
unsigned int ppid; |
|
55 |
}; |
|
56 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
57 |
static jclass smi_class; /* sun.nio.ch.sctp.MessageInfoImpl */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
58 |
static jmethodID smi_ctrID; /* sun.nio.ch.sctp.MessageInfoImpl.<init> */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
59 |
static jfieldID src_valueID; /* sun.nio.ch.sctp.ResultContainer.value */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
60 |
static jfieldID src_typeID; /* sun.nio.ch.sctp.ResultContainer.type */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
61 |
static jclass ssf_class; /* sun.nio.ch.sctp.SendFailed */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
62 |
static jmethodID ssf_ctrID; /* sun.nio.ch.sctp.SendFailed.<init> */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
63 |
static jclass sac_class; /* sun.nio.ch.sctp.AssociationChange */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
64 |
static jmethodID sac_ctrID; /* sun.nio.ch.sctp.AssociationChange.<init> */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
65 |
static jclass spc_class; /* sun.nio.ch.sctp.PeerAddressChanged */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
66 |
static jmethodID spc_ctrID; /* sun.nio.ch.sctp.PeerAddressChanged.<init> */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
67 |
static jclass ss_class; /* sun.nio.ch.sctp.Shutdown */ |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
68 |
static jmethodID ss_ctrID; /* sun.nio.ch.sctp.Shutdown.<init> */ |
2542 | 69 |
|
70 |
/* defined in SctpNet.c */ |
|
71 |
jobject SockAddrToInetSocketAddress(JNIEnv* env, struct sockaddr* addr); |
|
72 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
73 |
jint handleSocketError(JNIEnv *env, jint errorValue); |
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
74 |
|
2542 | 75 |
/* use SocketChannelImpl's checkConnect implementation */ |
76 |
extern jint Java_sun_nio_ch_SocketChannelImpl_checkConnect(JNIEnv* env, |
|
77 |
jobject this, jobject fdo, jboolean block, jboolean ready); |
|
78 |
||
79 |
/* |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
80 |
* Class: sun_nio_ch_sctp_SctpChannelImpl |
2542 | 81 |
* Method: initIDs |
82 |
* Signature: ()V |
|
83 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
84 |
JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_initIDs |
2542 | 85 |
(JNIEnv *env, jclass klass) { |
86 |
jclass cls; |
|
87 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
88 |
/* MessageInfoImpl */ |
2542 | 89 |
cls = (*env)->FindClass(env, MESSAGE_IMPL_CLASS); |
90 |
CHECK_NULL(cls); |
|
91 |
smi_class = (*env)->NewGlobalRef(env, cls); |
|
92 |
CHECK_NULL(smi_class); |
|
93 |
smi_ctrID = (*env)->GetMethodID(env, cls, "<init>", |
|
94 |
"(ILjava/net/SocketAddress;IIZZI)V"); |
|
95 |
CHECK_NULL(smi_ctrID); |
|
96 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
97 |
/* ResultContainer */ |
2542 | 98 |
cls = (*env)->FindClass(env, RESULT_CONTAINER_CLASS); |
99 |
CHECK_NULL(cls); |
|
100 |
src_valueID = (*env)->GetFieldID(env, cls, "value", "Ljava/lang/Object;"); |
|
101 |
CHECK_NULL(src_valueID); |
|
102 |
src_typeID = (*env)->GetFieldID(env, cls, "type", "I"); |
|
103 |
CHECK_NULL(src_typeID); |
|
104 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
105 |
/* SendFailed */ |
2542 | 106 |
cls = (*env)->FindClass(env, SEND_FAILED_CLASS); |
107 |
CHECK_NULL(cls); |
|
108 |
ssf_class = (*env)->NewGlobalRef(env, cls); |
|
109 |
CHECK_NULL(ssf_class); |
|
110 |
ssf_ctrID = (*env)->GetMethodID(env, cls, "<init>", |
|
111 |
"(ILjava/net/SocketAddress;Ljava/nio/ByteBuffer;II)V"); |
|
112 |
CHECK_NULL(ssf_ctrID); |
|
113 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
114 |
/* AssociationChange */ |
2542 | 115 |
cls = (*env)->FindClass(env, ASSOC_CHANGE_CLASS); |
116 |
CHECK_NULL(cls); |
|
117 |
sac_class = (*env)->NewGlobalRef(env, cls); |
|
118 |
CHECK_NULL(sac_class); |
|
119 |
sac_ctrID = (*env)->GetMethodID(env, cls, "<init>", "(IIII)V"); |
|
120 |
CHECK_NULL(sac_ctrID); |
|
121 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
122 |
/* PeerAddrChange */ |
2542 | 123 |
cls = (*env)->FindClass(env, PEER_CHANGE_CLASS); |
124 |
CHECK_NULL(cls); |
|
125 |
spc_class = (*env)->NewGlobalRef(env, cls); |
|
126 |
CHECK_NULL(spc_class); |
|
127 |
spc_ctrID = (*env)->GetMethodID(env, cls, "<init>", |
|
128 |
"(ILjava/net/SocketAddress;I)V"); |
|
129 |
CHECK_NULL(spc_ctrID); |
|
130 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
131 |
/* Shutdown */ |
2542 | 132 |
cls = (*env)->FindClass(env, SHUTDOWN_CLASS); |
133 |
CHECK_NULL(cls); |
|
134 |
ss_class = (*env)->NewGlobalRef(env, cls); |
|
135 |
CHECK_NULL(ss_class); |
|
136 |
ss_ctrID = (*env)->GetMethodID(env, cls, "<init>", "(I)V"); |
|
137 |
CHECK_NULL(ss_ctrID); |
|
138 |
} |
|
139 |
||
140 |
void getControlData |
|
141 |
(struct msghdr* msg, struct controlData* cdata) { |
|
142 |
struct cmsghdr* cmsg; |
|
143 |
||
144 |
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
|
145 |
if (cmsg->cmsg_level == IPPROTO_SCTP && cmsg->cmsg_type == SCTP_SNDRCV) { |
|
146 |
struct sctp_sndrcvinfo *sri; |
|
147 |
||
148 |
sri = (struct sctp_sndrcvinfo *) CMSG_DATA(cmsg); |
|
149 |
cdata->assocId = sri->sinfo_assoc_id; |
|
150 |
cdata->streamNumber = sri->sinfo_stream; |
|
151 |
cdata->unordered = (sri->sinfo_flags & SCTP_UNORDERED) ? JNI_TRUE : |
|
152 |
JNI_FALSE; |
|
153 |
cdata->ppid = ntohl(sri->sinfo_ppid); |
|
154 |
||
155 |
return; |
|
156 |
} |
|
157 |
} |
|
158 |
return; |
|
159 |
} |
|
160 |
||
161 |
void setControlData |
|
162 |
(struct msghdr* msg, struct controlData* cdata) { |
|
163 |
struct cmsghdr* cmsg; |
|
164 |
struct sctp_sndrcvinfo *sri; |
|
165 |
||
166 |
cmsg = CMSG_FIRSTHDR(msg); |
|
167 |
cmsg->cmsg_level = IPPROTO_SCTP; |
|
168 |
cmsg->cmsg_type = SCTP_SNDRCV; |
|
169 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
|
170 |
||
171 |
/* Initialize the payload */ |
|
172 |
sri = (struct sctp_sndrcvinfo*) CMSG_DATA(cmsg); |
|
173 |
memset(sri, 0, sizeof (*sri)); |
|
174 |
||
175 |
if (cdata->streamNumber > 0) { |
|
176 |
sri->sinfo_stream = cdata->streamNumber; |
|
177 |
} |
|
178 |
if (cdata->assocId > 0) { |
|
179 |
sri->sinfo_assoc_id = cdata->assocId; |
|
180 |
} |
|
181 |
if (cdata->unordered == JNI_TRUE) { |
|
182 |
sri->sinfo_flags = sri->sinfo_flags | SCTP_UNORDERED; |
|
183 |
} |
|
184 |
||
185 |
if (cdata->ppid > 0) { |
|
186 |
sri->sinfo_ppid = htonl(cdata->ppid); |
|
187 |
} |
|
188 |
||
189 |
/* Sum of the length of all control messages in the buffer. */ |
|
190 |
msg->msg_controllen = cmsg->cmsg_len; |
|
191 |
} |
|
192 |
||
193 |
// TODO: test: can create send failed without any data? if so need to |
|
194 |
// update API so that buffer can be null if no data. |
|
195 |
void handleSendFailed |
|
196 |
(JNIEnv* env, int fd, jobject resultContainerObj, struct sctp_send_failed *ssf, |
|
197 |
int read, jboolean isEOR, struct sockaddr* sap) { |
|
198 |
jobject bufferObj = NULL, resultObj, isaObj; |
|
199 |
char *addressP; |
|
200 |
struct sctp_sndrcvinfo *sri; |
|
201 |
int remaining, dataLength; |
|
202 |
||
203 |
/* the actual undelivered message data is directly after the ssf */ |
|
204 |
int dataOffset = sizeof(struct sctp_send_failed); |
|
205 |
||
206 |
sri = (struct sctp_sndrcvinfo*) &ssf->ssf_info; |
|
207 |
||
208 |
/* the number of bytes remaining to be read in the sctp_send_failed notif*/ |
|
209 |
remaining = ssf->ssf_length - read; |
|
210 |
||
211 |
/* the size of the actual undelivered message */ |
|
212 |
dataLength = ssf->ssf_length - dataOffset; |
|
213 |
||
214 |
/* retrieved address from sockaddr */ |
|
215 |
isaObj = SockAddrToInetSocketAddress(env, sap); |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16100
diff
changeset
|
216 |
CHECK_NULL(isaObj); |
2542 | 217 |
|
218 |
/* data retrieved from sff_data */ |
|
219 |
if (dataLength > 0) { |
|
220 |
struct iovec iov[1]; |
|
221 |
struct msghdr msg[1]; |
|
222 |
int rv, alreadyRead; |
|
223 |
char *dataP = (char*) ssf; |
|
224 |
dataP += dataOffset; |
|
225 |
||
226 |
if ((addressP = malloc(dataLength)) == NULL) { |
|
227 |
JNU_ThrowOutOfMemoryError(env, "handleSendFailed"); |
|
228 |
return; |
|
229 |
} |
|
230 |
||
231 |
memset(msg, 0, sizeof (*msg)); |
|
232 |
msg->msg_iov = iov; |
|
233 |
msg->msg_iovlen = 1; |
|
234 |
||
235 |
bufferObj = (*env)->NewDirectByteBuffer(env, addressP, dataLength); |
|
236 |
CHECK_NULL(bufferObj); |
|
237 |
||
238 |
alreadyRead = read - dataOffset; |
|
239 |
if (alreadyRead > 0) { |
|
240 |
memcpy(addressP, /*ssf->ssf_data*/ dataP, alreadyRead); |
|
241 |
iov->iov_base = addressP + alreadyRead; |
|
242 |
iov->iov_len = dataLength - alreadyRead; |
|
243 |
} else { |
|
244 |
iov->iov_base = addressP; |
|
245 |
iov->iov_len = dataLength; |
|
246 |
} |
|
247 |
||
248 |
if (remaining > 0) { |
|
249 |
if ((rv = recvmsg(fd, msg, 0)) < 0) { |
|
250 |
handleSocketError(env, errno); |
|
251 |
return; |
|
252 |
} |
|
253 |
||
254 |
if (rv != (dataLength - alreadyRead) || !(msg->msg_flags & MSG_EOR)) { |
|
255 |
//TODO: assert false: "should not reach here"; |
|
256 |
return; |
|
257 |
} |
|
258 |
// TODO: Set and document (in API) buffers position. |
|
259 |
} |
|
260 |
} |
|
261 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
262 |
/* create SendFailed */ |
2542 | 263 |
resultObj = (*env)->NewObject(env, ssf_class, ssf_ctrID, ssf->ssf_assoc_id, |
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
264 |
isaObj, bufferObj, ssf->ssf_error, sri->sinfo_stream); |
2542 | 265 |
CHECK_NULL(resultObj); |
266 |
(*env)->SetObjectField(env, resultContainerObj, src_valueID, resultObj); |
|
267 |
(*env)->SetIntField(env, resultContainerObj, src_typeID, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
268 |
sun_nio_ch_sctp_ResultContainer_SEND_FAILED); |
2542 | 269 |
} |
270 |
||
271 |
void handleAssocChange |
|
272 |
(JNIEnv* env, jobject resultContainerObj, struct sctp_assoc_change *sac) { |
|
273 |
jobject resultObj; |
|
274 |
int state = 0; |
|
275 |
||
276 |
switch (sac->sac_state) { |
|
277 |
case SCTP_COMM_UP : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
278 |
state = sun_nio_ch_sctp_AssociationChange_SCTP_COMM_UP; |
2542 | 279 |
break; |
280 |
case SCTP_COMM_LOST : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
281 |
state = sun_nio_ch_sctp_AssociationChange_SCTP_COMM_LOST; |
2542 | 282 |
break; |
283 |
case SCTP_RESTART : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
284 |
state = sun_nio_ch_sctp_AssociationChange_SCTP_RESTART; |
2542 | 285 |
break; |
286 |
case SCTP_SHUTDOWN_COMP : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
287 |
state = sun_nio_ch_sctp_AssociationChange_SCTP_SHUTDOWN; |
2542 | 288 |
break; |
289 |
case SCTP_CANT_STR_ASSOC : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
290 |
state = sun_nio_ch_sctp_AssociationChange_SCTP_CANT_START; |
2542 | 291 |
} |
292 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
293 |
/* create AssociationChange */ |
2542 | 294 |
resultObj = (*env)->NewObject(env, sac_class, sac_ctrID, sac->sac_assoc_id, |
295 |
state, sac->sac_outbound_streams, sac->sac_inbound_streams); |
|
296 |
CHECK_NULL(resultObj); |
|
297 |
(*env)->SetObjectField(env, resultContainerObj, src_valueID, resultObj); |
|
298 |
(*env)->SetIntField(env, resultContainerObj, src_typeID, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
299 |
sun_nio_ch_sctp_ResultContainer_ASSOCIATION_CHANGED); |
2542 | 300 |
} |
301 |
||
302 |
void handleShutdown |
|
303 |
(JNIEnv* env, jobject resultContainerObj, struct sctp_shutdown_event* sse) { |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
304 |
/* create Shutdown */ |
2542 | 305 |
jobject resultObj = (*env)->NewObject(env, ss_class, ss_ctrID, sse->sse_assoc_id); |
306 |
CHECK_NULL(resultObj); |
|
307 |
(*env)->SetObjectField(env, resultContainerObj, src_valueID, resultObj); |
|
308 |
(*env)->SetIntField(env, resultContainerObj, src_typeID, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
309 |
sun_nio_ch_sctp_ResultContainer_SHUTDOWN); |
2542 | 310 |
} |
311 |
||
312 |
void handlePeerAddrChange |
|
313 |
(JNIEnv* env, jobject resultContainerObj, struct sctp_paddr_change* spc) { |
|
314 |
int event = 0; |
|
315 |
jobject addressObj, resultObj; |
|
316 |
unsigned int state = spc->spc_state; |
|
317 |
||
318 |
switch (state) { |
|
319 |
case SCTP_ADDR_AVAILABLE : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
320 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_AVAILABLE; |
2542 | 321 |
break; |
322 |
case SCTP_ADDR_UNREACHABLE : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
323 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_UNREACHABLE; |
2542 | 324 |
break; |
325 |
case SCTP_ADDR_REMOVED : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
326 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_REMOVED; |
2542 | 327 |
break; |
328 |
case SCTP_ADDR_ADDED : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
329 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_ADDED; |
2542 | 330 |
break; |
331 |
case SCTP_ADDR_MADE_PRIM : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
332 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_MADE_PRIM; |
2542 | 333 |
#ifdef __linux__ /* Solaris currently doesn't support SCTP_ADDR_CONFIRMED */ |
334 |
break; |
|
335 |
case SCTP_ADDR_CONFIRMED : |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
336 |
event = sun_nio_ch_sctp_PeerAddrChange_SCTP_ADDR_CONFIRMED; |
2542 | 337 |
#endif /* __linux__ */ |
338 |
} |
|
339 |
||
340 |
addressObj = SockAddrToInetSocketAddress(env, (struct sockaddr*)&spc->spc_aaddr); |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16100
diff
changeset
|
341 |
CHECK_NULL(addressObj); |
2542 | 342 |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
343 |
/* create PeerAddressChanged */ |
2542 | 344 |
resultObj = (*env)->NewObject(env, spc_class, spc_ctrID, spc->spc_assoc_id, |
345 |
addressObj, event); |
|
346 |
CHECK_NULL(resultObj); |
|
347 |
(*env)->SetObjectField(env, resultContainerObj, src_valueID, resultObj); |
|
348 |
(*env)->SetIntField(env, resultContainerObj, src_typeID, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
349 |
sun_nio_ch_sctp_ResultContainer_PEER_ADDRESS_CHANGED); |
2542 | 350 |
} |
351 |
||
352 |
void handleUninteresting |
|
353 |
(union sctp_notification *snp) { |
|
354 |
//fprintf(stdout,"\nNative: handleUninterestingNotification: Receive notification type [%u]", snp->sn_header.sn_type); |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Handle notifications from the SCTP stack. |
|
359 |
* Returns JNI_TRUE if the notification is one that is of interest to the |
|
360 |
* Java API, otherwise JNI_FALSE. |
|
361 |
*/ |
|
362 |
jboolean handleNotification |
|
363 |
(JNIEnv* env, int fd, jobject resultContainerObj, union sctp_notification* snp, |
|
364 |
int read, jboolean isEOR, struct sockaddr* sap) { |
|
365 |
switch (snp->sn_header.sn_type) { |
|
366 |
case SCTP_SEND_FAILED: |
|
367 |
handleSendFailed(env, fd, resultContainerObj, &snp->sn_send_failed, |
|
368 |
read, isEOR, sap); |
|
369 |
return JNI_TRUE; |
|
370 |
case SCTP_ASSOC_CHANGE: |
|
371 |
handleAssocChange(env, resultContainerObj, &snp->sn_assoc_change); |
|
372 |
return JNI_TRUE; |
|
373 |
case SCTP_SHUTDOWN_EVENT: |
|
374 |
handleShutdown(env, resultContainerObj, &snp->sn_shutdown_event); |
|
375 |
return JNI_TRUE; |
|
376 |
case SCTP_PEER_ADDR_CHANGE: |
|
377 |
handlePeerAddrChange(env, resultContainerObj, &snp->sn_paddr_change); |
|
378 |
return JNI_TRUE; |
|
379 |
default : |
|
380 |
/* the Java API is not interested in this event, maybe we are? */ |
|
381 |
handleUninteresting(snp); |
|
382 |
} |
|
383 |
return JNI_FALSE; |
|
384 |
} |
|
385 |
||
386 |
void handleMessage |
|
387 |
(JNIEnv* env, jobject resultContainerObj, struct msghdr* msg,int read, |
|
388 |
jboolean isEOR, struct sockaddr* sap) { |
|
389 |
jobject isa, resultObj; |
|
390 |
struct controlData cdata[1]; |
|
391 |
||
392 |
if (read == 0) { |
|
393 |
/* we reached EOF */ |
|
394 |
read = -1; |
|
395 |
} |
|
396 |
||
397 |
isa = SockAddrToInetSocketAddress(env, sap); |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
16100
diff
changeset
|
398 |
CHECK_NULL(isa); |
2542 | 399 |
getControlData(msg, cdata); |
400 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
401 |
/* create MessageInfoImpl */ |
2542 | 402 |
resultObj = (*env)->NewObject(env, smi_class, smi_ctrID, cdata->assocId, |
403 |
isa, read, cdata->streamNumber, |
|
404 |
isEOR ? JNI_TRUE : JNI_FALSE, |
|
405 |
cdata->unordered, cdata->ppid); |
|
406 |
CHECK_NULL(resultObj); |
|
407 |
(*env)->SetObjectField(env, resultContainerObj, src_valueID, resultObj); |
|
408 |
(*env)->SetIntField(env, resultContainerObj, src_typeID, |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
409 |
sun_nio_ch_sctp_ResultContainer_MESSAGE); |
2542 | 410 |
} |
411 |
||
412 |
/* |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
413 |
* Class: sun_nio_ch_sctp_SctpChannelImpl |
2542 | 414 |
* Method: receive0 |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
415 |
* Signature: (ILsun/nio/ch/sctp/ResultContainer;JIZ)I |
2542 | 416 |
*/ |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
417 |
JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_receive0 |
2542 | 418 |
(JNIEnv *env, jclass klass, jint fd, jobject resultContainerObj, |
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
419 |
jlong address, jint length, jboolean peek) { |
2542 | 420 |
SOCKADDR sa; |
421 |
int sa_len = sizeof(sa); |
|
422 |
ssize_t rv = 0; |
|
423 |
jlong *addr = jlong_to_ptr(address); |
|
424 |
struct iovec iov[1]; |
|
425 |
struct msghdr msg[1]; |
|
426 |
char cbuf[CMSG_SPACE(sizeof (struct sctp_sndrcvinfo))]; |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
427 |
int flags = peek == JNI_TRUE ? MSG_PEEK : 0; |
2542 | 428 |
|
429 |
/* Set up the msghdr structure for receiving */ |
|
430 |
memset(msg, 0, sizeof (*msg)); |
|
431 |
msg->msg_name = &sa; |
|
432 |
msg->msg_namelen = sa_len; |
|
433 |
iov->iov_base = addr; |
|
434 |
iov->iov_len = length; |
|
435 |
msg->msg_iov = iov; |
|
436 |
msg->msg_iovlen = 1; |
|
437 |
msg->msg_control = cbuf; |
|
438 |
msg->msg_controllen = sizeof(cbuf); |
|
439 |
msg->msg_flags = 0; |
|
440 |
||
441 |
do { |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
442 |
if ((rv = recvmsg(fd, msg, flags)) < 0) { |
2542 | 443 |
if (errno == EWOULDBLOCK) { |
444 |
return IOS_UNAVAILABLE; |
|
445 |
} else if (errno == EINTR) { |
|
446 |
return IOS_INTERRUPTED; |
|
447 |
||
448 |
#ifdef __linux__ |
|
449 |
} else if (errno == ENOTCONN) { |
|
450 |
/* ENOTCONN when EOF reached */ |
|
451 |
rv = 0; |
|
452 |
/* there will be no control data */ |
|
453 |
msg->msg_controllen = 0; |
|
454 |
#endif /* __linux__ */ |
|
455 |
||
456 |
} else { |
|
457 |
handleSocketError(env, errno); |
|
458 |
return 0; |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
if (msg->msg_flags & MSG_NOTIFICATION) { |
|
463 |
char *bufp = (char*)addr; |
|
464 |
union sctp_notification *snp; |
|
23575 | 465 |
jboolean allocated = JNI_FALSE; |
2542 | 466 |
|
23575 | 467 |
if (!(msg->msg_flags & MSG_EOR) && length < SCTP_NOTIFICATION_SIZE) { |
468 |
char* newBuf; |
|
2542 | 469 |
int rvSAVE = rv; |
23575 | 470 |
|
471 |
if ((newBuf = malloc(SCTP_NOTIFICATION_SIZE)) == NULL) { |
|
472 |
JNU_ThrowOutOfMemoryError(env, "Out of native heap space."); |
|
473 |
return -1; |
|
474 |
} |
|
475 |
allocated = JNI_TRUE; |
|
476 |
||
477 |
memcpy(newBuf, addr, rv); |
|
478 |
iov->iov_base = newBuf + rv; |
|
479 |
iov->iov_len = SCTP_NOTIFICATION_SIZE - rv; |
|
3320
a7c037dd2e14
6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents:
3072
diff
changeset
|
480 |
if ((rv = recvmsg(fd, msg, flags)) < 0) { |
2542 | 481 |
handleSocketError(env, errno); |
482 |
return 0; |
|
483 |
} |
|
23575 | 484 |
bufp = newBuf; |
2542 | 485 |
rv += rvSAVE; |
486 |
} |
|
23575 | 487 |
#ifdef __sparc |
488 |
else if ((intptr_t)addr & 0x3) { |
|
489 |
/* the given buffer is not 4 byte aligned */ |
|
490 |
char* newBuf; |
|
491 |
if ((newBuf = malloc(SCTP_NOTIFICATION_SIZE)) == NULL) { |
|
492 |
JNU_ThrowOutOfMemoryError(env, "Out of native heap space."); |
|
493 |
return -1; |
|
494 |
} |
|
495 |
allocated = JNI_TRUE; |
|
496 |
||
497 |
memcpy(newBuf, addr, rv); |
|
498 |
bufp = newBuf; |
|
499 |
} |
|
500 |
#endif |
|
2542 | 501 |
snp = (union sctp_notification *) bufp; |
502 |
if (handleNotification(env, fd, resultContainerObj, snp, rv, |
|
503 |
(msg->msg_flags & MSG_EOR), |
|
504 |
(struct sockaddr*)&sa ) == JNI_TRUE) { |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
505 |
/* We have received a notification that is of interest |
2542 | 506 |
to the Java API. The appropriate notification will be |
507 |
set in the result container. */ |
|
23575 | 508 |
if (allocated == JNI_TRUE) { |
509 |
free(bufp); |
|
510 |
} |
|
2542 | 511 |
return 0; |
512 |
} |
|
513 |
||
23575 | 514 |
if (allocated == JNI_TRUE) { |
515 |
free(bufp); |
|
516 |
} |
|
517 |
||
2542 | 518 |
// set iov back to addr, and reset msg_controllen |
519 |
iov->iov_base = addr; |
|
520 |
iov->iov_len = length; |
|
521 |
msg->msg_control = cbuf; |
|
522 |
msg->msg_controllen = sizeof(cbuf); |
|
523 |
} |
|
524 |
} while (msg->msg_flags & MSG_NOTIFICATION); |
|
525 |
||
526 |
handleMessage(env, resultContainerObj, msg, rv, |
|
527 |
(msg->msg_flags & MSG_EOR), (struct sockaddr*)&sa); |
|
528 |
return rv; |
|
529 |
} |
|
530 |
||
531 |
/* |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
532 |
* Class: sun_nio_ch_sctp_SctpChannelImpl |
2542 | 533 |
* Method: send0 |
16089 | 534 |
* Signature: (IJILjava/net/InetAddress;IIIZI)I |
2542 | 535 |
*/ |
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
536 |
JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_send0 |
2542 | 537 |
(JNIEnv *env, jclass klass, jint fd, jlong address, jint length, |
16089 | 538 |
jobject targetAddress, jint targetPort, jint assocId, jint streamNumber, |
539 |
jboolean unordered, jint ppid) { |
|
2542 | 540 |
SOCKADDR sa; |
541 |
int sa_len = sizeof(sa); |
|
542 |
ssize_t rv = 0; |
|
543 |
jlong *addr = jlong_to_ptr(address); |
|
544 |
struct iovec iov[1]; |
|
545 |
struct msghdr msg[1]; |
|
546 |
int cbuf_size = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo)); |
|
547 |
char cbuf[CMSG_SPACE(sizeof (struct sctp_sndrcvinfo))]; |
|
548 |
struct controlData cdata[1]; |
|
549 |
||
550 |
/* SctpChannel: |
|
16089 | 551 |
* targetAddress may contain the preferred address or NULL to use primary, |
2542 | 552 |
* assocId will always be -1 |
553 |
* SctpMultiChannell: |
|
16089 | 554 |
* Setup new association, targetAddress will contain address, assocId = -1 |
555 |
* Association already existing, assocId != -1, targetAddress = preferred addr |
|
2542 | 556 |
*/ |
16089 | 557 |
if (targetAddress != NULL /*&& assocId <= 0*/) { |
2542 | 558 |
if (NET_InetAddressToSockaddr(env, targetAddress, targetPort, |
559 |
(struct sockaddr *)&sa, |
|
560 |
&sa_len, JNI_TRUE) != 0) { |
|
561 |
return IOS_THROWN; |
|
562 |
} |
|
563 |
} else { |
|
564 |
memset(&sa, '\x0', sa_len); |
|
565 |
sa_len = 0; |
|
566 |
} |
|
567 |
||
568 |
/* Set up the msghdr structure for sending */ |
|
569 |
memset(msg, 0, sizeof (*msg)); |
|
570 |
memset(cbuf, 0, cbuf_size); |
|
571 |
msg->msg_name = &sa; |
|
572 |
msg->msg_namelen = sa_len; |
|
573 |
iov->iov_base = addr; |
|
574 |
iov->iov_len = length; |
|
575 |
msg->msg_iov = iov; |
|
576 |
msg->msg_iovlen = 1; |
|
577 |
msg->msg_control = cbuf; |
|
578 |
msg->msg_controllen = cbuf_size; |
|
579 |
msg->msg_flags = 0; |
|
580 |
||
581 |
cdata->streamNumber = streamNumber; |
|
582 |
cdata->assocId = assocId; |
|
583 |
cdata->unordered = unordered; |
|
584 |
cdata->ppid = ppid; |
|
585 |
setControlData(msg, cdata); |
|
586 |
||
587 |
if ((rv = sendmsg(fd, msg, 0)) < 0) { |
|
588 |
if (errno == EWOULDBLOCK) { |
|
589 |
return IOS_UNAVAILABLE; |
|
590 |
} else if (errno == EINTR) { |
|
591 |
return IOS_INTERRUPTED; |
|
592 |
} else if (errno == EPIPE) { |
|
593 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
594 |
"Socket is shutdown for writing"); |
|
595 |
} else { |
|
596 |
handleSocketError(env, errno); |
|
597 |
return 0; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
return rv; |
|
602 |
} |
|
603 |
||
604 |
/* |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
605 |
* Class: sun_nio_ch_sctp_SctpChannelImpl |
2542 | 606 |
* Method: checkConnect |
607 |
* Signature: (Ljava/io/FileDescriptor;ZZ)I |
|
608 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
609 |
JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_checkConnect |
2542 | 610 |
(JNIEnv* env, jobject this, jobject fdo, jboolean block, jboolean ready) { |
611 |
return Java_sun_nio_ch_SocketChannelImpl_checkConnect(env, this, |
|
612 |
fdo, block, ready); |
|
613 |
} |