Debian Codegraph @

protolib/src/common/protoPktIGMP.cpp

raw
1 #include "protoPktIGMP.h"
2
3 const UINT8 ProtoPktIGMP::DEFAULT_QRV = 2; // default query robustness value
4 const double ProtoPktIGMP::DEFAULT_QQIC = 125.0; // default query interval (seconds)
5 const double ProtoPktIGMP::DEFAULT_MAX_RESP = 10.0; // default query response time (seconds)
6
7 ProtoPktIGMP::ProtoPktIGMP(void* bufferPtr,
8 unsigned int numBytes,
9 bool initFromBuffer,
10 bool freeOnDestruct)
11 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
12 {
13 if (initFromBuffer) InitFromBuffer(numBytes);
14 }
15
16 ProtoPktIGMP::~ProtoPktIGMP()
17 {
18 }
19
20 bool ProtoPktIGMP::InitFromBuffer(UINT16 pktLength,
21 void* bufferPtr,
22 unsigned int bufferBytes,
23 bool freeOnDestruct)
24 {
25 unsigned int minLength = OFFSET_RESERVED;
26 if (NULL != bufferPtr)
27 {
28 if (bufferBytes < minLength) // IGMPv2 msg size
29 return false;
30 else
31 AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct);
32 }
33 if (GetBufferLength() < pktLength)
34 {
35 PLOG(PL_ERROR, "ProtoPktIGMP::InitFromBuffer() error: insufficient buffer size\n");
36 return false;
37 }
38 SetLength(pktLength);
39 if (GetVersion() > 2)
40 {
41 minLength = OFFSET_SRC_LIST;
42 if (pktLength < minLength)
43 {
44 PLOG(PL_ERROR, "ProtoPktIGMP::InitFromBuffer() error: invalid IGMPv3 packet\n");
45 return false;
46 }
47 minLength += 4*GetNumSources();
48 if (pktLength < minLength)
49 {
50 PLOG(PL_ERROR, "ProtoPktIGMP::InitFromBuffer() error: truncated IGMPv3 packet\n");
51 return false;
52 }
53 }
54 return true;
55 } // end ProtoPktIGMP::InitFromBuffer()
56
57 UINT8 ProtoPktIGMP::GetVersion() const
58 {
59 switch (GetType())
60 {
61 case REPORT_V1:
62 return 1;
63 case REPORT_V2:
64 return 2;
65 case REPORT_V3:
66 return 3;
67 case QUERY:
68 case LEAVE:
69 if (GetLength() > OFFSET_RESERVED)
70 return 3;
71 else
72 return 2;
73 default:
74 return 0;
75 }
76 } // end ProtoPktIGMP::GetVersion()
77
78 double ProtoPktIGMP::GetMaxResponseTime() const
79 {
80 UINT8 value = GetUINT8(OFFSET_MAX_RESP);
81 if ((value < 128) || (GetVersion() < 3))
82 {
83 // value is in units of 0.1 seconds
84 return (0.1*(double)value);
85 }
86 else
87 {
88 unsigned int exp = (value & 0x70) >> 4;
89 unsigned int mant = value & 0x0f;
90 unsigned int maxRespTime = (mant | 0x10) << (exp + 3);
91 return (0.1*(double)maxRespTime);
92 }
93 } // end ProtoPktIGMP::GetMaxResponseTime()
94
95 double ProtoPktIGMP::GetQQIC() const
96 {
97 UINT8 qqic = GetUINT8(OFFSET_QQIC);
98 if (qqic < 128)
99 {
100 return (double)qqic;
101 }
102 else
103 {
104 unsigned int exp = (qqic & 0x70) >> 4;
105 unsigned int mant = qqic & 0x0f;
106 return (double)((mant | 0x10) << (exp + 3));
107 }
108 } // end ProtoPktIGMP::GetQQIC()
109
110 bool ProtoPktIGMP::GetSourceAddress(UINT16 index, ProtoAddress& srcAddr) const
111 {
112 if (index < GetNumSources())
113 {
114 srcAddr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer(OFFSET_SRC_LIST + index*4), 4);
115 return true;
116 }
117 else
118 {
119 srcAddr.Invalidate();
120 return false;
121 }
122 } // end ProtoPktIGMP::GetSourceAddress()
123
124 bool ProtoPktIGMP::GetNextGroupRecord(ProtoPktIGMP::GroupRecord& groupRecord, bool first)
125 {
126 if (0 == GetNumRecords()) return false;
127 void* recordPtr;
128 unsigned int bufferSpace;
129 if (first || (NULL == groupRecord.GetBuffer()))
130 {
131 // Point to first record
132 recordPtr = AccessBuffer(OFFSET_REC_LIST);
133 bufferSpace = GetLength() - OFFSET_REC_LIST;
134 }
135 else
136 {
137 recordPtr = groupRecord.AccessBuffer(groupRecord.GetLength());
138 // Make sure it's in scope of this IGMP message size.
139 size_t offset = (char*)recordPtr - (char*)GetBuffer();
140 if (offset > GetLength())
141 return false; // out of bounds
142 bufferSpace = GetLength() - (unsigned int)offset;
143 }
144 if (0 == bufferSpace)
145 return false;
146 else
147 return groupRecord.InitFromBuffer(recordPtr, bufferSpace);
148 } // end ProtoPktIGMP::GetGroupRecord()
149
150 bool ProtoPktIGMP::InitIntoBuffer(Type type,
151 unsigned int version,
152 void* bufferPtr,
153 unsigned int bufferBytes,
154 bool freeOnDestruct)
155 {
156 UINT16 minLength ;
157 switch (type)
158 {
159 case QUERY:
160 if (version < 3)
161 minLength = OFFSET_RESERVED;
162 else
163 minLength = OFFSET_SRC_LIST;
164 case REPORT_V1:
165 version = 1;
166 minLength = OFFSET_RESERVED;
167 break;
168 case REPORT_V2:
169 case LEAVE:
170 version = 2;
171 minLength = OFFSET_RESERVED;
172 break;
173 case REPORT_V3:
174 version = 3;
175 minLength = OFFSET_REC_LIST;
176 break;
177 default:
178 break;
179 }
180 if (NULL != bufferPtr)
181 {
182 if (bufferBytes < minLength)
183 return false;
184 else
185 AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct);
186 }
187 if (GetBufferLength() < minLength) return false;
188 memset(AccessBuffer(), 0, minLength);
189 SetUINT8(OFFSET_TYPE, type);
190 SetLength(minLength);
191 if (QUERY == type)
192 {
193 SetMaxResponseTime(DEFAULT_MAX_RESP);
194 if (3 == version)
195 {
196 SetQRV(DEFAULT_QRV);
197 SetQQIC(DEFAULT_QQIC);
198 }
199 }
200 return true;
201
202 } // end ProtoPktIGMP::InitIntoBuffer()
203
204 void ProtoPktIGMP::SetMaxResponseTime(double seconds, bool updateChecksum)
205 {
206 seconds *= 10.0; // convert to 1/10 sec units
207 UINT8 code;
208 if (seconds < 128)
209 {
210 code = (unsigned int)seconds;
211 }
212 else if (seconds > 31743.0)
213 {
214 code = 255;
215 }
216 else
217 {
218 unsigned int value = (unsigned int)seconds;
219 unsigned exp = 0;
220 value >>= 3;
221 while (value > 31)
222 {
223 exp++;
224 value >>= 1;
225 }
226 exp <<= 4;
227 code = (0x80 | exp | (value & 0x0f));
228
229 }
230 SetUINT8(OFFSET_MAX_RESP, code);
231 if (updateChecksum) ComputeChecksum();
232 } // end ProtoPktIGMP::SetMaxResponseTime()
233
234 void ProtoPktIGMP::SetGroupAddress(ProtoAddress* groupAddr, bool updateChecksum)
235 {
236 void* ptr = AccessBuffer(OFFSET_GROUP);
237 if ((NULL == groupAddr) || (ProtoAddress::IPv4 != groupAddr->GetType()) || (!groupAddr->IsMulticast()))
238 memset(ptr, 0, 4);
239 else
240 memcpy(ptr, groupAddr->GetRawHostAddress(), 4);
241 if (updateChecksum) ComputeChecksum();
242 } // end ProtoPktIGMP::SetGroupAddress()
243
244
245 bool ProtoPktIGMP::SetSuppress(bool state, bool updateChecksum)
246 {
247 if (GetBufferLength() <= OFFSET_RESERVED)
248 {
249 PLOG(PL_ERROR, "ProtoPktIGMP::SetSuppress() error: insufficient buffer space\n");
250 return false;
251 }
252 UINT8 code = GetUINT8(OFFSET_S);
253 if (state)
254 code |= 0x08;
255 else
256 code = (code & ~0x08) & 0x0f;
257 SetUINT8(OFFSET_S, code);
258 if (updateChecksum) ComputeChecksum();
259 return true;
260 } // end ProtoPktIGMP::SetSuppress()
261
262 bool ProtoPktIGMP::SetQRV(UINT8 qrv, bool updateChecksum)
263 {
264 if (GetBufferLength() <= OFFSET_RESERVED)
265 {
266 PLOG(PL_ERROR, "ProtoPktIGMP::SetQRV() error: insufficient buffer space\n");
267 return false;
268 }
269 UINT8 code = GetUINT8(OFFSET_QRV);
270 code &= 0x08; // clear old qrv
271 qrv &= 0x07; // mask new qrv
272 code |= qrv; // set new qrv
273 SetUINT8(OFFSET_QRV, code);
274 if (updateChecksum) ComputeChecksum();
275 return true;
276 } // end ProtoPktIGMP::SetQRV()
277
278 bool ProtoPktIGMP::SetQQIC(double seconds, bool updateChecksum)
279 {
280 if (GetBufferLength() <= OFFSET_RESERVED)
281 {
282 PLOG(PL_ERROR, "ProtoPktIGMP::SetQQIC() error: insufficient buffer space\n");
283 return false;
284 }
285 UINT8 code;
286 if (seconds < 128)
287 {
288 code = (unsigned int)seconds;
289 }
290 else if (seconds > 31743.0)
291 {
292 code = 255;
293 }
294 else
295 {
296 unsigned int value = (unsigned int)seconds;
297 unsigned exp = 0;
298 value >>= 3;
299 while (value > 31)
300 {
301 exp++;
302 value >>= 1;
303 }
304 exp <<= 4;
305 code = (0x80 | exp | (value & 0x0f));
306 }
307 SetUINT8(OFFSET_QQIC, code);
308 if (updateChecksum) ComputeChecksum();
309 return true;
310 } // end ProtoPktIGMP::SetQQIC()
311
312 bool ProtoPktIGMP::AppendSourceAddress(ProtoAddress& srcAddr, bool updateChecksum)
313 {
314 if ((ProtoAddress::IPv4 != srcAddr.GetType()) || !srcAddr.IsUnicast())
315 {
316 PLOG(PL_ERROR, "ProtoPktIGMP::AppendSourceAddress() error: invalid source address\n");
317 return false;
318 }
319 // Is there room for another source address?
320 UINT16 numSrc = GetNumSources();
321 unsigned int offset = OFFSET_SRC_LIST + 4*numSrc;
322 ASSERT(offset == GetLength());
323 unsigned int newLength = offset + 4;
324 if (newLength > GetBufferLength())
325 {
326 PLOG(PL_ERROR, "ProtoPktIGMP::AppendSourceAddress() error: insufficient buffer space\n");
327 return false;
328 }
329 memcpy(AccessBuffer(offset), srcAddr.GetRawHostAddress(), 4);
330 SetUINT16(OFFSET_NUM_SRC, numSrc + 1);
331 SetLength(newLength);
332 if (updateChecksum) ComputeChecksum();
333 return true;
334 } // end ProtoPktIGMP::AppendSourceAddress()
335
336 // This inits the groupRecord into buffer space at the
337 // end of the IGMP message being built (Need to call AppendGroupRecord() to commit it)
338 // (avoids need for additional allocation and copying when building up message)
339 bool ProtoPktIGMP::AttachGroupRecord(GroupRecord& groupRecord)
340 {
341 UINT32 currentLength = GetLength();
342 ASSERT(0 == (currentLength & 0x00000003)); // should be multiple of 4
343 void* bufferPtr = AccessBuffer(currentLength);
344 unsigned int bufferSpace = GetBufferLength() - currentLength;
345 if (!groupRecord.InitIntoBuffer(bufferPtr, bufferSpace))
346 {
347 PLOG(PL_ERROR, "ProtoPktIGMP::AttachGroupRecord() error: insufficient buffer space\n");
348 return false;
349 }
350 return true;
351 } // end ProtoPktIGMP::AttachGroupRecord()
352
353 bool ProtoPktIGMP::AppendGroupRecord(const GroupRecord& groupRecord, bool updateChecksum)
354 {
355 UINT32 currentLength = GetLength();
356 ASSERT(0 == (currentLength & 0x00000003)); // should be multiple of 4
357 unsigned int bufferSpace = GetBufferLength() - currentLength;
358 if (bufferSpace < groupRecord.GetLength())
359 {
360 PLOG(PL_ERROR, "ProtoPktIGMP::AppendGroupRecord() error: insufficient buffer space\n");
361 return false;
362 }
363 void* ptr = AccessBuffer(currentLength);
364 // Check if we need to copy since this is _not_ an attached group record
365 if (ptr != groupRecord.GetBuffer())
366 memcpy(ptr, groupRecord.GetBuffer(), groupRecord.GetLength());
367 SetUINT16(OFFSET_NUM_REC, GetNumRecords() + 1);
368 SetLength(currentLength + groupRecord.GetLength());
369 if (updateChecksum) ComputeChecksum();
370 return true;
371 } // end ProtoPktIGMP::AppendGroupRecord()
372
373 UINT16 ProtoPktIGMP::ComputeChecksum(bool set)
374 {
375 UINT32 sum = 0;
376 if (set) SetUINT16(OFFSET_CHECKSUM, (UINT16)0);
377 // Compute before checksum
378 unsigned int end = OFFSET_CHECKSUM/2;
379 end = GetLength() / 2;
380 for (unsigned int i = 0; i < end; i++)
381 sum += GetWord16(i);
382
383 // Carry as needed
384 while (0 != (sum >> 16))
385 sum = (sum & 0x0000ffff) + (sum >> 16);
386 // Complement
387 sum = ~sum;
388 // ZERO check/correct as needed
389 if (0 == sum) sum = 0x0000ffff;
390 if (set) SetUINT16(OFFSET_CHECKSUM, (UINT16)sum);
391 return (UINT16)sum;
392 } // end ProtoPktIGMP::ComputeChecksum()
393
394 ProtoPktIGMP::GroupRecord::GroupRecord(void* bufferPtr,
395 unsigned int numBytes,
396 bool initFromBuffer,
397 bool freeOnDestruct)
398 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
399 {
400 if (initFromBuffer)
401 InitFromBuffer();
402 else
403 InitIntoBuffer();
404 }
405
406 ProtoPktIGMP::GroupRecord::~GroupRecord()
407 {
408 }
409
410 bool ProtoPktIGMP::GroupRecord::InitFromBuffer(void* bufferPtr,
411 unsigned int bufferBytes,
412 bool freeOnDestruct)
413 {
414 unsigned int minLength = OFFSET_SRC_LIST;
415 if (NULL != bufferPtr)
416 {
417 if (bufferBytes < minLength) // IGMPv2 msg size
418 return false;
419 else
420 AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct);
421 }
422 minLength += GetAuxDataLen();
423 minLength += 4*GetNumSources();
424 if (GetBufferLength() < minLength)
425 {
426 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::InitFromBuffer() error: truncated IGMPv3 group record?!\n");
427 return false;
428 }
429 SetLength(minLength);
430 return true;
431 } // end ProtoPktIGMP::GroupRecord::InitFromBuffer()
432
433
434 bool ProtoPktIGMP::GroupRecord::GetSourceAddress(UINT16 index, ProtoAddress& srcAddr) const
435 {
436 if (index >= GetNumSources())
437 {
438 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::GetSourceAddress() error: index out of range\n");
439 srcAddr.Invalidate();
440 return false;
441 }
442 const void* ptr = GetBuffer(OFFSET_SRC_LIST + (index << 2));
443 srcAddr.SetRawHostAddress(ProtoAddress::IPv4, (char*)ptr, 4);
444 return true;
445 } // end ProtoPktIGMP::GroupRecord::GetSourceAddress()
446
447 bool ProtoPktIGMP::GroupRecord::InitIntoBuffer(void* bufferPtr,
448 unsigned int bufferBytes,
449 bool freeOnDestruct)
450 {
451 UINT16 minLength = OFFSET_SRC_LIST;
452 if (NULL != bufferPtr)
453 {
454 if (bufferBytes < minLength)
455 return false;
456 else
457 AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct);
458 }
459 if (GetBufferLength() < minLength) return false;
460 memset(AccessBuffer(), 0, OFFSET_SRC_LIST);
461 SetLength(minLength);
462 return true;
463 } // end ProtoPktIGMP::GroupRecord::InitIntoBuffer()
464
465 void ProtoPktIGMP::GroupRecord::SetGroupAddress(const ProtoAddress* groupAddr)
466 {
467 void* ptr = AccessBuffer(OFFSET_GROUP);
468 if ((NULL == groupAddr) || (ProtoAddress::IPv4 != groupAddr->GetType()) || (!groupAddr->IsMulticast()))
469 memset(ptr, 0, 4);
470 else
471 memcpy(ptr, groupAddr->GetRawHostAddress(), 4);
472 } // end ProtoPktIGMP::GroupRecord::SetGroupAddress()
473
474 bool ProtoPktIGMP::GroupRecord::AppendSourceAddress(const ProtoAddress& srcAddr)
475 {
476 if ((ProtoAddress::IPv4 != srcAddr.GetType()) || !srcAddr.IsUnicast())
477 {
478 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::AppendSourceAddress() error: invalid source address\n");
479 return false;
480 }
481 // Is there room for another source address?
482 UINT16 numSrc = GetNumSources();
483 unsigned int offset = OFFSET_SRC_LIST + 4*numSrc;
484 ASSERT(offset == GetLength());
485 unsigned int newLength = offset + 4;
486 if (newLength > GetBufferLength())
487 {
488 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::AppendSourceAddress() error: insufficient buffer space\n");
489 return false;
490 }
491 memcpy(AccessBuffer(offset), srcAddr.GetRawHostAddress(), 4);
492 SetUINT16(OFFSET_NUM_SRC, numSrc + 1);
493 SetLength(newLength);
494 return true;
495 } // end ProtoPktIGMP::GroupRecord::AppendSourceAddress()
496
497 bool ProtoPktIGMP::GroupRecord::AppendAuxiliaryData(const char* data, UINT16 len)
498 {
499 unsigned int currentLength = GetLength();
500 unsigned int bufferSpace = GetBufferLength() - currentLength;
501 if (0 != (len % 0xfffc))
502 {
503 // aux data len must by multiple of 4 bytes (32-bit words)
504 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::AppendAuxiliaryData() error: invalid data length\n");
505 return false;
506 }
507 if (bufferSpace < len)
508 {
509 PLOG(PL_ERROR, "ProtoPktIGMP::GroupRecord::AppendAuxiliaryData() error: insufficient buffer space\n");
510 return false;
511 }
512 void* ptr = AccessBuffer(currentLength);
513 memcpy(ptr, data, len);
514 SetUINT8(OFFSET_AUX_LEN,len >> 2);
515 SetLength(currentLength + len);
516 return true;
517 } // end ProtoPktIGMP::GroupRecord::AppendAuxiliaryData()
518
519
520