0N/A/*
5107N/A * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <errno.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include <sys/socket.h>
0N/A#include <netinet/tcp.h> /* Defines TCP_NODELAY, needed for 2.6 */
0N/A#include <netinet/in.h>
0N/A#include <net/if.h>
0N/A#include <netdb.h>
0N/A#include <stdlib.h>
0N/A#include <dlfcn.h>
4632N/A
4632N/A#ifndef _ALLBSD_SOURCE
3123N/A#include <values.h>
4632N/A#else
4632N/A#include <limits.h>
4632N/A#include <sys/param.h>
4632N/A#include <sys/sysctl.h>
4632N/A#ifndef MAXINT
4632N/A#define MAXINT INT_MAX
4632N/A#endif
4632N/A#endif
0N/A
0N/A#ifdef __solaris__
0N/A#include <sys/sockio.h>
0N/A#include <stropts.h>
0N/A#include <inet/nd.h>
0N/A#endif
0N/A
0N/A#ifdef __linux__
0N/A#include <arpa/inet.h>
0N/A#include <net/route.h>
0N/A#include <sys/utsname.h>
0N/A
0N/A#ifndef IPV6_FLOWINFO_SEND
0N/A#define IPV6_FLOWINFO_SEND 33
0N/A#endif
0N/A
0N/A#endif
0N/A
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "net_util.h"
0N/A
0N/A#include "java_net_SocketOptions.h"
0N/A
0N/A/* needed from libsocket on Solaris 8 */
0N/A
0N/Agetaddrinfo_f getaddrinfo_ptr = NULL;
0N/Afreeaddrinfo_f freeaddrinfo_ptr = NULL;
2801N/Agai_strerror_f gai_strerror_ptr = NULL;
0N/Agetnameinfo_f getnameinfo_ptr = NULL;
0N/A
0N/A/*
5107N/A * EXCLBIND socket options only on Solaris
0N/A */
0N/A#if defined(__solaris__) && !defined(TCP_EXCLBIND)
0N/A#define TCP_EXCLBIND 0x21
0N/A#endif
0N/A#if defined(__solaris__) && !defined(UDP_EXCLBIND)
0N/A#define UDP_EXCLBIND 0x0101
0N/A#endif
0N/A
4638N/Avoid setDefaultScopeID(JNIEnv *env, struct sockaddr *him)
4638N/A{
4638N/A#ifdef MACOSX
4649N/A static jclass ni_class = NULL;
4649N/A static jfieldID ni_defaultIndexID;
4649N/A if (ni_class == NULL) {
4649N/A jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
4649N/A CHECK_NULL(c);
4649N/A c = (*env)->NewGlobalRef(env, c);
4649N/A CHECK_NULL(c);
4649N/A ni_defaultIndexID = (*env)->GetStaticFieldID(
4649N/A env, c, "defaultIndex", "I");
4649N/A ni_class = c;
4649N/A }
4638N/A int defaultIndex;
4638N/A struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)him;
4638N/A if (sin6->sin6_family == AF_INET6 && (sin6->sin6_scope_id == 0)) {
4638N/A defaultIndex = (*env)->GetStaticIntField(env, ni_class,
4638N/A ni_defaultIndexID);
4638N/A sin6->sin6_scope_id = defaultIndex;
4638N/A }
4638N/A#endif
4638N/A}
4638N/A
4805N/Aint getDefaultScopeID(JNIEnv *env) {
4805N/A static jclass ni_class = NULL;
4805N/A static jfieldID ni_defaultIndexID;
4805N/A if (ni_class == NULL) {
4805N/A jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
4805N/A CHECK_NULL(c);
4805N/A c = (*env)->NewGlobalRef(env, c);
4805N/A CHECK_NULL(c);
4805N/A ni_defaultIndexID = (*env)->GetStaticFieldID(
4805N/A env, c, "defaultIndex", "I");
4805N/A ni_class = c;
4805N/A }
4805N/A int defaultIndex = 0;
4805N/A defaultIndex = (*env)->GetStaticIntField(env, ni_class,
4805N/A ni_defaultIndexID);
4805N/A return defaultIndex;
4805N/A}
4805N/A
0N/A#ifdef __solaris__
3123N/Astatic int init_tcp_max_buf, init_udp_max_buf;
0N/Astatic int tcp_max_buf;
0N/Astatic int udp_max_buf;
5107N/Astatic int useExclBind = 0;
0N/A
0N/A/*
0N/A * Get the specified parameter from the specified driver. The value
0N/A * of the parameter is assumed to be an 'int'. If the parameter
3123N/A * cannot be obtained return -1
0N/A */
0N/Astatic int
3123N/AgetParam(char *driver, char *param)
0N/A{
0N/A struct strioctl stri;
0N/A char buf [64];
0N/A int s;
0N/A int value;
0N/A
0N/A s = open (driver, O_RDWR);
0N/A if (s < 0) {
3123N/A return -1;
0N/A }
0N/A strncpy (buf, param, sizeof(buf));
0N/A stri.ic_cmd = ND_GET;
0N/A stri.ic_timout = 0;
0N/A stri.ic_dp = buf;
0N/A stri.ic_len = sizeof(buf);
0N/A if (ioctl (s, I_STR, &stri) < 0) {
3123N/A value = -1;
0N/A } else {
0N/A value = atoi(buf);
0N/A }
0N/A close (s);
0N/A return value;
0N/A}
3123N/A
3123N/A/*
3123N/A * Iterative way to find the max value that SO_SNDBUF or SO_RCVBUF
3123N/A * for Solaris versions that do not support the ioctl() in getParam().
3123N/A * Ugly, but only called once (for each sotype).
3123N/A *
3123N/A * As an optimisation, we make a guess using the default values for Solaris
3123N/A * assuming they haven't been modified with ndd.
3123N/A */
3123N/A
3123N/A#define MAX_TCP_GUESS 1024 * 1024
3123N/A#define MAX_UDP_GUESS 2 * 1024 * 1024
3123N/A
3123N/A#define FAIL_IF_NOT_ENOBUFS if (errno != ENOBUFS) return -1
3123N/A
3123N/Astatic int findMaxBuf(int fd, int opt, int sotype) {
3123N/A int a = 0;
3123N/A int b = MAXINT;
3123N/A int initial_guess;
3123N/A int limit = -1;
3123N/A
3123N/A if (sotype == SOCK_DGRAM) {
3123N/A initial_guess = MAX_UDP_GUESS;
3123N/A } else {
3123N/A initial_guess = MAX_TCP_GUESS;
3123N/A }
3123N/A
3123N/A if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess, sizeof(int)) == 0) {
3123N/A initial_guess++;
3123N/A if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess,sizeof(int)) < 0) {
3123N/A FAIL_IF_NOT_ENOBUFS;
3123N/A return initial_guess - 1;
3123N/A }
3123N/A a = initial_guess;
3123N/A } else {
3123N/A FAIL_IF_NOT_ENOBUFS;
3123N/A b = initial_guess - 1;
3123N/A }
3123N/A do {
3123N/A int mid = a + (b-a)/2;
3123N/A if (setsockopt(fd, SOL_SOCKET, opt, &mid, sizeof(int)) == 0) {
3123N/A limit = mid;
3123N/A a = mid + 1;
3123N/A } else {
3123N/A FAIL_IF_NOT_ENOBUFS;
3123N/A b = mid - 1;
3123N/A }
3123N/A } while (b >= a);
3123N/A
3123N/A return limit;
3123N/A}
0N/A#endif
0N/A
0N/A#ifdef __linux__
0N/Astatic int kernelV22 = 0;
0N/Astatic int vinit = 0;
0N/A
0N/Aint kernelIsV22 () {
0N/A if (!vinit) {
0N/A struct utsname sysinfo;
0N/A if (uname(&sysinfo) == 0) {
0N/A sysinfo.release[3] = '\0';
0N/A if (strcmp(sysinfo.release, "2.2") == 0) {
0N/A kernelV22 = JNI_TRUE;
0N/A }
0N/A }
0N/A vinit = 1;
0N/A }
0N/A return kernelV22;
0N/A}
0N/A
0N/Astatic int kernelV24 = 0;
0N/Astatic int vinit24 = 0;
0N/A
0N/Aint kernelIsV24 () {
0N/A if (!vinit24) {
0N/A struct utsname sysinfo;
0N/A if (uname(&sysinfo) == 0) {
0N/A sysinfo.release[3] = '\0';
0N/A if (strcmp(sysinfo.release, "2.4") == 0) {
0N/A kernelV24 = JNI_TRUE;
0N/A }
0N/A }
0N/A vinit24 = 1;
0N/A }
0N/A return kernelV24;
0N/A}
0N/A
0N/Aint getScopeID (struct sockaddr *him) {
0N/A struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
0N/A if (kernelIsV22()) {
0N/A return 0;
0N/A }
0N/A return hext->sin6_scope_id;
0N/A}
0N/A
0N/Aint cmpScopeID (unsigned int scope, struct sockaddr *him) {
0N/A struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
0N/A if (kernelIsV22()) {
0N/A return 1; /* scope is ignored for comparison in 2.2 kernel */
0N/A }
0N/A return hext->sin6_scope_id == scope;
0N/A}
0N/A
0N/A#else
0N/A
0N/Aint getScopeID (struct sockaddr *him) {
0N/A struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
0N/A return him6->sin6_scope_id;
0N/A}
0N/A
0N/Aint cmpScopeID (unsigned int scope, struct sockaddr *him) {
0N/A struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
0N/A return him6->sin6_scope_id == scope;
0N/A}
0N/A
0N/A#endif
0N/A
0N/A
0N/Avoid
0N/ANET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
0N/A const char *defaultDetail) {
0N/A char errmsg[255];
0N/A sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail);
0N/A JNU_ThrowByNameWithLastError(env, name, errmsg);
0N/A}
0N/A
0N/Avoid
0N/ANET_ThrowCurrent(JNIEnv *env, char *msg) {
0N/A NET_ThrowNew(env, errno, msg);
0N/A}
0N/A
0N/Avoid
0N/ANET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) {
0N/A char fullMsg[512];
0N/A if (!msg) {
0N/A msg = "no further information";
0N/A }
0N/A switch(errorNumber) {
0N/A case EBADF:
0N/A jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg);
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
0N/A break;
0N/A case EINTR:
0N/A JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg);
0N/A break;
0N/A default:
0N/A errno = errorNumber;
0N/A JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg);
0N/A break;
0N/A }
0N/A}
0N/A
0N/A
0N/AjfieldID
0N/ANET_GetFileDescriptorID(JNIEnv *env)
0N/A{
0N/A jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
0N/A CHECK_NULL_RETURN(cls, NULL);
0N/A return (*env)->GetFieldID(env, cls, "fd", "I");
0N/A}
0N/A
4632N/A#if defined(DONT_ENABLE_IPV6)
4632N/Ajint IPv6_supported()
4632N/A{
4632N/A return JNI_FALSE;
4632N/A}
4632N/A
4632N/A#else /* !DONT_ENABLE_IPV6 */
4632N/A
0N/Ajint IPv6_supported()
0N/A{
0N/A#ifndef AF_INET6
0N/A return JNI_FALSE;
0N/A#endif
0N/A
0N/A#ifdef AF_INET6
0N/A int fd;
0N/A void *ipv6_fn;
0N/A SOCKADDR sa;
453N/A socklen_t sa_len = sizeof(sa);
0N/A
0N/A fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0) ;
0N/A if (fd < 0) {
0N/A /*
0N/A * TODO: We really cant tell since it may be an unrelated error
0N/A * for now we will assume that AF_INET6 is not available
0N/A */
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /*
0N/A * If fd 0 is a socket it means we've been launched from inetd or
0N/A * xinetd. If it's a socket then check the family - if it's an
0N/A * IPv4 socket then we need to disable IPv6.
0N/A */
0N/A if (getsockname(0, (struct sockaddr *)&sa, &sa_len) == 0) {
0N/A struct sockaddr *saP = (struct sockaddr *)&sa;
0N/A if (saP->sa_family != AF_INET6) {
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Linux - check if any interface has an IPv6 address.
0N/A * Don't need to parse the line - we just need an indication.
0N/A */
0N/A#ifdef __linux__
0N/A {
0N/A FILE *fP = fopen("/proc/net/if_inet6", "r");
0N/A char buf[255];
0N/A char *bufP;
0N/A
0N/A if (fP == NULL) {
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A bufP = fgets(buf, sizeof(buf), fP);
0N/A fclose(fP);
0N/A if (bufP == NULL) {
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A /**
0N/A * On Solaris 8 it's possible to create INET6 sockets even
0N/A * though IPv6 is not enabled on all interfaces. Thus we
0N/A * query the number of IPv6 addresses to verify that IPv6
0N/A * has been configured on at least one interface.
0N/A *
0N/A * On Linux it doesn't matter - if IPv6 is built-in the
0N/A * kernel then IPv6 addresses will be bound automatically
0N/A * to all interfaces.
0N/A */
0N/A#ifdef __solaris__
0N/A
0N/A#ifdef SIOCGLIFNUM
0N/A {
0N/A struct lifnum numifs;
0N/A
0N/A numifs.lifn_family = AF_INET6;
0N/A numifs.lifn_flags = 0;
0N/A if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) {
0N/A /**
0N/A * SIOCGLIFNUM failed - assume IPv6 not configured
0N/A */
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A /**
0N/A * If no IPv6 addresses then return false. If count > 0
0N/A * it's possible that all IPv6 addresses are "down" but
0N/A * that's okay as they may be brought "up" while the
0N/A * VM is running.
0N/A */
0N/A if (numifs.lifn_count == 0) {
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A#else
0N/A /* SIOCGLIFNUM not defined in build environment ??? */
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A#endif
0N/A
0N/A#endif /* __solaris */
0N/A
0N/A /*
0N/A * OK we may have the stack available in the kernel,
0N/A * we should also check if the APIs are available.
0N/A */
0N/A ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
0N/A if (ipv6_fn == NULL ) {
0N/A close(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /*
0N/A * We've got the library, let's get the pointers to some
0N/A * IPV6 specific functions. We have to do that because, at least
0N/A * on Solaris we may build on a system without IPV6 networking
0N/A * libraries, therefore we can't have a hard link to these
0N/A * functions.
0N/A */
0N/A getaddrinfo_ptr = (getaddrinfo_f)
0N/A JVM_FindLibraryEntry(RTLD_DEFAULT, "getaddrinfo");
0N/A
0N/A freeaddrinfo_ptr = (freeaddrinfo_f)
0N/A JVM_FindLibraryEntry(RTLD_DEFAULT, "freeaddrinfo");
0N/A
2801N/A gai_strerror_ptr = (gai_strerror_f)
2801N/A JVM_FindLibraryEntry(RTLD_DEFAULT, "gai_strerror");
2801N/A
0N/A getnameinfo_ptr = (getnameinfo_f)
0N/A JVM_FindLibraryEntry(RTLD_DEFAULT, "getnameinfo");
0N/A
0N/A if (freeaddrinfo_ptr == NULL || getnameinfo_ptr == NULL) {
2801N/A /* We need all 3 of them */
0N/A getaddrinfo_ptr = NULL;
0N/A }
0N/A
0N/A close(fd);
0N/A return JNI_TRUE;
2112N/A#endif /* AF_INET6 */
0N/A}
4632N/A#endif /* DONT_ENABLE_IPV6 */
0N/A
2801N/Avoid ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
2801N/A const char* hostname,
2801N/A int gai_error)
2801N/A{
2827N/A int size;
2827N/A char *buf;
2801N/A const char *format = "%s: %s";
2801N/A const char *error_string =
2801N/A (gai_strerror_ptr == NULL) ? NULL : (*gai_strerror_ptr)(gai_error);
2801N/A if (error_string == NULL)
2801N/A error_string = "unknown error";
2801N/A
2827N/A size = strlen(format) + strlen(hostname) + strlen(error_string) + 2;
2827N/A buf = (char *) malloc(size);
2801N/A if (buf) {
2827N/A jstring s;
2801N/A sprintf(buf, format, hostname, error_string);
2827N/A s = JNU_NewStringPlatform(env, buf);
2801N/A if (s != NULL) {
2801N/A jobject x = JNU_NewObjectByName(env,
2801N/A "java/net/UnknownHostException",
2801N/A "(Ljava/lang/String;)V", s);
2801N/A if (x != NULL)
2801N/A (*env)->Throw(env, x);
2801N/A }
2801N/A free(buf);
2801N/A }
2801N/A}
2801N/A
0N/Avoid
0N/ANET_AllocSockaddr(struct sockaddr **him, int *len) {
0N/A#ifdef AF_INET6
0N/A if (ipv6_available()) {
0N/A struct sockaddr_in6 *him6 = (struct sockaddr_in6*)malloc(sizeof(struct sockaddr_in6));
0N/A *him = (struct sockaddr*)him6;
0N/A *len = sizeof(struct sockaddr_in6);
0N/A } else
0N/A#endif /* AF_INET6 */
0N/A {
0N/A struct sockaddr_in *him4 = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
0N/A *him = (struct sockaddr*)him4;
0N/A *len = sizeof(struct sockaddr_in);
0N/A }
0N/A}
0N/A
0N/A#if defined(__linux__) && defined(AF_INET6)
0N/A
0N/A
0N/A/* following code creates a list of addresses from the kernel
0N/A * routing table that are routed via the loopback address.
0N/A * We check all destination addresses against this table
0N/A * and override the scope_id field to use the relevant value for "lo"
0N/A * in order to work-around the Linux bug that prevents packets destined
0N/A * for certain local addresses from being sent via a physical interface.
0N/A */
0N/A
0N/Astruct loopback_route {
0N/A struct in6_addr addr; /* destination address */
0N/A int plen; /* prefix length */
0N/A};
0N/A
0N/Astatic struct loopback_route *loRoutes = 0;
0N/Astatic int nRoutes = 0; /* number of routes */
0N/Astatic int loRoutes_size = 16; /* initial size */
0N/Astatic int lo_scope_id = 0;
0N/A
0N/Astatic void initLoopbackRoutes();
0N/A
0N/Avoid printAddr (struct in6_addr *addr) {
0N/A int i;
0N/A for (i=0; i<16; i++) {
0N/A printf ("%02x", addr->s6_addr[i]);
0N/A }
0N/A printf ("\n");
0N/A}
0N/A
0N/Astatic jboolean needsLoopbackRoute (struct in6_addr* dest_addr) {
0N/A int byte_count;
0N/A int extra_bits, i;
0N/A struct loopback_route *ptr;
0N/A
0N/A if (loRoutes == 0) {
0N/A initLoopbackRoutes();
0N/A }
0N/A
0N/A for (ptr = loRoutes, i=0; i<nRoutes; i++, ptr++) {
0N/A struct in6_addr *target_addr=&ptr->addr;
0N/A int dest_plen = ptr->plen;
0N/A byte_count = dest_plen >> 3;
0N/A extra_bits = dest_plen & 0x3;
0N/A
0N/A if (byte_count > 0) {
0N/A if (memcmp(target_addr, dest_addr, byte_count)) {
0N/A continue; /* no match */
0N/A }
0N/A }
0N/A
0N/A if (extra_bits > 0) {
0N/A unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
0N/A unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
0N/A unsigned char mask = 0xff << (8 - extra_bits);
0N/A if ((c1 & mask) != (c2 & mask)) {
0N/A continue;
0N/A }
0N/A }
0N/A return JNI_TRUE;
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/A
0N/Astatic void initLoopbackRoutes() {
0N/A FILE *f;
0N/A char srcp[8][5];
0N/A char hopp[8][5];
0N/A int dest_plen, src_plen, use, refcnt, metric;
0N/A unsigned long flags;
0N/A char dest_str[40];
0N/A struct in6_addr dest_addr;
0N/A char device[16];
0N/A
0N/A if (loRoutes != 0) {
0N/A free (loRoutes);
0N/A }
0N/A loRoutes = calloc (loRoutes_size, sizeof(struct loopback_route));
0N/A if (loRoutes == 0) {
0N/A return;
0N/A }
0N/A /*
0N/A * Scan /proc/net/ipv6_route looking for a matching
0N/A * route.
0N/A */
0N/A if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
0N/A return ;
0N/A }
0N/A while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
0N/A "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
0N/A "%4s%4s%4s%4s%4s%4s%4s%4s "
0N/A "%08x %08x %08x %08lx %8s",
0N/A dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
0N/A &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
0N/A &dest_plen,
0N/A srcp[0], srcp[1], srcp[2], srcp[3],
0N/A srcp[4], srcp[5], srcp[6], srcp[7],
0N/A &src_plen,
0N/A hopp[0], hopp[1], hopp[2], hopp[3],
0N/A hopp[4], hopp[5], hopp[6], hopp[7],
0N/A &metric, &use, &refcnt, &flags, device) == 31) {
0N/A
0N/A /*
0N/A * Some routes should be ignored
0N/A */
0N/A if ( (dest_plen < 0 || dest_plen > 128) ||
0N/A (src_plen != 0) ||
0N/A (flags & (RTF_POLICY | RTF_FLOW)) ||
0N/A ((flags & RTF_REJECT) && dest_plen == 0) ) {
0N/A continue;
0N/A }
0N/A
0N/A /*
0N/A * Convert the destination address
0N/A */
0N/A dest_str[4] = ':';
0N/A dest_str[9] = ':';
0N/A dest_str[14] = ':';
0N/A dest_str[19] = ':';
0N/A dest_str[24] = ':';
0N/A dest_str[29] = ':';
0N/A dest_str[34] = ':';
0N/A dest_str[39] = '\0';
0N/A
0N/A if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
0N/A /* not an Ipv6 address */
0N/A continue;
0N/A }
0N/A if (strcmp(device, "lo") != 0) {
0N/A /* Not a loopback route */
0N/A continue;
0N/A } else {
0N/A if (nRoutes == loRoutes_size) {
0N/A loRoutes = realloc (loRoutes, loRoutes_size *
0N/A sizeof (struct loopback_route) * 2);
0N/A if (loRoutes == 0) {
0N/A return ;
0N/A }
0N/A loRoutes_size *= 2;
0N/A }
0N/A memcpy (&loRoutes[nRoutes].addr,&dest_addr,sizeof(struct in6_addr));
0N/A loRoutes[nRoutes].plen = dest_plen;
0N/A nRoutes ++;
0N/A }
0N/A }
0N/A
0N/A fclose (f);
0N/A {
0N/A /* now find the scope_id for "lo" */
0N/A
3657N/A char devname[21];
0N/A char addr6p[8][5];
0N/A int plen, scope, dad_status, if_idx;
0N/A
0N/A if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
0N/A while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
0N/A addr6p[0], addr6p[1], addr6p[2], addr6p[3],
0N/A addr6p[4], addr6p[5], addr6p[6], addr6p[7],
0N/A &if_idx, &plen, &scope, &dad_status, devname) == 13) {
0N/A
0N/A if (strcmp(devname, "lo") == 0) {
0N/A /*
0N/A * Found - so just return the index
0N/A */
0N/A fclose(f);
0N/A lo_scope_id = if_idx;
0N/A return;
0N/A }
0N/A }
0N/A fclose(f);
0N/A }
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Following is used for binding to local addresses. Equivalent
0N/A * to code above, for bind().
0N/A */
0N/A
0N/Astruct localinterface {
0N/A int index;
0N/A char localaddr [16];
0N/A};
0N/A
0N/Astatic struct localinterface *localifs = 0;
0N/Astatic int localifsSize = 0; /* size of array */
0N/Astatic int nifs = 0; /* number of entries used in array */
0N/A
0N/A/* not thread safe: make sure called once from one thread */
0N/A
0N/Astatic void initLocalIfs () {
0N/A FILE *f;
0N/A unsigned char staddr [16];
3657N/A char ifname [33];
0N/A struct localinterface *lif=0;
0N/A int index, x1, x2, x3;
0N/A unsigned int u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,ua,ub,uc,ud,ue,uf;
0N/A
0N/A if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
0N/A return ;
0N/A }
0N/A while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x "
3657N/A "%d %x %x %x %32s",&u0,&u1,&u2,&u3,&u4,&u5,&u6,&u7,
0N/A &u8,&u9,&ua,&ub,&uc,&ud,&ue,&uf,
0N/A &index, &x1, &x2, &x3, ifname) == 21) {
0N/A staddr[0] = (unsigned char)u0;
0N/A staddr[1] = (unsigned char)u1;
0N/A staddr[2] = (unsigned char)u2;
0N/A staddr[3] = (unsigned char)u3;
0N/A staddr[4] = (unsigned char)u4;
0N/A staddr[5] = (unsigned char)u5;
0N/A staddr[6] = (unsigned char)u6;
0N/A staddr[7] = (unsigned char)u7;
0N/A staddr[8] = (unsigned char)u8;
0N/A staddr[9] = (unsigned char)u9;
0N/A staddr[10] = (unsigned char)ua;
0N/A staddr[11] = (unsigned char)ub;
0N/A staddr[12] = (unsigned char)uc;
0N/A staddr[13] = (unsigned char)ud;
0N/A staddr[14] = (unsigned char)ue;
0N/A staddr[15] = (unsigned char)uf;
0N/A nifs ++;
0N/A if (nifs > localifsSize) {
0N/A localifs = (struct localinterface *) realloc (
0N/A localifs, sizeof (struct localinterface)* (localifsSize+5));
0N/A if (localifs == 0) {
0N/A nifs = 0;
0N/A fclose (f);
0N/A return;
0N/A }
0N/A lif = localifs + localifsSize;
0N/A localifsSize += 5;
0N/A } else {
0N/A lif ++;
0N/A }
0N/A memcpy (lif->localaddr, staddr, 16);
0N/A lif->index = index;
0N/A }
0N/A fclose (f);
0N/A}
0N/A
0N/A/* return the scope_id (interface index) of the
0N/A * interface corresponding to the given address
0N/A * returns 0 if no match found
0N/A */
0N/A
0N/Astatic int getLocalScopeID (char *addr) {
0N/A struct localinterface *lif;
0N/A int i;
0N/A if (localifs == 0) {
0N/A initLocalIfs();
0N/A }
0N/A for (i=0, lif=localifs; i<nifs; i++, lif++) {
0N/A if (memcmp (addr, lif->localaddr, 16) == 0) {
0N/A return lif->index;
0N/A }
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/Avoid initLocalAddrTable () {
0N/A initLoopbackRoutes();
0N/A initLocalIfs();
0N/A}
0N/A
0N/A#else
0N/A
0N/Avoid initLocalAddrTable () {}
0N/A
0N/A#endif
0N/A
5107N/Avoid parseExclusiveBindProperty(JNIEnv *env) {
5107N/A#ifdef __solaris__
5107N/A jstring s, flagSet;
5107N/A jclass iCls;
5107N/A jmethodID mid;
5107N/A
5107N/A s = (*env)->NewStringUTF(env, "sun.net.useExclusiveBind");
5107N/A CHECK_NULL(s);
5107N/A iCls = (*env)->FindClass(env, "java/lang/System");
5107N/A CHECK_NULL(iCls);
5107N/A mid = (*env)->GetStaticMethodID(env, iCls, "getProperty",
5107N/A "(Ljava/lang/String;)Ljava/lang/String;");
5107N/A CHECK_NULL(mid);
5107N/A flagSet = (*env)->CallStaticObjectMethod(env, iCls, mid, s);
5107N/A if (flagSet != NULL) {
5107N/A useExclBind = 1;
5107N/A }
5107N/A#endif
5107N/A}
0N/A/* In the case of an IPv4 Inetaddress this method will return an
0N/A * IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE.
0N/A * Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress.
0N/A*/
0N/AJNIEXPORT int JNICALL
0N/ANET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
0N/A int *len, jboolean v4MappedAddress) {
0N/A jint family;
5888N/A family = getInetAddress_family(env, iaObj);
0N/A#ifdef AF_INET6
0N/A /* needs work. 1. family 2. clean up him6 etc deallocate memory */
0N/A if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) {
0N/A struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
0N/A jbyteArray ipaddress;
0N/A jbyte caddr[16];
0N/A jint address;
0N/A
0N/A
0N/A if (family == IPv4) { /* will convert to IPv4-mapped address */
0N/A memset((char *) caddr, 0, 16);
5888N/A address = getInetAddress_addr(env, iaObj);
0N/A if (address == INADDR_ANY) {
0N/A /* we would always prefer IPv6 wildcard address
0N/A caddr[10] = 0xff;
0N/A caddr[11] = 0xff; */
0N/A } else {
0N/A caddr[10] = 0xff;
0N/A caddr[11] = 0xff;
0N/A caddr[12] = ((address >> 24) & 0xff);
0N/A caddr[13] = ((address >> 16) & 0xff);
0N/A caddr[14] = ((address >> 8) & 0xff);
0N/A caddr[15] = (address & 0xff);
0N/A }
0N/A } else {
0N/A ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
0N/A (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
0N/A }
0N/A memset((char *)him6, 0, sizeof(struct sockaddr_in6));
0N/A him6->sin6_port = htons(port);
0N/A memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
0N/A him6->sin6_family = AF_INET6;
0N/A *len = sizeof(struct sockaddr_in6) ;
0N/A
4632N/A#if defined(_ALLBSD_SOURCE) && defined(_AF_INET6)
4632N/A// XXXBSD: should we do something with scope id here ? see below linux comment
4632N/A/* MMM: Come back to this! */
4632N/A#endif
4632N/A
0N/A /*
0N/A * On Linux if we are connecting to a link-local address
0N/A * we need to specify the interface in the scope_id (2.4 kernel only)
0N/A *
0N/A * If the scope was cached the we use the cached value. If not cached but
0N/A * specified in the Inet6Address we use that, but we first check if the
0N/A * address needs to be routed via the loopback interface. In this case,
0N/A * we override the specified value with that of the loopback interface.
0N/A * If no cached value exists and no value was specified by user, then
0N/A * we try to determine a value ffrom the routing table. In all these
0N/A * cases the used value is cached for further use.
0N/A */
0N/A#ifdef __linux__
0N/A if (IN6_IS_ADDR_LINKLOCAL(&(him6->sin6_addr))) {
0N/A int cached_scope_id = 0, scope_id = 0;
0N/A int old_kernel = kernelIsV22();
0N/A
0N/A if (ia6_cachedscopeidID && !old_kernel) {
0N/A cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
0N/A /* if cached value exists then use it. Otherwise, check
0N/A * if scope is set in the address.
0N/A */
0N/A if (!cached_scope_id) {
0N/A if (ia6_scopeidID) {
0N/A scope_id = (int)(*env)->GetIntField(env,iaObj,ia6_scopeidID);
0N/A }
0N/A if (scope_id != 0) {
0N/A /* check user-specified value for loopback case
0N/A * that needs to be overridden
0N/A */
0N/A if (kernelIsV24() && needsLoopbackRoute (&him6->sin6_addr)) {
0N/A cached_scope_id = lo_scope_id;
0N/A (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
0N/A }
0N/A } else {
0N/A /*
0N/A * Otherwise consult the IPv6 routing tables to
0N/A * try determine the appropriate interface.
0N/A */
0N/A if (kernelIsV24()) {
0N/A cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
0N/A } else {
0N/A cached_scope_id = getLocalScopeID( (char *)&(him6->sin6_addr) );
0N/A if (cached_scope_id == 0) {
0N/A cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
0N/A }
0N/A }
0N/A (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * If we have a scope_id use the extended form
0N/A * of sockaddr_in6.
0N/A */
0N/A
0N/A if (!old_kernel) {
0N/A struct sockaddr_in6 *him6 =
0N/A (struct sockaddr_in6 *)him;
0N/A him6->sin6_scope_id = cached_scope_id != 0 ?
0N/A cached_scope_id : scope_id;
0N/A *len = sizeof(struct sockaddr_in6);
0N/A }
0N/A }
0N/A#else
0N/A /* handle scope_id for solaris */
0N/A
0N/A if (family != IPv4) {
0N/A if (ia6_scopeidID) {
0N/A him6->sin6_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_scopeidID);
0N/A }
0N/A }
0N/A#endif
0N/A } else
0N/A#endif /* AF_INET6 */
0N/A {
0N/A struct sockaddr_in *him4 = (struct sockaddr_in*)him;
0N/A jint address;
0N/A if (family == IPv6) {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
0N/A return -1;
0N/A }
0N/A memset((char *) him4, 0, sizeof(struct sockaddr_in));
5888N/A address = getInetAddress_addr(env, iaObj);
0N/A him4->sin_port = htons((short) port);
0N/A him4->sin_addr.s_addr = (uint32_t) htonl(address);
0N/A him4->sin_family = AF_INET;
0N/A *len = sizeof(struct sockaddr_in);
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/Avoid
0N/ANET_SetTrafficClass(struct sockaddr *him, int trafficClass) {
0N/A#ifdef AF_INET6
0N/A if (him->sa_family == AF_INET6) {
0N/A struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
0N/A him6->sin6_flowinfo = htonl((trafficClass & 0xff) << 20);
0N/A }
0N/A#endif /* AF_INET6 */
0N/A}
0N/A
524N/AJNIEXPORT jint JNICALL
0N/ANET_GetPortFromSockaddr(struct sockaddr *him) {
0N/A#ifdef AF_INET6
0N/A if (him->sa_family == AF_INET6) {
0N/A return ntohs(((struct sockaddr_in6*)him)->sin6_port);
0N/A
0N/A } else
0N/A#endif /* AF_INET6 */
0N/A {
0N/A return ntohs(((struct sockaddr_in*)him)->sin_port);
0N/A }
0N/A}
0N/A
0N/Aint
0N/ANET_IsIPv4Mapped(jbyte* caddr) {
0N/A int i;
0N/A for (i = 0; i < 10; i++) {
0N/A if (caddr[i] != 0x00) {
0N/A return 0; /* false */
0N/A }
0N/A }
0N/A
0N/A if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
0N/A return 1; /* true */
0N/A }
0N/A return 0; /* false */
0N/A}
0N/A
0N/Aint
0N/ANET_IPv4MappedToIPv4(jbyte* caddr) {
0N/A return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
0N/A | (caddr[15] & 0xff);
0N/A}
0N/A
0N/Aint
0N/ANET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
0N/A int i;
0N/A for (i = 0; i < 16; i++) {
0N/A if (caddr1[i] != caddr2[i]) {
0N/A return 0; /* false */
0N/A }
0N/A }
0N/A return 1;
0N/A}
0N/A
0N/Ajboolean NET_addrtransAvailable() {
0N/A return (jboolean)(getaddrinfo_ptr != NULL);
0N/A}
0N/A
5518N/Aint NET_IsZeroAddr(jbyte* caddr) {
5518N/A int i;
5518N/A for (i = 0; i < 16; i++) {
5518N/A if (caddr[i] != 0) {
5518N/A return 0;
5518N/A }
5518N/A }
5518N/A return 1;
5518N/A}
5518N/A
0N/A/*
0N/A * Map the Java level socket option to the platform specific
0N/A * level and option name.
0N/A */
0N/Aint
0N/ANET_MapSocketOption(jint cmd, int *level, int *optname) {
0N/A static struct {
0N/A jint cmd;
0N/A int level;
0N/A int optname;
0N/A } const opts[] = {
0N/A { java_net_SocketOptions_TCP_NODELAY, IPPROTO_TCP, TCP_NODELAY },
0N/A { java_net_SocketOptions_SO_OOBINLINE, SOL_SOCKET, SO_OOBINLINE },
0N/A { java_net_SocketOptions_SO_LINGER, SOL_SOCKET, SO_LINGER },
0N/A { java_net_SocketOptions_SO_SNDBUF, SOL_SOCKET, SO_SNDBUF },
0N/A { java_net_SocketOptions_SO_RCVBUF, SOL_SOCKET, SO_RCVBUF },
0N/A { java_net_SocketOptions_SO_KEEPALIVE, SOL_SOCKET, SO_KEEPALIVE },
0N/A { java_net_SocketOptions_SO_REUSEADDR, SOL_SOCKET, SO_REUSEADDR },
0N/A { java_net_SocketOptions_SO_BROADCAST, SOL_SOCKET, SO_BROADCAST },
0N/A { java_net_SocketOptions_IP_TOS, IPPROTO_IP, IP_TOS },
0N/A { java_net_SocketOptions_IP_MULTICAST_IF, IPPROTO_IP, IP_MULTICAST_IF },
0N/A { java_net_SocketOptions_IP_MULTICAST_IF2, IPPROTO_IP, IP_MULTICAST_IF },
0N/A { java_net_SocketOptions_IP_MULTICAST_LOOP, IPPROTO_IP, IP_MULTICAST_LOOP },
0N/A };
0N/A
0N/A int i;
0N/A
0N/A /*
0N/A * Different multicast options if IPv6 is enabled
0N/A */
0N/A#ifdef AF_INET6
0N/A if (ipv6_available()) {
0N/A switch (cmd) {
0N/A case java_net_SocketOptions_IP_MULTICAST_IF:
0N/A case java_net_SocketOptions_IP_MULTICAST_IF2:
0N/A *level = IPPROTO_IPV6;
0N/A *optname = IPV6_MULTICAST_IF;
0N/A return 0;
0N/A
0N/A case java_net_SocketOptions_IP_MULTICAST_LOOP:
0N/A *level = IPPROTO_IPV6;
0N/A *optname = IPV6_MULTICAST_LOOP;
0N/A return 0;
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A /*
0N/A * Map the Java level option to the native level
0N/A */
0N/A for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
0N/A if (cmd == opts[i].cmd) {
0N/A *level = opts[i].level;
0N/A *optname = opts[i].optname;
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A /* not found */
0N/A return -1;
0N/A}
0N/A
0N/A/*
0N/A * Determine the default interface for an IPv6 address.
0N/A *
0N/A * 1. Scans /proc/net/ipv6_route for a matching route
0N/A * (eg: fe80::/10 or a route for the specific address).
0N/A * This will tell us the interface to use (eg: "eth0").
0N/A *
0N/A * 2. Lookup /proc/net/if_inet6 to map the interface
0N/A * name to an interface index.
0N/A *
0N/A * Returns :-
0N/A * -1 if error
0N/A * 0 if no matching interface
0N/A * >1 interface index to use for the link-local address.
0N/A */
0N/A#if defined(__linux__) && defined(AF_INET6)
0N/Aint getDefaultIPv6Interface(struct in6_addr *target_addr) {
0N/A FILE *f;
0N/A char srcp[8][5];
0N/A char hopp[8][5];
0N/A int dest_plen, src_plen, use, refcnt, metric;
0N/A unsigned long flags;
0N/A char dest_str[40];
0N/A struct in6_addr dest_addr;
0N/A char device[16];
0N/A jboolean match = JNI_FALSE;
0N/A
0N/A /*
0N/A * Scan /proc/net/ipv6_route looking for a matching
0N/A * route.
0N/A */
0N/A if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
0N/A return -1;
0N/A }
0N/A while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
0N/A "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
0N/A "%4s%4s%4s%4s%4s%4s%4s%4s "
0N/A "%08x %08x %08x %08lx %8s",
0N/A dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
0N/A &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
0N/A &dest_plen,
0N/A srcp[0], srcp[1], srcp[2], srcp[3],
0N/A srcp[4], srcp[5], srcp[6], srcp[7],
0N/A &src_plen,
0N/A hopp[0], hopp[1], hopp[2], hopp[3],
0N/A hopp[4], hopp[5], hopp[6], hopp[7],
0N/A &metric, &use, &refcnt, &flags, device) == 31) {
0N/A
0N/A /*
0N/A * Some routes should be ignored
0N/A */
0N/A if ( (dest_plen < 0 || dest_plen > 128) ||
0N/A (src_plen != 0) ||
0N/A (flags & (RTF_POLICY | RTF_FLOW)) ||
0N/A ((flags & RTF_REJECT) && dest_plen == 0) ) {
0N/A continue;
0N/A }
0N/A
0N/A /*
0N/A * Convert the destination address
0N/A */
0N/A dest_str[4] = ':';
0N/A dest_str[9] = ':';
0N/A dest_str[14] = ':';
0N/A dest_str[19] = ':';
0N/A dest_str[24] = ':';
0N/A dest_str[29] = ':';
0N/A dest_str[34] = ':';
0N/A dest_str[39] = '\0';
0N/A
0N/A if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
0N/A /* not an Ipv6 address */
0N/A continue;
0N/A } else {
0N/A /*
0N/A * The prefix len (dest_plen) indicates the number of bits we
0N/A * need to match on.
0N/A *
0N/A * dest_plen / 8 => number of bytes to match
0N/A * dest_plen % 8 => number of additional bits to match
0N/A *
0N/A * eg: fe80::/10 => match 1 byte + 2 additional bits in the
0N/A * the next byte.
0N/A */
0N/A int byte_count = dest_plen >> 3;
0N/A int extra_bits = dest_plen & 0x3;
0N/A
0N/A if (byte_count > 0) {
0N/A if (memcmp(target_addr, &dest_addr, byte_count)) {
0N/A continue; /* no match */
0N/A }
0N/A }
0N/A
0N/A if (extra_bits > 0) {
0N/A unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
0N/A unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
0N/A unsigned char mask = 0xff << (8 - extra_bits);
0N/A if ((c1 & mask) != (c2 & mask)) {
0N/A continue;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * We have a match
0N/A */
0N/A match = JNI_TRUE;
0N/A break;
0N/A }
0N/A }
0N/A fclose(f);
0N/A
0N/A /*
0N/A * If there's a match then we lookup the interface
0N/A * index.
0N/A */
0N/A if (match) {
3657N/A char devname[21];
0N/A char addr6p[8][5];
0N/A int plen, scope, dad_status, if_idx;
0N/A
0N/A if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
0N/A while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
0N/A addr6p[0], addr6p[1], addr6p[2], addr6p[3],
0N/A addr6p[4], addr6p[5], addr6p[6], addr6p[7],
0N/A &if_idx, &plen, &scope, &dad_status, devname) == 13) {
0N/A
0N/A if (strcmp(devname, device) == 0) {
0N/A /*
0N/A * Found - so just return the index
0N/A */
0N/A fclose(f);
0N/A return if_idx;
0N/A }
0N/A }
0N/A fclose(f);
0N/A } else {
0N/A /*
0N/A * Couldn't open /proc/net/if_inet6
0N/A */
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * If we get here it means we didn't there wasn't any
0N/A * route or we couldn't get the index of the interface.
0N/A */
0N/A return 0;
0N/A}
0N/A#endif
0N/A
0N/A
0N/A/*
0N/A * Wrapper for getsockopt system routine - does any necessary
0N/A * pre/post processing to deal with OS specific oddies :-
0N/A *
0N/A * IP_TOS is a no-op with IPv6 sockets as it's setup when
0N/A * the connection is established.
0N/A *
0N/A * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
0N/A * to compensate for an incorrect value returned by the kernel.
0N/A */
0N/Aint
0N/ANET_GetSockOpt(int fd, int level, int opt, void *result,
0N/A int *len)
0N/A{
0N/A int rv;
0N/A
0N/A#ifdef AF_INET6
0N/A if ((level == IPPROTO_IP) && (opt == IP_TOS)) {
0N/A if (ipv6_available()) {
0N/A
0N/A /*
0N/A * For IPv6 socket option implemented at Java-level
0N/A * so return -1.
0N/A */
0N/A int *tc = (int *)result;
0N/A *tc = -1;
0N/A return 0;
0N/A }
0N/A }
0N/A#endif
0N/A
454N/A#ifdef __solaris__
454N/A rv = getsockopt(fd, level, opt, result, len);
454N/A#else
454N/A {
454N/A socklen_t socklen = *len;
454N/A rv = getsockopt(fd, level, opt, result, &socklen);
454N/A *len = socklen;
454N/A }
454N/A#endif
454N/A
0N/A if (rv < 0) {
0N/A return rv;
0N/A }
0N/A
0N/A#ifdef __linux__
0N/A /*
0N/A * On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This
0N/A * stems from additional socket structures in the send
0N/A * and receive buffers.
0N/A */
0N/A if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF)
0N/A || (opt == SO_RCVBUF))) {
0N/A int n = *((int *)result);
0N/A n /= 2;
0N/A *((int *)result) = n;
0N/A }
0N/A#endif
0N/A
4638N/A/* Workaround for Mac OS treating linger value as
4638N/A * signed integer
4638N/A */
4638N/A#ifdef MACOSX
4638N/A if (level == SOL_SOCKET && opt == SO_LINGER) {
4638N/A struct linger* to_cast = (struct linger*)result;
4638N/A to_cast->l_linger = (unsigned short)to_cast->l_linger;
4638N/A }
4638N/A#endif
0N/A return rv;
0N/A}
0N/A
0N/A/*
0N/A * Wrapper for setsockopt system routine - performs any
0N/A * necessary pre/post processing to deal with OS specific
0N/A * issue :-
0N/A *
0N/A * On Solaris need to limit the suggested value for SO_SNDBUF
0N/A * and SO_RCVBUF to the kernel configured limit
0N/A *
0N/A * For IP_TOS socket option need to mask off bits as this
0N/A * aren't automatically masked by the kernel and results in
0N/A * an error. In addition IP_TOS is a noop with IPv6 as it
0N/A * should be setup as connection time.
0N/A */
0N/Aint
0N/ANET_SetSockOpt(int fd, int level, int opt, const void *arg,
0N/A int len)
0N/A{
0N/A#ifndef IPTOS_TOS_MASK
0N/A#define IPTOS_TOS_MASK 0x1e
0N/A#endif
0N/A#ifndef IPTOS_PREC_MASK
0N/A#define IPTOS_PREC_MASK 0xe0
0N/A#endif
0N/A
4632N/A#if defined(_ALLBSD_SOURCE)
4632N/A#if defined(KIPC_MAXSOCKBUF)
4632N/A int mib[3];
4632N/A size_t rlen;
4632N/A#endif
4632N/A
4632N/A int *bufsize;
4632N/A
4632N/A#ifdef __APPLE__
4632N/A static int maxsockbuf = -1;
4632N/A#else
4632N/A static long maxsockbuf = -1;
4632N/A#endif
4632N/A
4632N/A int addopt;
4632N/A struct linger *ling;
4632N/A#endif
4632N/A
0N/A /*
0N/A * IPPROTO/IP_TOS :-
4669N/A * 1. IPv6 on Solaris/Mac OS: no-op and will be set
4669N/A * in flowinfo field when connecting TCP socket,
4669N/A * or sending UDP packet.
0N/A * 2. IPv6 on Linux: By default Linux ignores flowinfo
0N/A * field so enable IPV6_FLOWINFO_SEND so that flowinfo
0N/A * will be examined.
0N/A * 3. IPv4: set socket option based on ToS and Precedence
0N/A * fields (otherwise get invalid argument)
0N/A */
0N/A if (level == IPPROTO_IP && opt == IP_TOS) {
0N/A int *iptos;
0N/A
4669N/A#if defined(AF_INET6) && (defined(__solaris__) || defined(MACOSX))
0N/A if (ipv6_available()) {
0N/A return 0;
0N/A }
0N/A#endif
0N/A
0N/A#if defined(AF_INET6) && defined(__linux__)
0N/A if (ipv6_available()) {
0N/A int optval = 1;
0N/A return setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
0N/A (void *)&optval, sizeof(optval));
0N/A }
0N/A#endif
0N/A
0N/A iptos = (int *)arg;
0N/A *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
0N/A }
0N/A
0N/A /*
2827N/A * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris we may need to clamp
2827N/A * the value when it exceeds the system limit.
0N/A */
0N/A#ifdef __solaris__
0N/A if (level == SOL_SOCKET) {
0N/A if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
3123N/A int sotype=0, arglen;
0N/A int *bufsize, maxbuf;
2827N/A int ret;
2827N/A
2827N/A /* Attempt with the original size */
2827N/A ret = setsockopt(fd, level, opt, arg, len);
2827N/A if ((ret == 0) || (ret == -1 && errno != ENOBUFS))
2827N/A return ret;
2827N/A
2827N/A /* Exceeded system limit so clamp and retry */
0N/A
0N/A arglen = sizeof(sotype);
0N/A if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
0N/A &arglen) < 0) {
0N/A return -1;
0N/A }
0N/A
3123N/A /*
3123N/A * We try to get tcp_maxbuf (and udp_max_buf) using
3123N/A * an ioctl() that isn't available on all versions of Solaris.
3123N/A * If that fails, we use the search algorithm in findMaxBuf()
3123N/A */
3123N/A if (!init_tcp_max_buf && sotype == SOCK_STREAM) {
3123N/A tcp_max_buf = getParam("/dev/tcp", "tcp_max_buf");
3123N/A if (tcp_max_buf == -1) {
3123N/A tcp_max_buf = findMaxBuf(fd, opt, SOCK_STREAM);
3123N/A if (tcp_max_buf == -1) {
3123N/A return -1;
3123N/A }
3123N/A }
3123N/A init_tcp_max_buf = 1;
3123N/A } else if (!init_udp_max_buf && sotype == SOCK_DGRAM) {
3123N/A udp_max_buf = getParam("/dev/udp", "udp_max_buf");
3123N/A if (udp_max_buf == -1) {
3123N/A udp_max_buf = findMaxBuf(fd, opt, SOCK_DGRAM);
3123N/A if (udp_max_buf == -1) {
3123N/A return -1;
3123N/A }
3123N/A }
3123N/A init_udp_max_buf = 1;
3123N/A }
3123N/A
0N/A maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
0N/A bufsize = (int *)arg;
0N/A if (*bufsize > maxbuf) {
0N/A *bufsize = maxbuf;
0N/A }
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A /*
0N/A * On Linux the receive buffer is used for both socket
0N/A * structures and the the packet payload. The implication
0N/A * is that if SO_RCVBUF is too small then small packets
0N/A * must be discard.
0N/A */
0N/A#ifdef __linux__
0N/A if (level == SOL_SOCKET && opt == SO_RCVBUF) {
0N/A int *bufsize = (int *)arg;
0N/A if (*bufsize < 1024) {
0N/A *bufsize = 1024;
0N/A }
0N/A }
0N/A#endif
0N/A
4632N/A#if defined(_ALLBSD_SOURCE)
4632N/A /*
4632N/A * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On FreeBSD need to
4632N/A * ensure that value is <= kern.ipc.maxsockbuf as otherwise we get
4632N/A * an ENOBUFS error.
4632N/A */
4632N/A if (level == SOL_SOCKET) {
4632N/A if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
4632N/A#ifdef KIPC_MAXSOCKBUF
4632N/A if (maxsockbuf == -1) {
4632N/A mib[0] = CTL_KERN;
4632N/A mib[1] = KERN_IPC;
4632N/A mib[2] = KIPC_MAXSOCKBUF;
4632N/A rlen = sizeof(maxsockbuf);
4632N/A if (sysctl(mib, 3, &maxsockbuf, &rlen, NULL, 0) == -1)
4632N/A maxsockbuf = 1024;
4632N/A
4632N/A#if 1
4632N/A /* XXXBSD: This is a hack to workaround mb_max/mb_max_adj
4632N/A problem. It should be removed when kern.ipc.maxsockbuf
4632N/A will be real value. */
4632N/A maxsockbuf = (maxsockbuf/5)*4;
4632N/A#endif
4632N/A }
4632N/A#elif defined(__OpenBSD__)
4632N/A maxsockbuf = SB_MAX;
4632N/A#else
4632N/A maxsockbuf = 64 * 1024; /* XXX: NetBSD */
4632N/A#endif
4632N/A
4632N/A bufsize = (int *)arg;
4632N/A if (*bufsize > maxsockbuf) {
4632N/A *bufsize = maxsockbuf;
4632N/A }
4632N/A
4632N/A if (opt == SO_RCVBUF && *bufsize < 1024) {
4632N/A *bufsize = 1024;
4632N/A }
4632N/A
4632N/A }
4632N/A }
4632N/A
4632N/A /*
4632N/A * On Solaris, SO_REUSEADDR will allow multiple datagram
4632N/A * sockets to bind to the same port. The network jck tests
4632N/A * for this "feature", so we need to emulate it by turning on
4632N/A * SO_REUSEPORT as well for that combination.
4632N/A */
4632N/A if (level == SOL_SOCKET && opt == SO_REUSEADDR) {
4632N/A int sotype;
4632N/A socklen_t arglen;
4632N/A
4632N/A arglen = sizeof(sotype);
4632N/A if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) < 0) {
4632N/A return -1;
4632N/A }
4632N/A
4632N/A if (sotype == SOCK_DGRAM) {
4632N/A addopt = SO_REUSEPORT;
4632N/A setsockopt(fd, level, addopt, arg, len);
4632N/A }
4632N/A }
4632N/A
4632N/A#endif
4632N/A
0N/A return setsockopt(fd, level, opt, arg, len);
0N/A}
0N/A
0N/A/*
0N/A * Wrapper for bind system call - performs any necessary pre/post
0N/A * processing to deal with OS specific issues :-
0N/A *
0N/A * Linux allows a socket to bind to 127.0.0.255 which must be
0N/A * caught.
0N/A *
5107N/A * On Solaris with IPv6 enabled we must use an exclusive
0N/A * bind to guaranteed a unique port number across the IPv4 and
0N/A * IPv6 port spaces.
0N/A *
0N/A */
0N/Aint
0N/ANET_Bind(int fd, struct sockaddr *him, int len)
0N/A{
0N/A#if defined(__solaris__) && defined(AF_INET6)
0N/A int level = -1;
0N/A int exclbind = -1;
0N/A#endif
0N/A int rv;
0N/A
0N/A#ifdef __linux__
0N/A /*
0N/A * ## get bugId for this issue - goes back to 1.2.2 port ##
0N/A * ## When IPv6 is enabled this will be an IPv4-mapped
0N/A * ## with family set to AF_INET6
0N/A */
0N/A if (him->sa_family == AF_INET) {
0N/A struct sockaddr_in *sa = (struct sockaddr_in *)him;
0N/A if ((ntohl(sa->sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) {
0N/A errno = EADDRNOTAVAIL;
0N/A return -1;
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A#if defined(__solaris__) && defined(AF_INET6)
0N/A /*
5107N/A * Solaris has seperate IPv4 and IPv6 port spaces so we
0N/A * use an exclusive bind when SO_REUSEADDR is not used to
0N/A * give the illusion of a unified port space.
5107N/A * This also avoids problems with IPv6 sockets connecting
0N/A * to IPv4 mapped addresses whereby the socket conversion
0N/A * results in a late bind that fails because the
0N/A * corresponding IPv4 port is in use.
0N/A */
0N/A if (ipv6_available()) {
0N/A int arg, len;
0N/A
0N/A len = sizeof(arg);
5107N/A if (useExclBind || getsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
5107N/A (char *)&arg, &len) == 0) {
5107N/A if (useExclBind || arg == 0) {
0N/A /*
5107N/A * SO_REUSEADDR is disabled or sun.net.useExclusiveBind
5107N/A * property is true so enable TCP_EXCLBIND or
0N/A * UDP_EXCLBIND
0N/A */
0N/A len = sizeof(arg);
0N/A if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg,
0N/A &len) == 0) {
0N/A if (arg == SOCK_STREAM) {
0N/A level = IPPROTO_TCP;
0N/A exclbind = TCP_EXCLBIND;
0N/A } else {
0N/A level = IPPROTO_UDP;
0N/A exclbind = UDP_EXCLBIND;
0N/A }
0N/A }
0N/A
0N/A arg = 1;
0N/A setsockopt(fd, level, exclbind, (char *)&arg,
0N/A sizeof(arg));
0N/A }
0N/A }
0N/A }
0N/A
0N/A#endif
0N/A
0N/A rv = bind(fd, him, len);
0N/A
0N/A#if defined(__solaris__) && defined(AF_INET6)
0N/A if (rv < 0) {
0N/A int en = errno;
0N/A /* Restore *_EXCLBIND if the bind fails */
0N/A if (exclbind != -1) {
0N/A int arg = 0;
0N/A setsockopt(fd, level, exclbind, (char *)&arg,
0N/A sizeof(arg));
0N/A }
0N/A errno = en;
0N/A }
0N/A#endif
0N/A
0N/A return rv;
0N/A}
0N/A
0N/A/**
0N/A * Wrapper for select/poll with timeout on a single file descriptor.
0N/A *
0N/A * flags (defined in net_util_md.h can be any combination of
0N/A * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
0N/A *
0N/A * The function will return when either the socket is ready for one
0N/A * of the specified operation or the timeout expired.
0N/A *
0N/A * It returns the time left from the timeout (possibly 0), or -1 if it expired.
0N/A */
0N/A
0N/Ajint
0N/ANET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
0N/A{
0N/A jlong prevTime = JVM_CurrentTimeMillis(env, 0);
0N/A jint read_rv;
0N/A
0N/A while (1) {
0N/A jlong newTime;
0N/A#ifndef USE_SELECT
0N/A {
0N/A struct pollfd pfd;
0N/A pfd.fd = fd;
0N/A pfd.events = 0;
0N/A if (flags & NET_WAIT_READ)
0N/A pfd.events |= POLLIN;
0N/A if (flags & NET_WAIT_WRITE)
0N/A pfd.events |= POLLOUT;
0N/A if (flags & NET_WAIT_CONNECT)
0N/A pfd.events |= POLLOUT;
0N/A
0N/A errno = 0;
0N/A read_rv = NET_Poll(&pfd, 1, timeout);
0N/A }
0N/A#else
0N/A {
0N/A fd_set rd, wr, ex;
0N/A struct timeval t;
0N/A
0N/A t.tv_sec = timeout / 1000;
0N/A t.tv_usec = (timeout % 1000) * 1000;
0N/A
0N/A FD_ZERO(&rd);
0N/A FD_ZERO(&wr);
0N/A FD_ZERO(&ex);
0N/A if (flags & NET_WAIT_READ) {
0N/A FD_SET(fd, &rd);
0N/A }
0N/A if (flags & NET_WAIT_WRITE) {
0N/A FD_SET(fd, &wr);
0N/A }
0N/A if (flags & NET_WAIT_CONNECT) {
0N/A FD_SET(fd, &wr);
0N/A FD_SET(fd, &ex);
0N/A }
0N/A
0N/A errno = 0;
0N/A read_rv = NET_Select(fd+1, &rd, &wr, &ex, &t);
0N/A }
0N/A#endif
0N/A
0N/A newTime = JVM_CurrentTimeMillis(env, 0);
0N/A timeout -= (newTime - prevTime);
0N/A if (timeout <= 0) {
0N/A return read_rv > 0 ? 0 : -1;
0N/A }
0N/A newTime = prevTime;
0N/A
0N/A if (read_rv > 0) {
0N/A break;
0N/A }
0N/A
0N/A
0N/A } /* while */
0N/A
0N/A return timeout;
0N/A}