A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
aodv-packet.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Based on
19  * NS-2 AODV model developed by the CMU/MONARCH group and optimized and
20  * tuned by Samir Das and Mahesh Marina, University of Cincinnati;
21  *
22  * AODV-UU implementation by Erik Nordström of Uppsala University
23  * http://core.it.uu.se/core/index.php/AODV-UU
24  *
25  * Authors: Elena Buchatskaia <borovkovaes@iitp.ru>
26  * Pavel Boyko <boyko@iitp.ru>
27  */
28 #include "aodv-packet.h"
29 #include "ns3/address-utils.h"
30 #include "ns3/packet.h"
31 
32 namespace ns3
33 {
34 namespace aodv
35 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (TypeHeader);
38 
40  m_type (t), m_valid (true)
41 {
42 }
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::aodv::TypeHeader")
48  .SetParent<Header> ()
49  .AddConstructor<TypeHeader> ()
50  ;
51  return tid;
52 }
53 
54 TypeId
56 {
57  return GetTypeId ();
58 }
59 
60 uint32_t
62 {
63  return 1;
64 }
65 
66 void
68 {
69  i.WriteU8 ((uint8_t) m_type);
70 }
71 
72 uint32_t
74 {
76  uint8_t type = i.ReadU8 ();
77  m_valid = true;
78  switch (type)
79  {
80  case AODVTYPE_RREQ:
81  case AODVTYPE_RREP:
82  case AODVTYPE_RERR:
83  case AODVTYPE_RREP_ACK:
84  {
85  m_type = (MessageType) type;
86  break;
87  }
88  default:
89  m_valid = false;
90  }
91  uint32_t dist = i.GetDistanceFrom (start);
92  NS_ASSERT (dist == GetSerializedSize ());
93  return dist;
94 }
95 
96 void
97 TypeHeader::Print (std::ostream &os) const
98 {
99  switch (m_type)
100  {
101  case AODVTYPE_RREQ:
102  {
103  os << "RREQ";
104  break;
105  }
106  case AODVTYPE_RREP:
107  {
108  os << "RREP";
109  break;
110  }
111  case AODVTYPE_RERR:
112  {
113  os << "RERR";
114  break;
115  }
116  case AODVTYPE_RREP_ACK:
117  {
118  os << "RREP_ACK";
119  break;
120  }
121  default:
122  os << "UNKNOWN_TYPE";
123  }
124 }
125 
126 bool
128 {
129  return (m_type == o.m_type && m_valid == o.m_valid);
130 }
131 
132 std::ostream &
133 operator<< (std::ostream & os, TypeHeader const & h)
134 {
135  h.Print (os);
136  return os;
137 }
138 
139 //-----------------------------------------------------------------------------
140 // RREQ
141 //-----------------------------------------------------------------------------
142 RreqHeader::RreqHeader (uint8_t flags, uint8_t reserved, uint8_t hopCount, uint32_t requestID, Ipv4Address dst,
143  uint32_t dstSeqNo, Ipv4Address origin, uint32_t originSeqNo) :
144  m_flags (flags), m_reserved (reserved), m_hopCount (hopCount), m_requestID (requestID), m_dst (dst),
145  m_dstSeqNo (dstSeqNo), m_origin (origin), m_originSeqNo (originSeqNo)
146 {
147 }
148 
150 
151 TypeId
153 {
154  static TypeId tid = TypeId ("ns3::aodv::RreqHeader")
155  .SetParent<Header> ()
156  .AddConstructor<RreqHeader> ()
157  ;
158  return tid;
159 }
160 
161 TypeId
163 {
164  return GetTypeId ();
165 }
166 
167 uint32_t
169 {
170  return 23;
171 }
172 
173 void
175 {
176  i.WriteU8 (m_flags);
177  i.WriteU8 (m_reserved);
178  i.WriteU8 (m_hopCount);
180  WriteTo (i, m_dst);
182  WriteTo (i, m_origin);
184 }
185 
186 uint32_t
188 {
190  m_flags = i.ReadU8 ();
191  m_reserved = i.ReadU8 ();
192  m_hopCount = i.ReadU8 ();
193  m_requestID = i.ReadNtohU32 ();
194  ReadFrom (i, m_dst);
195  m_dstSeqNo = i.ReadNtohU32 ();
196  ReadFrom (i, m_origin);
197  m_originSeqNo = i.ReadNtohU32 ();
198 
199  uint32_t dist = i.GetDistanceFrom (start);
200  NS_ASSERT (dist == GetSerializedSize ());
201  return dist;
202 }
203 
204 void
205 RreqHeader::Print (std::ostream &os) const
206 {
207  os << "RREQ ID " << m_requestID << " destination: ipv4 " << m_dst
208  << " sequence number " << m_dstSeqNo << " source: ipv4 "
209  << m_origin << " sequence number " << m_originSeqNo
210  << " flags:" << " Gratuitous RREP " << (*this).GetGratiousRrep ()
211  << " Destination only " << (*this).GetDestinationOnly ()
212  << " Unknown sequence number " << (*this).GetUnknownSeqno ();
213 }
214 
215 std::ostream &
216 operator<< (std::ostream & os, RreqHeader const & h)
217 {
218  h.Print (os);
219  return os;
220 }
221 
222 void
224 {
225  if (f)
226  m_flags |= (1 << 5);
227  else
228  m_flags &= ~(1 << 5);
229 }
230 
231 bool
233 {
234  return (m_flags & (1 << 5));
235 }
236 
237 void
239 {
240  if (f)
241  m_flags |= (1 << 4);
242  else
243  m_flags &= ~(1 << 4);
244 }
245 
246 bool
248 {
249  return (m_flags & (1 << 4));
250 }
251 
252 void
254 {
255  if (f)
256  m_flags |= (1 << 3);
257  else
258  m_flags &= ~(1 << 3);
259 }
260 
261 bool
263 {
264  return (m_flags & (1 << 3));
265 }
266 
267 bool
269 {
270  return (m_flags == o.m_flags && m_reserved == o.m_reserved &&
272  m_dst == o.m_dst && m_dstSeqNo == o.m_dstSeqNo &&
274 }
275 
276 //-----------------------------------------------------------------------------
277 // RREP
278 //-----------------------------------------------------------------------------
279 
280 RrepHeader::RrepHeader (uint8_t prefixSize, uint8_t hopCount, Ipv4Address dst,
281  uint32_t dstSeqNo, Ipv4Address origin, Time lifeTime) :
282  m_flags (0), m_prefixSize (prefixSize), m_hopCount (hopCount),
283  m_dst (dst), m_dstSeqNo (dstSeqNo), m_origin (origin)
284 {
285  m_lifeTime = uint32_t (lifeTime.GetMilliSeconds ());
286 }
287 
289 
290 TypeId
292 {
293  static TypeId tid = TypeId ("ns3::aodv::RrepHeader")
294  .SetParent<Header> ()
295  .AddConstructor<RrepHeader> ()
296  ;
297  return tid;
298 }
299 
300 TypeId
302 {
303  return GetTypeId ();
304 }
305 
306 uint32_t
308 {
309  return 19;
310 }
311 
312 void
314 {
315  i.WriteU8 (m_flags);
316  i.WriteU8 (m_prefixSize);
317  i.WriteU8 (m_hopCount);
318  WriteTo (i, m_dst);
320  WriteTo (i, m_origin);
322 }
323 
324 uint32_t
326 {
328 
329  m_flags = i.ReadU8 ();
330  m_prefixSize = i.ReadU8 ();
331  m_hopCount = i.ReadU8 ();
332  ReadFrom (i, m_dst);
333  m_dstSeqNo = i.ReadNtohU32 ();
334  ReadFrom (i, m_origin);
335  m_lifeTime = i.ReadNtohU32 ();
336 
337  uint32_t dist = i.GetDistanceFrom (start);
338  NS_ASSERT (dist == GetSerializedSize ());
339  return dist;
340 }
341 
342 void
343 RrepHeader::Print (std::ostream &os) const
344 {
345  os << "destination: ipv4 " << m_dst << " sequence number " << m_dstSeqNo;
346  if (m_prefixSize != 0)
347  {
348  os << " prefix size " << m_prefixSize;
349  }
350  os << " source ipv4 " << m_origin << " lifetime " << m_lifeTime
351  << " acknowledgment required flag " << (*this).GetAckRequired ();
352 }
353 
354 void
356 {
358 }
359 
360 Time
362 {
364  return t;
365 }
366 
367 void
369 {
370  if (f)
371  m_flags |= (1 << 6);
372  else
373  m_flags &= ~(1 << 6);
374 }
375 
376 bool
378 {
379  return (m_flags & (1 << 6));
380 }
381 
382 void
384 {
385  m_prefixSize = sz;
386 }
387 
388 uint8_t
390 {
391  return m_prefixSize;
392 }
393 
394 bool
396 {
397  return (m_flags == o.m_flags && m_prefixSize == o.m_prefixSize &&
398  m_hopCount == o.m_hopCount && m_dst == o.m_dst && m_dstSeqNo == o.m_dstSeqNo &&
399  m_origin == o.m_origin && m_lifeTime == o.m_lifeTime);
400 }
401 
402 void
403 RrepHeader::SetHello (Ipv4Address origin, uint32_t srcSeqNo, Time lifetime)
404 {
405  m_flags = 0;
406  m_prefixSize = 0;
407  m_hopCount = 0;
408  m_dst = origin;
409  m_dstSeqNo = srcSeqNo;
410  m_origin = origin;
411  m_lifeTime = lifetime.GetMilliSeconds ();
412 }
413 
414 std::ostream &
415 operator<< (std::ostream & os, RrepHeader const & h)
416 {
417  h.Print (os);
418  return os;
419 }
420 
421 //-----------------------------------------------------------------------------
422 // RREP-ACK
423 //-----------------------------------------------------------------------------
424 
426  m_reserved (0)
427 {
428 }
429 
431 TypeId
433 {
434  static TypeId tid = TypeId ("ns3::aodv::RrepAckHeader")
435  .SetParent<Header> ()
436  .AddConstructor<RrepAckHeader> ()
437  ;
438  return tid;
439 }
440 
441 TypeId
443 {
444  return GetTypeId ();
445 }
446 
447 uint32_t
449 {
450  return 1;
451 }
452 
453 void
455 {
456  i.WriteU8 (m_reserved);
457 }
458 
459 uint32_t
461 {
463  m_reserved = i.ReadU8 ();
464  uint32_t dist = i.GetDistanceFrom (start);
465  NS_ASSERT (dist == GetSerializedSize ());
466  return dist;
467 }
468 
469 void
470 RrepAckHeader::Print (std::ostream &os ) const
471 {
472 }
473 
474 bool
476 {
477  return m_reserved == o.m_reserved;
478 }
479 
480 std::ostream &
481 operator<< (std::ostream & os, RrepAckHeader const & h )
482 {
483  h.Print (os);
484  return os;
485 }
486 
487 //-----------------------------------------------------------------------------
488 // RERR
489 //-----------------------------------------------------------------------------
491  m_flag (0), m_reserved (0)
492 {
493 }
494 
496 
497 TypeId
499 {
500  static TypeId tid = TypeId ("ns3::aodv::RerrHeader")
501  .SetParent<Header> ()
502  .AddConstructor<RerrHeader> ()
503  ;
504  return tid;
505 }
506 
507 TypeId
509 {
510  return GetTypeId ();
511 }
512 
513 uint32_t
515 {
516  return (3 + 8 * GetDestCount ());
517 }
518 
519 void
521 {
522  i.WriteU8 (m_flag);
523  i.WriteU8 (m_reserved);
524  i.WriteU8 (GetDestCount ());
525  std::map<Ipv4Address, uint32_t>::const_iterator j;
526  for (j = m_unreachableDstSeqNo.begin (); j != m_unreachableDstSeqNo.end (); ++j)
527  {
528  WriteTo (i, (*j).first);
529  i.WriteHtonU32 ((*j).second);
530  }
531 }
532 
533 uint32_t
535 {
537  m_flag = i.ReadU8 ();
538  m_reserved = i.ReadU8 ();
539  uint8_t dest = i.ReadU8 ();
540  m_unreachableDstSeqNo.clear ();
541  Ipv4Address address;
542  uint32_t seqNo;
543  for (uint8_t k = 0; k < dest; ++k)
544  {
545  ReadFrom (i, address);
546  seqNo = i.ReadNtohU32 ();
547  m_unreachableDstSeqNo.insert (std::make_pair (address, seqNo));
548  }
549 
550  uint32_t dist = i.GetDistanceFrom (start);
551  NS_ASSERT (dist == GetSerializedSize ());
552  return dist;
553 }
554 
555 void
556 RerrHeader::Print (std::ostream &os ) const
557 {
558  os << "Unreachable destination (ipv4 address, seq. number):";
559  std::map<Ipv4Address, uint32_t>::const_iterator j;
560  for (j = m_unreachableDstSeqNo.begin (); j != m_unreachableDstSeqNo.end (); ++j)
561  {
562  os << (*j).first << ", " << (*j).second;
563  }
564  os << "No delete flag " << (*this).GetNoDelete ();
565 }
566 
567 void
569 {
570  if (f)
571  m_flag |= (1 << 0);
572  else
573  m_flag &= ~(1 << 0);
574 }
575 
576 bool
578 {
579  return (m_flag & (1 << 0));
580 }
581 
582 bool
584 {
585  if (m_unreachableDstSeqNo.find (dst) != m_unreachableDstSeqNo.end ())
586  return true;
587 
588  NS_ASSERT (GetDestCount () < 255); // can't support more than 255 destinations in single RERR
589  m_unreachableDstSeqNo.insert (std::make_pair (dst, seqNo));
590  return true;
591 }
592 
593 bool
594 RerrHeader::RemoveUnDestination (std::pair<Ipv4Address, uint32_t> & un )
595 {
596  if (m_unreachableDstSeqNo.empty ())
597  return false;
598  std::map<Ipv4Address, uint32_t>::iterator i = m_unreachableDstSeqNo.begin ();
599  un = *i;
600  m_unreachableDstSeqNo.erase (i);
601  return true;
602 }
603 
604 void
606 {
607  m_unreachableDstSeqNo.clear ();
608  m_flag = 0;
609  m_reserved = 0;
610 }
611 
612 bool
614 {
615  if (m_flag != o.m_flag || m_reserved != o.m_reserved || GetDestCount () != o.GetDestCount ())
616  return false;
617 
618  std::map<Ipv4Address, uint32_t>::const_iterator j = m_unreachableDstSeqNo.begin ();
619  std::map<Ipv4Address, uint32_t>::const_iterator k = o.m_unreachableDstSeqNo.begin ();
620  for (uint8_t i = 0; i < GetDestCount (); ++i)
621  {
622  if ((j->first != k->first) || (j->second != k->second))
623  return false;
624 
625  j++;
626  k++;
627  }
628  return true;
629 }
630 
631 std::ostream &
632 operator<< (std::ostream & os, RerrHeader const & h )
633 {
634  h.Print (os);
635  return os;
636 }
637 }
638 }