A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
olsr-header.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INESC Porto
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  * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
19  */
20 
21 #ifndef OLSR_HEADER_H
22 #define OLSR_HEADER_H
23 
24 #include <stdint.h>
25 #include <vector>
26 #include "ns3/header.h"
27 #include "ns3/ipv4-address.h"
28 #include "ns3/nstime.h"
29 
30 
31 namespace ns3 {
32 namespace olsr {
33 
34 double EmfToSeconds (uint8_t emf);
35 uint8_t SecondsToEmf (double seconds);
36 
37 // 3.3. Packet Format
38 //
39 // The basic layout of any packet in OLSR is as follows (omitting IP and
40 // UDP headers):
41 //
42 // 0 1 2 3
43 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
44 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 // | Packet Length | Packet Sequence Number |
46 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 // | Message Type | Vtime | Message Size |
48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 // | Originator Address |
50 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 // | Time To Live | Hop Count | Message Sequence Number |
52 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 // | |
54 // : MESSAGE :
55 // | |
56 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 // | Message Type | Vtime | Message Size |
58 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 // | Originator Address |
60 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 // | Time To Live | Hop Count | Message Sequence Number |
62 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 // | |
64 // : MESSAGE :
65 // | |
66 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 // : :
68 // (etc.)
69 class PacketHeader : public Header
70 {
71 public:
72  PacketHeader ();
73  virtual ~PacketHeader ();
74 
75  void SetPacketLength (uint16_t length)
76  {
77  m_packetLength = length;
78  }
79  uint16_t GetPacketLength () const
80  {
81  return m_packetLength;
82  }
83 
84  void SetPacketSequenceNumber (uint16_t seqnum)
85  {
86  m_packetSequenceNumber = seqnum;
87  }
88  uint16_t GetPacketSequenceNumber () const
89  {
91  }
92 
93 private:
94  uint16_t m_packetLength;
96 
97 public:
98  static TypeId GetTypeId (void);
99  virtual TypeId GetInstanceTypeId (void) const;
100  virtual void Print (std::ostream &os) const;
101  virtual uint32_t GetSerializedSize (void) const;
102  virtual void Serialize (Buffer::Iterator start) const;
103  virtual uint32_t Deserialize (Buffer::Iterator start);
104 };
105 
106 
107 // 0 1 2 3
108 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
109 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 // | Message Type | Vtime | Message Size |
111 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 // | Originator Address |
113 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 // | Time To Live | Hop Count | Message Sequence Number |
115 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 class MessageHeader : public Header
117 {
118 public:
119 
120  enum MessageType {
125  };
126 
127  MessageHeader ();
128  virtual ~MessageHeader ();
129 
130  void SetMessageType (MessageType messageType)
131  {
132  m_messageType = messageType;
133  }
135  {
136  return m_messageType;
137  }
138 
139  void SetVTime (Time time)
140  {
141  m_vTime = SecondsToEmf (time.GetSeconds ());
142  }
143  Time GetVTime () const
144  {
145  return Seconds (EmfToSeconds (m_vTime));
146  }
147 
148  void SetOriginatorAddress (Ipv4Address originatorAddress)
149  {
150  m_originatorAddress = originatorAddress;
151  }
153  {
154  return m_originatorAddress;
155  }
156 
157  void SetTimeToLive (uint8_t timeToLive)
158  {
159  m_timeToLive = timeToLive;
160  }
161  uint8_t GetTimeToLive () const
162  {
163  return m_timeToLive;
164  }
165 
166  void SetHopCount (uint8_t hopCount)
167  {
168  m_hopCount = hopCount;
169  }
170  uint8_t GetHopCount () const
171  {
172  return m_hopCount;
173  }
174 
175  void SetMessageSequenceNumber (uint16_t messageSequenceNumber)
176  {
177  m_messageSequenceNumber = messageSequenceNumber;
178  }
179  uint16_t GetMessageSequenceNumber () const
180  {
182  }
183 
184 // void SetMessageSize (uint16_t messageSize)
185 // {
186 // m_messageSize = messageSize;
187 // }
188 // uint16_t GetMessageSize () const
189 // {
190 // return m_messageSize;
191 // }
192 
193 private:
195  uint8_t m_vTime;
197  uint8_t m_timeToLive;
198  uint8_t m_hopCount;
200  uint16_t m_messageSize;
201 
202 public:
203  static TypeId GetTypeId (void);
204  virtual TypeId GetInstanceTypeId (void) const;
205  virtual void Print (std::ostream &os) const;
206  virtual uint32_t GetSerializedSize (void) const;
207  virtual void Serialize (Buffer::Iterator start) const;
208  virtual uint32_t Deserialize (Buffer::Iterator start);
209 
210  // 5.1. MID Message Format
211  //
212  // The proposed format of a MID message is as follows:
213  //
214  // 0 1 2 3
215  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
216  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217  // | OLSR Interface Address |
218  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219  // | OLSR Interface Address |
220  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221  // | ... |
222  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223  struct Mid
224  {
225  std::vector<Ipv4Address> interfaceAddresses;
226  void Print (std::ostream &os) const;
227  uint32_t GetSerializedSize (void) const;
228  void Serialize (Buffer::Iterator start) const;
229  uint32_t Deserialize (Buffer::Iterator start, uint32_t messageSize);
230  };
231 
232  // 6.1. HELLO Message Format
233  //
234  // 0 1 2 3
235  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
236  //
237  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238  // | Reserved | Htime | Willingness |
239  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240  // | Link Code | Reserved | Link Message Size |
241  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
242  // | Neighbor Interface Address |
243  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
244  // | Neighbor Interface Address |
245  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
246  // : . . . :
247  // : :
248  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
249  // | Link Code | Reserved | Link Message Size |
250  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
251  // | Neighbor Interface Address |
252  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
253  // | Neighbor Interface Address |
254  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
255  // : :
256  // : :
257  // (etc.)
258  struct Hello
259  {
260  struct LinkMessage {
261  uint8_t linkCode;
262  std::vector<Ipv4Address> neighborInterfaceAddresses;
263  };
264 
265  uint8_t hTime;
266  void SetHTime (Time time)
267  {
268  this->hTime = SecondsToEmf (time.GetSeconds ());
269  }
270  Time GetHTime () const
271  {
272  return Seconds (EmfToSeconds (this->hTime));
273  }
274 
275  uint8_t willingness;
276  std::vector<LinkMessage> linkMessages;
277 
278  void Print (std::ostream &os) const;
279  uint32_t GetSerializedSize (void) const;
280  void Serialize (Buffer::Iterator start) const;
281  uint32_t Deserialize (Buffer::Iterator start, uint32_t messageSize);
282  };
283 
284  // 9.1. TC Message Format
285  //
286  // The proposed format of a TC message is as follows:
287  //
288  // 0 1 2 3
289  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
290  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
291  // | ANSN | Reserved |
292  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
293  // | Advertised Neighbor Main Address |
294  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
295  // | Advertised Neighbor Main Address |
296  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297  // | ... |
298  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
299 
300  struct Tc
301  {
302  std::vector<Ipv4Address> neighborAddresses;
303  uint16_t ansn;
304 
305  void Print (std::ostream &os) const;
306  uint32_t GetSerializedSize (void) const;
307  void Serialize (Buffer::Iterator start) const;
308  uint32_t Deserialize (Buffer::Iterator start, uint32_t messageSize);
309  };
310 
311 
312  // 12.1. HNA Message Format
313  //
314  // The proposed format of an HNA-message is:
315  //
316  // 0 1 2 3
317  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
318  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
319  // | Network Address |
320  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
321  // | Netmask |
322  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
323  // | Network Address |
324  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325  // | Netmask |
326  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
327  // | ... |
328  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329 
330  // Note: HNA stands for Host Network Association
331  struct Hna
332  {
333  struct Association
334  {
337  };
338  std::vector<Association> associations;
339 
340  void Print (std::ostream &os) const;
341  uint32_t GetSerializedSize (void) const;
342  void Serialize (Buffer::Iterator start) const;
343  uint32_t Deserialize (Buffer::Iterator start, uint32_t messageSize);
344  };
345 
346 private:
347  struct
348  {
353  } m_message; // union not allowed
354 
355 public:
356 
358  {
359  if (m_messageType == 0)
360  {
362  }
363  else
364  {
366  }
367  return m_message.mid;
368  }
369 
371  {
372  if (m_messageType == 0)
373  {
375  }
376  else
377  {
379  }
380  return m_message.hello;
381  }
382 
383  Tc& GetTc ()
384  {
385  if (m_messageType == 0)
386  {
388  }
389  else
390  {
392  }
393  return m_message.tc;
394  }
395 
397  {
398  if (m_messageType == 0)
399  {
401  }
402  else
403  {
405  }
406  return m_message.hna;
407  }
408 
409 
410  const Mid& GetMid () const
411  {
413  return m_message.mid;
414  }
415 
416  const Hello& GetHello () const
417  {
419  return m_message.hello;
420  }
421 
422  const Tc& GetTc () const
423  {
425  return m_message.tc;
426  }
427 
428  const Hna& GetHna () const
429  {
431  return m_message.hna;
432  }
433 
434 
435 };
436 
437 
438 static inline std::ostream& operator<< (std::ostream& os, const PacketHeader & packet)
439 {
440  packet.Print (os);
441  return os;
442 }
443 
444 static inline std::ostream& operator<< (std::ostream& os, const MessageHeader & message)
445 {
446  message.Print (os);
447  return os;
448 }
449 
450 typedef std::vector<MessageHeader> MessageList;
451 
452 static inline std::ostream& operator<< (std::ostream& os, const MessageList & messages)
453 {
454  os << "[";
455  for (std::vector<MessageHeader>::const_iterator messageIter = messages.begin ();
456  messageIter != messages.end (); messageIter++)
457  {
458  messageIter->Print (os);
459  if (messageIter+1 != messages.end ())
460  os << ", ";
461  }
462  os << "]";
463  return os;
464 }
465 
466 
467 }
468 } // namespace olsr, ns3
469 
470 #endif /* OLSR_HEADER_H */
471 
Protocol header serialization and deserialization.
Definition: header.h:42
void Print(std::ostream &os) const
Definition: olsr-header.cc:424
uint16_t m_messageSequenceNumber
Definition: olsr-header.h:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:295
Ipv4Address GetOriginatorAddress() const
Definition: olsr-header.h:152
Doxygen introspection did not find any typical Config paths.
Definition: olsr-header.h:116
virtual uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:125
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:283
const Hna & GetHna() const
Definition: olsr-header.h:428
static std::ostream & operator<<(std::ostream &os, const PacketHeader &packet)
Definition: olsr-header.h:438
void SetPacketSequenceNumber(uint16_t seqnum)
Definition: olsr-header.h:84
uint32_t Deserialize(Buffer::Iterator start, uint32_t messageSize)
Definition: olsr-header.cc:378
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
const Tc & GetTc() const
Definition: olsr-header.h:422
std::vector< LinkMessage > linkMessages
Definition: olsr-header.h:276
uint16_t GetMessageSequenceNumber() const
Definition: olsr-header.h:179
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: olsr-header.cc:145
virtual void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:137
void SetHopCount(uint8_t hopCount)
Definition: olsr-header.h:166
MessageType GetMessageType() const
Definition: olsr-header.h:134
Time GetVTime() const
Definition: olsr-header.h:143
void SetMessageType(MessageType messageType)
Definition: olsr-header.h:130
iterator in a Buffer instance
Definition: buffer.h:98
void SetVTime(Time time)
Definition: olsr-header.h:139
virtual void Print(std::ostream &os) const
Definition: olsr-header.cc:131
std::vector< Ipv4Address > interfaceAddresses
Definition: olsr-header.h:225
std::vector< Ipv4Address > neighborAddresses
Definition: olsr-header.h:302
void SetTimeToLive(uint8_t timeToLive)
Definition: olsr-header.h:157
uint16_t m_packetSequenceNumber
Definition: olsr-header.h:95
double GetSeconds(void) const
Definition: nstime.h:272
struct ns3::olsr::MessageHeader::@86 m_message
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: olsr-header.cc:246
uint32_t Deserialize(Buffer::Iterator start, uint32_t messageSize)
Definition: olsr-header.cc:307
void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:347
virtual TypeId GetInstanceTypeId(void) const
Definition: olsr-header.cc:177
const Hello & GetHello() const
Definition: olsr-header.h:416
Ipv4Address m_originatorAddress
Definition: olsr-header.h:196
void SetPacketLength(uint16_t length)
Definition: olsr-header.h:75
void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:480
uint8_t GetHopCount() const
Definition: olsr-header.h:170
uint8_t GetTimeToLive() const
Definition: olsr-header.h:161
void Print(std::ostream &os) const
Definition: olsr-header.cc:474
uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:468
virtual void Print(std::ostream &os) const
Definition: olsr-header.cc:208
void SetOriginatorAddress(Ipv4Address originatorAddress)
Definition: olsr-header.h:148
static TypeId GetTypeId(void)
Definition: olsr-header.cc:168
std::vector< Association > associations
Definition: olsr-header.h:338
void Print(std::ostream &os) const
Definition: olsr-header.cc:289
uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:327
std::vector< MessageHeader > MessageList
Definition: olsr-header.h:450
Doxygen introspection did not find any typical Config paths.
Definition: olsr-header.h:69
void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:430
virtual TypeId GetInstanceTypeId(void) const
Definition: olsr-header.cc:119
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
const Mid & GetMid() const
Definition: olsr-header.h:410
uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:418
void Print(std::ostream &os) const
Definition: olsr-header.cc:341
uint32_t Deserialize(Buffer::Iterator start, uint32_t messageSize)
Definition: olsr-header.cc:445
uint8_t SecondsToEmf(double seconds)
Converts a decimal number of seconds to the mantissa/exponent format.
Definition: olsr-header.cc:48
uint16_t GetPacketSequenceNumber() const
Definition: olsr-header.h:88
static TypeId GetTypeId(void)
Definition: olsr-header.cc:110
void SetMessageSequenceNumber(uint16_t messageSequenceNumber)
Definition: olsr-header.h:175
double EmfToSeconds(uint8_t olsrFormat)
Converts a number of seconds in the mantissa/exponent format to a decimal number. ...
Definition: olsr-header.cc:87
virtual uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:183
a unique identifier for an interface.
Definition: type-id.h:49
uint32_t Deserialize(Buffer::Iterator start, uint32_t messageSize)
Definition: olsr-header.cc:492
uint16_t GetPacketLength() const
Definition: olsr-header.h:79
virtual void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:214