Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 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 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
| 17 | |
| 18 | import org.onlab.packet.BasePacket; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 20 | import org.onlab.packet.IPacket; |
| 21 | import org.onlab.packet.Ip6Address; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Arrays; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 25 | import java.util.List; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 26 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 28 | import static org.onlab.packet.PacketUtils.checkInput; |
| 29 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 30 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 31 | * Implements ICMPv6 Redirect packet format. (RFC 4861) |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 32 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 33 | public class Redirect extends BasePacket { |
| 34 | public static final byte HEADER_LENGTH = 36; // bytes |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 35 | |
| 36 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 37 | protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 38 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 39 | private final NeighborDiscoveryOptions options = |
| 40 | new NeighborDiscoveryOptions(); |
| 41 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 42 | /** |
| 43 | * Gets target address. |
| 44 | * |
| 45 | * @return the target IPv6 address |
| 46 | */ |
| 47 | public byte[] getTargetAddress() { |
| 48 | return this.targetAddress; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Sets target address. |
| 53 | * |
| 54 | * @param targetAddress the target IPv6 address to set |
| 55 | * @return this |
| 56 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 57 | public Redirect setTargetAddress(final byte[] targetAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 58 | this.targetAddress = |
| 59 | Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 60 | return this; |
| 61 | } |
| 62 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 63 | /** |
| 64 | * Gets destination address. |
| 65 | * |
| 66 | * @return the destination IPv6 address |
| 67 | */ |
| 68 | public byte[] getDestinationAddress() { |
| 69 | return this.destinationAddress; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets destination address. |
| 74 | * |
| 75 | * @param destinationAddress the destination IPv6 address to set |
| 76 | * @return this |
| 77 | */ |
| 78 | public Redirect setDestinationAddress(final byte[] destinationAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 79 | this.destinationAddress = |
| 80 | Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the Neighbor Discovery Protocol packet options. |
| 86 | * |
| 87 | * @return the Neighbor Discovery Protocol packet options |
| 88 | */ |
| 89 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 90 | return this.options.options(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Adds a Neighbor Discovery Protocol packet option. |
| 95 | * |
| 96 | * @param type the option type |
| 97 | * @param data the option data |
| 98 | * @return this |
| 99 | */ |
| 100 | public Redirect addOption(final byte type, final byte[] data) { |
| 101 | this.options.addOption(type, data); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 102 | return this; |
| 103 | } |
| 104 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 105 | @Override |
| 106 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 107 | byte[] optionsData = null; |
| 108 | if (this.options.hasOptions()) { |
| 109 | optionsData = this.options.serialize(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 110 | } |
| 111 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 112 | int optionsLength = 0; |
| 113 | if (optionsData != null) { |
| 114 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 115 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 116 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 117 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 118 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 119 | |
| 120 | bb.putInt(0); |
| 121 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 122 | bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 123 | if (optionsData != null) { |
| 124 | bb.put(optionsData); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return data; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 132 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 133 | |
| 134 | bb.getInt(); |
| 135 | bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 136 | bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 137 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 138 | this.options.deserialize(data, bb.position(), |
| 139 | bb.limit() - bb.position()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 140 | |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * (non-Javadoc) |
| 146 | * |
| 147 | * @see java.lang.Object#hashCode() |
| 148 | */ |
| 149 | @Override |
| 150 | public int hashCode() { |
| 151 | final int prime = 5807; |
| 152 | int result = super.hashCode(); |
| 153 | ByteBuffer bb; |
| 154 | bb = ByteBuffer.wrap(this.targetAddress); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 155 | for (int i = 0; i < this.targetAddress.length / 4; i++) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 156 | result = prime * result + bb.getInt(); |
| 157 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 158 | bb = ByteBuffer.wrap(this.destinationAddress); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 159 | for (int i = 0; i < this.destinationAddress.length / 4; i++) { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 160 | result = prime * result + bb.getInt(); |
| 161 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 162 | result = prime * result + this.options.hashCode(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 163 | return result; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * (non-Javadoc) |
| 168 | * |
| 169 | * @see java.lang.Object#equals(java.lang.Object) |
| 170 | */ |
| 171 | @Override |
| 172 | public boolean equals(final Object obj) { |
| 173 | if (this == obj) { |
| 174 | return true; |
| 175 | } |
| 176 | if (!super.equals(obj)) { |
| 177 | return false; |
| 178 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 179 | if (!(obj instanceof Redirect)) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 182 | final Redirect other = (Redirect) obj; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 183 | if (!Arrays.equals(this.targetAddress, other.targetAddress)) { |
| 184 | return false; |
| 185 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 186 | if (!Arrays.equals(this.destinationAddress, |
| 187 | other.destinationAddress)) { |
| 188 | return false; |
| 189 | } |
| 190 | if (!this.options.equals(other.options)) { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 191 | return false; |
| 192 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 193 | return true; |
| 194 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Deserializer function for redirect packets. |
| 198 | * |
| 199 | * @return deserializer function |
| 200 | */ |
| 201 | public static Deserializer<Redirect> deserializer() { |
| 202 | return (data, offset, length) -> { |
| 203 | checkInput(data, offset, length, HEADER_LENGTH); |
| 204 | |
| 205 | Redirect redirect = new Redirect(); |
| 206 | |
| 207 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 208 | |
| 209 | bb.getInt(); |
| 210 | |
| 211 | bb.get(redirect.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 212 | bb.get(redirect.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 213 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 214 | if (bb.limit() - bb.position() > 0) { |
| 215 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 216 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 217 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 218 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 219 | redirect.addOption(option.type(), option.data()); |
| 220 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return redirect; |
| 224 | }; |
| 225 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 226 | |
| 227 | @Override |
| 228 | public String toString() { |
| 229 | return toStringHelper(getClass()) |
| 230 | .add("targetAddress", Arrays.toString(targetAddress)) |
| 231 | .add("destinationAddress", Arrays.toString(destinationAddress)) |
| 232 | .toString(); |
| 233 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 234 | } |