blob: 516938a4938d39dde88ed10133305067aae125bd [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;
22
Jonathan Hart4a60bb32015-06-30 15:31:20 -070023import static org.onlab.packet.PacketUtils.checkHeaderLength;
24import static org.onlab.packet.PacketUtils.checkInput;
Ari Saha79d7c252015-06-26 10:31:48 -070025
26/**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070027 * EAP (Extensible Authentication Protocol) packet.
Ari Saha79d7c252015-06-26 10:31:48 -070028 */
29public class EAP extends BasePacket {
Jonathan Hart4a60bb32015-06-30 15:31:20 -070030 private static final int HEADER_LENGTH = 4;
31
Ari Saha79d7c252015-06-26 10:31:48 -070032 public static final short MIN_LEN = 0x4;
33 public static final short EAP_HDR_LEN_REQ_RESP = 5;
34 public static final short EAP_HDR_LEN_SUC_FAIL = 4;
35
Jonathan Hart4a60bb32015-06-30 15:31:20 -070036 // EAP Code
Ari Saha79d7c252015-06-26 10:31:48 -070037 public static final byte REQUEST = 0x1;
38 public static final byte RESPONSE = 0x2;
39 public static final byte SUCCESS = 0x3;
40 public static final byte FAILURE = 0x4;
41
Jonathan Hart4a60bb32015-06-30 15:31:20 -070042 // EAP Attribute Type
Ari Saha79d7c252015-06-26 10:31:48 -070043 public static final byte ATTR_IDENTITY = 0x1;
44 public static final byte ATTR_NOTIFICATION = 0x2;
45 public static final byte ATTR_NAK = 0x3;
46 public static final byte ATTR_MD5 = 0x4;
47 public static final byte ATTR_OTP = 0x5;
48 public static final byte ATTR_GTC = 0x6;
49 public static final byte ATTR_TLS = 0xd;
50
51 protected byte code;
52 protected byte identifier;
53 protected short length;
54 protected byte type;
55 protected byte[] data;
56
57
58 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070059 * Gets the EAP code.
60 *
Ari Saha79d7c252015-06-26 10:31:48 -070061 * @return EAP code
62 */
63 public byte getCode() {
64 return this.code;
65 }
66
67
68 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070069 * Sets the EAP code.
70 *
Ari Saha79d7c252015-06-26 10:31:48 -070071 * @param code EAP code
72 * @return this
73 */
74 public EAP setCode(final byte code) {
75 this.code = code;
76 return this;
77 }
78
79 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070080 * Gets the EAP identifier.
81 *
Ari Saha79d7c252015-06-26 10:31:48 -070082 * @return EAP identifier
83 */
84 public byte getIdentifier() {
85 return this.identifier;
86 }
87
88 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070089 * Sets the EAP identifier.
90 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -070091 * @param identifier EAP identifier
Ari Saha79d7c252015-06-26 10:31:48 -070092 * @return this
93 */
94 public EAP setIdentifier(final byte identifier) {
95 this.identifier = identifier;
96 return this;
97 }
98
99 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700100 * Gets the get packet length.
101 *
Ari Saha79d7c252015-06-26 10:31:48 -0700102 * @return packet length
103 */
104 public short getLength() {
105 return this.length;
106 }
107
108 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700109 * Sets the packet length.
110 *
Ari Saha79d7c252015-06-26 10:31:48 -0700111 * @param length packet length
112 * @return this
113 */
114 public EAP setLength(final short length) {
115 this.length = length;
116 return this;
117 }
118
119 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700120 * Gets the data type.
121 *
Ari Saha79d7c252015-06-26 10:31:48 -0700122 * @return data type
123 */
124 public byte getDataType() {
125 return this.type;
126 }
127
128 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700129 * Sets the data type.
130 *
Ari Saha79d7c252015-06-26 10:31:48 -0700131 * @param type data type
132 * @return this
133 */
134 public EAP setDataType(final byte type) {
135 this.type = type;
136 return this;
137 }
138
139 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700140 * Gets the EAP data.
141 *
Ari Saha79d7c252015-06-26 10:31:48 -0700142 * @return EAP data
143 */
144 public byte[] getData() {
145 return this.data;
146 }
147
148 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700149 * Sets the EAP data.
150 *
Ari Saha79d7c252015-06-26 10:31:48 -0700151 * @param data EAP data to be set
152 * @return this
153 */
154 public EAP setData(final byte[] data) {
155 this.data = data;
156 return this;
157 }
158
159 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700160 * Default EAP constructor that sets the EAP code to 0.
Ari Saha79d7c252015-06-26 10:31:48 -0700161 */
162 public EAP() {
163 this.code = 0;
164 }
165
166 /**
167 * EAP constructor that initially sets all fields.
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700168 *
Ari Saha79d7c252015-06-26 10:31:48 -0700169 * @param code EAP code
170 * @param identifier EAP identifier
171 * @param type packet type
172 * @param data EAP data
173 */
174 public EAP(byte code, byte identifier, byte type, byte[] data) {
175 this.code = code;
176 this.identifier = identifier;
177 if (this.code == REQUEST || this.code == RESPONSE) {
178 this.length = (short) (5 + (data == null ? 0 : data.length));
179 this.type = type;
180 } else {
181 this.length = (short) (4 + (data == null ? 0 : data.length));
182 }
183 this.data = data;
184 }
185
186 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700187 * Deserializer for EAP packets.
188 *
189 * @return deserializer
Ari Saha79d7c252015-06-26 10:31:48 -0700190 */
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700191 public static Deserializer<EAP> deserializer() {
192 return (data, offset, length) -> {
193 checkInput(data, offset, length, HEADER_LENGTH);
194
195 EAP eap = new EAP();
196 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
197 eap.code = bb.get();
198 eap.identifier = bb.get();
199 eap.length = bb.getShort();
200
Chip Bolinge48c2d32015-07-27 10:41:44 -0500201 checkHeaderLength(length, eap.length);
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700202
203 int dataLength;
204 if (eap.code == REQUEST || eap.code == RESPONSE) {
205 eap.type = bb.get();
206 dataLength = eap.length - 5;
207 } else {
208 dataLength = eap.length - 4;
209 }
210
211 eap.data = new byte[dataLength];
212 bb.get(eap.data);
213 return eap;
214 };
215 }
216
Ari Saha79d7c252015-06-26 10:31:48 -0700217 @Override
218 public byte[] serialize() {
219 final byte[] data = new byte[this.length];
220
221 final ByteBuffer bb = ByteBuffer.wrap(data);
222 bb.put(this.code);
223 bb.put(this.identifier);
224 bb.putShort(this.length);
225 if (this.code == REQUEST || this.code == RESPONSE) {
226 bb.put(this.type);
227 }
228 if (this.data != null) {
229 bb.put(this.data);
230 }
231 return data;
232 }
233
234 @Override
235 public IPacket deserialize(final byte[] data, final int offset,
236 final int length) {
237 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
238 this.code = bb.get();
239 this.identifier = bb.get();
240 this.length = bb.getShort();
241
242 int dataLength;
243 if (this.code == REQUEST || this.code == RESPONSE) {
244 this.type = bb.get();
245 dataLength = this.length - 5;
246 } else {
247 dataLength = this.length - 4;
248 }
249 this.data = new byte[dataLength];
250 bb.get(this.data);
251 return this;
252 }
253
254 @Override
255 public int hashCode() {
256 final int prime = 3889;
257 int result = super.hashCode();
258 result = prime * result + this.code;
259 result = prime * result + this.identifier;
260 result = prime * result + this.length;
261 result = prime * result + this.type;
262 return result;
263 }
264}