jdk/src/solaris/native/java/net/net_util_md.c
author chegar
Tue, 29 Jul 2008 09:53:35 -0700
changeset 911 9edd1b9607e2
parent 910 1f53246fb014
child 1152 29d6145d1097
permissions -rw-r--r--
6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address" Reviewed-by: alanb, jccollet
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 1997-2007 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 <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netinet/tcp.h>        /* Defines TCP_NODELAY, needed for 2.6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <net/if.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <sys/sockio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <stropts.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <inet/nd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <arpa/inet.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <net/route.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <sys/utsname.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#ifndef IPV6_FLOWINFO_SEND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define IPV6_FLOWINFO_SEND      33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#include "java_net_SocketOptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/* needed from libsocket on Solaris 8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
getaddrinfo_f getaddrinfo_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
freeaddrinfo_f freeaddrinfo_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
getnameinfo_f getnameinfo_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * EXCLBIND socket options only on Solaris 8 & 9.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#if defined(__solaris__) && !defined(TCP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#define TCP_EXCLBIND            0x21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
#if defined(__solaris__) && !defined(UDP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define UDP_EXCLBIND            0x0101
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static int init_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static int tcp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static int udp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Get the specified parameter from the specified driver. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * of the parameter is assumed to be an 'int'. If the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * cannot be obtained return the specified default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
getParam(char *driver, char *param, int dflt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    struct strioctl stri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    char buf [64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    s = open (driver, O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return dflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    strncpy (buf, param, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    stri.ic_cmd = ND_GET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    stri.ic_timout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    stri.ic_dp = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    stri.ic_len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (ioctl (s, I_STR, &stri) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        value = dflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        value = atoi(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    close (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static int kernelV22 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static int vinit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
int kernelIsV22 () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if (!vinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        struct utsname sysinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (uname(&sysinfo) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            sysinfo.release[3] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (strcmp(sysinfo.release, "2.2") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                kernelV22 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        vinit = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    return kernelV22;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
static int kernelV24 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
static int vinit24 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
int kernelIsV24 () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (!vinit24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        struct utsname sysinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (uname(&sysinfo) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            sysinfo.release[3] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (strcmp(sysinfo.release, "2.4") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                kernelV24 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        vinit24 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    return kernelV24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
int getScopeID (struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if (kernelIsV22()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    return hext->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
int cmpScopeID (unsigned int scope, struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (kernelIsV22()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return 1;       /* scope is ignored for comparison in 2.2 kernel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    return hext->sin6_scope_id == scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
int getScopeID (struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    return him6->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
int cmpScopeID (unsigned int scope, struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    return him6->sin6_scope_id == scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                   const char *defaultDetail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    JNU_ThrowByNameWithLastError(env, name, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
NET_ThrowCurrent(JNIEnv *env, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    NET_ThrowNew(env, errno, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
NET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    char fullMsg[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        msg = "no further information";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    switch(errorNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    case EBADF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    case EINTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        errno = errorNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
jfieldID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
NET_GetFileDescriptorID(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    CHECK_NULL_RETURN(cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    return (*env)->GetFieldID(env, cls, "fd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
jint  IPv6_supported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
#ifndef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    void *ipv6_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    SOCKADDR sa;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   232
    socklen_t sa_len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         *  TODO: We really cant tell since it may be an unrelated error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         *  for now we will assume that AF_INET6 is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * If fd 0 is a socket it means we've been launched from inetd or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * xinetd. If it's a socket then check the family - if it's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * IPv4 socket then we need to disable IPv6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    if (getsockname(0, (struct sockaddr *)&sa, &sa_len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        struct sockaddr *saP = (struct sockaddr *)&sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (saP->sa_family != AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Linux - check if any interface has an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Don't need to parse the line - we just need an indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        FILE *fP = fopen("/proc/net/if_inet6", "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (fP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        bufP = fgets(buf, sizeof(buf), fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        fclose(fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * On Solaris 8 it's possible to create INET6 sockets even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * though IPv6 is not enabled on all interfaces. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * query the number of IPv6 addresses to verify that IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * has been configured on at least one interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * On Linux it doesn't matter - if IPv6 is built-in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * kernel then IPv6 addresses will be bound automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * to all interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
#ifdef SIOCGLIFNUM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        struct lifnum numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        numifs.lifn_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        numifs.lifn_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
             * SIOCGLIFNUM failed - assume IPv6 not configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * If no IPv6 addresses then return false. If count > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * it's possible that all IPv6 addresses are "down" but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * that's okay as they may be brought "up" while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * VM is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (numifs.lifn_count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /* SIOCGLIFNUM not defined in build environment ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
#endif /* __solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *  OK we may have the stack available in the kernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *  we should also check if the APIs are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if (ipv6_fn == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * We've got the library, let's get the pointers to some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * IPV6 specific functions. We have to do that because, at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * on Solaris we may build on a system without IPV6 networking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * libraries, therefore we can't have a hard link to these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    getaddrinfo_ptr = (getaddrinfo_f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        JVM_FindLibraryEntry(RTLD_DEFAULT, "getaddrinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    freeaddrinfo_ptr = (freeaddrinfo_f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        JVM_FindLibraryEntry(RTLD_DEFAULT, "freeaddrinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    getnameinfo_ptr = (getnameinfo_f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        JVM_FindLibraryEntry(RTLD_DEFAULT, "getnameinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    if (freeaddrinfo_ptr == NULL || getnameinfo_ptr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        /* Wee need all 3 of them */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        getaddrinfo_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
NET_AllocSockaddr(struct sockaddr **him, int *len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        struct sockaddr_in6 *him6 = (struct sockaddr_in6*)malloc(sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        *him = (struct sockaddr*)him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        *len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            struct sockaddr_in *him4 = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            *him = (struct sockaddr*)him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            *len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
/* following code creates a list of addresses from the kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * routing table that are routed via the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 * We check all destination addresses against this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 * and override the scope_id field to use the relevant value for "lo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 * in order to work-around the Linux bug that prevents packets destined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * for certain local addresses from being sent via a physical interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
struct loopback_route {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    struct in6_addr addr; /* destination address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    int plen; /* prefix length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
static struct loopback_route *loRoutes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
static int nRoutes = 0; /* number of routes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
static int loRoutes_size = 16; /* initial size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
static int lo_scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
static void initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
void printAddr (struct in6_addr *addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    for (i=0; i<16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        printf ("%02x", addr->s6_addr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    printf ("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
static jboolean needsLoopbackRoute (struct in6_addr* dest_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    int byte_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    int extra_bits, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    struct loopback_route *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if (loRoutes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    for (ptr = loRoutes, i=0; i<nRoutes; i++, ptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        struct in6_addr *target_addr=&ptr->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int dest_plen = ptr->plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        byte_count = dest_plen >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        extra_bits = dest_plen & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (byte_count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (memcmp(target_addr, dest_addr, byte_count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                continue;  /* no match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (extra_bits > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            unsigned char mask = 0xff << (8 - extra_bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if ((c1 & mask) != (c2 & mask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
static void initLoopbackRoutes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    char srcp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    char hopp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    int dest_plen, src_plen, use, refcnt, metric;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    unsigned long flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    char dest_str[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    struct in6_addr dest_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    char device[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    if (loRoutes != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        free (loRoutes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    loRoutes = calloc (loRoutes_size, sizeof(struct loopback_route));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    if (loRoutes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Scan /proc/net/ipv6_route looking for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                     "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                     "%4s%4s%4s%4s%4s%4s%4s%4s "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                     "%08x %08x %08x %08lx %8s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                     dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                     &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                     &dest_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                     srcp[0], srcp[1], srcp[2], srcp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                     srcp[4], srcp[5], srcp[6], srcp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                     &src_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                     hopp[0], hopp[1], hopp[2], hopp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                     hopp[4], hopp[5], hopp[6], hopp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                     &metric, &use, &refcnt, &flags, device) == 31) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         * Some routes should be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if ( (dest_plen < 0 || dest_plen > 128)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
             (src_plen != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             (flags & (RTF_POLICY | RTF_FLOW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             ((flags & RTF_REJECT) && dest_plen == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * Convert the destination address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        dest_str[4] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        dest_str[9] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        dest_str[14] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        dest_str[19] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        dest_str[24] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        dest_str[29] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        dest_str[34] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        dest_str[39] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            /* not an Ipv6 address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (strcmp(device, "lo") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            /* Not a loopback route */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (nRoutes == loRoutes_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                loRoutes = realloc (loRoutes, loRoutes_size *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                sizeof (struct loopback_route) * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                if (loRoutes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                loRoutes_size *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            memcpy (&loRoutes[nRoutes].addr,&dest_addr,sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            loRoutes[nRoutes].plen = dest_plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            nRoutes ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        /* now find the scope_id for "lo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   527
        char devname[20];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                  &if_idx, &plen, &scope, &dad_status, devname) == 13) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (strcmp(devname, "lo") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                     * Found - so just return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    lo_scope_id = if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
 * Following is used for binding to local addresses. Equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 * to code above, for bind().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
struct localinterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    char localaddr [16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
static struct localinterface *localifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
static int localifsSize = 0;    /* size of array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
static int nifs = 0;            /* number of entries used in array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
/* not thread safe: make sure called once from one thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
static void initLocalIfs () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    unsigned char staddr [16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    char ifname [32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    struct localinterface *lif=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    int index, x1, x2, x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    unsigned int u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,ua,ub,uc,ud,ue,uf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                "%d %x %x %x %s",&u0,&u1,&u2,&u3,&u4,&u5,&u6,&u7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                &u8,&u9,&ua,&ub,&uc,&ud,&ue,&uf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                &index, &x1, &x2, &x3, ifname) == 21) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        staddr[0] = (unsigned char)u0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        staddr[1] = (unsigned char)u1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        staddr[2] = (unsigned char)u2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        staddr[3] = (unsigned char)u3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        staddr[4] = (unsigned char)u4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        staddr[5] = (unsigned char)u5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        staddr[6] = (unsigned char)u6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        staddr[7] = (unsigned char)u7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        staddr[8] = (unsigned char)u8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        staddr[9] = (unsigned char)u9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        staddr[10] = (unsigned char)ua;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        staddr[11] = (unsigned char)ub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        staddr[12] = (unsigned char)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        staddr[13] = (unsigned char)ud;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        staddr[14] = (unsigned char)ue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        staddr[15] = (unsigned char)uf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        nifs ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (nifs > localifsSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            localifs = (struct localinterface *) realloc (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        localifs, sizeof (struct localinterface)* (localifsSize+5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (localifs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                nifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            lif = localifs + localifsSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            localifsSize += 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            lif ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        memcpy (lif->localaddr, staddr, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        lif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
/* return the scope_id (interface index) of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 * interface corresponding to the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 * returns 0 if no match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
static int getLocalScopeID (char *addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    struct localinterface *lif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    if (localifs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        initLocalIfs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    for (i=0, lif=localifs; i<nifs; i++, lif++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (memcmp (addr, lif->localaddr, 16) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return lif->index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
void initLocalAddrTable () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    initLocalIfs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
void initLocalAddrTable () {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
/* In the case of an IPv4 Inetaddress this method will return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
 * IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
 * Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                          int *len, jboolean v4MappedAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    family = (*env)->GetIntField(env, iaObj, ia_familyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /* needs work. 1. family 2. clean up him6 etc deallocate memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (family == IPv4) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (address == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                /* we would always prefer IPv6 wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                   caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                   caddr[11] = 0xff; */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        memset((char *)him6, 0, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        him6->sin6_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        him6->sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        *len = sizeof(struct sockaddr_in6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * On Linux if we are connecting to a link-local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * we need to specify the interface in the scope_id (2.4 kernel only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * If the scope was cached the we use the cached value. If not cached but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * specified in the Inet6Address we use that, but we first check if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * address needs to be routed via the loopback interface. In this case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * we override the specified value with that of the loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * If no cached value exists and no value was specified by user, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * we try to determine a value ffrom the routing table. In all these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * cases the used value is cached for further use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (IN6_IS_ADDR_LINKLOCAL(&(him6->sin6_addr))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            int cached_scope_id = 0, scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            int old_kernel = kernelIsV22();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (ia6_cachedscopeidID && !old_kernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                /* if cached value exists then use it. Otherwise, check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                 * if scope is set in the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if (!cached_scope_id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    if (ia6_scopeidID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        scope_id = (int)(*env)->GetIntField(env,iaObj,ia6_scopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    if (scope_id != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        /* check user-specified value for loopback case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                         * that needs to be overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        if (kernelIsV24() && needsLoopbackRoute (&him6->sin6_addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            cached_scope_id = lo_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                         * Otherwise consult the IPv6 routing tables to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                         * try determine the appropriate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        if (kernelIsV24()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                            cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                            cached_scope_id = getLocalScopeID( (char *)&(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            if (cached_scope_id == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
             * If we have a scope_id use the extended form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
             * of sockaddr_in6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (!old_kernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                struct sockaddr_in6 *him6 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                him6->sin6_scope_id = cached_scope_id != 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                            cached_scope_id    : scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                *len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        /* handle scope_id for solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (family != IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (ia6_scopeidID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                him6->sin6_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_scopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            struct sockaddr_in *him4 = (struct sockaddr_in*)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if (family == IPv6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
              JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
              return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            memset((char *) him4, 0, sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            him4->sin_port = htons((short) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            him4->sin_addr.s_addr = (uint32_t) htonl(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            him4->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            *len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
NET_SetTrafficClass(struct sockaddr *him, int trafficClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        him6->sin6_flowinfo = htonl((trafficClass & 0xff) << 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
NET_GetPortFromSockaddr(struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        return ntohs(((struct sockaddr_in6*)him)->sin6_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                return ntohs(((struct sockaddr_in*)him)->sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
NET_IsIPv4Mapped(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    for (i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (caddr[i] != 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return 1; /* true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
NET_IPv4MappedToIPv4(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        | (caddr[15] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (caddr1[i] != caddr2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
jboolean NET_addrtransAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    return (jboolean)(getaddrinfo_ptr != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
 * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
 * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
NET_MapSocketOption(jint cmd, int *level, int *optname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        jint cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        int level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        int optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    } const opts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        { java_net_SocketOptions_TCP_NODELAY,           IPPROTO_TCP,    TCP_NODELAY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        { java_net_SocketOptions_SO_OOBINLINE,          SOL_SOCKET,     SO_OOBINLINE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        { java_net_SocketOptions_SO_LINGER,             SOL_SOCKET,     SO_LINGER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        { java_net_SocketOptions_SO_SNDBUF,             SOL_SOCKET,     SO_SNDBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        { java_net_SocketOptions_SO_RCVBUF,             SOL_SOCKET,     SO_RCVBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        { java_net_SocketOptions_SO_KEEPALIVE,          SOL_SOCKET,     SO_KEEPALIVE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        { java_net_SocketOptions_SO_REUSEADDR,          SOL_SOCKET,     SO_REUSEADDR },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        { java_net_SocketOptions_SO_BROADCAST,          SOL_SOCKET,     SO_BROADCAST },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        { java_net_SocketOptions_IP_TOS,                IPPROTO_IP,     IP_TOS },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        { java_net_SocketOptions_IP_MULTICAST_IF,       IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        { java_net_SocketOptions_IP_MULTICAST_IF2,      IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        { java_net_SocketOptions_IP_MULTICAST_LOOP,     IPPROTO_IP,     IP_MULTICAST_LOOP },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Different multicast options if IPv6 is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            case java_net_SocketOptions_IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            case java_net_SocketOptions_IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                *optname = IPV6_MULTICAST_IF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                *optname = IPV6_MULTICAST_LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Map the Java level option to the native level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (cmd == opts[i].cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            *level = opts[i].level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            *optname = opts[i].optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    /* not found */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
 * Determine the default interface for an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
 * 1. Scans /proc/net/ipv6_route for a matching route
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
 *    (eg: fe80::/10 or a route for the specific address).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
 *    This will tell us the interface to use (eg: "eth0").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
 * 2. Lookup /proc/net/if_inet6 to map the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
 *    name to an interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
 * Returns :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
 *      -1 if error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
 *       0 if no matching interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
 *      >1 interface index to use for the link-local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
int getDefaultIPv6Interface(struct in6_addr *target_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    char srcp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    char hopp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    int dest_plen, src_plen, use, refcnt, metric;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    unsigned long flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    char dest_str[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    struct in6_addr dest_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    char device[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    jboolean match = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * Scan /proc/net/ipv6_route looking for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                     "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                     "%4s%4s%4s%4s%4s%4s%4s%4s "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                     "%08x %08x %08x %08lx %8s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                     dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                     &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                     &dest_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                     srcp[0], srcp[1], srcp[2], srcp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                     srcp[4], srcp[5], srcp[6], srcp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                     &src_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                     hopp[0], hopp[1], hopp[2], hopp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                     hopp[4], hopp[5], hopp[6], hopp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                     &metric, &use, &refcnt, &flags, device) == 31) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * Some routes should be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if ( (dest_plen < 0 || dest_plen > 128)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
             (src_plen != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
             (flags & (RTF_POLICY | RTF_FLOW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
             ((flags & RTF_REJECT) && dest_plen == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * Convert the destination address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        dest_str[4] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        dest_str[9] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        dest_str[14] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        dest_str[19] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        dest_str[24] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        dest_str[29] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        dest_str[34] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        dest_str[39] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            /* not an Ipv6 address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
             * The prefix len (dest_plen) indicates the number of bits we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
             * need to match on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
             * dest_plen / 8    => number of bytes to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
             * dest_plen % 8    => number of additional bits to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
             * eg: fe80::/10 => match 1 byte + 2 additional bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
             *                  the next byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            int byte_count = dest_plen >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            int extra_bits = dest_plen & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (byte_count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                if (memcmp(target_addr, &dest_addr, byte_count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    continue;  /* no match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            if (extra_bits > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                unsigned char mask = 0xff << (8 - extra_bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                if ((c1 & mask) != (c2 & mask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
             * We have a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * If there's a match then we lookup the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    if (match) {
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
  1021
        char devname[20];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                  &if_idx, &plen, &scope, &dad_status, devname) == 13) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                if (strcmp(devname, device) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                     * Found - so just return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    return if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
             * Couldn't open /proc/net/if_inet6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * If we get here it means we didn't there wasn't any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * route or we couldn't get the index of the interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
 * Wrapper for getsockopt system routine - does any necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
 * pre/post processing to deal with OS specific oddies :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
 * IP_TOS is a no-op with IPv6 sockets as it's setup when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
 * the connection is established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
 * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
 * to compensate for an incorrect value returned by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
NET_GetSockOpt(int fd, int level, int opt, void *result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
               int *len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    if ((level == IPPROTO_IP) && (opt == IP_TOS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
             * For IPv6 socket option implemented at Java-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
             * so return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            int *tc = (int *)result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            *tc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
911
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1088
#ifdef __solaris__
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1089
    rv = getsockopt(fd, level, opt, result, len);
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1090
#else
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1091
    {
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1092
        socklen_t socklen = *len;
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1093
        rv = getsockopt(fd, level, opt, result, &socklen);
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1094
        *len = socklen;
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1095
    }
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1096
#endif
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1097
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * stems from additional socket structures in the send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * and receive buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                  || (opt == SO_RCVBUF))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        int n = *((int *)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        n /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        *((int *)result) = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
 * Wrapper for setsockopt system routine - performs any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
 * necessary pre/post processing to deal with OS specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
 * issue :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
 * On Solaris need to limit the suggested value for SO_SNDBUF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
 * and SO_RCVBUF to the kernel configured limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
 * For IP_TOS socket option need to mask off bits as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
 * aren't automatically masked by the kernel and results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
 * an error. In addition IP_TOS is a noop with IPv6 as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
 * should be setup as connection time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
               int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * IPPROTO/IP_TOS :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * 1. IPv6 on Solaris: no-op and will be set in flowinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *    field when connecting TCP socket, or sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *    UDP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * 2. IPv6 on Linux: By default Linux ignores flowinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     *    field so enable IPV6_FLOWINFO_SEND so that flowinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *    will be examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * 3. IPv4: set socket option based on ToS and Precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *    fields (otherwise get invalid argument)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    if (level == IPPROTO_IP && opt == IP_TOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        int *iptos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
#if defined(AF_INET6) && defined(__solaris__)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
#if defined(AF_INET6) && defined(__linux__)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            int optval = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                              (void *)&optval, sizeof(optval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        iptos = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * ensure that value is <= max_buf as otherwise we get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * an invalid argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    if (level == SOL_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            int sotype, arglen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            int *bufsize, maxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (!init_max_buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                tcp_max_buf = getParam("/dev/tcp", "tcp_max_buf", 64*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                udp_max_buf = getParam("/dev/udp", "udp_max_buf", 64*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                init_max_buf = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            arglen = sizeof(sotype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                           &arglen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            if (*bufsize > maxbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                *bufsize = maxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * On Linux the receive buffer is used for both socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * structures and the the packet payload. The implication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * is that if SO_RCVBUF is too small then small packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * must be discard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    if (level == SOL_SOCKET && opt == SO_RCVBUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        int *bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        if (*bufsize < 1024) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            *bufsize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    return setsockopt(fd, level, opt, arg, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
 * Wrapper for bind system call - performs any necessary pre/post
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
 * processing to deal with OS specific issues :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
 * Linux allows a socket to bind to 127.0.0.255 which must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
 * caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
 * On Solaris 8/9 with IPv6 enabled we must use an exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
 * bind to guaranteed a unique port number across the IPv4 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
 * IPv6 port spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
NET_Bind(int fd, struct sockaddr *him, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    int level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    int exclbind = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * ## get bugId for this issue - goes back to 1.2.2 port ##
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * ## When IPv6 is enabled this will be an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * ## with family set to AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    if (him->sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        struct sockaddr_in *sa = (struct sockaddr_in *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        if ((ntohl(sa->sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            errno = EADDRNOTAVAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * Solaris 8/9 have seperate IPv4 and IPv6 port spaces so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * use an exclusive bind when SO_REUSEADDR is not used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * give the illusion of a unified port space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * This also avoid problems with IPv6 sockets connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * to IPv4 mapped addresses whereby the socket conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * results in a late bind that fails because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * corresponding IPv4 port is in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        int arg, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        len = sizeof(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                       &len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            if (arg == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                 * SO_REUSEADDR is disabled so enable TCP_EXCLBIND or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                 * UDP_EXCLBIND
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                len = sizeof(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                               &len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    if (arg == SOCK_STREAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                        level = IPPROTO_TCP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                        exclbind = TCP_EXCLBIND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                        level = IPPROTO_UDP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                        exclbind = UDP_EXCLBIND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                arg = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                setsockopt(fd, level, exclbind, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                           sizeof(arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    rv = bind(fd, him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        int en = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        /* Restore *_EXCLBIND if the bind fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (exclbind != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            int arg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            setsockopt(fd, level, exclbind, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                       sizeof(arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        errno = en;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
 * Wrapper for select/poll with timeout on a single file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
 * flags (defined in net_util_md.h can be any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
 * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
 * The function will return when either the socket is ready for one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
 * of the specified operation or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
 * It returns the time left from the timeout (possibly 0), or -1 if it expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    jlong prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    jint read_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        jlong newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
#ifndef USE_SELECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
          struct pollfd pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
          pfd.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
          pfd.events = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
          if (flags & NET_WAIT_READ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            pfd.events |= POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
          if (flags & NET_WAIT_WRITE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            pfd.events |= POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
          if (flags & NET_WAIT_CONNECT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            pfd.events |= POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
          errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
          read_rv = NET_Poll(&pfd, 1, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
          fd_set rd, wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
          struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
          t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
          t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
          FD_ZERO(&rd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
          FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
          FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
          if (flags & NET_WAIT_READ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            FD_SET(fd, &rd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
          if (flags & NET_WAIT_WRITE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
          if (flags & NET_WAIT_CONNECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
          errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
          read_rv = NET_Select(fd+1, &rd, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
          return read_rv > 0 ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        newTime = prevTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        if (read_rv > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
      } /* while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
}