blob: 78114cc07ab00c0023872f4221614697f0242e81 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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
alshabibc4901cd2014-09-05 16:50:40 -07009 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
Thomas Vachuska24c849c2014-10-27 09:53:05 -070012 * 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 */
alshabibc4901cd2014-09-05 16:50:40 -070019package org.onlab.packet;
20
21import java.util.Arrays;
22
23/**
24 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070025 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070026public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070027
tom545708e2014-10-09 17:10:02 -070028 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
29 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
30
31 public static final byte[] ZERO_MAC_ADDRESS = ZERO.getAddress();
32 public static final byte[] BROADCAST_MAC = BROADCAST.getAddress();
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070033
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070034 private static final byte[] LL = new byte[]{
35 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00,
36 0x00, 0x0e, 0x03
37 };
38
alshabibc4901cd2014-09-05 16:50:40 -070039 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070040 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070041
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070042 public MacAddress(final byte[] address) {
43 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070044 }
45
46 /**
47 * Returns a MAC address instance representing the value of the specified
48 * {@code String}.
49 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070050 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070051 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070052 * {@code String}.
53 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070054 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070055 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070056 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070058 throw new IllegalArgumentException(
59 "Specified MAC Address must contain 12 hex digits"
60 + " separated pairwise by :'s.");
61 }
62
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070063 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
64 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070065 final String element = elements[i];
66 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
67 }
68
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070070 }
71
72 /**
73 * Returns a MAC address instance representing the specified {@code byte}
74 * array.
75 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070076 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070077 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070078 * array.
79 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070080 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 public static MacAddress valueOf(final byte[] address) {
82 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070083 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070084 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070085 }
86
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070088 }
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 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070095 * @param address the long value to be parsed. The lower 48 bits are used for a
96 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070097 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070098 * value.
99 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700100 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700101 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700102 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700103 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
104 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700105 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700106
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700108 }
109
110 /**
111 * Returns the length of the {@code MACAddress}.
112 *
113 * @return the length of the {@code MACAddress}.
114 */
115 public int length() {
116 return this.address.length;
117 }
118
119 /**
120 * Returns the value of the {@code MACAddress} as a {@code byte} array.
121 *
122 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700123 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700124 */
125 public byte[] toBytes() {
126 return Arrays.copyOf(this.address, this.address.length);
127 }
128
129 /**
130 * Returns the value of the {@code MACAddress} as a {@code long}.
131 *
132 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700133 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700134 */
135 public long toLong() {
136 long mac = 0;
137 for (int i = 0; i < 6; i++) {
138 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
139 mac |= t;
140 }
141 return mac;
142 }
143
144 /**
145 * Returns {@code true} if the MAC address is the broadcast address.
146 *
147 * @return {@code true} if the MAC address is the broadcast address.
148 */
149 public boolean isBroadcast() {
150 for (final byte b : this.address) {
151 if (b != -1) {
152 return false;
153 }
154 }
155 return true;
156 }
157
158 /**
159 * Returns {@code true} if the MAC address is the multicast address.
160 *
161 * @return {@code true} if the MAC address is the multicast address.
162 */
163 public boolean isMulticast() {
164 if (this.isBroadcast()) {
165 return false;
166 }
167 return (this.address[0] & 0x01) != 0;
168 }
169
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700170 /**
171 * Returns true if this MAC address is link local.
172 *
173 * @return true if link local
174 */
175 public boolean isLinkLocal() {
176 return LL[0] == address[0] && LL[1] == address[1] && LL[2] == address[2] &&
177 LL[3] == address[3] && LL[4] == address[4] &&
178 (LL[5] == address[5] || LL[6] == address[5] || LL[7] == address[5]);
179 }
180
alshabibc4901cd2014-09-05 16:50:40 -0700181 @Override
182 public boolean equals(final Object o) {
183 if (o == this) {
184 return true;
185 }
186
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700187 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700188 return false;
189 }
190
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700191 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700192 return Arrays.equals(this.address, other.address);
193 }
194
195 @Override
196 public int hashCode() {
197 return Arrays.hashCode(this.address);
198 }
199
200 @Override
201 public String toString() {
202 final StringBuilder builder = new StringBuilder();
203 for (final byte b : this.address) {
204 if (builder.length() > 0) {
205 builder.append(":");
206 }
207 builder.append(String.format("%02X", b & 0xFF));
208 }
209 return builder.toString();
210 }
211
212 /**
213 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700214 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700215 */
216 public String toStringNoColon() {
217 final StringBuilder builder = new StringBuilder();
218 for (final byte b : this.address) {
219 builder.append(String.format("%02X", b & 0xFF));
220 }
221 return builder.toString();
222 }
223
224 public byte[] getAddress() {
225 return this.address;
226 }
227}