blob: cc658b775cf3b543e2714ce71ccf1389fd8c0417 [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Charles M.C. Chan94f37372015-01-10 17:53:42 +08003 *
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.onlab.packet.ipv6;
18
19import org.onlab.packet.BasePacket;
20import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070021import org.onlab.packet.DeserializationException;
22import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080023import org.onlab.packet.IPacket;
24import org.onlab.packet.IPv6;
25
26import java.nio.ByteBuffer;
27import java.util.Arrays;
28
Jian Li5fc14292015-12-04 11:30:46 -080029import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070030import static org.onlab.packet.PacketUtils.checkHeaderLength;
31import static org.onlab.packet.PacketUtils.checkInput;
32
Charles M.C. Chan94f37372015-01-10 17:53:42 +080033/**
34 * Implements IPv6 routing extension header format. (RFC 2460)
35 */
36public class Routing extends BasePacket implements IExtensionHeader {
37 public static final byte FIXED_HEADER_LENGTH = 4; // bytes
38 public static final byte FIXED_ROUTING_DATA_LENGTH = 4; // bytes
39 public static final byte LENGTH_UNIT = 8; // bytes per unit
40
41 protected byte nextHeader;
42 protected byte headerExtLength;
43 protected byte routingType;
44 protected byte segmentsLeft;
45 protected byte[] routingData;
46
47 @Override
48 public byte getNextHeader() {
49 return this.nextHeader;
50 }
51
52 @Override
53 public Routing setNextHeader(final byte nextHeader) {
54 this.nextHeader = nextHeader;
55 return this;
56 }
57
58 /**
59 * Gets the extension length of this header.
60 *
61 * @return header length
62 */
63 public byte getHeaderExtLength() {
64 return this.headerExtLength;
65 }
66
67 /**
68 * Sets the extension length of this header.
69 *
70 * @param headerExtLength the header length to set
71 * @return this
72 */
73 public Routing setHeaderExtLength(final byte headerExtLength) {
74 this.headerExtLength = headerExtLength;
75 return this;
76 }
77
78 /**
79 * Gets the routing type of this header.
80 *
81 * @return routing type
82 */
83 public byte getRoutingType() {
84 return this.routingType;
85 }
86
87 /**
88 * Sets the routing type of this header.
89 *
90 * @param routingType the routing type to set
91 * @return this
92 */
93 public Routing setRoutingType(final byte routingType) {
94 this.routingType = routingType;
95 return this;
96 }
97
98 /**
99 * Gets the number of remaining route segments of this header.
100 *
101 * @return number of remaining route segments
102 */
103 public byte getSegmentsLeft() {
104 return this.segmentsLeft;
105 }
106
107 /**
108 * Sets the number of remaining route segments of this header.
109 *
110 * @param segmentsLeft the number of remaining route segments to set
111 * @return this
112 */
113 public Routing setSegmntsLeft(final byte segmentsLeft) {
114 this.segmentsLeft = segmentsLeft;
115 return this;
116 }
117
118 /**
119 * Gets the routing data.
120 *
121 * @return the routing data
122 */
123 public byte[] getRoutingData() {
124 return this.routingData;
125 }
126
127 /**
128 * Sets the routing data.
129 *
130 * @param routingData the routing data to set
131 * @return this
132 */
133 public Routing setRoutingData(final byte[] routingData) {
134 this.routingData =
135 Arrays.copyOfRange(routingData, 0, routingData.length);
136 return this;
137 }
138
139 @Override
140 public byte[] serialize() {
141 byte[] payloadData = null;
142 if (this.payload != null) {
143 this.payload.setParent(this);
144 payloadData = this.payload.serialize();
145 }
146
147 int headerLength = FIXED_HEADER_LENGTH + routingData.length;
148 int payloadLength = 0;
149 if (payloadData != null) {
150 payloadLength = payloadData.length;
151 }
152
153 final byte[] data = new byte[headerLength + payloadLength];
154 final ByteBuffer bb = ByteBuffer.wrap(data);
155
156 bb.put(this.nextHeader);
157 bb.put(this.headerExtLength);
158 bb.put(this.routingType);
159 bb.put(this.segmentsLeft);
160 bb.put(this.routingData, 0, routingData.length);
161
162 if (payloadData != null) {
163 bb.put(payloadData);
164 }
165
166 if (this.parent != null && this.parent instanceof IExtensionHeader) {
167 ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ROUTING);
168 }
169 return data;
170 }
171
172 @Override
173 public IPacket deserialize(byte[] data, int offset, int length) {
174 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
175 this.nextHeader = bb.get();
176 this.headerExtLength = bb.get();
177 this.routingType = bb.get();
178 this.segmentsLeft = bb.get();
179 int dataLength =
180 FIXED_ROUTING_DATA_LENGTH + LENGTH_UNIT * this.headerExtLength;
181 this.routingData = new byte[dataLength];
182 bb.get(this.routingData, 0, dataLength);
183
Jonathan Hart2a655752015-04-07 16:46:33 -0700184 Deserializer<? extends IPacket> deserializer;
185 if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(this.nextHeader)) {
186 deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(this.nextHeader);
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800187 } else {
Jonathan Hart2a655752015-04-07 16:46:33 -0700188 deserializer = new Data().deserializer();
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800189 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700190 try {
191 this.payload = deserializer.deserialize(data, bb.position(),
192 bb.limit() - bb.position());
193 this.payload.setParent(this);
194 } catch (DeserializationException e) {
195 return this;
196 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800197
198 return this;
199 }
200
201 /*
202 * (non-Javadoc)
203 *
204 * @see java.lang.Object#hashCode()
205 */
206 @Override
207 public int hashCode() {
208 final int prime = 5807;
209 int result = super.hashCode();
210 result = prime * result + this.nextHeader;
211 result = prime * result + this.headerExtLength;
212 result = prime * result + this.routingType;
213 result = prime * result + this.segmentsLeft;
214 for (byte b : this.routingData) {
215 result = prime * result + b;
216 }
217 return result;
218 }
219
220 /*
221 * (non-Javadoc)
222 *
223 * @see java.lang.Object#equals(java.lang.Object)
224 */
225 @Override
226 public boolean equals(final Object obj) {
227 if (this == obj) {
228 return true;
229 }
230 if (!super.equals(obj)) {
231 return false;
232 }
233 if (!(obj instanceof Routing)) {
234 return false;
235 }
236 final Routing other = (Routing) obj;
237 if (this.nextHeader != other.nextHeader) {
238 return false;
239 }
240 if (this.headerExtLength != other.headerExtLength) {
241 return false;
242 }
243 if (this.routingType != other.routingType) {
244 return false;
245 }
246 if (this.segmentsLeft != other.segmentsLeft) {
247 return false;
248 }
249 if (!Arrays.equals(this.routingData, other.routingData)) {
250 return false;
251 }
252 return true;
253 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700254
255 /**
256 * Deserializer function for routing headers.
257 *
258 * @return deserializer function
259 */
260 public static Deserializer<Routing> deserializer() {
261 return (data, offset, length) -> {
262 checkInput(data, offset, length, FIXED_HEADER_LENGTH);
263
264 Routing routing = new Routing();
265
266 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
267 routing.nextHeader = bb.get();
268 routing.headerExtLength = bb.get();
269 routing.routingType = bb.get();
270 routing.segmentsLeft = bb.get();
271 int dataLength =
272 FIXED_ROUTING_DATA_LENGTH + LENGTH_UNIT * routing.headerExtLength;
273
274 checkHeaderLength(bb.remaining(), dataLength);
275
276 routing.routingData = new byte[dataLength];
277 bb.get(routing.routingData, 0, dataLength);
278
279 Deserializer<? extends IPacket> deserializer;
280 if (IPv6.PROTOCOL_DESERIALIZER_MAP.containsKey(routing.nextHeader)) {
281 deserializer = IPv6.PROTOCOL_DESERIALIZER_MAP.get(routing.nextHeader);
282 } else {
283 deserializer = new Data().deserializer();
284 }
285 routing.payload = deserializer.deserialize(data, bb.position(),
286 bb.limit() - bb.position());
287 routing.payload.setParent(routing);
288
289 return routing;
290 };
291 }
Jian Li5fc14292015-12-04 11:30:46 -0800292
293 @Override
294 public String toString() {
295 return toStringHelper(getClass())
296 .add("nextHeader", Byte.toString(nextHeader))
297 .add("headerExtLength", Byte.toString(headerExtLength))
298 .add("routingType", Byte.toString(routingType))
299 .add("segmentsLeft", Byte.toString(segmentsLeft))
300 .add("routingData", Arrays.toString(routingData))
301 .toString();
302 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700303}