blob: 878f9391ef51a771ae43e294d91c1efa64feec89 [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
Charles M.C. Chan5c0b4762015-01-10 18:38:37 +080065 int payloadLength = 0;
66 if (payloadData != null) {
67 payloadLength = payloadData.length;
68 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080069
70 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
71 final ByteBuffer bb = ByteBuffer.wrap(data);
72
73 bb.putInt(0);
74 bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
75 if (payloadData != null) {
76 bb.put(payloadData);
77 }
78
79 return data;
80 }
81
82 @Override
83 public IPacket deserialize(byte[] data, int offset, int length) {
84 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
85
86 bb.getInt();
87 bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
88
89 this.payload = new Data();
90 this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
91 - bb.position());
92 this.payload.setParent(this);
93
94 return this;
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see java.lang.Object#hashCode()
101 */
102 @Override
103 public int hashCode() {
104 final int prime = 5807;
105 int result = super.hashCode();
106 ByteBuffer bb;
107 bb = ByteBuffer.wrap(this.targetAddress);
108 for (int i = 0; i < 4; i++) {
109 result = prime * result + bb.getInt();
110 }
111 return result;
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see java.lang.Object#equals(java.lang.Object)
118 */
119 @Override
120 public boolean equals(final Object obj) {
121 if (this == obj) {
122 return true;
123 }
124 if (!super.equals(obj)) {
125 return false;
126 }
127 if (!(obj instanceof NeighborSolicitation)) {
128 return false;
129 }
130 final NeighborSolicitation other = (NeighborSolicitation) obj;
131 if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
132 return false;
133 }
134 return true;
135 }
136}