blob: e6ef0aed704305d0553d3ae957879b0c9b4f99a9 [file] [log] [blame]
Rusty Eddy80f12522015-09-03 22:42:02 +00001/*
2 * Copyright 2015 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 */
16package org.onlab.packet;
17
18import org.onlab.packet.pim.PIMHello;
19import org.onlab.packet.pim.PIMJoinPrune;
20
21import java.nio.ByteBuffer;
22import java.util.HashMap;
23import java.util.Map;
24
25import static org.onlab.packet.PacketUtils.checkInput;
26
27/**
28 * Implements PIM control packet format.
29 */
30public class PIM extends BasePacket {
31
32 public static final IpAddress PIM_ADDRESS = IpAddress.valueOf("224.0.0.13");
33
34 public static final byte TYPE_HELLO = 0x00;
35 public static final byte TYPE_REGISTER = 0x01;
Rusty Eddy9cbc0952015-09-14 22:29:07 +000036 public static final byte TYPE_REGISTER_STOP = 0x02;
Rusty Eddy80f12522015-09-03 22:42:02 +000037 public static final byte TYPE_JOIN_PRUNE_REQUEST = 0x03;
38 public static final byte TYPE_BOOTSTRAP = 0x04;
39 public static final byte TYPE_ASSERT = 0x05;
40 public static final byte TYPE_GRAFT = 0x06;
41 public static final byte TYPE_GRAFT_ACK = 0x07;
42 public static final byte TYPE_CANDIDATE_RP_ADV = 0x08;
43
44 public static final int PIM_HEADER_LEN = 4;
45
Rusty Eddy0c12e402015-10-19 19:27:01 -070046 public static final byte ADDRESS_FAMILY_IP4 = 0x1;
47 public static final byte ADDRESS_FAMILY_IP6 = 0x2;
48
Rusty Eddy80f12522015-09-03 22:42:02 +000049 public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
50 new HashMap<>();
51
52 static {
53 PIM.PROTOCOL_DESERIALIZER_MAP.put(PIM.TYPE_HELLO, PIMHello.deserializer());
54 PIM.PROTOCOL_DESERIALIZER_MAP.put(PIM.TYPE_JOIN_PRUNE_REQUEST, PIMJoinPrune.deserializer());
55 }
56
57 /*
58 * PIM Header fields
59 */
60 protected byte version;
61 protected byte type;
62 protected byte reserved;
63 protected short checksum;
64
65 /**
66 * Default constructor.
67 */
68 public PIM() {
69 super();
70 this.version = 2;
71 this.reserved = 0;
72 }
73
74 /**
75 * Return the PIM message type.
76 *
77 * @return the pimMsgType
78 */
79 public byte getPimMsgType() {
80 return this.type;
81 }
82
83 /**
84 * Set the PIM message type. Currently PIMJoinPrune and PIMHello are
85 * supported.
86 *
87 * @param type PIM message type
88 * @return PIM Header
89 */
90 public PIM setPIMType(final byte type) {
91 this.type = type;
92 return this;
93 }
94
95 /**
96 * Get the version of PIM.
97 *
98 * @return the PIM version. Must be 2.
99 */
100 public byte getVersion() {
101 return version;
102 }
103
104 /**
105 * Set the PIM version type. Should not change from 2.
106 *
Thomas Vachuskab9c7ea62015-09-10 15:17:02 -0700107 * @param version PIM version
Rusty Eddy80f12522015-09-03 22:42:02 +0000108 */
109 public void setVersion(byte version) {
110 this.version = version;
111 }
112
113 /**
114 * Get the reserved field.
115 *
116 * @return the reserved field. Must be ignored.
117 */
118 public byte getReserved() {
119 return reserved;
120 }
121
122 /**
123 * Set the reserved field.
124 *
125 * @param reserved should be 0
126 */
127 public void setReserved(byte reserved) {
128 this.reserved = reserved;
129 }
130
131 /**
132 * Get the checksum of this packet.
133 *
134 * @return the checksum
135 */
136 public short getChecksum() {
137 return checksum;
138 }
139
140 /**
141 * Set the checksum.
142 *
143 * @param checksum the checksum
144 */
145 public void setChecksum(short checksum) {
146 this.checksum = checksum;
147 }
148
149 /*
150 * (non-Javadoc)
151 *
152 * @see java.lang.Object#hashCode()
153 */
154 @Override
155 public int hashCode() {
156 final int prime = 5807;
157 int result = super.hashCode();
158 result = prime * result + this.type;
159 result = prime * result + this.version;
160 result = prime * result + this.checksum;
161 return result;
162 }
163
164 /*
165 * (non-Javadoc)
166 *
167 * @see java.lang.Object#equals(java.lang.Object)
168 */
169 @Override
170 public boolean equals(final Object obj) {
171 if (this == obj) {
172 return true;
173 }
174 if (!super.equals(obj)) {
175 return false;
176 }
177 if (!(obj instanceof PIM)) {
178 return false;
179 }
180 final PIM other = (PIM) obj;
181 if (this.type != other.type) {
182 return false;
183 }
184 if (this.version != other.version) {
185 return false;
186 }
187 if (this.checksum != other.checksum) {
188 return false;
189 }
190 return true;
191 }
192
193 /**
194 * Serializes the packet. Will compute and set the following fields if they
195 * are set to specific values at the time serialize is called: -checksum : 0
196 * -length : 0
197 *
198 * @return will return the serialized packet
199 */
200 @Override
201 public byte[] serialize() {
202 int length = 4;
203 byte[] payloadData = null;
204 if (this.payload != null) {
205 this.payload.setParent(this);
206 payloadData = this.payload.serialize();
207 length += payloadData.length;
208 }
209
210 final byte[] data = new byte[length];
211 final ByteBuffer bb = ByteBuffer.wrap(data);
212
213 bb.put((byte) ((this.version & 0xf) << 4 | this.type & 0xf));
214 bb.put(this.reserved);
215 bb.putShort(this.checksum);
216 if (payloadData != null) {
217 bb.put(payloadData);
218 }
219
220 if (this.parent != null && this.parent instanceof PIM) {
221 ((PIM) this.parent).setPIMType(TYPE_JOIN_PRUNE_REQUEST);
222 }
223
224 // compute checksum if needed
225 if (this.checksum == 0) {
226 bb.rewind();
227 int accumulation = 0;
228
229 for (int i = 0; i < length / 2; ++i) {
230 accumulation += 0xffff & bb.getShort();
231 }
232 // pad to an even number of shorts
233 if (length % 2 > 0) {
234 accumulation += (bb.get() & 0xff) << 8;
235 }
236
237 accumulation = (accumulation >> 16 & 0xffff)
238 + (accumulation & 0xffff);
239 this.checksum = (short) (~accumulation & 0xffff);
240 bb.putShort(2, this.checksum);
241 }
242 return data;
243 }
244
245 /**
246 * Deserialize the PIM packet.
247 *
248 * @param data bytes to deserialize.
249 * @param offset offset to start deserializing from
250 * @param length length of the data to deserialize
251 *
252 * @return the deserialized PIM packet.
253 */
254 @Override
255 public IPacket deserialize(final byte[] data, final int offset,
256 final int length) {
257 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
258 this.type = bb.get();
259 this.version = bb.get();
260 this.checksum = bb.getShort();
261
262 //this.payload = new Data();
263 this.payload = this.payload.deserialize(data, bb.position(), bb.limit() - bb.position());
264 this.payload.setParent(this);
265 return this;
266 }
267 /**
268 * Deserializer function for IPv4 packets.
269 *
270 * @return deserializer function
271 */
272 public static Deserializer<PIM> deserializer() {
273 return (data, offset, length) -> {
274 checkInput(data, offset, length, PIM_HEADER_LEN);
275
276 PIM pim = new PIM();
277
278 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
279
280 byte versionByte = bb.get();
281 pim.version = (byte) (versionByte >> 4 & 0xf);
282 pim.setPIMType((byte) (versionByte & 0xf));
283 pim.reserved = bb.get();
284 pim.checksum = bb.getShort();
285
286 Deserializer<? extends IPacket> deserializer;
287 if (PIM.PROTOCOL_DESERIALIZER_MAP.containsKey(pim.getPimMsgType())) {
288 deserializer = PIM.PROTOCOL_DESERIALIZER_MAP.get(pim.getPimMsgType());
289 } else {
290 deserializer = Data.deserializer();
291 }
292
293 pim.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position());
294 pim.payload.setParent(pim);
295
296 return pim;
297 };
298 }
299}