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