jdk/src/solaris/native/sun/nio/ch/Net.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6850 56966b0a6a0d
child 8788 b98f18278dc4
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6850
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
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: 4814
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netinet/tcp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "sun_nio_ch_Net.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "net_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    42
/**
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    43
 * Definitions for source-specific multicast to allow for building
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    44
 * with older header files.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    45
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    46
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    47
#ifdef __solaris__
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    48
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    49
#ifndef IP_BLOCK_SOURCE
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    50
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    51
#define IP_BLOCK_SOURCE                 0x15
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    52
#define IP_UNBLOCK_SOURCE               0x16
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    53
#define IP_ADD_SOURCE_MEMBERSHIP        0x17
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    54
#define IP_DROP_SOURCE_MEMBERSHIP       0x18
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    55
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    56
#define MCAST_BLOCK_SOURCE              0x2b
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    57
#define MCAST_UNBLOCK_SOURCE            0x2c
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    58
#define MCAST_JOIN_SOURCE_GROUP         0x2d
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    59
#define MCAST_LEAVE_SOURCE_GROUP        0x2e
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    60
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    61
#endif  /* IP_BLOCK_SOURCE */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    62
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    63
struct my_ip_mreq_source {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    64
        struct in_addr  imr_multiaddr;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    65
        struct in_addr  imr_sourceaddr;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    66
        struct in_addr  imr_interface;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    67
};
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    68
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    69
/*
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    70
 * Use #pragma pack() construct to force 32-bit alignment on amd64.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    71
 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    72
#if defined(amd64)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    73
#pragma pack(4)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    74
#endif
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    75
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    76
struct my_group_source_req {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    77
        uint32_t                gsr_interface;  /* interface index */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    78
        struct sockaddr_storage gsr_group;      /* group address */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    79
        struct sockaddr_storage gsr_source;     /* source address */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    80
};
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    81
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    82
#if defined(amd64)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    83
#pragma pack()
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    84
#endif
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    85
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    86
#endif  /* __solaris__ */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    87
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    88
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#ifdef __linux__
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    90
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    91
#ifndef IP_BLOCK_SOURCE
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    92
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    93
#define IP_BLOCK_SOURCE                 38
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    94
#define IP_UNBLOCK_SOURCE               37
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    95
#define IP_ADD_SOURCE_MEMBERSHIP        39
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    96
#define IP_DROP_SOURCE_MEMBERSHIP       40
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    97
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    98
#define MCAST_BLOCK_SOURCE              43
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
    99
#define MCAST_UNBLOCK_SOURCE            44
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   100
#define MCAST_JOIN_SOURCE_GROUP         42
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   101
#define MCAST_LEAVE_SOURCE_GROUP        45
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   102
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   103
#endif  /* IP_BLOCK_SOURCE */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   104
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   105
struct my_ip_mreq_source {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   106
        struct in_addr  imr_multiaddr;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   107
        struct in_addr  imr_interface;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   108
        struct in_addr  imr_sourceaddr;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   109
};
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   110
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   111
struct my_group_source_req {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   112
        uint32_t                gsr_interface;  /* interface index */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   113
        struct sockaddr_storage gsr_group;      /* group address */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   114
        struct sockaddr_storage gsr_source;     /* source address */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   115
};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   117
#endif   /* __linux__ */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   118
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   119
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   120
#define COPY_INET6_ADDRESS(env, source, target) \
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   121
    (*env)->GetByteArrayRegion(env, source, 0, 16, target)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   122
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   123
/*
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   124
 * Copy IPv6 group, interface index, and IPv6 source address
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   125
 * into group_source_req structure.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   126
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   127
#ifdef AF_INET6
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   128
static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   129
                               jbyteArray source, struct my_group_source_req* req)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   130
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   131
    struct sockaddr_in6* sin6;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   132
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   133
    req->gsr_interface = (uint32_t)index;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   134
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   135
    sin6 = (struct sockaddr_in6*)&(req->gsr_group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   136
    sin6->sin6_family = AF_INET6;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   137
    COPY_INET6_ADDRESS(env, group, (jbyte*)&(sin6->sin6_addr));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   138
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   139
    sin6 = (struct sockaddr_in6*)&(req->gsr_source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   140
    sin6->sin6_family = AF_INET6;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   141
    COPY_INET6_ADDRESS(env, source, (jbyte*)&(sin6->sin6_addr));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   142
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   143
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /* Here because Windows native code does need to init IDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   151
JNIEXPORT jboolean JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   152
Java_sun_nio_ch_Net_isIPv6Available0(JNIEnv* env, jclass cl)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   153
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   154
    return (ipv6_available()) ? JNI_TRUE : JNI_FALSE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   155
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   156
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
JNIEXPORT int JNICALL
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   158
Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   159
                            jboolean stream, jboolean reuse)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int fd;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   162
    int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   163
#ifdef AF_INET6
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   164
    int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   165
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   166
    int domain = AF_INET;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   167
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   169
    fd = socket(domain, type, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   173
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   174
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   175
    /* Disable IPV6_V6ONLY to ensure dual-socket support */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   176
    if (domain == AF_INET6) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   177
        int arg = 0;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   178
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   179
                       sizeof(int)) < 0) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   180
            JNU_ThrowByNameWithLastError(env,
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   181
                                         JNU_JAVANETPKG "SocketException",
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   182
                                         "sun.nio.ch.Net.setIntOption");
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   183
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   184
            return -1;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   185
        }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   186
    }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   187
#endif
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (reuse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int arg = 1;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   191
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   192
                       sizeof(arg)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            JNU_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                         JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                         "sun.nio.ch.Net.setIntOption");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   196
            close(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   197
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   200
#if defined(__linux__) && defined(AF_INET6)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   201
    /* By default, Linux uses the route default */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   202
    if (domain == AF_INET6 && type == SOCK_DGRAM) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   203
        int arg = 1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   204
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &arg,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   205
                       sizeof(arg)) < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   206
            JNU_ThrowByNameWithLastError(env,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   207
                                         JNU_JAVANETPKG "SocketException",
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   208
                                         "sun.nio.ch.Net.setIntOption");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   209
            close(fd);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   210
            return -1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   211
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   212
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   213
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
JNIEXPORT void JNICALL
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   218
Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   219
                          jobject fdo, jobject iao, int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    SOCKADDR sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    int sa_len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    int rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   225
    if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    if (rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   235
JNIEXPORT void JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   236
Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   237
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   238
    if (listen(fdval(env, fdo), backlog) < 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   239
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   240
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   241
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
JNIEXPORT jint JNICALL
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   243
Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   244
                             jobject fdo, jobject iao, jint port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    SOCKADDR sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    int sa_len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   250
    if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   251
                                  &sa_len, preferIPv6) != 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   252
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    if (rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (errno == EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    SOCKADDR sa;
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   272
    socklen_t sa_len = SOCKADDR_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    SOCKADDR sa;
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   284
    socklen_t sa_len = SOCKADDR_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        handleSocketError(env, errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   293
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   294
Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   295
                                  jboolean mayNeedConversion, jint level, jint opt)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
{
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   297
    int result;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   298
    struct linger linger;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   299
    u_char carg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   300
    void *arg;
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 6299
diff changeset
   301
    socklen_t arglen;
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 6299
diff changeset
   302
    int n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   304
    /* Option value is an int except for a few specific cases */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   305
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   306
    arg = (void *)&result;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   307
    arglen = sizeof(result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   309
    if (level == IPPROTO_IP &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   310
        (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   311
        arg = (void*)&carg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   312
        arglen = sizeof(carg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   315
    if (level == SOL_SOCKET && opt == SO_LINGER) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   316
        arg = (void *)&linger;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   317
        arglen = sizeof(linger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   320
    if (mayNeedConversion) {
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 6299
diff changeset
   321
        n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, (int*)&arglen);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   322
    } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   323
        n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   325
    if (n < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        JNU_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                     JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                     "sun.nio.ch.Net.getIntOption");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   332
    if (level == IPPROTO_IP &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   333
        (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   334
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   335
        return (jint)carg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   336
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   337
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   338
    if (level == SOL_SOCKET && opt == SO_LINGER)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   339
        return linger.l_onoff ? (jint)linger.l_linger : (jint)-1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   340
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   341
    return (jint)result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
JNIEXPORT void JNICALL
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   345
Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   346
                                  jboolean mayNeedConversion, jint level, jint opt, jint arg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    struct linger linger;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   350
    u_char carg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    void *parg;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   352
    int arglen, n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   353
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   354
    /* Option value is an int except for a few specific cases */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   356
    parg = (void*)&arg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   357
    arglen = sizeof(arg);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   358
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   359
    if (level == IPPROTO_IP &&
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   360
        (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   361
        parg = (void*)&carg;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   362
        arglen = sizeof(carg);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   363
        carg = (u_char)arg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   366
    if (level == SOL_SOCKET && opt == SO_LINGER) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        parg = (void *)&linger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        arglen = sizeof(linger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (arg >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            linger.l_onoff = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            linger.l_linger = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            linger.l_onoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            linger.l_linger = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   378
    if (mayNeedConversion) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   379
        n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   380
    } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   381
        n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   382
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   383
    if (n < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        JNU_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                     JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                     "sun.nio.ch.Net.setIntOption");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   390
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   391
Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   392
                                jint group, jint interf, jint source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   393
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   394
    struct ip_mreq mreq;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   395
    struct my_ip_mreq_source mreq_source;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   396
    int opt, n, optlen;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   397
    void* optval;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   399
    if (source == 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   400
        mreq.imr_multiaddr.s_addr = htonl(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   401
        mreq.imr_interface.s_addr = htonl(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   402
        opt = (join) ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   403
        optval = (void*)&mreq;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   404
        optlen = sizeof(mreq);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   405
    } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   406
        mreq_source.imr_multiaddr.s_addr = htonl(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   407
        mreq_source.imr_sourceaddr.s_addr = htonl(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   408
        mreq_source.imr_interface.s_addr = htonl(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   409
        opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   410
        optval = (void*)&mreq_source;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   411
        optlen = sizeof(mreq_source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   412
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   413
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   414
    n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   415
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   416
        if (join && (errno == ENOPROTOOPT))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   417
            return IOS_UNAVAILABLE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   418
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   419
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   420
    return 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   421
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   422
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   423
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   424
Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   425
                                    jint group, jint interf, jint source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   426
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   427
    struct my_ip_mreq_source mreq_source;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   428
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   429
    int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   430
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   431
    mreq_source.imr_multiaddr.s_addr = htonl(group);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   432
    mreq_source.imr_sourceaddr.s_addr = htonl(source);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   433
    mreq_source.imr_interface.s_addr = htonl(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   434
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   435
    n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   436
                   (void*)&mreq_source, sizeof(mreq_source));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   437
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   438
        if (block && (errno == ENOPROTOOPT))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   439
            return IOS_UNAVAILABLE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   440
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   441
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   442
    return 0;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   443
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   444
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   445
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   446
Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   447
                                jbyteArray group, jint index, jbyteArray source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   448
{
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   449
#ifdef AF_INET6
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   450
    struct ipv6_mreq mreq6;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   451
    struct my_group_source_req req;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   452
    int opt, n, optlen;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   453
    void* optval;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   454
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   455
    if (source == NULL) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   456
        COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   457
        mreq6.ipv6mr_interface = (int)index;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   458
        opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   459
        optval = (void*)&mreq6;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   460
        optlen = sizeof(mreq6);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   461
    } else {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   462
#ifdef __linux__
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   463
        /* Include-mode filtering broken on Linux at least to 2.6.24 */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   464
        return IOS_UNAVAILABLE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   465
#else
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   466
        initGroupSourceReq(env, group, index, source, &req);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   467
        opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   468
        optval = (void*)&req;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   469
        optlen = sizeof(req);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   470
#endif
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   471
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   472
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   473
    n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   474
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   475
        if (join && (errno == ENOPROTOOPT))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   476
            return IOS_UNAVAILABLE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   477
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   478
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   479
    return 0;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   480
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   481
    JNU_ThrowInternalError(env, "Should not get here");
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   482
    return IOS_THROWN;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   483
#endif  /* AF_INET6 */
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   484
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   485
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   486
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   487
Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   488
                                    jbyteArray group, jint index, jbyteArray source)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   489
{
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   490
#ifdef AF_INET6
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   491
    struct my_group_source_req req;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   492
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   493
    int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   494
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   495
    initGroupSourceReq(env, group, index, source, &req);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   496
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   497
    n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   498
        (void*)&req, sizeof(req));
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   499
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   500
        if (block && (errno == ENOPROTOOPT))
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   501
            return IOS_UNAVAILABLE;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   502
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   503
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   504
    return 0;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   505
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   506
    JNU_ThrowInternalError(env, "Should not get here");
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   507
    return IOS_THROWN;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 3486
diff changeset
   508
#endif
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   509
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   510
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   511
JNIEXPORT void JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   512
Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint interf)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   513
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   514
    struct in_addr in;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   515
    int arglen = sizeof(struct in_addr);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   516
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   517
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   518
    in.s_addr = htonl(interf);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   519
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   520
    n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   521
                   (void*)&(in.s_addr), arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   522
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   523
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   524
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   525
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   526
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   527
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   528
Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   529
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   530
    struct in_addr in;
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 6299
diff changeset
   531
    socklen_t arglen = sizeof(struct in_addr);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   532
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   533
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   534
    n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   535
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   536
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   537
        return -1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   538
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   539
    return ntohl(in.s_addr);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   540
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   541
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   542
JNIEXPORT void JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   543
Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint index)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   544
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   545
    int value = (jint)index;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   546
    int arglen = sizeof(value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   547
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   548
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   549
    n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   550
                   (void*)&(index), arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   551
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   552
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   553
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   554
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   555
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   556
JNIEXPORT jint JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   557
Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   558
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   559
    int index;
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 6299
diff changeset
   560
    socklen_t arglen = sizeof(index);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   561
    int n;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   562
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   563
    n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   564
    if (n < 0) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   565
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   566
        return -1;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   567
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   568
    return (jint)index;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   569
}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   570
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   571
JNIEXPORT void JNICALL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   572
Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   573
{
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   574
    int how = (jhow == sun_nio_ch_Net_SHUT_RD) ? SHUT_RD :
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   575
        (jhow == sun_nio_ch_Net_SHUT_WR) ? SHUT_WR : SHUT_RDWR;
3486
0e14ae32369e 4516760: (so) Intermittent SocketException: Transport endpoint is not connected (lnx)
alanb
parents: 1247
diff changeset
   576
    if ((shutdown(fdval(env, fdo), how) < 0) && (errno != ENOTCONN))
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   577
        handleSocketError(env, errno);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 895
diff changeset
   578
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
/* Declared in nio_util.h */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
handleSocketError(JNIEnv *env, jint errorValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    char *xn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    switch (errorValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        case EINPROGRESS:       /* Non-blocking connect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        case EPROTO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            xn = JNU_JAVANETPKG "ProtocolException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        case ECONNREFUSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            xn = JNU_JAVANETPKG "ConnectException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        case ETIMEDOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            xn = JNU_JAVANETPKG "ConnectException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        case EHOSTUNREACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            xn = JNU_JAVANETPKG "NoRouteToHostException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        case EADDRINUSE:  /* Fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        case EADDRNOTAVAIL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            xn = JNU_JAVANETPKG "BindException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            xn = JNU_JAVANETPKG "SocketException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    errno = errorValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
}