blob: d55000519415714dbee7d816f93ced4fbf768b57 [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 com.google.common.collect.Lists;
21import org.onlab.packet.DeserializationException;
22import org.onlab.packet.Deserializer;
23import org.onlab.packet.IpAddress;
24
25import java.nio.ByteBuffer;
26import java.util.List;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static org.onlab.packet.PacketUtils.checkHeaderLength;
30import static org.onlab.packet.PacketUtils.checkInput;
31
32/**
33 * Implements the IP Control Protocol (IPCP) packet format.
34 */
35
36public class Ipcp extends Ppp {
37
38 public static final byte CONF_REQ = 0x01;
39 public static final byte ACK = 0x02;
40 public static final byte NAK = 0x03;
41
42 private PppTlv ipaddresstlv = null;
43
44 private List<PppTlv> pppTlvList;
45
46 Ipcp() {
47 super();
48 pppTlvList = Lists.newLinkedList();
49 }
50
51 /**
52 * Gets the IP Address TLV field.
53 *
54 * @return the IP Address TLV field if present, null otherwise
55 */
56 public PppTlv getIpAddressTlv() {
57 return this.ipaddresstlv;
58 }
59
60 /**
61 * Gets the IP address TLV field as an IpAddress object.
62 *
63 * @return The IP address TLV field as IpAddress object, it will be address
64 * 0 if no TLV address is present in the packet
65 */
66 public IpAddress getIpAddress() {
67 return IpAddress.valueOf(IpAddress.Version.INET,
68 this.getIpAddressTlv().getValue());
69 }
70
71 /**
72 * Sets the IP address TLV field.
73 *
74 * @param ipaddresstlv the IP address TLV to set
75 * @return this
76 */
77 public Ipcp setIpAddressTlv(PppTlv ipaddresstlv) {
78 this.ipaddresstlv = ipaddresstlv;
79 return this;
80 }
81
82 /**
83 * Gets the TLV field list.
84 *
85 * @return the IPCP TLV field list
86 */
87 public List<PppTlv> getIpcpTlvList() {
88 return this.pppTlvList;
89 }
90
91 @Override
92 public byte[] serialize() {
93 // TODO: Can it have any payload?
94 final byte[] data = new byte[this.length];
95 final ByteBuffer bb = ByteBuffer.wrap(data);
96 bb.put(this.code);
97 bb.put(this.identifier);
98 bb.putShort(this.length);
99 if (ipaddresstlv != null) {
100 bb.put(ipaddresstlv.serialize());
101 }
102 if (this.pppTlvList != null) {
103 for (final PppTlv tlv : this.pppTlvList) {
104 bb.put(tlv.serialize());
105 }
106 }
107 return data;
108 }
109
110 /**
111 * Deserializer function for IPCP packets.
112 *
113 * @return deserializer function
114 */
115 public static Deserializer<Ipcp> deserializer() {
116 return (data, offset, length) -> {
117 checkInput(data, offset, length, MIN_HEADER_LENGTH);
118 Ipcp ipcp = new Ipcp();
119 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
120 ipcp.code = bb.get();
121 ipcp.identifier = bb.get();
122 ipcp.length = bb.getShort();
123 short currentIndex = MIN_HEADER_LENGTH;
124 PppTlv tlv;
125 while (currentIndex < ipcp.length) {
126 // Each new TLV IPCP must be a minimum of 2 bytes
127 // (containing the type and length fields).
128 currentIndex += 2;
129 checkHeaderLength(length, currentIndex);
130
131 tlv = (new PppTlv()).deserialize(bb);
132 // if there was a failure to deserialize stop processing TLVs
133 if (tlv == null) {
134 break;
135 }
136 if (tlv.getType() == PppTlv.IPCPTLV_IP_ADDRESS) {
137 ipcp.ipaddresstlv = tlv;
138 } else {
139 ipcp.pppTlvList.add(tlv);
140 }
141 currentIndex += tlv.getLength() - 2;
142 }
143 if (currentIndex != ipcp.length) {
144 throw new DeserializationException("Length of packet do not correspond to IPCP TLVs options");
145 }
146 return ipcp;
147 };
148 }
149
150 @Override
151 public int hashCode() {
152 return 31 * super.hashCode() + Objects.hashCode(ipaddresstlv);
153 }
154
155 @Override
156 public boolean equals(Object obj) {
157 if (this == obj) {
158 return true;
159 }
160 if (obj == null || getClass() != obj.getClass()) {
161 return false;
162 }
163 if (!super.equals(obj)) {
164 return false;
165 }
166 final Ipcp other = (Ipcp) obj;
167 return Objects.equal(this.ipaddresstlv, other.ipaddresstlv);
168 }
169
170 @Override
171 public String toString() {
172 return toStringHelper(this)
173 .add("code", code)
174 .add("identifier", identifier)
175 .add("length", length)
176 .add("pppTlvList", pppTlvList)
177 .add("ipaddresstlv", ipaddresstlv)
178 .toString();
179 }
180}