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 | |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 21 | * A class representing an IPv4 prefix. |
| 22 | * <p/> |
| 23 | * A prefix consists of an IP address and a subnet mask. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 24 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 25 | public final class IpPrefix { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 26 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 27 | // TODO a comparator for netmasks? E.g. for sorting by prefix match order. |
| 28 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 29 | //IP Versions |
| 30 | public enum Version { INET, INET6 }; |
| 31 | |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 32 | //lengths of address, in bytes |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 33 | public static final int INET_LEN = 4; |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 34 | public static final int INET6_LEN = 16; |
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 | //maximum CIDR value |
| 37 | public static final int MAX_INET_MASK = 32; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 38 | //no mask (no network), e.g. a simple address |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 39 | public static final int DEFAULT_MASK = 0; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 40 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Default value indicating an unspecified address. |
| 43 | */ |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 44 | static final byte[] ANY = new byte [] {0, 0, 0, 0}; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 45 | |
| 46 | protected Version version; |
| 47 | |
| 48 | protected byte[] octets; |
| 49 | protected int netmask; |
| 50 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 51 | private IpPrefix(Version ver, byte[] octets, int netmask) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 52 | this.version = ver; |
| 53 | this.octets = Arrays.copyOf(octets, INET_LEN); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 54 | this.netmask = netmask; |
| 55 | } |
| 56 | |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 57 | private IpPrefix(Version ver, byte[] octets) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 58 | this.version = ver; |
| 59 | this.octets = Arrays.copyOf(octets, INET_LEN); |
| 60 | this.netmask = DEFAULT_MASK; |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Converts a byte array into an IP address. |
| 65 | * |
| 66 | * @param address a byte array |
| 67 | * @return an IP address |
| 68 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 69 | public static IpPrefix valueOf(byte [] address) { |
| 70 | return new IpPrefix(Version.INET, address); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 74 | * Converts a byte array into an IP address. |
| 75 | * |
| 76 | * @param address a byte array |
| 77 | * @param netmask the CIDR value subnet mask |
| 78 | * @return an IP address |
| 79 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 80 | public static IpPrefix valueOf(byte [] address, int netmask) { |
| 81 | return new IpPrefix(Version.INET, address, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Helper to convert an integer into a byte array. |
| 86 | * |
| 87 | * @param address the integer to convert |
| 88 | * @return a byte array |
| 89 | */ |
| 90 | private static byte [] bytes(int address) { |
| 91 | byte [] bytes = new byte [INET_LEN]; |
| 92 | for (int i = 0; i < INET_LEN; i++) { |
| 93 | bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); |
| 94 | } |
| 95 | |
| 96 | return bytes; |
| 97 | } |
| 98 | |
| 99 | /** |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 100 | * Converts an integer into an IPv4 address. |
| 101 | * |
| 102 | * @param address an integer representing an IP value |
| 103 | * @return an IP address |
| 104 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 105 | public static IpPrefix valueOf(int address) { |
| 106 | return new IpPrefix(Version.INET, bytes(address)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 110 | * Converts an integer into an IPv4 address. |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 111 | * |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 112 | * @param address an integer representing an IP value |
| 113 | * @param netmask the CIDR value subnet mask |
| 114 | * @return an IP address |
| 115 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 116 | public static IpPrefix valueOf(int address, int netmask) { |
| 117 | return new IpPrefix(Version.INET, bytes(address), netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. The |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 122 | * string can also be in CIDR (slash) notation. If the netmask is omitted, |
| 123 | * it will be set to DEFAULT_MASK (0). |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 124 | * |
| 125 | * @param address a IP address in string form, e.g. "10.0.0.1", "10.0.0.1/24" |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 126 | * @return an IP address |
| 127 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 128 | public static IpPrefix valueOf(String address) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 129 | |
| 130 | final String [] parts = address.split("\\/"); |
| 131 | if (parts.length > 2) { |
| 132 | throw new IllegalArgumentException("Malformed IP address string; " |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 133 | + "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\""); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int mask = DEFAULT_MASK; |
| 137 | if (parts.length == 2) { |
Yuta HIGUCHI | 7f3df23 | 2014-10-15 23:38:24 -0700 | [diff] [blame] | 138 | mask = Integer.parseInt(parts[1]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 139 | if (mask > MAX_INET_MASK) { |
| 140 | throw new IllegalArgumentException( |
| 141 | "Value of subnet mask cannot exceed " |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 142 | + MAX_INET_MASK); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | final String [] net = parts[0].split("\\."); |
| 147 | if (net.length != INET_LEN) { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 148 | throw new IllegalArgumentException("Malformed IP address string; " |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 149 | + "Address must have four decimal values separated by dots (.)"); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 150 | } |
| 151 | final byte [] bytes = new byte[INET_LEN]; |
| 152 | for (int i = 0; i < INET_LEN; i++) { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 153 | bytes[i] = (byte) Short.parseShort(net[i], 10); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 154 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 155 | return new IpPrefix(Version.INET, bytes, mask); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the IP version of this address. |
| 160 | * |
| 161 | * @return the version |
| 162 | */ |
| 163 | public Version version() { |
| 164 | return this.version; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the IP address as a byte array. |
| 169 | * |
| 170 | * @return a byte array |
| 171 | */ |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 172 | public byte[] toOctets() { |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 173 | return Arrays.copyOf(this.octets, INET_LEN); |
| 174 | } |
| 175 | |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 176 | /** |
Yuta HIGUCHI | 10681f6 | 2014-09-21 17:49:46 -0700 | [diff] [blame] | 177 | * Returns the IP address prefix length. |
| 178 | * |
| 179 | * @return prefix length |
| 180 | */ |
| 181 | public int prefixLength() { |
| 182 | return netmask; |
| 183 | } |
| 184 | |
| 185 | /** |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 186 | * Returns the integral value of this IP address. |
| 187 | * |
| 188 | * @return the IP address's value as an integer |
| 189 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 190 | public int toInt() { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 191 | int val = 0; |
| 192 | for (int i = 0; i < octets.length; i++) { |
| 193 | val <<= 8; |
| 194 | val |= octets[i] & 0xff; |
| 195 | } |
| 196 | return val; |
| 197 | } |
| 198 | |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Helper for computing the mask value from CIDR. |
| 201 | * |
| 202 | * @return an integer bitmask |
| 203 | */ |
| 204 | private int mask() { |
| 205 | int shift = MAX_INET_MASK - this.netmask; |
| 206 | return ((Integer.MAX_VALUE >>> (shift - 1)) << shift); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the subnet mask in IpAddress form. The netmask value for |
| 211 | * the returned IpAddress is 0, as the address itself is a mask. |
| 212 | * |
| 213 | * @return the subnet mask |
| 214 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 215 | public IpPrefix netmask() { |
| 216 | return new IpPrefix(Version.INET, bytes(mask())); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the network portion of this address as an IpAddress. |
| 221 | * The netmask of the returned IpAddress is the current mask. If this |
| 222 | * address doesn't have a mask, this returns an all-0 IpAddress. |
| 223 | * |
| 224 | * @return the network address or null |
| 225 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 226 | public IpPrefix network() { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 227 | if (netmask == DEFAULT_MASK) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 228 | return new IpPrefix(version, ANY, DEFAULT_MASK); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | byte [] net = new byte [4]; |
| 232 | byte [] mask = bytes(mask()); |
| 233 | for (int i = 0; i < INET_LEN; i++) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 234 | net[i] = (byte) (octets[i] & mask[i]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 235 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 236 | return new IpPrefix(version, net, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the host portion of the IPAddress, as an IPAddress. |
| 241 | * The netmask of the returned IpAddress is the current mask. If this |
| 242 | * address doesn't have a mask, this returns a copy of the current |
| 243 | * address. |
| 244 | * |
| 245 | * @return the host address |
| 246 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 247 | public IpPrefix host() { |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 248 | if (netmask == DEFAULT_MASK) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 249 | new IpPrefix(version, octets, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | byte [] host = new byte [INET_LEN]; |
| 253 | byte [] mask = bytes(mask()); |
| 254 | for (int i = 0; i < INET_LEN; i++) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 255 | host[i] = (byte) (octets[i] & ~mask[i]); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 256 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 257 | return new IpPrefix(version, host, netmask); |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Returns an IpAddress of the bytes contained in this prefix. |
| 262 | * FIXME this is a hack for now and only works because IpPrefix doesn't |
| 263 | * mask the input bytes on creation. |
| 264 | * |
| 265 | * @return the IpAddress |
| 266 | */ |
| 267 | public IpAddress toIpAddress() { |
| 268 | return IpAddress.valueOf(octets); |
| 269 | } |
| 270 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 271 | public boolean isMasked() { |
| 272 | return mask() != 0; |
| 273 | } |
| 274 | |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 275 | /** |
| 276 | * Determines whether a given address is contained within this IpAddress' |
| 277 | * network. |
| 278 | * |
| 279 | * @param other another IP address that could be contained in this network |
| 280 | * @return true if the other IP address is contained in this address' |
| 281 | * network, otherwise false |
| 282 | */ |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 283 | public boolean contains(IpPrefix other) { |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 284 | if (this.netmask <= other.netmask) { |
| 285 | // Special case where they're both /32 addresses |
| 286 | if (this.netmask == MAX_INET_MASK) { |
| 287 | return Arrays.equals(octets, other.octets); |
| 288 | } |
| 289 | |
| 290 | // Mask the other address with our network mask |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 291 | IpPrefix otherMasked = |
| 292 | IpPrefix.valueOf(other.octets, netmask).network(); |
Jonathan Hart | b7a2ac3 | 2014-09-19 10:42:27 -0700 | [diff] [blame] | 293 | |
| 294 | return network().equals(otherMasked); |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 299 | public boolean contains(IpAddress address) { |
| 300 | // Need to get the network address because prefixes aren't automatically |
| 301 | // masked on creation |
| 302 | IpPrefix meMasked = network(); |
| 303 | |
| 304 | IpPrefix otherMasked = |
| 305 | IpPrefix.valueOf(address.octets, netmask).network(); |
| 306 | |
| 307 | return Arrays.equals(meMasked.octets, otherMasked.octets); |
| 308 | } |
| 309 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 310 | @Override |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 311 | public int hashCode() { |
| 312 | final int prime = 31; |
| 313 | int result = 1; |
| 314 | result = prime * result + netmask; |
| 315 | result = prime * result + Arrays.hashCode(octets); |
| 316 | result = prime * result + ((version == null) ? 0 : version.hashCode()); |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public boolean equals(Object obj) { |
| 322 | if (this == obj) { |
| 323 | return true; |
| 324 | } |
| 325 | if (obj == null) { |
| 326 | return false; |
| 327 | } |
| 328 | if (getClass() != obj.getClass()) { |
| 329 | return false; |
| 330 | } |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 331 | IpPrefix other = (IpPrefix) obj; |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 332 | if (netmask != other.netmask) { |
| 333 | return false; |
| 334 | } |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 335 | // TODO not quite right until we mask the input |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 336 | if (!Arrays.equals(octets, other.octets)) { |
| 337 | return false; |
| 338 | } |
| 339 | if (version != other.version) { |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | /* |
| 347 | * (non-Javadoc) |
| 348 | * format is "x.x.x.x" for non-masked (netmask 0) addresses, |
| 349 | * and "x.x.x.x/y" for masked addresses. |
| 350 | * |
| 351 | * @see java.lang.Object#toString() |
| 352 | */ |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 353 | public String toString() { |
| 354 | final StringBuilder builder = new StringBuilder(); |
| 355 | for (final byte b : this.octets) { |
| 356 | if (builder.length() > 0) { |
| 357 | builder.append("."); |
| 358 | } |
Ayaka Koshibe | 40e7fec | 2014-09-16 22:32:19 -0700 | [diff] [blame] | 359 | builder.append(String.format("%d", b & 0xff)); |
| 360 | } |
| 361 | if (netmask != DEFAULT_MASK) { |
| 362 | builder.append("/"); |
| 363 | builder.append(String.format("%d", netmask)); |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 364 | } |
| 365 | return builder.toString(); |
| 366 | } |
| 367 | |
Ayaka Koshibe | 1c7b38e | 2014-09-11 13:09:51 -0700 | [diff] [blame] | 368 | } |