blob: 51256d41bf3655e990da679bc6f3fdeca0362fee [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 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
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
129 @Override
130 public IPacket deserialize(byte[] data, int offset, int length) {
131 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
132
133 bb.getInt();
134 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800135 bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800136
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800137 this.options.deserialize(data, bb.position(),
138 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800139
140 return this;
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see java.lang.Object#hashCode()
147 */
148 @Override
149 public int hashCode() {
150 final int prime = 5807;
151 int result = super.hashCode();
152 ByteBuffer bb;
153 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800154 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800155 result = prime * result + bb.getInt();
156 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800157 bb = ByteBuffer.wrap(this.destinationAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800158 for (int i = 0; i < this.destinationAddress.length / 4; i++) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800159 result = prime * result + bb.getInt();
160 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800161 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800162 return result;
163 }
164
165 /*
166 * (non-Javadoc)
167 *
168 * @see java.lang.Object#equals(java.lang.Object)
169 */
170 @Override
171 public boolean equals(final Object obj) {
172 if (this == obj) {
173 return true;
174 }
175 if (!super.equals(obj)) {
176 return false;
177 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800178 if (!(obj instanceof Redirect)) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800179 return false;
180 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800181 final Redirect other = (Redirect) obj;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800182 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
183 return false;
184 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800185 if (!Arrays.equals(this.destinationAddress,
186 other.destinationAddress)) {
187 return false;
188 }
189 if (!this.options.equals(other.options)) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800190 return false;
191 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800192 return true;
193 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700194
195 /**
196 * Deserializer function for redirect packets.
197 *
198 * @return deserializer function
199 */
200 public static Deserializer<Redirect> deserializer() {
201 return (data, offset, length) -> {
202 checkInput(data, offset, length, HEADER_LENGTH);
203
204 Redirect redirect = new Redirect();
205
206 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
207
208 bb.getInt();
209
210 bb.get(redirect.targetAddress, 0, Ip6Address.BYTE_LENGTH);
211 bb.get(redirect.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
212
Charles Chan3599d632015-09-05 14:47:51 +0800213 if (bb.limit() - bb.position() > 0) {
214 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
215 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700216
Charles Chan3599d632015-09-05 14:47:51 +0800217 for (NeighborDiscoveryOptions.Option option : options.options()) {
218 redirect.addOption(option.type(), option.data());
219 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700220 }
221
222 return redirect;
223 };
224 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800225}