org.p2psockets
Class P2PInetAddress

java.lang.Object
  extended byorg.p2psockets.P2PInetAddress

public class P2PInetAddress
extends java.lang.Object

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()).

Version:
0.5 - Updated to not need subclassing of java.net.InetAddress
Author:
Brad GNUberg, bkn3@columbia.edu

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

getByAddress

public static java.net.InetAddress getByAddress(java.lang.String host,
                                                byte[] addr)
                                         throws P2PInetAddressException
Create an InetAddress based on the provided host name and IP address No name service is checked for the validity of the address.

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.

Parameters:
host - the specified host
addr - the raw IP address in network byte order
Returns:
an InetAddress object created from the raw IP address and hostname.
Throws:
P2PInetAddressException

getByName

public static java.net.InetAddress getByName(java.lang.String host)
                                      throws java.net.UnknownHostException
Determines the IP address of a host, given the host's name.

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.

Parameters:
host - the specified host, or null for the local host.
Returns:
an IP address for the given host name.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.

getAllByName

public static java.net.InetAddress[] getAllByName(java.lang.String host)
                                           throws java.net.UnknownHostException
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.

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.

Parameters:
host - the name of the host.
Returns:
an array of all the IP addresses for a given host name.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.

getByAddress

public static java.net.InetAddress getByAddress(byte[] addr)
                                         throws P2PInetAddressException
Returns an 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.

Parameters:
addr - the raw IP address in network byte order
Returns:
an InetAddress object created from the raw IP address.
Throws:
java.net.UnknownHostException - if IP address is of illegal length
P2PInetAddressException

getLocalHost

public static java.net.InetAddress getLocalHost()
                                         throws P2PInetAddressException
Returns the local host.

Returns:
the IP address of the local host.
Throws:
java.net.UnknownHostException - if no IP address for the host could be found.
P2PInetAddressException

anyLocalAddress

public static java.net.InetAddress anyLocalAddress()
                                            throws P2PInetAddressException
Throws:
P2PInetAddressException

loopbackAddress

public static java.net.InetAddress loopbackAddress()
                                            throws P2PInetAddressException
Throws:
P2PInetAddressException

toInetAddress

protected java.net.InetAddress toInetAddress()
                                      throws P2PInetAddressException
Converts this P2PInetAddress into a java.net.InetAddress object.

Throws:
P2PInetAddressException

getIPAddress

protected static byte[] getIPAddress(java.lang.String hostName)
Returns the IP address for a given host name as a series of bytes. If host name is null, the it is assumed that the "any"/wildcard interface is being requested (0.0.0.0). We resolve this to being the host name and IP address for the localhost. The same thing is returned for the loop back interface (i.e. the host "localhost" or the address "127.0.0.1").


getIPString

protected static java.lang.String getIPString(java.lang.String hostName)
Returns the IP address for a given host name as a formatted string. If host name is null, the it is assumed that the "any"/wildcard interface is being requested (0.0.0.0). We resolve this to being the host name and IP address for the localhost. The same thing is returned for the loop back interface (i.e. the host "localhost" or the address "127.0.0.1").


getIPString

protected static java.lang.String getIPString(byte[] addr)
Converts an IP address from a series of bytes to a string.


generateIPAddress

protected static byte[] generateIPAddress(java.lang.String hostName)
Hashes hostnames into an 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 protected 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.


toIPString

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".


fromIPString

protected static byte[] fromIPString(java.lang.String ipString)
Converts a dotted IP string into an array of 4 bytes.


isIPAddress

protected static boolean isIPAddress(java.lang.String checkMe)
Checks to see if the given value in 'checkMe' is an IP address.


getIPAddressAsInteger

protected static int getIPAddressAsInteger(byte[] addr)

getLocalHostName

protected static java.lang.String getLocalHostName()
Gets the local host name for this peer, such as "Brad GNUberg".


getLocalHostAddress

protected static byte[] getLocalHostAddress()
This method mediates access to the static localHostAddress variable to help with thread-safety.


setLocalHostAddress

protected static void setLocalHostAddress(byte[] addr)
This method mediates access to the static localHostAddress variable to help with thread-safety.


getAddress

protected static byte[] getAddress(int intAddr)
Converts the IP address as the integer intAddr to a byte array.