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 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 8 | public class IpAddress { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 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 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 21 | protected IpAddress(Version ver, byte[] octets) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 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 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 32 | public static IpAddress valueOf(byte [] address) { |
| 33 | return new IpAddress(Version.INET, address); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 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 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 42 | public static IpAddress valueOf(int address) { |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 43 | byte [] bytes = new byte [INET_LEN]; |
| 44 | for (int i = 0; i < INET_LEN; i++) { |
| 45 | bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); |
| 46 | } |
| 47 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 48 | return new IpAddress(Version.INET, bytes); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Converts a string in dotted-decimal notation (x.x.x.x) into |
| 53 | * an IPv4 address. |
| 54 | * |
| 55 | * @param address a string representing an IP address, e.g. "10.0.0.1" |
| 56 | * @return an IP address |
| 57 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 58 | public static IpAddress valueOf(String address) { |
Ayaka Koshibe | 50ee924 | 2014-09-12 16:37:46 -0700 | [diff] [blame] | 59 | final String [] parts = address.split("\\."); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 60 | if (parts.length != INET_LEN) { |
| 61 | throw new IllegalArgumentException("Malformed IP address string; " |
| 62 | + "Addres must have four decimal values separated by dots (.)"); |
| 63 | } |
| 64 | final byte [] bytes = new byte[INET_LEN]; |
| 65 | for (int i = 0; i < INET_LEN; i++) { |
| 66 | bytes[i] = Byte.parseByte(parts[i], 10); |
| 67 | } |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 68 | return new IpAddress(Version.INET, bytes); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the IP version of this address. |
| 73 | * |
| 74 | * @return the version |
| 75 | */ |
| 76 | public Version version() { |
| 77 | return this.version; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the IP address as a byte array. |
| 82 | * |
| 83 | * @return a byte array |
| 84 | */ |
| 85 | public byte [] toOctets() { |
| 86 | return Arrays.copyOf(this.octets, INET_LEN); |
| 87 | } |
| 88 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Returns the integral value of this IP address. |
| 91 | * |
| 92 | * @return the IP address's value as an integer |
| 93 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 94 | public int toInt() { |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 95 | int address = 0; |
| 96 | for (int i = 0; i < INET_LEN; i++) { |
| 97 | address |= octets[i] << ((INET_LEN - (i + 1)) * 8); |
| 98 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 99 | return address; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | final StringBuilder builder = new StringBuilder(); |
| 105 | for (final byte b : this.octets) { |
| 106 | if (builder.length() > 0) { |
| 107 | builder.append("."); |
| 108 | } |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 109 | builder.append(String.format("%d", b)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 110 | } |
| 111 | return builder.toString(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 116 | return Arrays.hashCode(octets); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
Ayaka Koshibe | 50ee924 | 2014-09-12 16:37:46 -0700 | [diff] [blame] | 121 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame^] | 122 | if (obj instanceof IpAddress) { |
| 123 | IpAddress other = (IpAddress) obj; |
Ayaka Koshibe | 50ee924 | 2014-09-12 16:37:46 -0700 | [diff] [blame] | 124 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 125 | if (this.version.equals(other.version) |
| 126 | && (Arrays.equals(this.octets, other.octets))) { |
| 127 | return true; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 130 | return false; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 131 | } |
| 132 | } |