Public Member Functions | Private Attributes

RawIpAddress Class Reference

Encapsulates arithmetics on raw IP addresses (see RawIpAddress.addr). More...

List of all members.

Public Member Functions

 RawIpAddress (RawIpAddress original)
 Creates instance from a copy of the existing IP address.
 RawIpAddress (String hostname) throws UnknownHostException
 Creates instance from the determined IP address of a host, given the host's name.
InetAddress getInetAddress () throws UnknownHostException
 Returns a new instance of InetAddress given the raw IP address.
boolean isSameVersion (RawIpAddress a2)
 Returns true if two IP addresses belongs to the same version.
void set (RawIpAddress a2)
 Sets the IP address from the value of other IP address.
void increase ()
 Increases the IP address by one.
long subtract (RawIpAddress a2)
 Returns difference of two IP addresses.
int compare (RawIpAddress a2)
 Compares two IP addresses.
String toString ()
 Returns standard decimal dotted representation of the IP address.

Private Attributes

byte[] addr = null
 Raw IP address (IPv4 or IPv6) in network byte (MSB) order where the highest order byte of the address is in addr[0].

Detailed Description

Encapsulates arithmetics on raw IP addresses (see RawIpAddress.addr).

Author:
Mikica B Kocic

Definition at line 10 of file RawIpAddress.java.


Constructor & Destructor Documentation

RawIpAddress.RawIpAddress ( RawIpAddress  original )

Creates instance from a copy of the existing IP address.

Definition at line 27 of file RawIpAddress.java.

References addr.

    {
        this.addr = new byte[ original.addr.length ];
        this.set( original );
    }
RawIpAddress.RawIpAddress ( String  hostname ) throws UnknownHostException

Creates instance from the determined IP address of a host, given the host's name.

See: InetAddress.getByName() in http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html

Exceptions:
UnknownHostExceptionif no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.

Definition at line 42 of file RawIpAddress.java.

References addr.

    {
        this.addr = InetAddress.getByName( hostname ).getAddress ();
    }

Member Function Documentation

int RawIpAddress.compare ( RawIpAddress  a2 )

Compares two IP addresses.

Parameters:
a2the second IP address
Returns:
-1 if first < second, 0 if first == second, 1 if first > second address

Definition at line 137 of file RawIpAddress.java.

References addr, and isSameVersion().

Referenced by PortScanner.getNextSocketAddress().

    {
        assert ! isSameVersion( a2 ) 
            : "We can do arithmetics only with addresses of the same IP version!";

        for ( int i = 0; i < this.addr.length; ++i )
        {
            int b1 = ( this.addr[i] + 0x100 ) & 0xFF;
            int b2 = ( a2.addr[i] + 0x100 ) & 0xFF;

            if ( b1 < b2 ) {
                return -1;
            } else if ( b1 > b2 ) {
                return +1;
            } else {
                /* equal; continue */
            }
        }
        
        return 0;
    }
InetAddress RawIpAddress.getInetAddress (  ) throws UnknownHostException

Returns a new instance of InetAddress given the raw IP address.

This method doesn't block, i.e. no reverse name service lookup is performed.

Exceptions:
UnknownHostExceptionif IP address is of illegal length

Definition at line 53 of file RawIpAddress.java.

References addr.

Referenced by PortScanner.getNextSocketAddress().

    {
        return InetAddress.getByAddress( this.addr );
    }
void RawIpAddress.increase (  )

Increases the IP address by one.

Definition at line 80 of file RawIpAddress.java.

References addr.

Referenced by PortScanner.getNextSocketAddress().

    {
        int carry = 1; // initial carry is 1 meaning increment by one
        
        for ( int i = addr.length - 1; i >= 0 && carry > 0; --i )
        {
            int sum = ( ( addr[i] + 0x100 ) & 0xFF ) + carry; // add carry to current
            carry = sum >> 8; // carry is overflow over 0x100
            addr[i] = (byte)( ( sum & 0xFF ) - 0x100 ); 
        }
    }
boolean RawIpAddress.isSameVersion ( RawIpAddress  a2 )

Returns true if two IP addresses belongs to the same version.

Definition at line 61 of file RawIpAddress.java.

References addr.

Referenced by compare(), PortScanner.main(), set(), and subtract().

    {
        return this.addr.length == a2.addr.length;
    }
void RawIpAddress.set ( RawIpAddress  a2 )

Sets the IP address from the value of other IP address.

Definition at line 69 of file RawIpAddress.java.

References isSameVersion().

Referenced by PortScanner.getNextSocketAddress().

    {
        assert ! isSameVersion( a2 ) 
            : "We can do arithmetics only with addresses of the same IP version!";

        System.arraycopy( a2.addr, 0, this.addr, 0, a2.addr.length );
    }
long RawIpAddress.subtract ( RawIpAddress  a2 )

Returns difference of two IP addresses.

Warning:
{ Caveats:

IPv6 addresses are 16 octets long, but this function returns primitive type long that is 8 octets long, so it will give wrong result if upper 8 octets of two IPv6 addresses are different.

However, the meaning of this procedure was to calculate the span of the range between two IP addresses that will be scanned, so it will actually fail if we wanted to scan more than 2^64 hosts.

Maybe it would be better to specify start IP address and count of IP addresses instead of start and stop IP addresses as input parameters? }

Parameters:
a2the second IP address

Definition at line 112 of file RawIpAddress.java.

References addr, and isSameVersion().

Referenced by PortScanner.main(), and PortScanner.run().

    {
        assert ! isSameVersion( a2 ) 
            : "We can do arithmetics only with addresses of the same IP version!";

        long delta = 0;
        
        for ( int i = 0; i < this.addr.length; ++i ) 
        {
            int b1 = ( this.addr[i] + 0x100 ) & 0xFF;
            int b2 = ( a2.addr[i] + 0x100 ) & 0xFF;

            delta <<= 8;
            delta += ( b1 - b2 );
        }

        return delta;
    }
String RawIpAddress.toString (  )

Returns standard decimal dotted representation of the IP address.

Definition at line 162 of file RawIpAddress.java.

References addr.

    {
        StringBuffer result = new StringBuffer ();

        for( int i = 0; i < this.addr.length; i++ )
        {
            if ( i != 0 ) {
                result.append( "." );
            }
            result.append( ( this.addr[i] + 0x100 ) & 0xFF );
        }

        return result.toString ();
    }

Member Data Documentation

byte [] RawIpAddress.addr = null [private]

Raw IP address (IPv4 or IPv6) in network byte (MSB) order where the highest order byte of the address is in addr[0].

IPv4 address byte array must be 4 bytes long and IPv6 byte array must be 16 bytes long.

See: InetAddress.getByAddress() in http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html

Definition at line 22 of file RawIpAddress.java.

Referenced by compare(), getInetAddress(), increase(), isSameVersion(), RawIpAddress(), subtract(), and toString().


The documentation for this class was generated from the following file: