blob: 5675a3136050d9cf3871bf4dbc6bc5bb1b49e495 [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
28 public static final byte[] ZERO_MAC_ADDRESS = ZERO.getAddress();
29 public static final byte[] BROADCAST_MAC = BROADCAST.getAddress();
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070030
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070031 private static final byte[] LL = new byte[]{
32 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00,
33 0x00, 0x0e, 0x03
34 };
35
alshabibc4901cd2014-09-05 16:50:40 -070036 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070037 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070038
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070039 public MacAddress(final byte[] address) {
40 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070041 }
42
43 /**
44 * Returns a MAC address instance representing the value of the specified
45 * {@code String}.
46 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070047 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070048 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070049 * {@code String}.
50 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070051 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070053 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070054 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070055 throw new IllegalArgumentException(
56 "Specified MAC Address must contain 12 hex digits"
57 + " separated pairwise by :'s.");
58 }
59
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070060 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
61 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070062 final String element = elements[i];
63 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
64 }
65
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070066 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070067 }
68
69 /**
70 * Returns a MAC address instance representing the specified {@code byte}
71 * array.
72 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070073 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070074 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070075 * array.
76 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070077 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070078 public static MacAddress valueOf(final byte[] address) {
79 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070080 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070081 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070082 }
83
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070084 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070085 }
86
87 /**
88 * Returns a MAC address instance representing the specified {@code long}
89 * value. The lower 48 bits of the long value are used to parse as a MAC
90 * address.
91 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070092 * @param address the long value to be parsed. The lower 48 bits are used for a
93 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070094 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070095 * value.
96 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070097 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070098 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070099 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700100 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
101 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700102 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700103
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700104 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700105 }
106
107 /**
108 * Returns the length of the {@code MACAddress}.
109 *
110 * @return the length of the {@code MACAddress}.
111 */
112 public int length() {
113 return this.address.length;
114 }
115
116 /**
117 * Returns the value of the {@code MACAddress} as a {@code byte} array.
118 *
119 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700120 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700121 */
122 public byte[] toBytes() {
123 return Arrays.copyOf(this.address, this.address.length);
124 }
125
126 /**
127 * Returns the value of the {@code MACAddress} as a {@code long}.
128 *
129 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700130 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700131 */
132 public long toLong() {
133 long mac = 0;
134 for (int i = 0; i < 6; i++) {
135 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
136 mac |= t;
137 }
138 return mac;
139 }
140
141 /**
142 * Returns {@code true} if the MAC address is the broadcast address.
143 *
144 * @return {@code true} if the MAC address is the broadcast address.
145 */
146 public boolean isBroadcast() {
147 for (final byte b : this.address) {
148 if (b != -1) {
149 return false;
150 }
151 }
152 return true;
153 }
154
155 /**
156 * Returns {@code true} if the MAC address is the multicast address.
157 *
158 * @return {@code true} if the MAC address is the multicast address.
159 */
160 public boolean isMulticast() {
161 if (this.isBroadcast()) {
162 return false;
163 }
164 return (this.address[0] & 0x01) != 0;
165 }
166
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700167 /**
168 * Returns true if this MAC address is link local.
169 *
170 * @return true if link local
171 */
172 public boolean isLinkLocal() {
173 return LL[0] == address[0] && LL[1] == address[1] && LL[2] == address[2] &&
174 LL[3] == address[3] && LL[4] == address[4] &&
175 (LL[5] == address[5] || LL[6] == address[5] || LL[7] == address[5]);
176 }
177
alshabibc4901cd2014-09-05 16:50:40 -0700178 @Override
179 public boolean equals(final Object o) {
180 if (o == this) {
181 return true;
182 }
183
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700184 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700185 return false;
186 }
187
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700188 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700189 return Arrays.equals(this.address, other.address);
190 }
191
192 @Override
193 public int hashCode() {
194 return Arrays.hashCode(this.address);
195 }
196
197 @Override
198 public String toString() {
199 final StringBuilder builder = new StringBuilder();
200 for (final byte b : this.address) {
201 if (builder.length() > 0) {
202 builder.append(":");
203 }
204 builder.append(String.format("%02X", b & 0xFF));
205 }
206 return builder.toString();
207 }
208
209 /**
210 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700211 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700212 */
213 public String toStringNoColon() {
214 final StringBuilder builder = new StringBuilder();
215 for (final byte b : this.address) {
216 builder.append(String.format("%02X", b & 0xFF));
217 }
218 return builder.toString();
219 }
220
221 public byte[] getAddress() {
222 return this.address;
223 }
224}