blob: 0515869adf7a4c8fda92126f67173a348d277e67 [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;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080019import org.onlab.packet.IPacket;
20import 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
26/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027 * Implements ICMPv6 Redirect packet format. (RFC 4861)
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080028 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080029public class Redirect extends BasePacket {
30 public static final byte HEADER_LENGTH = 36; // bytes
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080031
32 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080033 protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080034
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080035 private final NeighborDiscoveryOptions options =
36 new NeighborDiscoveryOptions();
37
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080038 /**
39 * Gets target address.
40 *
41 * @return the target IPv6 address
42 */
43 public byte[] getTargetAddress() {
44 return this.targetAddress;
45 }
46
47 /**
48 * Sets target address.
49 *
50 * @param targetAddress the target IPv6 address to set
51 * @return this
52 */
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080053 public Redirect setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080054 this.targetAddress =
55 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080056 return this;
57 }
58
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080059 /**
60 * Gets destination address.
61 *
62 * @return the destination IPv6 address
63 */
64 public byte[] getDestinationAddress() {
65 return this.destinationAddress;
66 }
67
68 /**
69 * Sets destination address.
70 *
71 * @param destinationAddress the destination IPv6 address to set
72 * @return this
73 */
74 public Redirect setDestinationAddress(final byte[] destinationAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080075 this.destinationAddress =
76 Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH);
77 return this;
78 }
79
80 /**
81 * Gets the Neighbor Discovery Protocol packet options.
82 *
83 * @return the Neighbor Discovery Protocol packet options
84 */
85 public List<NeighborDiscoveryOptions.Option> getOptions() {
86 return this.options.options();
87 }
88
89 /**
90 * Adds a Neighbor Discovery Protocol packet option.
91 *
92 * @param type the option type
93 * @param data the option data
94 * @return this
95 */
96 public Redirect addOption(final byte type, final byte[] data) {
97 this.options.addOption(type, data);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080098 return this;
99 }
100
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800101 @Override
102 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800103 byte[] optionsData = null;
104 if (this.options.hasOptions()) {
105 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800106 }
107
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800108 int optionsLength = 0;
109 if (optionsData != null) {
110 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800111 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800112
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800113 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800114 final ByteBuffer bb = ByteBuffer.wrap(data);
115
116 bb.putInt(0);
117 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800118 bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800119 if (optionsData != null) {
120 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800121 }
122
123 return data;
124 }
125
126 @Override
127 public IPacket deserialize(byte[] data, int offset, int length) {
128 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
129
130 bb.getInt();
131 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800132 bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800133
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800134 this.options.deserialize(data, bb.position(),
135 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800136
137 return this;
138 }
139
140 /*
141 * (non-Javadoc)
142 *
143 * @see java.lang.Object#hashCode()
144 */
145 @Override
146 public int hashCode() {
147 final int prime = 5807;
148 int result = super.hashCode();
149 ByteBuffer bb;
150 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800151 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800152 result = prime * result + bb.getInt();
153 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800154 bb = ByteBuffer.wrap(this.destinationAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800155 for (int i = 0; i < this.destinationAddress.length / 4; i++) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800156 result = prime * result + bb.getInt();
157 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800158 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800159 return result;
160 }
161
162 /*
163 * (non-Javadoc)
164 *
165 * @see java.lang.Object#equals(java.lang.Object)
166 */
167 @Override
168 public boolean equals(final Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (!super.equals(obj)) {
173 return false;
174 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800175 if (!(obj instanceof Redirect)) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800176 return false;
177 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800178 final Redirect other = (Redirect) obj;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800179 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
180 return false;
181 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800182 if (!Arrays.equals(this.destinationAddress,
183 other.destinationAddress)) {
184 return false;
185 }
186 if (!this.options.equals(other.options)) {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800187 return false;
188 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800189 return true;
190 }
191}