Debian Codegraph @

protolib/include/manetMsg.h

raw
1 #ifndef _MANET_MESSAGE
2 #define _MANET_MESSAGE
3
4 /**
5 * @class ManetTlv
6 * @brief This C++ class is an implementation of the packet and message formats given
7 * in IETF RFC 5444
8 *
9 * Methods are provided to construct and parse the Type-Length-Value (TLV)
10 * based scheme described in that document.
11 */
12 #include <protoPkt.h>
13 #include <protoDefs.h>
14 #include <protoDebug.h>
15 #include <protoAddress.h>
16
17 namespace ManetMsgTypes
18 {
19 // These IANA registries are defined in RFC 5444 section 6 with specific assignment in various derived documents
20 // Standard defined Packet TLV types
21 enum PktTlv
22 {
23 };
24 // Standard defined Message Types
25 enum MsgType
26 {
27 MSG_TYPE_NHDP = 0, //RFC6130
28 MSG_TYPE_DYMO = 2, //TBD dymo document
29 MSG_TYPE_OLSRv2 = 1 //TBD olsrv2 document
30 };
31 // Standard defined Message TLV types
32 enum MsgTlv
33 {
34 MSG_TLV_INTERVAL_TIME = 0, //RFC5497
35 MSG_TLV_VALIDITY_TIME = 1, //RFC5497
36 MSG_TLV_NHDP_SMF = 128, //RFC6621
37 MSG_TLV_OLSR_MPR_WILLING = 129, //OLSRv2
38 MSG_TLV_OLSR_CONT_SEQ_NUM = 130 //OLSRv2
39 };
40 enum SubTypeSmfRelayAlgorithmID
41 {
42 SUB_TYPE_SMF_CF = 0, //RFC6621
43 SUB_TYPE_SMF_SMPR = 1, //RFC6621
44 SUB_TYPE_SMF_ECDS = 2, //RFC6621
45 SUB_TYPE_SMF_MPRCDS = 3, //RFC6621
46 SUB_TYPE_SMF_ECDS_ETX = 128 //TBD metric/ETX document
47 };
48 // Standard defined Address Block TLV types
49 enum AddrBlkTlv
50 {
51 ADDR_TLV_INTERVAL_TIME = 0, //RFC5497
52 ADDR_TLV_VALIDITY_TIME = 1, //RFC5497
53 ADDR_TLV_LOCAL_IF = 2, //RFC6130
54 ADDR_TLV_LINK_STATUS = 3, //RFC6130
55 ADDR_TLV_OTHER_NEIGHB = 4, //RFC6130
56 ADDR_TLV_OLSR_MPR = 5, //TBD OLSRv2
57 ADDR_TLV_OLSR_NBR_ADDR_TYPE = 6, //TBD OLSRv2
58 ADDR_TLV_OLSR_LINK_METRIC = 7, //TBD OLSRv2
59 ADDR_TLV_NHDP_SMF = 128, //RFC6621
60 ADDR_TLV_LINK_ETX_DF = 129, //TBD metric/ETX document (Delivery Ratio Forward/Outgoing)
61 ADDR_TLV_LINK_ETX_DR = 130 //TBD metric/ETX document (Delivery Ratio Reverse/Incoming)
62 };
63 }
64
65 class ManetTlv : public ProtoPkt
66 {
67 public:
68 ManetTlv();
69 ~ManetTlv();
70
71 // (TBD) Will there be standard "namespace:ietf:manet:message:tlvTypes"
72 // that we should enumerate here?
73
74 // Should this enum be moved to ManetMsg::TlvType???
75 enum Type
76 {
77 // (TBD) Provide methods for dealing with fragmented TLVs
78 MPR_WILLING = 10,
79 CONTENT_SEQ_NUMBER = 11, // value length
80 GATEWAY = 12
81 };
82
83 enum TypeFlag
84 {
85 TLV_USER = 0X80,
86 TLV_PROTOCOL = 0X40
87 };
88
89 // TLV building routines (these MUST be called in order given here)
90 bool InitIntoBuffer(UINT8 type, void* bufferPtr = NULL, unsigned numBytes = 0);
91
92 // Defined to use with any built-in enumerated TLV types (FRAGMENTATION, etc)
93 bool InitIntoBuffer(Type type, void* bufferPtr = NULL, unsigned int numBytes = 0)
94 {return InitIntoBuffer((UINT8)type, bufferPtr, numBytes);}
95
96 void SetType(UINT8 type)
97 {SetUINT8(OFFSET_TYPE,type); return;}
98
99 bool SetExtendedType(UINT8 extType);
100
101 void SetIsMultiValue() {return SetSemantic(MULTIVALUE);}
102 // By default, TLV is non-indexed, single value (or no value) until this is called
103 // (non-zero "numAddrs" indicates AddressBlock TLV and need for explicit index is determined)
104 bool SetIndexRange(UINT8 start, UINT8 stop, bool multivalue = false, unsigned int numAddrs = 0);
105
106
107
108 // By default, TLV is "no value" until this is called.
109 // Note "index" MUST be >= GetIndexStart() and <= GetIndexStop() if MULTIVALUE or -1 otherwise
110 // The optional "numAddrs" parameter is for multivalue AddressBlock TLVs where an index isn't present
111 // (Note that for building up TLVs, etc the index is always included for AddressBlock multivalue TLVs
112 // but this may change in the future. For parsing AddressBlock TLVs from other PacketBB builders,
113 // it is important that the "numAddrs" be indicated for AddressBlock TLVs.
114 bool SetValue(const char* value, UINT16 valueLength, int index = -1, unsigned int numAddrs = 0);
115
116 /**
117 * Here are some "shortcut" versions of SetValue()
118 * The index values are indexed to the address block not the value field itself.
119 **/
120 bool SetValue(UINT8 value, int index = -1, unsigned int numAddrs = 0)
121 {return (SetValue((char*)&value, sizeof(UINT8), index, numAddrs));}
122 bool SetValue(UINT16 value, int index = -1, unsigned int numAddrs = 0)
123 {value = htons(value); return (SetValue((char*)&value, sizeof(UINT16), index, numAddrs));}
124 bool SetValue(UINT32 value, int index = -1, unsigned int numAddrs = 0)
125 {value = htonl(value); return (SetValue((char*)&value, sizeof(UINT32), index, numAddrs));}
126
127 // This lets you edit the index range _after_ setting some values if desired
128 bool UpdateIndexRange(UINT8 start, UINT8 stop, bool multivalue, unsigned int numAddrs = 0);
129
130 // TLV parsing methods
131 // Note: This one is called by the ManetMsg and ManetAddrBlock "TlvIterator"
132 // classes so you shouldn't call this yourself, typically.
133 bool InitFromBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0);
134
135 UINT8 GetType() const
136 {return GetUINT8(OFFSET_TYPE);}
137 bool HasExtendedType() const
138 {return SemanticIsSet(EXTENDED_TYPE);}
139 UINT8 GetExtendedType() const
140 {return (HasExtendedType() ? GetUINT8(OFFSET_TYPE_EXT) : 0);}
141 UINT16 GetFullType() const
142 {return ((GetType() << 8) | GetExtendedType());}
143
144 bool HasIndex() const
145 {return !HasNoIndex();}
146 bool HasMultiIndex() const
147 {return SemanticIsSet(MULTI_INDEX);}
148 bool HasSingleIndex() const
149 {return SemanticIsSet(SINGLE_INDEX);}
150 UINT8 GetIndexStart() const
151 {return (HasIndex() ? GetUINT8(OffsetIndexStart()) : 0);}
152
153 // IMPORTANT: The "numAddrs" parameter SHOULD be set appropriately to properly parse AddressBlock TLVs
154 // (AddressBlock TLVs differ from Packet and Message TLVs in that an explicit index is _not_ required
155 // for multivalue TLVs (the index range 0..numAddrs-1 is assumed according to RFC 5444
156 UINT8 GetIndexStop(unsigned int numAddrs = 0) const
157 {return (HasSingleIndex() ? GetIndexStart() :
158 (HasMultiIndex() ? GetUINT8(OffsetIndexStop()) :
159 (SemanticIsSet(MULTIVALUE) ? (numAddrs - 1) : 0)));}
160
161 bool HasValue() const
162 {return (SemanticIsSet(HAS_VALUE));}
163 bool IsMultiValue() const
164 {
165 bool result = SemanticIsSet(MULTIVALUE);
166 ASSERT(!result || (HasValue() && !HasSingleIndex()));
167 return result;
168 }
169
170 // Returns _total_ length of tlv <value> field
171 UINT16 GetTlvLength() const;
172
173 // Returns length of _single_ tlv value (in bytes)
174 UINT16 GetValueLength(unsigned int numAddrs = 0) const;
175
176 /**
177 * This copies the value content. Index is related to the address block field and not the value field.
178 **/
179 bool GetValue(char* value, UINT16 valueLength, int index = -1, unsigned int numAddrs = 0) const;
180
181 /**
182 * Here are some "shortcut" methods for certain lengths.
183 * Index is related to the address block field and not the value field.
184 **/
185 bool GetValue(UINT8& value, int index = -1, unsigned int numAddrs = 0) const
186 {return GetValue((char*)&value, sizeof(UINT8), index, numAddrs);}
187 bool GetValue(UINT16& value, int index = -1, unsigned int numAddrs = 0) const
188 {
189 bool result = GetValue((char*)&value, sizeof(UINT16), index, numAddrs);
190 value = ntohs(value);
191 return result;
192 }
193
194 bool GetValue(UINT32& value, int index = -1, unsigned int numAddrs = 0) const
195 {
196 bool result = GetValue((char*)&value, sizeof(UINT32), index, numAddrs);
197 value = ntohl(value);
198 return result;
199 }
200
201 // This returns a pointer to the value portion of the tlv buffer_ptr
202 const char* GetValuePtr(UINT16 valueLength = 0, int index = -1, unsigned int numAddrs = 0) const;
203
204 private:
205
206 enum Semantic
207 {
208 EXTENDED_TYPE = 0x80, // Indicates TLV "type" has added 8-bits of extended type
209 SINGLE_INDEX = 0x40, // value for only "index-start" is given (no "index-stop" field)
210 MULTI_INDEX = 0x20, // value for only "index-start" is given (no "index-stop" field)
211 HAS_VALUE = 0x10, // no value content and no value length
212 EXTENDED_LENGTH = 0x08, // Indicates TLV "length" is 16-bit instead of 8-bit
213 MULTIVALUE = 0x04 // one value for each address in index range <startIndex:stopIndex>
214 };
215
216 bool HasNoIndex() const
217 {return !SemanticIsSet(SINGLE_INDEX) && !SemanticIsSet(MULTI_INDEX);}
218
219 bool HasExtendedLength() const
220 {return SemanticIsSet(EXTENDED_LENGTH);}
221
222 // Enumeration and methods to calculate offset of various TLV fields
223 enum
224 {
225 OFFSET_TYPE = 0,
226 OFFSET_SEMANTICS = OFFSET_TYPE + 1,
227 OFFSET_TYPE_EXT = OFFSET_SEMANTICS + 1
228 };
229 unsigned int OffsetIndexStart() const
230 {return (OFFSET_TYPE_EXT + (HasExtendedType() ? 1 : 0));}
231 unsigned int OffsetIndexStop() const
232 {return (OffsetIndexStart() + 1);}
233 unsigned int OffsetLength() const
234 {return (OffsetIndexStart() + (HasNoIndex() ? 0 : (HasSingleIndex() ? 1 : 2)));}
235 unsigned int OffsetValue() const
236 {return (OffsetLength() + (HasExtendedLength() ? 2 : 1));}
237
238 // Helper method used to in TLV build/parse initialization
239 static unsigned int GetMinLength(UINT8 semantics)
240 {
241 unsigned int minLength = 2; // "type" and "semantics" field
242 minLength += (0 != (semantics & EXTENDED_TYPE)) ? 1 : 0; // opt. "type-ext" field
243 minLength += (0 != (semantics & SINGLE_INDEX) )? // opt. "index" fields
244 1 :
245 ((0 != (semantics & MULTI_INDEX)) ? 2 : 0);
246 minLength += (0 != (semantics & HAS_VALUE)) ? // opt. "length" field
247 ((0 != (semantics & EXTENDED_LENGTH)) ? 2 : 1) :
248 0;
249 return minLength;
250 }
251
252 void SetTlvLength(UINT16 tlvLength);
253
254 bool SemanticIsSet(Semantic semantic) const
255 {return (0 != ((UINT8)semantic & GetUINT8(OFFSET_SEMANTICS)));}//((UINT8*)buffer_ptr)[OFFSET_SEMANTICS]));}
256 void SetSemantic(Semantic semantic)
257 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] |= (UINT8)semantic;}
258 void ClearSemantic(Semantic semantic)
259 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= ~((UINT8)semantic);}
260 void ClearSemantics(int flags)
261 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= ~((UINT8)flags);}
262
263 }; // end class ManetTlv
264
265
266
267 // Note: Typically, you should not have to use the ManetTlvBlock class directly yourself
268 class ManetTlvBlock: public ProtoPkt
269 {
270 public:
271 ManetTlvBlock();
272 ~ManetTlvBlock();
273
274 // TLV Block building methods
275 // Note: This one is called for you by the ManetMsg and ManetAddrBlock
276 // "AppendTlv()", etc routines, so you shouldn't normally
277 // need to make calls related to ManetTlvBlock yourself.
278 bool InitIntoBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0);
279
280 // This returns a ManetTlv pointer initialized into the
281 // tlv block's buffer_ptr space
282 ManetTlv* AppendTlv(UINT8 type = 0);
283
284 // This copies an existing ManetTlv into the tlv block's
285 // buffer_ptr space if there is room.
286 bool AppendTlv(ManetTlv& tlv);
287
288 void Pack();
289
290 // TLV Block parsing methods
291 bool InitFromBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0);
292
293 UINT16 GetTlvBlockLength()
294 {return (GetUINT16(OFFSET_LENGTH));}
295
296 class Iterator
297 {
298 public:
299 Iterator(ManetTlvBlock& tlvBlk);
300 ~Iterator();
301
302 void Reset() {tlv_temp.AttachBuffer(NULL, 0);}
303 bool GetNextTlv(ManetTlv & tlv)
304 {
305 bool result = tlv_block.GetNextTlv(tlv_temp);
306 tlv = tlv_temp;
307 return result;
308 }
309
310
311 private:
312 ManetTlvBlock& tlv_block;
313 ManetTlv tlv_temp;
314 }; // end class ManetTlvBlock::Iterator
315
316
317 private:
318 bool GetNextTlv(ManetTlv& tlv) const;
319
320 void SetTlvBlockLength(UINT16 tlvLength)
321 {SetUINT16(OFFSET_LENGTH, tlvLength);}
322
323 ManetTlv tlv_temp;
324 bool tlv_pending;
325
326 enum
327 {
328 OFFSET_LENGTH = 0, // 2-byte <tlv-length> field
329 OFFSET_CONTENT = 2
330 };
331
332 }; // end class ManetTlvBlock
333
334 class ManetAddrBlock : public ProtoPkt
335 {
336 public:
337 ManetAddrBlock();
338 ManetAddrBlock(void* bufferPtr, unsigned int numBytes, bool freeOnDestroy = false);
339 ~ManetAddrBlock();
340
341 // Addr block building methods (These MUST be called in order given)
342 // Note: This is called for you by ManetMsg::AppendAddressBlock() so
343 // typically you shouldn't call this one yourself.
344 bool InitIntoBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false);
345
346 // if <head-length> == addr-length, this sets the entire addr-block content
347 // Note: "hlen" is the prefix length in bytes
348 bool SetHead(const ProtoAddress& addr, UINT8 hlen);
349
350 bool SetTail(const ProtoAddress& addr, UINT8 tlen);
351
352 bool AppendAddress(const ProtoAddress& addr);
353
354 bool SetPrefixLength(UINT8 length, UINT8 index = 0);
355
356 // This returns a ManetTlv pointer initialized into the
357 // address block's buffer_ptr space
358 ManetTlv* AppendTlv(UINT8 type = 0);
359
360 // This copies an _existing_ ManetTlv into the address block's
361 // buffer_ptr space, if there is room.
362 // (This lets us build TLV's in a separate buffer_ptr if we choose to)
363 // (or copy TLVs from one block to another)
364 bool AppendTlv(ManetTlv& tlv);
365
366 // This finalizes the address block build
367 // Note: This is taken care of for you by ManetMsg::Pack() which is called by
368 // ManetPkt::Pack() so you shouldn't call this yourself if you use
369 // those constructs to encapsulate your address block.
370 void Pack();
371
372 // Addr block parsing methods
373 // Note: This is called for you by ManetMsg::AddrBlockIterator() so typically
374 // you shouldn't have to call this yourself.
375 bool InitFromBuffer(UINT8 addrLength, void* bufferPtr, unsigned int numBytes = 0);
376 UINT8 GetAddressLength() const
377 {return (addr_length);}
378 UINT8 GetAddressCount() const
379 {return GetUINT8(OFFSET_NUM_ADDR);}
380 bool GetAddress(UINT8 index, ProtoAddress& theAddr) const;
381 ProtoAddress::Type GetAddressType() const
382 {return ProtoAddress::GetType(addr_length);}
383
384 // These methods are provided for more optimal (but stateful)
385 // parsing by application code using this class
386 // (i.e, directly copy addresses into a buffer_ptr)
387 bool HasHead() const
388 {return (SemanticIsSet(HAS_HEAD));}
389 UINT8 GetHeadLength() const
390 {return (HasHead() ? GetUINT8(OFFSET_HEAD_LENGTH) : 0);}
391 const char* GetHead() const
392 {return ((char*)buffer_ptr + OFFSET_HEAD);}
393
394 bool HasTail() const
395 {return (SemanticIsSet(HAS_FULL_TAIL));}
396 bool HasZeroTail() const
397 {return SemanticIsSet(HAS_ZERO_TAIL);}
398 UINT8 GetTailLength() const
399 {return ((HasTail() || HasZeroTail()) ? GetUINT8(OffsetTailLength()) : 0);}
400 const char* GetTail(UINT8 index) const
401 {return ((char*)buffer_ptr + OffsetTail());}
402
403 UINT8 GetMidLength() const
404 {return (GetAddressLength() - GetHeadLength() - GetTailLength());}
405 const char* GetMid(UINT8 index) const
406 {
407 ASSERT(index < GetAddressCount());
408 return ((char*)buffer_ptr + OffsetMid() + index*GetMidLength());
409 }
410
411 bool HasSinglePrefixLength() const
412 {return SemanticIsSet(HAS_SINGLE_PREFIX_LEN);}
413 bool HasMultiPrefixLength() const
414 {return SemanticIsSet(HAS_MULTI_PREFIX_LEN);}
415 bool HasPrefixLength() const
416 {return (HasSinglePrefixLength() || HasMultiPrefixLength());}
417 // returns "prefix length" in bits
418 UINT8 GetPrefixLength(UINT8 index = 0)
419 {
420 if (HasSinglePrefixLength())
421 return GetUINT8(OffsetPrefixLength());
422 else if (HasMultiPrefixLength())
423 return GetUINT8(OffsetPrefixLength() + index);
424 else
425 return (GetAddressLength() << 3);
426 }
427
428 class TlvIterator : public ManetTlvBlock::Iterator
429 {
430 public:
431 TlvIterator(ManetAddrBlock& addrBlk);
432 ~TlvIterator();
433
434 void Reset()
435 {ManetTlvBlock::Iterator::Reset();}
436
437 bool GetNextTlv(ManetTlv& tlv)
438 {return ManetTlvBlock::Iterator::GetNextTlv(tlv);}
439
440 }; // end class ManetAddrBlock::TlvIterator
441
442 private:
443 UINT8 addr_length;
444 ManetTlvBlock tlv_block;
445 bool tlv_block_pending;
446
447 enum SemanticFlag
448 {
449 HAS_HEAD = 0x80,
450 HAS_FULL_TAIL = 0x40,
451 HAS_ZERO_TAIL = 0x20,
452 HAS_SINGLE_PREFIX_LEN = 0x10,
453 HAS_MULTI_PREFIX_LEN = 0x08
454 };
455
456 bool SemanticIsSet(SemanticFlag flag) const
457 {return (0 != (flag & GetUINT8(OFFSET_SEMANTICS)));}
458 void SetSemantic(SemanticFlag flag)
459 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] |= (UINT8)flag;}
460 void ClearSemantic(SemanticFlag flag)
461 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= (UINT8)(~flag);}
462 void SetSemantics(UINT8 semantics)
463 {SetUINT8(OFFSET_SEMANTICS, semantics);}
464 void ClearAllSemantics()
465 {SetSemantics(0);}
466
467 enum
468 {
469 OFFSET_NUM_ADDR = 0,
470 OFFSET_SEMANTICS = OFFSET_NUM_ADDR + 1,
471 OFFSET_HEAD_LENGTH = OFFSET_SEMANTICS + 1, // optional
472 OFFSET_HEAD = OFFSET_HEAD_LENGTH + 1 // optional
473 };
474 unsigned int OffsetTailLength() const
475 {return (OFFSET_HEAD_LENGTH + (HasHead() ? (1 + GetHeadLength()) : 0));}
476 unsigned int OffsetTail() const
477 {return (OffsetTailLength() + 1);}
478 unsigned int OffsetMid() const
479 {
480 UINT16 offset = OffsetTailLength();
481 offset += (HasTail() ? (1 + (HasZeroTail() ? 0 : GetTailLength())) : 0);
482 return offset;
483 }
484 unsigned int OffsetPrefixLength() const
485 {return (OffsetMid() + (GetAddressCount() * GetMidLength()));}
486
487 void SetAddressCount(UINT8 numAddrs)
488 {SetUINT8(OFFSET_NUM_ADDR, numAddrs);}
489
490 }; // end class ManetAddrBlock
491
492 /**
493 * @class ManetMsg
494 *
495 * @brief Class that implements the general packet format of IETF RFC 5444
496 */
497 class ManetMsg : public ProtoPkt
498 {
499 public:
500 ManetMsg();
501 ManetMsg(void* bufferPtr, unsigned int numBytes, bool freeOnDestroy = false);
502 virtual ~ManetMsg();
503
504 // (TBD) Will there be standard "namespace:ietf:manet:message:types"
505 // that we should enumerate here?
506 // Message Building (all header & info fields MUST be set
507 // _before_ tlv's, etc). Also message header/info fields
508 // MUST be set in order given here!
509
510 // Init into given "bufferPtr" space
511 // Note:: ManetPkt::AppendMessage() will call this one for you so
512 // typically you shouldn't need to call this
513 bool InitIntoBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0);
514
515 // This MUST be called if "SetOriginator()" is not called.
516 void SetAddressLength(UINT8 addrLength)
517 {
518 UINT8 semantics = ((UINT8*)buffer_ptr)[OFFSET_SEMANTICS];
519 semantics &= 0xf0;
520 semantics |= ((addrLength-1) & 0x0f);
521 ((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] = semantics;
522 }
523 void SetType(UINT8 type)
524 {SetUINT8(OFFSET_TYPE, type);}
525
526 // <msg-semantics> flags are set as corresponding fields are set
527 // <msg-size> is set when message is Pack()'d
528
529 bool SetOriginator(const ProtoAddress& addr);
530
531 bool SetHopLimit(UINT8 hopLimit);
532
533 bool SetHopCount(UINT8 hopCount);
534
535 bool SetSequence(UINT16 sequence);
536
537 // Use next to append any message TLV's (and set their indexes, values, etc as needed)
538 // (TBD - add code to enforce "order contraint" as they are added)
539
540 // Append a <msg-tlv>
541 // This returns a ManetTlv pointer initialized into the ManetMsg buffer_ptr space
542 ManetTlv* AppendTlv(UINT8 type = 0)
543 {return (tlv_block_pending ? tlv_block.AppendTlv(type) : NULL);}
544
545 // This copies an existing ManetTlv into the message's buffer_ptr space
546 // if there is room. (This lets us build TLV's in a separate
547 // buffer_ptr if we so choose)
548 bool AppendTlv(ManetTlv& tlv)
549 {
550 ASSERT(!tlv.HasIndex()); // msg-tlv's never have index!
551 return (tlv_block_pending ? tlv_block.AppendTlv(tlv) : false);
552 }
553
554 // Then, append any address block(s)
555 // This returns a pointer to an address-block initialized into the message's buffer_ptr
556 // (This pointer is for temporary use and should not be deleted)
557 ManetAddrBlock* AppendAddressBlock();
558
559 // Note: ManetPkt::Pack() will call this for you, so you don't have to!
560 void Pack();
561
562 // Message parsing
563 // Note: InitFromBuffer() is called for you by the ManetPkt::Iterator()
564 // so you shouldn't need to call this (perhaps this should be private
565 // assuming the message is encapsulated in an ManetPkt?)
566
567 bool InitFromBuffer(void* bufferPtr, unsigned int numBytes = 0);
568
569 UINT8 GetType() const
570 {return GetUINT8(OFFSET_TYPE);}
571
572 UINT16 GetMsgSize() const
573 {return GetUINT16(OffsetSize());}
574
575 UINT8 GetAddressLength() const
576 {return ((GetUINT8(OFFSET_SEMANTICS) & 0x0F) + 1);}
577
578 ProtoAddress::Type GetAddressType() const
579 {return ProtoAddress::GetType(GetAddressLength());}
580
581 bool HasOriginator() const
582 {return (SemanticIsSet(HAS_ORIGINATOR));}
583
584 bool GetOriginator(ProtoAddress& addr) const;
585
586 void GetOriginatorAsBytes(char* target) const
587 {memcpy(target, (char*)buffer_ptr+OffsetOriginator(), GetAddressLength());}
588
589 bool HasHopLimit() const
590 {return (SemanticIsSet(HAS_HOP_LIMIT));}
591
592 UINT8 GetHopLimit() const
593 {return GetUINT8(OffsetHopLimit());}
594
595 bool HasHopCount() const
596 {return (SemanticIsSet(HAS_HOP_COUNT));}
597
598 UINT8 GetHopCount() const
599 {return GetUINT8(OffsetHopCount());}
600
601 bool HasSequence() const
602 {return (SemanticIsSet(HAS_SEQ_NUM));}
603
604 UINT16 GetSequence() const
605 {return GetUINT16(OffsetSequence());}
606
607 class TlvIterator : public ManetTlvBlock::Iterator
608 {
609 public:
610 TlvIterator(ManetMsg& msg);
611 ~TlvIterator();
612
613 void Reset() {ManetTlvBlock::Iterator::Reset();}
614
615 bool GetNextTlv(ManetTlv& tlv)
616 {return ManetTlvBlock::Iterator::GetNextTlv(tlv);}
617 }; // end class ManetAddrBlock::TlvIterator
618
619 class AddrBlockIterator
620 {
621 public:
622 AddrBlockIterator(ManetMsg& theMsg);
623 ~AddrBlockIterator();
624
625 void Reset()
626 {addr_block_temp.AttachBuffer(NULL, 0);}
627
628 bool GetNextAddressBlock(ManetAddrBlock& addrBlk)
629 {
630 bool result = msg.GetNextAddressBlock(addr_block_temp);
631 addrBlk = addr_block_temp;
632 return result;
633 }
634 private:
635 ManetMsg& msg;
636 ManetAddrBlock addr_block_temp;
637 }; // end class ManetMessage::AddrBlockIterator()
638
639 protected:
640 bool GetTlvBlock(ManetTlvBlock& tlvBlk) const
641 {
642 UINT16 tlvBlockOffset = OffsetTlvBlock();
643 char* tlvBuffer = (char*)buffer_ptr + tlvBlockOffset;
644 return tlvBlk.InitFromBuffer(tlvBuffer, pkt_length - tlvBlockOffset);
645 }
646
647 bool GetNextAddressBlock(ManetAddrBlock& addrBlk) const;
648
649 enum SemanticFlag
650 {
651 HAS_ORIGINATOR = 0x80,
652 HAS_HOP_LIMIT = 0x40,
653 HAS_HOP_COUNT = 0x20,
654 HAS_SEQ_NUM = 0x10
655 };
656
657 bool SemanticIsSet(SemanticFlag flag) const
658 {return (0 != (flag & ((UINT8*)buffer_ptr)[OFFSET_SEMANTICS]));}
659 void SetSemantic(SemanticFlag flag)
660 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] |= (UINT8)flag;}
661 void ClearSemantic(SemanticFlag flag)
662 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= (UINT8)(~flag);}
663 void ClearAllSemantics()
664 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] = 0;}
665
666 void SetMsgSize(UINT16 numBytes)
667 {SetUINT16(OffsetSize(), numBytes);}
668
669 ManetTlvBlock tlv_block; // msg-tlv-blk
670 bool tlv_block_pending;
671 ManetAddrBlock addr_block_temp;
672 bool addr_block_pending;
673
674 // Byte offsets to various message header fields, etc
675 enum
676 {
677 OFFSET_TYPE = 0,
678 OFFSET_SEMANTICS = OFFSET_TYPE + 1,
679 OFFSET_SIZE = OFFSET_SEMANTICS + 1
680 };
681 unsigned int OffsetSize() const
682 {return OFFSET_SIZE;}
683 unsigned int OffsetOriginator() const
684 {return (OffsetSize() + 2);}
685 unsigned int OffsetHopLimit() const
686 {return (OffsetOriginator() + (HasOriginator() ? GetAddressLength() : 0));}
687 unsigned int OffsetHopCount() const
688 {return (OffsetHopLimit() + (HasHopLimit() ? 1 : 0));}
689 unsigned int OffsetSequence() const
690 {return (OffsetHopCount() + (HasHopCount() ? 1 : 0));}
691 UINT16 OffsetTlvBlock() const
692 {return (OffsetSequence() + (HasSequence() ? 2 : 0));}
693
694 }; // end class ManetMsg
695
696 /**
697 * @class ManetPkt
698 *
699 * @brief Manet Packet building/parsing class of IETF RFC 5444
700 *
701 */
702 class ManetPkt : public ProtoPkt
703 {
704 public:
705 ManetPkt();
706 ManetPkt(void* bufferPtr, unsigned int numBytes, bool freeOnDestruct = false);
707 ~ManetPkt();
708
709 // Packet building methods (IMPORTANT: Call these in the order given)
710 bool InitIntoBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0);
711
712 void SetVersion(UINT8 version);
713
714 void SetSequence(UINT16 sequence);
715
716 // Two possible methods to append TLVs to packet tlv block
717 ManetTlv* AppendTlv(UINT8 type = 0);
718
719 // This copies an existing ManetTlv into the packets's buffer_ptr space
720 // if there is room. (This lets us build TLV's in a separate
721 // buffer_ptr if we so choose for whatever reason)
722 bool AppendTlv(ManetTlv& tlv);
723
724 // "Attach", "build", then "pack" messages into ManetPkt
725 // This returns a pointer to a ManetMsg initialized into the packet's buffer_ptr space
726 ManetMsg* AppendMessage();
727
728 // This one appends, by copying, an existing "ManetMsg" to the pkt
729 // (Can be used to copy a message into a "ManetPkt")
730 bool AppendMessage(ManetMsg& msg);
731
732 // Must call this last to "finalize" the packet and its contents
733 void Pack();
734
735 // Packet parsing methods
736 bool InitFromBuffer(unsigned int pktLength, void* bufferPtr = NULL, unsigned int numBytes = 0);
737
738 UINT8 GetVersion() const
739 {return GetUINT8(OFFSET_SEMANTICS) & 0xF0;}
740
741 UINT16 GetPktSize() const
742 {return GetLength();}
743
744 bool HasSequence() const
745 {return SemanticIsSet(HAS_SEQ_NUM);}
746 UINT16 GetSequence() const
747 {
748 ASSERT(SemanticIsSet(HAS_SEQ_NUM));
749 return GetUINT16(OffsetSequence());
750 }
751
752 bool HasTlvBlock() const
753 {return SemanticIsSet(HAS_TLV_BLOCK);}
754
755 const char* GetPayload() const
756 {return ((char*)buffer_ptr + OffsetPayload());}
757
758 UINT16 GetPayloadLength() const
759 {return ((pkt_length > OffsetPayload()) ? (pkt_length - OffsetPayload()) : 0);}
760
761 class TlvIterator : public ManetTlvBlock::Iterator
762 {
763 public:
764 TlvIterator(ManetPkt& pkt);
765 ~TlvIterator();
766
767 void Reset() {ManetTlvBlock::Iterator::Reset();}
768
769 bool GetNextTlv(ManetTlv& tlv)
770 {return ManetTlvBlock::Iterator::GetNextTlv(tlv);}
771 }; // end class ManetPkt::TlvIterator
772
773
774 class MsgIterator
775 {
776 public:
777 MsgIterator(ManetPkt& thePkt);
778 ~MsgIterator();
779
780 void Reset()
781 {msg_temp.AttachBuffer(NULL, 0);}
782
783 bool GetNextMessage(ManetMsg& msg)
784 {
785 bool result = pkt.GetNextMessage(msg_temp);
786 msg = msg_temp;
787 return result;
788 }
789
790 private:
791 ManetPkt& pkt;
792 ManetMsg msg_temp;
793 }; // end class ManetPkt::MsgIterator
794
795
796 private:
797 // IMPORTANT NOTE: All of the offsets are
798 // _byte_ offsets, since there
799 // is not much consideration
800 // towards alignment in the
801 // "PacketBB" specification.
802 enum
803 {
804 OFFSET_SEMANTICS = 0,
805 OFFSET_SEQUENCE_NUMBER = 1
806 };
807 unsigned OffsetSequence() const
808 {return OFFSET_SEQUENCE_NUMBER;}
809 unsigned int OffsetTlvBlock() const
810 {return (OffsetSequence() + (HasSequence() ? 2 : 0));}
811 unsigned int OffsetPayload() const
812 {return (OffsetTlvBlock() + (HasTlvBlock() ? tlv_block.GetLength() : 0));}
813
814 enum SemanticFlag
815 {
816 HAS_SEQ_NUM = 0x08, // sequence (2 bytes) if set
817 HAS_TLV_BLOCK = 0x04 // packet tlv block present if set
818 };
819
820 bool SemanticIsSet(SemanticFlag flag) const
821 {return (0 != (flag & GetUINT8(OFFSET_SEMANTICS)));}
822 void SetSemantic(SemanticFlag flag)
823 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] |= (UINT8)flag;}
824 void ClearSemantic(SemanticFlag flag)
825 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= (UINT8)(~flag);}
826 void ClearAllSemantics()
827 {((UINT8*)buffer_ptr)[OFFSET_SEMANTICS] &= 0xF0;}
828
829 // This is provided for our friend, the "ManetPkt::MsgIterator"
830 bool GetNextMessage(ManetMsg& msg);
831
832 static unsigned int GetMinLength(UINT8 semantics)
833 {
834 unsigned int minLength = 1; // "semantics" fields
835 minLength += (0 != ((UINT8)HAS_SEQ_NUM & semantics)) ? 2 : 0;
836 minLength += (0 != ((UINT8)HAS_TLV_BLOCK & semantics)) ? 2 : 0;
837 return minLength;
838 }
839
840 ManetTlvBlock tlv_block; // pkt-tlv-blk
841 bool tlv_block_pending;
842 ManetMsg msg_temp;
843 bool msg_pending;
844
845 }; // end class ManetPkt
846
847 #endif // _MANET_MESSAGE