blob: 64589f09e2d645e5a6fb92c076388715aff2b42b [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
Charles Chanba138712016-04-06 22:19:13 -070025 /**
26 * Dummy MAC address.
27 */
Luca Pretee3879f72015-10-16 11:19:53 +020028 public static final MacAddress NONE = valueOf("a4:23:05:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070029 /**
30 * All-zero MAC address.
31 */
tom545708e2014-10-09 17:10:02 -070032 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070033 /**
34 * Broadcast MAC address.
35 */
tom545708e2014-10-09 17:10:02 -070036 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070037 /**
38 * IPv4 multicast MAC address.
39 */
40 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
41 /**
42 * IPv4 multicast MAC mask.
43 */
44 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
tom545708e2014-10-09 17:10:02 -070045
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070046 private static final byte[] LL = new byte[]{
47 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00,
48 0x00, 0x0e, 0x03
49 };
50
alshabibc4901cd2014-09-05 16:50:40 -070051 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070053
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070054 public MacAddress(final byte[] address) {
55 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070056 }
57
58 /**
59 * Returns a MAC address instance representing the value of the specified
60 * {@code String}.
61 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070062 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070063 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070064 * {@code String}.
65 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070066 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070068 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070070 throw new IllegalArgumentException(
71 "Specified MAC Address must contain 12 hex digits"
72 + " separated pairwise by :'s.");
73 }
74
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070075 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
76 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070077 final String element = elements[i];
78 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
79 }
80
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070082 }
83
84 /**
85 * Returns a MAC address instance representing the specified {@code byte}
86 * array.
87 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070088 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070089 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070090 * array.
91 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070092 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070093 public static MacAddress valueOf(final byte[] address) {
94 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070095 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070096 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070097 }
98
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070099 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700100 }
101
102 /**
103 * Returns a MAC address instance representing the specified {@code long}
104 * value. The lower 48 bits of the long value are used to parse as a MAC
105 * address.
106 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700107 * @param address the long value to be parsed. The lower 48 bits are used for a
108 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700109 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700110 * value.
111 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700112 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700113 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700114 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700115 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
116 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700117 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700118
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700119 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700120 }
121
122 /**
123 * Returns the length of the {@code MACAddress}.
124 *
125 * @return the length of the {@code MACAddress}.
126 */
127 public int length() {
128 return this.address.length;
129 }
130
131 /**
132 * Returns the value of the {@code MACAddress} as a {@code byte} array.
133 *
134 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700135 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700136 */
137 public byte[] toBytes() {
138 return Arrays.copyOf(this.address, this.address.length);
139 }
140
141 /**
142 * Returns the value of the {@code MACAddress} as a {@code long}.
143 *
144 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700145 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700146 */
147 public long toLong() {
148 long mac = 0;
149 for (int i = 0; i < 6; i++) {
150 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
151 mac |= t;
152 }
153 return mac;
154 }
155
156 /**
157 * Returns {@code true} if the MAC address is the broadcast address.
158 *
159 * @return {@code true} if the MAC address is the broadcast address.
160 */
161 public boolean isBroadcast() {
162 for (final byte b : this.address) {
163 if (b != -1) {
164 return false;
165 }
166 }
167 return true;
168 }
169
170 /**
171 * Returns {@code true} if the MAC address is the multicast address.
172 *
173 * @return {@code true} if the MAC address is the multicast address.
174 */
175 public boolean isMulticast() {
176 if (this.isBroadcast()) {
177 return false;
178 }
179 return (this.address[0] & 0x01) != 0;
180 }
181
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700182 /**
183 * Returns true if this MAC address is link local.
184 *
185 * @return true if link local
186 */
187 public boolean isLinkLocal() {
188 return LL[0] == address[0] && LL[1] == address[1] && LL[2] == address[2] &&
189 LL[3] == address[3] && LL[4] == address[4] &&
190 (LL[5] == address[5] || LL[6] == address[5] || LL[7] == address[5]);
191 }
192
alshabibc4901cd2014-09-05 16:50:40 -0700193 @Override
194 public boolean equals(final Object o) {
195 if (o == this) {
196 return true;
197 }
198
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700199 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700200 return false;
201 }
202
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700203 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700204 return Arrays.equals(this.address, other.address);
205 }
206
207 @Override
208 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800209 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700210 }
211
212 @Override
213 public String toString() {
214 final StringBuilder builder = new StringBuilder();
215 for (final byte b : this.address) {
216 if (builder.length() > 0) {
217 builder.append(":");
218 }
219 builder.append(String.format("%02X", b & 0xFF));
220 }
221 return builder.toString();
222 }
223
224 /**
225 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700226 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700227 */
228 public String toStringNoColon() {
229 final StringBuilder builder = new StringBuilder();
230 for (final byte b : this.address) {
231 builder.append(String.format("%02X", b & 0xFF));
232 }
233 return builder.toString();
234 }
alshabibc4901cd2014-09-05 16:50:40 -0700235}