Encapsulates arithmetics on raw IP addresses (see RawIpAddress.addr). More...
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]. |
Encapsulates arithmetics on raw IP addresses (see RawIpAddress.addr).
Definition at line 10 of file RawIpAddress.java.
RawIpAddress.RawIpAddress | ( | RawIpAddress | original ) |
Creates instance from a copy of the existing IP address.
Definition at line 27 of file RawIpAddress.java.
References addr.
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
UnknownHostException | if 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 (); }
int RawIpAddress.compare | ( | RawIpAddress | a2 ) |
Compares two IP addresses.
a2 | the second IP 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.
UnknownHostException | if 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().
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().
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.
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? }
a2 | the 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.
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().