blob: bf53671fed9278149e18f2b09fc6c6c4f8c34848 [file] [log] [blame]
Ari Saha79d7c252015-06-26 10:31:48 -07001/*
2 *
3 * * Copyright 2015 AT&T Foundry
4 * *
5 * * Licensed under the Apache License, Version 2.0 (the "License");
6 * * you may not use this file except in compliance with the License.
7 * * You may obtain a copy of the License at
8 * *
9 * * http://www.apache.org/licenses/LICENSE-2.0
10 * *
11 * * Unless required by applicable law or agreed to in writing, software
12 * * distributed under the License is distributed on an "AS IS" BASIS,
13 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * * See the License for the specific language governing permissions and
15 * * limitations under the License.
16 *
17 */
18
Jonathan Hart4a60bb32015-06-30 15:31:20 -070019package org.onlab.packet;
Ari Saha79d7c252015-06-26 10:31:48 -070020
21import java.nio.ByteBuffer;
Jian Li5fc14292015-12-04 11:30:46 -080022import java.util.Arrays;
Ari Saha79d7c252015-06-26 10:31:48 -070023
Jian Li5fc14292015-12-04 11:30:46 -080024import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart4a60bb32015-06-30 15:31:20 -070025import static org.onlab.packet.PacketUtils.checkHeaderLength;
26import static org.onlab.packet.PacketUtils.checkInput;
Ari Saha79d7c252015-06-26 10:31:48 -070027
28/**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070029 * EAP (Extensible Authentication Protocol) packet.
Ari Saha79d7c252015-06-26 10:31:48 -070030 */
31public class EAP extends BasePacket {
Jonathan Hart4a60bb32015-06-30 15:31:20 -070032 private static final int HEADER_LENGTH = 4;
33
Ari Saha79d7c252015-06-26 10:31:48 -070034 public static final short MIN_LEN = 0x4;
35 public static final short EAP_HDR_LEN_REQ_RESP = 5;
36 public static final short EAP_HDR_LEN_SUC_FAIL = 4;
37
Jonathan Hart4a60bb32015-06-30 15:31:20 -070038 // EAP Code
Ari Saha79d7c252015-06-26 10:31:48 -070039 public static final byte REQUEST = 0x1;
40 public static final byte RESPONSE = 0x2;
41 public static final byte SUCCESS = 0x3;
42 public static final byte FAILURE = 0x4;
43
Jonathan Hart4a60bb32015-06-30 15:31:20 -070044 // EAP Attribute Type
Ari Saha79d7c252015-06-26 10:31:48 -070045 public static final byte ATTR_IDENTITY = 0x1;
46 public static final byte ATTR_NOTIFICATION = 0x2;
47 public static final byte ATTR_NAK = 0x3;
48 public static final byte ATTR_MD5 = 0x4;
49 public static final byte ATTR_OTP = 0x5;
50 public static final byte ATTR_GTC = 0x6;
51 public static final byte ATTR_TLS = 0xd;
52
53 protected byte code;
54 protected byte identifier;
55 protected short length;
56 protected byte type;
57 protected byte[] data;
58
Ari Saha79d7c252015-06-26 10:31:48 -070059 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070060 * Gets the EAP code.
61 *
Ari Saha79d7c252015-06-26 10:31:48 -070062 * @return EAP code
63 */
64 public byte getCode() {
65 return this.code;
66 }
67
68
69 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070070 * Sets the EAP code.
71 *
Ari Saha79d7c252015-06-26 10:31:48 -070072 * @param code EAP code
73 * @return this
74 */
75 public EAP setCode(final byte code) {
76 this.code = code;
77 return this;
78 }
79
80 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070081 * Gets the EAP identifier.
82 *
Ari Saha79d7c252015-06-26 10:31:48 -070083 * @return EAP identifier
84 */
85 public byte getIdentifier() {
86 return this.identifier;
87 }
88
89 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070090 * Sets the EAP identifier.
91 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -070092 * @param identifier EAP identifier
Ari Saha79d7c252015-06-26 10:31:48 -070093 * @return this
94 */
95 public EAP setIdentifier(final byte identifier) {
96 this.identifier = identifier;
97 return this;
98 }
99
100 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700101 * Gets the get packet length.
102 *
Ari Saha79d7c252015-06-26 10:31:48 -0700103 * @return packet length
104 */
105 public short getLength() {
106 return this.length;
107 }
108
109 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700110 * Sets the packet length.
111 *
Ari Saha79d7c252015-06-26 10:31:48 -0700112 * @param length packet length
113 * @return this
114 */
115 public EAP setLength(final short length) {
116 this.length = length;
117 return this;
118 }
119
120 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700121 * Gets the data type.
122 *
Ari Saha79d7c252015-06-26 10:31:48 -0700123 * @return data type
124 */
125 public byte getDataType() {
126 return this.type;
127 }
128
129 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700130 * Sets the data type.
131 *
Ari Saha79d7c252015-06-26 10:31:48 -0700132 * @param type data type
133 * @return this
134 */
135 public EAP setDataType(final byte type) {
136 this.type = type;
137 return this;
138 }
139
140 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700141 * Gets the EAP data.
142 *
Ari Saha79d7c252015-06-26 10:31:48 -0700143 * @return EAP data
144 */
145 public byte[] getData() {
146 return this.data;
147 }
148
149 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700150 * Sets the EAP data.
151 *
Ari Saha79d7c252015-06-26 10:31:48 -0700152 * @param data EAP data to be set
153 * @return this
154 */
155 public EAP setData(final byte[] data) {
156 this.data = data;
157 return this;
158 }
159
160 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700161 * Default EAP constructor that sets the EAP code to 0.
Ari Saha79d7c252015-06-26 10:31:48 -0700162 */
163 public EAP() {
164 this.code = 0;
165 }
166
167 /**
168 * EAP constructor that initially sets all fields.
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700169 *
Ari Saha79d7c252015-06-26 10:31:48 -0700170 * @param code EAP code
171 * @param identifier EAP identifier
172 * @param type packet type
173 * @param data EAP data
174 */
175 public EAP(byte code, byte identifier, byte type, byte[] data) {
176 this.code = code;
177 this.identifier = identifier;
178 if (this.code == REQUEST || this.code == RESPONSE) {
179 this.length = (short) (5 + (data == null ? 0 : data.length));
180 this.type = type;
181 } else {
182 this.length = (short) (4 + (data == null ? 0 : data.length));
183 }
184 this.data = data;
185 }
186
187 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700188 * Deserializer for EAP packets.
189 *
190 * @return deserializer
Ari Saha79d7c252015-06-26 10:31:48 -0700191 */
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700192 public static Deserializer<EAP> deserializer() {
193 return (data, offset, length) -> {
194 checkInput(data, offset, length, HEADER_LENGTH);
195
196 EAP eap = new EAP();
197 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
198 eap.code = bb.get();
199 eap.identifier = bb.get();
200 eap.length = bb.getShort();
201
Chip Bolinge48c2d32015-07-27 10:41:44 -0500202 checkHeaderLength(length, eap.length);
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700203
204 int dataLength;
205 if (eap.code == REQUEST || eap.code == RESPONSE) {
206 eap.type = bb.get();
207 dataLength = eap.length - 5;
208 } else {
209 dataLength = eap.length - 4;
210 }
211
212 eap.data = new byte[dataLength];
213 bb.get(eap.data);
214 return eap;
215 };
216 }
217
Ari Saha79d7c252015-06-26 10:31:48 -0700218 @Override
219 public byte[] serialize() {
220 final byte[] data = new byte[this.length];
221
222 final ByteBuffer bb = ByteBuffer.wrap(data);
223 bb.put(this.code);
224 bb.put(this.identifier);
225 bb.putShort(this.length);
226 if (this.code == REQUEST || this.code == RESPONSE) {
227 bb.put(this.type);
228 }
229 if (this.data != null) {
230 bb.put(this.data);
231 }
232 return data;
233 }
234
235 @Override
236 public IPacket deserialize(final byte[] data, final int offset,
237 final int length) {
238 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
239 this.code = bb.get();
240 this.identifier = bb.get();
241 this.length = bb.getShort();
242
243 int dataLength;
244 if (this.code == REQUEST || this.code == RESPONSE) {
245 this.type = bb.get();
246 dataLength = this.length - 5;
247 } else {
248 dataLength = this.length - 4;
249 }
250 this.data = new byte[dataLength];
251 bb.get(this.data);
252 return this;
253 }
254
255 @Override
256 public int hashCode() {
257 final int prime = 3889;
258 int result = super.hashCode();
259 result = prime * result + this.code;
260 result = prime * result + this.identifier;
261 result = prime * result + this.length;
262 result = prime * result + this.type;
263 return result;
264 }
Jian Li5fc14292015-12-04 11:30:46 -0800265
266 @Override
Ray Milkey86f20cc2015-12-09 16:54:09 -0800267 public boolean equals(Object o) {
268 if (this == o) {
269 return true;
270 }
271 if (!(o instanceof EAP)) {
272 return false;
273 }
274 EAP that = (EAP) o;
275
276 if (this.code != that.code) {
277 return false;
278 }
279 if (this.identifier != that.identifier) {
280 return false;
281 }
282 if (this.length != that.length) {
283 return false;
284 }
285 if (this.type != that.type) {
286 return false;
287 }
288 return true;
289 }
290
291 @Override
Jian Li5fc14292015-12-04 11:30:46 -0800292 public String toString() {
293 return toStringHelper(getClass())
294 .add("code", Byte.toString(code))
295 .add("identifier", Byte.toString(identifier))
296 .add("length", Short.toString(length))
297 .add("type", Byte.toString(type))
298 .add("data", Arrays.toString(data))
299 .toString();
300 }
Ari Saha79d7c252015-06-26 10:31:48 -0700301}