src/java.base/windows/native/libnet/net_util_md.h
author michaelm
Fri, 25 Oct 2019 15:56:35 +0100
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 49892 8bed781a8d9c
child 59019 dafa6389ed47
permissions -rw-r--r--
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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: 5456
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: 5456
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: 5456
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <WS2tcpip.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    27
#include <iphlpapi.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    28
#include <icmpapi.h>
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 49892
diff changeset
    29
#include <afunix.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/* used to disable connection reset messages on Windows XP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#ifndef SIO_UDP_CONNRESET
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#ifndef IN6_IS_ADDR_ANY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define IN6_IS_ADDR_ANY(a)      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    (((a)->s6_words[0] == 0) && ((a)->s6_words[1] == 0) &&      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    ((a)->s6_words[2] == 0) && ((a)->s6_words[3] == 0) &&       \
5456
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents: 1247
diff changeset
    40
    ((a)->s6_words[4] == 0) && ((a)->s6_words[5] == 0) &&       \
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents: 1247
diff changeset
    41
    ((a)->s6_words[6] == 0) && ((a)->s6_words[7] == 0))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifndef IPV6_V6ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define IPV6_V6ONLY     27 /* Treat wildcard bind as AF_INET6-only. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define MAX_BUFFER_LEN          2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define MAX_HEAP_BUFFER_LEN     65536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/* true if SO_RCVTIMEO is supported by underlying provider */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
extern jboolean isRcvTimeoutSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
void NET_ThrowCurrent(JNIEnv *env, char *msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
typedef union {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    57
    struct sockaddr     sa;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    58
    struct sockaddr_in  sa4;
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    59
    struct sockaddr_in6 sa6;
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 49892
diff changeset
    60
    struct sockaddr_un  saun;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
} SOCKETADDRESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * passed to NET_BindV6. Both ipv4_fd and ipv6_fd must be created and unbound
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * sockets. On return they may refer to different sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
struct ipv6bind {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    68
    SOCKETADDRESS      *addr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    69
    SOCKET              ipv4_fd;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    70
    SOCKET              ipv6_fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define SOCKETADDRESS_COPY(DST,SRC) {                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    if ((SRC)->sa_family == AF_INET6) {                         \
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    75
        memcpy ((DST), (SRC), sizeof (struct sockaddr_in6));    \
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    } else {                                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        memcpy ((DST), (SRC), sizeof (struct sockaddr_in));     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    81
#define SET_PORT(X,Y) {                    \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    82
    if ((X)->sa.sa_family == AF_INET) {    \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    83
        (X)->sa4.sin_port = (Y);           \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    84
    } else {                               \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    85
        (X)->sa6.sin6_port = (Y);          \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    86
    }                                      \
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    89
#define GET_PORT(X) ((X)->sa.sa_family == AF_INET ? (X)->sa4.sin_port : (X)->sa6.sin6_port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#define IS_LOOPBACK_ADDRESS(x) ( \
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    92
    ((x)->sa.sa_family == AF_INET) ? \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    93
        (ntohl((x)->sa4.sin_addr.s_addr) == INADDR_LOOPBACK) : \
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
    94
        (IN6ADDR_ISLOOPBACK(x)) \
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
JNIEXPORT int JNICALL NET_SocketClose(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
JNIEXPORT int JNICALL NET_Timeout(int fd, long timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   101
int NET_Socket(int domain, int type, int protocol);
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   102
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   103
void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   104
                                  const char *defaultDetail);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * differs from NET_Timeout() as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * If timeout = -1, it blocks forever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * returns 1 or 2 depending if only one or both sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * fire at same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * *fdret is (one of) the active fds. If both sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * fire at same time, *fd == fd always.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
JNIEXPORT int JNICALL NET_Timeout2(int fd, int fd1, long timeout, int *fdret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   119
JNIEXPORT int JNICALL NET_BindV6(struct ipv6bind *b, jboolean exclBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   121
JNIEXPORT int JNICALL NET_WinBind(int s, SOCKETADDRESS *sa, int len,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   122
                                  jboolean exclBind);
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 14342
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
/* XP versions of the native routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   127
  (JNIEnv *env, jclass cls, jstring name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   129
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  (JNIEnv *env, jclass cls, jint index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   133
  (JNIEnv *env, jclass cls, jobject iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   136
  (JNIEnv *env, jclass cls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   139
  (JNIEnv *env, jclass cls, jstring name, jint index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   142
  (JNIEnv *env, jclass cls, jstring name, jint index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   145
  (JNIEnv *env, jclass cls, jstring name, jint index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   148
  (JNIEnv *env, jclass cls, jstring name, jint index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   151
  (JNIEnv *env, jclass class, jstring name, jint index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41210
diff changeset
   154
  (JNIEnv *env, jclass cls, jstring name, jint index);