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