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