blob: f144b84e6e43a6387ca24982c8e3a7f9143ee62d [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
16
17
18
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080019package org.onlab.packet.ndp;
20
21import org.onlab.packet.BasePacket;
22import org.onlab.packet.Data;
23import org.onlab.packet.IPacket;
24import org.onlab.packet.Ip6Address;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080025
26import java.nio.ByteBuffer;
27import java.util.Arrays;
28
29/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080030 * Implements ICMPv6 Neighbor Advertisement packet format. (RFC 4861)
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080031 */
32public class NeighborAdvertisement extends BasePacket {
33 public static final byte HEADER_LENGTH = 20; // bytes
34
35 protected byte routerFlag;
36 protected byte solicitedFlag;
37 protected byte overrideFlag;
38 protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
39
40 /**
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) {
116 this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
117 return this;
118 }
119
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800120 @Override
121 public byte[] serialize() {
122 byte[] payloadData = null;
123 if (this.payload != null) {
124 this.payload.setParent(this);
125 payloadData = this.payload.serialize();
126 }
127
128 int payloadLength = payloadData == null ? 0 : (short) payloadData.length;
129
130 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
131 final ByteBuffer bb = ByteBuffer.wrap(data);
132
133 bb.putInt((this.routerFlag & 0x1) << 31 | (this.solicitedFlag & 0x1) << 30 | (this.overrideFlag & 0x1) << 29);
134 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
135 if (payloadData != null) {
136 bb.put(payloadData);
137 }
138
139 return data;
140 }
141
142 @Override
143 public IPacket deserialize(byte[] data, int offset, int length) {
144 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
145 int iscratch;
146
147 iscratch = bb.getInt();
148 this.routerFlag = (byte) (iscratch >> 31 & 0x1);
149 this.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
150 this.overrideFlag = (byte) (iscratch >> 29 & 0x1);
151 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
152
153 this.payload = new Data();
154 this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
155 - bb.position());
156 this.payload.setParent(this);
157
158 return this;
159 }
160
161 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800162 * (non-Javadoc)
163 *
164 * @see java.lang.Object#hashCode()
165 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800166 @Override
167 public int hashCode() {
168 final int prime = 5807;
169 int result = super.hashCode();
170 ByteBuffer bb;
171 result = prime * result + this.routerFlag;
172 result = prime * result + this.solicitedFlag;
173 result = prime * result + this.overrideFlag;
174 bb = ByteBuffer.wrap(this.targetAddress);
175 for (int i = 0; i < 4; i++) {
176 result = prime * result + bb.getInt();
177 }
178 return result;
179 }
180
181 /*
182 * (non-Javadoc)
183 *
184 * @see java.lang.Object#equals(java.lang.Object)
185 */
186 @Override
187 public boolean equals(final Object obj) {
188 if (this == obj) {
189 return true;
190 }
191 if (!super.equals(obj)) {
192 return false;
193 }
194 if (!(obj instanceof NeighborAdvertisement)) {
195 return false;
196 }
197 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
198 if (this.routerFlag != other.routerFlag) {
199 return false;
200 }
201 if (this.solicitedFlag != other.solicitedFlag) {
202 return false;
203 }
204 if (this.overrideFlag != other.overrideFlag) {
205 return false;
206 }
207 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
208 return false;
209 }
210 return true;
211 }
212}