blob: 594b79d094b705737a793d789fcfe9c5164b4972 [file] [log] [blame]
Rusty Eddy1da61a22015-09-01 00:48:58 +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
Jian Li5fc14292015-12-04 11:30:46 -080018import org.slf4j.Logger;
19
Rusty Eddy1da61a22015-09-01 00:48:58 +000020import java.nio.ByteBuffer;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
Jian Li5fc14292015-12-04 11:30:46 -080025import java.util.Arrays;
Rusty Eddy1da61a22015-09-01 00:48:58 +000026
Jian Li5fc14292015-12-04 11:30:46 -080027import static com.google.common.base.MoreObjects.toStringHelper;
Rusty Eddy1da61a22015-09-01 00:48:58 +000028import static com.google.common.base.Preconditions.checkNotNull;
29import static org.onlab.packet.PacketUtils.checkInput;
Jian Li5fc14292015-12-04 11:30:46 -080030import static org.slf4j.LoggerFactory.getLogger;
Rusty Eddy1da61a22015-09-01 00:48:58 +000031
32
33/**
34 * Implements IGMP control packet format.
35 */
36public class IGMP extends BasePacket {
37 private final Logger log = getLogger(getClass());
38
39 public static final byte TYPE_IGMPV3_MEMBERSHIP_QUERY = 0x11;
40 public static final byte TYPE_IGMPV1_MEMBERSHIP_REPORT = 0x12;
41 public static final byte TYPE_IGMPV2_MEMBERSHIP_REPORT = 0x16;
42 public static final byte TYPE_IGMPV2_LEAVE_GROUP = 0x17;
43 public static final byte TYPE_IGMPV3_MEMBERSHIP_REPORT = 0x22;
44 public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP = new HashMap<>();
45
46 public static final int MINIMUM_HEADER_LEN = 12;
47
48 List<IGMPGroup> groups = new ArrayList<>();
49
50 // Fields contained in the IGMP header
51 private byte igmpType;
52 private byte resField = 0;
53 private short checksum = 0;
54
55 private byte[] unsupportTypeData;
56
57 public IGMP() {
58 }
59
60 /**
61 * Get the IGMP message type.
62 *
63 * @return the IGMP message type
64 */
65 public byte getIgmpType() {
66 return igmpType;
67 }
68
69 /**
70 * Set the IGMP message type.
71 *
72 * @param msgType IGMP message type
73 */
74 public void setIgmpType(byte msgType) {
75 igmpType = msgType;
76 }
77
78 /**
79 * Get the checksum of this message.
80 *
81 * @return the checksum
82 */
83 public short getChecksum() {
84 return checksum;
85 }
86
87 /**
88 * get the Max Resp Code.
89 *
90 * @return The Maximum Time allowed before before sending a responding report.
91 */
92 public byte getMaxRespField() {
93 return resField;
94 }
95
96 /**
97 * Set the Max Resp Code.
98 *
99 * @param respCode the Maximum Response Code.
100 */
101 public void setMaxRespCode(byte respCode) {
102 if (igmpType != IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY) {
103 log.debug("Requesting the max response code for an incorrect field: ");
104 }
105 this.resField = respCode;
106 }
107
108 /**
109 * Get the list of IGMPGroups. The group objects will be either IGMPQuery or IGMPMembership
110 * depending on the IGMP message type. For IGMP Query, the groups list should only be
111 * one group.
112 *
113 * @return The list of IGMP groups.
114 */
115 public List<IGMPGroup> getGroups() {
116 return groups;
117 }
118
119 /**
120 * Add a multicast group to this IGMP message.
121 *
122 * @param group the IGMPGroup will be IGMPQuery or IGMPMembership depending on the message type.
123 * @return true if group was valid and added, false otherwise.
124 */
125 public boolean addGroup(IGMPGroup group) {
126 checkNotNull(group);
127 switch (this.igmpType) {
128 case TYPE_IGMPV3_MEMBERSHIP_QUERY:
129 if (group instanceof IGMPMembership) {
130 return false;
131 }
132
133 if (group.sources.size() > 1) {
134 return false;
135 }
136 break;
137
138 case TYPE_IGMPV3_MEMBERSHIP_REPORT:
139 if (group instanceof IGMPMembership) {
140 return false;
141 }
142 break;
143
144 default:
145 log.debug("Warning no IGMP message type has been set");
146 }
147
148 this.groups.add(group);
149 return true;
150 }
151
152 /**
153 * Serialize this IGMP packet. This will take care
154 * of serializing IGMPv3 Queries and IGMPv3 Membership
155 * Reports.
156 *
157 * @return the serialized IGMP message
158 */
159 @Override
160 public byte[] serialize() {
161 byte [] data = new byte[8915];
162
163 ByteBuffer bb = ByteBuffer.wrap(data);
164 bb.put(this.getIgmpType());
165
166 // reserved or max resp code depending on type.
167 bb.put(this.resField);
168
169 // Must calculate checksum
170 bb.putShort((short) 0);
171
172 switch (this.igmpType) {
173
174 case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT:
175 // reserved
176 bb.putShort((short) 0);
177 // Number of groups
178 bb.putShort((short) groups.size());
179 // Fall through
180
181 case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:
182
183 for (IGMPGroup grp : groups) {
184 grp.serialize(bb);
185 }
186 break;
187
188 default:
189 bb.put(this.unsupportTypeData);
190 break;
191 }
192
193 int size = bb.position();
194 bb.position(0);
195 byte [] rdata = new byte[size];
196 bb.get(rdata, 0, size);
197 return rdata;
198 }
199
200 /**
201 * Deserialize an IGMP message.
202 *
203 * @param data bytes to deserialize
204 * @param offset offset to start deserializing from
205 * @param length length of the data to deserialize
206 * @return populated IGMP object
207 */
208 @Override
209 public IPacket deserialize(final byte[] data, final int offset,
210 final int length) {
211
212 IGMP igmp = new IGMP();
213 try {
214 igmp = IGMP.deserializer().deserialize(data, offset, length);
215 } catch (DeserializationException e) {
216 log.error(e.getStackTrace().toString());
217 return this;
218 }
219 this.igmpType = igmp.igmpType;
220 this.resField = igmp.resField;
221 this.checksum = igmp.checksum;
222 this.groups = igmp.groups;
223 return this;
224 }
225
226 /**
227 * Deserializer function for IPv4 packets.
228 *
229 * @return deserializer function
230 */
231 public static Deserializer<IGMP> deserializer() {
232 return (data, offset, length) -> {
233 checkInput(data, offset, length, MINIMUM_HEADER_LEN);
234
235 IGMP igmp = new IGMP();
236
Rusty Eddy158d5d82015-10-12 16:59:04 -0700237 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
Rusty Eddy1da61a22015-09-01 00:48:58 +0000238 igmp.igmpType = bb.get();
239 igmp.resField = bb.get();
240 igmp.checksum = bb.getShort();
241 int len = MINIMUM_HEADER_LEN;
242 String msg;
243
244 switch (igmp.igmpType) {
245
246 case TYPE_IGMPV3_MEMBERSHIP_QUERY:
247 IGMPQuery qgroup = new IGMPQuery();
248 qgroup.deserialize(bb);
249 igmp.groups.add(qgroup);
250 break;
251
252 case TYPE_IGMPV3_MEMBERSHIP_REPORT:
253 bb.getShort(); // Ignore resvd
254 int ngrps = bb.getShort();
255
256 for (; ngrps > 0; ngrps--) {
257 IGMPMembership mgroup = new IGMPMembership();
258 mgroup.deserialize(bb);
259 igmp.groups.add(mgroup);
260 }
261 break;
262
263 /*
264 * NOTE: according to the IGMPv3 spec. These previous IGMP type fields
265 * must be supported. At this time we are going to <b>assume</b> we run
266 * in a modern network where all devices are IGMPv3 capable.
267 */
268 case TYPE_IGMPV1_MEMBERSHIP_REPORT:
269 case TYPE_IGMPV2_MEMBERSHIP_REPORT:
270 case TYPE_IGMPV2_LEAVE_GROUP:
271 igmp.unsupportTypeData = bb.array(); // Is this the entire array?
272 msg = "IGMP message type: " + igmp.igmpType + " is not supported";
273 igmp.log.debug(msg);
274 break;
275
276 default:
alshabib0588e572015-09-04 12:00:42 -0700277 msg = "IGMP message type: " + igmp.igmpType + " is not recognized";
278 igmp.unsupportTypeData = bb.array();
Rusty Eddy1da61a22015-09-01 00:48:58 +0000279 igmp.log.debug(msg);
280 break;
281 }
282 return igmp;
283 };
284 }
285
286 /*
287 * (non-Javadoc)
288 *
289 * @see java.lang.Object#equals(java.lang.Object)
290 */
291 @Override
292 public boolean equals(final Object obj) {
293 if (this == obj) {
294 return true;
295 }
296 if (!super.equals(obj)) {
297 return false;
298 }
299 if (!(obj instanceof IGMP)) {
300 return false;
301 }
302 final IGMP other = (IGMP) obj;
303 if (this.igmpType != other.igmpType) {
304 return false;
305 }
306 if (this.resField != other.resField) {
307 return false;
308 }
309 if (this.checksum != other.checksum) {
310 return false;
311 }
312 if (this.groups.size() != other.groups.size()) {
313 return false;
314 }
315 // TODO: equals should be true regardless of order.
316 if (!groups.equals(other.groups)) {
317 return false;
318 }
319 return true;
320 }
321
322 /*
323 * (non-Javadoc)
324 *
325 * @see java.lang.Object#hashCode()
326 */
327 @Override
328 public int hashCode() {
329 final int prime = 2521;
330 int result = super.hashCode();
331 result = prime * result + this.igmpType;
332 result = prime * result + this.groups.size();
333 result = prime * result + this.resField;
334 result = prime * result + this.checksum;
335 result = prime * result + this.groups.hashCode();
336 return result;
337 }
Jian Li5fc14292015-12-04 11:30:46 -0800338
339 @Override
340 public String toString() {
341 return toStringHelper(getClass())
342 .add("igmpType", Byte.toString(igmpType))
343 .add("resField", Byte.toString(resField))
344 .add("checksum", Short.toString(checksum))
345 .add("unsupportTypeData", Arrays.toString(unsupportTypeData))
346 .toString();
347 // TODO: need to handle groups
348 }
Rusty Eddy1da61a22015-09-01 00:48:58 +0000349}