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