Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onlab.packet.ipv6; |
| 18 | |
| 19 | import org.onlab.packet.BasePacket; |
| 20 | import org.onlab.packet.Data; |
| 21 | import org.onlab.packet.IPacket; |
| 22 | import org.onlab.packet.IPv6; |
| 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.Arrays; |
| 26 | |
| 27 | /** |
| 28 | * Implements IPv6 routing extension header format. (RFC 2460) |
| 29 | */ |
| 30 | public class Routing extends BasePacket implements IExtensionHeader { |
| 31 | public static final byte FIXED_HEADER_LENGTH = 4; // bytes |
| 32 | public static final byte FIXED_ROUTING_DATA_LENGTH = 4; // bytes |
| 33 | public static final byte LENGTH_UNIT = 8; // bytes per unit |
| 34 | |
| 35 | protected byte nextHeader; |
| 36 | protected byte headerExtLength; |
| 37 | protected byte routingType; |
| 38 | protected byte segmentsLeft; |
| 39 | protected byte[] routingData; |
| 40 | |
| 41 | @Override |
| 42 | public byte getNextHeader() { |
| 43 | return this.nextHeader; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Routing setNextHeader(final byte nextHeader) { |
| 48 | this.nextHeader = nextHeader; |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets the extension length of this header. |
| 54 | * |
| 55 | * @return header length |
| 56 | */ |
| 57 | public byte getHeaderExtLength() { |
| 58 | return this.headerExtLength; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets the extension length of this header. |
| 63 | * |
| 64 | * @param headerExtLength the header length to set |
| 65 | * @return this |
| 66 | */ |
| 67 | public Routing setHeaderExtLength(final byte headerExtLength) { |
| 68 | this.headerExtLength = headerExtLength; |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Gets the routing type of this header. |
| 74 | * |
| 75 | * @return routing type |
| 76 | */ |
| 77 | public byte getRoutingType() { |
| 78 | return this.routingType; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the routing type of this header. |
| 83 | * |
| 84 | * @param routingType the routing type to set |
| 85 | * @return this |
| 86 | */ |
| 87 | public Routing setRoutingType(final byte routingType) { |
| 88 | this.routingType = routingType; |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets the number of remaining route segments of this header. |
| 94 | * |
| 95 | * @return number of remaining route segments |
| 96 | */ |
| 97 | public byte getSegmentsLeft() { |
| 98 | return this.segmentsLeft; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets the number of remaining route segments of this header. |
| 103 | * |
| 104 | * @param segmentsLeft the number of remaining route segments to set |
| 105 | * @return this |
| 106 | */ |
| 107 | public Routing setSegmntsLeft(final byte segmentsLeft) { |
| 108 | this.segmentsLeft = segmentsLeft; |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets the routing data. |
| 114 | * |
| 115 | * @return the routing data |
| 116 | */ |
| 117 | public byte[] getRoutingData() { |
| 118 | return this.routingData; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the routing data. |
| 123 | * |
| 124 | * @param routingData the routing data to set |
| 125 | * @return this |
| 126 | */ |
| 127 | public Routing setRoutingData(final byte[] routingData) { |
| 128 | this.routingData = |
| 129 | Arrays.copyOfRange(routingData, 0, routingData.length); |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public byte[] serialize() { |
| 135 | byte[] payloadData = null; |
| 136 | if (this.payload != null) { |
| 137 | this.payload.setParent(this); |
| 138 | payloadData = this.payload.serialize(); |
| 139 | } |
| 140 | |
| 141 | int headerLength = FIXED_HEADER_LENGTH + routingData.length; |
| 142 | int payloadLength = 0; |
| 143 | if (payloadData != null) { |
| 144 | payloadLength = payloadData.length; |
| 145 | } |
| 146 | |
| 147 | final byte[] data = new byte[headerLength + payloadLength]; |
| 148 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 149 | |
| 150 | bb.put(this.nextHeader); |
| 151 | bb.put(this.headerExtLength); |
| 152 | bb.put(this.routingType); |
| 153 | bb.put(this.segmentsLeft); |
| 154 | bb.put(this.routingData, 0, routingData.length); |
| 155 | |
| 156 | if (payloadData != null) { |
| 157 | bb.put(payloadData); |
| 158 | } |
| 159 | |
| 160 | if (this.parent != null && this.parent instanceof IExtensionHeader) { |
| 161 | ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ROUTING); |
| 162 | } |
| 163 | return data; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 168 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 169 | this.nextHeader = bb.get(); |
| 170 | this.headerExtLength = bb.get(); |
| 171 | this.routingType = bb.get(); |
| 172 | this.segmentsLeft = bb.get(); |
| 173 | int dataLength = |
| 174 | FIXED_ROUTING_DATA_LENGTH + LENGTH_UNIT * this.headerExtLength; |
| 175 | this.routingData = new byte[dataLength]; |
| 176 | bb.get(this.routingData, 0, dataLength); |
| 177 | |
| 178 | IPacket payload; |
| 179 | if (IPv6.PROTOCOL_CLASS_MAP.containsKey(this.nextHeader)) { |
| 180 | final Class<? extends IPacket> clazz = IPv6.PROTOCOL_CLASS_MAP |
| 181 | .get(this.nextHeader); |
| 182 | try { |
| 183 | payload = clazz.newInstance(); |
| 184 | } catch (final Exception e) { |
| 185 | throw new RuntimeException( |
| 186 | "Error parsing payload for Routing packet", e); |
| 187 | } |
| 188 | } else { |
| 189 | payload = new Data(); |
| 190 | } |
| 191 | this.payload = payload.deserialize(data, bb.position(), |
| 192 | bb.limit() - bb.position()); |
| 193 | this.payload.setParent(this); |
| 194 | |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * (non-Javadoc) |
| 200 | * |
| 201 | * @see java.lang.Object#hashCode() |
| 202 | */ |
| 203 | @Override |
| 204 | public int hashCode() { |
| 205 | final int prime = 5807; |
| 206 | int result = super.hashCode(); |
| 207 | result = prime * result + this.nextHeader; |
| 208 | result = prime * result + this.headerExtLength; |
| 209 | result = prime * result + this.routingType; |
| 210 | result = prime * result + this.segmentsLeft; |
| 211 | for (byte b : this.routingData) { |
| 212 | result = prime * result + b; |
| 213 | } |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * (non-Javadoc) |
| 219 | * |
| 220 | * @see java.lang.Object#equals(java.lang.Object) |
| 221 | */ |
| 222 | @Override |
| 223 | public boolean equals(final Object obj) { |
| 224 | if (this == obj) { |
| 225 | return true; |
| 226 | } |
| 227 | if (!super.equals(obj)) { |
| 228 | return false; |
| 229 | } |
| 230 | if (!(obj instanceof Routing)) { |
| 231 | return false; |
| 232 | } |
| 233 | final Routing other = (Routing) obj; |
| 234 | if (this.nextHeader != other.nextHeader) { |
| 235 | return false; |
| 236 | } |
| 237 | if (this.headerExtLength != other.headerExtLength) { |
| 238 | return false; |
| 239 | } |
| 240 | if (this.routingType != other.routingType) { |
| 241 | return false; |
| 242 | } |
| 243 | if (this.segmentsLeft != other.segmentsLeft) { |
| 244 | return false; |
| 245 | } |
| 246 | if (!Arrays.equals(this.routingData, other.routingData)) { |
| 247 | return false; |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | } |