blob: 6da8503990457ca27e1f5d3f5ca286175c27c20d [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +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. Chan7fee36a2014-12-31 00:19:59 +080025
26import java.nio.ByteBuffer;
27import java.util.Arrays;
28
29/**
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
37 /**
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) {
53 this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
54 return this;
55 }
56
57 @Override
58 public byte[] serialize() {
59 byte[] payloadData = null;
60 if (this.payload != null) {
61 this.payload.setParent(this);
62 payloadData = this.payload.serialize();
63 }
64
65 int payloadLength = payloadData == null ? 0 : (short) payloadData.length;
66
67 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
68 final ByteBuffer bb = ByteBuffer.wrap(data);
69
70 bb.putInt(0);
71 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
72 if (payloadData != null) {
73 bb.put(payloadData);
74 }
75
76 return data;
77 }
78
79 @Override
80 public IPacket deserialize(byte[] data, int offset, int length) {
81 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
82
83 bb.getInt();
84 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
85
86 this.payload = new Data();
87 this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
88 - bb.position());
89 this.payload.setParent(this);
90
91 return this;
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see java.lang.Object#hashCode()
98 */
99 @Override
100 public int hashCode() {
101 final int prime = 5807;
102 int result = super.hashCode();
103 ByteBuffer bb;
104 bb = ByteBuffer.wrap(this.targetAddress);
105 for (int i = 0; i < 4; i++) {
106 result = prime * result + bb.getInt();
107 }
108 return result;
109 }
110
111 /*
112 * (non-Javadoc)
113 *
114 * @see java.lang.Object#equals(java.lang.Object)
115 */
116 @Override
117 public boolean equals(final Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (!super.equals(obj)) {
122 return false;
123 }
124 if (!(obj instanceof NeighborSolicitation)) {
125 return false;
126 }
127 final NeighborSolicitation other = (NeighborSolicitation) obj;
128 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
129 return false;
130 }
131 return true;
132 }
133}