Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | /** |
| 21 | * The class representing MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 22 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 23 | public class MacAddress { |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 24 | |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 25 | public static final MacAddress NONE = valueOf("a4:23:05:00:00:00"); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 26 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); |
| 27 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); |
| 28 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 29 | private static final byte[] LL = new byte[]{ |
| 30 | 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00, |
| 31 | 0x00, 0x0e, 0x03 |
| 32 | }; |
| 33 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 34 | public static final int MAC_ADDRESS_LENGTH = 6; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 35 | private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 36 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 37 | public MacAddress(final byte[] address) { |
| 38 | this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns a MAC address instance representing the value of the specified |
| 43 | * {@code String}. |
| 44 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 45 | * @param address the String representation of the MAC Address to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 46 | * @return a MAC Address instance representing the value of the specified |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 47 | * {@code String}. |
| 48 | * @throws IllegalArgumentException if the string cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 49 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 50 | public static MacAddress valueOf(final String address) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 51 | final String[] elements = address.split(":"); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 52 | if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 53 | throw new IllegalArgumentException( |
| 54 | "Specified MAC Address must contain 12 hex digits" |
| 55 | + " separated pairwise by :'s."); |
| 56 | } |
| 57 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 58 | final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 59 | for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 60 | final String element = elements[i]; |
| 61 | addressInBytes[i] = (byte) Integer.parseInt(element, 16); |
| 62 | } |
| 63 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 64 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a MAC address instance representing the specified {@code byte} |
| 69 | * array. |
| 70 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 71 | * @param address the byte array to be parsed. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 72 | * @return a MAC address instance representing the specified {@code byte} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 73 | * array. |
| 74 | * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 75 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 76 | public static MacAddress valueOf(final byte[] address) { |
| 77 | if (address.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 78 | throw new IllegalArgumentException("the length is not " |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 79 | + MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 82 | return new MacAddress(address); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a MAC address instance representing the specified {@code long} |
| 87 | * value. The lower 48 bits of the long value are used to parse as a MAC |
| 88 | * address. |
| 89 | * |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 90 | * @param address the long value to be parsed. The lower 48 bits are used for a |
| 91 | * MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 92 | * @return a MAC address instance representing the specified {@code long} |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 93 | * value. |
| 94 | * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 95 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 96 | public static MacAddress valueOf(final long address) { |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 97 | final byte[] addressInBytes = new byte[]{ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 98 | (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff), |
| 99 | (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff), |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 100 | (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)}; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 101 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 102 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the length of the {@code MACAddress}. |
| 107 | * |
| 108 | * @return the length of the {@code MACAddress}. |
| 109 | */ |
| 110 | public int length() { |
| 111 | return this.address.length; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the value of the {@code MACAddress} as a {@code byte} array. |
| 116 | * |
| 117 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 118 | * type {@code byte} array. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 119 | */ |
| 120 | public byte[] toBytes() { |
| 121 | return Arrays.copyOf(this.address, this.address.length); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the value of the {@code MACAddress} as a {@code long}. |
| 126 | * |
| 127 | * @return the numeric value represented by this object after conversion to |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 128 | * type {@code long}. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 129 | */ |
| 130 | public long toLong() { |
| 131 | long mac = 0; |
| 132 | for (int i = 0; i < 6; i++) { |
| 133 | final long t = (this.address[i] & 0xffL) << (5 - i) * 8; |
| 134 | mac |= t; |
| 135 | } |
| 136 | return mac; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns {@code true} if the MAC address is the broadcast address. |
| 141 | * |
| 142 | * @return {@code true} if the MAC address is the broadcast address. |
| 143 | */ |
| 144 | public boolean isBroadcast() { |
| 145 | for (final byte b : this.address) { |
| 146 | if (b != -1) { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns {@code true} if the MAC address is the multicast address. |
| 155 | * |
| 156 | * @return {@code true} if the MAC address is the multicast address. |
| 157 | */ |
| 158 | public boolean isMulticast() { |
| 159 | if (this.isBroadcast()) { |
| 160 | return false; |
| 161 | } |
| 162 | return (this.address[0] & 0x01) != 0; |
| 163 | } |
| 164 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Returns true if this MAC address is link local. |
| 167 | * |
| 168 | * @return true if link local |
| 169 | */ |
| 170 | public boolean isLinkLocal() { |
| 171 | return LL[0] == address[0] && LL[1] == address[1] && LL[2] == address[2] && |
| 172 | LL[3] == address[3] && LL[4] == address[4] && |
| 173 | (LL[5] == address[5] || LL[6] == address[5] || LL[7] == address[5]); |
| 174 | } |
| 175 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public boolean equals(final Object o) { |
| 178 | if (o == this) { |
| 179 | return true; |
| 180 | } |
| 181 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 182 | if (!(o instanceof MacAddress)) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 186 | final MacAddress other = (MacAddress) o; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 187 | return Arrays.equals(this.address, other.address); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int hashCode() { |
Brian O'Connor | 8576c2a | 2014-11-19 16:49:26 -0800 | [diff] [blame] | 192 | return Long.hashCode(toLong()); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public String toString() { |
| 197 | final StringBuilder builder = new StringBuilder(); |
| 198 | for (final byte b : this.address) { |
| 199 | if (builder.length() > 0) { |
| 200 | builder.append(":"); |
| 201 | } |
| 202 | builder.append(String.format("%02X", b & 0xFF)); |
| 203 | } |
| 204 | return builder.toString(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @return MAC address in string representation without colons (useful for |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 209 | * radix tree storage) |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 210 | */ |
| 211 | public String toStringNoColon() { |
| 212 | final StringBuilder builder = new StringBuilder(); |
| 213 | for (final byte b : this.address) { |
| 214 | builder.append(String.format("%02X", b & 0xFF)); |
| 215 | } |
| 216 | return builder.toString(); |
| 217 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 218 | } |