8302659: Modernize Windows native code for NetworkInterface

Reviewed-by: ihse, djelinski, alanb, michaelm
This commit is contained in:
Rich DiCroce 2023-03-15 17:06:32 +00:00 committed by Daniel Jeliński
parent 01e6920581
commit 35a2969057
6 changed files with 654 additions and 2080 deletions

View File

@ -47,8 +47,6 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBNET, \
DISABLED_WARNINGS_gcc_NetworkInterface.c := unused-function, \
DISABLED_WARNINGS_clang_net_util_md.c := format-nonliteral, \
DISABLED_WARNINGS_microsoft_InetAddress.c := 4244, \
DISABLED_WARNINGS_microsoft_NetworkInterface.c := 4133, \
DISABLED_WARNINGS_microsoft_NetworkInterface_winXP.c := 4133, \
DISABLED_WARNINGS_microsoft_ResolverConfigurationImpl.c := 4996, \
LDFLAGS := $(LDFLAGS_JDKLIB) \
$(call SET_SHARED_LIBRARY_ORIGIN), \

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,55 +31,10 @@
/*
* Structures used when enumerating interfaces and addresses
*/
typedef struct _netaddr {
SOCKETADDRESS addr; /* IPv4 or IPv6 address */
SOCKETADDRESS brdcast;
short mask;
struct _netaddr *next;
typedef struct _netaddr {
SOCKADDR_INET Address;
UINT8 PrefixLength;
struct _netaddr *Next;
} netaddr;
typedef struct _netif {
char *name;
char *displayName;
DWORD dwIndex; /* Internal index */
DWORD ifType; /* Interface type */
int index; /* Friendly index */
struct _netif *next;
/* Following fields used on Windows XP when IPv6 is used only */
jboolean hasIpv6Address; /* true when following fields valid */
jboolean dNameIsUnicode; /* Display Name is Unicode */
int naddrs; /* Number of addrs */
DWORD ipv6Index;
struct _netaddr *addrs; /* addr list for interfaces */
} netif;
extern void free_netif(netif *netifP);
extern void free_netaddr(netaddr *netaddrP);
/* various JNI ids */
extern jclass ni_class; /* NetworkInterface */
extern jmethodID ni_ctor; /* NetworkInterface() */
extern jfieldID ni_indexID; /* NetworkInterface.index */
extern jfieldID ni_addrsID; /* NetworkInterface.addrs */
extern jfieldID ni_bindsID; /* NetworkInterface.bindings */
extern jfieldID ni_nameID; /* NetworkInterface.name */
extern jfieldID ni_displayNameID; /* NetworkInterface.displayName */
extern jfieldID ni_childsID; /* NetworkInterface.childs */
extern jclass ni_ibcls; /* InterfaceAddress */
extern jmethodID ni_ibctrID; /* InterfaceAddress() */
extern jfieldID ni_ibaddressID; /* InterfaceAddress.address */
extern jfieldID ni_ibbroadcastID; /* InterfaceAddress.broadcast */
extern jfieldID ni_ibmaskID; /* InterfaceAddress.maskLength */
int enumInterfaces(JNIEnv *env, netif **netifPP);
// Windows Visa (and later) only.....
#ifndef IF_TYPE_IEEE80211
#define IF_TYPE_IEEE80211 71
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,6 +33,8 @@
#include "jni_util.h"
#define MAX_STR_LEN 1024
#define BUFF_SIZE 15360
#define MAX_TRIES 3
#define STS_NO_CONFIG 0x0 /* no configuration found */
#define STS_SL_FOUND 0x1 /* search list found */
@ -46,7 +48,78 @@
static jfieldID searchlistID;
static jfieldID nameserversID;
extern int getAdapters(JNIEnv *env, int flags, IP_ADAPTER_ADDRESSES **adapters);
/*
* return an array of IP_ADAPTER_ADDRESSES containing one element
* for each adapter on the system. Returned in *adapters.
* Buffer is malloc'd and must be freed (unless error returned)
*/
static int getAdapters (JNIEnv *env, int flags, IP_ADAPTER_ADDRESSES **adapters) {
DWORD ret;
IP_ADAPTER_ADDRESSES *adapterInfo;
ULONG len;
int try;
*adapters = NULL;
adapterInfo = (IP_ADAPTER_ADDRESSES *) malloc(BUFF_SIZE);
if (adapterInfo == NULL) {
JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
"Native heap allocation failure");
return -1;
}
len = BUFF_SIZE;
ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
for (try = 0; ret == ERROR_BUFFER_OVERFLOW && try < MAX_TRIES; ++try) {
IP_ADAPTER_ADDRESSES * newAdapterInfo = NULL;
if (len < (ULONG_MAX - BUFF_SIZE)) {
len += BUFF_SIZE;
}
newAdapterInfo =
(IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
if (newAdapterInfo == NULL) {
free(adapterInfo);
JNU_ThrowByName(env, "java/lang/OutOfMemoryError",
"Native heap allocation failure");
return -1;
}
adapterInfo = newAdapterInfo;
ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
}
if (ret != ERROR_SUCCESS) {
free (adapterInfo);
switch (ret) {
case ERROR_INVALID_PARAMETER:
JNU_ThrowInternalError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"invalid parameter");
break;
case ERROR_NOT_ENOUGH_MEMORY:
JNU_ThrowOutOfMemoryError(env,
"IP Helper Library GetAdaptersAddresses function failed: "
"not enough memory");
break;
case ERROR_NO_DATA:
// not an error
*adapters = NULL;
return ERROR_SUCCESS;
default:
SetLastError(ret);
JNU_ThrowByNameWithMessageAndLastError(env,
JNU_JAVANETPKG "SocketException",
"IP Helper Library GetAdaptersAddresses function failed");
break;
}
return -1;
}
*adapters = adapterInfo;
return ERROR_SUCCESS;
}
/*
* Utility routine to append s2 to s1 with a comma delimiter.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -106,7 +106,7 @@ public class OptionsTest {
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
while (nifs.hasMoreElements()) {
NetworkInterface ni = nifs.nextElement();
if (ni.supportsMulticast()) {
if (ni.supportsMulticast() && !ni.getInterfaceAddresses().isEmpty()) {
return ni;
}
}