blob: ce3b7b7b8a9e67a03b81731f35ca4f7a27a103a1 [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
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/**
31 * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
32 */
33public class NeighborSolicitation extends BasePacket {
34 public static final byte HEADER_LENGTH = 20; // bytes
35
36 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
37
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 */
56 public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080057 this.targetAddress =
58 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
59 return this;
60 }
61
62 /**
63 * Gets the Neighbor Discovery Protocol packet options.
64 *
65 * @return the Neighbor Discovery Protocol packet options
66 */
67 public List<NeighborDiscoveryOptions.Option> getOptions() {
68 return this.options.options();
69 }
70
71 /**
72 * Adds a Neighbor Discovery Protocol packet option.
73 *
74 * @param type the option type
75 * @param data the option data
76 * @return this
77 */
78 public NeighborSolicitation addOption(final byte type,
79 final byte[] data) {
80 this.options.addOption(type, data);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080081 return this;
82 }
83
84 @Override
85 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086 byte[] optionsData = null;
87 if (this.options.hasOptions()) {
88 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080089 }
90
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080091 int optionsLength = 0;
92 if (optionsData != null) {
93 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080094 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080095
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080096 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080097 final ByteBuffer bb = ByteBuffer.wrap(data);
98
99 bb.putInt(0);
100 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800101 if (optionsData != null) {
102 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800103 }
104
105 return data;
106 }
107
108 @Override
109 public IPacket deserialize(byte[] data, int offset, int length) {
110 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
111
112 bb.getInt();
113 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
114
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800115 this.options.deserialize(data, bb.position(),
116 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800117
118 return this;
119 }
120
121 /*
122 * (non-Javadoc)
123 *
124 * @see java.lang.Object#hashCode()
125 */
126 @Override
127 public int hashCode() {
128 final int prime = 5807;
129 int result = super.hashCode();
130 ByteBuffer bb;
131 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800132 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800133 result = prime * result + bb.getInt();
134 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800135 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800136 return result;
137 }
138
139 /*
140 * (non-Javadoc)
141 *
142 * @see java.lang.Object#equals(java.lang.Object)
143 */
144 @Override
145 public boolean equals(final Object obj) {
146 if (this == obj) {
147 return true;
148 }
149 if (!super.equals(obj)) {
150 return false;
151 }
152 if (!(obj instanceof NeighborSolicitation)) {
153 return false;
154 }
155 final NeighborSolicitation other = (NeighborSolicitation) obj;
156 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
157 return false;
158 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800159 if (!this.options.equals(other.options)) {
160 return false;
161 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800162 return true;
163 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700164
165 /**
166 * Deserializer function for neighbor solicitation packets.
167 *
168 * @return deserializer function
169 */
170 public static Deserializer<NeighborSolicitation> deserializer() {
171 return (data, offset, length) -> {
172 checkInput(data, offset, length, HEADER_LENGTH);
173
174 NeighborSolicitation neighborSolicitation = new NeighborSolicitation();
175
176 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
177
178 bb.getInt();
179 bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH);
180
Charles Chan3599d632015-09-05 14:47:51 +0800181 if (bb.limit() - bb.position() > 0) {
182 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
183 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700184
Charles Chan3599d632015-09-05 14:47:51 +0800185 for (NeighborDiscoveryOptions.Option option : options.options()) {
186 neighborSolicitation.addOption(option.type(), option.data());
187 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700188 }
189
190 return neighborSolicitation;
191 };
192 }
Jian Li5fc14292015-12-04 11:30:46 -0800193
194 @Override
195 public String toString() {
196 return toStringHelper(getClass())
197 .add("targetAddress", Arrays.toString(targetAddress))
198 .toString();
199 // TODO: need to handle options
200 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800201}