blob: 25df3b436259e9dadba9535311d256a61fa1e1bf [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/**
27 * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
28 */
29public class NeighborSolicitation extends BasePacket {
30 public static final byte HEADER_LENGTH = 20; // bytes
31
32 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
33
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080034 private final NeighborDiscoveryOptions options =
35 new NeighborDiscoveryOptions();
36
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080037 /**
38 * Gets target address.
39 *
40 * @return the target IPv6 address
41 */
42 public byte[] getTargetAddress() {
43 return this.targetAddress;
44 }
45
46 /**
47 * Sets target address.
48 *
49 * @param targetAddress the target IPv6 address to set
50 * @return this
51 */
52 public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080053 this.targetAddress =
54 Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
55 return this;
56 }
57
58 /**
59 * Gets the Neighbor Discovery Protocol packet options.
60 *
61 * @return the Neighbor Discovery Protocol packet options
62 */
63 public List<NeighborDiscoveryOptions.Option> getOptions() {
64 return this.options.options();
65 }
66
67 /**
68 * Adds a Neighbor Discovery Protocol packet option.
69 *
70 * @param type the option type
71 * @param data the option data
72 * @return this
73 */
74 public NeighborSolicitation addOption(final byte type,
75 final byte[] data) {
76 this.options.addOption(type, data);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080077 return this;
78 }
79
80 @Override
81 public byte[] serialize() {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080082 byte[] optionsData = null;
83 if (this.options.hasOptions()) {
84 optionsData = this.options.serialize();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080085 }
86
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080087 int optionsLength = 0;
88 if (optionsData != null) {
89 optionsLength = optionsData.length;
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080090 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080091
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080092 final byte[] data = new byte[HEADER_LENGTH + optionsLength];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080093 final ByteBuffer bb = ByteBuffer.wrap(data);
94
95 bb.putInt(0);
96 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080097 if (optionsData != null) {
98 bb.put(optionsData);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080099 }
100
101 return data;
102 }
103
104 @Override
105 public IPacket deserialize(byte[] data, int offset, int length) {
106 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
107
108 bb.getInt();
109 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
110
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800111 this.options.deserialize(data, bb.position(),
112 bb.limit() - bb.position());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800113
114 return this;
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see java.lang.Object#hashCode()
121 */
122 @Override
123 public int hashCode() {
124 final int prime = 5807;
125 int result = super.hashCode();
126 ByteBuffer bb;
127 bb = ByteBuffer.wrap(this.targetAddress);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800128 for (int i = 0; i < this.targetAddress.length / 4; i++) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800129 result = prime * result + bb.getInt();
130 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800131 result = prime * result + this.options.hashCode();
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800132 return result;
133 }
134
135 /*
136 * (non-Javadoc)
137 *
138 * @see java.lang.Object#equals(java.lang.Object)
139 */
140 @Override
141 public boolean equals(final Object obj) {
142 if (this == obj) {
143 return true;
144 }
145 if (!super.equals(obj)) {
146 return false;
147 }
148 if (!(obj instanceof NeighborSolicitation)) {
149 return false;
150 }
151 final NeighborSolicitation other = (NeighborSolicitation) obj;
152 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
153 return false;
154 }
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800155 if (!this.options.equals(other.options)) {
156 return false;
157 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800158 return true;
159 }
160}