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