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