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 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 19 | |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 20 | // TODO: Add support for IPv6 as well. |
| 21 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 22 | /** |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 23 | * A class representing an IP prefix. A prefix consists of an IP address and |
| 24 | * a subnet mask. |
| 25 | * <p> |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 26 | * NOTE: The stored IP address in the result IP prefix is masked to |
| 27 | * contain zeroes in all bits after the prefix length. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 28 | * </p> |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 29 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 30 | public final class IpPrefix { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 31 | // Maximum network mask length |
| 32 | public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH; |
| 33 | public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 34 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 35 | private final IpAddress address; |
| 36 | private final short prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 37 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 38 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 39 | * Constructor for given IP address, and a prefix length. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 40 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 41 | * @param address the IP address |
| 42 | * @param prefixLength the prefix length |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 43 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 44 | private IpPrefix(IpAddress address, int prefixLength) { |
| 45 | checkPrefixLength(prefixLength); |
| 46 | this.address = IpAddress.makeMaskedAddress(address, prefixLength); |
| 47 | this.prefixLength = (short) prefixLength; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 51 | * Checks whether the prefix length is valid. |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 52 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 53 | * @param prefixLength the prefix length value to check |
| 54 | * @throws IllegalArgumentException if the prefix length value is invalid |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 55 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 56 | private static void checkPrefixLength(int prefixLength) { |
| 57 | if ((prefixLength < 0) || (prefixLength > MAX_INET_MASK_LENGTH)) { |
| 58 | String msg = "Invalid prefix length " + prefixLength + ". " + |
| 59 | "The value must be in the interval [0, " + |
| 60 | MAX_INET_MASK_LENGTH + "]"; |
| 61 | throw new IllegalArgumentException(msg); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 62 | } |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 66 | * Converts an integer and a prefix length into an IPv4 prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 67 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 68 | * @param address an integer representing the IPv4 address |
| 69 | * @param prefixLength the prefix length |
| 70 | * @return an IP prefix |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 71 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 72 | public static IpPrefix valueOf(int address, int prefixLength) { |
| 73 | return new IpPrefix(IpAddress.valueOf(address), prefixLength); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 77 | * Converts a byte array and a prefix length into an IP prefix. |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 78 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 79 | * @param address the IP address value stored in network byte order |
| 80 | * @param prefixLength the prefix length |
| 81 | * @return an IP prefix |
| 82 | */ |
| 83 | public static IpPrefix valueOf(byte[] address, int prefixLength) { |
| 84 | return new IpPrefix(IpAddress.valueOf(address), prefixLength); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Converts an IP address and a prefix length into IP prefix. |
| 89 | * |
| 90 | * @param address the IP address |
| 91 | * @param prefixLength the prefix length |
| 92 | * @return an IP prefix |
| 93 | */ |
| 94 | public static IpPrefix valueOf(IpAddress address, int prefixLength) { |
| 95 | return new IpPrefix(address, prefixLength); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16") into an |
| 100 | * IP prefix. |
| 101 | * |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 102 | * @param address an IP prefix in string form, e.g. "10.1.0.0/16" |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 103 | * @return an IP prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 104 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 105 | public static IpPrefix valueOf(String address) { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 106 | final String[] parts = address.split("/"); |
| 107 | if (parts.length != 2) { |
| 108 | String msg = "Malformed IP prefix string: " + address + "." + |
| 109 | "Address must take form \"x.x.x.x/y\""; |
| 110 | throw new IllegalArgumentException(msg); |
| 111 | } |
| 112 | IpAddress ipAddress = IpAddress.valueOf(parts[0]); |
| 113 | int prefixLength = Integer.parseInt(parts[1]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 114 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 115 | return new IpPrefix(ipAddress, prefixLength); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 119 | * Returns the IP version of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 120 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 121 | * @return the IP version of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 122 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 123 | public IpAddress.Version version() { |
| 124 | return address.version(); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 128 | * Returns the IP address value of the prefix. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 129 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 130 | * @return the IP address value of the prefix |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 131 | */ |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 132 | public IpAddress address() { |
| 133 | return address; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 136 | /** |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 137 | * Returns the IP address prefix length. |
| 138 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 139 | * @return the IP address prefix length |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 140 | */ |
| 141 | public int prefixLength() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 142 | return prefixLength; |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 146 | * Determines whether a given IP prefix is contained within this prefix. |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 147 | * |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 148 | * @param other the IP prefix to test |
| 149 | * @return true if the other IP prefix is contained in this prefix, |
| 150 | * otherwise false |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 151 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 152 | public boolean contains(IpPrefix other) { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 153 | if (this.prefixLength > other.prefixLength) { |
| 154 | return false; // This prefix has smaller prefix size |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 155 | } |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 156 | |
| 157 | // |
| 158 | // Mask the other address with my prefix length. |
| 159 | // If the other prefix is within this prefix, the masked address must |
| 160 | // be same as the address of this prefix. |
| 161 | // |
| 162 | IpAddress maskedAddr = |
| 163 | IpAddress.makeMaskedAddress(other.address, this.prefixLength); |
| 164 | return this.address.equals(maskedAddr); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Determines whether a given IP address is contained within this prefix. |
| 169 | * |
| 170 | * @param other the IP address to test |
| 171 | * @return true if the IP address is contained in this prefix, otherwise |
| 172 | * false |
| 173 | */ |
| 174 | public boolean contains(IpAddress other) { |
| 175 | // |
| 176 | // Mask the other address with my prefix length. |
| 177 | // If the other prefix is within this prefix, the masked address must |
| 178 | // be same as the address of this prefix. |
| 179 | // |
| 180 | IpAddress maskedAddr = |
| 181 | IpAddress.makeMaskedAddress(other, this.prefixLength); |
| 182 | return this.address.equals(maskedAddr); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 185 | @Override |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 186 | public int hashCode() { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 187 | return Objects.hash(address, prefixLength); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public boolean equals(Object obj) { |
| 192 | if (this == obj) { |
| 193 | return true; |
| 194 | } |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 195 | if ((obj == null) || (getClass() != obj.getClass())) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 196 | return false; |
| 197 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 198 | IpPrefix other = (IpPrefix) obj; |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 199 | return ((prefixLength == other.prefixLength) && |
| 200 | address.equals(other.address)); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | /* |
| 205 | * (non-Javadoc) |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 206 | * The format is "x.x.x.x/y" for IPv4 prefixes. |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 207 | * |
| 208 | * @see java.lang.Object#toString() |
| 209 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 210 | public String toString() { |
| 211 | final StringBuilder builder = new StringBuilder(); |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 212 | builder.append(address.toString()); |
| 213 | builder.append("/"); |
| 214 | builder.append(String.format("%d", prefixLength)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 215 | return builder.toString(); |
| 216 | } |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 217 | } |