A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
packetbb.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /* vim: set ts=2 sw=2 sta expandtab ai si cin: */
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 /* These classes implement RFC 5444 - The Generalized Mobile Ad Hoc Network
22  * (MANET) Packet/PbbMessage Format
23  * See: http://tools.ietf.org/html/rfc5444 for details */
24 
25 #ifndef PACKETBB_H
26 #define PACKETBB_H
27 
28 #include <list>
29 
30 #include "ns3/ptr.h"
31 #include "ns3/address.h"
32 #include "ns3/header.h"
33 #include "ns3/buffer.h"
34 #include "ns3/simple-ref-count.h"
35 
36 namespace ns3 {
37 
38 /* Forward declare objects */
39 class PbbMessage;
40 class PbbAddressBlock;
41 class PbbTlv;
42 class PbbAddressTlv;
43 
46  IPV4 = 3,
47  IPV6 = 15,
48 };
49 
56 {
57 public:
58  typedef std::list< Ptr<PbbTlv> >::iterator Iterator;
59  typedef std::list< Ptr<PbbTlv> >::const_iterator ConstIterator;
60 
61  PbbTlvBlock (void);
62  ~PbbTlvBlock (void);
63 
67  Iterator Begin (void);
68 
72  ConstIterator Begin (void) const;
73 
77  Iterator End (void);
78 
82  ConstIterator End (void) const;
83 
87  int Size (void) const;
88 
92  bool Empty (void) const;
93 
97  Ptr<PbbTlv> Front (void) const;
98 
102  Ptr<PbbTlv> Back (void) const;
103 
108  void PushFront (Ptr<PbbTlv> tlv);
109 
113  void PopFront (void);
114 
119  void PushBack (Ptr<PbbTlv> tlv);
120 
124  void PopBack (void);
125 
133  Iterator Insert (Iterator position, const Ptr<PbbTlv> tlv);
134 
140  Iterator Erase (Iterator position);
141 
149  Iterator Erase (Iterator first, Iterator last);
150 
154  void Clear (void);
155 
159  uint32_t GetSerializedSize (void) const;
160 
168  void Serialize (Buffer::Iterator &start) const;
169 
178 
183  void Print (std::ostream &os) const;
184 
193  void Print (std::ostream &os, int level) const;
194 
195  bool operator== (const PbbTlvBlock &other) const;
196  bool operator!= (const PbbTlvBlock &other) const;
197 
198 private:
199  std::list< Ptr<PbbTlv> > m_tlvList;
200 };
201 
208 {
209 public:
210  typedef std::list< Ptr<PbbAddressTlv> >::iterator Iterator;
211  typedef std::list< Ptr<PbbAddressTlv> >::const_iterator ConstIterator;
212 
213  PbbAddressTlvBlock (void);
214  ~PbbAddressTlvBlock (void);
215 
219  Iterator Begin (void);
220 
224  ConstIterator Begin (void) const;
225 
229  Iterator End (void);
230 
234  ConstIterator End (void) const;
235 
239  int Size (void) const;
240 
244  bool Empty (void) const;
245 
249  Ptr<PbbAddressTlv> Front (void) const;
250 
254  Ptr<PbbAddressTlv> Back (void) const;
255 
260  void PushFront (Ptr<PbbAddressTlv> tlv);
261 
265  void PopFront (void);
266 
271  void PushBack (Ptr<PbbAddressTlv> tlv);
272 
276  void PopBack (void);
277 
285  Iterator Insert (Iterator position, const Ptr<PbbAddressTlv> tlv);
286 
292  Iterator Erase (Iterator position);
293 
303  Iterator Erase (Iterator first, Iterator last);
304 
308  void Clear (void);
309 
313  uint32_t GetSerializedSize (void) const;
314 
322  void Serialize (Buffer::Iterator &start) const;
323 
332 
337  void Print (std::ostream &os) const;
338 
347  void Print (std::ostream &os, int level) const;
348 
349  bool operator== (const PbbAddressTlvBlock &other) const;
350  bool operator!= (const PbbAddressTlvBlock &other) const;
351 
352 private:
353  std::list< Ptr<PbbAddressTlv> > m_tlvList;
354 };
355 
364 class PbbPacket : public SimpleRefCount<PbbPacket,Header>
365 {
366 public:
367  typedef std::list< Ptr<PbbTlv> >::iterator TlvIterator;
368  typedef std::list< Ptr<PbbTlv> >::const_iterator ConstTlvIterator;
369  typedef std::list< Ptr<PbbMessage> >::iterator MessageIterator;
370  typedef std::list< Ptr<PbbMessage> >::const_iterator ConstMessageIterator;
371 
372  PbbPacket (void);
373  ~PbbPacket (void);
374 
380  uint8_t GetVersion (void) const;
381 
386  void SetSequenceNumber (uint16_t number);
387 
394  uint16_t GetSequenceNumber (void) const;
395 
403  bool HasSequenceNumber (void) const;
404 
405  /* Manipulating Packet TLVs */
406 
410  TlvIterator TlvBegin (void);
411 
415  ConstTlvIterator TlvBegin (void) const;
416 
420  TlvIterator TlvEnd (void);
421 
426  ConstTlvIterator TlvEnd (void) const;
427 
431  int TlvSize (void) const;
432 
436  bool TlvEmpty (void) const;
437 
441  Ptr<PbbTlv> TlvFront (void);
442 
446  const Ptr<PbbTlv> TlvFront (void) const;
447 
451  Ptr<PbbTlv> TlvBack (void);
452 
456  const Ptr<PbbTlv> TlvBack (void) const;
457 
462  void TlvPushFront (Ptr<PbbTlv> tlv);
463 
467  void TlvPopFront (void);
468 
473  void TlvPushBack (Ptr<PbbTlv> tlv);
474 
478  void TlvPopBack (void);
479 
485  TlvIterator Erase (TlvIterator position);
486 
497 
501  void TlvClear (void);
502 
503  /* Manipulating Packet Messages */
504 
509 
513  ConstMessageIterator MessageBegin (void) const;
514 
519 
524  ConstMessageIterator MessageEnd (void) const;
525 
529  int MessageSize (void) const;
530 
534  bool MessageEmpty (void) const;
535 
540 
544  const Ptr<PbbMessage> MessageFront (void) const;
545 
550 
554  const Ptr<PbbMessage> MessageBack (void) const;
555 
560  void MessagePushFront (Ptr<PbbMessage> message);
561 
565  void MessagePopFront (void);
566 
571  void MessagePushBack (Ptr<PbbMessage> message);
572 
576  void MessagePopBack (void);
577 
584 
593 
597  void MessageClear (void);
598 
599  /* Methods implemented by all headers */
600  static TypeId GetTypeId (void);
601  virtual TypeId GetInstanceTypeId (void) const;
602 
606  virtual uint32_t GetSerializedSize (void) const;
607 
612  virtual void Serialize (Buffer::Iterator start) const;
613 
622  virtual uint32_t Deserialize (Buffer::Iterator start);
623 
628  virtual void Print (std::ostream &os) const;
629 
630  bool operator== (const PbbPacket &other) const;
631  bool operator!= (const PbbPacket &other) const;
632 
633 protected:
634 
635 private:
637  std::list< Ptr<PbbMessage> > m_messageList;
638 
639  uint8_t m_version;
640 
642  uint16_t m_seqnum;
643 };
644 
652 class PbbMessage : public SimpleRefCount<PbbMessage>
653 {
654 public:
655  typedef std::list< Ptr<PbbTlv> >::iterator TlvIterator;
656  typedef std::list< Ptr<PbbTlv> >::const_iterator ConstTlvIterator;
657  typedef std::list< Ptr<PbbAddressBlock> >::iterator AddressBlockIterator;
658  typedef std::list< Ptr<PbbAddressBlock> >::const_iterator ConstAddressBlockIterator;
659 
660  PbbMessage ();
661  virtual ~PbbMessage ();
662 
667  void SetType (uint8_t type);
668 
672  uint8_t GetType (void) const;
673 
678  void SetOriginatorAddress (Address address);
679 
686  Address GetOriginatorAddress (void) const;
687 
692  bool HasOriginatorAddress (void) const;
693 
698  void SetHopLimit (uint8_t hoplimit);
699 
706  uint8_t GetHopLimit (void) const;
707 
714  bool HasHopLimit (void) const;
715 
720  void SetHopCount (uint8_t hopcount);
721 
728  uint8_t GetHopCount (void) const;
729 
734  bool HasHopCount (void) const;
735 
740  void SetSequenceNumber (uint16_t seqnum);
741 
748  uint16_t GetSequenceNumber (void) const;
749 
754  bool HasSequenceNumber (void) const;
755 
756  /* Manipulating PbbMessage TLVs */
757 
762 
766  ConstTlvIterator TlvBegin () const;
767 
772  TlvIterator TlvEnd ();
773 
778  ConstTlvIterator TlvEnd () const;
779 
783  int TlvSize (void) const;
784 
788  bool TlvEmpty (void) const;
789 
793  Ptr<PbbTlv> TlvFront (void);
794 
798  const Ptr<PbbTlv> TlvFront (void) const;
799 
803  Ptr<PbbTlv> TlvBack (void);
804 
808  const Ptr<PbbTlv> TlvBack (void) const;
809 
814  void TlvPushFront (Ptr<PbbTlv> tlv);
815 
819  void TlvPopFront (void);
820 
825  void TlvPushBack (Ptr<PbbTlv> tlv);
826 
830  void TlvPopBack (void);
831 
837  TlvIterator TlvErase (TlvIterator position);
838 
849 
853  void TlvClear (void);
854 
855  /* Manipulating Address Block and Address TLV pairs */
856 
861 
866 
872 
878 
882  int AddressBlockSize (void) const;
883 
888  bool AddressBlockEmpty (void) const;
889 
894 
898  const Ptr<PbbAddressBlock> AddressBlockFront (void) const;
899 
904 
908  const Ptr<PbbAddressBlock> AddressBlockBack (void) const;
909 
915 
919  void AddressBlockPopFront (void);
920 
926 
930  void AddressBlockPopBack (void);
931 
938 
949  AddressBlockIterator last);
950 
954  void AddressBlockClear (void);
955 
966 
970  uint32_t GetSerializedSize (void) const;
971 
979  void Serialize (Buffer::Iterator &start) const;
980 
989 
994  void Print (std::ostream &os) const;
995 
1005  void Print (std::ostream &os, int level) const;
1006 
1007  bool operator== (const PbbMessage &other) const;
1008  bool operator!= (const PbbMessage &other) const;
1009 
1010 protected:
1011  /* PbbMessage size in bytes - 1.
1012  *
1013  * IPv4 = 4 - 1 = 3, IPv6 = 16 - 1 = 15
1014  */
1015  virtual PbbAddressLength GetAddressLength (void) const = 0;
1016 
1017  virtual void SerializeOriginatorAddress (Buffer::Iterator &start) const = 0;
1019  virtual void PrintOriginatorAddress (std::ostream &os) const = 0;
1020 
1022 
1023 private:
1025  std::list< Ptr<PbbAddressBlock> > m_addressBlockList;
1026 
1027  uint8_t m_type;
1029 
1032 
1034  uint8_t m_hopLimit;
1035 
1037  uint8_t m_hopCount;
1038 
1041 };
1042 
1048 class PbbMessageIpv4 : public PbbMessage {
1049 public:
1050  PbbMessageIpv4 ();
1051  virtual ~PbbMessageIpv4 ();
1052 
1053 protected:
1054  virtual PbbAddressLength GetAddressLength (void) const;
1055 
1056  virtual void SerializeOriginatorAddress (Buffer::Iterator &start) const;
1057  virtual Address DeserializeOriginatorAddress (Buffer::Iterator &start) const;
1058  virtual void PrintOriginatorAddress (std::ostream &os) const;
1059 
1061 };
1062 
1068 class PbbMessageIpv6 : public PbbMessage {
1069 public:
1070  PbbMessageIpv6 ();
1071  virtual ~PbbMessageIpv6 ();
1072 
1073 protected:
1074  virtual PbbAddressLength GetAddressLength (void) const;
1075 
1076  virtual void SerializeOriginatorAddress (Buffer::Iterator &start) const;
1077  virtual Address DeserializeOriginatorAddress (Buffer::Iterator &start) const;
1078  virtual void PrintOriginatorAddress (std::ostream &os) const;
1079 
1081 };
1082 
1089 class PbbAddressBlock : public SimpleRefCount<PbbAddressBlock>
1090 {
1091 public:
1092  typedef std::list< Address >::iterator AddressIterator;
1093  typedef std::list< Address >::const_iterator ConstAddressIterator;
1094 
1095  typedef std::list<uint8_t>::iterator PrefixIterator;
1096  typedef std::list<uint8_t>::const_iterator ConstPrefixIterator;
1097 
1100 
1101  PbbAddressBlock ();
1102  virtual ~PbbAddressBlock ();
1103 
1104  /* Manipulating the address block */
1105 
1110 
1114  ConstAddressIterator AddressBegin (void) const;
1115 
1119  AddressIterator AddressEnd (void);
1120 
1124  ConstAddressIterator AddressEnd (void) const;
1125 
1129  int AddressSize (void) const;
1130 
1134  bool AddressEmpty (void) const;
1135 
1139  Address AddressFront (void) const;
1140 
1144  Address AddressBack (void) const;
1145 
1150  void AddressPushFront (Address address);
1151 
1155  void AddressPopFront (void);
1156 
1161  void AddressPushBack (Address address);
1162 
1166  void AddressPopBack (void);
1167 
1176  const Address value);
1177 
1184 
1195 
1199  void AddressClear (void);
1200 
1201  /* Prefix methods */
1202 
1206  PrefixIterator PrefixBegin (void);
1207 
1211  ConstPrefixIterator PrefixBegin (void) const;
1212 
1216  PrefixIterator PrefixEnd (void);
1217 
1221  ConstPrefixIterator PrefixEnd (void) const;
1222 
1226  int PrefixSize (void) const;
1227 
1231  bool PrefixEmpty (void) const;
1232 
1236  uint8_t PrefixFront (void) const;
1237 
1241  uint8_t PrefixBack (void) const;
1242 
1247  void PrefixPushFront (uint8_t prefix);
1248 
1252  void PrefixPopFront (void);
1253 
1258  void PrefixPushBack (uint8_t prefix);
1259 
1263  void PrefixPopBack (void);
1264 
1272  PrefixIterator PrefixInsert (PrefixIterator position, const uint8_t value);
1273 
1280 
1291 
1295  void PrefixClear (void);
1296 
1297  /* Manipulating the TLV block */
1298 
1302  TlvIterator TlvBegin (void);
1303 
1307  ConstTlvIterator TlvBegin (void) const;
1308 
1312  TlvIterator TlvEnd (void);
1313 
1317  ConstTlvIterator TlvEnd (void) const;
1318 
1322  int TlvSize (void) const;
1323 
1327  bool TlvEmpty (void) const;
1328 
1333 
1337  const Ptr<PbbAddressTlv> TlvFront (void) const;
1338 
1342  Ptr<PbbAddressTlv> TlvBack (void);
1343 
1347  const Ptr<PbbAddressTlv> TlvBack (void) const;
1348 
1353  void TlvPushFront (Ptr<PbbAddressTlv> address);
1354 
1358  void TlvPopFront (void);
1359 
1364  void TlvPushBack (Ptr<PbbAddressTlv> address);
1365 
1369  void TlvPopBack (void);
1370 
1378  TlvIterator TlvInsert (TlvIterator position, const Ptr<PbbTlv> value);
1379 
1385  TlvIterator TlvErase (TlvIterator position);
1386 
1397 
1401  void TlvClear (void);
1402 
1406  uint32_t GetSerializedSize (void) const;
1407 
1415  void Serialize (Buffer::Iterator &start) const;
1416 
1425 
1430  void Print (std::ostream &os) const;
1431 
1441  void Print (std::ostream &os, int level) const;
1442 
1443  bool operator== (const PbbAddressBlock &other) const;
1444  bool operator!= (const PbbAddressBlock &other) const;
1445 
1446 protected:
1447  virtual uint8_t GetAddressLength (void) const = 0;
1448 
1449  virtual void SerializeAddress (uint8_t *buffer, ConstAddressIterator iter) const = 0;
1450  virtual Address DeserializeAddress (uint8_t *buffer) const = 0;
1451  virtual void PrintAddress (std::ostream &os, ConstAddressIterator iter) const = 0;
1452 
1453 private:
1454  uint8_t GetPrefixFlags (void) const;
1455  void GetHeadTail (uint8_t *head, uint8_t &headlen,
1456  uint8_t *tail, uint8_t &taillen) const;
1457  bool HasZeroTail (const uint8_t *tail, uint8_t taillen) const;
1458 
1459  std::list<Address> m_addressList;
1460  std::list<uint8_t> m_prefixList;
1462 };
1463 
1470 {
1471 public:
1473  virtual ~PbbAddressBlockIpv4 ();
1474 
1475 protected:
1476  virtual uint8_t GetAddressLength (void) const;
1477 
1478  virtual void SerializeAddress (uint8_t *buffer, ConstAddressIterator iter) const;
1479  virtual Address DeserializeAddress (uint8_t *buffer) const;
1480  virtual void PrintAddress (std::ostream &os, ConstAddressIterator iter) const;
1481 };
1482 
1489 {
1490 public:
1492  virtual ~PbbAddressBlockIpv6 ();
1493 
1494 protected:
1495  virtual uint8_t GetAddressLength (void) const;
1496 
1497  virtual void SerializeAddress (uint8_t *buffer, ConstAddressIterator iter) const;
1498  virtual Address DeserializeAddress (uint8_t *buffer) const;
1499  virtual void PrintAddress (std::ostream &os, ConstAddressIterator iter) const;
1500 };
1501 
1505 class PbbTlv : public SimpleRefCount<PbbTlv>
1506 {
1507 public:
1508  PbbTlv (void);
1509  virtual ~PbbTlv (void);
1510 
1515  void SetType (uint8_t type);
1516 
1520  uint8_t GetType (void) const;
1521 
1529  void SetTypeExt (uint8_t type);
1530 
1537  uint8_t GetTypeExt (void) const;
1538 
1546  bool HasTypeExt (void) const;
1547 
1555  void SetValue (Buffer start);
1556 
1565  void SetValue (const uint8_t * buffer, uint32_t size);
1566 
1573  Buffer GetValue (void) const;
1574 
1582  bool HasValue (void) const;
1583 
1587  uint32_t GetSerializedSize (void) const;
1588 
1596  void Serialize (Buffer::Iterator &start) const;
1597 
1605  void Deserialize (Buffer::Iterator &start);
1606 
1611  void Print (std::ostream &os) const;
1612 
1621  void Print (std::ostream &os, int level) const;
1622 
1623  bool operator== (const PbbTlv &other) const;
1624  bool operator!= (const PbbTlv &other) const;
1625 
1626 protected:
1627  void SetIndexStart (uint8_t index);
1628  uint8_t GetIndexStart (void) const;
1629  bool HasIndexStart (void) const;
1630 
1631  void SetIndexStop (uint8_t index);
1632  uint8_t GetIndexStop (void) const;
1633  bool HasIndexStop (void) const;
1634 
1635  void SetMultivalue (bool isMultivalue);
1636  bool IsMultivalue (void) const;
1637 
1638 private:
1639  uint8_t m_type;
1640 
1642  uint8_t m_typeExt;
1643 
1645  uint8_t m_indexStart;
1646 
1648  uint8_t m_indexStop;
1649 
1653 };
1654 
1658 class PbbAddressTlv : public PbbTlv
1659 {
1660 public:
1666  void SetIndexStart (uint8_t index);
1667 
1675  uint8_t GetIndexStart (void) const;
1676 
1684  bool HasIndexStart (void) const;
1685 
1691  void SetIndexStop (uint8_t index);
1692 
1700  uint8_t GetIndexStop (void) const;
1701 
1709  bool HasIndexStop (void) const;
1710 
1719  void SetMultivalue (bool isMultivalue);
1720 
1725  bool IsMultivalue (void) const;
1726 };
1727 
1728 } /* namespace ns3 */
1729 
1730 #endif /* PACKETBB_H */