Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / java / net / ResolverCache.java
blobad329b6075dcdc52fdaff1a804049a92b650ec1d
1 /* ResolverCache.java -- A cache of resolver lookups for InetAddress.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.net;
41 import java.security.Security;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.LinkedList;
46 /**
47 * This class provides a cache of name service resolutions. By
48 * default successful resolutions are cached forever to guard
49 * against DNS spoofing attacks and failed resolutions are cached
50 * for 10 seconds to improve performance. The length of time that
51 * results remain in the cache is determined by the following
52 * security properties:
53 * <dl>
54 * <dt><code>networkaddress.cache.ttl</code></dt>
55 * <dd>
56 * This property specifies the length of time in seconds that
57 * successful resolutions remain in the cache. The default is
58 * -1, indicating to cache forever.
59 * </dd>
60 * <dt><code>networkaddress.cache.negative.ttl</code></dt>
61 * <dd>
62 * This property specifies the length of time in seconds that
63 * unsuccessful resolutions remain in the cache. The default
64 * is 10, indicating to cache for 10 seconds.
65 * </dd>
66 * In both cases, a value of -1 indicates to cache forever and a
67 * value of 0 indicates not to cache.
69 * @author Gary Benson (gbenson@redhat.com)
71 class ResolverCache
73 /**
74 * The time in seconds for which successful lookups are cached.
76 private static final int POSITIVE_TTL =
77 getTTL("networkaddress.cache.ttl", -1);
79 /**
80 * The time in seconds for which unsuccessful lookups are cached.
82 private static final int NEGATIVE_TTL =
83 getTTL("networkaddress.cache.negative.ttl", 10);
85 /**
86 * Helper function to set the TTLs.
88 private static int getTTL(String propName, int defaultValue)
90 String propValue = Security.getProperty(propName);
91 if (propValue == null)
92 return defaultValue;
94 return Integer.parseInt(propValue);
97 /**
98 * The cache itself.
100 private static HashMap<Object, Entry> cache = new HashMap<Object, Entry>();
103 * List of entries which may expire.
105 private static LinkedList<Entry> killqueue = new LinkedList<Entry>();
108 * Return the hostname for the specified IP address.
110 * @param addr The IP address as a byte array
112 * @return The hostname
114 * @exception UnknownHostException If the reverse lookup fails
116 public static String getHostByAddr(byte[] addr) throws UnknownHostException
118 Object key = makeHashableAddress(addr);
119 Entry entry = get(key);
120 if (entry != null)
122 if (entry.value == null)
123 throw new UnknownHostException();
124 return (String) entry.value;
129 String hostname = VMInetAddress.getHostByAddr(addr);
130 put(new Entry(key, hostname));
131 return hostname;
133 catch (UnknownHostException e)
135 put(new Entry(key, null));
136 throw e;
141 * Return a list of all IP addresses for the specified hostname.
143 * @param hostname The hostname
145 * @return An list of IP addresses as byte arrays
147 * @exception UnknownHostException If the lookup fails
149 public static byte[][] getHostByName(String hostname)
150 throws UnknownHostException
152 Entry entry = get(hostname);
153 if (entry != null)
155 if (entry.value == null)
156 throw new UnknownHostException();
157 return (byte[][]) entry.value;
162 byte[][] addrs = VMInetAddress.getHostByName(hostname);
163 put(new Entry(hostname, addrs));
164 return addrs;
166 catch (UnknownHostException e)
168 put(new Entry(hostname, null));
169 throw e;
174 * Convert an IP address expressed as a byte array into something
175 * we can use as a hashtable key.
177 private static Object makeHashableAddress(byte[] addr)
179 char[] chars = new char[addr.length];
180 for (int i = 0; i < addr.length; i++)
181 chars[i] = (char) addr[i];
182 return new String(chars);
186 * Return the entry in the cache associated with the supplied key,
187 * or <code>null</code> if the cache does not contain an entry
188 * associated with this key.
190 private static synchronized Entry get(Object key)
192 reap();
193 return (Entry) cache.get(key);
197 * Insert the supplied entry into the cache.
199 private static synchronized void put(Entry entry)
201 reap();
202 if (entry.expires != 0)
204 if (entry.expires != -1)
205 killqueue.add(entry);
206 cache.put(entry.key, entry);
211 * Clear expired entries. This method is not synchronized, so
212 * it must only be called by methods that are.
214 private static void reap()
216 if (!killqueue.isEmpty())
218 long now = System.currentTimeMillis();
220 Iterator iter = killqueue.iterator();
221 while (iter.hasNext())
223 Entry entry = (Entry) iter.next();
224 if (entry.expires > now)
225 break;
226 cache.remove(entry.key);
227 iter.remove();
233 * An entry in the cache.
235 private static class Entry
238 * The key by which this entry is referenced.
240 public final Object key;
243 * The entry itself. A null value indicates a failed lookup.
245 public final Object value;
248 * The time when this cache entry expires. If set to -1 then
249 * this entry will never expire. If set to 0 then this entry
250 * expires immediately and will not be inserted into the cache.
252 public final long expires;
255 * Constructor.
257 public Entry(Object key, Object value)
259 this.key = key;
260 this.value = value;
262 int ttl = value != null ? POSITIVE_TTL : NEGATIVE_TTL;
263 if (ttl < 1)
264 expires = ttl;
265 else
266 expires = System.currentTimeMillis() + ttl * 1000;