A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
aodv-packet.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Based on
7 * NS-2 AODV model developed by the CMU/MONARCH group and optimized and
8 * tuned by Samir Das and Mahesh Marina, University of Cincinnati;
9 *
10 * AODV-UU implementation by Erik Nordström of Uppsala University
11 * https://web.archive.org/web/20100527072022/http://core.it.uu.se/core/index.php/AODV-UU
12 *
13 * Authors: Elena Buchatskaia <borovkovaes@iitp.ru>
14 * Pavel Boyko <boyko@iitp.ru>
15 */
16#include "aodv-packet.h"
17
18#include "ns3/address-utils.h"
19#include "ns3/packet.h"
20
21namespace ns3
22{
23namespace aodv
24{
25
27
29 : m_type(t),
30 m_valid(true)
31{
32}
33
36{
37 static TypeId tid = TypeId("ns3::aodv::TypeHeader")
39 .SetGroupName("Aodv")
40 .AddConstructor<TypeHeader>();
41 return tid;
42}
43
46{
47 return GetTypeId();
48}
49
52{
53 return 1;
54}
55
56void
58{
59 i.WriteU8((uint8_t)m_type);
60}
61
64{
65 Buffer::Iterator i = start;
66 uint8_t type = i.ReadU8();
67 m_valid = true;
68 switch (type)
69 {
70 case AODVTYPE_RREQ:
71 case AODVTYPE_RREP:
72 case AODVTYPE_RERR:
73 case AODVTYPE_RREP_ACK: {
74 m_type = (MessageType)type;
75 break;
76 }
77 default:
78 m_valid = false;
79 }
80 uint32_t dist = i.GetDistanceFrom(start);
82 return dist;
83}
84
85void
86TypeHeader::Print(std::ostream& os) const
87{
88 switch (m_type)
89 {
90 case AODVTYPE_RREQ: {
91 os << "RREQ";
92 break;
93 }
94 case AODVTYPE_RREP: {
95 os << "RREP";
96 break;
97 }
98 case AODVTYPE_RERR: {
99 os << "RERR";
100 break;
101 }
102 case AODVTYPE_RREP_ACK: {
103 os << "RREP_ACK";
104 break;
105 }
106 default:
107 os << "UNKNOWN_TYPE";
108 }
109}
110
111bool
113{
114 return (m_type == o.m_type && m_valid == o.m_valid);
115}
116
117std::ostream&
118operator<<(std::ostream& os, const TypeHeader& h)
119{
120 h.Print(os);
121 return os;
122}
123
124//-----------------------------------------------------------------------------
125// RREQ
126//-----------------------------------------------------------------------------
128 uint8_t reserved,
129 uint8_t hopCount,
130 uint32_t requestID,
131 Ipv4Address dst,
132 uint32_t dstSeqNo,
133 Ipv4Address origin,
134 uint32_t originSeqNo)
135 : m_flags(flags),
136 m_reserved(reserved),
137 m_hopCount(hopCount),
138 m_requestID(requestID),
139 m_dst(dst),
140 m_dstSeqNo(dstSeqNo),
141 m_origin(origin),
142 m_originSeqNo(originSeqNo)
143{
144}
145
147
148TypeId
150{
151 static TypeId tid = TypeId("ns3::aodv::RreqHeader")
152 .SetParent<Header>()
153 .SetGroupName("Aodv")
154 .AddConstructor<RreqHeader>();
155 return tid;
156}
157
158TypeId
160{
161 return GetTypeId();
162}
163
166{
167 return 23;
168}
169
170void
182
185{
186 Buffer::Iterator i = start;
187 m_flags = i.ReadU8();
188 m_reserved = i.ReadU8();
189 m_hopCount = i.ReadU8();
191 ReadFrom(i, m_dst);
193 ReadFrom(i, m_origin);
195
196 uint32_t dist = i.GetDistanceFrom(start);
197 NS_ASSERT(dist == GetSerializedSize());
198 return dist;
199}
200
201void
202RreqHeader::Print(std::ostream& os) const
203{
204 os << "RREQ ID " << m_requestID << " destination: ipv4 " << m_dst << " sequence number "
205 << m_dstSeqNo << " source: ipv4 " << m_origin << " sequence number " << m_originSeqNo
206 << " flags:"
207 << " Gratuitous RREP " << (*this).GetGratuitousRrep() << " Destination only "
208 << (*this).GetDestinationOnly() << " Unknown sequence number " << (*this).GetUnknownSeqno();
209}
210
211std::ostream&
212operator<<(std::ostream& os, const RreqHeader& h)
213{
214 h.Print(os);
215 return os;
216}
217
218void
220{
221 if (f)
222 {
223 m_flags |= (1 << 5);
224 }
225 else
226 {
227 m_flags &= ~(1 << 5);
228 }
229}
230
231bool
233{
234 return (m_flags & (1 << 5));
235}
236
237void
239{
240 if (f)
241 {
242 m_flags |= (1 << 4);
243 }
244 else
245 {
246 m_flags &= ~(1 << 4);
247 }
248}
249
250bool
252{
253 return (m_flags & (1 << 4));
254}
255
256void
258{
259 if (f)
260 {
261 m_flags |= (1 << 3);
262 }
263 else
264 {
265 m_flags &= ~(1 << 3);
266 }
267}
268
269bool
271{
272 return (m_flags & (1 << 3));
273}
274
275bool
277{
278 return (m_flags == o.m_flags && m_reserved == o.m_reserved && m_hopCount == o.m_hopCount &&
281}
282
283//-----------------------------------------------------------------------------
284// RREP
285//-----------------------------------------------------------------------------
286
287RrepHeader::RrepHeader(uint8_t prefixSize,
288 uint8_t hopCount,
289 Ipv4Address dst,
290 uint32_t dstSeqNo,
291 Ipv4Address origin,
292 Time lifeTime)
293 : m_flags(0),
294 m_prefixSize(prefixSize),
295 m_hopCount(hopCount),
296 m_dst(dst),
297 m_dstSeqNo(dstSeqNo),
298 m_origin(origin)
299{
300 m_lifeTime = uint32_t(lifeTime.GetMilliSeconds());
301}
302
304
305TypeId
307{
308 static TypeId tid = TypeId("ns3::aodv::RrepHeader")
309 .SetParent<Header>()
310 .SetGroupName("Aodv")
311 .AddConstructor<RrepHeader>();
312 return tid;
313}
314
315TypeId
317{
318 return GetTypeId();
319}
320
323{
324 return 19;
325}
326
327void
338
341{
342 Buffer::Iterator i = start;
343
344 m_flags = i.ReadU8();
345 m_prefixSize = i.ReadU8();
346 m_hopCount = i.ReadU8();
347 ReadFrom(i, m_dst);
349 ReadFrom(i, m_origin);
351
352 uint32_t dist = i.GetDistanceFrom(start);
353 NS_ASSERT(dist == GetSerializedSize());
354 return dist;
355}
356
357void
358RrepHeader::Print(std::ostream& os) const
359{
360 os << "destination: ipv4 " << m_dst << " sequence number " << m_dstSeqNo;
361 if (m_prefixSize != 0)
362 {
363 os << " prefix size " << m_prefixSize;
364 }
365 os << " source ipv4 " << m_origin << " lifetime " << m_lifeTime
366 << " acknowledgment required flag " << (*this).GetAckRequired();
367}
368
369void
374
375Time
377{
379 return t;
380}
381
382void
384{
385 if (f)
386 {
387 m_flags |= (1 << 6);
388 }
389 else
390 {
391 m_flags &= ~(1 << 6);
392 }
393}
394
395bool
397{
398 return (m_flags & (1 << 6));
399}
400
401void
403{
404 m_prefixSize = sz;
405}
406
407uint8_t
409{
410 return m_prefixSize;
411}
412
413bool
415{
416 return (m_flags == o.m_flags && m_prefixSize == o.m_prefixSize && m_hopCount == o.m_hopCount &&
417 m_dst == o.m_dst && m_dstSeqNo == o.m_dstSeqNo && m_origin == o.m_origin &&
419}
420
421void
423{
424 m_flags = 0;
425 m_prefixSize = 0;
426 m_hopCount = 0;
427 m_dst = origin;
428 m_dstSeqNo = srcSeqNo;
429 m_origin = origin;
430 m_lifeTime = lifetime.GetMilliSeconds();
431}
432
433std::ostream&
434operator<<(std::ostream& os, const RrepHeader& h)
435{
436 h.Print(os);
437 return os;
438}
439
440//-----------------------------------------------------------------------------
441// RREP-ACK
442//-----------------------------------------------------------------------------
443
445 : m_reserved(0)
446{
447}
448
450
451TypeId
453{
454 static TypeId tid = TypeId("ns3::aodv::RrepAckHeader")
455 .SetParent<Header>()
456 .SetGroupName("Aodv")
457 .AddConstructor<RrepAckHeader>();
458 return tid;
459}
460
461TypeId
463{
464 return GetTypeId();
465}
466
469{
470 return 1;
471}
472
473void
478
481{
482 Buffer::Iterator i = start;
483 m_reserved = i.ReadU8();
484 uint32_t dist = i.GetDistanceFrom(start);
485 NS_ASSERT(dist == GetSerializedSize());
486 return dist;
487}
488
489void
490RrepAckHeader::Print(std::ostream& os) const
491{
492}
493
494bool
496{
497 return m_reserved == o.m_reserved;
498}
499
500std::ostream&
501operator<<(std::ostream& os, const RrepAckHeader& h)
502{
503 h.Print(os);
504 return os;
505}
506
507//-----------------------------------------------------------------------------
508// RERR
509//-----------------------------------------------------------------------------
511 : m_flag(0),
512 m_reserved(0)
513{
514}
515
517
518TypeId
520{
521 static TypeId tid = TypeId("ns3::aodv::RerrHeader")
522 .SetParent<Header>()
523 .SetGroupName("Aodv")
524 .AddConstructor<RerrHeader>();
525 return tid;
526}
527
528TypeId
530{
531 return GetTypeId();
532}
533
536{
537 return (3 + 8 * GetDestCount());
538}
539
540void
542{
543 i.WriteU8(m_flag);
546 for (auto j = m_unreachableDstSeqNo.begin(); j != m_unreachableDstSeqNo.end(); ++j)
547 {
548 WriteTo(i, (*j).first);
549 i.WriteHtonU32((*j).second);
550 }
551}
552
555{
556 Buffer::Iterator i = start;
557 m_flag = i.ReadU8();
558 m_reserved = i.ReadU8();
559 uint8_t dest = i.ReadU8();
560 m_unreachableDstSeqNo.clear();
561 Ipv4Address address;
562 uint32_t seqNo;
563 for (uint8_t k = 0; k < dest; ++k)
564 {
565 ReadFrom(i, address);
566 seqNo = i.ReadNtohU32();
567 m_unreachableDstSeqNo.insert(std::make_pair(address, seqNo));
568 }
569
570 uint32_t dist = i.GetDistanceFrom(start);
571 NS_ASSERT(dist == GetSerializedSize());
572 return dist;
573}
574
575void
576RerrHeader::Print(std::ostream& os) const
577{
578 os << "Unreachable destination (ipv4 address, seq. number):";
579 for (auto j = m_unreachableDstSeqNo.begin(); j != m_unreachableDstSeqNo.end(); ++j)
580 {
581 os << (*j).first << ", " << (*j).second;
582 }
583 os << "No delete flag " << (*this).GetNoDelete();
584}
585
586void
588{
589 if (f)
590 {
591 m_flag |= (1 << 0);
592 }
593 else
594 {
595 m_flag &= ~(1 << 0);
596 }
597}
598
599bool
601{
602 return (m_flag & (1 << 0));
603}
604
605bool
607{
608 if (m_unreachableDstSeqNo.find(dst) != m_unreachableDstSeqNo.end())
609 {
610 return true;
611 }
612
613 NS_ASSERT(GetDestCount() < 255); // can't support more than 255 destinations in single RERR
614 m_unreachableDstSeqNo.insert(std::make_pair(dst, seqNo));
615 return true;
616}
617
618bool
619RerrHeader::RemoveUnDestination(std::pair<Ipv4Address, uint32_t>& un)
620{
621 if (m_unreachableDstSeqNo.empty())
622 {
623 return false;
624 }
625 auto i = m_unreachableDstSeqNo.begin();
626 un = *i;
627 m_unreachableDstSeqNo.erase(i);
628 return true;
629}
630
631void
633{
634 m_unreachableDstSeqNo.clear();
635 m_flag = 0;
636 m_reserved = 0;
637}
638
639bool
641{
642 if (m_flag != o.m_flag || m_reserved != o.m_reserved || GetDestCount() != o.GetDestCount())
643 {
644 return false;
645 }
646
647 auto j = m_unreachableDstSeqNo.begin();
648 auto k = o.m_unreachableDstSeqNo.begin();
649 for (uint8_t i = 0; i < GetDestCount(); ++i)
650 {
651 if ((j->first != k->first) || (j->second != k->second))
652 {
653 return false;
654 }
655
656 j++;
657 k++;
658 }
659 return true;
660}
661
662std::ostream&
663operator<<(std::ostream& os, const RerrHeader& h)
664{
665 h.Print(os);
666 return os;
667}
668} // namespace aodv
669} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void WriteU8(uint8_t data)
Definition buffer.h:870
uint32_t ReadNtohU32()
Definition buffer.h:967
void WriteHtonU32(uint32_t data)
Definition buffer.h:922
uint32_t GetDistanceFrom(const Iterator &o) const
Definition buffer.cc:769
Protocol header serialization and deserialization.
Definition header.h:33
Ipv4 addresses are stored in host order in this class.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:397
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Route Error (RERR) Message Format.
uint32_t Deserialize(Buffer::Iterator start) override
uint8_t GetDestCount() const
void Clear()
Clear header.
static TypeId GetTypeId()
Get the type ID.
uint8_t m_reserved
Not used (must be 0)
bool GetNoDelete() const
Get the no delete flag.
uint32_t GetSerializedSize() const override
RerrHeader()
constructor
uint8_t m_flag
No delete flag.
bool operator==(const RerrHeader &o) const
Comparison operator.
void Serialize(Buffer::Iterator i) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetNoDelete(bool f)
Set the no delete flag.
bool AddUnDestination(Ipv4Address dst, uint32_t seqNo)
Add unreachable node address and its sequence number in RERR header.
std::map< Ipv4Address, uint32_t > m_unreachableDstSeqNo
List of Unreachable destination: IP addresses and sequence numbers.
void Print(std::ostream &os) const override
bool RemoveUnDestination(std::pair< Ipv4Address, uint32_t > &un)
Delete pair (address + sequence number) from REER header, if the number of unreachable destinations >...
Route Reply Acknowledgment (RREP-ACK) Message Format.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Serialize(Buffer::Iterator start) const override
void Print(std::ostream &os) const override
static TypeId GetTypeId()
Get the type ID.
uint32_t Deserialize(Buffer::Iterator start) override
bool operator==(const RrepAckHeader &o) const
Comparison operator.
uint32_t GetSerializedSize() const override
uint8_t m_reserved
Not used (must be 0)
Route Reply (RREP) Message Format.
bool GetAckRequired() const
get the ack required flag
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint8_t GetPrefixSize() const
Set the prefix size.
uint32_t Deserialize(Buffer::Iterator start) override
void Print(std::ostream &os) const override
void Serialize(Buffer::Iterator start) const override
void SetHello(Ipv4Address src, uint32_t srcSeqNo, Time lifetime)
Configure RREP to be a Hello message.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetSerializedSize() const override
uint32_t m_dstSeqNo
Destination Sequence Number.
void SetLifeTime(Time t)
Set the lifetime.
void SetAckRequired(bool f)
Set the ack required flag.
void SetPrefixSize(uint8_t sz)
Set the prefix size.
RrepHeader(uint8_t prefixSize=0, uint8_t hopCount=0, Ipv4Address dst=Ipv4Address(), uint32_t dstSeqNo=0, Ipv4Address origin=Ipv4Address(), Time lifetime=MilliSeconds(0))
constructor
Time GetLifeTime() const
Get the lifetime.
Ipv4Address m_dst
Destination IP Address.
uint8_t m_flags
A - acknowledgment required flag.
uint8_t m_hopCount
Hop Count.
uint8_t m_prefixSize
Prefix Size.
bool operator==(const RrepHeader &o) const
Comparison operator.
Ipv4Address m_origin
Source IP Address.
uint32_t m_lifeTime
Lifetime (in milliseconds)
Route Request (RREQ) Message Format.
bool GetUnknownSeqno() const
Get the unknown sequence number flag.
uint32_t m_originSeqNo
Source Sequence Number.
RreqHeader(uint8_t flags=0, uint8_t reserved=0, uint8_t hopCount=0, uint32_t requestID=0, Ipv4Address dst=Ipv4Address(), uint32_t dstSeqNo=0, Ipv4Address origin=Ipv4Address(), uint32_t originSeqNo=0)
constructor
uint8_t m_hopCount
Hop Count.
void SetUnknownSeqno(bool f)
Set the unknown sequence number flag.
void SetGratuitousRrep(bool f)
Set the gratuitous RREP flag.
void SetDestinationOnly(bool f)
Set the Destination only flag.
Ipv4Address m_origin
Originator IP Address.
bool GetDestinationOnly() const
Get the Destination only flag.
uint32_t GetSerializedSize() const override
Ipv4Address m_dst
Destination IP Address.
static TypeId GetTypeId()
Get the type ID.
uint32_t m_requestID
RREQ ID.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Serialize(Buffer::Iterator start) const override
uint8_t m_reserved
Not used (must be 0)
bool GetGratuitousRrep() const
Get the gratuitous RREP flag.
uint32_t m_dstSeqNo
Destination Sequence Number.
uint32_t Deserialize(Buffer::Iterator start) override
bool operator==(const RreqHeader &o) const
Comparison operator.
uint8_t m_flags
|J|R|G|D|U| bit flags, see RFC
uint32_t Deserialize(Buffer::Iterator start) override
TypeHeader(MessageType t=AODVTYPE_RREQ)
constructor
bool operator==(const TypeHeader &o) const
Comparison operator.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Serialize(Buffer::Iterator start) const override
MessageType m_type
type of the message
Definition aodv-packet.h:93
bool m_valid
Indicates if the message is valid.
Definition aodv-packet.h:94
uint32_t GetSerializedSize() const override
static TypeId GetTypeId()
Get the type ID.
MessageType
MessageType enumeration.
Definition aodv-packet.h:37
@ AODVTYPE_RREP
AODVTYPE_RREP.
Definition aodv-packet.h:39
@ AODVTYPE_RREP_ACK
AODVTYPE_RREP_ACK.
Definition aodv-packet.h:41
@ AODVTYPE_RERR
AODVTYPE_RERR.
Definition aodv-packet.h:40
@ AODVTYPE_RREQ
AODVTYPE_RREQ.
Definition aodv-packet.h:38
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
std::ostream & operator<<(std::ostream &os, const TypeHeader &h)
Stream output operator.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.