Debian Codegraph @

protolib/src/manet/manetMsg.cpp

raw
1 #include "manetMsg.h"
2 #include "protoDebug.h"
3
4 ManetTlv::ManetTlv()
5 {
6 }
7
8 ManetTlv::~ManetTlv()
9 {
10 }
11
12 bool ManetTlv::InitIntoBuffer(UINT8 type, void* bufferPtr, unsigned int numBytes)
13 {
14 unsigned int minLength = OFFSET_SEMANTICS + 1; // "type" and "semantics" fields only
15 if (NULL != bufferPtr)
16 {
17 if (numBytes < minLength)
18 return false;
19 else
20 AttachBuffer(bufferPtr, numBytes);
21 }
22 else if (buffer_bytes < minLength)
23 {
24 return false;
25 }
26 ((UINT8*)buffer_ptr)[OFFSET_TYPE] = (UINT8)type;
27 ((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] = (UINT8)0;
28 pkt_length = minLength;
29 return true;
30 } // end ManetTlv::InitIntoBuffer()
31
32 bool ManetTlv::SetExtendedType(UINT8 extType)
33 {
34 unsigned int minLength = OFFSET_TYPE_EXT + 1;
35 if (buffer_bytes < minLength) return false;
36 SetSemantic(EXTENDED_TYPE);
37 SetUINT8(OFFSET_TYPE_EXT, extType);
38 return true;
39 } // end ManetTlv::SetExtendedType()
40
41 bool ManetTlv::SetIndexRange(UINT8 start, UINT8 stop, bool multivalue, unsigned int numAddrs)
42 {
43 ASSERT(start <= stop);
44 ASSERT((0 == numAddrs) || (stop < numAddrs));
45 if ((0 != numAddrs) && (0 == start) && (stop == (numAddrs - 1)))
46 {
47 // This is for a full address block, so no explicit index fields are needed
48 unsigned int minLength = OffsetIndexStart();
49 if (buffer_bytes < minLength) return false;
50 ClearSemantics(SINGLE_INDEX | MULTI_INDEX | MULTIVALUE);
51 if (multivalue && (start != stop)) SetSemantic(MULTIVALUE);
52 pkt_length = minLength;
53 }
54 else if (start == stop)
55 {
56 // single index
57 unsigned int minLength = OffsetIndexStart() + 1;
58 if (buffer_bytes < minLength) return false;
59 ClearSemantics(MULTI_INDEX | MULTIVALUE);
60 SetSemantic(SINGLE_INDEX);
61 SetUINT8(OffsetIndexStart(), start);
62 pkt_length = minLength;
63 }
64 else
65 {
66 // index range
67 unsigned int minLength = OffsetIndexStart() + 2;
68 if (buffer_bytes < minLength) return false;
69 ClearSemantics(SINGLE_INDEX | MULTIVALUE);
70 SetSemantic(MULTI_INDEX);
71 if (multivalue) SetSemantic(MULTIVALUE);
72 SetUINT8(OffsetIndexStart(), start);
73 SetUINT8(OffsetIndexStop(), stop);
74 pkt_length = minLength;
75 }
76 return true;
77 } // end ManetTlv::SetIndexRange()
78
79 bool ManetTlv::UpdateIndexRange(UINT8 start, UINT8 stop, bool multivalue, unsigned int numAddrs)
80 {
81 ASSERT(start <= stop);
82 ASSERT((0 == numAddrs) || (stop < numAddrs));
83 if (start < GetIndexStart())
84 PLOG(PL_WARN, "ManetTlv::UpdateIndexRange() warning: start index is smaller than current start index.\n");
85
86 if ((0 != numAddrs) && (0 == start) && (stop == (numAddrs - 1)))
87 {
88 // This is for a full address block, so no explicit index fields are needed
89 if (HasSingleIndex())
90 {
91 // slide content backwards one byte to omit former single index
92 unsigned int contentStart = OffsetIndexStart() + 1;
93 unsigned int contentLength = pkt_length - contentStart;
94 memmove((char*)buffer_ptr+contentStart-1, (char*)buffer_ptr+contentStart, contentLength);
95 pkt_length--;
96 }
97 else if (HasMultiIndex())
98 {
99 // slide content backwards two byte to omit former start/stop indices
100 unsigned int contentStart = OffsetIndexStart() + 2;
101 unsigned int contentLength = pkt_length - contentStart;
102 memmove((char*)buffer_ptr+contentStart-2, (char*)buffer_ptr+contentStart, contentLength);
103 pkt_length -= 2;
104 }
105 ClearSemantics(SINGLE_INDEX | MULTI_INDEX | MULTIVALUE);
106 if (multivalue && (start != stop)) SetSemantic(MULTIVALUE);
107 }
108 else if (start == stop)
109 {
110 if (HasMultiIndex())
111 {
112 // we need to slide any content backwards one byte to omit stop index
113 unsigned int contentStart = OffsetIndexStart() + 2;
114 unsigned int contentLength = pkt_length - contentStart;
115 memmove((char*)buffer_ptr+contentStart-1, (char*)buffer_ptr+contentStart, contentLength);
116 pkt_length--;
117 }
118 else if (!HasSingleIndex())
119 {
120 // we need to slide any content forward a byte to add start index
121 if ((pkt_length+1) > buffer_bytes) return false;
122 unsigned int contentStart = OffsetIndexStart();
123 unsigned int contentLength = pkt_length - contentStart;
124 memmove((char*)buffer_ptr+contentStart+1, (char*)buffer_ptr+contentStart, contentLength);
125 pkt_length++;
126 }
127 ClearSemantics(MULTI_INDEX | MULTIVALUE);
128 SetSemantic(SINGLE_INDEX);
129 SetUINT8(OffsetIndexStart(), start);
130 }
131 else
132 {
133 // TBD - add code to allow AddressBlockTLVs (0 != numAddrs) to be multivalue but without explict index
134 if (HasSingleIndex())
135 {
136 // we need to slide any content forwards one byte to add stop index
137 if ((pkt_length+1) > buffer_bytes) return false; // no more room at the inn
138 unsigned int contentStart = OffsetIndexStart() + 1;
139 unsigned int contentLength = pkt_length - contentStart ;
140 memmove((char*)buffer_ptr+contentStart+1, (char*)buffer_ptr+contentStart, contentLength);
141 pkt_length++;
142 }
143 else if (!HasMultiIndex())
144 {
145 // we need to slide any content forwards one byte to add start/stop indices
146 if ((pkt_length+2) > buffer_bytes) return false; // no more room at the inn
147 unsigned int contentStart = OffsetIndexStart();
148 unsigned int contentLength = pkt_length - contentStart ;
149 memmove((char*)buffer_ptr+contentStart+2, (char*)buffer_ptr+contentStart, contentLength);
150 pkt_length++;
151
152 }
153 ClearSemantics(SINGLE_INDEX | MULTIVALUE);
154 SetSemantic(MULTI_INDEX);
155 if (multivalue) SetSemantic(MULTIVALUE);
156 SetUINT8(OffsetIndexStart(), start);
157 SetUINT8(OffsetIndexStop(), stop);
158 }
159 return true;
160 } // end ManetTlv::UpdateIndexRange()
161
162 // Note: User MUST call SetIndexRange() _before_ this method for "index" >= 0
163 bool ManetTlv::SetValue(const char* value, UINT16 valueLength, int index, unsigned int numAddrs)
164 {
165 if (index < 0)
166 {
167 if (SemanticIsSet(MULTIVALUE))
168 {
169 PLOG(PL_ERROR, "ManetTlv::SetValue() error: can't set non-indexed value for MultiValue TLV\n");
170 return false;
171 }
172 index = 0;
173 }
174 //else if (!SemanticIsSet(SINGLE_INDEX) && !SemanticIsSet(MULTI_INDEX)) ((0 == numAddrs) || (
175 else if (HasNoIndex() && !SemanticIsSet(MULTIVALUE))
176 {
177 ASSERT(index < 0);
178 if (index >= 0)
179 {
180 PLOG(PL_ERROR, "ManetTlv::SetValue() error: can't set indexed value for NO_INDEX TLV\n");
181 return false;
182 }
183 index = 0; // ignore index
184 }
185 else if (SemanticIsSet(SINGLE_INDEX))
186 {
187 if (index != GetIndexStart())
188 {
189 PLOG(PL_ERROR, "ManetTlv::SetValue() error: can't set multiple values for SINGLE_INDEX TLV\n");
190 return false;
191 }
192 index = 0;
193 }
194 else
195 {
196 // TBD - add code to allow multivalue AddressBlock TLVs to have no index
197 UINT8 startIndex = GetIndexStart();
198 if ((index < startIndex) || (index > GetIndexStop(numAddrs)))
199 {
200 PLOG(PL_ERROR, "ManetTlv::SetValue() error: out-of-range \"index\"\n");
201 return false; // (TBD) print a warning here?
202 }
203 else if (SemanticIsSet(MULTIVALUE))
204 {
205 index -= startIndex;
206 }
207 else
208 {
209 PLOG(PL_WARN, "ManetTlv::SetValue() warning: index specified for single value TLV?\n");
210 index = 0;
211 }
212 }
213
214
215 unsigned int offsetValue = OffsetValue();
216 UINT16 oldTlvLength = GetTlvLength();
217 unsigned int newTlvOffset = valueLength * index;
218 UINT16 newTlvLength = newTlvOffset + valueLength;
219 if (newTlvLength > oldTlvLength)
220 {
221 // Make sure the "value" will fit into our buffer space
222 unsigned int newPktLength = offsetValue + newTlvLength;
223 if ((newTlvLength >= 256) && (!SemanticIsSet(EXTENDED_LENGTH)))
224 newPktLength++;
225 if (newPktLength > buffer_bytes)
226 {
227 PLOG(PL_WARN, "ManetTlv::SetValue() value length exceeds packet buffer size\n");
228 return false;
229 }
230
231 if ((newTlvLength >= 256) && (!SemanticIsSet(EXTENDED_LENGTH)))
232 {
233 // Length necessitates "extended length" TLV
234 if (0 != oldTlvLength)
235 {
236 // Shift existing TLV content by one byte
237 char* ptrOld = (char*)buffer_ptr + offsetValue;
238 char* ptrNew = ptrOld + 1;
239 memmove(ptrNew, ptrOld, oldTlvLength);
240 }
241 SetSemantic(EXTENDED_LENGTH);
242 offsetValue++;
243 }
244
245 SetTlvLength(newTlvLength);
246 pkt_length = newPktLength;
247 }
248 // Copy new value to its position
249 memcpy((char*)buffer_ptr + offsetValue + newTlvOffset, value, valueLength);
250 if (!SemanticIsSet(HAS_VALUE)) SetSemantic(HAS_VALUE);
251 return true;
252 } // end ManetTlv::SetValue()
253
254 void ManetTlv::SetTlvLength(UINT16 tlvLength)
255 {
256 if (SemanticIsSet(EXTENDED_LENGTH))
257 SetUINT16(OffsetLength(), tlvLength);
258 else if (tlvLength < 256)
259 SetUINT8(OffsetLength(), (UINT8)tlvLength);
260 else
261 PLOG(PL_ERROR, "ManetTlv::SetTlvLength() error: tlvLength exceeds non-extended maximum\n");
262 } // end ManetTlv::SetTlvLength()
263
264 bool ManetTlv::InitFromBuffer(void* bufferPtr, unsigned numBytes)
265 {
266 if (NULL != bufferPtr) AttachBuffer(bufferPtr, numBytes);
267 UINT8 semantics = (buffer_bytes >= OFFSET_SEMANTICS) ? ((char*)buffer_ptr)[OFFSET_SEMANTICS] : 0;
268 unsigned int minLength = GetMinLength(semantics);
269 if (buffer_bytes < minLength)
270 {
271 pkt_length = 0;
272 return false;
273 }
274 else
275 {
276 pkt_length = minLength + GetTlvLength();
277 return true;
278 }
279 } // end ManetTlv::InitFromBuffer()
280
281
282 UINT16 ManetTlv::GetTlvLength() const
283 {
284 if (HasValue())
285 {
286 if (HasExtendedLength())
287 return GetUINT16(OffsetLength());
288 else
289 return ((UINT16)GetUINT8(OffsetLength()));
290 }
291 else
292 {
293 return 0;
294 }
295 } // end ManetTlv::GetTlvLength()
296
297 UINT16 ManetTlv::GetValueLength(unsigned int numAddrs) const
298 {
299 UINT16 tlvLength = GetTlvLength();
300 if (IsMultiValue() && (0 != tlvLength))
301 {
302 ASSERT(GetIndexStop(numAddrs) >= GetIndexStart());
303 int numberValues = GetIndexStop(numAddrs) - GetIndexStart() + 1;
304 if (numberValues > 0)
305 {
306 if (0 != (tlvLength % numberValues))
307 {
308 PLOG(PL_WARN, "ManetTlv::GetValueLength() TLV value field not integral of num-values\n");
309 return 0;
310 }
311 return (tlvLength / numberValues);
312 }
313 else
314 {
315 return 0; // (TBD) print warning here
316 }
317 }
318 else
319 {
320 return tlvLength;
321 }
322 } // end ManetTlv::GetValueLength()
323
324 bool ManetTlv::GetValue(char* value, UINT16 valueLength, int index, unsigned int numAddrs) const
325 {
326 const char* valuePtr = GetValuePtr(valueLength, index, numAddrs);
327 if (NULL != valuePtr)
328 {
329 memcpy(value, valuePtr, valueLength);
330 return true;
331 }
332 else
333 {
334 return false;
335 }
336 } // end ManetTlv::GetValue()
337
338
339 const char* ManetTlv::GetValuePtr(UINT16 valueLength, int index, unsigned int numAddrs) const
340 {
341 if (!HasValue())
342 return NULL; // (TBD) print a warning here?
343
344 if (index < 0)
345 {
346 if (IsMultiValue())
347 {
348 PLOG(PL_ERROR, "ManetTlv::GetValuePtr() error: can't return non-indexed value for MultiValue TLV\n");
349 return NULL;
350 }
351 index = 0;
352 }
353 else if (HasNoIndex() && (0 == numAddrs))
354 {
355 index = 0; // ignore index
356 }
357 else if (HasSingleIndex())
358 {
359 if (index != GetIndexStart())
360 {
361 PLOG(PL_ERROR, "ManetTlv::GetValuePtr() error: invalid \"index\" for Single Index TLV\n");
362 return NULL;
363 }
364 index = 0;
365 }
366 else
367 {
368 if (IsMultiValue() || HasIndex())
369 {
370 int startIndex = GetIndexStart();
371 if ((index < startIndex) || (index > GetIndexStop(numAddrs)))
372 {
373 PLOG(PL_ERROR, "ManetTlv::GetValuePtr() error: invalid \"index\" out-of-range\n");
374 return NULL; // (TBD) print a warning here?
375 }
376 if (IsMultiValue())
377 index -= startIndex;
378 else
379 index = 0;
380 }
381 else
382 {
383 index = 0; // ignore index for non-indexed, single value tlvs
384 }
385 }
386 ASSERT((0 == index) || (valueLength == GetValueLength(numAddrs)));
387 UINT16 offset = OffsetValue() + (index * valueLength);
388 if ((offset + valueLength) > pkt_length)
389 {
390 PLOG(PL_ERROR, "ManetTlv::GetValuePtr() error: requested value exceeds packet size");
391 return NULL; // (TBD) print a warning here?
392 }
393 return ((char*)buffer_ptr + offset);
394 } // end ManetTlv::GetValue()
395
396
397 ManetTlvBlock::ManetTlvBlock()
398 : tlv_pending(false)
399 {
400 }
401
402 ManetTlvBlock::~ManetTlvBlock()
403 {
404 }
405
406 bool ManetTlvBlock::InitIntoBuffer(void* bufferPtr, unsigned int numBytes)
407 {
408 tlv_pending = false;
409 if (NULL != bufferPtr)
410 AttachBuffer(bufferPtr, numBytes);
411 if (buffer_bytes >= 2)
412 {
413 SetTlvBlockLength(0);
414 pkt_length = 2;
415 return true;
416 }
417 else
418 {
419 pkt_length = 0;
420 return false;
421 }
422 } // end ManetTlvBlock::InitIntoBuffer()
423
424 ManetTlv* ManetTlvBlock::AppendTlv(UINT8 type)
425 {
426 if (tlv_pending)
427 pkt_length += tlv_temp.GetLength();
428 tlv_pending =
429 tlv_temp.InitIntoBuffer(type, (char*)buffer_ptr + pkt_length, buffer_bytes - pkt_length);
430
431 return tlv_pending ? &tlv_temp : NULL;
432 } // end ManetTlvBlock::AppendTlv()
433
434 bool ManetTlvBlock::AppendTlv(ManetTlv& tlv)
435 {
436 if (tlv_pending)
437 {
438 pkt_length += tlv_temp.GetLength();
439 tlv_pending = false;
440 }
441 unsigned int tlvLength = tlv.GetLength();
442 unsigned int newLength = pkt_length + tlvLength;
443 if (buffer_bytes < newLength) return false;
444 memcpy((char*)buffer_ptr+pkt_length, (char*)tlv.GetBuffer(), tlvLength);
445 pkt_length += tlvLength;
446 return true;
447 } // end ManetTlvBlock::AppendTlv()
448
449 void ManetTlvBlock::Pack()
450 {
451 ASSERT(pkt_length >= 2);
452 if (tlv_pending)
453 {
454 pkt_length += tlv_temp.GetLength();
455 tlv_pending = false;
456 }
457 SetTlvBlockLength(pkt_length - 2);
458 } // end ManetTlvBlock::Pack()
459
460 bool ManetTlvBlock::InitFromBuffer(void* bufferPtr, unsigned numBytes)
461 {
462 if (NULL != bufferPtr) AttachBuffer(bufferPtr, numBytes);
463 if (buffer_bytes >= 2)
464 {
465 pkt_length = GetTlvBlockLength() + OFFSET_CONTENT;
466 return true;
467 }
468 else
469 {
470 pkt_length = 0;
471 return false;
472 }
473 } // end ManetTlvBlock::InitFromBuffer()
474
475 bool ManetTlvBlock::GetNextTlv(ManetTlv& tlv) const
476 {
477 char* currentBuffer = (char*)tlv.GetBuffer();
478 unsigned int nextOffset;
479 if (NULL == currentBuffer)
480 nextOffset = OFFSET_CONTENT;
481 else
482 nextOffset = currentBuffer - (char*)buffer_ptr + tlv.GetLength();
483
484 if (nextOffset < pkt_length)
485 return tlv.InitFromBuffer((char*)buffer_ptr + nextOffset, pkt_length - nextOffset);
486 else
487 return false;
488 } // end ManetTlvBlock::GetNextTlv()
489
490 ManetTlvBlock::Iterator::Iterator(ManetTlvBlock& tlvBlock)
491 : tlv_block(tlvBlock)
492 {
493 }
494
495 ManetTlvBlock::Iterator::~Iterator()
496 {
497 }
498
499
500 ManetAddrBlock::ManetAddrBlock()
501 : addr_length(0), tlv_block_pending(false)
502 {
503 }
504
505 ManetAddrBlock::ManetAddrBlock(void* bufferPtr,
506 unsigned int numBytes,
507 bool freeOnDestruct)
508 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct),
509 addr_length(0), tlv_block_pending(false)
510 {
511 }
512
513 ManetAddrBlock::~ManetAddrBlock()
514 {
515 }
516
517
518
519 bool ManetAddrBlock::InitIntoBuffer(void* bufferPtr, unsigned int numBytes, bool freeOnDestruct)
520 {
521 // minLength = num-addr field (1) + addr-semantics (1)
522 unsigned int minLength = 2;
523 if (NULL != bufferPtr)
524 AttachBuffer(bufferPtr, numBytes, freeOnDestruct);
525 if (buffer_bytes < minLength) return false;
526 addr_length = 0;
527 // Init "num-addr" and "addr-semantics" fields
528 SetAddressCount(0);
529 ClearAllSemantics();
530 tlv_block.AttachBuffer(NULL, 0);
531 tlv_block_pending = false;
532 pkt_length = 2; // don't include tlv-block part yet.
533 return true;
534 } // end ManetAddrBlock::InitIntoBuffer()
535 bool ManetAddrBlock::SetHead(const ProtoAddress& addr, UINT8 hlen)
536 {
537 // Re-init to be safe since head must be set first
538 ASSERT(addr.IsValid());
539 if ((hlen < 1) || (hlen > 127) || (hlen > addr.GetLength())) return false;
540 // minLength: 1 (num-addr) + 1 (addr-semantics) + 1 (head-length) + hlen (head)
541 unsigned int minLength = 3 + hlen;
542 if (buffer_bytes < minLength) return false;
543 // Save the addr_length, hlen, and head
544 addr_length = addr.GetLength();
545 // set HAS_HEAD semantic since there is a "head"
546 SetSemantic(HAS_HEAD);
547 SetUINT8(OFFSET_HEAD_LENGTH, hlen);
548 memcpy((char*)buffer_ptr+ OFFSET_HEAD, addr.GetRawHostAddress(), hlen);
549 pkt_length = OFFSET_HEAD + hlen; // don't include tlv-block part yet.
550 return true;
551 } // end ManetAddrBlock::SetHead()
552
553 bool ManetAddrBlock::SetTail(const ProtoAddress& addr, UINT8 tlen)
554 {
555 if (tlen > addr.GetLength()) return false;
556 unsigned int hlen = GetHeadLength();
557 if (0 == hlen)
558 {
559 // In case SetTail() is called first
560 //if (!InitIntoBuffer((char*)buffer_ptr, buffer_bytes)) return false;
561 addr_length = addr.GetLength();
562 }
563 else if (addr_length != addr.GetLength())
564 {
565 return false;
566 }
567 // minLength: 1 (num-addr) + 1 (addr-semantics) + [1 (head-length) + hlen (head)] + 1 (tail-length) + tlen (tail)
568 unsigned int minLength = 2 + (hlen ? (hlen + 1) : 0) + 1 + tlen;
569 if (tlen > 0)
570 {
571 // Is it an all-zero tail?
572 bool zeroTail = true;
573 const char* tailPtr = addr.GetRawHostAddress() + addr.GetLength() - tlen;
574 for (UINT8 i = 0; i < tlen; i++)
575 {
576 if (0 != tailPtr[i])
577 {
578 zeroTail = false;
579 break;
580 }
581 }
582 unsigned int offsetTail = OffsetTail();
583 if (zeroTail)
584 {
585 minLength -= tlen;
586 if (minLength > buffer_bytes) return false;
587 SetSemantic(HAS_ZERO_TAIL);
588 pkt_length = offsetTail; // don't include tlv-block part yet.
589 }
590 else
591 {
592 if (minLength > buffer_bytes) return false;
593 memcpy((char*)buffer_ptr + offsetTail, tailPtr, tlen);
594 pkt_length = offsetTail + tlen; // don't include tlv-block part yet.
595 SetSemantic(HAS_FULL_TAIL);
596 }
597 SetUINT8(OffsetTailLength(), tlen);
598 }
599 else
600 {
601 minLength -= 1; // don't need "tail-length" field
602 ClearSemantic(HAS_ZERO_TAIL);
603 ClearSemantic(HAS_FULL_TAIL);
604 pkt_length = HasHead() ? (OFFSET_HEAD + hlen) : (OFFSET_SEMANTICS + 1); // don't include tlv-block part yet.
605 }
606 return true;
607 } // end ManetAddrBlock::SetTail()
608
609 bool ManetAddrBlock::AppendAddress(const ProtoAddress& addr)
610 {
611 // (TBD) Should we make sure this addr prefix matches our "head" _and_ "tail"
612 if (!HasHead() && !HasTail())
613 {
614 addr_length = addr.GetLength();
615 }
616 else if (addr.GetLength() != addr_length)
617 {
618 PLOG(PL_ERROR,"ManetAddrBlock::AppendAddress() error appending address %s because the address length %d is not equal to the message addr length %d!\n",addr.GetHostString(),addr.GetLength(),addr_length);
619 return false;
620 }
621 UINT8 mlen = GetMidLength();
622 if (0 == mlen)
623 {
624 PLOG(PL_ERROR, "ManetAddrBlock::AppendAddress() error: address mid-length is zero\n");
625 return false;
626 }
627 UINT8 numAddr = GetAddressCount();
628 unsigned int offset = (0 == numAddr) ? OffsetMid() : pkt_length;
629 unsigned int minLength = offset + mlen;
630 if (buffer_bytes < minLength)
631 {
632 PLOG(PL_WARN,"ManetAddrBlock::AppendAddress() warn: buffer_bytes(%u) < minLength(%u) no room for packing message\n",buffer_bytes,minLength);
633 return false;
634 }
635 memcpy((char*)buffer_ptr + offset, addr.GetRawHostAddress() + GetHeadLength(), mlen);
636 ((UINT8*)buffer_ptr)[OFFSET_NUM_ADDR]++;
637 pkt_length = offset + mlen;
638 return true;
639 } // end ManetAddrBlock::AppendAddress()
640
641 bool ManetAddrBlock::SetPrefixLength(UINT8 length, UINT8 index)
642 {
643 ASSERT(length <= (GetAddressLength() << 3));
644 if (0 == GetAddressCount())
645 {
646 // Check for "head-only" or "tail-only" address block
647 if ((0 == GetHeadLength()) && (0 == GetTailLength()))
648 return false;
649 else
650 SetAddressCount(1);
651 }
652 ASSERT(index < GetAddressCount());
653 unsigned int offset = OffsetPrefixLength() + index;
654 unsigned int newLength = offset + 1;
655 if (newLength > buffer_bytes) return false;
656 if (!HasPrefixLength() && (0 == index))
657 {
658 SetSemantic(HAS_SINGLE_PREFIX_LEN);
659 }
660 else if (!HasMultiPrefixLength() && (index > 0))
661 {
662 ClearSemantic(HAS_SINGLE_PREFIX_LEN);
663 SetSemantic(HAS_MULTI_PREFIX_LEN);
664 }
665 SetUINT8(offset, length);
666 if (pkt_length < newLength) pkt_length = newLength;
667 return true;
668 } // end ManetAddrBlock::SetPrefixLength()
669
670 ManetTlv* ManetAddrBlock::AppendTlv(UINT8 type)
671 {
672 if (0 == GetAddressCount())
673 {
674 // Check for "head-only" or "tail-only" address block
675 if ((0 == GetHeadLength()) && (0 == GetTailLength()))
676 return NULL;
677 else
678 SetAddressCount(1);
679 }
680 if (!tlv_block_pending)
681 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr + pkt_length, buffer_bytes - pkt_length);
682 return tlv_block_pending ? tlv_block.AppendTlv(type) : NULL;
683 } // end ManetAddrBlock::AppendTlv()
684
685 bool ManetAddrBlock::AppendTlv(ManetTlv& tlv)
686 {
687 if (0 == GetAddressCount())
688 {
689 // Check for "head-only" or "tail-only" address block
690 if ((0 == GetHeadLength()) && (0 == GetTailLength()))
691 return false;
692 else
693 SetAddressCount(1);
694 }
695 if (!tlv_block_pending)
696 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr + pkt_length, buffer_bytes - pkt_length);
697 return (tlv_block_pending ? tlv_block.AppendTlv(tlv) : false);
698 } // end ManetAddrBlock::AppendTlv()
699
700 void ManetAddrBlock::Pack()
701 {
702 if (0 == GetAddressCount())
703 {
704 // Check for "head-only" or "tail-only" address block
705 if ((0 == GetHeadLength()) && (0 == GetTailLength()))
706 {
707 pkt_length = 0;
708 return;
709 }
710 else
711 {
712 SetAddressCount(1);
713 }
714 }
715 if (!tlv_block_pending)
716 tlv_block.InitIntoBuffer((char*)buffer_ptr + pkt_length, buffer_bytes - pkt_length);
717 tlv_block.Pack();
718 pkt_length += tlv_block.GetLength();
719 tlv_block_pending = false;
720 } // end ManetAddrBlock::Pack()
721
722 bool ManetAddrBlock::InitFromBuffer(UINT8 addrLength, void* bufferPtr, unsigned numBytes)
723 {
724 if (NULL != bufferPtr) AttachBuffer(bufferPtr, numBytes);
725 addr_length = addrLength;
726 pkt_length = 0;
727 unsigned int minLength = 2;
728 if (buffer_bytes < minLength) return false;
729 if (HasHead())
730 {
731 minLength +=1;
732 if (buffer_bytes < minLength) return false;
733 minLength += GetHeadLength();
734 if (buffer_bytes < minLength) return false;
735 }
736 if (HasTail())
737 {
738 minLength += 1;
739 if (buffer_bytes < minLength) return false;
740 minLength += HasZeroTail() ? 0 : GetTailLength();
741 if (buffer_bytes < minLength) return false;
742 }
743
744 // Make sure big enough for any listed address mid-sections, prefixes,
745 // and at least tlv-block-length
746 minLength += GetAddressCount() * GetMidLength();
747 if (buffer_bytes < (minLength + 2)) return false;
748 if (HasPrefixLength())
749 {
750 minLength += HasSinglePrefixLength() ? 1 : GetAddressCount();
751 if (buffer_bytes < minLength) return false;
752 }
753 if (tlv_block.InitFromBuffer((char*)buffer_ptr+minLength, buffer_bytes - minLength))
754 {
755 pkt_length = minLength + tlv_block.GetLength();
756 return true;
757 }
758 else
759 {
760 pkt_length = 0;
761 return false;
762 }
763 } // end ManetAddrBlock::InitFromBuffer()
764
765 bool ManetAddrBlock::GetAddress(UINT8 index, ProtoAddress& theAddr) const
766 {
767 ASSERT(index < GetAddressCount());
768 char addrBuffer[16]; // supports up to IPv6
769 // Copy head into addrBuffer
770 UINT8 hlen = GetHeadLength();
771 ASSERT(hlen <= GetAddressLength());
772 if (hlen > 0)
773 memcpy(addrBuffer, (char*)buffer_ptr+OFFSET_HEAD, hlen);
774 // Copy mid into addrBuffer
775 UINT8 mlen = GetMidLength();
776 ASSERT(mlen <= GetAddressLength());
777 if (mlen > 0)
778 memcpy(addrBuffer+hlen, (char*)buffer_ptr + OffsetMid() + index*GetMidLength(), mlen);
779 UINT8 tlen = GetTailLength();
780 // Copy tail into addrBuffer
781 if (tlen > 0)
782 {
783 if (HasZeroTail())
784 memset(addrBuffer+hlen+mlen, 0, tlen);
785 else
786 memcpy(addrBuffer+hlen+mlen, (char*)buffer_ptr + OffsetTail(), tlen);
787 }
788 ASSERT((hlen + mlen + tlen) == GetAddressLength());
789 theAddr.SetRawHostAddress(GetAddressType(), addrBuffer, GetAddressLength());
790 return theAddr.IsValid();
791 } // end ManetAddrBlock::GetAddress()
792
793 ManetAddrBlock::TlvIterator::TlvIterator(ManetAddrBlock& addrBlk)
794 : ManetTlvBlock::Iterator(addrBlk.tlv_block)
795 {
796 }
797
798 ManetAddrBlock::TlvIterator::~TlvIterator()
799 {
800 }
801
802 ManetMsg::ManetMsg()
803 {
804 }
805
806 ManetMsg::ManetMsg(void* bufferPtr,
807 unsigned int numBytes,
808 bool freeOnDestruct)
809 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct),
810 tlv_block_pending(false), addr_block_pending(false)
811 {
812 }
813
814 ManetMsg::~ManetMsg()
815 {
816 }
817
818 bool ManetMsg::InitIntoBuffer(void* bufferPtr, unsigned int numBytes)
819 {
820 addr_block_pending = false;
821 if (NULL != bufferPtr)
822 AttachBuffer(bufferPtr, numBytes);
823 // At least 4 bytes required for "type", "semantics" and "size"
824 if (buffer_bytes >= 4)
825 {
826 pkt_length = 4;
827 ClearAllSemantics(); // addr length & semantic flags will be set w/ corresponding 'Set' calls
828 // Assume initial msg-tlv-block location
829 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr+4, buffer_bytes-4);
830 return tlv_block_pending;
831 }
832 else
833 {
834 pkt_length = 0;
835 return false;
836 }
837 } // end ManetMsg::InitIntoBuffer()
838
839 bool ManetMsg::SetOriginator(const ProtoAddress& addr)
840 {
841 if (!addr.IsValid())
842 {
843 PLOG(PL_ERROR, "ManetMsg::SetOriginator() error: invalid address!\n");
844 return false;
845 }
846 UINT8 addrLen = addr.GetLength();
847 SetAddressLength(addrLen);
848 unsigned int offsetOriginator = OffsetOriginator();
849 if (!HasOriginator())
850 {
851 UINT16 newLength = offsetOriginator + addrLen;
852 if (newLength > buffer_bytes) return false;
853 // Try to move the msg-tlv-block location
854 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr+newLength, buffer_bytes-newLength);
855 if (tlv_block_pending)
856 {
857 pkt_length = newLength;
858 SetSemantic(HAS_ORIGINATOR);
859 }
860 else
861 {
862 return false;
863 }
864 }
865 memcpy((char*)buffer_ptr + offsetOriginator, addr.GetRawHostAddress(), addrLen);
866 return true;
867 } // end ManetMsg::SetOriginator()
868
869 bool ManetMsg::SetHopLimit(UINT8 hopLimit)
870 {
871 unsigned int offsetHopLimit = OffsetHopLimit();
872 if (!HasHopLimit())
873 {
874 UINT16 newLength = offsetHopLimit + 1;
875 if (newLength > buffer_bytes) return false;
876 // Try to move the msg-tlv-block location
877 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr+newLength, buffer_bytes-newLength);
878 if (tlv_block_pending)
879 {
880 pkt_length = newLength;
881 SetSemantic(HAS_HOP_LIMIT);
882 }
883 else
884 {
885 return false;
886 }
887 }
888 SetUINT8(offsetHopLimit, hopLimit);
889 return true;
890 } // end ManetMsg::SetHopLimit()
891
892 bool ManetMsg::SetHopCount(UINT8 hopCount)
893 {
894 unsigned int offsetHopCount = OffsetHopCount();
895 if (!HasHopCount())
896 {
897 UINT16 newLength = offsetHopCount + 1;
898 if (newLength > buffer_bytes) return false;
899 // Try to move the msg-tlv-block location
900 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr+newLength, buffer_bytes-newLength);
901 if (tlv_block_pending)
902 {
903 pkt_length = newLength;
904 SetSemantic(HAS_HOP_COUNT);
905 }
906 else
907 {
908 return false;
909 }
910 }
911 SetUINT8(offsetHopCount, hopCount);
912 return true;
913 } // end ManetMsg::SetHopCount()
914
915 bool ManetMsg::SetSequence(UINT16 sequence)
916 {
917 unsigned int offsetSequence = OffsetSequence();
918 if (!HasSequence())
919 {
920 UINT16 newLength = offsetSequence + 2;
921 if (newLength > buffer_bytes) return false;
922 // Try to move the msg-tlv-block location
923 tlv_block_pending = tlv_block.InitIntoBuffer((char*)buffer_ptr+newLength, buffer_bytes-newLength);
924 if (tlv_block_pending)
925 {
926 pkt_length = newLength;
927 SetSemantic(HAS_SEQ_NUM);
928 }
929 else
930 {
931 return false;
932 }
933 }
934 SetUINT8(offsetSequence, sequence);
935 return true;
936 } // end ManetMsg::SetSequence()
937
938 ManetAddrBlock* ManetMsg::AppendAddressBlock()
939 {
940 if (tlv_block_pending)
941 {
942 tlv_block.Pack();
943 pkt_length += tlv_block.GetLength();
944 tlv_block_pending = false;
945 }
946 if (addr_block_pending)
947 {
948 addr_block_temp.Pack();
949 pkt_length += addr_block_temp.GetLength();
950 }
951 unsigned int bufferMax = (buffer_bytes > pkt_length) ? buffer_bytes - pkt_length : 0;
952 char* bufferPtr = (char*)buffer_ptr + pkt_length;
953 addr_block_pending = addr_block_temp.InitIntoBuffer(bufferPtr, bufferMax);
954 return (addr_block_pending ? &addr_block_temp : NULL);
955 } // end ManetMsg::AppendAddressBlock()
956
957 void ManetMsg::Pack()
958 {
959 if (tlv_block_pending)
960 {
961 tlv_block.Pack();
962 pkt_length += tlv_block.GetLength();
963 tlv_block_pending = false;
964 }
965 if (addr_block_pending)
966 {
967 addr_block_temp.Pack();
968 pkt_length += addr_block_temp.GetLength();
969 addr_block_pending = false;
970 }
971 SetMsgSize((UINT16)pkt_length); // includes message header _and_ body
972 } // end ManetMsg::Pack()
973
974 bool ManetMsg::InitFromBuffer(void* bufferPtr, unsigned int numBytes)
975 {
976 if (NULL != bufferPtr) AttachBuffer(bufferPtr, numBytes);
977 if (buffer_bytes < 1) return false; // not even enough for type/semantic fields
978 //pkt_length = (buffer_bytes >= (OffsetSize() + 2)) ? GetMsgSize() : 0;
979 pkt_length = GetMsgSize();
980
981 if ((0 == pkt_length) || (pkt_length > buffer_bytes))
982 {
983 PLOG(PL_ERROR, "ManetMsg::InitFromBuffer() error: msg size:%u larger than buffer size:%u\n",
984 pkt_length, buffer_bytes);
985 pkt_length = 0;
986 return false;
987 }
988 UINT16 tlvBlockOffset = OffsetTlvBlock();
989 if (pkt_length < tlvBlockOffset)
990 {
991 PLOG(PL_ERROR, "ManetMsg::InitFromBuffer() error: msg too short?!\n");
992 pkt_length = 0;
993 return false;
994 }
995 if (tlv_block.InitFromBuffer((char*)buffer_ptr + tlvBlockOffset, pkt_length - tlvBlockOffset))
996 {
997 return true;
998 }
999 else
1000 {
1001 PLOG(PL_ERROR, "ManetMsg::InitFromBuffer() error: invalid msg-tlv-block?!\n");
1002 pkt_length = 0;
1003 return false;
1004 }
1005 } // end ManetMsg::InitFromBuffer()
1006
1007 bool ManetMsg::GetOriginator(ProtoAddress& addr) const
1008 {
1009 if (HasOriginator())
1010 {
1011 UINT8 addrLen = GetAddressLength();
1012 addr.SetRawHostAddress(ProtoAddress::GetType(addrLen), // infers from msg-addr-length
1013 (char*)buffer_ptr+OffsetOriginator(),
1014 addrLen);
1015 }
1016 else
1017 {
1018 addr.Invalidate();
1019 }
1020 return addr.IsValid();
1021 } // ManetMsg::GetOriginator()
1022
1023 bool ManetMsg::GetNextAddressBlock(ManetAddrBlock& addrBlk) const
1024 {
1025 char* nextBuffer = (char*)addrBlk.GetBuffer();
1026 if (NULL == nextBuffer)
1027 {
1028 // Get _first_ address block
1029 ManetTlvBlock tlvBlk;
1030 if (GetTlvBlock(tlvBlk))
1031 nextBuffer = (char*)tlvBlk.GetBuffer() + tlvBlk.GetLength();
1032 else
1033 return false; // there was no <msg-tlv-block>
1034 }
1035 else
1036 {
1037 nextBuffer += addrBlk.GetLength();
1038 }
1039 unsigned int offset = nextBuffer - (char*)buffer_ptr;
1040 if (offset < pkt_length)
1041 return addrBlk.InitFromBuffer(GetAddressLength(), nextBuffer, pkt_length - offset);
1042 else
1043 return false;
1044 } // end ManetMsg::GetNextAddressBlock()
1045
1046
1047 ManetMsg::TlvIterator::TlvIterator(ManetMsg& msg)
1048 : ManetTlvBlock::Iterator(msg.tlv_block)
1049 {
1050 }
1051
1052 ManetMsg::TlvIterator::~TlvIterator()
1053 {
1054 }
1055
1056 ManetMsg::AddrBlockIterator::AddrBlockIterator(ManetMsg& theMsg)
1057 : msg(theMsg)
1058 {
1059 }
1060
1061 ManetMsg::AddrBlockIterator::~AddrBlockIterator()
1062 {
1063 }
1064
1065
1066 ManetPkt::ManetPkt()
1067 {
1068 }
1069
1070 ManetPkt::ManetPkt(void* bufferPtr,
1071 unsigned int numBytes,
1072 bool freeOnDestruct)
1073 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct),
1074 tlv_block_pending(false), msg_pending(false)
1075 {
1076 }
1077
1078 ManetPkt::~ManetPkt()
1079 {
1080 }
1081
1082 bool ManetPkt::InitIntoBuffer(void* bufferPtr, unsigned int numBytes)
1083 {
1084 if (NULL != bufferPtr)
1085 AttachBuffer(bufferPtr, numBytes);
1086 if (buffer_bytes < 1) return false;
1087 ClearAllSemantics();
1088 //ClearOrderConstraint(); // cleared by default because we don't like it
1089 pkt_length = 1;
1090 tlv_block_pending = false;
1091 msg_pending = false;
1092 return true;
1093 } // end ManetPkt::InitIntoBuffer()
1094
1095 void ManetPkt::SetVersion(UINT8 version)
1096 {
1097 uint8_t flags = GetUINT8(OFFSET_SEMANTICS) & 0x0F; // clear version
1098 SetUINT8(OFFSET_SEMANTICS, (version << 4) | flags);
1099 } // end ManetPkt::SetVersion()
1100
1101 void ManetPkt::SetSequence(UINT16 sequence)
1102 {
1103 if (!SemanticIsSet(HAS_SEQ_NUM))
1104 {
1105 SetSemantic(HAS_SEQ_NUM);
1106 pkt_length += 2;
1107 }
1108 SetUINT16(OffsetSequence(), sequence);
1109 } // end ManetPkt::SetSequence()
1110
1111 ManetTlv* ManetPkt::AppendTlv(UINT8 type)
1112 {
1113 if (msg_pending)
1114 return NULL; // Can't append Tlv after appending a message!
1115 if (!tlv_block_pending)
1116 {
1117 // Set semantic and init tlv_block
1118 SetSemantic(HAS_TLV_BLOCK);
1119 unsigned int offsetTlvBlock = OffsetTlvBlock();
1120 tlv_block_pending =
1121 tlv_block.InitIntoBuffer((char*)buffer_ptr+offsetTlvBlock, buffer_bytes-offsetTlvBlock);
1122 }
1123 return (tlv_block_pending ? tlv_block.AppendTlv(type) : NULL);
1124 } // end ManetPkt::AppendTlv()
1125
1126 bool ManetPkt::AppendTlv(ManetTlv& tlv)
1127 {
1128 if (msg_pending)
1129 return false; // Can't append Tlv after appending a message!
1130 if (!tlv_block_pending)
1131 {
1132 // Set semantic and init tlv_block
1133 SetSemantic(HAS_TLV_BLOCK);
1134 unsigned int offsetTlvBlock = OffsetTlvBlock();
1135 tlv_block_pending =
1136 tlv_block.InitIntoBuffer((char*)buffer_ptr+offsetTlvBlock, buffer_bytes-offsetTlvBlock);
1137 }
1138 return (tlv_block_pending ? tlv_block.AppendTlv(tlv) : false);
1139 } // end ManetPkt::AppendTlv()
1140
1141 ManetMsg* ManetPkt::AppendMessage()
1142 {
1143 if (tlv_block_pending)
1144 {
1145 tlv_block.Pack();
1146 pkt_length += tlv_block.GetLength();
1147 tlv_block_pending = false;
1148 }
1149 if (msg_pending)
1150 {
1151 msg_temp.Pack();
1152 pkt_length += msg_temp.GetLength();
1153 }
1154 unsigned int bufferMax = (buffer_bytes > pkt_length) ? buffer_bytes - pkt_length : 0;
1155 char* bufferPtr = (char*)buffer_ptr + pkt_length;
1156 msg_pending = msg_temp.InitIntoBuffer(bufferPtr, bufferMax);
1157 return msg_pending ? &msg_temp : NULL;
1158 } // end ManetPkt::AppendMessage()
1159
1160
1161 bool ManetPkt::AppendMessage(ManetMsg& msg)
1162 {
1163 if (tlv_block_pending)
1164 {
1165 tlv_block.Pack();
1166 pkt_length += tlv_block.GetLength();
1167 tlv_block_pending = false;
1168 }
1169 if (msg_pending)
1170 {
1171 msg_temp.Pack();
1172 pkt_length += msg_temp.GetLength();
1173 }
1174 msg.Pack(); // just to be safe
1175 unsigned int msgLength = msg.GetLength();
1176 unsigned int newLength = pkt_length + msgLength;
1177 if (buffer_bytes < newLength) return false;
1178 memcpy((char*)buffer_ptr + pkt_length, (char*)msg.GetBuffer(), msgLength);
1179 pkt_length = newLength;
1180 return true;
1181 } // end ManetPkt::AppendMessage()
1182
1183
1184 void ManetPkt::Pack()
1185 {
1186 if (tlv_block_pending)
1187 {
1188 tlv_block.Pack();
1189 pkt_length += tlv_block.GetLength();
1190 tlv_block_pending = false;
1191 }
1192 if (msg_pending)
1193 {
1194 msg_temp.Pack();
1195 pkt_length += msg_temp.GetLength();
1196 msg_pending = false;
1197 }
1198 //if (HasSize()) SetPktSize(pkt_length);
1199 } // end ManetPkt::Pack()
1200
1201 bool ManetPkt::InitFromBuffer(unsigned int pktLength, void* bufferPtr, unsigned int numBytes)
1202 {
1203 if (NULL != bufferPtr)
1204 AttachBuffer(bufferPtr, numBytes);
1205 msg_pending = false;
1206 if ((pktLength < 1) || (buffer_bytes < pktLength))
1207 return false;
1208 if (pktLength < GetMinLength(GetUINT8(OFFSET_SEMANTICS)))
1209 return false;
1210 else
1211 pkt_length = pktLength;
1212
1213 if (HasTlvBlock())
1214 {
1215 unsigned tlvBlockOffset = OffsetTlvBlock();
1216 char* tlvBuffer = (char*)buffer_ptr + tlvBlockOffset;
1217 return tlv_block.InitFromBuffer(tlvBuffer, pkt_length - tlvBlockOffset);
1218 }
1219 else
1220 {
1221 tlv_block.InitFromBuffer(NULL, 0);
1222 return true;
1223 }
1224 } // end ManetPkt::InitFromBuffer()
1225
1226 bool ManetPkt::GetNextMessage(ManetMsg& msg)
1227 {
1228 char* currentBuffer = (char*)msg.GetBuffer();
1229 char* nextBuffer = currentBuffer ?
1230 (currentBuffer + msg.GetLength()) :
1231 ((char*)buffer_ptr + OffsetPayload());
1232 unsigned int nextOffset = nextBuffer - (char*)buffer_ptr;
1233 if (nextOffset < pkt_length)
1234 {
1235 return msg.InitFromBuffer(nextBuffer, pkt_length - nextOffset);
1236 }
1237 else
1238 return false; // no more messages in packet
1239 } // end ManetPkt::GetNextMessage()
1240
1241 ManetPkt::TlvIterator::TlvIterator(ManetPkt& pkt)
1242 : ManetTlvBlock::Iterator(pkt.tlv_block)
1243 {
1244 }
1245
1246 ManetPkt::TlvIterator::~TlvIterator()
1247 {
1248 }
1249
1250 ManetPkt::MsgIterator::MsgIterator(ManetPkt& thePkt)
1251 : pkt(thePkt)
1252 {
1253 }
1254
1255 ManetPkt::MsgIterator::~MsgIterator()
1256 {
1257 }