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; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 22 | import java.util.Arrays; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 23 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 25 | import static org.onlab.packet.PacketUtils.checkHeaderLength; |
| 26 | import static org.onlab.packet.PacketUtils.checkInput; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 29 | * EAP (Extensible Authentication Protocol) packet. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 30 | */ |
| 31 | public class EAP extends BasePacket { |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 32 | private static final int HEADER_LENGTH = 4; |
| 33 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 34 | 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 38 | // EAP Code |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 39 | 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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 44 | // EAP Attribute Type |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 45 | 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 Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 59 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 60 | * Gets the EAP code. |
| 61 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 62 | * @return EAP code |
| 63 | */ |
| 64 | public byte getCode() { |
| 65 | return this.code; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 70 | * Sets the EAP code. |
| 71 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 72 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 81 | * Gets the EAP identifier. |
| 82 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 83 | * @return EAP identifier |
| 84 | */ |
| 85 | public byte getIdentifier() { |
| 86 | return this.identifier; |
| 87 | } |
| 88 | |
| 89 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 90 | * Sets the EAP identifier. |
| 91 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 92 | * @param identifier EAP identifier |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 93 | * @return this |
| 94 | */ |
| 95 | public EAP setIdentifier(final byte identifier) { |
| 96 | this.identifier = identifier; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 101 | * Gets the get packet length. |
| 102 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 103 | * @return packet length |
| 104 | */ |
| 105 | public short getLength() { |
| 106 | return this.length; |
| 107 | } |
| 108 | |
| 109 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 110 | * Sets the packet length. |
| 111 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 112 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 121 | * Gets the data type. |
| 122 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 123 | * @return data type |
| 124 | */ |
| 125 | public byte getDataType() { |
| 126 | return this.type; |
| 127 | } |
| 128 | |
| 129 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 130 | * Sets the data type. |
| 131 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 132 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 141 | * Gets the EAP data. |
| 142 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 143 | * @return EAP data |
| 144 | */ |
| 145 | public byte[] getData() { |
| 146 | return this.data; |
| 147 | } |
| 148 | |
| 149 | /** |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 150 | * Sets the EAP data. |
| 151 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 152 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 161 | * Default EAP constructor that sets the EAP code to 0. |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 162 | */ |
| 163 | public EAP() { |
| 164 | this.code = 0; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * EAP constructor that initially sets all fields. |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 169 | * |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 170 | * @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 Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 188 | * Deserializer for EAP packets. |
| 189 | * |
| 190 | * @return deserializer |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 191 | */ |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 192 | 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 Boling | e48c2d3 | 2015-07-27 10:41:44 -0500 | [diff] [blame] | 202 | checkHeaderLength(length, eap.length); |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 203 | |
| 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 Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 218 | @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 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 235 | |
| 236 | @Override |
| 237 | public int hashCode() { |
| 238 | final int prime = 3889; |
| 239 | int result = super.hashCode(); |
| 240 | result = prime * result + this.code; |
| 241 | result = prime * result + this.identifier; |
| 242 | result = prime * result + this.length; |
| 243 | result = prime * result + this.type; |
| 244 | return result; |
| 245 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 246 | |
| 247 | @Override |
Ray Milkey | 86f20cc | 2015-12-09 16:54:09 -0800 | [diff] [blame] | 248 | public boolean equals(Object o) { |
| 249 | if (this == o) { |
| 250 | return true; |
| 251 | } |
| 252 | if (!(o instanceof EAP)) { |
| 253 | return false; |
| 254 | } |
| 255 | EAP that = (EAP) o; |
| 256 | |
| 257 | if (this.code != that.code) { |
| 258 | return false; |
| 259 | } |
| 260 | if (this.identifier != that.identifier) { |
| 261 | return false; |
| 262 | } |
| 263 | if (this.length != that.length) { |
| 264 | return false; |
| 265 | } |
| 266 | if (this.type != that.type) { |
| 267 | return false; |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | @Override |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 273 | public String toString() { |
| 274 | return toStringHelper(getClass()) |
| 275 | .add("code", Byte.toString(code)) |
| 276 | .add("identifier", Byte.toString(identifier)) |
| 277 | .add("length", Short.toString(length)) |
| 278 | .add("type", Byte.toString(type)) |
| 279 | .add("data", Arrays.toString(data)) |
| 280 | .toString(); |
| 281 | } |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 282 | } |