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