|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.p2psockets.P2PInetAddress
This class can store and represent hostnames and IP addresses. Peer and PeerGroup IP addresses are in IPv4 format (i.e. four bytes). We generate this value by cycling over each of the bytes in the hostName string. At each stop, we add the byte value to the byte value in the 4 byte IP array, also cycling through the 4 byte IP array. None of the bytes can have the values 0, 127, or 255. To protect against this, we check to see if the generated IP address has the bytes 0, 127, or 255. If any byte is 0 or 127, we add one to it; if any byte is 255, then we subtract one from it. Todo: We also check to make sure that the address generated is not a multicast address (i.e. the first four bits are 1110). Threading: this object is considered threadsafe because its values are immutable after creation and can't be changed later. We also mediate access to the class-level variable 'localHostAddress' through synchronized getter/setters to lock if someone is changing this variable. Current Limitations: If an IP address is passed in and no host name is provided, and if a search on the P2P network doesn't resolve the IP address into a domain name, then the host name is automatically set to the IP address "stringified". Calling getHostName() on the InetAddress instance in the future will not cause the IP address to resolve again, even if that IP address is now bound to a host name. Implementation Notes: Earlier versions of P2PInetAddress (1.0 beta 1 release) had to be in the java.net package, in order to subclass java.net.InetAddress which has a package-private constructor. Because of this, when running any P2P Sockets application, users/programmers had to modify the bootclasspath before running (-Xbootclasspath/a), adding all of our JARs. This presented serious deployment and security issues. In this new release of P2PInetAddress, we are no longer in the java.net package, which means we dont have to modify the bootclasspath. Instead, after carefully looking at the InetAddress source code, it is possible to "trick" InetAddress into holding the values we need (such as the domain name and IP address) without accessing its internal DNS-based name-service, which would cause it to resolve out to a normal, non-JXTA based TCP/IP network, which is wrong. If an InetAddress is constructed where it is given the domain name, its IP address, and its canonical domain name, then it will never touch the internal DNS-based name-service. We do exactly this; all of our calls to P2PInetAddress construct the three values and use them to set InetAddress. Since there is no way to touch the canonical domain name (it is private and there are no accessor methods), we have to use reflection to access the package-private field (see http://www.onjava.com/pub/a/onjava/2003/11/12/reflection.html for an article on how we do this). Most of this work is done in the method toInetAddress(). A client first calls one of the static factory methods, such as getByAddress(); we then construct a P2PInetAddress instance, and then ask that instance to give us an InetAddress representation of itself (toInetAddress()).
Method Summary | |
static java.net.InetAddress |
anyLocalAddress()
|
protected static byte[] |
fromIPString(java.lang.String ipString)
Converts a dotted IP string into an array of 4 bytes. |
protected static byte[] |
generateIPAddress(java.lang.String hostName)
Hashes hostnames into an IPv4 format (i.e. |
protected static byte[] |
getAddress(int intAddr)
Converts the IP address as the integer intAddr to a byte array. |
static java.net.InetAddress[] |
getAllByName(java.lang.String host)
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. |
static java.net.InetAddress |
getByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address . |
static java.net.InetAddress |
getByAddress(java.lang.String host,
byte[] addr)
Create an InetAddress based on the provided host name and IP address No name service is checked for the validity of the address. |
static java.net.InetAddress |
getByName(java.lang.String host)
Determines the IP address of a host, given the host's name. |
protected static byte[] |
getIPAddress(java.lang.String hostName)
Returns the IP address for a given host name as a series of bytes. |
protected static int |
getIPAddressAsInteger(byte[] addr)
|
protected static java.lang.String |
getIPString(byte[] addr)
Converts an IP address from a series of bytes to a string. |
protected static java.lang.String |
getIPString(java.lang.String hostName)
Returns the IP address for a given host name as a formatted string. |
static java.net.InetAddress |
getLocalHost()
Returns the local host. |
protected static byte[] |
getLocalHostAddress()
This method mediates access to the static localHostAddress variable to help with thread-safety. |
protected static java.lang.String |
getLocalHostName()
Gets the local host name for this peer, such as "Brad GNUberg". |
protected static boolean |
isIPAddress(java.lang.String checkMe)
Checks to see if the given value in 'checkMe' is an IP address. |
static java.net.InetAddress |
loopbackAddress()
|
protected static void |
setLocalHostAddress(byte[] addr)
This method mediates access to the static localHostAddress variable to help with thread-safety. |
protected java.net.InetAddress |
toInetAddress()
Converts this P2PInetAddress into a java.net.InetAddress object. |
protected static java.lang.String |
toIPString(byte[] value)
Converts a series of bytes expressing an IP address into a string, such as "33.22.44.12". |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static java.net.InetAddress getByAddress(java.lang.String host, byte[] addr) throws P2PInetAddressException
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its IP
address.
IPv4 address byte array must be 4 bytes long.
host
- the specified hostaddr
- the raw IP address in network byte order
P2PInetAddressException
public static java.net.InetAddress getByName(java.lang.String host) throws java.net.UnknownHostException
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its
IP address. If a literal IP address is supplied, only the
validity of the address format is checked.
host
- the specified host, or null
for the
local host.
java.net.UnknownHostException
- if no IP address for the
host
could be found.public static java.net.InetAddress[] getAllByName(java.lang.String host) throws java.net.UnknownHostException
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its IP
address. If a literal IP address is supplied, only the
validity of the address format is checked.
host
- the name of the host.
java.net.UnknownHostException
- if no IP address for the
host
could be found.public static java.net.InetAddress getByAddress(byte[] addr) throws P2PInetAddressException
InetAddress
object given the raw IP address .
The argument is in network byte order: the highest order
byte of the address is in getAddress()[0]
.
This method doesn't block, i.e. no reverse name service lookup is performed.
IPv4 address byte array must be 4 bytes long.
addr
- the raw IP address in network byte order
java.net.UnknownHostException
- if IP address is of illegal length
P2PInetAddressException
public static java.net.InetAddress getLocalHost() throws P2PInetAddressException
java.net.UnknownHostException
- if no IP address for the
host
could be found.
P2PInetAddressException
public static java.net.InetAddress anyLocalAddress() throws P2PInetAddressException
P2PInetAddressException
public static java.net.InetAddress loopbackAddress() throws P2PInetAddressException
P2PInetAddressException
protected java.net.InetAddress toInetAddress() throws P2PInetAddressException
P2PInetAddressException
protected static byte[] getIPAddress(java.lang.String hostName)
protected static java.lang.String getIPString(java.lang.String hostName)
protected static java.lang.String getIPString(byte[] addr)
protected static byte[] generateIPAddress(java.lang.String hostName)
protected static java.lang.String toIPString(byte[] value)
protected static byte[] fromIPString(java.lang.String ipString)
protected static boolean isIPAddress(java.lang.String checkMe)
protected static int getIPAddressAsInteger(byte[] addr)
protected static java.lang.String getLocalHostName()
protected static byte[] getLocalHostAddress()
protected static void setLocalHostAddress(byte[] addr)
protected static byte[] getAddress(int intAddr)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |