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