blob: 464bf2d7c9ea8f31f7a8cce7207ad22b330f5b7e [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.opencord.bng.packets;
18
19import com.google.common.base.Objects;
20import org.onlab.packet.Data;
21import org.onlab.packet.Deserializer;
22
23import java.nio.ByteBuffer;
24
25import static org.onlab.packet.PacketUtils.checkInput;
26
27/**
28 * Implements the Link Control Protocol (LCP) header.
29 */
30
31public class GenericPpp extends Ppp {
32
33 // TODO: Add support for TLV options.
34
35 public static final byte CHAP_CODE_CHALLENGE = 0x01;
36 public static final byte CHAP_CODE_RESPONSE = 0x02;
37 public static final byte CHAP_CODE_SUCCESS = 0x03;
38 public static final byte CHAP_CODE_FAILURE = 0x04;
39
40 public static final byte PAP_AUTH_REQ = 0x01;
41 public static final byte PAP_AUTH_ACK = 0x02;
42 public static final byte PAP_AUTH_NACK = 0x03;
43
44 public static final byte CODE_TERM_REQ = 0x05;
45 public static final byte CODE_TERM_ACK = 0x06;
46
47
48 /**
49 * Deserializer function for LCP packets.
50 *
51 * @return deserializer function
52 */
53 public static Deserializer<GenericPpp> deserializer() {
54 return (data, offset, length) -> {
55 checkInput(data, offset, length, MIN_HEADER_LENGTH);
56 GenericPpp ppp = new GenericPpp();
57 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
58 ppp.code = bb.get();
59 ppp.identifier = bb.get();
60 ppp.length = bb.getShort();
61 if (ppp.length > MIN_HEADER_LENGTH) {
62 ppp.payload = Data.deserializer()
63 .deserialize(data, bb.position(),
64 Math.min(bb.limit() - bb.position(),
65 ppp.length - MIN_HEADER_LENGTH));
66 ppp.payload.setParent(ppp);
67 }
68 return ppp;
69 };
70 }
71
72 @Override
73 public byte[] serialize() {
74 byte[] payloadData = null;
75 int payloadLength = 0;
76 if (this.payload != null) {
77 this.payload.setParent(this);
78 payloadData = this.payload.serialize();
79 payloadLength = payloadData.length + MIN_HEADER_LENGTH;
80 }
81 int realLength = this.length > payloadLength ? this.length : payloadLength;
82 final byte[] data = new byte[realLength];
83 final ByteBuffer bb = ByteBuffer.wrap(data);
84 bb.put(this.code);
85 bb.put(this.identifier);
86 bb.putShort(this.length);
87 if (payloadData != null) {
88 bb.put(payloadData);
89 }
90 return data;
91 }
92
93 @Override
94 public int hashCode() {
95 return 31 * super.hashCode() + Objects.hashCode(code, identifier, length);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
103 if (obj == null || getClass() != obj.getClass()) {
104 return false;
105 }
106 if (!super.equals(obj)) {
107 return false;
108 }
109 final GenericPpp other = (GenericPpp) obj;
110 return Objects.equal(this.code, other.code)
111 && Objects.equal(this.identifier, other.identifier)
112 && Objects.equal(this.length, other.length);
113 }
114}
115