blob: ad5e9dc2c28915d28a698b2603e8ea7bf0a80f90 [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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.Ip6Address;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080021
22import java.nio.ByteBuffer;
23import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080024import java.util.List;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080025
Jian Li5fc14292015-12-04 11:30:46 -080026import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070027import static org.onlab.packet.PacketUtils.checkInput;
28
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080029/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080030 * Implements ICMPv6 Redirect packet format. (RFC 4861)
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080031 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080032public class Redirect extends BasePacket {
33 public static final byte HEADER_LENGTH = 36; // bytes
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080034
35 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080036 protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080037
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080038 private final NeighborDiscoveryOptions options =
39 new NeighborDiscoveryOptions();
40
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080041 /**
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. Chanea5aa472015-01-03 13:40:39 +080056 public Redirect setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080057 this.targetAddress =
58 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080059 return this;
60 }
61
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080062 /**
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 Radoslavova2626ef2015-02-18 18:33:25 -080078 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. Chanea5aa472015-01-03 13:40:39 +0800101 return this;
102 }
103
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800104 @Override
105 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800106 byte[] optionsData = null;
107 if (this.options.hasOptions()) {
108 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800109 }
110
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800111 int optionsLength = 0;
112 if (optionsData != null) {
113 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800114 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800115
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800116 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800117 final ByteBuffer bb = ByteBuffer.wrap(data);
118
119 bb.putInt(0);
120 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800121 bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800122 if (optionsData != null) {
123 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800124 }
125
126 return data;
127 }
128
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800129
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800130
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 Radoslavova2626ef2015-02-18 18:33:25 -0800142 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800143 result = prime * result + bb.getInt();
144 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800145 bb = ByteBuffer.wrap(this.destinationAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800146 for (int i = 0; i < this.destinationAddress.length / 4; i++) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800147 result = prime * result + bb.getInt();
148 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800149 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800150 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. Chanea5aa472015-01-03 13:40:39 +0800166 if (!(obj instanceof Redirect)) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800167 return false;
168 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800169 final Redirect other = (Redirect) obj;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800170 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
171 return false;
172 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800173 if (!Arrays.equals(this.destinationAddress,
174 other.destinationAddress)) {
175 return false;
176 }
177 if (!this.options.equals(other.options)) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800178 return false;
179 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800180 return true;
181 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700182
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 Chan3599d632015-09-05 14:47:51 +0800201 if (bb.limit() - bb.position() > 0) {
202 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
203 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700204
Charles Chan3599d632015-09-05 14:47:51 +0800205 for (NeighborDiscoveryOptions.Option option : options.options()) {
206 redirect.addOption(option.type(), option.data());
207 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700208 }
209
210 return redirect;
211 };
212 }
Jian Li5fc14292015-12-04 11:30:46 -0800213
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. Chan7fee36a2014-12-31 00:19:59 +0800221}