/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#include <dlfcn.h>
#ifndef _ALLBSD_SOURCE
#include <values.h>
#else
#include <limits.h>
#ifndef MAXINT
#endif
#endif
#ifdef __solaris__
#include <stropts.h>
#endif
#ifdef __linux__
#ifndef IPV6_FLOWINFO_SEND
#endif
#endif
#include "jni_util.h"
#include "jvm.h"
#include "net_util.h"
#include "java_net_SocketOptions.h"
/* needed from libsocket on Solaris 8 */
/*
* EXCLBIND socket options only on Solaris
*/
#if defined(__solaris__) && !defined(TCP_EXCLBIND)
#endif
#if defined(__solaris__) && !defined(UDP_EXCLBIND)
#endif
{
#ifdef MACOSX
CHECK_NULL(c);
CHECK_NULL(c);
ni_class = c;
}
int defaultIndex;
}
#endif
}
CHECK_NULL(c);
CHECK_NULL(c);
ni_class = c;
}
int defaultIndex = 0;
return defaultIndex;
}
#ifdef __solaris__
static int tcp_max_buf;
static int udp_max_buf;
static int useExclBind = 0;
/*
* Get the specified parameter from the specified driver. The value
* of the parameter is assumed to be an 'int'. If the parameter
* cannot be obtained return -1
*/
static int
{
int s;
int value;
if (s < 0) {
return -1;
}
value = -1;
} else {
}
close (s);
return value;
}
/*
* Iterative way to find the max value that SO_SNDBUF or SO_RCVBUF
* for Solaris versions that do not support the ioctl() in getParam().
* Ugly, but only called once (for each sotype).
*
* As an optimisation, we make a guess using the default values for Solaris
* assuming they haven't been modified with ndd.
*/
int a = 0;
int b = MAXINT;
int initial_guess;
if (sotype == SOCK_DGRAM) {
} else {
}
return initial_guess - 1;
}
a = initial_guess;
} else {
b = initial_guess - 1;
}
do {
a = mid + 1;
} else {
b = mid - 1;
}
} while (b >= a);
return limit;
}
#endif
#ifdef __linux__
static int kernelV22 = 0;
static int vinit = 0;
int kernelIsV22 () {
if (!vinit) {
}
}
vinit = 1;
}
return kernelV22;
}
static int kernelV24 = 0;
static int vinit24 = 0;
int kernelIsV24 () {
if (!vinit24) {
}
}
vinit24 = 1;
}
return kernelV24;
}
if (kernelIsV22()) {
return 0;
}
return hext->sin6_scope_id;
}
if (kernelIsV22()) {
return 1; /* scope is ignored for comparison in 2.2 kernel */
}
}
#else
return him6->sin6_scope_id;
}
}
#endif
void
const char *defaultDetail) {
}
void
}
void
if (!msg) {
msg = "no further information";
}
switch(errorNumber) {
case EBADF:
break;
case EINTR:
break;
default:
errno = errorNumber;
break;
}
}
{
}
#if defined(DONT_ENABLE_IPV6)
{
return JNI_FALSE;
}
#else /* !DONT_ENABLE_IPV6 */
{
#ifndef AF_INET6
return JNI_FALSE;
#endif
#ifdef AF_INET6
int fd;
void *ipv6_fn;
if (fd < 0) {
/*
* TODO: We really cant tell since it may be an unrelated error
* for now we will assume that AF_INET6 is not available
*/
return JNI_FALSE;
}
/*
* If fd 0 is a socket it means we've been launched from inetd or
* xinetd. If it's a socket then check the family - if it's an
* IPv4 socket then we need to disable IPv6.
*/
return JNI_FALSE;
}
}
/**
* Linux - check if any interface has an IPv6 address.
* Don't need to parse the line - we just need an indication.
*/
#ifdef __linux__
{
char *bufP;
return JNI_FALSE;
}
return JNI_FALSE;
}
}
#endif
/**
* On Solaris 8 it's possible to create INET6 sockets even
* though IPv6 is not enabled on all interfaces. Thus we
* query the number of IPv6 addresses to verify that IPv6
* has been configured on at least one interface.
*
* On Linux it doesn't matter - if IPv6 is built-in the
* kernel then IPv6 addresses will be bound automatically
* to all interfaces.
*/
#ifdef __solaris__
#ifdef SIOCGLIFNUM
{
numifs.lifn_flags = 0;
/**
* SIOCGLIFNUM failed - assume IPv6 not configured
*/
return JNI_FALSE;
}
/**
* If no IPv6 addresses then return false. If count > 0
* it's possible that all IPv6 addresses are "down" but
* that's okay as they may be brought "up" while the
* VM is running.
*/
if (numifs.lifn_count == 0) {
return JNI_FALSE;
}
}
#else
/* SIOCGLIFNUM not defined in build environment ??? */
return JNI_FALSE;
#endif
#endif /* __solaris */
/*
* OK we may have the stack available in the kernel,
* we should also check if the APIs are available.
*/
return JNI_FALSE;
}
/*
* We've got the library, let's get the pointers to some
* IPV6 specific functions. We have to do that because, at least
* on Solaris we may build on a system without IPV6 networking
* libraries, therefore we can't have a hard link to these
* functions.
*/
/* We need all 3 of them */
}
return JNI_TRUE;
#endif /* AF_INET6 */
}
#endif /* DONT_ENABLE_IPV6 */
const char* hostname,
int gai_error)
{
int size;
char *buf;
const char *error_string =
if (error_string == NULL)
error_string = "unknown error";
if (buf) {
jstring s;
if (s != NULL) {
if (x != NULL)
}
}
}
void
#ifdef AF_INET6
if (ipv6_available()) {
*len = sizeof(struct sockaddr_in6);
} else
#endif /* AF_INET6 */
{
*len = sizeof(struct sockaddr_in);
}
}
/* following code creates a list of addresses from the kernel
* routing table that are routed via the loopback address.
* We check all destination addresses against this table
* and override the scope_id field to use the relevant value for "lo"
* in order to work-around the Linux bug that prevents packets destined
* for certain local addresses from being sent via a physical interface.
*/
struct loopback_route {
};
static int lo_scope_id = 0;
static void initLoopbackRoutes();
int i;
for (i=0; i<16; i++) {
}
printf ("\n");
}
int byte_count;
int extra_bits, i;
if (loRoutes == 0) {
}
if (byte_count > 0) {
continue; /* no match */
}
}
if (extra_bits > 0) {
continue;
}
}
return JNI_TRUE;
}
return JNI_FALSE;
}
static void initLoopbackRoutes() {
FILE *f;
unsigned long flags;
if (loRoutes != 0) {
}
if (loRoutes == 0) {
return;
}
/*
* Scan /proc/net/ipv6_route looking for a matching
* route.
*/
return ;
}
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
"%4s%4s%4s%4s%4s%4s%4s%4s %02x "
"%4s%4s%4s%4s%4s%4s%4s%4s "
"%08x %08x %08x %08lx %8s",
&src_plen,
/*
* Some routes should be ignored
*/
(src_plen != 0) ||
continue;
}
/*
* Convert the destination address
*/
/* not an Ipv6 address */
continue;
}
/* Not a loopback route */
continue;
} else {
if (nRoutes == loRoutes_size) {
sizeof (struct loopback_route) * 2);
if (loRoutes == 0) {
return ;
}
loRoutes_size *= 2;
}
nRoutes ++;
}
}
fclose (f);
{
/* now find the scope_id for "lo" */
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
/*
* Found - so just return the index
*/
fclose(f);
return;
}
}
fclose(f);
}
}
}
/*
* Following is used for binding to local addresses. Equivalent
* to code above, for bind().
*/
struct localinterface {
int index;
};
/* not thread safe: make sure called once from one thread */
static void initLocalIfs () {
FILE *f;
return ;
}
while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x "
nifs ++;
if (nifs > localifsSize) {
if (localifs == 0) {
nifs = 0;
fclose (f);
return;
}
localifsSize += 5;
} else {
lif ++;
}
}
fclose (f);
}
/* return the scope_id (interface index) of the
* interface corresponding to the given address
* returns 0 if no match found
*/
int i;
if (localifs == 0) {
initLocalIfs();
}
}
}
return 0;
}
void initLocalAddrTable () {
initLocalIfs();
}
#else
void initLocalAddrTable () {}
#endif
#ifdef __solaris__
CHECK_NULL(s);
useExclBind = 1;
}
#endif
}
/* In the case of an IPv4 Inetaddress this method will return an
* IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE.
* Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress.
*/
#ifdef AF_INET6
/* needs work. 1. family 2. clean up him6 etc deallocate memory */
if (address == INADDR_ANY) {
/* we would always prefer IPv6 wildcard address
caddr[10] = 0xff;
caddr[11] = 0xff; */
} else {
}
} else {
}
*len = sizeof(struct sockaddr_in6) ;
#if defined(_ALLBSD_SOURCE) && defined(_AF_INET6)
// XXXBSD: should we do something with scope id here ? see below linux comment
/* MMM: Come back to this! */
#endif
/*
* On Linux if we are connecting to a link-local address
* we need to specify the interface in the scope_id (2.4 kernel only)
*
* If the scope was cached the we use the cached value. If not cached but
* specified in the Inet6Address we use that, but we first check if the
* address needs to be routed via the loopback interface. In this case,
* we override the specified value with that of the loopback interface.
* If no cached value exists and no value was specified by user, then
* we try to determine a value ffrom the routing table. In all these
* cases the used value is cached for further use.
*/
#ifdef __linux__
if (ia6_cachedscopeidID && !old_kernel) {
/* if cached value exists then use it. Otherwise, check
* if scope is set in the address.
*/
if (!cached_scope_id) {
if (ia6_scopeidID) {
}
if (scope_id != 0) {
/* check user-specified value for loopback case
* that needs to be overridden
*/
}
} else {
/*
* Otherwise consult the IPv6 routing tables to
* try determine the appropriate interface.
*/
if (kernelIsV24()) {
} else {
if (cached_scope_id == 0) {
}
}
}
}
}
/*
* If we have a scope_id use the extended form
* of sockaddr_in6.
*/
if (!old_kernel) {
(struct sockaddr_in6 *)him;
*len = sizeof(struct sockaddr_in6);
}
}
#else
/* handle scope_id for solaris */
if (ia6_scopeidID) {
}
}
#endif
} else
#endif /* AF_INET6 */
{
return -1;
}
*len = sizeof(struct sockaddr_in);
}
return 0;
}
void
#ifdef AF_INET6
}
#endif /* AF_INET6 */
}
#ifdef AF_INET6
} else
#endif /* AF_INET6 */
{
}
}
int
int i;
for (i = 0; i < 10; i++) {
if (caddr[i] != 0x00) {
return 0; /* false */
}
}
return 1; /* true */
}
return 0; /* false */
}
int
}
int
int i;
for (i = 0; i < 16; i++) {
return 0; /* false */
}
}
return 1;
}
}
int i;
for (i = 0; i < 16; i++) {
if (caddr[i] != 0) {
return 0;
}
}
return 1;
}
/*
* Map the Java level socket option to the platform specific
* level and option name.
*/
int
static struct {
int level;
int optname;
} const opts[] = {
};
int i;
/*
* Different multicast options if IPv6 is enabled
*/
#ifdef AF_INET6
if (ipv6_available()) {
switch (cmd) {
*level = IPPROTO_IPV6;
return 0;
*level = IPPROTO_IPV6;
return 0;
}
}
#endif
/*
* Map the Java level option to the native level
*/
return 0;
}
}
/* not found */
return -1;
}
/*
* Determine the default interface for an IPv6 address.
*
* 1. Scans /proc/net/ipv6_route for a matching route
* (eg: fe80::/10 or a route for the specific address).
* This will tell us the interface to use (eg: "eth0").
*
* name to an interface index.
*
* Returns :-
* -1 if error
* 0 if no matching interface
* >1 interface index to use for the link-local address.
*/
FILE *f;
unsigned long flags;
/*
* Scan /proc/net/ipv6_route looking for a matching
* route.
*/
return -1;
}
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
"%4s%4s%4s%4s%4s%4s%4s%4s %02x "
"%4s%4s%4s%4s%4s%4s%4s%4s "
"%08x %08x %08x %08lx %8s",
&src_plen,
/*
* Some routes should be ignored
*/
(src_plen != 0) ||
continue;
}
/*
* Convert the destination address
*/
/* not an Ipv6 address */
continue;
} else {
/*
* The prefix len (dest_plen) indicates the number of bits we
* need to match on.
*
* dest_plen / 8 => number of bytes to match
* dest_plen % 8 => number of additional bits to match
*
* eg: fe80::/10 => match 1 byte + 2 additional bits in the
* the next byte.
*/
if (byte_count > 0) {
continue; /* no match */
}
}
if (extra_bits > 0) {
continue;
}
}
/*
* We have a match
*/
break;
}
}
fclose(f);
/*
* If there's a match then we lookup the interface
* index.
*/
if (match) {
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
/*
* Found - so just return the index
*/
fclose(f);
return if_idx;
}
}
fclose(f);
} else {
/*
*/
return -1;
}
}
/*
* If we get here it means we didn't there wasn't any
* route or we couldn't get the index of the interface.
*/
return 0;
}
#endif
/*
* Wrapper for getsockopt system routine - does any necessary
*
* IP_TOS is a no-op with IPv6 sockets as it's setup when
* the connection is established.
*
* to compensate for an incorrect value returned by the kernel.
*/
int
int *len)
{
int rv;
#ifdef AF_INET6
if (ipv6_available()) {
/*
* For IPv6 socket option implemented at Java-level
* so return -1.
*/
*tc = -1;
return 0;
}
}
#endif
#ifdef __solaris__
#else
{
}
#endif
if (rv < 0) {
return rv;
}
#ifdef __linux__
/*
* stems from additional socket structures in the send
* and receive buffers.
*/
int n = *((int *)result);
n /= 2;
*((int *)result) = n;
}
#endif
/* Workaround for Mac OS treating linger value as
* signed integer
*/
#ifdef MACOSX
}
#endif
return rv;
}
/*
* Wrapper for setsockopt system routine - performs any
* issue :-
*
* On Solaris need to limit the suggested value for SO_SNDBUF
* and SO_RCVBUF to the kernel configured limit
*
* For IP_TOS socket option need to mask off bits as this
* aren't automatically masked by the kernel and results in
* an error. In addition IP_TOS is a noop with IPv6 as it
* should be setup as connection time.
*/
int
int len)
{
#ifndef IPTOS_TOS_MASK
#endif
#ifndef IPTOS_PREC_MASK
#endif
#if defined(_ALLBSD_SOURCE)
#if defined(KIPC_MAXSOCKBUF)
#endif
int *bufsize;
#ifdef __APPLE__
#else
#endif
int addopt;
#endif
/*
* in flowinfo field when connecting TCP socket,
* or sending UDP packet.
* 2. IPv6 on Linux: By default Linux ignores flowinfo
* field so enable IPV6_FLOWINFO_SEND so that flowinfo
* will be examined.
* 3. IPv4: set socket option based on ToS and Precedence
* fields (otherwise get invalid argument)
*/
int *iptos;
if (ipv6_available()) {
return 0;
}
#endif
if (ipv6_available()) {
}
#endif
}
/*
* SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris we may need to clamp
* the value when it exceeds the system limit.
*/
#ifdef __solaris__
if (level == SOL_SOCKET) {
int ret;
/* Attempt with the original size */
return ret;
/* Exceeded system limit so clamp and retry */
&arglen) < 0) {
return -1;
}
/*
* We try to get tcp_maxbuf (and udp_max_buf) using
* an ioctl() that isn't available on all versions of Solaris.
* If that fails, we use the search algorithm in findMaxBuf()
*/
if (tcp_max_buf == -1) {
if (tcp_max_buf == -1) {
return -1;
}
}
init_tcp_max_buf = 1;
if (udp_max_buf == -1) {
if (udp_max_buf == -1) {
return -1;
}
}
init_udp_max_buf = 1;
}
}
}
}
#endif
/*
* On Linux the receive buffer is used for both socket
* structures and the the packet payload. The implication
* is that if SO_RCVBUF is too small then small packets
* must be discard.
*/
#ifdef __linux__
if (*bufsize < 1024) {
*bufsize = 1024;
}
}
#endif
#if defined(_ALLBSD_SOURCE)
/*
* SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On FreeBSD need to
* ensure that value is <= kern.ipc.maxsockbuf as otherwise we get
* an ENOBUFS error.
*/
if (level == SOL_SOCKET) {
#ifdef KIPC_MAXSOCKBUF
if (maxsockbuf == -1) {
rlen = sizeof(maxsockbuf);
maxsockbuf = 1024;
#if 1
/* XXXBSD: This is a hack to workaround mb_max/mb_max_adj
problem. It should be removed when kern.ipc.maxsockbuf
will be real value. */
#endif
}
#elif defined(__OpenBSD__)
maxsockbuf = SB_MAX;
#else
#endif
if (*bufsize > maxsockbuf) {
*bufsize = maxsockbuf;
}
*bufsize = 1024;
}
}
}
/*
* On Solaris, SO_REUSEADDR will allow multiple datagram
* sockets to bind to the same port. The network jck tests
* for this "feature", so we need to emulate it by turning on
* SO_REUSEPORT as well for that combination.
*/
int sotype;
return -1;
}
if (sotype == SOCK_DGRAM) {
}
}
#endif
}
/*
* processing to deal with OS specific issues :-
*
* Linux allows a socket to bind to 127.0.0.255 which must be
* caught.
*
* On Solaris with IPv6 enabled we must use an exclusive
* bind to guaranteed a unique port number across the IPv4 and
* IPv6 port spaces.
*
*/
int
{
#if defined(__solaris__) && defined(AF_INET6)
#endif
int rv;
#ifdef __linux__
/*
* ## get bugId for this issue - goes back to 1.2.2 port ##
* ## When IPv6 is enabled this will be an IPv4-mapped
* ## with family set to AF_INET6
*/
return -1;
}
}
#endif
#if defined(__solaris__) && defined(AF_INET6)
/*
* Solaris has seperate IPv4 and IPv6 port spaces so we
* use an exclusive bind when SO_REUSEADDR is not used to
* give the illusion of a unified port space.
* This also avoids problems with IPv6 sockets connecting
* to IPv4 mapped addresses whereby the socket conversion
* results in a late bind that fails because the
* corresponding IPv4 port is in use.
*/
if (ipv6_available()) {
if (useExclBind || arg == 0) {
/*
* SO_REUSEADDR is disabled or sun.net.useExclusiveBind
* property is true so enable TCP_EXCLBIND or
* UDP_EXCLBIND
*/
&len) == 0) {
if (arg == SOCK_STREAM) {
level = IPPROTO_TCP;
} else {
level = IPPROTO_UDP;
}
}
arg = 1;
sizeof(arg));
}
}
}
#endif
#if defined(__solaris__) && defined(AF_INET6)
if (rv < 0) {
/* Restore *_EXCLBIND if the bind fails */
if (exclbind != -1) {
int arg = 0;
sizeof(arg));
}
}
#endif
return rv;
}
/**
*
* flags (defined in net_util_md.h can be any combination of
* NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
*
* The function will return when either the socket is ready for one
* of the specified operation or the timeout expired.
*
* It returns the time left from the timeout (possibly 0), or -1 if it expired.
*/
{
while (1) {
#ifndef USE_SELECT
{
if (flags & NET_WAIT_READ)
if (flags & NET_WAIT_WRITE)
if (flags & NET_WAIT_CONNECT)
errno = 0;
}
#else
{
struct timeval t;
if (flags & NET_WAIT_READ) {
}
if (flags & NET_WAIT_WRITE) {
}
if (flags & NET_WAIT_CONNECT) {
}
errno = 0;
}
#endif
if (timeout <= 0) {
return read_rv > 0 ? 0 : -1;
}
if (read_rv > 0) {
break;
}
} /* while */
return timeout;
}