blob: 212c7a15007cde25cd0b529b1dccdde931b5c931 [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
18import java.nio.ByteBuffer;
19import java.util.ArrayList;
Ray Milkey9f87e512016-01-05 10:00:22 -080020import java.util.Arrays;
Rusty Eddy1da61a22015-09-01 00:48:58 +000021import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
Ray Milkey9f87e512016-01-05 10:00:22 -080024
25import org.slf4j.Logger;
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
Rusty Eddy1da61a22015-09-01 00:48:58 +000032/**
33 * Implements IGMP control packet format.
34 */
35public class IGMP extends BasePacket {
36 private final Logger log = getLogger(getClass());
37
38 public static final byte TYPE_IGMPV3_MEMBERSHIP_QUERY = 0x11;
39 public static final byte TYPE_IGMPV1_MEMBERSHIP_REPORT = 0x12;
40 public static final byte TYPE_IGMPV2_MEMBERSHIP_REPORT = 0x16;
41 public static final byte TYPE_IGMPV2_LEAVE_GROUP = 0x17;
42 public static final byte TYPE_IGMPV3_MEMBERSHIP_REPORT = 0x22;
43 public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP = new HashMap<>();
44
45 public static final int MINIMUM_HEADER_LEN = 12;
46
47 List<IGMPGroup> groups = new ArrayList<>();
48
49 // Fields contained in the IGMP header
50 private byte igmpType;
51 private byte resField = 0;
52 private short checksum = 0;
53
54 private byte[] unsupportTypeData;
55
56 public IGMP() {
57 }
58
59 /**
60 * Get the IGMP message type.
61 *
62 * @return the IGMP message type
63 */
64 public byte getIgmpType() {
65 return igmpType;
66 }
67
68 /**
69 * Set the IGMP message type.
70 *
71 * @param msgType IGMP message type
72 */
73 public void setIgmpType(byte msgType) {
74 igmpType = msgType;
75 }
76
77 /**
78 * Get the checksum of this message.
79 *
80 * @return the checksum
81 */
82 public short getChecksum() {
83 return checksum;
84 }
85
86 /**
87 * get the Max Resp Code.
88 *
89 * @return The Maximum Time allowed before before sending a responding report.
90 */
91 public byte getMaxRespField() {
92 return resField;
93 }
94
95 /**
96 * Set the Max Resp Code.
97 *
98 * @param respCode the Maximum Response Code.
99 */
100 public void setMaxRespCode(byte respCode) {
101 if (igmpType != IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY) {
102 log.debug("Requesting the max response code for an incorrect field: ");
103 }
104 this.resField = respCode;
105 }
106
107 /**
108 * Get the list of IGMPGroups. The group objects will be either IGMPQuery or IGMPMembership
109 * depending on the IGMP message type. For IGMP Query, the groups list should only be
110 * one group.
111 *
112 * @return The list of IGMP groups.
113 */
114 public List<IGMPGroup> getGroups() {
115 return groups;
116 }
117
118 /**
119 * Add a multicast group to this IGMP message.
120 *
121 * @param group the IGMPGroup will be IGMPQuery or IGMPMembership depending on the message type.
122 * @return true if group was valid and added, false otherwise.
123 */
124 public boolean addGroup(IGMPGroup group) {
125 checkNotNull(group);
126 switch (this.igmpType) {
127 case TYPE_IGMPV3_MEMBERSHIP_QUERY:
128 if (group instanceof IGMPMembership) {
129 return false;
130 }
131
132 if (group.sources.size() > 1) {
133 return false;
134 }
135 break;
136
137 case TYPE_IGMPV3_MEMBERSHIP_REPORT:
138 if (group instanceof IGMPMembership) {
139 return false;
140 }
141 break;
142
143 default:
144 log.debug("Warning no IGMP message type has been set");
145 }
146
147 this.groups.add(group);
148 return true;
149 }
150
151 /**
152 * Serialize this IGMP packet. This will take care
153 * of serializing IGMPv3 Queries and IGMPv3 Membership
154 * Reports.
155 *
156 * @return the serialized IGMP message
157 */
Ray Milkeyaef45852016-01-11 17:13:19 -0800158 @java.lang.SuppressWarnings("squid:S128") // suppress switch fall through warning
Rusty Eddy1da61a22015-09-01 00:48:58 +0000159 @Override
160 public byte[] serialize() {
Jian Li68c4fc42016-01-11 16:07:03 -0800161 byte[] data = new byte[8915];
Rusty Eddy1da61a22015-09-01 00:48:58 +0000162
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);
Jian Li68c4fc42016-01-11 16:07:03 -0800195 byte[] rdata = new byte[size];
Rusty Eddy1da61a22015-09-01 00:48:58 +0000196 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}