Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
| 20 | |
| 21 | import java.util.Arrays; |
| 22 | |
| 23 | /** |
| 24 | * The class representing MAC address. |
| 25 | * |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 26 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 27 | public class MacAddress { |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 28 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 29 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); |
| 30 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); |
| 31 | |
| 32 | public static final byte[] ZERO_MAC_ADDRESS = ZERO.getAddress(); |
| 33 | public static final byte[] BROADCAST_MAC = BROADCAST.getAddress(); |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 34 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 35 | public static final int MAC_ADDRESS_LENGTH = 6; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 36 | private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 37 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 38 | public MacAddress(final byte[] address) { |
| 39 | this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns a MAC address instance representing the value of the specified |
| 44 | * {@code String}. |
| 45 | * |
| 46 | * @param address |
| 47 | * the String representation of the MAC Address to be parsed. |
| 48 | * @return a MAC Address instance representing the value of the specified |
| 49 | * {@code String}. |
| 50 | * @throws IllegalArgumentException |
| 51 | * if the string cannot be parsed as a MAC address. |
| 52 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 53 | public static MacAddress valueOf(final String address) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 54 | final String[] elements = address.split(":"); |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 55 | if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 56 | throw new IllegalArgumentException( |
| 57 | "Specified MAC Address must contain 12 hex digits" |
| 58 | + " separated pairwise by :'s."); |
| 59 | } |
| 60 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 61 | final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 62 | for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 63 | final String element = elements[i]; |
| 64 | addressInBytes[i] = (byte) Integer.parseInt(element, 16); |
| 65 | } |
| 66 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 67 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns a MAC address instance representing the specified {@code byte} |
| 72 | * array. |
| 73 | * |
| 74 | * @param address |
| 75 | * the byte array to be parsed. |
| 76 | * @return a MAC address instance representing the specified {@code byte} |
| 77 | * array. |
| 78 | * @throws IllegalArgumentException |
| 79 | * if the byte array cannot be parsed as a MAC address. |
| 80 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 81 | public static MacAddress valueOf(final byte[] address) { |
| 82 | if (address.length != MacAddress.MAC_ADDRESS_LENGTH) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 83 | throw new IllegalArgumentException("the length is not " |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 84 | + MacAddress.MAC_ADDRESS_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 87 | return new MacAddress(address); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns a MAC address instance representing the specified {@code long} |
| 92 | * value. The lower 48 bits of the long value are used to parse as a MAC |
| 93 | * address. |
| 94 | * |
| 95 | * @param address |
| 96 | * the long value to be parsed. The lower 48 bits are used for a |
| 97 | * MAC address. |
| 98 | * @return a MAC address instance representing the specified {@code long} |
| 99 | * value. |
| 100 | * @throws IllegalArgumentException |
| 101 | * if the long value cannot be parsed as a MAC address. |
| 102 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 103 | public static MacAddress valueOf(final long address) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 104 | final byte[] addressInBytes = new byte[] { |
| 105 | (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff), |
| 106 | (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff), |
| 107 | (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff) }; |
| 108 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 109 | return new MacAddress(addressInBytes); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the length of the {@code MACAddress}. |
| 114 | * |
| 115 | * @return the length of the {@code MACAddress}. |
| 116 | */ |
| 117 | public int length() { |
| 118 | return this.address.length; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the value of the {@code MACAddress} as a {@code byte} array. |
| 123 | * |
| 124 | * @return the numeric value represented by this object after conversion to |
| 125 | * type {@code byte} array. |
| 126 | */ |
| 127 | public byte[] toBytes() { |
| 128 | return Arrays.copyOf(this.address, this.address.length); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the value of the {@code MACAddress} as a {@code long}. |
| 133 | * |
| 134 | * @return the numeric value represented by this object after conversion to |
| 135 | * type {@code long}. |
| 136 | */ |
| 137 | public long toLong() { |
| 138 | long mac = 0; |
| 139 | for (int i = 0; i < 6; i++) { |
| 140 | final long t = (this.address[i] & 0xffL) << (5 - i) * 8; |
| 141 | mac |= t; |
| 142 | } |
| 143 | return mac; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns {@code true} if the MAC address is the broadcast address. |
| 148 | * |
| 149 | * @return {@code true} if the MAC address is the broadcast address. |
| 150 | */ |
| 151 | public boolean isBroadcast() { |
| 152 | for (final byte b : this.address) { |
| 153 | if (b != -1) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns {@code true} if the MAC address is the multicast address. |
| 162 | * |
| 163 | * @return {@code true} if the MAC address is the multicast address. |
| 164 | */ |
| 165 | public boolean isMulticast() { |
| 166 | if (this.isBroadcast()) { |
| 167 | return false; |
| 168 | } |
| 169 | return (this.address[0] & 0x01) != 0; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean equals(final Object o) { |
| 174 | if (o == this) { |
| 175 | return true; |
| 176 | } |
| 177 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 178 | if (!(o instanceof MacAddress)) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 182 | final MacAddress other = (MacAddress) o; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 183 | return Arrays.equals(this.address, other.address); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int hashCode() { |
| 188 | return Arrays.hashCode(this.address); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | final StringBuilder builder = new StringBuilder(); |
| 194 | for (final byte b : this.address) { |
| 195 | if (builder.length() > 0) { |
| 196 | builder.append(":"); |
| 197 | } |
| 198 | builder.append(String.format("%02X", b & 0xFF)); |
| 199 | } |
| 200 | return builder.toString(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @return MAC address in string representation without colons (useful for |
| 205 | * radix tree storage) |
| 206 | */ |
| 207 | public String toStringNoColon() { |
| 208 | final StringBuilder builder = new StringBuilder(); |
| 209 | for (final byte b : this.address) { |
| 210 | builder.append(String.format("%02X", b & 0xFF)); |
| 211 | } |
| 212 | return builder.toString(); |
| 213 | } |
| 214 | |
| 215 | public byte[] getAddress() { |
| 216 | return this.address; |
| 217 | } |
| 218 | } |