blob: 6d2455ad9406541fe86d318e8d1c87244d531973 [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
19package org.onlab.packet;
20
21import java.nio.ByteBuffer;
22import java.util.HashMap;
23import java.util.Map;
24
25/**
26 * Implements ICMPv6 packet format.
27 *
28 */
29public class ICMP6 extends BasePacket {
30 public static final byte HEADER_LENGTH = 4; // bytes
31
32 public static final byte ROUTER_SOLICITATION = (byte) 0x85;
33 public static final byte ROUTER_ADVERTISEMENT = (byte) 0x86;
34 public static final byte NEIGHBOR_SOLICITATION = (byte) 0x87;
35 public static final byte NEIGHBOR_ADVERTISEMENT = (byte) 0x88;
36 public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP =
37 new HashMap<>();
38
39 static {
40 ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.class);
41 }
42
43 protected byte icmpType;
44 protected byte icmpCode;
45 protected short checksum;
46
47 /**
48 * @return the icmpType
49 */
50 public byte getIcmpType() {
51 return this.icmpType;
52 }
53
54 /**
55 * @param icmpType
56 * to set
57 * @return this
58 */
59 public ICMP6 setIcmpType(final byte icmpType) {
60 this.icmpType = icmpType;
61 return this;
62 }
63
64 /**
65 * @return the icmp code
66 */
67 public byte getIcmpCode() {
68 return this.icmpCode;
69 }
70
71 /**
72 * @param icmpCode
73 * code to set
74 * @return this
75 */
76 public ICMP6 setIcmpCode(final byte icmpCode) {
77 this.icmpCode = icmpCode;
78 return this;
79 }
80
81 /**
82 * @return the checksum
83 */
84 public short getChecksum() {
85 return this.checksum;
86 }
87
88 /**
89 * @param checksum
90 * the checksum to set
91 * @return this
92 */
93 public ICMP6 setChecksum(final short checksum) {
94 this.checksum = checksum;
95 return this;
96 }
97
98 /**
99 * Serializes the packet. Will compute and set the following fields if they
100 * are set to specific values at the time serialize is called: -checksum : 0
101 * -length : 0
102 */
103 @Override
104 public byte[] serialize() {
105 byte[] payloadData = null;
106 if (this.payload != null) {
107 this.payload.setParent(this);
108 payloadData = this.payload.serialize();
109 }
110
111 int payloadLength = payloadData == null ? 0 : (short) payloadData.length;
112
113 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
114 final ByteBuffer bb = ByteBuffer.wrap(data);
115
116 bb.put(this.icmpType);
117 bb.put(this.icmpCode);
118 bb.putShort(this.checksum);
119 if (payloadData != null) {
120 bb.put(payloadData);
121 }
122
123 if (this.parent != null && this.parent instanceof IPv6) {
124 ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6);
125 }
126
127 // compute checksum if needed
128 if (this.checksum == 0) {
129 bb.rewind();
130 int accumulation = 0;
131
132 for (int i = 0; i < data.length / 2; ++i) {
133 accumulation += 0xffff & bb.getShort();
134 }
135 // pad to an even number of shorts
136 if (data.length % 2 > 0) {
137 accumulation += (bb.get() & 0xff) << 8;
138 }
139
140 accumulation = (accumulation >> 16 & 0xffff)
141 + (accumulation & 0xffff);
142 this.checksum = (short) (~accumulation & 0xffff);
143 bb.putShort(2, this.checksum);
144 }
145 return data;
146 }
147
148 /*
149 * (non-Javadoc)
150 *
151 * @see java.lang.Object#hashCode()
152 */
153 @Override
154 public int hashCode() {
155 final int prime = 5807;
156 int result = super.hashCode();
157 result = prime * result + this.icmpType;
158 result = prime * result + this.icmpCode;
159 result = prime * result + this.checksum;
160 return result;
161 }
162
163 /*
164 * (non-Javadoc)
165 *
166 * @see java.lang.Object#equals(java.lang.Object)
167 */
168 @Override
169 public boolean equals(final Object obj) {
170 if (this == obj) {
171 return true;
172 }
173 if (!super.equals(obj)) {
174 return false;
175 }
176 if (!(obj instanceof ICMP6)) {
177 return false;
178 }
179 final ICMP6 other = (ICMP6) obj;
180 if (this.icmpType != other.icmpType) {
181 return false;
182 }
183 if (this.icmpCode != other.icmpCode) {
184 return false;
185 }
186 if (this.checksum != other.checksum) {
187 return false;
188 }
189 return true;
190 }
191
192 @Override
193 public IPacket deserialize(final byte[] data, final int offset,
194 final int length) {
195 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
196 this.icmpType = bb.get();
197 this.icmpCode = bb.get();
198 this.checksum = bb.getShort();
199
200 IPacket payload;
201 if (ICMP6.PROTOCOL_CLASS_MAP.containsKey(this.icmpType)) {
202 final Class<? extends IPacket> clazz = ICMP6.PROTOCOL_CLASS_MAP
203 .get(this.icmpType);
204 try {
205 payload = clazz.newInstance();
206 } catch (final Exception e) {
207 throw new RuntimeException(
208 "Error parsing payload for ICMP6 packet", e);
209 }
210 } else {
211 payload = new Data();
212 }
213 this.payload = payload.deserialize(data, bb.position(),
214 bb.limit() - bb.position());
215 this.payload.setParent(this);
216
217 return this;
218 }
219}