jdk/src/java.base/windows/native/libnet/net_util_md.c
author alanb
Tue, 23 Feb 2016 17:41:00 +0000
changeset 36115 0676e37a0b9c
parent 27178 885f4428b501
child 41210 73593ed418ff
permissions -rw-r--r--
6432031: Add support for SO_REUSEPORT Reviewed-by: alanb, simonis, chegar Contributed-by: yingqi.lu@intel.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <ws2tcpip.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
    32
// Taken from mstcpip.h in Windows SDK 8.0 or newer.
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
    33
#define SIO_LOOPBACK_FAST_PATH              _WSAIOW(IOC_VENDOR,16)
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/* true if SO_RCVTIMEO is supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
jboolean isRcvTimeoutSupported = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Table of Windows Sockets errors, the specific exception we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * throw for the error, and the error text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Note that this table excludes OS dependent errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Latest list of Windows Sockets errors can be found at :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * http://msdn.microsoft.com/library/psdk/winsock/errors_3wc2.htm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int errCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    const char *exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    const char *errString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
} const winsock_errors[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    { WSAEACCES,                0,      "Permission denied" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    { WSAEADDRINUSE,            "BindException",        "Address already in use" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    { WSAEADDRNOTAVAIL,         "BindException",        "Cannot assign requested address" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    { WSAEAFNOSUPPORT,          0,      "Address family not supported by protocol family" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    { WSAEALREADY,              0,      "Operation already in progress" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    { WSAECONNABORTED,          0,      "Software caused connection abort" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    { WSAECONNREFUSED,          "ConnectException",     "Connection refused" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    { WSAECONNRESET,            0,      "Connection reset by peer" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    { WSAEDESTADDRREQ,          0,      "Destination address required" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    { WSAEFAULT,                0,      "Bad address" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    { WSAEHOSTDOWN,             0,      "Host is down" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    { WSAEHOSTUNREACH,          "NoRouteToHostException",       "No route to host" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    { WSAEINPROGRESS,           0,      "Operation now in progress" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    { WSAEINTR,                 0,      "Interrupted function call" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    { WSAEINVAL,                0,      "Invalid argument" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    { WSAEISCONN,               0,      "Socket is already connected" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    { WSAEMFILE,                0,      "Too many open files" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    { WSAEMSGSIZE,              0,      "The message is larger than the maximum supported by the underlying transport" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    { WSAENETDOWN,              0,      "Network is down" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    { WSAENETRESET,             0,      "Network dropped connection on reset" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    { WSAENETUNREACH,           0,      "Network is unreachable" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    { WSAENOBUFS,               0,      "No buffer space available (maximum connections reached?)" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    { WSAENOPROTOOPT,           0,      "Bad protocol option" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    { WSAENOTCONN,              0,      "Socket is not connected" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    { WSAENOTSOCK,              0,      "Socket operation on nonsocket" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    { WSAEOPNOTSUPP,            0,      "Operation not supported" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    { WSAEPFNOSUPPORT,          0,      "Protocol family not supported" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    { WSAEPROCLIM,              0,      "Too many processes" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    { WSAEPROTONOSUPPORT,       0,      "Protocol not supported" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    { WSAEPROTOTYPE,            0,      "Protocol wrong type for socket" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    { WSAESHUTDOWN,             0,      "Cannot send after socket shutdown" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    { WSAESOCKTNOSUPPORT,       0,      "Socket type not supported" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    { WSAETIMEDOUT,             "ConnectException",     "Connection timed out" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    { WSATYPE_NOT_FOUND,        0,      "Class type not found" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    { WSAEWOULDBLOCK,           0,      "Resource temporarily unavailable" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    { WSAHOST_NOT_FOUND,        0,      "Host not found" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    { WSA_NOT_ENOUGH_MEMORY,    0,      "Insufficient memory available" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    { WSANOTINITIALISED,        0,      "Successful WSAStartup not yet performed" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    { WSANO_DATA,               0,      "Valid name, no data record of requested type" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    { WSANO_RECOVERY,           0,      "This is a nonrecoverable error" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    { WSASYSNOTREADY,           0,      "Network subsystem is unavailable" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    { WSATRY_AGAIN,             0,      "Nonauthoritative host not found" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    { WSAVERNOTSUPPORTED,       0,      "Winsock.dll version out of range" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    { WSAEDISCON,               0,      "Graceful shutdown in progress" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    { WSA_OPERATION_ABORTED,    0,      "Overlapped operation aborted" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Initialize Windows Sockets API support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
BOOL WINAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    WSADATA wsadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        case DLL_PROCESS_ATTACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        case DLL_PROCESS_DETACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            WSACleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   131
void platformInit() {}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   132
void parseExclusiveBindProperty(JNIEnv *env) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Since winsock doesn't have the equivalent of strerror(errno)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * use table to lookup error text for the error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
NET_ThrowNew(JNIEnv *env, int errorNum, char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    int table_size = sizeof(winsock_errors) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                     sizeof(winsock_errors[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    char exc[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    char fullMsg[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    char *excP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * If exception already throw then don't overwrite it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Default message text if not provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        msg = "no further information";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Check table for known winsock errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    while (i < table_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (errorNum == winsock_errors[i].errCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * If found get pick the specific exception and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * message corresponding to this error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (i < table_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        excP = (char *)winsock_errors[i].exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        jio_snprintf(fullMsg, sizeof(fullMsg), "%s: %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     (char *)winsock_errors[i].errString, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        jio_snprintf(fullMsg, sizeof(fullMsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                     "Unrecognized Windows Sockets error: %d: %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                     errorNum, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Throw SocketException if no specific exception for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (excP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        excP = "SocketException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    sprintf(exc, "%s%s", JNU_JAVANETPKG, excP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    JNU_ThrowByName(env, exc, fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
NET_ThrowCurrent(JNIEnv *env, char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    NET_ThrowNew(env, WSAGetLastError(), msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
NET_ThrowSocketException(JNIEnv *env, char* msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    static jclass cls = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        cls = (*env)->FindClass(env, "java/net/SocketException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        cls = (*env)->NewGlobalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    (*env)->ThrowNew(env, cls, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                   const char *defaultDetail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    sprintf(errmsg, "errno: %d, error: %s\n", WSAGetLastError(), defaultDetail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    JNU_ThrowByNameWithLastError(env, name, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
jfieldID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
NET_GetFileDescriptorID(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    CHECK_NULL_RETURN(cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    return (*env)->GetFieldID(env, cls, "fd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
jint  IPv6_supported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
{
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   236
    SOCKET s = socket(AF_INET6, SOCK_STREAM, 0) ;
9019
03920d69bb93 7032866: Problem with fix for 7030256
michaelm
parents: 9003
diff changeset
   237
    if (s == INVALID_SOCKET) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   240
    closesocket(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
   245
jint reuseport_supported()
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
   246
{
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
   247
    /* SO_REUSEPORT is not supported onn Windows */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
   248
    return JNI_FALSE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 27178
diff changeset
   249
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Return the default TOS value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
int NET_GetDefaultTOS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static int default_tos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    OSVERSIONINFO ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    HKEY hKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    LONG ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * If default ToS already determined then return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if (default_tos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return default_tos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Assume default is "normal service"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    default_tos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Which OS is this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    ver.dwOSVersionInfoSize = sizeof(ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    GetVersionEx(&ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * If 2000 or greater then no default ToS in registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (ver.dwMajorVersion >= 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return default_tos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Query the registry to see if a Default ToS has been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Different registry entry for NT vs 95/98/ME.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                           "SYSTEM\\CurrentControlSet\\Services\\Tcp\\Parameters",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                           0, KEY_READ, (PHKEY)&hKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                           "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP\\Parameters",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                           0, KEY_READ, (PHKEY)&hKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    if (ret == ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        DWORD dwLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        DWORD dwDefaultTOS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        ULONG ulType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        dwLen = sizeof(dwDefaultTOS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        ret = RegQueryValueEx(hKey, "DefaultTOS",  NULL, &ulType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                             (LPBYTE)&dwDefaultTOS, &dwLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        RegCloseKey(hKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (ret == ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            default_tos = (int)dwDefaultTOS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    return default_tos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
/* call NET_MapSocketOptionV6 for the IPv6 fd only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * and NET_MapSocketOption for the IPv4 fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
NET_MapSocketOptionV6(jint cmd, int *level, int *optname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    switch (cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        case java_net_SocketOptions_IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        case java_net_SocketOptions_IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            *optname = IPV6_MULTICAST_IF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            *optname = IPV6_MULTICAST_LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    return NET_MapSocketOption (cmd, level, optname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
NET_MapSocketOption(jint cmd, int *level, int *optname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        jint cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    } sockopts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static sockopts opts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        { java_net_SocketOptions_TCP_NODELAY,   IPPROTO_TCP,    TCP_NODELAY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        { java_net_SocketOptions_SO_OOBINLINE,  SOL_SOCKET,     SO_OOBINLINE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        { java_net_SocketOptions_SO_LINGER,     SOL_SOCKET,     SO_LINGER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        { java_net_SocketOptions_SO_SNDBUF,     SOL_SOCKET,     SO_SNDBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        { java_net_SocketOptions_SO_RCVBUF,     SOL_SOCKET,     SO_RCVBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        { java_net_SocketOptions_SO_KEEPALIVE,  SOL_SOCKET,     SO_KEEPALIVE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        { java_net_SocketOptions_SO_REUSEADDR,  SOL_SOCKET,     SO_REUSEADDR },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        { java_net_SocketOptions_SO_BROADCAST,  SOL_SOCKET,     SO_BROADCAST },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        { java_net_SocketOptions_IP_MULTICAST_IF,   IPPROTO_IP, IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        { java_net_SocketOptions_IP_MULTICAST_LOOP, IPPROTO_IP, IP_MULTICAST_LOOP },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        { java_net_SocketOptions_IP_TOS,            IPPROTO_IP, IP_TOS },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Map the Java level option to the native level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (cmd == opts[i].cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            *level = opts[i].level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            *optname = opts[i].optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /* not found */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * Wrapper for setsockopt dealing with Windows specific issues :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
 * IP_TOS and IP_MULTICAST_LOOP can't be set on some Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
 * editions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 * The value for the type-of-service (TOS) needs to be masked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * to get consistent behaviour with other operating systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
NET_SetSockOpt(int s, int level, int optname, const void *optval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
               int optlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
{
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   397
    int rv = 0;
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   398
    int parg = 0;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   399
    int plen = sizeof(parg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    if (level == IPPROTO_IP && optname == IP_TOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int *tos = (int *)optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        *tos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   406
    if (optname == SO_REUSEADDR) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   407
        /*
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   408
         * Do not set SO_REUSEADDE if SO_EXCLUSIVEADDUSE is already set
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   409
         */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   410
        rv = NET_GetSockOpt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *)&parg, &plen);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   411
        if (rv == 0 && parg == 1) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   412
            return rv;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   413
        }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   414
    }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   415
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    rv = setsockopt(s, level, optname, optval, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         * IP_TOS & IP_MULTICAST_LOOP can't be set on some versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * of Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if ((WSAGetLastError() == WSAENOPROTOOPT) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            (level == IPPROTO_IP) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            (optname == IP_TOS || optname == IP_MULTICAST_LOOP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * IP_TOS can't be set on unbound UDP sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if ((WSAGetLastError() == WSAEINVAL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            (level == IPPROTO_IP) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            (optname == IP_TOS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * Wrapper for setsockopt dealing with Windows specific issues :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * IP_TOS is not supported on some versions of Windows so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * instead return the default value for the OS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
NET_GetSockOpt(int s, int level, int optname, void *optval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
               int *optlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
25388
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   454
    if (level == IPPROTO_IPV6 && optname == IPV6_TCLASS) {
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   455
        int *intopt = (int *)optval;
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   456
        *intopt = 0;
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   457
        *optlen = sizeof(*intopt);
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   458
        return 0;
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   459
    }
65f81ea02187 8048212: Two tests failed with "java.net.SocketException: Bad protocol option" on Windows after 8029607
michaelm
parents: 23010
diff changeset
   460
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    rv = getsockopt(s, level, optname, optval, optlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * IPPROTO_IP/IP_TOS is not supported on some Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * editions so return the default type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (WSAGetLastError() == WSAENOPROTOOPT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            level == IPPROTO_IP && optname == IP_TOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            int *tos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            tos = (int *)optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            *tos = NET_GetDefaultTOS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
/*
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   486
 * Sets SO_ECLUSIVEADDRUSE if SO_REUSEADDR is not already set.
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   487
 */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   488
void setExclusiveBind(int fd) {
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   489
    int parg = 0;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   490
    int plen = sizeof(parg);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   491
    int rv = 0;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   492
    rv = NET_GetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&parg, &plen);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   493
    if (rv == 0 && parg == 0) {
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   494
        parg = 1;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   495
        rv = NET_SetSockOpt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&parg, plen);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   496
    }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   497
}
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   498
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   499
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 * Wrapper for bind winsock call - transparent converts an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 * error related to binding to a port that has exclusive access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * into an error indicating the port is in use (facilitates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * better error reporting).
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   504
 *
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   505
 * Should be only called by the wrapper method NET_WinBind
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
NET_Bind(int s, struct sockaddr *him, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
{
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   510
    int rv = 0;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   511
    rv = bind(s, him, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * If bind fails with WSAEACCES it means that a privileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * process has done an exclusive bind (NT SP4/2000/XP only).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (WSAGetLastError() == WSAEACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            WSASetLastError(WSAEADDRINUSE);
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
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   526
/*
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   527
 * Wrapper for NET_Bind call. Sets SO_EXCLUSIVEADDRUSE
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   528
 * if required, and then calls NET_BIND
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   529
 */
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   530
JNIEXPORT int JNICALL
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   531
NET_WinBind(int s, struct sockaddr *him, int len, jboolean exclBind)
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   532
{
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   533
    if (exclBind == JNI_TRUE)
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   534
        setExclusiveBind(s);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   535
    return NET_Bind(s, him, len);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   536
}
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   537
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
NET_SocketClose(int fd) {
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   540
    struct linger l = {0, 0};
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 25388
diff changeset
   541
    int ret = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    int len = sizeof (l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (l.l_onoff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            WSASendDisconnect(fd, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    ret = closesocket (fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
NET_Timeout(int fd, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    FD_SET(fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    ret = select (fd + 1, &tbl, 0, 0, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * differs from NET_Timeout() as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * If timeout = -1, it blocks forever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 * returns 1 or 2 depending if only one or both sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 * fire at same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
 * *fdret is (one of) the active fds. If both sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
 * fire at same time, *fdret = fd always.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
NET_Timeout2(int fd, int fd1, long timeout, int *fdret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    struct timeval t, *tP = &t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    if (timeout == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        tP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    FD_SET(fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    FD_SET(fd1, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    ret = select (0, &tbl, 0, 0, tP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    switch (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return 0; /* timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (FD_ISSET (fd, &tbl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            *fdret= fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            *fdret= fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        *fdret= fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
void dumpAddr (char *str, void *addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    struct SOCKADDR_IN6 *a = (struct SOCKADDR_IN6 *)addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    int family = a->sin6_family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    printf ("%s\n", str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        struct sockaddr_in *him = (struct sockaddr_in *)addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        printf ("AF_INET: port %d: %x\n", ntohs(him->sin_port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                          ntohl(him->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        struct in6_addr *in = &a->sin6_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        printf ("AF_INET6 ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        printf ("port %d ", ntohs (a->sin6_port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        printf ("flow %d ", a->sin6_flowinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        printf ("addr ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        for (i=0; i<7; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            printf ("%04x:", ntohs(in->s6_words[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        printf ("%04x", ntohs(in->s6_words[7]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        printf (" scope %d\n", a->sin6_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
/* Macro, which cleans-up the iv6bind structure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
 * closes the two sockets (if open),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
 * and returns SOCKET_ERROR. Used in NET_BindV6 only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
26458
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   638
#define CLOSE_SOCKETS_AND_RETURN do {   \
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    if (fd != -1) {                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        closesocket (fd);               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        fd = -1;                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    if (ofd != -1) {                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        closesocket (ofd);              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        ofd = -1;                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    if (close_fd != -1) {               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        closesocket (close_fd);         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        close_fd = -1;                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    if (close_ofd != -1) {              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        closesocket (close_ofd);        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        close_ofd = -1;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    b->ipv4_fd = b->ipv6_fd = -1;       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    return SOCKET_ERROR;                \
26458
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   657
} while(0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
 * if ipv6 is available, call NET_BindV6 to bind to the required address/port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 * Because the same port number may need to be reserved in both v4 and v6 space,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 * this may require socket(s) to be re-opened. Therefore, all of this information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
 * is passed in and returned through the ipv6bind structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * If the request is to bind to a specific address, then this (by definition) means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * only bind in either v4 or v6, and this is just the same as normal. ie. a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * call to bind() will suffice. The other socket is closed in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 * The more complicated case is when the requested address is ::0 or 0.0.0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
 * Two further cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
 * 2. If the reqeusted port is 0 (ie. any port) then we try to bind in v4 space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
 *    first with a wild-card port argument. We then try to bind in v6 space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 *    using the returned port number. If this fails, we repeat the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 *    until a free port common to both spaces becomes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 * 3. If the requested port is a specific port, then we just try to get that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
 *    port in both spaces, and if it is not free in both, then the bind fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
 * On failure, sockets are closed and an error returned with CLOSE_SOCKETS_AND_RETURN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
JNIEXPORT int JNICALL
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   684
NET_BindV6(struct ipv6bind* b, jboolean exclBind) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    int fd=-1, ofd=-1, rv, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /* need to defer close until new sockets created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    int close_fd=-1, close_ofd=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    SOCKETADDRESS oaddr; /* other address to bind */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    int family = b->addr->him.sa_family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    int ofamily;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    u_short port; /* requested port parameter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    u_short bound_port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    if (family == AF_INET && (b->addr->him4.sin_addr.s_addr != INADDR_ANY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        /* bind to v4 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int ret;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   697
        ret = NET_WinBind ((int)b->ipv4_fd, (struct sockaddr *)b->addr,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   698
                                sizeof (struct sockaddr_in), exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if (ret == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        closesocket (b->ipv6_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        b->ipv6_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    if (family == AF_INET6 && (!IN6_IS_ADDR_ANY(&b->addr->him6.sin6_addr))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        /* bind to v6 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        int ret;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   709
        ret = NET_WinBind ((int)b->ipv6_fd, (struct sockaddr *)b->addr,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   710
                                sizeof (struct SOCKADDR_IN6), exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (ret == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        closesocket (b->ipv4_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        b->ipv4_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /* We need to bind on both stacks, with the same port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    memset (&oaddr, 0, sizeof(oaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        ofamily = AF_INET6;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   724
        fd = (int)b->ipv4_fd;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   725
        ofd = (int)b->ipv6_fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        port = (u_short)GET_PORT (b->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        IN6ADDR_SETANY (&oaddr.him6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        oaddr.him6.sin6_port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        ofamily = AF_INET;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   731
        ofd = (int)b->ipv4_fd;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   732
        fd = (int)b->ipv6_fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        port = (u_short)GET_PORT (b->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        oaddr.him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        oaddr.him4.sin_port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        oaddr.him4.sin_addr.s_addr = INADDR_ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   739
    rv = NET_WinBind(fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr), exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /* get the port and set it in the other address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    len = SOCKETADDRESS_LEN(b->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    if (getsockname(fd, (struct sockaddr *)b->addr, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    bound_port = GET_PORT (b->addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    SET_PORT (&oaddr, bound_port);
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   751
    if ((rv=NET_WinBind (ofd, (struct sockaddr *) &oaddr,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   752
                         SOCKETADDRESS_LEN (&oaddr), exclBind)) == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        int retries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        int sotype, arglen=sizeof(sotype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        /* no retries unless, the request was for any free port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (port != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
#define SOCK_RETRIES 50
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        /* 50 is an arbitrary limit, just to ensure that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * cannot be an endless loop. Would expect socket creation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * succeed sooner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        for (retries = 0; retries < SOCK_RETRIES; retries ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            close_fd = fd; fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            close_ofd = ofd; ofd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            b->ipv4_fd = SOCKET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            b->ipv6_fd = SOCKET_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            /* create two new sockets */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   777
            fd = (int)socket (family, sotype, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (fd == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   781
            ofd = (int)socket (ofamily, sotype, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (ofd == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            /* bind random port on first socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            SET_PORT (&oaddr, 0);
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   788
            rv = NET_WinBind (ofd, (struct sockaddr *)&oaddr, SOCKETADDRESS_LEN(&oaddr),
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   789
                              exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            /* close the original pair of sockets before continuing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            closesocket (close_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            closesocket (close_ofd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            close_fd = close_ofd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            /* bind new port on second socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            len = SOCKETADDRESS_LEN(&oaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (getsockname(ofd, (struct sockaddr *)&oaddr, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            bound_port = GET_PORT (&oaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            SET_PORT (b->addr, bound_port);
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   805
            rv = NET_WinBind (fd, (struct sockaddr *)b->addr, SOCKETADDRESS_LEN(b->addr),
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   806
                              exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (rv != SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    b->ipv4_fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    b->ipv6_fd = ofd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    b->ipv4_fd = ofd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    b->ipv6_fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        CLOSE_SOCKETS_AND_RETURN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
 * Determine the default interface for an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
 * Returns :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
 *      0 if error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
 *      > 0 interface index to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
jint getDefaultIPv6Interface(JNIEnv *env, struct SOCKADDR_IN6 *target_addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    DWORD b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    struct sockaddr_in6 route;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    SOCKET fd = socket(AF_INET6, SOCK_STREAM, 0);
9019
03920d69bb93 7032866: Problem with fix for 7030256
michaelm
parents: 9003
diff changeset
   837
    if (fd == INVALID_SOCKET) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    ret = WSAIoctl(fd, SIO_ROUTING_INTERFACE_QUERY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    (void *)target_addr, sizeof(struct sockaddr_in6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    (void *)&route, sizeof(struct sockaddr_in6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    &b, 0, 0);
9019
03920d69bb93 7032866: Problem with fix for 7030256
michaelm
parents: 9003
diff changeset
   845
    if (ret == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        return route.sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   855
/**
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   856
 * Enables SIO_LOOPBACK_FAST_PATH
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   857
 */
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   858
JNIEXPORT jint JNICALL
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   859
NET_EnableFastTcpLoopback(int fd) {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   860
    int enabled = 1;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   861
    DWORD result_byte_count = -1;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   862
    int result = WSAIoctl(fd,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   863
                          SIO_LOOPBACK_FAST_PATH,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   864
                          &enabled,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   865
                          sizeof(enabled),
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   866
                          NULL,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   867
                          0,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   868
                          &result_byte_count,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   869
                          NULL,
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   870
                          NULL);
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   871
    return result == SOCKET_ERROR ? WSAGetLastError() : 0;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   872
}
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 26458
diff changeset
   873
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
/* If address types is IPv6, then IPv6 must be available. Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 * no address can be generated. In the case of an IPv4 Inetaddress this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
 * method will return an IPv4 mapped address where IPv6 is available and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 * v4MappedAddress is TRUE. Otherwise it will return a sockaddr_in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
 * structure for an IPv4 InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                          int *len, jboolean v4MappedAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    jint family, iafam;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   884
    iafam = getInetAddress_family(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    family = (iafam == IPv4)? AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    if (ipv6_available() && !(family == AF_INET && v4MappedAddress == JNI_FALSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        jint address, scopeid = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        jint cached_scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        if (family == AF_INET) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            memset((char *) caddr, 0, 16);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   894
            address = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (address == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                /* we would always prefer IPv6 wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                caddr[11] = 0xff; */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        } else {
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 18192
diff changeset
   908
            getInet6Address_ipaddress(env, iaObj, (char *)caddr);
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 18192
diff changeset
   909
            scopeid = getInet6Address_scopeid(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            cached_scope_id = (jint)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        memset((char *)him6, 0, sizeof(struct SOCKADDR_IN6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        him6->sin6_port = (u_short) htons((u_short)port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        him6->sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if ((family == AF_INET6) && IN6_IS_ADDR_LINKLOCAL( &(him6->sin6_addr) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            && (!scopeid && !cached_scope_id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            cached_scope_id = getDefaultIPv6Interface(env, him6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        him6->sin6_scope_id = scopeid != 0 ? scopeid : cached_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        *len = sizeof(struct SOCKADDR_IN6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        struct sockaddr_in *him4 = (struct sockaddr_in*)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (family != AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
          JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
          return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        memset((char *) him4, 0, sizeof(struct sockaddr_in));
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   932
        address = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        him4->sin_port = htons((short) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        him4->sin_addr.s_addr = (u_long) htonl(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        him4->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        *len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 1090
diff changeset
   941
JNIEXPORT jint JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
NET_GetPortFromSockaddr(struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        return ntohs(((struct sockaddr_in6*)him)->sin6_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        return ntohs(((struct sockaddr_in*)him)->sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
NET_IsIPv4Mapped(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    for (i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        if (caddr[i] != 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        return 1; /* true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
NET_IPv4MappedToIPv4(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        | (caddr[15] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        if (caddr1[i] != caddr2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
int getScopeID (struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    return him6->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
int cmpScopeID (unsigned int scope, struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    return him6->sin6_scope_id == scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
 * Wrapper for select/poll with timeout on a single file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
 * flags (defined in net_util_md.h can be any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
 * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
 * The function will return when either the socket is ready for one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
 * of the specified operation or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
 * It returns the time left from the timeout, or -1 if it expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    jlong prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    jint read_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        jlong newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        fd_set rd, wr, ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        t.tv_sec = timeout / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        t.tv_usec = (timeout % 1000) * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        FD_ZERO(&rd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        FD_ZERO(&wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        FD_ZERO(&ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (flags & NET_WAIT_READ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
          FD_SET(fd, &rd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        if (flags & NET_WAIT_WRITE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
          FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if (flags & NET_WAIT_CONNECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
          FD_SET(fd, &wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
          FD_SET(fd, &ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        read_rv = select(fd+1, &rd, &wr, &ex, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        newTime = JVM_CurrentTimeMillis(env, 0);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
  1036
        timeout -= (jint)(newTime - prevTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
          return read_rv > 0 ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        newTime = prevTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (read_rv > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
      } /* while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
int NET_Socket (int domain, int type, int protocol) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
  1053
    SOCKET sock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    sock = socket (domain, type, protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    if (sock != INVALID_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        SetHandleInformation((HANDLE)(uintptr_t)sock, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
  1058
    return (int)sock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
}