Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
alshabib | c4901cd | 2014-09-05 16:50:40 -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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -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 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 20 | import java.util.Set; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * The class representing MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 24 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 25 | public class MacAddress { |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 26 | |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 27 | /** |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 28 | * First MAC address in ONOS OUI range. |
| 29 | */ |
| 30 | public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00"); |
| 31 | /** |
Charles Chan | 79cddb1 | 2017-05-12 18:07:16 -0700 | [diff] [blame] | 32 | * Dummy MAC address. We use the first MAC address in ONOS OUI range as the dummy MAC address. |
| 33 | */ |
| 34 | public static final MacAddress NONE = ONOS; |
| 35 | /** |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 36 | * ONOS LLDP MAC address with multicast bit set. |
| 37 | */ |
| 38 | public static final MacAddress ONOS_LLDP = valueOf("a5:23:05:00:00:01"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 39 | /** |
| 40 | * All-zero MAC address. |
| 41 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 42 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Broadcast MAC address. |
| 45 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 46 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); |
Charles Chan | ba13871 | 2016-04-06 22:19:13 -0700 | [diff] [blame] | 47 | /** |
| 48 | * IPv4 multicast MAC address. |
| 49 | */ |
| 50 | public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00"); |
| 51 | /** |
| 52 | * IPv4 multicast MAC mask. |
| 53 | */ |
| 54 | public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00"); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 55 | /** |
| 56 | * A set of LLDP MAC addresses. |
| 57 | */ |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 58 | public static final Set<MacAddress> LLDP = ImmutableSet.of( |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 59 | MacAddress.valueOf("01:80:c2:00:00:00"), |
| 60 | MacAddress.valueOf("01:80:c2:00:00:03"), |
| 61 | MacAddress.valueOf("01:80:c2:00:00:0e")); |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 62 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 63 | public static final int MAC_ADDRESS_LENGTH = 6; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 64 | private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 65 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 66 | public MacAddress(final byte[] address) { |
| 67 | this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns a MAC address instance representing the value of the specified |
| 72 | * {@code String}. |
| 73 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 74 | * @param address the String representation of the MAC Address to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 75 | * @return a MAC Address instance representing the value of the specified |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 76 | * {@code String}. |
| 77 | * @throws IllegalArgumentException if the string cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 78 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 79 | public static MacAddress valueOf(final String address) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 80 | final String[] elements = address.split(":"); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 81 | if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 82 | throw new IllegalArgumentException( |
| 83 | "Specified MAC Address must contain 12 hex digits" |
| 84 | + " separated pairwise by :'s."); |
| 85 | } |
| 86 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 87 | final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 88 | for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 89 | final String element = elements[i]; |
| 90 | addressInBytes[i] = (byte) Integer.parseInt(element, 16); |
| 91 | } |
| 92 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 93 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a MAC address instance representing the specified {@code byte} |
| 98 | * array. |
| 99 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 100 | * @param address the byte array to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 101 | * @return a MAC address instance representing the specified {@code byte} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 102 | * array. |
| 103 | * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 104 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 105 | public static MacAddress valueOf(final byte[] address) { |
| 106 | if (address.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 107 | throw new IllegalArgumentException("the length is not " |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 108 | + MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 111 | return new MacAddress(address); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns a MAC address instance representing the specified {@code long} |
| 116 | * value. The lower 48 bits of the long value are used to parse as a MAC |
| 117 | * address. |
| 118 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 119 | * @param address the long value to be parsed. The lower 48 bits are used for a |
| 120 | * MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 121 | * @return a MAC address instance representing the specified {@code long} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 122 | * value. |
| 123 | * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 124 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 125 | public static MacAddress valueOf(final long address) { |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 126 | final byte[] addressInBytes = new byte[]{ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 127 | (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff), |
| 128 | (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff), |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 129 | (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)}; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 130 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 131 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the length of the {@code MACAddress}. |
| 136 | * |
| 137 | * @return the length of the {@code MACAddress}. |
| 138 | */ |
| 139 | public int length() { |
| 140 | return this.address.length; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the value of the {@code MACAddress} as a {@code byte} array. |
| 145 | * |
| 146 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 147 | * type {@code byte} array. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 148 | */ |
| 149 | public byte[] toBytes() { |
| 150 | return Arrays.copyOf(this.address, this.address.length); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the value of the {@code MACAddress} as a {@code long}. |
| 155 | * |
| 156 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 157 | * type {@code long}. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 158 | */ |
| 159 | public long toLong() { |
| 160 | long mac = 0; |
| 161 | for (int i = 0; i < 6; i++) { |
| 162 | final long t = (this.address[i] & 0xffL) << (5 - i) * 8; |
| 163 | mac |= t; |
| 164 | } |
| 165 | return mac; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns {@code true} if the MAC address is the broadcast address. |
| 170 | * |
| 171 | * @return {@code true} if the MAC address is the broadcast address. |
| 172 | */ |
| 173 | public boolean isBroadcast() { |
| 174 | for (final byte b : this.address) { |
| 175 | if (b != -1) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns {@code true} if the MAC address is the multicast address. |
| 184 | * |
| 185 | * @return {@code true} if the MAC address is the multicast address. |
| 186 | */ |
| 187 | public boolean isMulticast() { |
| 188 | if (this.isBroadcast()) { |
| 189 | return false; |
| 190 | } |
| 191 | return (this.address[0] & 0x01) != 0; |
| 192 | } |
| 193 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Returns true if this MAC address is link local. |
| 196 | * |
| 197 | * @return true if link local |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 198 | * @deprecated in Kingfisher release. Link local is not a correct description for |
| 199 | * this MAC address. Replaced with {@link #isLldp()} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 200 | */ |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 201 | @Deprecated |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 202 | public boolean isLinkLocal() { |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 203 | return isLldp(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns true if this MAC address is used by link layer discovery protocol. |
| 208 | * |
| 209 | * @return true if this MAC is LLDP MAC. |
| 210 | */ |
| 211 | public boolean isLldp() { |
| 212 | return LLDP.contains(this); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Returns true if the Organizationally Unique Identifier (OUI) of this MAC |
| 217 | * address matches ONOS OUI. |
| 218 | * |
| 219 | * @return true if the OUI of this MAC address matches ONOS OUI. |
| 220 | */ |
| 221 | public boolean isOnos() { |
| 222 | return Arrays.equals(this.oui(), ONOS.oui()); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns the Organizationally Unique Identifier (OUI) of this MAC address. |
| 227 | * |
| 228 | * @return the OUI of this MAC address. |
| 229 | */ |
| 230 | public byte[] oui() { |
| 231 | return Arrays.copyOfRange(this.address, 0, 3); |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 232 | } |
| 233 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 234 | @Override |
| 235 | public boolean equals(final Object o) { |
| 236 | if (o == this) { |
| 237 | return true; |
| 238 | } |
| 239 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 240 | if (!(o instanceof MacAddress)) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 244 | final MacAddress other = (MacAddress) o; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 245 | return Arrays.equals(this.address, other.address); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public int hashCode() { |
Brian O'Connor | 8576c2a | 2014-11-19 16:49:26 -0800 | [diff] [blame] | 250 | return Long.hashCode(toLong()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public String toString() { |
Yuta HIGUCHI | ed1ef3a | 2017-05-15 18:10:01 -0700 | [diff] [blame] | 255 | final StringBuilder builder = new StringBuilder(2 * 6 + 5); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 256 | for (final byte b : this.address) { |
| 257 | if (builder.length() > 0) { |
Yuta HIGUCHI | ed1ef3a | 2017-05-15 18:10:01 -0700 | [diff] [blame] | 258 | builder.append(':'); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 259 | } |
| 260 | builder.append(String.format("%02X", b & 0xFF)); |
| 261 | } |
| 262 | return builder.toString(); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @return MAC address in string representation without colons (useful for |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 267 | * radix tree storage) |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 268 | */ |
| 269 | public String toStringNoColon() { |
| 270 | final StringBuilder builder = new StringBuilder(); |
| 271 | for (final byte b : this.address) { |
| 272 | builder.append(String.format("%02X", b & 0xFF)); |
| 273 | } |
| 274 | return builder.toString(); |
| 275 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 276 | } |