blob: 7a71f7d560153fd2f4487e40f85c40da0ab51b08 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.net;
27
28import java.net.InetAddress;
29import java.security.PrivilegedAction;
30import java.security.Security;
31
32public 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 int cachePolicy;
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 int negativeCachePolicy;
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 set = false;
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 negativeSet = false;
83
84 /*
85 * Initialize
86 */
87 static {
88
89 set = false;
90 negativeSet = false;
91
92 cachePolicy = FOREVER;
93 negativeCachePolicy = 0;
94
95 Integer tmp = null;
96
97 try {
98 tmp = new Integer(
99 java.security.AccessController.doPrivileged (
100 new PrivilegedAction<String>() {
101 public String run() {
102 return Security.getProperty(cachePolicyProp);
103 }
104 }));
105 } catch (NumberFormatException e) {
106 // ignore
107 }
108 if (tmp != null) {
109 cachePolicy = tmp.intValue();
110 if (cachePolicy < 0) {
111 cachePolicy = FOREVER;
112 }
113 set = true;
114 } else {
115 tmp = java.security.AccessController.doPrivileged
116 (new sun.security.action.GetIntegerAction(cachePolicyPropFallback));
117 if (tmp != null) {
118 cachePolicy = tmp.intValue();
119 if (cachePolicy < 0) {
120 cachePolicy = FOREVER;
121 }
122 set = true;
123 }
124 }
125
126 try {
127 tmp = new Integer(
128 java.security.AccessController.doPrivileged (
129 new PrivilegedAction<String>() {
130 public String run() {
131 return Security.getProperty(negativeCachePolicyProp);
132 }
133 }));
134 } catch (NumberFormatException e) {
135 // ignore
136 }
137
138 if (tmp != null) {
139 negativeCachePolicy = tmp.intValue();
140 if (negativeCachePolicy < 0) {
141 negativeCachePolicy = FOREVER;
142 }
143 negativeSet = true;
144 } else {
145 tmp = java.security.AccessController.doPrivileged
146 (new sun.security.action.GetIntegerAction(negativeCachePolicyPropFallback));
147 if (tmp != null) {
148 negativeCachePolicy = tmp.intValue();
149 if (negativeCachePolicy < 0) {
150 negativeCachePolicy = FOREVER;
151 }
152 negativeSet = true;
153 }
154 }
155 }
156
157 public static synchronized int get() {
158 if (!set && System.getSecurityManager() == null) {
159 return DEFAULT_POSITIVE;
160 } else {
161 return cachePolicy;
162 }
163 }
164
165 public static synchronized int getNegative() {
166 return negativeCachePolicy;
167 }
168
169 /**
170 * Sets the cache policy for successful lookups if the user has not
171 * already specified a cache policy for it using a
172 * command-property.
173 * @param newPolicy the value in seconds for how long the lookup
174 * should be cached
175 */
176 public static synchronized void setIfNotSet(int newPolicy) {
177
178 /*
179 * When setting the new value we may want to signal that the
180 * cache should be flushed, though this doesn't seem strictly
181 * necessary.
182 */
183
184 if (!set) {
185 checkValue(newPolicy, cachePolicy);
186 cachePolicy = newPolicy;
187 }
188
189 }
190
191
192 /**
193 * Sets the cache policy for negative lookups if the user has not
194 * already specified a cache policy for it using a
195 * command-property.
196 * @param newPolicy the value in seconds for how long the lookup
197 * should be cached
198 */
199 public static synchronized void setNegativeIfNotSet(int newPolicy) {
200
201 /*
202 * When setting the new value we may want to signal that the
203 * cache should be flushed, though this doesn't seem strictly
204 * necessary.
205 */
206
207 if (!negativeSet) {
208 // Negative caching does not seem to have any security
209 // implications.
210 // checkValue(newPolicy, negativeCachePolicy);
211 negativeCachePolicy = newPolicy;
212 }
213 }
214
215 private static void checkValue(int newPolicy, int oldPolicy) {
216
217 /*
218 * If malicious code gets a hold of this method, prevent
219 * setting the cache policy to something laxer or some
220 * invalid negative value.
221 */
222
223 if (newPolicy == FOREVER)
224 return;
225
226 if ((oldPolicy == FOREVER) ||
227 (newPolicy < oldPolicy) ||
228 (newPolicy < FOREVER)) {
229
230 throw new
231 SecurityException("can't make InetAddress cache more lax");
232
233 }
234 }
235}