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