blob: 8c9a5666073a73b00657b8a6144429ceb2277585 [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +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 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080016package org.onlab.packet.ndp;
17
18import org.onlab.packet.BasePacket;
Jonathan Hart2a655752015-04-07 16:46:33 -070019import org.onlab.packet.Deserializer;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080020import org.onlab.packet.IPacket;
21import org.onlab.packet.Ip6Address;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080022
23import java.nio.ByteBuffer;
24import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080025import java.util.List;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080026
Jian Li5fc14292015-12-04 11:30:46 -080027import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070028import static org.onlab.packet.PacketUtils.checkInput;
29
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080030/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080031 * Implements ICMPv6 Redirect packet format. (RFC 4861)
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080032 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080033public class Redirect extends BasePacket {
34 public static final byte HEADER_LENGTH = 36; // bytes
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080035
36 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080037 protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080038
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080039 private final NeighborDiscoveryOptions options =
40 new NeighborDiscoveryOptions();
41
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080042 /**
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. Chanea5aa472015-01-03 13:40:39 +080057 public Redirect setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080058 this.targetAddress =
59 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080060 return this;
61 }
62
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080063 /**
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 Radoslavova2626ef2015-02-18 18:33:25 -080079 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. Chanea5aa472015-01-03 13:40:39 +0800102 return this;
103 }
104
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800105 @Override
106 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800107 byte[] optionsData = null;
108 if (this.options.hasOptions()) {
109 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800110 }
111
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800112 int optionsLength = 0;
113 if (optionsData != null) {
114 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800115 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800116
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800117 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800118 final ByteBuffer bb = ByteBuffer.wrap(data);
119
120 bb.putInt(0);
121 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800122 bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800123 if (optionsData != null) {
124 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800125 }
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. Chanea5aa472015-01-03 13:40:39 +0800136 bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800137
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800138 this.options.deserialize(data, bb.position(),
139 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800140
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 Radoslavova2626ef2015-02-18 18:33:25 -0800155 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800156 result = prime * result + bb.getInt();
157 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800158 bb = ByteBuffer.wrap(this.destinationAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800159 for (int i = 0; i < this.destinationAddress.length / 4; i++) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800160 result = prime * result + bb.getInt();
161 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800162 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800163 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. Chanea5aa472015-01-03 13:40:39 +0800179 if (!(obj instanceof Redirect)) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800180 return false;
181 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800182 final Redirect other = (Redirect) obj;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800183 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
184 return false;
185 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800186 if (!Arrays.equals(this.destinationAddress,
187 other.destinationAddress)) {
188 return false;
189 }
190 if (!this.options.equals(other.options)) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800191 return false;
192 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800193 return true;
194 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700195
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 Chan3599d632015-09-05 14:47:51 +0800214 if (bb.limit() - bb.position() > 0) {
215 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
216 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700217
Charles Chan3599d632015-09-05 14:47:51 +0800218 for (NeighborDiscoveryOptions.Option option : options.options()) {
219 redirect.addOption(option.type(), option.data());
220 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700221 }
222
223 return redirect;
224 };
225 }
Jian Li5fc14292015-12-04 11:30:46 -0800226
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. Chan7fee36a2014-12-31 00:19:59 +0800234}