Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 18 | import java.nio.ByteBuffer; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
| 22 | * A class representing an IPv4 address. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 23 | */ |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 24 | public final class IpAddress implements Comparable<IpAddress> { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 25 | // IP Versions |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 26 | public enum Version { INET, INET6 }; |
| 27 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 28 | // lengths of address, in bytes |
| 29 | public static final int INET_BYTE_LENGTH = 4; |
| 30 | public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE; |
| 31 | public static final int INET6_BYTE_LENGTH = 16; |
| 32 | public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 33 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 34 | private final Version version; |
| 35 | private final byte[] octets; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 38 | * Constructor for given IP address version and address octets. |
| 39 | * |
| 40 | * @param ver the IP address version |
| 41 | * @param octets the IP address octets |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 42 | */ |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 43 | private IpAddress(Version ver, byte[] octets) { |
| 44 | this.version = ver; |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 45 | this.octets = Arrays.copyOf(octets, INET_BYTE_LENGTH); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Converts a byte array into an IP address. |
| 50 | * |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 51 | * @param address the IP address value stored in network byte order |
| 52 | * (i.e., the most significant byte first) |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 53 | * @return an IP address |
| 54 | */ |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 55 | public static IpAddress valueOf(byte[] address) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 56 | return new IpAddress(Version.INET, address); |
| 57 | } |
| 58 | |
| 59 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 60 | * Converts an integer into an IPv4 address. |
| 61 | * |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 62 | * @param address an integer representing an IPv4 value |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 63 | * @return an IP address |
| 64 | */ |
| 65 | public static IpAddress valueOf(int address) { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 66 | byte[] bytes = |
| 67 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(address).array(); |
| 68 | return new IpAddress(Version.INET, bytes); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 72 | * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 73 | * |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 74 | * @param address a IP address in string form, e.g. "10.0.0.1". |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 75 | * @return an IP address |
| 76 | */ |
| 77 | public static IpAddress valueOf(String address) { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 78 | final String[] net = address.split("\\."); |
| 79 | if (net.length != INET_BYTE_LENGTH) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 80 | throw new IllegalArgumentException("Malformed IP address string; " |
| 81 | + "Address must have four decimal values separated by dots (.)"); |
| 82 | } |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 83 | final byte[] bytes = new byte[INET_BYTE_LENGTH]; |
| 84 | for (int i = 0; i < INET_BYTE_LENGTH; i++) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 85 | bytes[i] = (byte) Short.parseShort(net[i], 10); |
| 86 | } |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 87 | return new IpAddress(Version.INET, bytes); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the IP version of this address. |
| 92 | * |
| 93 | * @return the version |
| 94 | */ |
| 95 | public Version version() { |
| 96 | return this.version; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the IP address as a byte array. |
| 101 | * |
| 102 | * @return a byte array |
| 103 | */ |
| 104 | public byte[] toOctets() { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 105 | return Arrays.copyOf(this.octets, INET_BYTE_LENGTH); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the integral value of this IP address. |
| 110 | * |
| 111 | * @return the IP address's value as an integer |
| 112 | */ |
| 113 | public int toInt() { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 114 | ByteBuffer bb = ByteBuffer.wrap(octets); |
| 115 | return bb.getInt(); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 119 | public int compareTo(IpAddress o) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 120 | Long lv = ((long) this.toInt()) & 0xffffffffL; |
| 121 | Long rv = ((long) o.toInt()) & 0xffffffffL; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 122 | return lv.compareTo(rv); |
| 123 | } |
| 124 | |
| 125 | @Override |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 126 | public int hashCode() { |
| 127 | final int prime = 31; |
| 128 | int result = 1; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 129 | result = prime * result + Arrays.hashCode(octets); |
| 130 | result = prime * result + ((version == null) ? 0 : version.hashCode()); |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean equals(Object obj) { |
| 136 | if (this == obj) { |
| 137 | return true; |
| 138 | } |
| 139 | if (obj == null) { |
| 140 | return false; |
| 141 | } |
| 142 | if (getClass() != obj.getClass()) { |
| 143 | return false; |
| 144 | } |
| 145 | IpAddress other = (IpAddress) obj; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 146 | if (!Arrays.equals(octets, other.octets)) { |
| 147 | return false; |
| 148 | } |
| 149 | if (version != other.version) { |
| 150 | return false; |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | /* |
| 157 | * (non-Javadoc) |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame^] | 158 | * format is "x.x.x.x" for IPv4 addresses. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 159 | * |
| 160 | * @see java.lang.Object#toString() |
| 161 | */ |
| 162 | public String toString() { |
| 163 | final StringBuilder builder = new StringBuilder(); |
| 164 | for (final byte b : this.octets) { |
| 165 | if (builder.length() > 0) { |
| 166 | builder.append("."); |
| 167 | } |
| 168 | builder.append(String.format("%d", b & 0xff)); |
| 169 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 170 | return builder.toString(); |
| 171 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 172 | } |