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