|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* vim: set noai ts=2 sw=2 expandtab: */ |
|
3 /* |
|
4 * Copyright (c) 2009 Drexel University |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Tom Wambold <tom5760@gmail.com> |
|
20 */ |
|
21 /* TODO: |
|
22 * - Check style |
|
23 * - Check copy constructors |
|
24 */ |
|
25 |
|
26 #include "ns3/ipv4-address.h" |
|
27 #include "ns3/ipv6-address.h" |
|
28 |
|
29 #include "packetbb.h" |
|
30 |
|
31 static const uint8_t VERSION = 0; |
|
32 /* Packet flags */ |
|
33 static const uint8_t PHAS_SEQ_NUM = 0x8; |
|
34 static const uint8_t PHAS_TLV = 0x4; |
|
35 |
|
36 /* Message flags */ |
|
37 static const uint8_t MHAS_ORIG = 0x80; |
|
38 static const uint8_t MHAS_HOP_LIMIT = 0x40; |
|
39 static const uint8_t MHAS_HOP_COUNT = 0x20; |
|
40 static const uint8_t MHAS_SEQ_NUM = 0x10; |
|
41 |
|
42 /* Address block flags */ |
|
43 static const uint8_t AHAS_HEAD = 0x80; |
|
44 static const uint8_t AHAS_FULL_TAIL = 0x40; |
|
45 static const uint8_t AHAS_ZERO_TAIL = 0x20; |
|
46 static const uint8_t AHAS_SINGLE_PRE_LEN = 0x10; |
|
47 static const uint8_t AHAS_MULTI_PRE_LEN = 0x08; |
|
48 |
|
49 /* TLV Flags */ |
|
50 static const uint8_t THAS_TYPE_EXT = 0x80; |
|
51 static const uint8_t THAS_SINGLE_INDEX = 0x40; |
|
52 static const uint8_t THAS_MULTI_INDEX = 0x20; |
|
53 static const uint8_t THAS_VALUE = 0x10; |
|
54 static const uint8_t THAS_EXT_LEN = 0x08; |
|
55 static const uint8_t TIS_MULTIVALUE = 0x04; |
|
56 |
|
57 namespace ns3 { |
|
58 |
|
59 namespace pbb { |
|
60 |
|
61 NS_OBJECT_ENSURE_REGISTERED (PacketBB); |
|
62 |
|
63 TlvBlock::Iterator |
|
64 TlvBlock::Begin (void) |
|
65 { |
|
66 return m_tlvList.begin (); |
|
67 } |
|
68 |
|
69 TlvBlock::ConstIterator |
|
70 TlvBlock::Begin (void) const |
|
71 { |
|
72 return m_tlvList.begin (); |
|
73 } |
|
74 |
|
75 TlvBlock::Iterator |
|
76 TlvBlock::End (void) |
|
77 { |
|
78 return m_tlvList.end (); |
|
79 } |
|
80 |
|
81 TlvBlock::ConstIterator |
|
82 TlvBlock::End (void) const |
|
83 { |
|
84 return m_tlvList.end (); |
|
85 } |
|
86 |
|
87 int |
|
88 TlvBlock::Size (void) const |
|
89 { |
|
90 return m_tlvList.size (); |
|
91 } |
|
92 |
|
93 bool |
|
94 TlvBlock::Empty (void) const |
|
95 { |
|
96 return m_tlvList.empty (); |
|
97 } |
|
98 |
|
99 Ptr<Tlv> |
|
100 TlvBlock::Front (void) const |
|
101 { |
|
102 return m_tlvList.front (); |
|
103 } |
|
104 |
|
105 Ptr<Tlv> |
|
106 TlvBlock::Back (void) const |
|
107 { |
|
108 return m_tlvList.back (); |
|
109 } |
|
110 |
|
111 void |
|
112 TlvBlock::PushFront (Ptr<Tlv> tlv) |
|
113 { |
|
114 //Ptr<Tlv> * newptr = new Ptr<Tlv> (GetPointer<Tlv> (tlv)); |
|
115 m_tlvList.push_front (tlv); |
|
116 } |
|
117 |
|
118 void |
|
119 TlvBlock::PopFront (void) |
|
120 { |
|
121 m_tlvList.pop_front (); |
|
122 } |
|
123 |
|
124 void |
|
125 TlvBlock::PushBack (Ptr<Tlv> tlv) |
|
126 { |
|
127 //Ptr<Tlv> * newptr = new Ptr<Tlv> (GetPointer<Tlv> (tlv)); |
|
128 m_tlvList.push_back (tlv); |
|
129 } |
|
130 |
|
131 void |
|
132 TlvBlock::PopBack (void) |
|
133 { |
|
134 m_tlvList.pop_back (); |
|
135 } |
|
136 |
|
137 TlvBlock::Iterator |
|
138 TlvBlock::Insert (TlvBlock::Iterator position, const Ptr<Tlv> tlv) |
|
139 { |
|
140 return m_tlvList.insert (position, tlv); |
|
141 } |
|
142 |
|
143 TlvBlock::Iterator |
|
144 TlvBlock::Erase (TlvBlock::Iterator position) |
|
145 { |
|
146 return m_tlvList.erase (position); |
|
147 } |
|
148 |
|
149 TlvBlock::Iterator |
|
150 TlvBlock::Erase (TlvBlock::Iterator first, TlvBlock::Iterator last) |
|
151 { |
|
152 return m_tlvList.erase (first, last); |
|
153 } |
|
154 |
|
155 void |
|
156 TlvBlock::Clear (void) |
|
157 { |
|
158 m_tlvList.clear (); |
|
159 } |
|
160 |
|
161 uint32_t |
|
162 TlvBlock::GetSerializedSize (void) const |
|
163 { |
|
164 /* tlv size */ |
|
165 uint32_t size = 2; |
|
166 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
167 { |
|
168 size += (*iter)->GetSerializedSize (); |
|
169 } |
|
170 return size; |
|
171 } |
|
172 |
|
173 void |
|
174 TlvBlock::Serialize (Buffer::Iterator &start) const |
|
175 { |
|
176 if (Empty ()) |
|
177 { |
|
178 start.WriteHtonU16 (0); |
|
179 return; |
|
180 } |
|
181 |
|
182 /* We need to write the size of the TLV block in front, so save its |
|
183 * position. */ |
|
184 Buffer::Iterator tlvsize = start; |
|
185 start.Next (2); |
|
186 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
187 { |
|
188 (*iter)->Serialize (start); |
|
189 } |
|
190 /* - 2 to not include the size field */ |
|
191 uint16_t size = start.GetDistanceFrom (tlvsize) - 2; |
|
192 tlvsize.WriteHtonU16 (size); |
|
193 } |
|
194 |
|
195 void |
|
196 TlvBlock::Deserialize (Buffer::Iterator &start) |
|
197 { |
|
198 uint16_t size = start.ReadNtohU16 (); |
|
199 |
|
200 Buffer::Iterator tlvstart = start; |
|
201 if (size > 0) |
|
202 { |
|
203 while (start.GetDistanceFrom (tlvstart) < size) |
|
204 { |
|
205 Ptr<Tlv> newtlv = Create<Tlv> (); |
|
206 //Tlv * newtlv = new Tlv (); |
|
207 newtlv->Deserialize (start); |
|
208 |
|
209 //Ptr<Tlv> * newptr = new Ptr<Tlv> (newtlv); |
|
210 PushBack (newtlv); |
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 void |
|
216 TlvBlock::Print (std::ostream &os) const |
|
217 { |
|
218 Print (os, 0); |
|
219 } |
|
220 |
|
221 void |
|
222 TlvBlock::Print (std::ostream &os, int level) const |
|
223 { |
|
224 std::string prefix = ""; |
|
225 for (int i = 0; i < level; i++) |
|
226 prefix.append("\t"); |
|
227 |
|
228 os << prefix << "TLV Block {" << std::endl; |
|
229 os << prefix << "\tsize = " << Size () << std::endl; |
|
230 os << prefix << "\tmembers [" << std::endl; |
|
231 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
232 { |
|
233 (*iter)->Print (os, level+2); |
|
234 } |
|
235 os << prefix << "\t]" << std::endl; |
|
236 os << prefix << "}" << std::endl; |
|
237 } |
|
238 |
|
239 bool |
|
240 TlvBlock::operator== (const TlvBlock &other) const |
|
241 { |
|
242 if (Size () != other.Size ()) |
|
243 return false; |
|
244 |
|
245 ConstIterator ti, oi; |
|
246 for (ti = Begin (), oi = other.Begin (); |
|
247 ti != End () && oi != other.End (); |
|
248 ti++, oi++) |
|
249 { |
|
250 if (**ti != **oi) |
|
251 return false; |
|
252 } |
|
253 return true; |
|
254 } |
|
255 |
|
256 bool |
|
257 TlvBlock::operator!= (const TlvBlock &other) const |
|
258 { |
|
259 return !(*this == other); |
|
260 } |
|
261 |
|
262 /* End TlvBlock class */ |
|
263 |
|
264 AddressTlvBlock::Iterator |
|
265 AddressTlvBlock::Begin (void) |
|
266 { |
|
267 return m_tlvList.begin (); |
|
268 } |
|
269 |
|
270 AddressTlvBlock::ConstIterator |
|
271 AddressTlvBlock::Begin (void) const |
|
272 { |
|
273 return m_tlvList.begin (); |
|
274 } |
|
275 |
|
276 AddressTlvBlock::Iterator |
|
277 AddressTlvBlock::End (void) |
|
278 { |
|
279 return m_tlvList.end (); |
|
280 } |
|
281 |
|
282 AddressTlvBlock::ConstIterator |
|
283 AddressTlvBlock::End (void) const |
|
284 { |
|
285 return m_tlvList.end (); |
|
286 } |
|
287 |
|
288 int |
|
289 AddressTlvBlock::Size (void) const |
|
290 { |
|
291 return m_tlvList.size (); |
|
292 } |
|
293 |
|
294 bool |
|
295 AddressTlvBlock::Empty (void) const |
|
296 { |
|
297 return m_tlvList.empty (); |
|
298 } |
|
299 |
|
300 Ptr<AddressTlv> |
|
301 AddressTlvBlock::Front (void) const |
|
302 { |
|
303 return m_tlvList.front (); |
|
304 } |
|
305 |
|
306 Ptr<AddressTlv> |
|
307 AddressTlvBlock::Back (void) const |
|
308 { |
|
309 return m_tlvList.back (); |
|
310 } |
|
311 |
|
312 void |
|
313 AddressTlvBlock::PushFront (Ptr<AddressTlv> tlv) |
|
314 { |
|
315 m_tlvList.push_front (tlv); |
|
316 } |
|
317 |
|
318 void |
|
319 AddressTlvBlock::PopFront (void) |
|
320 { |
|
321 m_tlvList.pop_front (); |
|
322 } |
|
323 |
|
324 void |
|
325 AddressTlvBlock::PushBack (Ptr<AddressTlv> tlv) |
|
326 { |
|
327 m_tlvList.push_back (tlv); |
|
328 } |
|
329 |
|
330 void |
|
331 AddressTlvBlock::PopBack (void) |
|
332 { |
|
333 m_tlvList.pop_back (); |
|
334 } |
|
335 |
|
336 AddressTlvBlock::Iterator |
|
337 AddressTlvBlock::Insert (AddressTlvBlock::Iterator position, const Ptr<AddressTlv> tlv) |
|
338 { |
|
339 return m_tlvList.insert (position, tlv); |
|
340 } |
|
341 |
|
342 AddressTlvBlock::Iterator |
|
343 AddressTlvBlock::Erase (AddressTlvBlock::Iterator position) |
|
344 { |
|
345 return m_tlvList.erase (position); |
|
346 } |
|
347 |
|
348 AddressTlvBlock::Iterator |
|
349 AddressTlvBlock::Erase (AddressTlvBlock::Iterator first, AddressTlvBlock::Iterator last) |
|
350 { |
|
351 return m_tlvList.erase (first, last); |
|
352 } |
|
353 |
|
354 void |
|
355 AddressTlvBlock::Clear (void) |
|
356 { |
|
357 m_tlvList.clear (); |
|
358 } |
|
359 |
|
360 uint32_t |
|
361 AddressTlvBlock::GetSerializedSize (void) const |
|
362 { |
|
363 /* tlv size */ |
|
364 uint32_t size = 2; |
|
365 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
366 { |
|
367 size += (*iter)->GetSerializedSize (); |
|
368 } |
|
369 return size; |
|
370 } |
|
371 |
|
372 void |
|
373 AddressTlvBlock::Serialize (Buffer::Iterator &start) const |
|
374 { |
|
375 if (Empty ()) |
|
376 { |
|
377 start.WriteHtonU16 (0); |
|
378 return; |
|
379 } |
|
380 |
|
381 /* We need to write the size of the TLV block in front, so save its |
|
382 * position. */ |
|
383 Buffer::Iterator tlvsize = start; |
|
384 start.Next (2); |
|
385 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
386 { |
|
387 (*iter)->Serialize (start); |
|
388 } |
|
389 /* - 2 to not include the size field */ |
|
390 uint16_t size = start.GetDistanceFrom (tlvsize) - 2; |
|
391 tlvsize.WriteHtonU16 (size); |
|
392 } |
|
393 |
|
394 void |
|
395 AddressTlvBlock::Deserialize (Buffer::Iterator &start) |
|
396 { |
|
397 uint16_t size = start.ReadNtohU16 (); |
|
398 |
|
399 Buffer::Iterator tlvstart = start; |
|
400 if (size > 0) |
|
401 { |
|
402 while (start.GetDistanceFrom (tlvstart) < size) |
|
403 { |
|
404 Ptr<AddressTlv> newtlv = Create<AddressTlv> (); |
|
405 //AddressTlv * newtlv = new AddressTlv (); |
|
406 newtlv->Deserialize (start); |
|
407 |
|
408 //Ptr<AddressTlv> * newptr = new Ptr<AddressTlv> (newtlv); |
|
409 PushBack (newtlv); |
|
410 } |
|
411 } |
|
412 } |
|
413 |
|
414 void |
|
415 AddressTlvBlock::Print (std::ostream &os) const |
|
416 { |
|
417 Print (os, 0); |
|
418 } |
|
419 |
|
420 void |
|
421 AddressTlvBlock::Print (std::ostream &os, int level) const |
|
422 { |
|
423 std::string prefix = ""; |
|
424 for (int i = 0; i < level; i++) |
|
425 prefix.append("\t"); |
|
426 |
|
427 os << prefix << "TLV Block {" << std::endl; |
|
428 os << prefix << "\tsize = " << Size () << std::endl; |
|
429 os << prefix << "\tmembers [" << std::endl; |
|
430 for (ConstIterator iter = Begin (); iter != End (); iter++) |
|
431 { |
|
432 (*iter)->Print (os, level+2); |
|
433 } |
|
434 os << prefix << "\t]" << std::endl; |
|
435 os << prefix << "}" << std::endl; |
|
436 } |
|
437 |
|
438 bool |
|
439 AddressTlvBlock::operator== (const AddressTlvBlock &other) const |
|
440 { |
|
441 if (Size () != other.Size ()) |
|
442 return false; |
|
443 |
|
444 ConstIterator it, ot; |
|
445 for (it = Begin (), ot = other.Begin (); |
|
446 it != End () && ot != other.End (); |
|
447 it++, ot++) |
|
448 { |
|
449 if (**it != **ot) |
|
450 return false; |
|
451 } |
|
452 return true; |
|
453 } |
|
454 |
|
455 bool |
|
456 AddressTlvBlock::operator!= (const AddressTlvBlock &other) const |
|
457 { |
|
458 return !(*this == other); |
|
459 } |
|
460 |
|
461 |
|
462 /* End AddressTlvBlock Class */ |
|
463 |
|
464 PacketBB::PacketBB (void) |
|
465 { |
|
466 m_refCount = 1; |
|
467 m_version = VERSION; |
|
468 m_hasseqnum = false; |
|
469 } |
|
470 |
|
471 uint8_t |
|
472 PacketBB::GetVersion (void) const |
|
473 { |
|
474 return m_version; |
|
475 } |
|
476 |
|
477 void |
|
478 PacketBB::SetSequenceNumber (uint16_t number) |
|
479 { |
|
480 m_seqnum = number; |
|
481 m_hasseqnum = true; |
|
482 } |
|
483 |
|
484 uint16_t |
|
485 PacketBB::GetSequenceNumber (void) const throw (PacketBBError) |
|
486 { |
|
487 if (!HasSequenceNumber ()) |
|
488 { |
|
489 throw PacketBBError ("Packet has no sequence number."); |
|
490 } |
|
491 return m_seqnum; |
|
492 } |
|
493 |
|
494 bool |
|
495 PacketBB::HasSequenceNumber (void) const |
|
496 { |
|
497 return m_hasseqnum; |
|
498 } |
|
499 |
|
500 /* Manipulating Packet TLVs */ |
|
501 |
|
502 PacketBB::TlvIterator |
|
503 PacketBB::TlvBegin (void) |
|
504 { |
|
505 return m_tlvList.Begin (); |
|
506 } |
|
507 |
|
508 PacketBB::ConstTlvIterator |
|
509 PacketBB::TlvBegin (void) const |
|
510 { |
|
511 return m_tlvList.Begin (); |
|
512 } |
|
513 |
|
514 PacketBB::TlvIterator |
|
515 PacketBB::TlvEnd (void) |
|
516 { |
|
517 return m_tlvList.End (); |
|
518 } |
|
519 |
|
520 PacketBB::ConstTlvIterator |
|
521 PacketBB::TlvEnd (void) const |
|
522 { |
|
523 return m_tlvList.End (); |
|
524 } |
|
525 |
|
526 int |
|
527 PacketBB::TlvSize (void) const |
|
528 { |
|
529 return m_tlvList.Size (); |
|
530 } |
|
531 |
|
532 bool |
|
533 PacketBB::TlvEmpty (void) const |
|
534 { |
|
535 return m_tlvList.Empty (); |
|
536 } |
|
537 |
|
538 Ptr<Tlv> |
|
539 PacketBB::TlvFront (void) |
|
540 { |
|
541 return m_tlvList.Front (); |
|
542 } |
|
543 |
|
544 const Ptr<Tlv> |
|
545 PacketBB::TlvFront (void) const |
|
546 { |
|
547 return m_tlvList.Front (); |
|
548 } |
|
549 |
|
550 Ptr<Tlv> |
|
551 PacketBB::TlvBack (void) |
|
552 { |
|
553 return m_tlvList.Back (); |
|
554 } |
|
555 |
|
556 const Ptr<Tlv> |
|
557 PacketBB::TlvBack (void) const |
|
558 { |
|
559 return m_tlvList.Back (); |
|
560 } |
|
561 |
|
562 void |
|
563 PacketBB::TlvPushFront (Ptr<Tlv> tlv) |
|
564 { |
|
565 m_tlvList.PushFront (tlv); |
|
566 } |
|
567 |
|
568 void |
|
569 PacketBB::TlvPopFront (void) |
|
570 { |
|
571 m_tlvList.PopFront (); |
|
572 } |
|
573 |
|
574 void |
|
575 PacketBB::TlvPushBack (Ptr<Tlv> tlv) |
|
576 { |
|
577 m_tlvList.PushBack (tlv); |
|
578 } |
|
579 |
|
580 void |
|
581 PacketBB::TlvPopBack (void) |
|
582 { |
|
583 m_tlvList.PopBack (); |
|
584 } |
|
585 |
|
586 PacketBB::TlvIterator |
|
587 PacketBB::Erase (PacketBB::TlvIterator position) |
|
588 { |
|
589 return m_tlvList.Erase (position); |
|
590 } |
|
591 |
|
592 PacketBB::TlvIterator |
|
593 PacketBB::Erase (PacketBB::TlvIterator first, PacketBB::TlvIterator last) |
|
594 { |
|
595 return m_tlvList.Erase (first, last); |
|
596 } |
|
597 |
|
598 void |
|
599 PacketBB::TlvClear (void) |
|
600 { |
|
601 m_tlvList.Clear (); |
|
602 } |
|
603 |
|
604 /* Manipulating Packet Messages */ |
|
605 |
|
606 PacketBB::MessageIterator |
|
607 PacketBB::MessageBegin (void) |
|
608 { |
|
609 return m_messageList.begin (); |
|
610 } |
|
611 |
|
612 PacketBB::ConstMessageIterator |
|
613 PacketBB::MessageBegin (void) const |
|
614 { |
|
615 return m_messageList.begin (); |
|
616 } |
|
617 |
|
618 PacketBB::MessageIterator |
|
619 PacketBB::MessageEnd (void) |
|
620 { |
|
621 return m_messageList.end (); |
|
622 } |
|
623 |
|
624 PacketBB::ConstMessageIterator |
|
625 PacketBB::MessageEnd (void) const |
|
626 { |
|
627 return m_messageList.end (); |
|
628 } |
|
629 |
|
630 int |
|
631 PacketBB::MessageSize (void) const |
|
632 { |
|
633 return m_messageList.size (); |
|
634 } |
|
635 |
|
636 bool |
|
637 PacketBB::MessageEmpty (void) const |
|
638 { |
|
639 return m_messageList.empty (); |
|
640 } |
|
641 |
|
642 Ptr<Message> |
|
643 PacketBB::MessageFront (void) |
|
644 { |
|
645 return m_messageList.front (); |
|
646 } |
|
647 |
|
648 const Ptr<Message> |
|
649 PacketBB::MessageFront (void) const |
|
650 { |
|
651 return m_messageList.front (); |
|
652 } |
|
653 |
|
654 Ptr<Message> |
|
655 PacketBB::MessageBack (void) |
|
656 { |
|
657 return m_messageList.back (); |
|
658 } |
|
659 |
|
660 const Ptr<Message> |
|
661 PacketBB::MessageBack (void) const |
|
662 { |
|
663 return m_messageList.back (); |
|
664 } |
|
665 |
|
666 void |
|
667 PacketBB::MessagePushFront (Ptr<Message> tlv) |
|
668 { |
|
669 m_messageList.push_front (tlv); |
|
670 } |
|
671 |
|
672 void |
|
673 PacketBB::MessagePopFront (void) |
|
674 { |
|
675 m_messageList.pop_front (); |
|
676 } |
|
677 |
|
678 void |
|
679 PacketBB::MessagePushBack (Ptr<Message> tlv) |
|
680 { |
|
681 m_messageList.push_back (tlv); |
|
682 } |
|
683 |
|
684 void |
|
685 PacketBB::MessagePopBack (void) |
|
686 { |
|
687 m_messageList.pop_back (); |
|
688 } |
|
689 |
|
690 PacketBB::MessageIterator |
|
691 PacketBB::Erase (PacketBB::MessageIterator position) |
|
692 { |
|
693 return m_messageList.erase (position); |
|
694 } |
|
695 |
|
696 PacketBB::MessageIterator |
|
697 PacketBB::Erase (PacketBB::MessageIterator first, |
|
698 PacketBB::MessageIterator last) |
|
699 { |
|
700 return m_messageList.erase (first, last); |
|
701 } |
|
702 |
|
703 void |
|
704 PacketBB::MessageClear (void) |
|
705 { |
|
706 m_messageList.clear (); |
|
707 } |
|
708 |
|
709 void |
|
710 PacketBB::Ref (void) const |
|
711 { |
|
712 m_refCount++; |
|
713 } |
|
714 |
|
715 void |
|
716 PacketBB::Unref (void) const |
|
717 { |
|
718 m_refCount--; |
|
719 if (m_refCount == 0) |
|
720 { |
|
721 delete this; |
|
722 } |
|
723 } |
|
724 |
|
725 TypeId |
|
726 PacketBB::GetTypeId (void) |
|
727 { |
|
728 static TypeId tid = TypeId ("PacketBB") |
|
729 .SetParent<Header> () |
|
730 .AddConstructor<PacketBB> () |
|
731 ; |
|
732 return tid; |
|
733 } |
|
734 |
|
735 TypeId |
|
736 PacketBB::GetInstanceTypeId (void) const |
|
737 { |
|
738 return GetTypeId (); |
|
739 } |
|
740 |
|
741 uint32_t |
|
742 PacketBB::GetSerializedSize (void) const |
|
743 { |
|
744 /* Version number + flags */ |
|
745 uint32_t size = 1; |
|
746 |
|
747 if (HasSequenceNumber()) |
|
748 { |
|
749 size += 2; |
|
750 } |
|
751 |
|
752 if (!TlvEmpty ()) |
|
753 size += m_tlvList.GetSerializedSize (); |
|
754 |
|
755 for (ConstMessageIterator iter = MessageBegin (); iter != MessageEnd (); |
|
756 iter++) |
|
757 { |
|
758 size += (*iter)->GetSerializedSize (); |
|
759 } |
|
760 |
|
761 return size; |
|
762 } |
|
763 |
|
764 void |
|
765 PacketBB::Serialize (Buffer::Iterator start) const |
|
766 { |
|
767 /* We remember the start, so we can write the flags after we check for a |
|
768 * sequence number and TLV. */ |
|
769 Buffer::Iterator bufref = start; |
|
770 start.Next (); |
|
771 |
|
772 uint8_t flags = VERSION; |
|
773 /* Make room for 4 bit flags */ |
|
774 flags <<= 4; |
|
775 |
|
776 if (HasSequenceNumber ()) |
|
777 { |
|
778 flags |= PHAS_SEQ_NUM; |
|
779 start.WriteHtonU16 (GetSequenceNumber ()); |
|
780 } |
|
781 |
|
782 if (!TlvEmpty ()) |
|
783 { |
|
784 flags |= PHAS_TLV; |
|
785 m_tlvList.Serialize (start); |
|
786 } |
|
787 |
|
788 bufref.WriteU8(flags); |
|
789 |
|
790 for (ConstMessageIterator iter = MessageBegin (); |
|
791 iter != MessageEnd (); |
|
792 iter++) |
|
793 { |
|
794 (*iter)->Serialize (start); |
|
795 } |
|
796 } |
|
797 |
|
798 uint32_t |
|
799 PacketBB::Deserialize (Buffer::Iterator start) |
|
800 { |
|
801 Buffer::Iterator begin = start; |
|
802 |
|
803 uint8_t flags = start.ReadU8 (); |
|
804 |
|
805 if (flags & PHAS_SEQ_NUM) |
|
806 SetSequenceNumber (start.ReadNtohU16 ()); |
|
807 |
|
808 if (flags & PHAS_TLV) |
|
809 m_tlvList.Deserialize (start); |
|
810 |
|
811 while (!start.IsEnd()) |
|
812 { |
|
813 Ptr<Message> newmsg = Message::DeserializeMessage (start); |
|
814 //Message * newmsg = Message::DeserializeMessage (start); |
|
815 //Ptr<Message> * newptr = new Ptr<Message> (newmsg); |
|
816 MessagePushBack (newmsg); |
|
817 } |
|
818 |
|
819 flags >>= 4; |
|
820 m_version = flags; |
|
821 |
|
822 return start.GetDistanceFrom (begin); |
|
823 } |
|
824 |
|
825 void |
|
826 PacketBB::Print (std::ostream &os) const |
|
827 { |
|
828 os << "PacketBB {" << std::endl; |
|
829 |
|
830 if (HasSequenceNumber ()) |
|
831 os << "\tsequence number = " << GetSequenceNumber (); |
|
832 |
|
833 os << std::endl; |
|
834 |
|
835 m_tlvList.Print (os, 1); |
|
836 |
|
837 for (ConstMessageIterator iter = MessageBegin (); |
|
838 iter != MessageEnd (); |
|
839 iter++) |
|
840 { |
|
841 (*iter)->Print (os, 1); |
|
842 } |
|
843 |
|
844 os << "}" << std::endl; |
|
845 } |
|
846 |
|
847 bool |
|
848 PacketBB::operator== (const PacketBB &other) const |
|
849 { |
|
850 if (GetVersion () != other.GetVersion ()) |
|
851 return false; |
|
852 |
|
853 if (HasSequenceNumber () != other.HasSequenceNumber ()) |
|
854 return false; |
|
855 |
|
856 if (HasSequenceNumber ()) |
|
857 { |
|
858 if (GetSequenceNumber () != other.GetSequenceNumber ()) |
|
859 return false; |
|
860 } |
|
861 |
|
862 if (m_tlvList != other.m_tlvList) |
|
863 return false; |
|
864 |
|
865 if (MessageSize () != other.MessageSize ()) |
|
866 return false; |
|
867 |
|
868 ConstMessageIterator tmi, omi; |
|
869 for (tmi = MessageBegin (), omi = other.MessageBegin (); |
|
870 tmi != MessageEnd () && omi != other.MessageEnd (); |
|
871 tmi++, omi++) |
|
872 { |
|
873 if (**tmi != **omi) |
|
874 return false; |
|
875 } |
|
876 return true; |
|
877 } |
|
878 |
|
879 bool |
|
880 PacketBB::operator!= (const PacketBB &other) const |
|
881 { |
|
882 return !(*this == other); |
|
883 } |
|
884 |
|
885 /* End PacketBB class */ |
|
886 |
|
887 Message::Message (void) |
|
888 { |
|
889 m_refCount = 1; |
|
890 /* Default to IPv4 */ |
|
891 m_addrSize = IPV4; |
|
892 m_hasOriginatorAddress = false; |
|
893 m_hasHopLimit = false; |
|
894 m_hasHopCount = false; |
|
895 m_hasSequenceNumber = false; |
|
896 } |
|
897 |
|
898 void |
|
899 Message::SetType (uint8_t type) |
|
900 { |
|
901 m_type = type; |
|
902 } |
|
903 |
|
904 uint8_t |
|
905 Message::GetType (void) const |
|
906 { |
|
907 return m_type; |
|
908 } |
|
909 |
|
910 AddressLength |
|
911 Message::GetAddressLength (void) const |
|
912 { |
|
913 return m_addrSize; |
|
914 } |
|
915 |
|
916 void |
|
917 Message::SetOriginatorAddress (Address address) |
|
918 { |
|
919 m_originatorAddress = address; |
|
920 m_hasOriginatorAddress = true; |
|
921 } |
|
922 |
|
923 Address |
|
924 Message::GetOriginatorAddress (void) const throw (PacketBBError) |
|
925 { |
|
926 if (!HasOriginatorAddress()) |
|
927 { |
|
928 throw PacketBBError ("Message has no originator address."); |
|
929 } |
|
930 return m_originatorAddress; |
|
931 } |
|
932 |
|
933 bool |
|
934 Message::HasOriginatorAddress (void) const |
|
935 { |
|
936 return m_hasOriginatorAddress; |
|
937 } |
|
938 |
|
939 void |
|
940 Message::SetHopLimit (uint8_t hopLimit) |
|
941 { |
|
942 m_hopLimit = hopLimit; |
|
943 m_hasHopLimit = true; |
|
944 } |
|
945 |
|
946 uint8_t |
|
947 Message::GetHopLimit (void) const throw (PacketBBError) |
|
948 { |
|
949 if (!HasHopLimit()) |
|
950 { |
|
951 throw PacketBBError ("Message has no hop limit."); |
|
952 } |
|
953 return m_hopLimit; |
|
954 } |
|
955 |
|
956 bool |
|
957 Message::HasHopLimit (void) const |
|
958 { |
|
959 return m_hasHopLimit; |
|
960 } |
|
961 |
|
962 void |
|
963 Message::SetHopCount (uint8_t hopCount) |
|
964 { |
|
965 m_hopCount = hopCount; |
|
966 m_hasHopCount = true; |
|
967 } |
|
968 |
|
969 uint8_t |
|
970 Message::GetHopCount (void) const throw (PacketBBError) |
|
971 { |
|
972 if (!HasHopCount()) |
|
973 { |
|
974 throw PacketBBError ("Message has no hop count."); |
|
975 } |
|
976 return m_hopCount; |
|
977 } |
|
978 |
|
979 bool |
|
980 Message::HasHopCount (void) const |
|
981 { |
|
982 return m_hasHopCount; |
|
983 } |
|
984 |
|
985 void |
|
986 Message::SetSequenceNumber (uint16_t sequenceNumber) |
|
987 { |
|
988 m_sequenceNumber = sequenceNumber; |
|
989 m_hasSequenceNumber = true; |
|
990 } |
|
991 |
|
992 uint16_t |
|
993 Message::GetSequenceNumber (void) const throw (PacketBBError) |
|
994 { |
|
995 if (!HasSequenceNumber()) |
|
996 { |
|
997 throw PacketBBError ("Message has no sequence number."); |
|
998 } |
|
999 return m_sequenceNumber; |
|
1000 } |
|
1001 |
|
1002 bool |
|
1003 Message::HasSequenceNumber (void) const |
|
1004 { |
|
1005 return m_hasSequenceNumber; |
|
1006 } |
|
1007 |
|
1008 /* Manipulating Message TLVs */ |
|
1009 |
|
1010 Message::TlvIterator |
|
1011 Message::TlvBegin (void) |
|
1012 { |
|
1013 return m_tlvList.Begin(); |
|
1014 } |
|
1015 |
|
1016 Message::ConstTlvIterator |
|
1017 Message::TlvBegin (void) const |
|
1018 { |
|
1019 return m_tlvList.Begin(); |
|
1020 } |
|
1021 |
|
1022 Message::TlvIterator |
|
1023 Message::TlvEnd (void) |
|
1024 { |
|
1025 return m_tlvList.End(); |
|
1026 } |
|
1027 |
|
1028 Message::ConstTlvIterator |
|
1029 Message::TlvEnd (void) const |
|
1030 { |
|
1031 return m_tlvList.End(); |
|
1032 } |
|
1033 |
|
1034 int |
|
1035 Message::TlvSize (void) const |
|
1036 { |
|
1037 return m_tlvList.Size(); |
|
1038 } |
|
1039 |
|
1040 bool |
|
1041 Message::TlvEmpty (void) const |
|
1042 { |
|
1043 return m_tlvList.Empty(); |
|
1044 } |
|
1045 |
|
1046 Ptr<Tlv> |
|
1047 Message::TlvFront (void) |
|
1048 { |
|
1049 return m_tlvList.Front(); |
|
1050 } |
|
1051 |
|
1052 const Ptr<Tlv> |
|
1053 Message::TlvFront (void) const |
|
1054 { |
|
1055 return m_tlvList.Front(); |
|
1056 } |
|
1057 |
|
1058 Ptr<Tlv> |
|
1059 Message::TlvBack (void) |
|
1060 { |
|
1061 return m_tlvList.Back(); |
|
1062 } |
|
1063 |
|
1064 const Ptr<Tlv> |
|
1065 Message::TlvBack (void) const |
|
1066 { |
|
1067 return m_tlvList.Back(); |
|
1068 } |
|
1069 |
|
1070 void |
|
1071 Message::TlvPushFront (Ptr<Tlv> tlv) |
|
1072 { |
|
1073 m_tlvList.PushFront(tlv); |
|
1074 } |
|
1075 |
|
1076 void |
|
1077 Message::TlvPopFront (void) |
|
1078 { |
|
1079 m_tlvList.PopFront(); |
|
1080 } |
|
1081 |
|
1082 void |
|
1083 Message::TlvPushBack (Ptr<Tlv> tlv) |
|
1084 { |
|
1085 m_tlvList.PushBack(tlv); |
|
1086 } |
|
1087 |
|
1088 void |
|
1089 Message::TlvPopBack (void) |
|
1090 { |
|
1091 m_tlvList.PopBack(); |
|
1092 } |
|
1093 |
|
1094 Message::TlvIterator |
|
1095 Message::TlvErase (Message::TlvIterator position) |
|
1096 { |
|
1097 return m_tlvList.Erase(position); |
|
1098 } |
|
1099 |
|
1100 Message::TlvIterator |
|
1101 Message::TlvErase (Message::TlvIterator first, Message::TlvIterator last) |
|
1102 { |
|
1103 return m_tlvList.Erase(first, last); |
|
1104 } |
|
1105 |
|
1106 void |
|
1107 Message::TlvClear (void) |
|
1108 { |
|
1109 return m_tlvList.Clear(); |
|
1110 } |
|
1111 |
|
1112 /* Manipulating Address Block and Address TLV pairs */ |
|
1113 |
|
1114 Message::AddressBlockIterator |
|
1115 Message::AddressBlockBegin (void) |
|
1116 { |
|
1117 return m_addressBlockList.begin(); |
|
1118 } |
|
1119 |
|
1120 Message::ConstAddressBlockIterator |
|
1121 Message::AddressBlockBegin (void) const |
|
1122 { |
|
1123 return m_addressBlockList.begin(); |
|
1124 } |
|
1125 |
|
1126 Message::AddressBlockIterator |
|
1127 Message::AddressBlockEnd (void) |
|
1128 { |
|
1129 return m_addressBlockList.end(); |
|
1130 } |
|
1131 |
|
1132 Message::ConstAddressBlockIterator |
|
1133 Message::AddressBlockEnd (void) const |
|
1134 { |
|
1135 return m_addressBlockList.end(); |
|
1136 } |
|
1137 |
|
1138 int |
|
1139 Message::AddressBlockSize (void) const |
|
1140 { |
|
1141 return m_addressBlockList.size(); |
|
1142 } |
|
1143 |
|
1144 bool |
|
1145 Message::AddressBlockEmpty (void) const |
|
1146 { |
|
1147 return m_addressBlockList.empty(); |
|
1148 } |
|
1149 |
|
1150 Ptr<AddressBlock> |
|
1151 Message::AddressBlockFront (void) |
|
1152 { |
|
1153 return m_addressBlockList.front(); |
|
1154 } |
|
1155 |
|
1156 const Ptr<AddressBlock> |
|
1157 Message::AddressBlockFront (void) const |
|
1158 { |
|
1159 return m_addressBlockList.front(); |
|
1160 } |
|
1161 |
|
1162 Ptr<AddressBlock> |
|
1163 Message::AddressBlockBack (void) |
|
1164 { |
|
1165 return m_addressBlockList.back(); |
|
1166 } |
|
1167 |
|
1168 const Ptr<AddressBlock> |
|
1169 Message::AddressBlockBack (void) const |
|
1170 { |
|
1171 return m_addressBlockList.back(); |
|
1172 } |
|
1173 |
|
1174 void |
|
1175 Message::AddressBlockPushFront (Ptr<AddressBlock> tlv) |
|
1176 { |
|
1177 m_addressBlockList.push_front(tlv); |
|
1178 } |
|
1179 |
|
1180 void |
|
1181 Message::AddressBlockPopFront (void) |
|
1182 { |
|
1183 m_addressBlockList.pop_front(); |
|
1184 } |
|
1185 |
|
1186 void |
|
1187 Message::AddressBlockPushBack (Ptr<AddressBlock> tlv) |
|
1188 { |
|
1189 m_addressBlockList.push_back(tlv); |
|
1190 } |
|
1191 |
|
1192 void |
|
1193 Message::AddressBlockPopBack (void) |
|
1194 { |
|
1195 m_addressBlockList.pop_back(); |
|
1196 } |
|
1197 |
|
1198 Message::AddressBlockIterator |
|
1199 Message::AddressBlockErase (Message::AddressBlockIterator position) |
|
1200 { |
|
1201 return m_addressBlockList.erase(position); |
|
1202 } |
|
1203 |
|
1204 Message::AddressBlockIterator |
|
1205 Message::AddressBlockErase (Message::AddressBlockIterator first, |
|
1206 Message::AddressBlockIterator last) |
|
1207 { |
|
1208 return m_addressBlockList.erase(first, last); |
|
1209 } |
|
1210 |
|
1211 void |
|
1212 Message::AddressBlockClear (void) |
|
1213 { |
|
1214 return m_addressBlockList.clear(); |
|
1215 } |
|
1216 |
|
1217 void |
|
1218 Message::Ref (void) const |
|
1219 { |
|
1220 m_refCount++; |
|
1221 } |
|
1222 |
|
1223 void |
|
1224 Message::Unref (void) const |
|
1225 { |
|
1226 m_refCount--; |
|
1227 if (m_refCount == 0) |
|
1228 { |
|
1229 delete this; |
|
1230 } |
|
1231 } |
|
1232 |
|
1233 uint32_t |
|
1234 Message::GetSerializedSize (void) const |
|
1235 { |
|
1236 /* msg-type + (msg-flags + msg-addr-length) + 2msg-size */ |
|
1237 uint32_t size = 4; |
|
1238 |
|
1239 if (HasOriginatorAddress()) |
|
1240 size += GetAddressLength() + 1; |
|
1241 |
|
1242 if (HasHopLimit()) |
|
1243 size++; |
|
1244 |
|
1245 if (HasHopCount()) |
|
1246 size++; |
|
1247 |
|
1248 if (HasSequenceNumber()) |
|
1249 size += 2; |
|
1250 |
|
1251 size += m_tlvList.GetSerializedSize (); |
|
1252 |
|
1253 for (ConstAddressBlockIterator iter = AddressBlockBegin (); |
|
1254 iter != AddressBlockEnd (); |
|
1255 iter++) |
|
1256 { |
|
1257 size += (*iter)->GetSerializedSize (); |
|
1258 } |
|
1259 |
|
1260 return size; |
|
1261 } |
|
1262 |
|
1263 void |
|
1264 Message::Serialize (Buffer::Iterator &start) const |
|
1265 { |
|
1266 Buffer::Iterator front = start; |
|
1267 |
|
1268 start.WriteU8 (GetType()); |
|
1269 |
|
1270 /* Save a reference to the spot where we will later write the flags */ |
|
1271 Buffer::Iterator bufref = start; |
|
1272 start.Next (1); |
|
1273 |
|
1274 uint8_t flags = 0; |
|
1275 |
|
1276 flags = GetAddressLength (); |
|
1277 |
|
1278 Buffer::Iterator sizeref = start; |
|
1279 start.Next (2); |
|
1280 |
|
1281 if (HasOriginatorAddress ()) |
|
1282 { |
|
1283 flags |= MHAS_ORIG; |
|
1284 SerializeOriginatorAddress (start); |
|
1285 } |
|
1286 |
|
1287 if (HasHopLimit ()) |
|
1288 { |
|
1289 flags |= MHAS_HOP_LIMIT; |
|
1290 start.WriteU8 (GetHopLimit ()); |
|
1291 } |
|
1292 |
|
1293 if (HasHopCount ()) |
|
1294 { |
|
1295 flags |= MHAS_HOP_COUNT; |
|
1296 start.WriteU8 (GetHopCount ()); |
|
1297 } |
|
1298 |
|
1299 if (HasSequenceNumber ()) |
|
1300 { |
|
1301 flags |= MHAS_SEQ_NUM; |
|
1302 start.WriteHtonU16 (GetSequenceNumber ()); |
|
1303 } |
|
1304 |
|
1305 bufref.WriteU8(flags); |
|
1306 |
|
1307 m_tlvList.Serialize (start); |
|
1308 |
|
1309 for (ConstAddressBlockIterator iter = AddressBlockBegin (); |
|
1310 iter != AddressBlockEnd (); |
|
1311 iter++) |
|
1312 { |
|
1313 (*iter)->Serialize (start); |
|
1314 } |
|
1315 |
|
1316 sizeref.WriteHtonU16 (front.GetDistanceFrom (start)); |
|
1317 } |
|
1318 |
|
1319 Ptr<Message> |
|
1320 Message::DeserializeMessage (Buffer::Iterator &start) throw (PacketBBError) |
|
1321 { |
|
1322 /* We need to read the msg-addr-len field to determine what kind of object to |
|
1323 * construct. */ |
|
1324 start.Next (); |
|
1325 uint8_t addrlen = start.ReadU8 (); |
|
1326 start.Prev (2); /* Go back to the start */ |
|
1327 |
|
1328 /* The first four bytes of the flag is the address length. Set the last four |
|
1329 * bytes to 0 to read it. */ |
|
1330 addrlen = (addrlen & 0xf); |
|
1331 |
|
1332 Ptr<Message> newmsg; |
|
1333 |
|
1334 switch (addrlen) |
|
1335 { |
|
1336 case 0: |
|
1337 case IPV4: |
|
1338 newmsg = Create<MessageIpv4> (); |
|
1339 break; |
|
1340 case IPV6: |
|
1341 newmsg = Create<MessageIpv6> (); |
|
1342 break; |
|
1343 default: |
|
1344 throw PacketBBError ("Message deserialization has invalid address size."); |
|
1345 break; |
|
1346 } |
|
1347 newmsg->Deserialize (start); |
|
1348 return newmsg; |
|
1349 } |
|
1350 |
|
1351 void |
|
1352 Message::Deserialize (Buffer::Iterator &start) |
|
1353 { |
|
1354 Buffer::Iterator front = start; |
|
1355 SetType (start.ReadU8 ()); |
|
1356 uint8_t flags = start.ReadU8 (); |
|
1357 |
|
1358 uint16_t size = start.ReadNtohU16 (); |
|
1359 |
|
1360 if (flags & MHAS_ORIG) |
|
1361 SetOriginatorAddress (DeserializeOriginatorAddress (start)); |
|
1362 |
|
1363 if (flags & MHAS_HOP_LIMIT) |
|
1364 SetHopLimit (start.ReadU8 ()); |
|
1365 |
|
1366 if (flags & MHAS_HOP_COUNT) |
|
1367 SetHopCount (start.ReadU8 ()); |
|
1368 |
|
1369 if (flags & MHAS_SEQ_NUM) |
|
1370 SetSequenceNumber (start.ReadNtohU16 ()); |
|
1371 |
|
1372 m_tlvList.Deserialize (start); |
|
1373 |
|
1374 if (size > 0) |
|
1375 { |
|
1376 while (start.GetDistanceFrom(front) < size) |
|
1377 { |
|
1378 Ptr<AddressBlock> newab = AddressBlockDeserialize (start); |
|
1379 //AddressBlock * newab = AddressBlockDeserialize (start); |
|
1380 //Ptr<AddressBlock> * newptr = new Ptr<AddressBlock> (newab); |
|
1381 AddressBlockPushBack (newab); |
|
1382 } |
|
1383 } |
|
1384 } |
|
1385 |
|
1386 void |
|
1387 Message::Print (std::ostream &os) const |
|
1388 { |
|
1389 Print (os, 0); |
|
1390 } |
|
1391 |
|
1392 void |
|
1393 Message::Print (std::ostream &os, int level) const |
|
1394 { |
|
1395 std::string prefix = ""; |
|
1396 for (int i = 0; i < level; i++) |
|
1397 prefix.append ("\t"); |
|
1398 |
|
1399 os << prefix << "Message {" << std::endl; |
|
1400 |
|
1401 os << prefix << "\tmessage type = " << (int)GetType () << std::endl; |
|
1402 os << prefix << "\taddress size = " << GetAddressLength () << std::endl; |
|
1403 |
|
1404 if (HasOriginatorAddress ()) |
|
1405 { |
|
1406 os << prefix << "\toriginator address = "; |
|
1407 PrintOriginatorAddress (os); |
|
1408 os << std::endl; |
|
1409 } |
|
1410 |
|
1411 if (HasHopLimit ()) |
|
1412 os << prefix << "\thop limit = " << (int)GetHopLimit () << std::endl; |
|
1413 |
|
1414 if (HasHopCount ()) |
|
1415 os << prefix << "\thop count = " << (int)GetHopCount () << std::endl; |
|
1416 |
|
1417 if (HasSequenceNumber ()) |
|
1418 os << prefix << "\tseqnum = " << GetSequenceNumber () << std::endl; |
|
1419 |
|
1420 m_tlvList.Print (os, level+1); |
|
1421 |
|
1422 for (ConstAddressBlockIterator iter = AddressBlockBegin (); |
|
1423 iter != AddressBlockEnd (); |
|
1424 iter++) |
|
1425 { |
|
1426 (*iter)->Print (os, level+1); |
|
1427 } |
|
1428 os << prefix << "}" << std::endl; |
|
1429 } |
|
1430 |
|
1431 bool |
|
1432 Message::operator== (const Message &other) const |
|
1433 { |
|
1434 if (GetAddressLength () != other.GetAddressLength ()) |
|
1435 return false; |
|
1436 |
|
1437 if (GetType () != other.GetType ()) |
|
1438 return false; |
|
1439 |
|
1440 if (HasOriginatorAddress () != other.HasOriginatorAddress ()) |
|
1441 return false; |
|
1442 |
|
1443 if (HasOriginatorAddress ()) |
|
1444 { |
|
1445 if (GetOriginatorAddress () != other.GetOriginatorAddress ()) |
|
1446 return false; |
|
1447 } |
|
1448 |
|
1449 if (HasHopLimit () != other.HasHopLimit ()) |
|
1450 return false; |
|
1451 |
|
1452 if (HasHopLimit ()) |
|
1453 { |
|
1454 if (GetHopLimit () != other.GetHopLimit ()) |
|
1455 return false; |
|
1456 } |
|
1457 |
|
1458 if (HasHopCount () != other.HasHopCount ()) |
|
1459 return false; |
|
1460 |
|
1461 if (HasHopCount ()) |
|
1462 { |
|
1463 if (GetHopCount () != other.GetHopCount ()) |
|
1464 return false; |
|
1465 } |
|
1466 |
|
1467 if (HasSequenceNumber () != other.HasSequenceNumber ()) |
|
1468 return false; |
|
1469 |
|
1470 if (HasSequenceNumber ()) |
|
1471 { |
|
1472 if (GetSequenceNumber () != other.GetSequenceNumber ()) |
|
1473 return false; |
|
1474 } |
|
1475 |
|
1476 if (m_tlvList != other.m_tlvList) |
|
1477 return false; |
|
1478 |
|
1479 if (AddressBlockSize () != other.AddressBlockSize ()) |
|
1480 return false; |
|
1481 |
|
1482 ConstAddressBlockIterator tai, oai; |
|
1483 for (tai = AddressBlockBegin (), oai = other.AddressBlockBegin (); |
|
1484 tai != AddressBlockEnd () && oai != other.AddressBlockEnd (); |
|
1485 tai++, oai++) |
|
1486 { |
|
1487 if (**tai != **oai) |
|
1488 return false; |
|
1489 } |
|
1490 return true; |
|
1491 } |
|
1492 |
|
1493 bool |
|
1494 Message::operator!= (const Message &other) const |
|
1495 { |
|
1496 return !(*this == other); |
|
1497 } |
|
1498 |
|
1499 /* End Message Class */ |
|
1500 |
|
1501 AddressLength |
|
1502 MessageIpv4::GetAddressLength (void) const |
|
1503 { |
|
1504 return IPV4; |
|
1505 } |
|
1506 |
|
1507 void |
|
1508 MessageIpv4::SerializeOriginatorAddress (Buffer::Iterator &start) const |
|
1509 { |
|
1510 uint8_t buffer[GetAddressLength () + 1]; |
|
1511 Ipv4Address::ConvertFrom (GetOriginatorAddress ()).Serialize(buffer); |
|
1512 start.Write (buffer, GetAddressLength () + 1); |
|
1513 } |
|
1514 |
|
1515 Address |
|
1516 MessageIpv4::DeserializeOriginatorAddress (Buffer::Iterator &start) const |
|
1517 { |
|
1518 uint8_t buffer[GetAddressLength () + 1]; |
|
1519 start.Read(buffer, GetAddressLength () + 1); |
|
1520 return Ipv4Address::Deserialize (buffer); |
|
1521 } |
|
1522 |
|
1523 void |
|
1524 MessageIpv4::PrintOriginatorAddress (std::ostream &os) const |
|
1525 { |
|
1526 Ipv4Address::ConvertFrom (GetOriginatorAddress ()).Print (os); |
|
1527 } |
|
1528 |
|
1529 Ptr<AddressBlock> |
|
1530 MessageIpv4::AddressBlockDeserialize (Buffer::Iterator &start) const |
|
1531 { |
|
1532 Ptr<AddressBlock> newab = Create<AddressBlockIpv4> (); |
|
1533 newab->Deserialize (start); |
|
1534 return newab; |
|
1535 } |
|
1536 |
|
1537 /* End MessageIpv4 Class */ |
|
1538 |
|
1539 AddressLength |
|
1540 MessageIpv6::GetAddressLength (void) const |
|
1541 { |
|
1542 return IPV6; |
|
1543 } |
|
1544 |
|
1545 void |
|
1546 MessageIpv6::SerializeOriginatorAddress (Buffer::Iterator &start) const |
|
1547 { |
|
1548 uint8_t buffer[GetAddressLength () + 1]; |
|
1549 Ipv6Address::ConvertFrom (GetOriginatorAddress ()).Serialize(buffer); |
|
1550 start.Write (buffer, GetAddressLength () + 1); |
|
1551 } |
|
1552 |
|
1553 Address |
|
1554 MessageIpv6::DeserializeOriginatorAddress (Buffer::Iterator &start) const |
|
1555 { |
|
1556 uint8_t buffer[GetAddressLength () + 1]; |
|
1557 start.Read(buffer, GetAddressLength () + 1); |
|
1558 return Ipv6Address::Deserialize (buffer); |
|
1559 } |
|
1560 |
|
1561 void |
|
1562 MessageIpv6::PrintOriginatorAddress (std::ostream &os) const |
|
1563 { |
|
1564 Ipv6Address::ConvertFrom (GetOriginatorAddress ()).Print (os); |
|
1565 } |
|
1566 |
|
1567 Ptr<AddressBlock> |
|
1568 MessageIpv6::AddressBlockDeserialize (Buffer::Iterator &start) const |
|
1569 { |
|
1570 Ptr<AddressBlock> newab = Create<AddressBlockIpv6> (); |
|
1571 newab->Deserialize (start); |
|
1572 return newab; |
|
1573 } |
|
1574 |
|
1575 /* End MessageIpv6 Class */ |
|
1576 |
|
1577 AddressBlock::AddressBlock () |
|
1578 { |
|
1579 m_refCount = 1; |
|
1580 } |
|
1581 |
|
1582 /* Manipulating the address block */ |
|
1583 |
|
1584 AddressBlock::AddressIterator |
|
1585 AddressBlock::AddressBegin (void) |
|
1586 { |
|
1587 return m_addressList.begin(); |
|
1588 } |
|
1589 |
|
1590 AddressBlock::ConstAddressIterator |
|
1591 AddressBlock::AddressBegin (void) const |
|
1592 { |
|
1593 return m_addressList.begin(); |
|
1594 } |
|
1595 |
|
1596 AddressBlock::AddressIterator |
|
1597 AddressBlock::AddressEnd (void) |
|
1598 { |
|
1599 return m_addressList.end(); |
|
1600 } |
|
1601 |
|
1602 AddressBlock::ConstAddressIterator |
|
1603 AddressBlock::AddressEnd (void) const |
|
1604 { |
|
1605 return m_addressList.end(); |
|
1606 } |
|
1607 |
|
1608 int |
|
1609 AddressBlock::AddressSize (void) const |
|
1610 { |
|
1611 return m_addressList.size(); |
|
1612 } |
|
1613 |
|
1614 bool |
|
1615 AddressBlock::AddressEmpty (void) const |
|
1616 { |
|
1617 return m_addressList.empty(); |
|
1618 } |
|
1619 |
|
1620 Address |
|
1621 AddressBlock::AddressFront (void) const |
|
1622 { |
|
1623 return m_addressList.front(); |
|
1624 } |
|
1625 |
|
1626 Address |
|
1627 AddressBlock::AddressBack (void) const |
|
1628 { |
|
1629 return m_addressList.back(); |
|
1630 } |
|
1631 |
|
1632 void |
|
1633 AddressBlock::AddressPushFront (Address tlv) |
|
1634 { |
|
1635 m_addressList.push_front(tlv); |
|
1636 } |
|
1637 |
|
1638 void |
|
1639 AddressBlock::AddressPopFront (void) |
|
1640 { |
|
1641 m_addressList.pop_front(); |
|
1642 } |
|
1643 |
|
1644 void |
|
1645 AddressBlock::AddressPushBack (Address tlv) |
|
1646 { |
|
1647 m_addressList.push_back(tlv); |
|
1648 } |
|
1649 |
|
1650 void |
|
1651 AddressBlock::AddressPopBack (void) |
|
1652 { |
|
1653 m_addressList.pop_back(); |
|
1654 } |
|
1655 |
|
1656 AddressBlock::AddressIterator |
|
1657 AddressBlock::AddressErase (AddressBlock::AddressIterator position) |
|
1658 { |
|
1659 return m_addressList.erase(position); |
|
1660 } |
|
1661 |
|
1662 AddressBlock::AddressIterator |
|
1663 AddressBlock::AddressErase (AddressBlock::AddressIterator first, |
|
1664 AddressBlock::AddressIterator last) |
|
1665 { |
|
1666 return m_addressList.erase(first, last); |
|
1667 } |
|
1668 |
|
1669 void |
|
1670 AddressBlock::AddressClear (void) |
|
1671 { |
|
1672 return m_addressList.clear(); |
|
1673 } |
|
1674 |
|
1675 /* Manipulating the prefix list */ |
|
1676 |
|
1677 AddressBlock::PrefixIterator |
|
1678 AddressBlock::PrefixBegin (void) |
|
1679 { |
|
1680 return m_prefixList.begin (); |
|
1681 } |
|
1682 |
|
1683 AddressBlock::ConstPrefixIterator |
|
1684 AddressBlock::PrefixBegin (void) const |
|
1685 { |
|
1686 return m_prefixList.begin (); |
|
1687 } |
|
1688 |
|
1689 AddressBlock::PrefixIterator |
|
1690 AddressBlock::PrefixEnd (void) |
|
1691 { |
|
1692 return m_prefixList.end (); |
|
1693 } |
|
1694 |
|
1695 AddressBlock::ConstPrefixIterator |
|
1696 AddressBlock::PrefixEnd (void) const |
|
1697 { |
|
1698 return m_prefixList.end (); |
|
1699 } |
|
1700 |
|
1701 int |
|
1702 AddressBlock::PrefixSize (void) const |
|
1703 { |
|
1704 return m_prefixList.size (); |
|
1705 } |
|
1706 |
|
1707 bool |
|
1708 AddressBlock::PrefixEmpty (void) const |
|
1709 { |
|
1710 return m_prefixList.empty (); |
|
1711 } |
|
1712 |
|
1713 uint8_t |
|
1714 AddressBlock::PrefixFront (void) const |
|
1715 { |
|
1716 return m_prefixList.front (); |
|
1717 } |
|
1718 |
|
1719 uint8_t |
|
1720 AddressBlock::PrefixBack (void) const |
|
1721 { |
|
1722 return m_prefixList.back (); |
|
1723 } |
|
1724 |
|
1725 void |
|
1726 AddressBlock::PrefixPushFront (uint8_t prefix) |
|
1727 { |
|
1728 m_prefixList.push_front (prefix); |
|
1729 } |
|
1730 |
|
1731 void |
|
1732 AddressBlock::PrefixPopFront (void) |
|
1733 { |
|
1734 m_prefixList.pop_front (); |
|
1735 } |
|
1736 |
|
1737 void |
|
1738 AddressBlock::PrefixPushBack (uint8_t prefix) |
|
1739 { |
|
1740 m_prefixList.push_back (prefix); |
|
1741 } |
|
1742 |
|
1743 void |
|
1744 AddressBlock::PrefixPopBack (void) |
|
1745 { |
|
1746 m_prefixList.pop_back (); |
|
1747 } |
|
1748 |
|
1749 AddressBlock::PrefixIterator |
|
1750 AddressBlock::PrefixInsert (AddressBlock::PrefixIterator position, const uint8_t value) |
|
1751 { |
|
1752 return m_prefixList.insert (position, value); |
|
1753 } |
|
1754 |
|
1755 AddressBlock::PrefixIterator |
|
1756 AddressBlock::PrefixErase (AddressBlock::PrefixIterator position) |
|
1757 { |
|
1758 return m_prefixList.erase (position); |
|
1759 } |
|
1760 |
|
1761 AddressBlock::PrefixIterator |
|
1762 AddressBlock::PrefixErase (AddressBlock::PrefixIterator first, AddressBlock::PrefixIterator last) |
|
1763 { |
|
1764 return m_prefixList.erase (first, last); |
|
1765 } |
|
1766 |
|
1767 void |
|
1768 AddressBlock::PrefixClear (void) |
|
1769 { |
|
1770 m_prefixList.clear (); |
|
1771 } |
|
1772 |
|
1773 /* Manipulating the TLV block */ |
|
1774 |
|
1775 AddressBlock::TlvIterator |
|
1776 AddressBlock::TlvBegin (void) |
|
1777 { |
|
1778 return m_addressTlvList.Begin(); |
|
1779 } |
|
1780 |
|
1781 AddressBlock::ConstTlvIterator |
|
1782 AddressBlock::TlvBegin (void) const |
|
1783 { |
|
1784 return m_addressTlvList.Begin(); |
|
1785 } |
|
1786 |
|
1787 AddressBlock::TlvIterator |
|
1788 AddressBlock::TlvEnd (void) |
|
1789 { |
|
1790 return m_addressTlvList.End(); |
|
1791 } |
|
1792 |
|
1793 AddressBlock::ConstTlvIterator |
|
1794 AddressBlock::TlvEnd (void) const |
|
1795 { |
|
1796 return m_addressTlvList.End(); |
|
1797 } |
|
1798 |
|
1799 int |
|
1800 AddressBlock::TlvSize (void) const |
|
1801 { |
|
1802 return m_addressTlvList.Size(); |
|
1803 } |
|
1804 |
|
1805 bool |
|
1806 AddressBlock::TlvEmpty (void) const |
|
1807 { |
|
1808 return m_addressTlvList.Empty(); |
|
1809 } |
|
1810 |
|
1811 Ptr<AddressTlv> |
|
1812 AddressBlock::TlvFront (void) |
|
1813 { |
|
1814 return m_addressTlvList.Front(); |
|
1815 } |
|
1816 |
|
1817 const Ptr<AddressTlv> |
|
1818 AddressBlock::TlvFront (void) const |
|
1819 { |
|
1820 return m_addressTlvList.Front(); |
|
1821 } |
|
1822 |
|
1823 Ptr<AddressTlv> |
|
1824 AddressBlock::TlvBack (void) |
|
1825 { |
|
1826 return m_addressTlvList.Back(); |
|
1827 } |
|
1828 |
|
1829 const Ptr<AddressTlv> |
|
1830 AddressBlock::TlvBack (void) const |
|
1831 { |
|
1832 return m_addressTlvList.Back(); |
|
1833 } |
|
1834 |
|
1835 void |
|
1836 AddressBlock::TlvPushFront (Ptr<AddressTlv> tlv) |
|
1837 { |
|
1838 m_addressTlvList.PushFront(tlv); |
|
1839 } |
|
1840 |
|
1841 void |
|
1842 AddressBlock::TlvPopFront (void) |
|
1843 { |
|
1844 m_addressTlvList.PopFront(); |
|
1845 } |
|
1846 |
|
1847 void |
|
1848 AddressBlock::TlvPushBack (Ptr<AddressTlv> tlv) |
|
1849 { |
|
1850 m_addressTlvList.PushBack(tlv); |
|
1851 } |
|
1852 |
|
1853 void |
|
1854 AddressBlock::TlvPopBack (void) |
|
1855 { |
|
1856 m_addressTlvList.PopBack(); |
|
1857 } |
|
1858 |
|
1859 AddressBlock::TlvIterator |
|
1860 AddressBlock::TlvErase (AddressBlock::TlvIterator position) |
|
1861 { |
|
1862 return m_addressTlvList.Erase(position); |
|
1863 } |
|
1864 |
|
1865 AddressBlock::TlvIterator |
|
1866 AddressBlock::TlvErase (AddressBlock::TlvIterator first, |
|
1867 AddressBlock::TlvIterator last) |
|
1868 { |
|
1869 return m_addressTlvList.Erase(first, last); |
|
1870 } |
|
1871 |
|
1872 void |
|
1873 AddressBlock::TlvClear (void) |
|
1874 { |
|
1875 return m_addressTlvList.Clear(); |
|
1876 } |
|
1877 |
|
1878 void |
|
1879 AddressBlock::Ref (void) const |
|
1880 { |
|
1881 m_refCount++; |
|
1882 } |
|
1883 |
|
1884 void |
|
1885 AddressBlock::Unref (void) const |
|
1886 { |
|
1887 m_refCount--; |
|
1888 if (m_refCount == 0) |
|
1889 { |
|
1890 delete this; |
|
1891 } |
|
1892 } |
|
1893 |
|
1894 uint32_t |
|
1895 AddressBlock::GetSerializedSize (void) const |
|
1896 { |
|
1897 /* num-addr + flags */ |
|
1898 uint32_t size = 2; |
|
1899 |
|
1900 if (AddressSize () == 1) |
|
1901 { |
|
1902 size += GetAddressLength () + PrefixSize(); |
|
1903 } |
|
1904 else if (AddressSize () > 0) |
|
1905 { |
|
1906 uint8_t head[GetAddressLength ()]; |
|
1907 uint8_t headlen = 0; |
|
1908 uint8_t tail[GetAddressLength ()]; |
|
1909 uint8_t taillen = 0; |
|
1910 |
|
1911 GetHeadTail (head, headlen, tail, taillen); |
|
1912 |
|
1913 if (headlen > 0) |
|
1914 size += 1 + headlen; |
|
1915 |
|
1916 if (taillen > 0) |
|
1917 { |
|
1918 size++; |
|
1919 if (!HasZeroTail (tail, taillen)) |
|
1920 size += taillen; |
|
1921 } |
|
1922 |
|
1923 /* mid size */ |
|
1924 size += (GetAddressLength () - headlen - taillen) * AddressSize (); |
|
1925 |
|
1926 size += PrefixSize (); |
|
1927 } |
|
1928 |
|
1929 size += m_addressTlvList.GetSerializedSize (); |
|
1930 |
|
1931 return size; |
|
1932 } |
|
1933 |
|
1934 void |
|
1935 AddressBlock::Serialize (Buffer::Iterator &start) const |
|
1936 { |
|
1937 start.WriteU8 (AddressSize ()); |
|
1938 |
|
1939 if (AddressSize () == 1) { |
|
1940 start.WriteU8 (0); |
|
1941 |
|
1942 uint8_t buf[GetAddressLength ()]; |
|
1943 SerializeAddress (buf, AddressBegin ()); |
|
1944 start.Write (buf, GetAddressLength ()); |
|
1945 |
|
1946 if (PrefixSize () == 1) |
|
1947 start.WriteU8 (PrefixFront ()); |
|
1948 } |
|
1949 else if (AddressSize () > 0) |
|
1950 { |
|
1951 Buffer::Iterator bufref = start; |
|
1952 uint8_t flags = 0; |
|
1953 start.Next (); |
|
1954 |
|
1955 uint8_t head[GetAddressLength ()]; |
|
1956 uint8_t tail[GetAddressLength ()]; |
|
1957 uint8_t headlen = 0; |
|
1958 uint8_t taillen = 0; |
|
1959 |
|
1960 GetHeadTail (head, headlen, tail, taillen); |
|
1961 |
|
1962 if (headlen > 0) |
|
1963 { |
|
1964 flags |= AHAS_HEAD; |
|
1965 start.WriteU8 (headlen); |
|
1966 start.Write (head, headlen); |
|
1967 } |
|
1968 |
|
1969 if (taillen > 0) |
|
1970 { |
|
1971 start.WriteU8 (taillen); |
|
1972 |
|
1973 if (HasZeroTail (tail, taillen)) |
|
1974 { |
|
1975 flags |= AHAS_ZERO_TAIL; |
|
1976 } |
|
1977 else |
|
1978 { |
|
1979 flags |= AHAS_FULL_TAIL; |
|
1980 start.Write (tail, taillen); |
|
1981 } |
|
1982 } |
|
1983 |
|
1984 if (headlen + taillen < GetAddressLength ()) |
|
1985 { |
|
1986 uint8_t mid[GetAddressLength ()]; |
|
1987 for (AddressBlock::ConstAddressIterator iter = AddressBegin (); |
|
1988 iter != AddressEnd (); |
|
1989 iter++) |
|
1990 { |
|
1991 SerializeAddress (mid, iter); |
|
1992 start.Write (mid + headlen, GetAddressLength () - headlen - taillen); |
|
1993 } |
|
1994 } |
|
1995 |
|
1996 flags |= GetPrefixFlags (); |
|
1997 bufref.WriteU8 (flags); |
|
1998 |
|
1999 for (ConstPrefixIterator iter = PrefixBegin (); |
|
2000 iter != PrefixEnd (); |
|
2001 iter++) |
|
2002 { |
|
2003 start.WriteU8 (*iter); |
|
2004 } |
|
2005 } |
|
2006 |
|
2007 m_addressTlvList.Serialize (start); |
|
2008 } |
|
2009 |
|
2010 void |
|
2011 AddressBlock::Deserialize (Buffer::Iterator &start) |
|
2012 { |
|
2013 uint8_t numaddr = start.ReadU8 (); |
|
2014 uint8_t flags = start.ReadU8 (); |
|
2015 |
|
2016 if (numaddr > 0) |
|
2017 { |
|
2018 uint8_t headlen = 0; |
|
2019 uint8_t taillen = 0; |
|
2020 uint8_t addrtmp[GetAddressLength ()]; |
|
2021 memset(addrtmp, 0, GetAddressLength ()); |
|
2022 |
|
2023 if (flags & AHAS_HEAD) |
|
2024 { |
|
2025 headlen = start.ReadU8 (); |
|
2026 start.Read (addrtmp, headlen); |
|
2027 } |
|
2028 |
|
2029 if ((flags & AHAS_FULL_TAIL) ^ (flags & AHAS_ZERO_TAIL)) |
|
2030 { |
|
2031 taillen = start.ReadU8 (); |
|
2032 |
|
2033 if (flags & AHAS_FULL_TAIL) |
|
2034 { |
|
2035 start.Read (addrtmp + GetAddressLength () - taillen, taillen); |
|
2036 } |
|
2037 } |
|
2038 |
|
2039 for (int i = 0; i < numaddr; i++) |
|
2040 { |
|
2041 start.Read (addrtmp + headlen, GetAddressLength () - headlen - taillen); |
|
2042 AddressPushBack (DeserializeAddress (addrtmp)); |
|
2043 } |
|
2044 |
|
2045 if (flags & AHAS_SINGLE_PRE_LEN) |
|
2046 { |
|
2047 PrefixPushBack (start.ReadU8 ()); |
|
2048 } |
|
2049 else if (flags & AHAS_MULTI_PRE_LEN) |
|
2050 { |
|
2051 for (int i = 0; i < numaddr; i++) |
|
2052 { |
|
2053 PrefixPushBack (start.ReadU8 ()); |
|
2054 } |
|
2055 } |
|
2056 } |
|
2057 |
|
2058 m_addressTlvList.Deserialize (start); |
|
2059 } |
|
2060 |
|
2061 void |
|
2062 AddressBlock::Print (std::ostream &os) const |
|
2063 { |
|
2064 Print (os, 0); |
|
2065 } |
|
2066 |
|
2067 void |
|
2068 AddressBlock::Print (std::ostream &os, int level) const |
|
2069 { |
|
2070 std::string prefix = ""; |
|
2071 for (int i = 0; i < level; i++) |
|
2072 prefix.append ("\t"); |
|
2073 |
|
2074 os << prefix << "AddressBlock {" << std::endl; |
|
2075 os << prefix << "\taddresses = " << std::endl; |
|
2076 for (ConstAddressIterator iter = AddressBegin (); |
|
2077 iter != AddressEnd (); |
|
2078 iter++) |
|
2079 { |
|
2080 os << prefix << "\t\t"; |
|
2081 PrintAddress(os, iter); |
|
2082 os << std::endl; |
|
2083 } |
|
2084 |
|
2085 os << prefix << "\tprefixes = " << std::endl; |
|
2086 for (ConstPrefixIterator iter = PrefixBegin (); |
|
2087 iter != PrefixEnd (); |
|
2088 iter++) |
|
2089 { |
|
2090 os << prefix << "\t\t" << (int)(*iter) << std::endl; |
|
2091 } |
|
2092 |
|
2093 m_addressTlvList.Print (os, level+1); |
|
2094 } |
|
2095 |
|
2096 bool |
|
2097 AddressBlock::operator== (const AddressBlock &other) const |
|
2098 { |
|
2099 if (AddressSize () != other.AddressSize ()) |
|
2100 return false; |
|
2101 |
|
2102 ConstAddressIterator tai, oai; |
|
2103 for (tai = AddressBegin (), oai = other.AddressBegin (); |
|
2104 tai != AddressEnd () && oai != other.AddressEnd (); |
|
2105 tai++, oai++) |
|
2106 { |
|
2107 if (*tai != *oai) |
|
2108 return false; |
|
2109 } |
|
2110 |
|
2111 if (PrefixSize () != other.PrefixSize ()) |
|
2112 return false; |
|
2113 |
|
2114 ConstPrefixIterator tpi, opi; |
|
2115 for (tpi = PrefixBegin (), opi = other.PrefixBegin (); |
|
2116 tpi != PrefixEnd () && opi != other.PrefixEnd (); |
|
2117 tpi++, opi++) |
|
2118 { |
|
2119 if (*tpi != *opi) |
|
2120 return false; |
|
2121 } |
|
2122 |
|
2123 if (m_addressTlvList != other.m_addressTlvList) |
|
2124 return false; |
|
2125 |
|
2126 return true; |
|
2127 } |
|
2128 |
|
2129 bool |
|
2130 AddressBlock::operator!= (const AddressBlock &other) const |
|
2131 { |
|
2132 return !(*this == other); |
|
2133 } |
|
2134 |
|
2135 uint8_t |
|
2136 AddressBlock::GetPrefixFlags (void) const |
|
2137 { |
|
2138 switch (PrefixSize ()) |
|
2139 { |
|
2140 case 0: |
|
2141 return 0; |
|
2142 break; |
|
2143 case 1: |
|
2144 return AHAS_SINGLE_PRE_LEN; |
|
2145 break; |
|
2146 default: |
|
2147 return AHAS_MULTI_PRE_LEN; |
|
2148 break; |
|
2149 } |
|
2150 } |
|
2151 |
|
2152 void |
|
2153 AddressBlock::GetHeadTail (uint8_t *head, uint8_t &headlen, |
|
2154 uint8_t *tail, uint8_t &taillen) const |
|
2155 { |
|
2156 headlen = GetAddressLength (); |
|
2157 taillen = headlen; |
|
2158 |
|
2159 /* Temporary automatic buffers to store serialized addresses */ |
|
2160 uint8_t * buflast = new uint8_t[GetAddressLength ()]; |
|
2161 uint8_t * bufcur = new uint8_t[GetAddressLength ()]; |
|
2162 uint8_t * tmp; |
|
2163 |
|
2164 SerializeAddress (buflast, AddressBegin ()); |
|
2165 |
|
2166 /* Skip the first item */ |
|
2167 for (AddressBlock::ConstAddressIterator iter = AddressBegin ()++; |
|
2168 iter != AddressEnd (); |
|
2169 iter++) |
|
2170 { |
|
2171 SerializeAddress (bufcur, iter); |
|
2172 |
|
2173 int i; |
|
2174 for (i = 0; i < headlen; i++) |
|
2175 { |
|
2176 if (buflast[i] != bufcur[i]) |
|
2177 { |
|
2178 headlen = i; |
|
2179 break; |
|
2180 } |
|
2181 } |
|
2182 |
|
2183 /* If headlen == fulllen - 1, then tail is 0 */ |
|
2184 if (headlen <= GetAddressLength () - 1) |
|
2185 { |
|
2186 for (i = GetAddressLength () - 1; |
|
2187 GetAddressLength () - 1 - i <= taillen && i > headlen; |
|
2188 i--) |
|
2189 { |
|
2190 if (buflast[i] != bufcur[i]) |
|
2191 break; |
|
2192 } |
|
2193 taillen = GetAddressLength () - 1 - i; |
|
2194 } |
|
2195 else if (headlen == 0) |
|
2196 { |
|
2197 taillen = 0; |
|
2198 break; |
|
2199 } |
|
2200 |
|
2201 tmp = buflast; |
|
2202 buflast = bufcur; |
|
2203 bufcur = tmp; |
|
2204 } |
|
2205 |
|
2206 memcpy(head, bufcur, headlen); |
|
2207 memcpy(tail, bufcur + (GetAddressLength () - taillen), taillen); |
|
2208 |
|
2209 delete[] buflast; |
|
2210 delete[] bufcur; |
|
2211 } |
|
2212 |
|
2213 bool |
|
2214 AddressBlock::HasZeroTail (const uint8_t *tail, uint8_t taillen) const |
|
2215 { |
|
2216 int i; |
|
2217 for (i = 0; i < taillen; i++) |
|
2218 { |
|
2219 if (tail[i] != 0) |
|
2220 break; |
|
2221 } |
|
2222 return i == taillen; |
|
2223 } |
|
2224 |
|
2225 /* End AddressBlock Class */ |
|
2226 |
|
2227 uint8_t |
|
2228 AddressBlockIpv4::GetAddressLength (void) const |
|
2229 { |
|
2230 return 4; |
|
2231 } |
|
2232 |
|
2233 void |
|
2234 AddressBlockIpv4::SerializeAddress (uint8_t *buffer, ConstAddressIterator iter) const |
|
2235 { |
|
2236 Ipv4Address::ConvertFrom (*iter).Serialize (buffer); |
|
2237 } |
|
2238 |
|
2239 Address |
|
2240 AddressBlockIpv4::DeserializeAddress (uint8_t *buffer) const |
|
2241 { |
|
2242 return Ipv4Address::Deserialize (buffer); |
|
2243 } |
|
2244 |
|
2245 void |
|
2246 AddressBlockIpv4::PrintAddress (std::ostream &os, ConstAddressIterator iter) const |
|
2247 { |
|
2248 Ipv4Address::ConvertFrom (*iter).Print (os); |
|
2249 } |
|
2250 |
|
2251 /* End AddressBlockIpv4 Class */ |
|
2252 |
|
2253 uint8_t |
|
2254 AddressBlockIpv6::GetAddressLength (void) const |
|
2255 { |
|
2256 return 16; |
|
2257 } |
|
2258 |
|
2259 void |
|
2260 AddressBlockIpv6::SerializeAddress (uint8_t *buffer, ConstAddressIterator iter) const |
|
2261 { |
|
2262 Ipv6Address::ConvertFrom (*iter).Serialize (buffer); |
|
2263 } |
|
2264 |
|
2265 Address |
|
2266 AddressBlockIpv6::DeserializeAddress (uint8_t *buffer) const |
|
2267 { |
|
2268 return Ipv6Address::Deserialize (buffer); |
|
2269 } |
|
2270 |
|
2271 void |
|
2272 AddressBlockIpv6::PrintAddress (std::ostream &os, ConstAddressIterator iter) const |
|
2273 { |
|
2274 Ipv6Address::ConvertFrom (*iter).Print (os); |
|
2275 } |
|
2276 |
|
2277 /* End AddressBlockIpv6 Class */ |
|
2278 |
|
2279 Tlv::Tlv (void) |
|
2280 { |
|
2281 m_refCount = 1; |
|
2282 m_hasTypeExt = false; |
|
2283 m_hasIndexStart = false; |
|
2284 m_hasIndexStop = false; |
|
2285 m_isMultivalue = false; |
|
2286 m_hasValue = false; |
|
2287 } |
|
2288 |
|
2289 void |
|
2290 Tlv::SetType (uint8_t type) |
|
2291 { |
|
2292 m_type = type; |
|
2293 } |
|
2294 |
|
2295 uint8_t |
|
2296 Tlv::GetType (void) const |
|
2297 { |
|
2298 return m_type; |
|
2299 } |
|
2300 |
|
2301 void |
|
2302 Tlv::SetTypeExt (uint8_t typeExt) |
|
2303 { |
|
2304 m_typeExt = typeExt; |
|
2305 m_hasTypeExt = true; |
|
2306 } |
|
2307 |
|
2308 uint8_t |
|
2309 Tlv::GetTypeExt (void) const throw (PacketBBError) |
|
2310 { |
|
2311 if (!HasTypeExt()) |
|
2312 { |
|
2313 throw PacketBBError ("TLV has no type extension."); |
|
2314 } |
|
2315 return m_typeExt; |
|
2316 } |
|
2317 |
|
2318 bool |
|
2319 Tlv::HasTypeExt (void) const |
|
2320 { |
|
2321 return m_hasTypeExt; |
|
2322 } |
|
2323 |
|
2324 void |
|
2325 Tlv::SetIndexStart (uint8_t index) |
|
2326 { |
|
2327 m_indexStart = index; |
|
2328 m_hasIndexStart = true; |
|
2329 } |
|
2330 |
|
2331 uint8_t |
|
2332 Tlv::GetIndexStart (void) const throw (PacketBBError) |
|
2333 { |
|
2334 if (!HasIndexStart()) |
|
2335 { |
|
2336 throw PacketBBError ("TLV has no start index."); |
|
2337 } |
|
2338 return m_indexStart; |
|
2339 } |
|
2340 |
|
2341 bool |
|
2342 Tlv::HasIndexStart (void) const |
|
2343 { |
|
2344 return m_hasIndexStart; |
|
2345 } |
|
2346 |
|
2347 void |
|
2348 Tlv::SetIndexStop (uint8_t index) |
|
2349 { |
|
2350 m_indexStop = index; |
|
2351 m_hasIndexStop = true; |
|
2352 } |
|
2353 |
|
2354 uint8_t |
|
2355 Tlv::GetIndexStop (void) const throw (PacketBBError) |
|
2356 { |
|
2357 if (!HasIndexStop()) |
|
2358 { |
|
2359 throw PacketBBError ("TLV has no stop index."); |
|
2360 } |
|
2361 return m_indexStop; |
|
2362 } |
|
2363 |
|
2364 bool |
|
2365 Tlv::HasIndexStop (void) const |
|
2366 { |
|
2367 return m_hasIndexStop; |
|
2368 } |
|
2369 |
|
2370 void |
|
2371 Tlv::SetMultivalue (bool isMultivalue) |
|
2372 { |
|
2373 m_isMultivalue = isMultivalue; |
|
2374 } |
|
2375 |
|
2376 bool |
|
2377 Tlv::IsMultivalue (void) const |
|
2378 { |
|
2379 return m_isMultivalue; |
|
2380 } |
|
2381 |
|
2382 void |
|
2383 Tlv::SetValue (Buffer start) |
|
2384 { |
|
2385 m_hasValue = true; |
|
2386 m_value = start; |
|
2387 } |
|
2388 |
|
2389 void |
|
2390 Tlv::SetValue (const uint8_t * buffer, uint32_t size) |
|
2391 { |
|
2392 Buffer value; |
|
2393 value.AddAtStart (size); |
|
2394 value.Begin ().Write (buffer, size); |
|
2395 SetValue (value); |
|
2396 } |
|
2397 |
|
2398 Buffer |
|
2399 Tlv::GetValue (void) const throw (PacketBBError) |
|
2400 { |
|
2401 if (!HasValue ()) |
|
2402 { |
|
2403 throw PacketBBError ("TLV has no value."); |
|
2404 } |
|
2405 return m_value; |
|
2406 } |
|
2407 |
|
2408 bool |
|
2409 Tlv::HasValue (void) const |
|
2410 { |
|
2411 return m_hasValue; |
|
2412 } |
|
2413 |
|
2414 void |
|
2415 Tlv::Ref (void) const |
|
2416 { |
|
2417 m_refCount++; |
|
2418 } |
|
2419 |
|
2420 void |
|
2421 Tlv::Unref (void) const |
|
2422 { |
|
2423 m_refCount--; |
|
2424 if (m_refCount == 0) |
|
2425 { |
|
2426 delete this; |
|
2427 } |
|
2428 } |
|
2429 |
|
2430 uint32_t |
|
2431 Tlv::GetSerializedSize (void) const |
|
2432 { |
|
2433 /* type + flags */ |
|
2434 uint32_t size = 2; |
|
2435 |
|
2436 if (HasTypeExt ()) { |
|
2437 size++; |
|
2438 } |
|
2439 |
|
2440 if (HasIndexStart ()) { |
|
2441 size++; |
|
2442 } |
|
2443 |
|
2444 if (HasIndexStop ()) { |
|
2445 size++; |
|
2446 } |
|
2447 |
|
2448 if (HasValue ()) |
|
2449 { |
|
2450 if (GetValue ().GetSize () > 255) { |
|
2451 size += 2; |
|
2452 } else { |
|
2453 size++; |
|
2454 } |
|
2455 size += GetValue ().GetSize (); |
|
2456 } |
|
2457 |
|
2458 return size; |
|
2459 } |
|
2460 |
|
2461 void |
|
2462 Tlv::Serialize (Buffer::Iterator &start) const |
|
2463 { |
|
2464 start.WriteU8 (GetType ()); |
|
2465 |
|
2466 Buffer::Iterator bufref = start; |
|
2467 uint8_t flags = 0; |
|
2468 start.Next(); |
|
2469 |
|
2470 if (HasTypeExt()) |
|
2471 { |
|
2472 flags |= THAS_TYPE_EXT; |
|
2473 start.WriteU8 (GetTypeExt ()); |
|
2474 } |
|
2475 |
|
2476 if (HasIndexStart ()) |
|
2477 { |
|
2478 start.WriteU8 (GetIndexStart ()); |
|
2479 |
|
2480 if (HasIndexStop ()) |
|
2481 { |
|
2482 flags |= THAS_MULTI_INDEX; |
|
2483 start.WriteU8 (GetIndexStop ()); |
|
2484 } |
|
2485 else |
|
2486 { |
|
2487 flags |= THAS_SINGLE_INDEX; |
|
2488 } |
|
2489 } |
|
2490 |
|
2491 if (HasValue ()) { |
|
2492 flags |= THAS_VALUE; |
|
2493 |
|
2494 uint32_t size = GetValue ().GetSize (); |
|
2495 if (size > 255) |
|
2496 { |
|
2497 flags |= THAS_EXT_LEN; |
|
2498 start.WriteHtonU16 (size); |
|
2499 } |
|
2500 else |
|
2501 { |
|
2502 start.WriteU8 (size); |
|
2503 } |
|
2504 |
|
2505 if (IsMultivalue ()) |
|
2506 flags |= TIS_MULTIVALUE; |
|
2507 |
|
2508 start.Write(GetValue ().Begin (), GetValue ().End ()); |
|
2509 } |
|
2510 |
|
2511 bufref.WriteU8 (flags); |
|
2512 } |
|
2513 |
|
2514 void |
|
2515 Tlv::Deserialize (Buffer::Iterator &start) |
|
2516 { |
|
2517 SetType (start.ReadU8 ()); |
|
2518 |
|
2519 uint8_t flags = start.ReadU8 (); |
|
2520 |
|
2521 if (flags & THAS_TYPE_EXT) |
|
2522 SetTypeExt (start.ReadU8 ()); |
|
2523 |
|
2524 if (flags & THAS_MULTI_INDEX) |
|
2525 { |
|
2526 SetIndexStart (start.ReadU8 ()); |
|
2527 SetIndexStop (start.ReadU8 ()); |
|
2528 } |
|
2529 else if (flags & THAS_SINGLE_INDEX) |
|
2530 { |
|
2531 SetIndexStart (start.ReadU8 ()); |
|
2532 } |
|
2533 |
|
2534 if (flags & THAS_VALUE) |
|
2535 { |
|
2536 uint16_t len = 0; |
|
2537 |
|
2538 if (flags & THAS_EXT_LEN) |
|
2539 len = start.ReadNtohU16 (); |
|
2540 else |
|
2541 len = start.ReadU8 (); |
|
2542 |
|
2543 m_value.AddAtStart (len); |
|
2544 |
|
2545 Buffer::Iterator valueStart = start; |
|
2546 start.Next (len); |
|
2547 m_value.Begin ().Write (valueStart, start); |
|
2548 m_hasValue = true; |
|
2549 } |
|
2550 } |
|
2551 |
|
2552 void |
|
2553 Tlv::Print (std::ostream &os) const |
|
2554 { |
|
2555 Print (os, 0); |
|
2556 } |
|
2557 |
|
2558 void |
|
2559 Tlv::Print (std::ostream &os, int level) const |
|
2560 { |
|
2561 std::string prefix = ""; |
|
2562 for (int i = 0; i < level; i++) |
|
2563 prefix.append ("\t"); |
|
2564 |
|
2565 os << prefix << "Tlv {" << std::endl; |
|
2566 os << prefix << "\ttype = " << (int)GetType () << std::endl; |
|
2567 |
|
2568 if (HasTypeExt ()) |
|
2569 os << prefix << "\ttypeext = " << (int)GetTypeExt () << std::endl; |
|
2570 |
|
2571 if (HasIndexStart ()) |
|
2572 os << prefix << "\tindexStart = " << (int)GetIndexStart () << std::endl; |
|
2573 |
|
2574 if (HasIndexStop ()) |
|
2575 os << prefix << "\tindexStop = " << (int)GetIndexStop () << std::endl; |
|
2576 |
|
2577 os << prefix << "\tisMultivalue = " << IsMultivalue () << std::endl; |
|
2578 |
|
2579 if (HasValue ()) |
|
2580 os << prefix << "\thas value; size = " << GetValue (). GetSize () << std::endl; |
|
2581 |
|
2582 os << prefix << "}" << std::endl; |
|
2583 } |
|
2584 |
|
2585 bool |
|
2586 Tlv::operator== (const Tlv &other) const |
|
2587 { |
|
2588 if (GetType () != other.GetType ()) |
|
2589 return false; |
|
2590 |
|
2591 if (HasTypeExt () != other.HasTypeExt ()) |
|
2592 return false; |
|
2593 |
|
2594 if (HasTypeExt ()) |
|
2595 { |
|
2596 if (GetTypeExt () != other.GetTypeExt ()) |
|
2597 return false; |
|
2598 } |
|
2599 |
|
2600 if (HasValue () != other.HasValue ()) |
|
2601 return false; |
|
2602 |
|
2603 if (HasValue ()) |
|
2604 { |
|
2605 Buffer tv = GetValue (); |
|
2606 Buffer ov = other.GetValue (); |
|
2607 if (tv.GetSize () != ov.GetSize ()) |
|
2608 return false; |
|
2609 |
|
2610 /* The docs say I probably shouldn't use Buffer::PeekData, but I think it |
|
2611 * is justified in this case. */ |
|
2612 if (memcmp (tv.PeekData (), ov.PeekData (), tv.GetSize ()) != 0) |
|
2613 return false; |
|
2614 } |
|
2615 return true; |
|
2616 } |
|
2617 |
|
2618 bool |
|
2619 Tlv::operator!= (const Tlv &other) const |
|
2620 { |
|
2621 return !(*this == other); |
|
2622 } |
|
2623 |
|
2624 /* End Tlv Class */ |
|
2625 |
|
2626 void |
|
2627 AddressTlv::SetIndexStart (uint8_t index) |
|
2628 { |
|
2629 Tlv::SetIndexStart (index); |
|
2630 } |
|
2631 |
|
2632 uint8_t |
|
2633 AddressTlv::GetIndexStart (void) const throw (PacketBBError) |
|
2634 { |
|
2635 return Tlv::GetIndexStart (); |
|
2636 } |
|
2637 |
|
2638 bool |
|
2639 AddressTlv::HasIndexStart (void) const |
|
2640 { |
|
2641 return Tlv::HasIndexStart (); |
|
2642 } |
|
2643 |
|
2644 void |
|
2645 AddressTlv::SetIndexStop (uint8_t index) |
|
2646 { |
|
2647 Tlv::SetIndexStop (index); |
|
2648 } |
|
2649 |
|
2650 uint8_t |
|
2651 AddressTlv::GetIndexStop (void) const throw (PacketBBError) |
|
2652 { |
|
2653 return Tlv::GetIndexStop (); |
|
2654 } |
|
2655 |
|
2656 bool |
|
2657 AddressTlv::HasIndexStop (void) const |
|
2658 { |
|
2659 return Tlv::HasIndexStop (); |
|
2660 } |
|
2661 |
|
2662 void |
|
2663 AddressTlv::SetMultivalue (bool isMultivalue) |
|
2664 { |
|
2665 Tlv::SetMultivalue (isMultivalue); |
|
2666 } |
|
2667 |
|
2668 bool |
|
2669 AddressTlv::IsMultivalue (void) const |
|
2670 { |
|
2671 return Tlv::IsMultivalue (); |
|
2672 } |
|
2673 |
|
2674 } /* namespace pbb */ |
|
2675 |
|
2676 } /* namespace ns3 */ |