Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/InetAddressCachePolicy.java
41152 views
1
/*
2
* Copyright (c) 1998, 2021, 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
package sun.net;
27
28
import java.security.PrivilegedAction;
29
import java.security.Security;
30
31
@SuppressWarnings("removal")
32
public final class InetAddressCachePolicy {
33
34
// Controls the cache policy for successful lookups only
35
private static final String cachePolicyProp = "networkaddress.cache.ttl";
36
private static final String cachePolicyPropFallback =
37
"sun.net.inetaddr.ttl";
38
39
// Controls the cache policy for negative lookups only
40
private static final String negativeCachePolicyProp =
41
"networkaddress.cache.negative.ttl";
42
private static final String negativeCachePolicyPropFallback =
43
"sun.net.inetaddr.negative.ttl";
44
45
public static final int FOREVER = -1;
46
public static final int NEVER = 0;
47
48
/* default value for positive lookups */
49
public static final int DEFAULT_POSITIVE = 30;
50
51
/* The Java-level namelookup cache policy for successful lookups:
52
*
53
* -1: caching forever
54
* any positive value: the number of seconds to cache an address for
55
*
56
* default value is forever (FOREVER), as we let the platform do the
57
* caching. For security reasons, this caching is made forever when
58
* a security manager is set.
59
*/
60
private static volatile int cachePolicy = FOREVER;
61
62
/* The Java-level namelookup cache policy for negative lookups:
63
*
64
* -1: caching forever
65
* any positive value: the number of seconds to cache an address for
66
*
67
* default value is 0. It can be set to some other value for
68
* performance reasons.
69
*/
70
private static volatile int negativeCachePolicy = NEVER;
71
72
/*
73
* Whether or not the cache policy for successful lookups was set
74
* using a property (cmd line).
75
*/
76
private static boolean propertySet;
77
78
/*
79
* Whether or not the cache policy for negative lookups was set
80
* using a property (cmd line).
81
*/
82
private static boolean propertyNegativeSet;
83
84
/*
85
* Initialize
86
*/
87
static {
88
89
Integer tmp = java.security.AccessController.doPrivileged(
90
new PrivilegedAction<Integer>() {
91
public Integer run() {
92
try {
93
String tmpString = Security.getProperty(cachePolicyProp);
94
if (tmpString != null) {
95
return Integer.valueOf(tmpString);
96
}
97
} catch (NumberFormatException ignored) {
98
// Ignore
99
}
100
101
try {
102
String tmpString = System.getProperty(cachePolicyPropFallback);
103
if (tmpString != null) {
104
return Integer.decode(tmpString);
105
}
106
} catch (NumberFormatException ignored) {
107
// Ignore
108
}
109
return null;
110
}
111
});
112
113
if (tmp != null) {
114
cachePolicy = tmp < 0 ? FOREVER : tmp;
115
propertySet = true;
116
} else {
117
/* No properties defined for positive caching. If there is no
118
* security manager then use the default positive cache value.
119
*/
120
if (System.getSecurityManager() == null) {
121
cachePolicy = DEFAULT_POSITIVE;
122
}
123
}
124
tmp = java.security.AccessController.doPrivileged (
125
new PrivilegedAction<Integer>() {
126
public Integer run() {
127
try {
128
String tmpString = Security.getProperty(negativeCachePolicyProp);
129
if (tmpString != null) {
130
return Integer.valueOf(tmpString);
131
}
132
} catch (NumberFormatException ignored) {
133
// Ignore
134
}
135
136
try {
137
String tmpString = System.getProperty(negativeCachePolicyPropFallback);
138
if (tmpString != null) {
139
return Integer.decode(tmpString);
140
}
141
} catch (NumberFormatException ignored) {
142
// Ignore
143
}
144
return null;
145
}
146
});
147
148
if (tmp != null) {
149
negativeCachePolicy = tmp < 0 ? FOREVER : tmp;
150
propertyNegativeSet = true;
151
}
152
}
153
154
public static int get() {
155
return cachePolicy;
156
}
157
158
public static int getNegative() {
159
return negativeCachePolicy;
160
}
161
162
/**
163
* Sets the cache policy for successful lookups if the user has not
164
* already specified a cache policy for it using a
165
* command-property.
166
* @param newPolicy the value in seconds for how long the lookup
167
* should be cached
168
*/
169
public static synchronized void setIfNotSet(int newPolicy) {
170
/*
171
* When setting the new value we may want to signal that the
172
* cache should be flushed, though this doesn't seem strictly
173
* necessary.
174
*/
175
if (!propertySet) {
176
checkValue(newPolicy, cachePolicy);
177
cachePolicy = newPolicy;
178
}
179
}
180
181
/**
182
* Sets the cache policy for negative lookups if the user has not
183
* already specified a cache policy for it using a
184
* command-property.
185
* @param newPolicy the value in seconds for how long the lookup
186
* should be cached
187
*/
188
public static void setNegativeIfNotSet(int newPolicy) {
189
/*
190
* When setting the new value we may want to signal that the
191
* cache should be flushed, though this doesn't seem strictly
192
* necessary.
193
*/
194
if (!propertyNegativeSet) {
195
// Negative caching does not seem to have any security
196
// implications.
197
// checkValue(newPolicy, negativeCachePolicy);
198
// but we should normalize negative policy
199
negativeCachePolicy = newPolicy < 0 ? FOREVER : newPolicy;
200
}
201
}
202
203
private static void checkValue(int newPolicy, int oldPolicy) {
204
/*
205
* If malicious code gets a hold of this method, prevent
206
* setting the cache policy to something laxer or some
207
* invalid negative value.
208
*/
209
if (newPolicy == FOREVER)
210
return;
211
212
if ((oldPolicy == FOREVER) ||
213
(newPolicy < oldPolicy) ||
214
(newPolicy < FOREVER)) {
215
216
throw new
217
SecurityException("can't make InetAddress cache more lax");
218
}
219
}
220
}
221
222