blob: ed2718e9be5340513cf8e239eff6c14b466043f6 [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
19package org.onosproject.aaa.packet;
20
21import org.onlab.packet.BasePacket;
22import org.onlab.packet.IPacket;
23
24import java.nio.ByteBuffer;
25
26
27/**
28 *
29 */
30public class EAP extends BasePacket {
31 public static final short MIN_LEN = 0x4;
32 public static final short EAP_HDR_LEN_REQ_RESP = 5;
33 public static final short EAP_HDR_LEN_SUC_FAIL = 4;
34
35 /* EAP Code */
36 public static final byte REQUEST = 0x1;
37 public static final byte RESPONSE = 0x2;
38 public static final byte SUCCESS = 0x3;
39 public static final byte FAILURE = 0x4;
40
41 /* EAP Attribute Type */
42 public static final byte ATTR_IDENTITY = 0x1;
43 public static final byte ATTR_NOTIFICATION = 0x2;
44 public static final byte ATTR_NAK = 0x3;
45 public static final byte ATTR_MD5 = 0x4;
46 public static final byte ATTR_OTP = 0x5;
47 public static final byte ATTR_GTC = 0x6;
48 public static final byte ATTR_TLS = 0xd;
49
50 protected byte code;
51 protected byte identifier;
52 protected short length;
53 protected byte type;
54 protected byte[] data;
55
56
57 /**
58 * Get the EAP code.
59 * @return EAP code
60 */
61 public byte getCode() {
62 return this.code;
63 }
64
65
66 /**
67 * Set the EAP code.
68 * @param code EAP code
69 * @return this
70 */
71 public EAP setCode(final byte code) {
72 this.code = code;
73 return this;
74 }
75
76 /**
77 * Get the EAP identifier.
78 * @return EAP identifier
79 */
80 public byte getIdentifier() {
81 return this.identifier;
82 }
83
84 /**
85 * Set the EAP identifier.
86 * @param identifier
87 * @return this
88 */
89 public EAP setIdentifier(final byte identifier) {
90 this.identifier = identifier;
91 return this;
92 }
93
94 /**
95 * Get the get packet length.
96 * @return packet length
97 */
98 public short getLength() {
99 return this.length;
100 }
101
102 /**
103 * Set the packet length.
104 * @param length packet length
105 * @return this
106 */
107 public EAP setLength(final short length) {
108 this.length = length;
109 return this;
110 }
111
112 /**
113 * Get the data type.
114 * @return data type
115 */
116 public byte getDataType() {
117 return this.type;
118 }
119
120 /**
121 * Set the data type.
122 * @param type data type
123 * @return this
124 */
125 public EAP setDataType(final byte type) {
126 this.type = type;
127 return this;
128 }
129
130 /**
131 * Get the EAP data.
132 * @return EAP data
133 */
134 public byte[] getData() {
135 return this.data;
136 }
137
138 /**
139 * Set the EAP data.
140 * @param data EAP data to be set
141 * @return this
142 */
143 public EAP setData(final byte[] data) {
144 this.data = data;
145 return this;
146 }
147
148 /**
149 * Default EAP constructor that set the EAP code to 0.
150 */
151 public EAP() {
152 this.code = 0;
153 }
154
155 /**
156 * EAP constructor that initially sets all fields.
157 * @param code EAP code
158 * @param identifier EAP identifier
159 * @param type packet type
160 * @param data EAP data
161 */
162 public EAP(byte code, byte identifier, byte type, byte[] data) {
163 this.code = code;
164 this.identifier = identifier;
165 if (this.code == REQUEST || this.code == RESPONSE) {
166 this.length = (short) (5 + (data == null ? 0 : data.length));
167 this.type = type;
168 } else {
169 this.length = (short) (4 + (data == null ? 0 : data.length));
170 }
171 this.data = data;
172 }
173
174 /**
175 * Serializes the packet, based on the code/type using the payload
176 * to compute its length.
177 * @return the serialized payload
178 */
179 @Override
180 public byte[] serialize() {
181 final byte[] data = new byte[this.length];
182
183 final ByteBuffer bb = ByteBuffer.wrap(data);
184 bb.put(this.code);
185 bb.put(this.identifier);
186 bb.putShort(this.length);
187 if (this.code == REQUEST || this.code == RESPONSE) {
188 bb.put(this.type);
189 }
190 if (this.data != null) {
191 bb.put(this.data);
192 }
193 return data;
194 }
195
196 @Override
197 public IPacket deserialize(final byte[] data, final int offset,
198 final int length) {
199 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
200 this.code = bb.get();
201 this.identifier = bb.get();
202 this.length = bb.getShort();
203
204 int dataLength;
205 if (this.code == REQUEST || this.code == RESPONSE) {
206 this.type = bb.get();
207 dataLength = this.length - 5;
208 } else {
209 dataLength = this.length - 4;
210 }
211 this.data = new byte[dataLength];
212 bb.get(this.data);
213 return this;
214 }
215
216 @Override
217 public int hashCode() {
218 final int prime = 3889;
219 int result = super.hashCode();
220 result = prime * result + this.code;
221 result = prime * result + this.identifier;
222 result = prime * result + this.length;
223 result = prime * result + this.type;
224 return result;
225 }
226}