blob: ccb87e3fb3c8c34d80531c6ffbca2ec2e01c37a2 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Dusan Pajina22b9702015-02-12 16:25:23 +01002 * Copyright 2014-2015 Open Networking Laboratory
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +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. Chan93b7fb02014-12-28 03:59:36 +080021
22import java.nio.ByteBuffer;
23import java.util.Arrays;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080024import java.util.List;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080025
26/**
Dusan Pajina22b9702015-02-12 16:25:23 +010027 * Implements ICMPv6 Neighbor Advertisement packet format (RFC 4861).
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080028 */
29public class NeighborAdvertisement extends BasePacket {
30 public static final byte HEADER_LENGTH = 20; // bytes
31
32 protected byte routerFlag;
33 protected byte solicitedFlag;
34 protected byte overrideFlag;
35 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
36
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080037 private final NeighborDiscoveryOptions options =
38 new NeighborDiscoveryOptions();
39
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080040 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080041 * Gets router flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080042 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080043 * @return the router flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080044 */
45 public byte getRouterFlag() {
46 return this.routerFlag;
47 }
48
49 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080050 * Sets router flag.
51 *
52 * @param routerFlag the router flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080053 * @return this
54 */
55 public NeighborAdvertisement setRouterFlag(final byte routerFlag) {
56 this.routerFlag = routerFlag;
57 return this;
58 }
59
60 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080061 * Gets solicited flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080062 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080063 * @return the solicited flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080064 */
65 public byte getSolicitedFlag() {
66 return this.solicitedFlag;
67 }
68
69 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080070 * Sets solicited flag.
71 *
72 * @param solicitedFlag the solicited flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080073 * @return this
74 */
75 public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) {
76 this.solicitedFlag = solicitedFlag;
77 return this;
78 }
79
80 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080081 * Gets override flag.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080082 *
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080083 * @return the override flag
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080084 */
85 public byte getOverrideFlag() {
86 return this.overrideFlag;
87 }
88
89 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080090 * Sets override flag.
91 *
92 * @param overrideFlag the override flag to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080093 * @return this
94 */
95 public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) {
96 this.overrideFlag = overrideFlag;
97 return this;
98 }
99
100 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800101 * Gets target address.
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800102 *
103 * @return the target IPv6 address
104 */
105 public byte[] getTargetAddress() {
106 return this.targetAddress;
107 }
108
109 /**
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800110 * Sets target address.
111 *
112 * @param targetAddress the target IPv6 address to set
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800113 * @return this
114 */
115 public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800116 this.targetAddress =
117 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
118 return this;
119 }
120
121 /**
122 * Gets the Neighbor Discovery Protocol packet options.
123 *
124 * @return the Neighbor Discovery Protocol packet options
125 */
126 public List<NeighborDiscoveryOptions.Option> getOptions() {
127 return this.options.options();
128 }
129
130 /**
131 * Adds a Neighbor Discovery Protocol packet option.
132 *
133 * @param type the option type
134 * @param data the option data
135 * @return this
136 */
137 public NeighborAdvertisement addOption(final byte type,
138 final byte[] data) {
139 this.options.addOption(type, data);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800140 return this;
141 }
142
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800143 @Override
144 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800145 byte[] optionsData = null;
146 if (this.options.hasOptions()) {
147 optionsData = this.options.serialize();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800148 }
149
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800150 int optionsLength = 0;
151 if (optionsData != null) {
152 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800153 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800154
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800155 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800156 final ByteBuffer bb = ByteBuffer.wrap(data);
157
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800158 bb.putInt((this.routerFlag & 0x1) << 31 |
159 (this.solicitedFlag & 0x1) << 30 |
160 (this.overrideFlag & 0x1) << 29);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800161 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800162 if (optionsData != null) {
163 bb.put(optionsData);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800164 }
165
166 return data;
167 }
168
169 @Override
170 public IPacket deserialize(byte[] data, int offset, int length) {
171 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
172 int iscratch;
173
174 iscratch = bb.getInt();
175 this.routerFlag = (byte) (iscratch >> 31 & 0x1);
176 this.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
177 this.overrideFlag = (byte) (iscratch >> 29 & 0x1);
178 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
179
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800180 this.options.deserialize(data, bb.position(),
181 bb.limit() - bb.position());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800182
183 return this;
184 }
185
186 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800187 * (non-Javadoc)
188 *
189 * @see java.lang.Object#hashCode()
190 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800191 @Override
192 public int hashCode() {
193 final int prime = 5807;
194 int result = super.hashCode();
195 ByteBuffer bb;
196 result = prime * result + this.routerFlag;
197 result = prime * result + this.solicitedFlag;
198 result = prime * result + this.overrideFlag;
199 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800200 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800201 result = prime * result + bb.getInt();
202 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800203 result = prime * result + this.options.hashCode();
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800204 return result;
205 }
206
207 /*
208 * (non-Javadoc)
209 *
210 * @see java.lang.Object#equals(java.lang.Object)
211 */
212 @Override
213 public boolean equals(final Object obj) {
214 if (this == obj) {
215 return true;
216 }
217 if (!super.equals(obj)) {
218 return false;
219 }
220 if (!(obj instanceof NeighborAdvertisement)) {
221 return false;
222 }
223 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
224 if (this.routerFlag != other.routerFlag) {
225 return false;
226 }
227 if (this.solicitedFlag != other.solicitedFlag) {
228 return false;
229 }
230 if (this.overrideFlag != other.overrideFlag) {
231 return false;
232 }
233 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
234 return false;
235 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800236 if (!this.options.equals(other.options)) {
237 return false;
238 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800239 return true;
240 }
241}