blob: 77c119a0a83933fbec93baf1889231dae14a9dbf [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/**
30 * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
31 */
32public class NeighborSolicitation extends BasePacket {
33 public static final byte HEADER_LENGTH = 20; // bytes
34
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. Chan7fee36a2014-12-31 00:19:59 +080040 /**
41 * Gets target address.
42 *
43 * @return the target IPv6 address
44 */
45 public byte[] getTargetAddress() {
46 return this.targetAddress;
47 }
48
49 /**
50 * Sets target address.
51 *
52 * @param targetAddress the target IPv6 address to set
53 * @return this
54 */
55 public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080056 this.targetAddress =
57 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
58 return this;
59 }
60
61 /**
62 * Gets the Neighbor Discovery Protocol packet options.
63 *
64 * @return the Neighbor Discovery Protocol packet options
65 */
66 public List<NeighborDiscoveryOptions.Option> getOptions() {
67 return this.options.options();
68 }
69
70 /**
71 * Adds a Neighbor Discovery Protocol packet option.
72 *
73 * @param type the option type
74 * @param data the option data
75 * @return this
76 */
77 public NeighborSolicitation addOption(final byte type,
78 final byte[] data) {
79 this.options.addOption(type, data);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080080 return this;
81 }
82
83 @Override
84 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080085 byte[] optionsData = null;
86 if (this.options.hasOptions()) {
87 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080088 }
89
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080090 int optionsLength = 0;
91 if (optionsData != null) {
92 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080093 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080094
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080095 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080096 final ByteBuffer bb = ByteBuffer.wrap(data);
97
98 bb.putInt(0);
99 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800100 if (optionsData != null) {
101 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800102 }
103
104 return data;
105 }
106
107 @Override
108 public IPacket deserialize(byte[] data, int offset, int length) {
109 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
110
111 bb.getInt();
112 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
113
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800114 this.options.deserialize(data, bb.position(),
115 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800116
117 return this;
118 }
119
120 /*
121 * (non-Javadoc)
122 *
123 * @see java.lang.Object#hashCode()
124 */
125 @Override
126 public int hashCode() {
127 final int prime = 5807;
128 int result = super.hashCode();
129 ByteBuffer bb;
130 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800131 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800132 result = prime * result + bb.getInt();
133 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800134 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800135 return result;
136 }
137
138 /*
139 * (non-Javadoc)
140 *
141 * @see java.lang.Object#equals(java.lang.Object)
142 */
143 @Override
144 public boolean equals(final Object obj) {
145 if (this == obj) {
146 return true;
147 }
148 if (!super.equals(obj)) {
149 return false;
150 }
151 if (!(obj instanceof NeighborSolicitation)) {
152 return false;
153 }
154 final NeighborSolicitation other = (NeighborSolicitation) obj;
155 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
156 return false;
157 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800158 if (!this.options.equals(other.options)) {
159 return false;
160 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800161 return true;
162 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700163
164 /**
165 * Deserializer function for neighbor solicitation packets.
166 *
167 * @return deserializer function
168 */
169 public static Deserializer<NeighborSolicitation> deserializer() {
170 return (data, offset, length) -> {
171 checkInput(data, offset, length, HEADER_LENGTH);
172
173 NeighborSolicitation neighborSolicitation = new NeighborSolicitation();
174
175 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
176
177 bb.getInt();
178 bb.get(neighborSolicitation.targetAddress, 0, Ip6Address.BYTE_LENGTH);
179
Charles Chan3599d632015-09-05 14:47:51 +0800180 if (bb.limit() - bb.position() > 0) {
181 NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
182 .deserialize(data, bb.position(), bb.limit() - bb.position());
Jonathan Hart2a655752015-04-07 16:46:33 -0700183
Charles Chan3599d632015-09-05 14:47:51 +0800184 for (NeighborDiscoveryOptions.Option option : options.options()) {
185 neighborSolicitation.addOption(option.type(), option.data());
186 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700187 }
188
189 return neighborSolicitation;
190 };
191 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800192}