blob: e9b91131b3826ce98b1edde165c2f93375f8c0b9 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
tom545708e2014-10-09 17:10:02 -070025 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
26 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
27
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070028 private static final byte[] LL = new byte[]{
29 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00,
30 0x00, 0x0e, 0x03
31 };
32
alshabibc4901cd2014-09-05 16:50:40 -070033 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070034 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070035
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070036 public MacAddress(final byte[] address) {
37 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070038 }
39
40 /**
41 * Returns a MAC address instance representing the value of the specified
42 * {@code String}.
43 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070044 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070045 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070046 * {@code String}.
47 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070048 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070049 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070050 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070051 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070052 throw new IllegalArgumentException(
53 "Specified MAC Address must contain 12 hex digits"
54 + " separated pairwise by :'s.");
55 }
56
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
58 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070059 final String element = elements[i];
60 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
61 }
62
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070063 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070064 }
65
66 /**
67 * Returns a MAC address instance representing the specified {@code byte}
68 * array.
69 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070070 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070071 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070072 * array.
73 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070074 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070075 public static MacAddress valueOf(final byte[] address) {
76 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070077 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070078 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070079 }
80
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070082 }
83
84 /**
85 * Returns a MAC address instance representing the specified {@code long}
86 * value. The lower 48 bits of the long value are used to parse as a MAC
87 * address.
88 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070089 * @param address the long value to be parsed. The lower 48 bits are used for a
90 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070091 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070092 * value.
93 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070094 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070095 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070096 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -070097 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
98 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070099 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700100
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700101 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700102 }
103
104 /**
105 * Returns the length of the {@code MACAddress}.
106 *
107 * @return the length of the {@code MACAddress}.
108 */
109 public int length() {
110 return this.address.length;
111 }
112
113 /**
114 * Returns the value of the {@code MACAddress} as a {@code byte} array.
115 *
116 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700117 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700118 */
119 public byte[] toBytes() {
120 return Arrays.copyOf(this.address, this.address.length);
121 }
122
123 /**
124 * Returns the value of the {@code MACAddress} as a {@code long}.
125 *
126 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700127 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700128 */
129 public long toLong() {
130 long mac = 0;
131 for (int i = 0; i < 6; i++) {
132 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
133 mac |= t;
134 }
135 return mac;
136 }
137
138 /**
139 * Returns {@code true} if the MAC address is the broadcast address.
140 *
141 * @return {@code true} if the MAC address is the broadcast address.
142 */
143 public boolean isBroadcast() {
144 for (final byte b : this.address) {
145 if (b != -1) {
146 return false;
147 }
148 }
149 return true;
150 }
151
152 /**
153 * Returns {@code true} if the MAC address is the multicast address.
154 *
155 * @return {@code true} if the MAC address is the multicast address.
156 */
157 public boolean isMulticast() {
158 if (this.isBroadcast()) {
159 return false;
160 }
161 return (this.address[0] & 0x01) != 0;
162 }
163
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700164 /**
165 * Returns true if this MAC address is link local.
166 *
167 * @return true if link local
168 */
169 public boolean isLinkLocal() {
170 return LL[0] == address[0] && LL[1] == address[1] && LL[2] == address[2] &&
171 LL[3] == address[3] && LL[4] == address[4] &&
172 (LL[5] == address[5] || LL[6] == address[5] || LL[7] == address[5]);
173 }
174
alshabibc4901cd2014-09-05 16:50:40 -0700175 @Override
176 public boolean equals(final Object o) {
177 if (o == this) {
178 return true;
179 }
180
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700181 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700182 return false;
183 }
184
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700185 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700186 return Arrays.equals(this.address, other.address);
187 }
188
189 @Override
190 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800191 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700192 }
193
194 @Override
195 public String toString() {
196 final StringBuilder builder = new StringBuilder();
197 for (final byte b : this.address) {
198 if (builder.length() > 0) {
199 builder.append(":");
200 }
201 builder.append(String.format("%02X", b & 0xFF));
202 }
203 return builder.toString();
204 }
205
206 /**
207 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700208 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700209 */
210 public String toStringNoColon() {
211 final StringBuilder builder = new StringBuilder();
212 for (final byte b : this.address) {
213 builder.append(String.format("%02X", b & 0xFF));
214 }
215 return builder.toString();
216 }
alshabibc4901cd2014-09-05 16:50:40 -0700217}