Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 23 | import static org.onlab.packet.PacketUtils.checkHeaderLength; |
| 24 | import static org.onlab.packet.PacketUtils.checkInput; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 27 | * EAP (Extensible Authentication Protocol) packet. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 28 | */ |
| 29 | public class EAP extends BasePacket { |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 30 | private static final int HEADER_LENGTH = 4; |
| 31 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 32 | 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 36 | // EAP Code |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 37 | 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 42 | // EAP Attribute Type |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 43 | 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 59 | * Gets the EAP code. |
| 60 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 61 | * @return EAP code |
| 62 | */ |
| 63 | public byte getCode() { |
| 64 | return this.code; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 69 | * Sets the EAP code. |
| 70 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 71 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 80 | * Gets the EAP identifier. |
| 81 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 82 | * @return EAP identifier |
| 83 | */ |
| 84 | public byte getIdentifier() { |
| 85 | return this.identifier; |
| 86 | } |
| 87 | |
| 88 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 89 | * Sets the EAP identifier. |
| 90 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame^] | 91 | * @param identifier EAP identifier |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 92 | * @return this |
| 93 | */ |
| 94 | public EAP setIdentifier(final byte identifier) { |
| 95 | this.identifier = identifier; |
| 96 | return this; |
| 97 | } |
| 98 | |
| 99 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 100 | * Gets the get packet length. |
| 101 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 102 | * @return packet length |
| 103 | */ |
| 104 | public short getLength() { |
| 105 | return this.length; |
| 106 | } |
| 107 | |
| 108 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 109 | * Sets the packet length. |
| 110 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 111 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 120 | * Gets the data type. |
| 121 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 122 | * @return data type |
| 123 | */ |
| 124 | public byte getDataType() { |
| 125 | return this.type; |
| 126 | } |
| 127 | |
| 128 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 129 | * Sets the data type. |
| 130 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 131 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 140 | * Gets the EAP data. |
| 141 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 142 | * @return EAP data |
| 143 | */ |
| 144 | public byte[] getData() { |
| 145 | return this.data; |
| 146 | } |
| 147 | |
| 148 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 149 | * Sets the EAP data. |
| 150 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 151 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 160 | * Default EAP constructor that sets the EAP code to 0. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 161 | */ |
| 162 | public EAP() { |
| 163 | this.code = 0; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * EAP constructor that initially sets all fields. |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 168 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 169 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 187 | * Deserializer for EAP packets. |
| 188 | * |
| 189 | * @return deserializer |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 190 | */ |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 191 | 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 Boling | e48c2d3 | 2015-07-27 10:41:44 -0500 | [diff] [blame] | 201 | checkHeaderLength(length, eap.length); |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 202 | |
| 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 Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 217 | @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 | } |