IPAddress.java
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.onlab.packet;
import java.util.Arrays;
/**
* A class representing an IPv4 address.
*/
public class IPAddress {
//IP Versions
public enum Version { INET, INET6 };
//lengths of addresses, in bytes
public static final int INET_LEN = 4;
public static final int INET6_LEN = 6;
protected Version version;
//does it make more sense to have a integral address?
protected byte[] octets;
protected IPAddress(Version ver, byte[] octets) {
this.version = ver;
this.octets = Arrays.copyOf(octets, INET_LEN);
}
/**
* Converts a byte array into an IP address.
*
* @param address a byte array
* @return an IP address
*/
public static IPAddress valueOf(byte [] address) {
return new IPAddress(Version.INET, address);
}
/**
* Converts an integer into an IPv4 address.
*
* @param address an integer representing an IP value
* @return an IP address
*/
public static IPAddress valueOf(int address) {
byte [] bytes = new byte [] {
(byte) ((address >> 24) & 0xff),
(byte) ((address >> 16) & 0xff),
(byte) ((address >> 8) & 0xff),
(byte) ((address >> 0) & 0xff)
};
return new IPAddress(Version.INET, bytes);
}
/**
* Converts a string in dotted-decimal notation (x.x.x.x) into
* an IPv4 address.
*
* @param address a string representing an IP address, e.g. "10.0.0.1"
* @return an IP address
*/
public static IPAddress valueOf(String address) {
final String [] parts = address.split(".");
if (parts.length != INET_LEN) {
throw new IllegalArgumentException("Malformed IP address string; "
+ "Addres must have four decimal values separated by dots (.)");
}
final byte [] bytes = new byte[INET_LEN];
for (int i = 0; i < INET_LEN; i++) {
bytes[i] = Byte.parseByte(parts[i], 10);
}
return new IPAddress(Version.INET, bytes);
}
/**
* Returns the IP version of this address.
*
* @return the version
*/
public Version version() {
return this.version;
}
/**
* Returns the IP address as a byte array.
*
* @return a byte array
*/
public byte [] toOctets() {
return Arrays.copyOf(this.octets, INET_LEN);
}
public int toInt() {
int address =
((octets[0] << 24) |
(octets[1] << 16) |
(octets[2] << 8) |
(octets[3] << 0));
return address;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for (final byte b : this.octets) {
if (builder.length() > 0) {
builder.append(".");
}
builder.append(String.format("%02d", b));
}
return builder.toString();
}
@Override
public int hashCode() {
return octets.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof IPAddress) {
IPAddress other = (IPAddress) obj;
if (!(this.version.equals(other.version))) {
return false;
}
if (!(Arrays.equals(this.octets, other.octets))) {
return false;
}
}
return true;
}
}