Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/native/libnet/ResolverConfigurationImpl.c
41119 views
1
/*
2
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <stdlib.h>
27
#include <stdio.h>
28
#include <stddef.h>
29
#include <time.h>
30
#include <assert.h>
31
32
#include "net_util.h"
33
#include "jni_util.h"
34
35
#define MAX_STR_LEN 1024
36
37
#define STS_NO_CONFIG 0x0 /* no configuration found */
38
#define STS_SL_FOUND 0x1 /* search list found */
39
#define STS_NS_FOUND 0x2 /* name servers found */
40
#define STS_ERROR -1 /* error return lodConfig failed memory allccation failure*/
41
42
#define IS_SL_FOUND(sts) (sts & STS_SL_FOUND)
43
#define IS_NS_FOUND(sts) (sts & STS_NS_FOUND)
44
45
/* JNI ids */
46
static jfieldID searchlistID;
47
static jfieldID nameserversID;
48
49
extern int getAdapters(JNIEnv *env, int flags, IP_ADAPTER_ADDRESSES **adapters);
50
51
/*
52
* Utility routine to append s2 to s1 with a comma delimiter.
53
* strappend(s1="abc", "def") => "abc,def"
54
* strappend(s1="", "def") => "def
55
*/
56
void strappend(char *s1, char *s2) {
57
size_t len;
58
59
if (s2[0] == '\0') /* nothing to append */
60
return;
61
62
len = strlen(s1)+1;
63
if (s1[0] != 0) /* needs comma character */
64
len++;
65
if (len + strlen(s2) > MAX_STR_LEN) /* insufficient space */
66
return;
67
68
if (s1[0] != 0) {
69
strcat(s1, ",");
70
}
71
strcat(s1, s2);
72
}
73
74
/*
75
* Use DNS server addresses returned by GetAdaptersAddresses for currently
76
* active interfaces.
77
*/
78
static int loadConfig(JNIEnv *env, char *sl, char *ns) {
79
IP_ADAPTER_ADDRESSES *adapters, *adapter;
80
IP_ADAPTER_DNS_SERVER_ADDRESS *dnsServer;
81
WCHAR *suffix;
82
DWORD ret, flags;
83
DWORD dwLen;
84
ULONG ulType;
85
char result[MAX_STR_LEN];
86
HANDLE hKey;
87
SOCKADDR *sockAddr;
88
struct sockaddr_in6 *sockAddrIpv6;
89
90
/*
91
* First see if there is a global suffix list specified.
92
*/
93
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
94
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
95
0,
96
KEY_READ,
97
(PHKEY)&hKey);
98
if (ret == ERROR_SUCCESS) {
99
dwLen = sizeof(result);
100
ret = RegQueryValueEx(hKey, "SearchList", NULL, &ulType,
101
(LPBYTE)&result, &dwLen);
102
if (ret == ERROR_SUCCESS) {
103
assert(ulType == REG_SZ);
104
if (strlen(result) > 0) {
105
strappend(sl, result);
106
}
107
}
108
RegCloseKey(hKey);
109
}
110
111
112
// We only need DNS server addresses so skip everything else.
113
flags = GAA_FLAG_SKIP_UNICAST;
114
flags |= GAA_FLAG_SKIP_ANYCAST;
115
flags |= GAA_FLAG_SKIP_MULTICAST;
116
flags |= GAA_FLAG_SKIP_FRIENDLY_NAME;
117
ret = getAdapters(env, flags, &adapters);
118
119
if (ret != ERROR_SUCCESS) {
120
return STS_ERROR;
121
}
122
123
adapter = adapters;
124
while (adapter != NULL) {
125
// Only load config from enabled adapters.
126
if (adapter->OperStatus == IfOperStatusUp) {
127
dnsServer = adapter->FirstDnsServerAddress;
128
while (dnsServer != NULL) {
129
sockAddr = dnsServer->Address.lpSockaddr;
130
if (sockAddr->sa_family == AF_INET6) {
131
sockAddrIpv6 = (struct sockaddr_in6 *)sockAddr;
132
if (sockAddrIpv6->sin6_scope_id != 0) {
133
// An address with a scope is either link-local or
134
// site-local, which aren't valid for DNS queries so
135
// we can skip them.
136
dnsServer = dnsServer->Next;
137
continue;
138
}
139
}
140
141
dwLen = sizeof(result);
142
ret = WSAAddressToStringA(sockAddr,
143
dnsServer->Address.iSockaddrLength, NULL,
144
result, &dwLen);
145
if (ret == 0) {
146
strappend(ns, result);
147
}
148
149
dnsServer = dnsServer->Next;
150
}
151
152
// Add connection-specific search domains in addition to global one.
153
suffix = adapter->DnsSuffix;
154
if (suffix != NULL) {
155
ret = WideCharToMultiByte(CP_UTF8, 0, suffix, -1,
156
result, sizeof(result), NULL, NULL);
157
if (ret != 0) {
158
strappend(sl, result);
159
}
160
}
161
}
162
163
adapter = adapter->Next;
164
}
165
166
free(adapters);
167
168
return STS_SL_FOUND & STS_NS_FOUND;
169
}
170
171
172
/*
173
* Initialize JNI field IDs and classes.
174
*/
175
JNIEXPORT void JNICALL
176
Java_sun_net_dns_ResolverConfigurationImpl_init0(JNIEnv *env, jclass cls)
177
{
178
searchlistID = (*env)->GetStaticFieldID(env, cls, "os_searchlist",
179
"Ljava/lang/String;");
180
CHECK_NULL(searchlistID);
181
nameserversID = (*env)->GetStaticFieldID(env, cls, "os_nameservers",
182
"Ljava/lang/String;");
183
}
184
185
/*
186
* Class: sun_net_dns_ResolverConfgurationImpl
187
* Method: loadConfig0
188
* Signature: ()V
189
*/
190
JNIEXPORT void JNICALL
191
Java_sun_net_dns_ResolverConfigurationImpl_loadDNSconfig0(JNIEnv *env, jclass cls)
192
{
193
char searchlist[MAX_STR_LEN];
194
char nameservers[MAX_STR_LEN];
195
jstring obj;
196
197
searchlist[0] = '\0';
198
nameservers[0] = '\0';
199
200
if (loadConfig(env, searchlist, nameservers) != STS_ERROR) {
201
202
/*
203
* Populate static fields in sun.net.DefaultResolverConfiguration
204
*/
205
obj = (*env)->NewStringUTF(env, searchlist);
206
CHECK_NULL(obj);
207
(*env)->SetStaticObjectField(env, cls, searchlistID, obj);
208
209
obj = (*env)->NewStringUTF(env, nameservers);
210
CHECK_NULL(obj);
211
(*env)->SetStaticObjectField(env, cls, nameserversID, obj);
212
}
213
}
214
215
216
/*
217
* Class: sun_net_dns_ResolverConfgurationImpl
218
* Method: notifyAddrChange0
219
* Signature: ()I
220
*/
221
JNIEXPORT jint JNICALL
222
Java_sun_net_dns_ResolverConfigurationImpl_notifyAddrChange0(JNIEnv *env, jclass cls)
223
{
224
OVERLAPPED ol;
225
HANDLE h;
226
DWORD rc, xfer;
227
228
ol.hEvent = (HANDLE)0;
229
rc = NotifyAddrChange(&h, &ol);
230
if (rc == ERROR_IO_PENDING) {
231
rc = GetOverlappedResult(h, &ol, &xfer, TRUE);
232
if (rc != 0) {
233
return 0; /* address changed */
234
}
235
}
236
237
/* error */
238
return -1;
239
}
240
241