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