3 import java
.net
.InetAddress
;
4 import java
.net
.UnknownHostException
;
7 * This is a wrapper class for InetAddress, which allows the setting of
8 * an InetAddress with a byte arrays. As well as other useful functions.
10 * @author Anthony Molinaro
12 public class IPAddress
implements java
.io
.Serializable
{
13 /* serializable UID */
14 static final long serialVersionUID
= -1;
16 /* inet address in network byte order */
17 private byte[] inet_addr
;
18 private InetAddress java_inet_addr
;
24 inet_addr
= new byte[4];
25 inet_addr
[0] = (byte) 0;
26 inet_addr
[1] = (byte) 0;
27 inet_addr
[2] = (byte) 0;
28 inet_addr
[3] = (byte) 0;
29 java_inet_addr
= null;
33 * Construct an IPAddress object with a string of the form
34 * "%d.%d.%d.%d" representing an InetAddress.
35 * The address defaults to 0.0.0.0 if an invalid address is
38 * @param anIPAddress the string representing the IPAddress
40 public IPAddress(String anIPAddress
) {
42 java_inet_addr
= InetAddress
.getByName(anIPAddress
);
43 inet_addr
= java_inet_addr
.getAddress();
45 catch (UnknownHostException e
) {
46 java_inet_addr
= null;
47 inet_addr
= new byte[4];
48 inet_addr
[0] = (byte) 0;
49 inet_addr
[1] = (byte) 0;
50 inet_addr
[2] = (byte) 0;
51 inet_addr
[3] = (byte) 0;
56 * Construct an IPAddress object with a byte array containing
57 * the InetAddress in little-endian notation.
59 * @param anIPAddress the byte array representing the IPAddress
61 public IPAddress(byte[] anIPAddress
) {
62 if (anIPAddress
.length
!= 4) {
63 System
.err
.println("ERROR : bad inet addr\n");
64 throw new RuntimeException("Bad inet address");
66 inet_addr
= anIPAddress
;
70 * Construct an IPAddress object with an InetAddress.
72 * @param anIPAddress the InetAddress to set this IPAddress to
74 public IPAddress(InetAddress anIPAddress
) {
75 java_inet_addr
= anIPAddress
;
76 inet_addr
= java_inet_addr
.getAddress();
80 * Get the InetAddress representing this IPAddress
82 * @return the InetAddress representing this IPAddress
84 public InetAddress
toInetAddress() {
85 if (java_inet_addr
!= null) {
86 return java_inet_addr
;
90 return InetAddress
.getByName(toString());
92 catch (UnknownHostException e
) {
99 * Return the IPAddress in a byte array in network order
101 * @return a byte array containing the IPAddress in network order
103 public byte[] getInetAddressAsBytes() {
108 * Return the IPAddress in as an integer
110 * @return an integer representing the IPAddress in network order
113 return (int) ((inet_addr
[0] << 24)
114 | (inet_addr
[1] << 16)
115 | (inet_addr
[2] << 8)
116 | (inet_addr
[3] << 0));
120 * return a string representation of an IPAddress in the following
121 * format "%d.%d.%d.%d".
123 * @return a string representation of an IPAddress.
125 public String
toString() {
126 return (((int) (inet_addr
[0]) & 0xff)
127 + "." + ((int) (inet_addr
[1]) & 0xff)
128 + "." + ((int) (inet_addr
[2]) & 0xff)
129 + "." + ((int) (inet_addr
[3]) & 0xff));
133 * return the size that an IPAddress takes up in a serialized byte array
135 public int bytesStoreSize() {
139 public boolean equals(Object o
) {
140 if (!(o
instanceof IPAddress
)) {
141 /* "o" is not an IPAddress object (note: it may be null). */
144 final IPAddress other
= (IPAddress
) o
;
145 final byte[] theseBytes
= this.getInetAddressAsBytes();
146 final byte[] otherBytes
= other
.getInetAddressAsBytes();
147 if (theseBytes
.length
!= otherBytes
.length
) {
148 /* same length or else not equal */
151 for (int i
= 0; i
< theseBytes
.length
; ++i
) {
152 if (theseBytes
[i
] != otherBytes
[i
]) {
153 /* found a difference; not equal. */
157 return true; /* passed all tests; return true */
160 public int hashCode() {
162 final byte[] theseBytes
= this.getInetAddressAsBytes();
163 for (int i
= 0; i
< theseBytes
.length
; ++i
) {
164 hash ^
= theseBytes
[i
];