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