1 |
/* InetAddress.java -- Class to model an Internet address |
/* InetAddress.java -- Class to model an Internet address |
2 |
Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2002, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package java.net; |
package java.net; |
40 |
|
|
41 |
import gnu.classpath.Configuration; |
import gnu.classpath.Configuration; |
42 |
|
|
43 |
import java.io.IOException; |
import java.io.IOException; |
44 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
45 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
48 |
import java.util.HashMap; |
import java.util.HashMap; |
49 |
import java.util.StringTokenizer; |
import java.util.StringTokenizer; |
50 |
|
|
|
|
|
51 |
/** |
/** |
52 |
* This class models an Internet address. It does not have a public |
* This class models an Internet address. It does not have a public |
53 |
* constructor. Instead, new instances of this objects are created |
* constructor. Instead, new instances of this objects are created |
54 |
* using the static methods getLocalHost(), getByName(), and |
* using the static methods getLocalHost(), getByName(), and |
55 |
* getAllByName(). |
* getAllByName(). |
56 |
* <p> |
* |
57 |
* This class fulfills the function of the C style functions gethostname(), |
* <p>This class fulfills the function of the C style functions gethostname(), |
58 |
* gethostbyname(), and gethostbyaddr(). It resolves Internet DNS names |
* gethostbyname(), and gethostbyaddr(). It resolves Internet DNS names |
59 |
* into their corresponding numeric addresses and vice versa. |
* into their corresponding numeric addresses and vice versa.</p> |
60 |
* |
* |
61 |
* @author Aaron M. Renn <arenn@urbanophile.com> |
* @author Aaron M. Renn (arenn@urbanophile.com) |
62 |
* @author Per Bothner |
* @author Per Bothner |
63 |
* |
* |
64 |
* @specnote This class is not final since JK 1.4 |
* @specnote This class is not final since JK 1.4 |
76 |
/** |
/** |
77 |
* The default caching period in minutes. |
* The default caching period in minutes. |
78 |
*/ |
*/ |
79 |
private static final int DEFAULT_CACHE_PERIOD = (4 * 60); |
private static final int DEFAULT_CACHE_PERIOD = 4 * 60; |
80 |
|
|
81 |
/** |
/** |
82 |
* Percentage of cache entries to purge when the table gets full. |
* Percentage of cache entries to purge when the table gets full. |
116 |
private static HashMap cache; |
private static HashMap cache; |
117 |
|
|
118 |
static |
static |
119 |
{ |
{ |
120 |
// load the shared library needed for name resolution |
// load the shared library needed for name resolution |
121 |
if (Configuration.INIT_LOAD_LIBRARY) |
if (Configuration.INIT_LOAD_LIBRARY) |
122 |
System.loadLibrary("javanet"); |
System.loadLibrary("javanet"); |
123 |
|
|
124 |
// Look for properties that override default caching behavior |
// Look for properties that override default caching behavior |
125 |
cache_size = |
cache_size = |
126 |
Integer.getInteger("gnu.java.net.dns_cache_size", DEFAULT_CACHE_SIZE) |
Integer.getInteger("gnu.java.net.dns_cache_size", DEFAULT_CACHE_SIZE) |
127 |
.intValue(); |
.intValue(); |
128 |
cache_period = |
cache_period = |
129 |
Integer.getInteger("gnu.java.net.dns_cache_period", |
Integer.getInteger("gnu.java.net.dns_cache_period", |
130 |
DEFAULT_CACHE_PERIOD * 60 * 1000).intValue(); |
DEFAULT_CACHE_PERIOD * 60 * 1000).intValue(); |
131 |
|
|
132 |
cache_purge_pct = |
cache_purge_pct = |
133 |
Integer.getInteger("gnu.java.net.dns_cache_purge_pct", |
Integer.getInteger("gnu.java.net.dns_cache_purge_pct", |
134 |
DEFAULT_CACHE_PURGE_PCT).intValue(); |
DEFAULT_CACHE_PURGE_PCT).intValue(); |
135 |
|
|
136 |
// Fallback to defaults if necessary |
// Fallback to defaults if necessary |
137 |
if ((cache_purge_pct < 1) || (cache_purge_pct > 100)) |
if ((cache_purge_pct < 1) || (cache_purge_pct > 100)) |
138 |
cache_purge_pct = DEFAULT_CACHE_PURGE_PCT; |
cache_purge_pct = DEFAULT_CACHE_PURGE_PCT; |
139 |
|
|
140 |
// Create the cache |
// Create the cache |
141 |
if (cache_size != 0) |
if (cache_size != 0) |
142 |
cache = new HashMap(cache_size); |
cache = new HashMap(cache_size); |
143 |
|
|
144 |
// precompute the ANY_IF address |
// precompute the ANY_IF address |
145 |
try |
try |
146 |
{ |
{ |
147 |
ANY_IF = getInaddrAny(); |
ANY_IF = getInaddrAny(); |
148 |
} |
} |
149 |
catch (UnknownHostException uhe) |
catch (UnknownHostException uhe) |
150 |
{ |
{ |
151 |
// Hmmm, make one up and hope that it works. |
// Hmmm, make one up and hope that it works. |
152 |
byte[] zeros = { 0, 0, 0, 0 }; |
byte[] zeros = { 0, 0, 0, 0 }; |
153 |
ANY_IF = new Inet4Address(zeros); |
ANY_IF = new Inet4Address(zeros); |
154 |
} |
} |
155 |
} |
} |
156 |
|
|
157 |
/** |
/** |
158 |
* The Serialized Form specifies that an int 'address' is saved/restored. |
* The Serialized Form specifies that an int 'address' is saved/restored. |
213 |
|
|
214 |
family = 2; /* AF_INET */ |
family = 2; /* AF_INET */ |
215 |
address = addr[3] & 0xff; |
address = addr[3] & 0xff; |
216 |
address |= ((addr[2] << 8) & 0xff00); |
address |= (addr[2] << 8) & 0xff00; |
217 |
address |= ((addr[1] << 16) & 0xff0000); |
address |= (addr[1] << 16) & 0xff0000; |
218 |
address |= ((addr[0] << 24) & 0xff000000); |
address |= (addr[0] << 24) & 0xff000000; |
219 |
} |
} |
220 |
|
|
221 |
/** |
/** |
417 |
{ |
{ |
418 |
StringBuffer sb = new StringBuffer(40); |
StringBuffer sb = new StringBuffer(40); |
419 |
|
|
420 |
for (int i = 0; i < addr.length; i++) |
int len = addr.length; |
421 |
|
int i = 0; |
422 |
|
|
423 |
|
for ( ; ; ) |
424 |
{ |
{ |
425 |
sb.append(addr[i] & 0xff); |
sb.append(addr[i] & 0xff); |
426 |
|
i++; |
427 |
if (i < (addr.length - 1)) |
|
428 |
sb.append("."); |
if (i == len) |
429 |
|
break; |
430 |
|
|
431 |
|
sb.append('.'); |
432 |
} |
} |
433 |
|
|
434 |
return sb.toString(); |
return sb.toString(); |
595 |
* default. This method is equivalent to returning the first element in |
* default. This method is equivalent to returning the first element in |
596 |
* the InetAddress array returned from GetAllByName. |
* the InetAddress array returned from GetAllByName. |
597 |
* |
* |
598 |
* @param hostname The name of the desired host, or null for the local machine. |
* @param hostname The name of the desired host, or null for the local |
599 |
|
* loopback address. |
600 |
* |
* |
601 |
* @return The address of the host as an InetAddress object. |
* @return The address of the host as an InetAddress object. |
602 |
* |
* |
634 |
* hostname of the local machine is supplied by default. |
* hostname of the local machine is supplied by default. |
635 |
* |
* |
636 |
* @param hostname The name of the desired host, or null for the |
* @param hostname The name of the desired host, or null for the |
637 |
* local machine. |
* local loopback address. |
638 |
* |
* |
639 |
* @return All addresses of the host as an array of InetAddress objects. |
* @return All addresses of the host as an array of InetAddress objects. |
640 |
* |
* |
701 |
InetAddress[] addresses = null; |
InetAddress[] addresses = null; |
702 |
|
|
703 |
if (cache_size == 0) |
if (cache_size == 0) |
704 |
return (null); |
return null; |
705 |
|
|
706 |
Object obj = cache.get(hostname); |
Object obj = cache.get(hostname); |
707 |
if (obj == null) |
if (obj == null) |
708 |
return (null); |
return null; |
709 |
|
|
710 |
if (obj instanceof InetAddress[]) |
if (obj instanceof InetAddress[]) |
711 |
addresses = (InetAddress[]) obj; |
addresses = (InetAddress[]) obj; |
712 |
|
|
713 |
if (addresses == null) |
if (addresses == null) |
714 |
return (null); |
return null; |
715 |
|
|
716 |
if (cache_period != -1) |
if (cache_period != -1) |
717 |
if ((System.currentTimeMillis() - addresses[0].lookup_time) > cache_period) |
if ((System.currentTimeMillis() - addresses[0].lookup_time) > cache_period) |
718 |
{ |
{ |
719 |
cache.remove(hostname); |
cache.remove(hostname); |
720 |
return (null); |
return null; |
721 |
} |
} |
722 |
|
|
723 |
return addresses; |
return addresses; |