blob: cb7f20138794dfc9fab19a80e02e13ef7bc93958 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
alshabibc4901cd2014-09-05 16:50:40 -070016package org.onlab.packet;
17
18import java.util.Arrays;
19
20/**
21 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070022 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070023public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070024
Luca Pretee3879f72015-10-16 11:19:53 +020025 public static final MacAddress NONE = valueOf("a4:23:05:00:00:00");
tom545708e2014-10-09 17:10:02 -070026 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 Vachuskae1bcb0b2014-10-27 17:45:10 -070029 private static final byte[] LL = new byte[]{
30 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00,
31 0x00, 0x0e, 0x03
32 };
33
alshabibc4901cd2014-09-05 16:50:40 -070034 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070035 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070036
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070037 public MacAddress(final byte[] address) {
38 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070039 }
40
41 /**
42 * Returns a MAC address instance representing the value of the specified
43 * {@code String}.
44 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070045 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070046 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070047 * {@code String}.
48 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070049 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070050 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070051 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070053 throw new IllegalArgumentException(
54 "Specified MAC Address must contain 12 hex digits"
55 + " separated pairwise by :'s.");
56 }
57
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070058 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
59 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070060 final String element = elements[i];
61 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
62 }
63
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070064 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070065 }
66
67 /**
68 * Returns a MAC address instance representing the specified {@code byte}
69 * array.
70 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070071 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070072 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070073 * array.
74 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070075 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070076 public static MacAddress valueOf(final byte[] address) {
77 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070078 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070079 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070080 }
81
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070082 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070083 }
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 Vachuskae1bcb0b2014-10-27 17:45:10 -070090 * @param address the long value to be parsed. The lower 48 bits are used for a
91 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070092 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070093 * value.
94 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070095 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070096 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070097 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -070098 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
99 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700100 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700101
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700102 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700103 }
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 Vachuskae1bcb0b2014-10-27 17:45:10 -0700118 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700119 */
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 Vachuskae1bcb0b2014-10-27 17:45:10 -0700128 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700129 */
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 Vachuskae1bcb0b2014-10-27 17:45:10 -0700165 /**
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
alshabibc4901cd2014-09-05 16:50:40 -0700176 @Override
177 public boolean equals(final Object o) {
178 if (o == this) {
179 return true;
180 }
181
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700182 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700183 return false;
184 }
185
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700186 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700187 return Arrays.equals(this.address, other.address);
188 }
189
190 @Override
191 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800192 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700193 }
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 Vachuskae1bcb0b2014-10-27 17:45:10 -0700209 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700210 */
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 }
alshabibc4901cd2014-09-05 16:50:40 -0700218}