blob: 113107a3c254efcb38771b3df3bbbef0698c1f14 [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
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +0800128 int payloadLength = 0;
129 if (payloadData != null) {
130 payloadLength = payloadData.length;
131 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800132
133 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
134 final ByteBuffer bb = ByteBuffer.wrap(data);
135
136 bb.putInt((this.routerFlag & 0x1) << 31 | (this.solicitedFlag & 0x1) << 30 | (this.overrideFlag & 0x1) << 29);
137 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
138 if (payloadData != null) {
139 bb.put(payloadData);
140 }
141
142 return data;
143 }
144
145 @Override
146 public IPacket deserialize(byte[] data, int offset, int length) {
147 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
148 int iscratch;
149
150 iscratch = bb.getInt();
151 this.routerFlag = (byte) (iscratch >> 31 & 0x1);
152 this.solicitedFlag = (byte) (iscratch >> 30 & 0x1);
153 this.overrideFlag = (byte) (iscratch >> 29 & 0x1);
154 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
155
156 this.payload = new Data();
157 this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
158 - bb.position());
159 this.payload.setParent(this);
160
161 return this;
162 }
163
164 /*
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800165 * (non-Javadoc)
166 *
167 * @see java.lang.Object#hashCode()
168 */
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800169 @Override
170 public int hashCode() {
171 final int prime = 5807;
172 int result = super.hashCode();
173 ByteBuffer bb;
174 result = prime * result + this.routerFlag;
175 result = prime * result + this.solicitedFlag;
176 result = prime * result + this.overrideFlag;
177 bb = ByteBuffer.wrap(this.targetAddress);
178 for (int i = 0; i < 4; i++) {
179 result = prime * result + bb.getInt();
180 }
181 return result;
182 }
183
184 /*
185 * (non-Javadoc)
186 *
187 * @see java.lang.Object#equals(java.lang.Object)
188 */
189 @Override
190 public boolean equals(final Object obj) {
191 if (this == obj) {
192 return true;
193 }
194 if (!super.equals(obj)) {
195 return false;
196 }
197 if (!(obj instanceof NeighborAdvertisement)) {
198 return false;
199 }
200 final NeighborAdvertisement other = (NeighborAdvertisement) obj;
201 if (this.routerFlag != other.routerFlag) {
202 return false;
203 }
204 if (this.solicitedFlag != other.solicitedFlag) {
205 return false;
206 }
207 if (this.overrideFlag != other.overrideFlag) {
208 return false;
209 }
210 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
211 return false;
212 }
213 return true;
214 }
215}