jdk/src/solaris/native/sun/nio/ch/InheritedChannel.c
author alanb
Thu, 24 Jul 2008 12:46:41 +0100
changeset 895 67f1dc69ad10
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
6726309: Compiler warnings in nio code Reviewed-by: sherman, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <fcntl.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "sun_nio_ch_InheritedChannel.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static int matchFamily(struct sockaddr *sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int family = sa->sa_family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return (family == AF_INET6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    return (family == AF_INET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    struct sockaddr *sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    socklen_t sa_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jobject remote_ia = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    jint remote_port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    NET_AllocSockaddr(&sa, (int *)&sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    if (getpeername(fd, sa, &sa_len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (matchFamily(sa)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    free((void *)sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    return remote_ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    struct sockaddr *sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    socklen_t sa_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    jint remote_port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    NET_AllocSockaddr(&sa, (int *)&sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (getpeername(fd, sa, &sa_len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (matchFamily(sa)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    free((void *)sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    return remote_port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    91
    int sotype;
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    92
    socklen_t arglen=sizeof(sotype);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (sotype == SOCK_STREAM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return sun_nio_ch_InheritedChannel_SOCK_STREAM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (sotype == SOCK_DGRAM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    return sun_nio_ch_InheritedChannel_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   int newfd = dup(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
   if (newfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        JNU_ThrowIOExceptionWithLastError(env, "dup failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
   return (jint)newfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
   if (dup2(fd, fd2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    const char* str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int oflag_actual;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /* convert to OS specific value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    switch (oflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        case sun_nio_ch_InheritedChannel_O_RDWR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            oflag_actual = O_RDWR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        case sun_nio_ch_InheritedChannel_O_RDONLY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            oflag_actual = O_RDONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        case sun_nio_ch_InheritedChannel_O_WRONLY :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            oflag_actual = O_WRONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            JNU_ThrowInternalError(env, "Unrecognized file mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    str = JNU_GetStringPlatformChars(env, path, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (str == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return (jint)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int fd = open(str, oflag_actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            JNU_ThrowIOExceptionWithLastError(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        JNU_ReleaseStringPlatformChars(env, path, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return (jint)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (close(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JNU_ThrowIOExceptionWithLastError(env, "close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}